1

我只想训练一个分类器来分类来自纽约和洛杉矶的街景图像。

一切都适用于图像输入或数据处理。

但是它在最后一个函数中报告了一个错误。

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
)
4

0 回答 0