简体   繁体   中英

CNN - Training Accuracy is 1.0 and Validation Accuracy is 1.0. Prediction returning 0.0

I'm trying to build a CNN with a training set of 206 images and test set of 19 images of a single class.

I've build a model of 2 convolution layers and one full connection. I've added dropout to the full connection to avoid over-fitting.

In the first epoch loss is starting at 0.02 and accuracy of 0.88. Validation accuracy is 1.00. And for 49 of the other epochs the training and validation accuracy is remaining at 1.00.

Just to check, I tried to predict using a correct image and a wrong image. Both times predict is returning 0.0

What am I doing wrong?

# Tanjavur Painting Detection

# Part 1 Building CNN
# Importing Keras packages
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.layers import Dropout

# Initializing a CNN
classifier = Sequential()

# Adding Convolution Layer
classifier.add(Convolution2D(32, 3,input_shape = (64, 64, 3), activation = 'relu'))

# Pooling
classifier.add(MaxPooling2D(pool_size = 2))

# Additional Convolutional Layer
classifier.add(Convolution2D(32, 3, activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = 2))

# Flattening
classifier.add(Flatten())

# Full Connection
classifier.add(Dense(256, activation = 'relu'))
classifier.add(Dropout(.4))
classifier.add(Dense(1, activation = 'sigmoid'))

# Compile CNN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

# Part 2 - Fitting Image set to CNN

# IMAGE Preprocessing & then Fitting
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_set = train_datagen.flow_from_directory('Tanjore_Paintings/Tanjavur_Train',
                                            target_size=(64, 64),
                                            batch_size=5,
                                            class_mode='binary')

test_set = test_datagen.flow_from_directory('Tanjore_Paintings/Tanjavur_Test',
                                            target_size=(64, 64),
                                            batch_size=5,
                                            class_mode='binary')
classifier.fit_generator(training_set,
                    steps_per_epoch = 206,
                    epochs = 50,
                    validation_data = test_set,
                    validation_steps = 19)

# Part 3 - Making predictions

import numpy as np
from keras.preprocessing import image
test_image = image.load_img('Tanjore_Paintings/Tanjore_Painting_Test_2.jpg', target_size = (64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)
training_set.class_indices
if result[0][0] == 1:
    prediction = 'Yes'
else:
    prediction = 'No'

Your training and testing data are rescaled by factor of 1/255. But at prediction time you are not rescaling the image. try rescaling the image as follows.

test_image *= (1/255.0)
result = classifier.predict(test_image)

EDIT:

If the training and test dataset have single class then there is no way the model is going to classify input into two classes. Your prediction should be based on the training and test data classes(Your dataset for prediction should come from distribution which is similar to train and test dataset distribution). If you have two classes in train and test dataset then try to assign 0 for one of(first) the classes and 1 for other(second). Then when predicting the classes if the model predicts number less than 0.5 then it is predicting the first class, if the model predicts number greater than or equals to 0.5 then the model is predicting the second class

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM