gpt4 book ai didi

python - 尽管 CNN 在训练和测试方面具有非常好的准确性,但在随机图像上表现不佳

转载 作者:太空宇宙 更新时间:2023-11-03 19:45:41 24 4
gpt4 key购买 nike

enter code here
from tensorflow import keras


classifier = keras.Sequential()

classifier.add(keras.layers.Convolution2D(16,kernel_size=(3,3),input_shape = (64,64,3),activation =
'relu'))

classifier.add(keras.layers.MaxPooling2D(pool_size = (2, 2)))

classifier.add(keras.layers.Convolution2D(32,kernel_size=(3, 3),activation = 'relu'))
classifier.add(keras.layers.MaxPooling2D(pool_size = (2, 2)))
#classifier.add(keras.layers.BatchNormalization())
classifier.add(keras.layers.Convolution2D(64,kernel_size=(3, 3),activation = 'relu'))
classifier.add(keras.layers.MaxPooling2D(pool_size = (2, 2)))
classifier.add(keras.layers.Dropout(0.2))
classifier.add(keras.layers.Flatten())
classifier.add(keras.layers.Dense(128, activation = 'relu'))
classifier.add(keras.layers.Dense( 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,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory(r'C:\Users\KIIT\Desktop\Deep Learning\dataset2\Training_set',
target_size = (64, 64),
batch_size = 8,
class_mode = 'binary')

test_set = test_datagen.flow_from_directory(r'C:\Users\KIIT\Desktop\Deep Learning\dataset2\Test_set',
target_size = (64, 64),
batch_size = 8,
class_mode = 'binary')

classifier.fit_generator(training_set,
steps_per_epoch =2140,
epochs = 30,
validation_data = test_set,
validation_steps = 90)

import numpy as np
from keras_preprocessing import image
test_image=image.load_img(r'D:\IDM DOWNLOADS\Data Set A-Z DL\Convolutional_Neural_Networks\dataset\single_prediction\shifaface6.jpg',target_size=(64,64))
test_image=image.array_to_img(test_image)
test_image=np.expand_dims(test_image,axis=0)
result=classifier.predict(test_image)
training_set.class_indices
if result[0][0]==1:
prediction='Shifa'
else:
prediction='Rishav'

经过训练和测试,我在测试和训练集中的准确度都接近 100%,但是当我给出图像时Shifa 仍然将其分类为 Rishav,Rishav 的图像被分类为 Rishav。我的数据集包含每个类别的 1070 张图像用于训练,每个类别的 45 张图像用于测试。

最佳答案

不要忘记将新图像的像素范围除以 255 来重新调整像素范围

关于python - 尽管 CNN 在训练和测试方面具有非常好的准确性,但在随机图像上表现不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60156718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com