执行拟合生成器时 Keras (R) 错误

Keras (R) error while executing fit generator

我在 R 中使用 fit_generator 时出错... 这是我的代码..`

model <- keras_model_sequential()

model %>%
  layer_conv_2d(32, c(3,3), input_shape = c(64, 64, 3)) %>%
  layer_activation("relu") %>%
  layer_max_pooling_2d(pool_size = c(2,2)) %>%
  layer_conv_2d(32, c(3, 3)) %>%
  layer_activation("relu") %>%
  layer_max_pooling_2d(pool_size = c(2, 2)) %>%
  layer_flatten() %>%
  layer_dense(128) %>%
  layer_activation("relu") %>%
  layer_dense(128) %>%
  layer_activation("relu") %>%
  layer_dense(2) %>%
  layer_activation("softmax")

opt <- optimizer_adam(lr = 0.001, decay = 1e-6)

model %>%
  compile(loss = "categorical_crossentropy", optimizer = opt, metrics = "accuracy")

train_gen <- image_data_generator(rescale = 1./255,
                                  shear_range = 0.2,
                                  zoom_range = 0.2,
                                  horizontal_flip = T)

test_gen <- image_data_generator(rescale = 1./255)

train_set = train_gen$flow_from_directory('dataset/training_set',
                                          target_size = c(64, 64),
                                          class_mode = "categorical")

test_set = test_gen$flow_from_directory('dataset/test_set',
                                        target_size = c(64, 64),
                                        batch_size = 32,
                                        class_mode = 'categorical')

model$fit_generator(train_set,
                    steps_per_epoch = 50,
                    epochs = 10)

Error: Error in py_call_impl(callable, dots$args, dots$keywords) : StopIteration: 'float' object cannot be interpreted as an integer

如果我把验证集也有另一个错误 布尔(validation_data)。浮动错误..

如果没有最小的可重现示例,很难为您提供帮助。

我猜你在尝试 运行

时遇到了这个错误
train_set = train_gen$flow_from_directory('dataset/training_set',
                                          target_size = c(64, 64),
                                          class_mode = "categorical")

在这里,您使用 reticulate 而不是 keras(R 包)包装器自己调用 python 函数。这可能有效,但你必须更明确地说明类型并使用 target_size = as.integer(c(64, 64)),因为 python 需要一个整数。

或者,我建议查看 keras 包中包含的 flow_images_from_directory() 函数。


也是如此
model$fit_generator(train_set,
                    steps_per_epoch = 50,
                    epochs = 10)

我建议调查

model %>% 
  fit_generator()

相反,它是 keras 包的一部分。