简体   繁体   中英

Keras model.fit_generator() gives 0.0% validation accuracy

I have a few training images arranged folder wise, few validation images and a few test images. I'm using image generator because the no. of images are not sufficient. I'm using this code:

height=150
width=150
channels=3
batch_size=32
seed=1337

# Training generator
train_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(train_dir,target_size=(height,width),batch_size=batch_size,seed=seed,class_mode='categorical')

# Test generator
test_datagen = ImageDataGenerator(rescale=1./255)
test_generator = test_datagen.flow_from_directory(test_dir,target_size=(height,width),batch_size=batch_size,seed=seed,class_mode='categorical')

and getting an output:

Found 723 images belonging to 5 classes.

Found 144 images belonging to 5 classes.

And this is my model architecture:

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(150, 150, 3)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

# the model so far outputs 3D feature maps (height, width, features)
model.add(Flatten())  # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(64))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(5))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])

This is the code for .fit_generator() :

history = model.fit_generator(
        train_generator,
        steps_per_epoch=2000 // 64,
        epochs=5,
        validation_data=test_generator,
        validation_steps=800 // 64)

I'm getting an accuracy of 70% after 5 epochs , but unfortunately val_acc and val_loss remains 0.00000e+00 and I'm not quite able to figure it out. Also I have a folder with 20 images, to be predicted. How do I use .predict() function on them? I don't have any .csv file where labels are given. Only the training images are given in separate folders, whose name are basically the class of the images.

First of all fix:

steps_per_epoch = 2000 // 64 validation_steps = 800 // 64

to:

steps_per_epoch = 723 / batch_size validation_steps = 144 / batch_size

However, this isn't the issue here. I don't see a problem in your code. I even ran it on my database and it worked fine. As you've been told here, check that the folders in test_dir and train_dir have the same names.

About predict_gen , read the documentation of Keras. The output is a vector for each validation image. If you want the string labels you can use the classes list of the generator. So something like:

pred_Y = np.argmax(model.predict_generator(valid_gen),axis=1) predicted_labels = [valid_gen.classes[pred_y] for pred_y in pred_Y ]

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