因此,我尝试使用TensorFlow 2编写Cat / Dog Pet分类器的代码。我已经完成了所有图层,并且在尝试拟合模型时出现错误'InvalidArgumentError:形状不兼容:[10,34, 34]与[10,1]'
我不确定自己在哪里做错了。我大约有20张狗的图像和20张猫的图像用于训练,每张图像有10张用于验证。
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import InputLayer, Dense, Conv2D, Flatten, Dropout, MaxPooling2D, Activation
from tensorflow.keras.preprocessing import image
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img_width, img_height = 150,150
train_data_dir = r"E:\Dhivya\Python\petclassification\data\train"
validation_data_dir=r"E:\Dhivya\Python\petclassification\data\test"
nb_train_sample = 40
nb_validation_samples = 20
epochs = 50
batch_size= 10
CATEGORIES = ['cats', 'dogs']
if k.image_data_format()=='channels_first':
input_shape=(3, img_width, img_height)
else:
input_shape=(img_width,img_height,3)
model = Sequential()
model.add(InputLayer(input_shape=input_shape))
model.add(Conv2D(32,(5,5)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64,(5,5)))
model.add(MaxPooling2D(pool_size=(2, 2), strides=2))
model.add(Dense(32))
model.add(Dropout(0.4))
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
model.summary()
model.fit_generator(train_generator,
steps_per_epoch=nb_train_sample,epochs=epochs,
validation_data=validation_generator,
validation_steps=nb_validation_samples)
对于解决此问题的任何帮助,我将不胜感激。我是初学者,学习深度学习