我只想训练一个分类器,对纽约和洛杉矶的街景图像进行分类。
对于图像输入或数据处理,一切正常。
但是它报告了最后一个函数中的错误。
ValueError:检查目标时出错:预期block5_pool具有4维,但数组的形状为(20,1)
代码在下面。如何解决调试此问题?我跳过了数据清理部分。
library(keras)
conv_base <- application_vgg16(
weights = "imagenet",
include_top = FALSE,
input_shape = c(150, 150, 3)
)
summary(conv_base)
train_datagen = image_data_generator(
rescale = 1/255,
rotation_range = 40,
width_shift_range = 0.2,
height_shift_range = 0.2,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = TRUE,
fill_mode = "nearest"
)
test_datagen <- image_data_generator(rescale = 1/255)
train_generator <- flow_images_from_directory(
train_dir, # Target directory
train_datagen, # Data generator
target_size = c(150, 150), # Resizes all images to 150 * 150
batch_size = 20,
class_mode = "binary" # binary_crossentropy loss for binary labels
)
validation_generator <- flow_images_from_directory(
validation_dir,
test_datagen,
target_size = c(150, 150),
batch_size = 20,
class_mode = "binary"
)
conv_base %>% compile(
loss = "binary_crossentropy",
optimizer = optimizer_rmsprop(lr = 2e-5),
metrics = c("accuracy")
)
history <- conv_base %>% fit_generator(
train_generator,
steps_per_epoch = 60,
epochs = 30,
validation_data = validation_generator,
validation_steps = 50
)