图像分类器总是给出相同的结果:没有想法
Image Classifier Always Giving Same Results: Out of Ideas
我目前正在开发一个 CNN 来预测两个 类 之间的图像分类:武器,而不是武器。该项目的目的是能够检测图像中是否存在武器 (handgun/rifle)。
我的问题:无论我尝试什么,分类器都预测图像中没有武器。你们能在我的代码中找到可能导致此问题的缺陷吗?
我是计算机科学专业的大四学生,但我对机器学习领域的背景知之甚少。
感谢任何帮助!
# 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.")
免责声明:"ProbablySoap"只是一组不包含武器的图像。
更新
此场景中的输入图像是包含武器的图像。
输出每次都预测 "Not."。
更新 2
这是代码的输出:
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
我认为你的问题:
您使用 缩放 生成了 test_set
:
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')
但是你从来没用过,你用的是test目录下导入的文件,不缩放就用:
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)
这就是预测器无法正确分类稍后导入图像的原因。
希望对您有所帮助。
我目前正在开发一个 CNN 来预测两个 类 之间的图像分类:武器,而不是武器。该项目的目的是能够检测图像中是否存在武器 (handgun/rifle)。
我的问题:无论我尝试什么,分类器都预测图像中没有武器。你们能在我的代码中找到可能导致此问题的缺陷吗?
我是计算机科学专业的大四学生,但我对机器学习领域的背景知之甚少。
感谢任何帮助!
# 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.")
免责声明:"ProbablySoap"只是一组不包含武器的图像。
更新
此场景中的输入图像是包含武器的图像。 输出每次都预测 "Not."。
更新 2 这是代码的输出:
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
我认为你的问题:
您使用 缩放 生成了 test_set
:
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')
但是你从来没用过,你用的是test目录下导入的文件,不缩放就用:
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)
这就是预测器无法正确分类稍后导入图像的原因。
希望对您有所帮助。