I'm new to Deep Learning. I used Keras
and trained a inception_resnet_v2
model for my binary classification application (fire detection). As suggested from my previous question of a non-X
class, I prepared a dataset of 8000 images of fire, and a larger dataset for non-fire (20,000 random images) to make sure the network also sees images of non-fire to perform classification.
I trained the model, but now when trying to load the model and pass images of fire and non-fire ones, it shows same result for all of them:
[[0. 1.]]
[[0. 1.]]
[[0. 1.]]
[[0. 1.]]
[[0. 1.]]
What is going wrong? Am I doing anything wrong? Should I get the result another way?
===============================================
I know it's not SO, but this is my prediction code in case it matters:
from __future__ import print_function
from keras.models import load_model, model_from_json
import cv2, os, glob
import numpy as np
from keras.preprocessing import image
if __name__ == '__main__':
model = load_model('Resnet_26_0.79_model_weights.h5')
os.chdir("test")
for file in glob.glob("*.jpg"):
img_path = file
img = image.load_img(img_path, target_size=(300, 300))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
dictionary = {0: 'non-fire', 1: 'fire'}
results = model.predict(x)
print(results)
predicted_class= np.argmax(results)
acc = 100*results[0][predicted_class]
print("Network prediction is: file: "+ file+", "+dictionary[predicted_class]+", %{:0.2f}".format(acc))
And here is the training:
from keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Dense, Activation, Flatten, Dropout
from keras.models import Sequential, Model
from keras.optimizers import SGD, Adam
from keras.callbacks import ModelCheckpoint
from keras.metrics import binary_accuracy
import os
import json
#==========================
HEIGHT = 300
WIDTH = 300
TRAIN_DIR = "data"
BATCH_SIZE = 8 #8
steps_per_epoch = 1000 #1000
NUM_EPOCHS = 50 #50
lr= 0.00001
#==========================
FC_LAYERS = [1024, 1024]
dropout = 0.5
def build_finetune_model(base_model, dropout, fc_layers, num_classes):
for layer in base_model.layers:
layer.trainable = False
x = base_model.output
x = Flatten()(x)
for fc in fc_layers:
# New FC layer, random init
x = Dense(fc, activation='relu')(x)
x = Dropout(dropout)(x)
# New layer
predictions = Dense(num_classes, activation='sigmoid')(x)
finetune_model = Model(inputs=base_model.input, outputs=predictions)
return finetune_model
train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input, rotation_range=90, horizontal_flip=True, vertical_flip=True
,validation_split=0.2)
train_generator = train_datagen.flow_from_directory(TRAIN_DIR, target_size=(HEIGHT, WIDTH), batch_size=BATCH_SIZE
,subset="training")
#split validation manually
validation_generator = train_datagen.flow_from_directory(TRAIN_DIR, target_size=(HEIGHT, WIDTH), batch_size=BATCH_SIZE,subset="validation")
base_model = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3))
root=TRAIN_DIR
class_list = [ item for item in os.listdir(root) if os.path.isdir(os.path.join(root, item)) ]
print ("class_list: "+str(class_list))
finetune_model = build_finetune_model(base_model, dropout=dropout, fc_layers=FC_LAYERS, num_classes=len(class_list))
adam = Adam(lr)
# change to categorical_crossentropy for multiple classes
finetune_model.compile(adam, loss='binary_crossentropy', metrics=['accuracy'])
filepath="./checkpoints/" + "Resnet_{epoch:02d}_{acc:.2f}" +"_model_weights.h5"
checkpoint = ModelCheckpoint(filepath, monitor=["val_accuracy"], verbose=1, mode='max', save_weights_only=False)
callbacks_list = [checkpoint]
history = finetune_model.fit_generator(train_generator, epochs=NUM_EPOCHS, workers=BATCH_SIZE,
validation_data=validation_generator, validation_steps = validation_generator.samples,
steps_per_epoch=steps_per_epoch,
shuffle=True, callbacks=callbacks_list)