Download the dataset:
https://data.mendeley.com/datasets/4drtyfjtfy/1



The “weather_test.jpeg” test image:

#import all necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from glob import glob
from keras.models import Model
from keras.applications.vgg16 import VGG16, preprocess_input, decode_predictions
from keras import Input
from keras.preprocessing.image import load_img, img_to_array, ImageDataGenerator
from keras.layers import Dense, Flatten
#ensure the layers are not trained. In order words, the weights are used as is
for layer in vgg.layers:
layer.trainable = False
#flatten the last layer and add a fully connected layer as output
hidden = Flatten()(vgg.output)
outputs = Dense(4, activation='softmax')(hidden)
#create the model
model = Model(inputs=vgg.input, outputs=outputs)
#check the model architecture
model.summary()
#compile the model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics='accuracy')
#generate the train and test data
train_data_gen = ImageDataGenerator(rescale=1.0/255,
shear_range=0.5,
zoom_range=0.7,
horizontal_flip=True,
vertical_flip=True)
test_data_gen = ImageDataGenerator(rescale=1.0/255)
train_data = train_data_gen.flow_from_directory('Weather/Train',
target_size=(224, 224),
class_mode='categorical')
test_data = test_data_gen.flow_from_directory('Weather/Test',
target_size=(224, 224),
class_mode='categorical')
#fit the model
history = model.fit_generator(train_data, validation_data=test_data, epochs=5)
#load the image
my_image = load_img('weather_test.jpeg', target_size=(224, 224))
#preprocess the image
my_image = img_to_array(my_image)
my_image = my_image.reshape((1, my_image.shape[0], my_image.shape[1], my_image.shape[2]))
my_image = preprocess_input(my_image)
#make the prediction
prediction = model.predict(my_image)
[np.round(x) for x in prediction]Output:
[array([0., 0., 0., 1.], dtype=float32)]As seen, the fourth class is the predicted class. See the image below for the classes arrangement.
