I am trying to make a CNN model that classifies American Sign Language. I already created and trained my model. Now I am trying to predict classes. My prediction set has 7250 unlabeled images however when I do the prediction, the model performs 587250 predictions while I need it to do 7250 predictions. I am providing the code below. What is the reason for this? Am I doing something wrong?
Code Block 1:
predict_set = data.flow_from_directory('/content/gdrive/MyDrive/test_data/')
Output 1:
Found 7250 images belonging to 1 classes.
Code Block 2:
import numpy as np
predictions = model.predict_classes(predict_set)
print(len(predictions),"\n", predictions)
Output 2:
587250
[27 27 27 ... 27 6 12]
Edit:
CNN Model:
model = Sequential()
# First Layer
model.add(Conv2D(filters = 64, kernel_size = (4, 4), input_shape = (64, 64, 3), activation = 'relu'))
model.add(Conv2D(filters = 64, kernel_size = (4, 4), strides = 2, activation = 'relu'))
model.add(Dropout(0.5))
model.add(BatchNormalization(axis = 3, momentum = 0.8))
# Second Layer
model.add(Conv2D(filters = 128, kernel_size = (4, 4), activation = 'relu'))
model.add(Conv2D(filters = 128, kernel_size = (4, 4), strides = 2, activation = 'relu'))
model.add(Dropout(0.5))
model.add(BatchNormalization(axis = 3, momentum = 0.8))
# Third Layer
model.add(Conv2D(filters = 256, kernel_size = (4, 4), activation = 'relu'))
model.add(Conv2D(filters = 256, kernel_size = (4, 4), strides = 2, activation = 'relu'))
model.add(Dropout(0.5))
model.add(BatchNormalization(axis = 3, momentum = 0.8))
# Flattening
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(units = 512, activation = 'relu')) # Hidden Layer
model.add(Dense(units = 29, activation = 'softmax')) # Output Layer
#Compiling the CNN
model.compile(optimizer= 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
model.fit_generator(training_set, steps_per_epoch = 350, epochs = 15, validation_data = test_set, validation_steps = 100)
The batch size of test_set and training_set is 64
Model Summary:
Model: "sequential_24"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_142 (Conv2D) (None, 61, 61, 64) 3136
_________________________________________________________________
conv2d_143 (Conv2D) (None, 29, 29, 64) 65600
_________________________________________________________________
dropout_90 (Dropout) (None, 29, 29, 64) 0
_________________________________________________________________
batch_normalization_69 (Batc (None, 29, 29, 64) 256
_________________________________________________________________
conv2d_144 (Conv2D) (None, 26, 26, 128) 131200
_________________________________________________________________
conv2d_145 (Conv2D) (None, 12, 12, 128) 262272
_________________________________________________________________
dropout_91 (Dropout) (None, 12, 12, 128) 0
_________________________________________________________________
batch_normalization_70 (Batc (None, 12, 12, 128) 512
_________________________________________________________________
conv2d_146 (Conv2D) (None, 9, 9, 256) 524544
_________________________________________________________________
conv2d_147 (Conv2D) (None, 3, 3, 256) 1048832
_________________________________________________________________
dropout_92 (Dropout) (None, 3, 3, 256) 0
_________________________________________________________________
batch_normalization_71 (Batc (None, 3, 3, 256) 1024
_________________________________________________________________
flatten_21 (Flatten) (None, 2304) 0
_________________________________________________________________
dropout_93 (Dropout) (None, 2304) 0
_________________________________________________________________
dense_42 (Dense) (None, 512) 1180160
_________________________________________________________________
dense_43 (Dense) (None, 29) 14877
=================================================================
Total params: 3,232,413
Trainable params: 3,231,517
Non-trainable params: 896
_________________________________________________________________
There is very limited documentation for predict_classes. It may not work if you are using a generator based on the documentation shown below
x: input data, as a Numpy array or list of Numpy arrays (if the model has multiple inputs).
so I do not think it works with a generator. So you will have to use model.predict.
predictions=model.predict(predict_set)
for p in predictions:
class_index=np.argmax(p) # this is the integer value assogned to a class.
if you used a generator to train your model (I will call it train_gen) then you can get the class_indices dictionary which is of the for (class name, class_index) as follows
class_dict=train_gen.class_indices
# reverse the dictionary
for key,value in class_dict.items():
new_dict[value]=key
````
now you can use the new_dict to get the class name with the code below
````
for p in predictions:
class_index=np.argmax(p)
class_name=new_dict[class_index]
````
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.