我在Keras中拥有以下内容:
train_data = ImageDataGenerator(rescale=1.0/255)
validation_data = ImageDataGenerator(rescale=1.0/255)
test_data = ImageDataGenerator(rescale=1.0/255)
train_features = np.zeros(shape=(number_of_training_samples,16,16,512))
train_labels = np.zeros(shape=(number_of_training_samples))
train_generator = train_data.flow_from_directory(
train_directory,
target_size=(512,512),
batch_size=batch_size,
class_mode='binary',
shuffle=True)
i = 0
for inputs_batch, labels_batch in train_generator:
features_batch = conv_base.predict(inputs_batch)
train_features[i*batch_size:(i+1)*batch_size] = features_batch
train_labels[i*batch_size:(i+1)*batch_size] = labels_batch
i += 1
if i * batch_size >= number_of_training_samples:
break
train_features = np.reshape(train_features, (number_of_training_samples,16*16*512))
validation_features = np.zeros(shape=(number_of_validation_samples,16,16,512))
validation_labels = np.zeros(shape=(number_of_validation_samples))
validation_generator = validation_data.flow_from_directory(
validation_directory,
target_size=(512,512),
batch_size=batch_size,
class_mode='binary',
shuffle=False)
i = 0
for inputs_batch, labels_batch in validation_generator:
features_batch = conv_base.predict(inputs_batch)
validation_features[i*batch_size:(i+1)*batch_size] = features_batch
validation_labels[i*batch_size:(i+1)*batch_size] = labels_batch
i += 1
if i * batch_size >= number_of_validation_samples:
break
validation_features = np.reshape(validation_features, (number_of_validation_samples,16*16*512))
test_generator = test_data.flow_from_directory(
test_directory,
target_size=(512,512),
batch_size=batch_size,
class_mode='binary',
shuffle=False)
# define the Convolutional Neural Network (CNN) model
model = models.Sequential()
model.add(layers.Dense(1024,activation='relu',input_dim=16*16*512))
model.add(layers.Dense(1,activation='sigmoid'))
# compile the model
model.compile(loss='binary_crossentropy',
optimizer=optimizers.Adam(lr=0.01),
metrics=['acc'])
# fit the model to the data
history = model.fit(train_features,
train_labels,
epochs=1,
batch_size=batch_size,
validation_data=(validation_features,validation_labels))
# save the model
model.save('model.h5')
我尝试在一组图像上测试模型,如下所示:
for root, dirs, files in os.walk(test_directory):
for file in files:
img = cv2.imread(root + '/' + file)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.resize(img, (512,512))
img_class = model.predict_classes(img)
prediction = img_class[0]
但是我得到了:
ValueError: Error when checking : expected dense_1_input to have shape (None, 131072) but got array with shape (512, 512)
在线:
img_class = model.predict_classes(img)
那是为什么?
谢谢。