R-keras ValueError:输出数组是自定义预处理函数中的只读

时间:2018-08-31 15:34:35

标签: python r keras

我有一个自定义图像预处理功能,可用于在模型管道中调用某些python代码。根据R keras文档,自定义预处理函数接受单个值(等级3张量)并返回单个等级3张量。当我在keras::image_data_generatorkeras::flow_images_from_data函数中使用reticulate::iter_next函数并通过图像使用Error in py_iter_next(it, completed) : ValueError: output array is read-only 时,出现错误:

WRITABLE: True

当我进入自定义python代码并逐步检查它生成的numpy数组上的标志时,它的状态为>>> print(img.flags) C_CONTIGUOUS : True F_CONTIGUOUS : False OWNDATA : True WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False UPDATEIFCOPY : False

library(keras)

preprocess_image <- function(x) {
  # Call custom python code/functions
  reticulate::source_python('bb.py', convert = FALSE)

  keras <- reticulate::import('keras')

  image <- keras$preprocessing$image$array_to_img(x)

  # Outsource some Python functions
  bb <- tf_bb(image)
  crop_image <- tf_crop(image, bb)

  # Return an image array
  img <- keras::image_to_array(crop_image, data_format = "channels_last")

  return(img)
}

x <- 'test_image_here.jpg'
img <- image_load(x)

x <- image_to_array(img)
dim(x) <- c(1,dim(x))
dim(x)
gen_images <- image_data_generator(rescale = 1/255,
                                  preprocessing_function = preprocess_image
                                  )

images_dir <- tempfile()
dir.create(images_dir)

images_iter <- flow_images_from_data (
  x=x, y=NULL,
  generator=gen_images,
  batch_size=1,
  save_to_dir=images_dir,
  save_prefix="aug",
  save_format="jpeg"
)

reticulate::iter_next(images_iter)

list.files(images_dir)

任何帮助弄清楚为什么会受到赞赏的帮助!

我知道这不是100%可重复的问题,但这是出现错误时正在使用的代码:

Uncaught TypeError: Cannot read property 'Version' of null
    at Object.callback (VM2084 outlook-web-16.01.js:9)
    at rt (VM2084 outlook-web-16.01.js:9)

0 个答案:

没有答案
相关问题