层conv2d_1的输入不是符号张量/ Keras / Cloudml / R

时间:2019-03-06 05:44:09

标签: r tensorflow keras google-cloud-ml

我正在使用Keras 2.14版和Tensorflow 1.5版的R接口。

当我在本地计算机上运行以下代码时,它运行没有任何问题。当我在cloudml上运行它时,出现以下错误消息。

我已经检查了论坛,但是由于我对python的了解有限,因此无法翻译到目前为止给R的解决方案。

谢谢您的帮助。

错误消息:

“错误:ValueError:使用非符号张量的输入调用了层conv2d_1。接收的类型:。完整输入:[]。该层的所有输入应为张量。详细的回溯:文件” / usr /在调用 self.assert_input_compatibility(inputs)文件“ / usr / local / lib / python2”中,本地/lib/python2.7/dist-packages/keras/engine/base_layer.py”第414行。 7 / dist-packages / keras / engine / base_layer.py“,第285行,在assert_input_compatibility str(inputs)+'中。该层的所有输入”

这是完整的代码:

original_dataset_dir <- "./train"

base_dir <- "./cats_and_dogs_small"

train_dir <- file.path(base_dir, "train")
validation_dir <- file.path(base_dir, "validation")
test_dir <- file.path(base_dir, "test")

train_cats_dir <- file.path(train_dir, "cats")

train_dogs_dir <- file.path(train_dir, "dogs")

validation_cats_dir <- file.path(validation_dir, "cats")

validation_dogs_dir <- file.path(validation_dir, "dogs")

test_cats_dir <- file.path(test_dir, "cats")

test_dogs_dir <- file.path(test_dir, "dogs")


cat("total training cat images:", length(list.files(train_cats_dir)), "\n")
cat("total training dog images:", length(list.files(train_dogs_dir)), "\n")
cat("total validation cat images:", length(list.files(validation_cats_dir)), "\n")
cat("total validation dog images:", length(list.files(validation_dogs_dir)), "\n")
cat("total test cat images:", length(list.files(test_cats_dir)), "\n")
cat("total test dog images:", length(list.files(test_dogs_dir)), "\n")
list.files(test_dogs_dir)

# now create the model
library(keras)

model <- keras_model_sequential() %>% 
  layer_conv_2d(filters = 32, kernel_size = c(3, 3), activation = "relu",
                input_shape = c(150, 150, 3)) %>% 
  layer_max_pooling_2d(pool_size = c(2, 2)) %>% 
  layer_conv_2d(filters = 64, kernel_size = c(3, 3), activation = "relu") %>% 
  layer_max_pooling_2d(pool_size = c(2, 2)) %>% 
  layer_conv_2d(filters = 128, kernel_size = c(3, 3), activation = "relu") %>% 
  layer_max_pooling_2d(pool_size = c(2, 2)) %>% 
  layer_conv_2d(filters = 128, kernel_size = c(3, 3), activation = "relu") %>% 
  layer_max_pooling_2d(pool_size = c(2, 2)) %>% 
  layer_flatten() %>% 
  layer_dense(units = 512, activation = "relu") %>% 
  layer_dense(units = 1, activation = "sigmoid")

# look at the model
summary(model)

model %>% compile(
  loss = "binary_crossentropy",
  optimizer = optimizer_rmsprop(lr = 1e-4),
  metrics = c("acc")
)

# All images will be rescaled by 1/255
train_datagen <- image_data_generator(rescale = 1/255)
validation_datagen <- image_data_generator(rescale = 1/255)

train_generator <- flow_images_from_directory(
  # This is the target directory
  train_dir,
  # This is the data generator
  train_datagen,
  # All images will be resized to 150x150
  target_size = c(150, 150),
  batch_size = 20,
  # Since we use binary_crossentropy loss, we need binary labels
  class_mode = "binary"
)

validation_generator <- flow_images_from_directory(
  validation_dir,
  validation_datagen,
  target_size = c(150, 150),
  batch_size = 20,
  class_mode = "binary"
)

batch <- generator_next(train_generator)
str(batch)

history <- model %>% fit_generator(
  train_generator,
  steps_per_epoch = 100,
  epochs = 5,
  validation_data = validation_generator,
  validation_steps = 50
)
model %>% save_model_hdf5("cats_and_dogs_small_1.h5")

plot(history)


cml1.R
library(cloudml)
#gcloud_init()

cloudml_train("cats-and-dogs.R")

0 个答案:

没有答案