执行适配生成器时出现Keras(R)错误

时间:2018-05-09 11:14:11

标签: r keras convolutional-neural-network

我在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)
  

错误:       py_call_impl中的错误(可调用,点$ args,点$关键字):         StopIteration:'float'对象不能解释为整数

如果我放置验证设置,它也有另一个错误 布尔(validation_data)。浮动错误..

1 个答案:

答案 0 :(得分:1)

如果没有最小的可重复性示例,很难帮助您。

我猜你在尝试运行

时会遇到这个错误
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需要一个整数。

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

同样如此
model$fit_generator(train_set,
                    steps_per_epoch = 50,
                    epochs = 10)

我建议调查

model %>% 
  fit_generator()

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