基本上,我是从udemy那里学习机器学习A-Z教程的,在那里我学会了训练自己的卷积神经网络模型,但是并未向我展示如何使用训练后的模型-如何给出输入并获得输出。我在youtube上查看了一些教程,以通过Keras中的cnn传递单个图像,并附带了this教程。当我运行代码时,出现错误
ValueError:检查输入时出错:预期conv2d_3_input具有形状(64,64,3),但数组具有形状(64,64,1)
现在我完全不知道如何处理这个问题。如果有人可以告诉我在ive培训了我的网络之后该怎么办以及如何解决整个问题,那么请衷心地感谢ive尝试更改img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
读取颜色,但我认为与此无关
我用来训练卷积网络的代码:
import keras
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
初始化cnn
classifier = Sequential()
第1步-应用卷积
classifier.add(Convolution2D(32,3,3, border_mode = 'same', input_shape=(64,64,3), activation = 'relu'))
应用最大池化
classifier.add(MaxPooling2D(pool_size=(2,2)))
添加另一个卷积层以提高准确性
classifier.add(Convolution2D(32,3,3, border_mode = 'same', activation = 'relu'))
classifier.add(MaxPooling2D(pool_size=(2,2)))
应用展平
classifier.add(Flatten())
第4步-完全连接
classifier.add(Dense(output_dim = 128, activation = 'relu'))
#input
classifier.add(Dense(output_dim = 1, activation = 'sigmoid'))
#output
全部编译
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
#fitting the cnn to the images
from keras.preprocessing.image import ImageDataGenerator
#pixels take values between 0 & 255
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('dataset/training_set', target_size=(64, 64), batch_size=32, class_mode='binary')
test_set = test_datagen.flow_from_directory('dataset/test_set',
target_size=(64, 64),
batch_size=32,
class_mode='binary')
classifier.fit_generator(training_set,
samples_per_epoch = 8000,
nb_epoch = 25,
verbose = 1,
validation_data = test_set,
nb_val_samples = 2000)
classifier.save('my_model.h5')
import cv2
import tensorflow as tf
import keras
from keras.models import Sequential
Categories = ["Dogs","cats"]
def prepare(filepath):
img_size = 64
img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
new_array = cv2.resize(img_array, (img_size,img_size))
return new_array.reshape(-1,img_size,img_size, 1)
classifier = keras.models.load_model('my_model.h5')
prediction = classifier.predict([prepare('/Users/m.zain/Documents/machine learning-2019 dps/Machine Learning A-Z Template Folder/Part 8 - Deep Learning/Section 40 - Convolutional Neural Networks (CNN)/dog.jpg')])
print(prediction)
prediction = classifier.predict([prepare('/Users/m.zain/Documents/machine learning-2019 dps/Machine Learning A-Z Template Folder/Part 8 - Deep Learning/Section 40 - Convolutional Neural Networks (CNN)/dog.jpg')])
print(Categories[int(prediction[0][0])])
prediction = classifier.predict([prepare('/Users/m.zain/Documents/machine learning-2019 dps/Machine Learning A-Z Template Folder/Part 8 - Deep Learning/Section 40 - Convolutional Neural Networks (CNN)/cat.jpg')])
print(Categories[int(prediction[0][0])])
我希望在执行代码时得到“ dog”作为输出:
prediction = classifier.predict([prepare('/Users/m.zain/Documents/machine learning-2019 dps/Machine Learning A-Z Template Folder/Part 8 - Deep Learning/Section 40 - Convolutional Neural Networks (CNN)/dog.jpg')])
print(Categories[int(prediction[0][0])])
当我运行它时将cat作为输出:
prediction = classifier.predict([prepare('/Users/m.zain/Documents/machine learning-2019 dps/Machine Learning A-Z Template Folder/Part 8 - Deep Learning/Section 40 - Convolutional Neural Networks (CNN)/cat.jpg')])
print(Categories[int(prediction[0][0])])