Search code examples
tensorflowmachine-learningkerasdeep-learningimage-recognition

Image Classifier Always Giving Same Results: Out of Ideas


I'm currently developing a CNN to predict image classification between two classes: Weapons, and not Weapons. The purpose of this project is to be able to detect whether or not a weapon (handgun/rifle) is present in an image.

My issue: No matter what I try, the classifier predicts that there is no weapon in the image. Can you guys find a flaw in my code that might be causing this issue?

I am senior Computer Science Student, but I have very little background the realm of Machine Learning.

Any help is appreciated!

# Initializing the CNN
classifier = Sequential()

# Step 1 - Convolution
classifier.add(Conv2D(32, (3, 3), input_shape=(64, 64, 3), activation='relu'))

# Step 2 - Pooling
classifier.add(MaxPooling2D(pool_size=(2, 2)))

# Adding a second convolutional layer
classifier.add(Conv2D(32, (3, 3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2, 2)))

# Step 3 - Flattening
classifier.add(Flatten())

# Step 4 - Full connection
classifier.add(Dense(units=128, activation='relu'))
classifier.add(Dense(units=1, activation='sigmoid'))

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

# Part 2 - Fitting the CNN to the images

from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale=1. / 255)

test_datagen = ImageDataGenerator(rescale=1. / 255)

valid_datagen = ImageDataGenerator(rescale=1. / 255)

training_set = train_datagen.flow_from_directory('C:/Users/chill/PycharmProjects/499Actual/venv/data/TrainDataSet/',
                                                 target_size=(64, 64),
                                                 batch_size=29,
                                                 class_mode='binary')

test_set = test_datagen.flow_from_directory('C:/Users/chill/PycharmProjects/499Actual/venv/data/TestDataSet/',
                                            target_size=(64, 64),
                                            batch_size=7,
                                            class_mode='binary')

valid_set = valid_datagen.flow_from_directory('C:/Users/chill/PycharmProjects/499Actual/venv/data/ValidationDataSet/',
                                              target_size=(64, 64),
                                              batch_size=7,
                                              class_mode='binary')

classifier.fit_generator(training_set,
                         steps_per_epoch=348,
                         epochs=1,
                         validation_data=valid_set,
                         validation_steps=100)

# Part 3 - Making new predictions
import numpy as np
from keras.preprocessing import image

# test_image = image.load_img('C:/Users/chill/PycharmProjects/499Actual/venv/data/TestDataSet/ProbablySoap/P1030135.jpg',
#                         target_size=(64, 64))
test_image = image.load_img('C:/Users/chill/PycharmProjects/499Actual/venv/data/TestDataSet/Guns/301.jpeg',
                            target_size=(64, 64))

test_image = image.img_to_array(test_image)

test_image = np.expand_dims(test_image, axis=0)
result = classifier.predict_classes(test_image)
print(result[0][0])
var = training_set.class_indices
if result[0][0] == 1:
    prediction = 1
    print("Gun!")
else:
    prediction = 0
    print("Not.")

Disclaimer: "ProbablySoap" is just the set of images that do not contain weapons.

UPDATE

The input image in this scenario is an image containing a weapon. The output predicts "Not." every time.

UPDATE 2 Here is the output of the code:

Found 348 images belonging to 2 classes.
Found 42 images belonging to 2 classes.
Found 42 images belonging to 2 classes.
Epoch 1/1

  1/348 [..............................] - ETA: 1:15 - loss: 0.6915 - accuracy: 0.5517
  2/348 [..............................] - ETA: 47s - loss: 0.6994 - accuracy: 0.6724 
  3/348 [..............................] - ETA: 38s - loss: 0.7130 - accuracy: 0.6897
  4/348 [..............................] - ETA: 33s - loss: 0.6565 - accuracy: 0.7155
  5/348 [..............................] - ETA: 30s - loss: 0.6496 - accuracy: 0.7103
  6/348 [..............................] - ETA: 28s - loss: 0.6384 - accuracy: 0.7241
  7/348 [..............................] - ETA: 27s - loss: 0.6301 - accuracy: 0.7340

...

346/348 [============================>.] - ETA: 0s - loss: 0.0940 - accuracy: 0.9628
347/348 [============================>.] - ETA: 0s - loss: 0.0937 - accuracy: 0.9629
348/348 [==============================] - 34s 98ms/step - loss: 0.0935 - accuracy: 0.9630 - val_loss: 0.2081 - val_accuracy: 0.9757
0
Not.

Process finished with exit code 0

Solution

  • I think your problem:

    You generated a test_set with scaling:

    test_datagen = ImageDataGenerator(rescale=1. / 255)
    test_set = test_datagen.flow_from_directory('C:/Users/chill/PycharmProjects/499Actual/venv/data/TestDataSet/',
                                                target_size=(64, 64),
                                                batch_size=7,
                                                class_mode='binary')
    

    But you never use it, you use an imported file from the test directory and use it without scaling:

    test_image = image.load_img('C:/Users/chill/PycharmProjects/499Actual/venv/data/TestDataSet/Guns/301.jpeg',
                                target_size=(64, 64))
    test_image = image.img_to_array(test_image)
    test_image = np.expand_dims(test_image, axis=0)
    

    That is why the predictor can not classify the later imported image correctly.

    Hope it helps.