I am training a CNN with 10 classes. The training folder has 40 images per class, and the validation folder has 10 images per class. I have a folder with 100 test imgaes. How do I load them (by using imagedatagenerator) and then make predictions with my trained model?I am getting different predictions everytime I run model.predict() for the same test data. Here is a link to the dataset I am using https://www.kaggle.com/s214316001/datasets214 . Here is my code
import tensorflow as tf
import matplotlib.pyplot as plt
import cv2
import os
import numpy as np
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D,BatchNormalization,Dropout
train = ImageDataGenerator(rescale = 1/255)
train_dataset = train.flow_from_directory('../input/datasets214/train/train',
target_size = (200,200),batch_size = 5,
class_mode = 'categorical')
validation_dataset=train.flow_from_directory('../input/datasets214/validation/validation',
target_size = (200,200),batch_size = 5,
class_mode = 'categorical')
model = Sequential()
model.add(Conv2D(16, (3, 3), input_shape=(200,200,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
model.fit(train_dataset,steps_per_epoch=5,epochs=300,validation_data=validation_dataset)
#prediction
datagen = ImageDataGenerator(rescale=1/255)
gen =datagen.flow_from_directory('../input/datasets214/gnr_test/gnr_test',shuffle = 'False',batch_size=100,
target_size = (200,200),classes = ['test'])
predict = model.predict(gen)
print(' fileID','label')
for file,i in enumerate(gen.filenames):
j = predict[file]
k = list(j).index(max(j))
print( i,k)
CodePudding user response:
in gen you have set shuffle=False