keras中的生成器功能,用于混合数据输入(图像,csv)-R

时间:2019-04-21 19:45:39

标签: r image tensorflow keras

我想构建一个生成器函数来输入图像数据以及相应的元数据。有超过20 k张图像,这就是为什么我不能只将它们读入内存。我也尝试过使用flow_from_dataframe,但是在多输入的情况下,它变得越来越复杂。

生成器函数如下:

data_files_generator_train <- function(dir) {


  files <- list.files(dir ,pattern = "jpg$")
  next_file <- 0

  function() {

next_file <<- next_file + 1

if (next_file > length(files))
{next_file <<- 1}

# determine the file name
file <- files[[next_file]]

image <- image_load(file, target_size = c(150,150)) %>%
  image_to_array() %>%
  array_reshape(dim = c(150, 150, 3)) %>%
  imagenet_preprocess_input()
x_image <- image/ 255

x_attr <- paintings_attributes_train %>% filter(path == file) %>% select(-c(target, path)) 

y <- paintings_attributes_train %>% filter(path == file) %>% select(target) 

x_attr <- matrix(x_attr, ncol = 16)
y <- matrix(y)

return(list(list(x_image, x_attr), y)) 
}}

我收到以下错误:

  

py_call_impl(可调用,dots $ args,dots $ keywords)错误:     ValueError:检查模型输入时出错:传递给模型的Numpy数组列表不是模型期望的大小。预计会看到2个数组,但获得了以下1个数组的列表:[array([[[0.00769116,0.17699115,0.1436

模型定义如下:

vision_model <- keras_model_sequential() 

vision_model %>% 
  layer_conv_2d(filters = 64, 
                kernel_size = c(3, 3), 
                activation = 'relu', 
                padding = 'same',
                input_shape = c(150, 150, 3)) %>% 
  layer_max_pooling_2d(pool_size = c(2, 2)) %>% 
  layer_flatten()

# tensor with the output of our vision model:
image_input <- layer_input(shape = c(150, 150, 3))
encoded_image <- image_input %>% vision_model

tabular_input <- layer_input(shape = 16, dtype = 'float32')

mlp_model <- tabular_input %>% 
  layer_dense(
    units              = 16, 
    kernel_initializer = "uniform", 
    activation         = "relu") 

# concatenate the metadata vector and the image vector then
# train a linear regression on it
output <- layer_concatenate(c(mlp_model, encoded_image)) %>% 
  layer_dense(units = 1, activation='linear')


# This is our final model:
vqa_model <- keras_model(inputs = c(tabular_input, image_input), outputs = output)

该问题与Create a mixed data generator (images,csv) in keras有关。

感谢您的帮助!

0 个答案:

没有答案