Search code examples
tensorflowkerasdeep-learningcomputer-visiontransfer-learning

My loss is "nan" and accuracy is " 0.0000e+00 " in Transfer learning: InceptionV3


I am working on transfer learning. My use case is to classify two categories of images. I used InceptionV3 to classify images. When training my model, I am getting nan as loss and 0.0000e+00 as accuracy in every epoch. I am using 20 epochs because my data amount is small: I got 1000 images for training and 100 for testing and per batch 5 records.

from keras.applications.inception_v3 import InceptionV3
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K

# create the base pre-trained model
base_model = InceptionV3(weights='imagenet', include_top=False)

# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
# let's add a fully-connected layer
x = Dense(1024, activation='relu')(x)

x = Dense(512, activation='relu')(x)
x = Dense(32, activation='relu')(x)
# and a logistic layer -- we have 2 classes
predictions = Dense(1, activation='softmax')(x)

# this is the model we will train
model = Model(inputs=base_model.input, outputs=predictions)


for layer in base_model.layers:
    layer.trainable = False

# we chose to train the top 2 inception blocks, i.e. we will freeze
# the first 249 layers and unfreeze the rest:
for layer in model.layers[:249]:
   layer.trainable = False
for layer in model.layers[249:]:
   layer.trainable = True

model.compile(loss="binary_crossentropy", optimizer="adam", 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_set = train_datagen.flow_from_directory(
        'C:/Users/Desktop/Transfer/train/',
        target_size=(64, 64),
        batch_size=5,
        class_mode='binary')

test_set = test_datagen.flow_from_directory(
        'C:/Users/Desktop/Transfer/test/',
        target_size=(64, 64),
        batch_size=5,
        class_mode='binary')

model.fit_generator(
        training_set,
        steps_per_epoch=1000,
        epochs=20,
        validation_data=test_set,
        validation_steps=100)

Solution

  • It sounds like your gradient is exploding. There could be a few reasons for that:

    • Check that your input is generated correctly. For example use the save_to_dir parameter of flow_from_directory
    • Since you have a batch size of 5, fix the steps_per_epoch from 1000 to 1000/5=200
    • Use sigmoid activation instead of softmax
    • Set a lower learning rate in Adam; to do that you need to create the optimizer separately like adam = Adam(0.0001) and pass it in model.compile(..., optimizer=adam)
    • Try VGG16 instead of InceptionV3

    Let us know when you tried all of the above.