I am using a Keras model with VGGnet as base model for image classification. Code is given below:
from keras import applications
from keras.callbacks import TensorBoard
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers
from keras.models import Sequential, Model
from keras.layers import Dropout, Flatten, Dense
from keras import optimizers
from keras import backend as keras_backend
from keras.callbacks import TensorBoard
from tensorflow.python import debug as tf_debug
inputshape=(224,224,3)
base_model=applications.vgg16.VGG16(weights = "imagenet", include_top=True,input_shape=inputshape)
print (base_model.summary())
new_sequential=Sequential()
print(type(base_model))
for mylayer in base_model.layers:
mylayer.trainable=False#this is done to set the weight as predefined
new_sequential.add(mylayer)
new_sequential.layers.pop()#remove my last layer
new_sequential.add(Dense(output_dim=1,activation='sigmoid'))
new_sequential.compile(optimizer="adam",loss='binary_crossentropy',metrics=['accuracy'])
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
training_data = train_datagen.flow_from_directory(directory="/home/Basic_1/dataset/training_set/",
target_size=(224, 224),
batch_size=8,
class_mode='binary')
test_validation = test_datagen.flow_from_directory("/home/Basic_1/dataset/test_set/",
target_size=(224, 224),
batch_size=8,
class_mode='binary')
cb=TensorBoard(log_dir=("/home/Basic_1"))
new_sequential.fit_generator(generator=training_data,
steps_per_epoch=200,
epochs=1000,
validation_data=test_validation,
validation_steps=5,callbacks=[cb])
from keras.models import load_model
new_sequential.save('saved_vgg.h5')
new_sequential.save_weights('saved_vgg_weights.h5')
import numpy as np
from keras.preprocessing import image
predict_image_path = "/home/Basic_1/dataset/predict/"
import glob
import os
from keras.preprocessing import image
os.getcwd()
os.chdir(predict_image_path)
images = []
images = glob.glob('*')
for imagepath in images:
test_image=image.load_img(path=predict_image_path+'/'+imagepath,target_size=(224,224))
test_image=image.img_to_array(test_image)
test_image=np.expand_dims(test_image,axis=0)
result=new_sequential.predict(test_image)
print(result)
if (result > 0.5):
print("Image path {}, Obstacle : {}".format(imagepath, result))
else:
print("Image path {}, Lane Follow : {}".format(imagepath, result))
Kindly help me in clarifying below doubts.
I have read that the
VGGnet
usesImageNet
for training the model. But in my case I am using a images from a custom class which is not included in theImagenet
. Will the network be able to train based on these custom classes ?I see that, from ```tensorboard`` that the training seems to be normal. However the model fails during inference with new images.
The images in the dataset I am using to train are having
1000x540
dimension. But in thisVGGnet
input shape is(224, 224, 3)
will the images resize during the training?
Updating the question based on the answer given:
Yes, I am taking an pre-trained
VGGnet
which is trained onImageNet
and training the model for my custom class images. (I have used the weights from pre-trained model for convolution layer and removed the FC layer from pre-trained model and used my own FC layer. Is that right what I am doing here ?Yes, training loss is decreasing and accuracy is increasing. Same case with testing set as well. After running for 500 epochs I got the below accuracy and loss.
Training - Accuracy : 0.81 Loss : 0.40
Testing - Accuracy : 0.78 Loss : 0.55
These images are not similar to ImageNet
.
During Inference if I give the images from training set, model is not able to classify it correctly.
Thank you, KK