简体   繁体   English

在R中并行打开图像文件

[英]Opening image files in R in parallel

What I'm trying to do: Open a stack of images using EBImage, process them, and save the processed image in a new file. 我想做的是:使用EBImage打开一堆图像,对其进行处理,然后将处理后的图像保存在新文件中。 I am attempting this in parallel using the package "doParallel" and "foreach". 我正在尝试使用“ doParallel”和“ foreach”包进行并行处理。

The problem: Any time I use more than one processor core for the task, R returns the error: 问题:每当我使用多个处理器内核执行任务时,R都会返回错误:

Error in unserialize(node$con) : error reading from connection
Calls: <Anonymous> ... doTryCatch -> recvData -> recvData.SOCKnode -> unserialize
Execution halted

I do not know how to get any more information on this error. 我不知道如何获取有关此错误的更多信息。 If I try to use the same script but with only one processor core, I don't get any issue. 如果我尝试使用相同的脚本,但仅使用一个处理器内核,则不会有任何问题。

Sample script: 示例脚本:

library(EBImage)
library(foreach)
library(doParallel)

nCores = 1
registerDoParallel(makeCluster(nCores))

img_stack_ids = c("A", "B", "C", "D")
foreach(i = 1:384, .packages = c("EBImage")) %dopar% {
  imgs = tryCatch(readImage(sprintf("/INPUT_IMGS/%s_%s, i, img_stack_ids)), 
                  error = function(e) array(0, dim = c(0,0,0)))

  img_processed = processingFunction(img_list)
  writeImage(img_processed, sprintf("/OUTPUT_IMGS/%s", i))
}

The code works when nCores = 1, it does not when nCores is anything between 1 and the maximum number of cores available. 该代码在nCores = 1时起作用,而在nCores在1到可用的最大核数之间时,则无效。

The system I want this to run on is a virtual machine with 36 cores running CentOS 7. 我要在其上运行的系统是一台具有36个运行CentOS 7内核的虚拟机。

The individual workers SHOULD be accessing unique files based on the file ID so I can't image that it's an issue with file locking or simultaneous reading, unless linux has issues with simultaneous reading and writing to the same directory as well. 各个工作人员应该基于文件ID访问唯一文件,因此我无法想象这是文件锁定或同时读取的问题,除非linux在同时读取和写入同一目录时也有问题。

I'd honestly be happy for a workaround as well as a solution. 老实说,我很高兴能找到解决方法和解决方案。

Thank you! 谢谢!


My session info: R version 3.3.1 (2016-06-21) Platform: x86_64-apple-darwin13.4.0 (64-bit) Running under: OS X 10.11.6 (El Capitan) 我的会话信息:R版本3.3.1(2016-06-21)平台:x86_64-apple-darwin13.4.0(64位)运行在:OS X 10.11.6(El Capitan)下

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods
    base     

other attached packages:
[1] doParallel_1.0.10  iterators_1.0.8    foreach_1.4.3
    ZProjection_0.99.0 EBImage_4.16.0    

loaded via a namespace (and not attached):
 [1] locfit_1.5-9.1      lattice_0.20-34     codetools_0.2-15
     png_0.1-7           fftwtools_0.9-7     tiff_0.1-5
     grid_3.3.1          tools_3.3.1         jpeg_0.1-8
     abind_1.4-5        
[11] rsconnect_0.5       BiocGenerics_0.20.0

Below a reproducible example based on your original code. 下面是基于原始代码的可复制示例。 I was able to successfully run it in parallel on both RedHat Linux (Fedora and CentOS 6.5) and OS X Yosemite (10.10.5). 我能够在RedHat Linux(Fedora和CentOS 6.5)和OS X Yosemite(10.10.5)上成功地并行运行它。 This indicates that your issue might be system- or configuration-specific. 这表明您的问题可能是特定于系统或特定于配置的。

library(EBImage)
library(foreach)
library(doParallel)

nCores = detectCores()
registerDoParallel(makeCluster(nCores))

input_dir = "input_imgs"
output_dir = "output_imgs"

dir.create(input_dir)
dir.create(output_dir)

no_images = 384
img_stack_ids = LETTERS[1:4]

## create sample images
n = 8 # image dimensions

for (i in 1:no_images)
  for (id in img_stack_ids)
    writeImage(Image(runif(n^2), c(n, n)),
               sprintf("%s/%s_%s.png", input_dir, i, id))

## do the actual work
foreach(i = 1:no_images, .packages = c("EBImage")) %dopar% {
  imgs = tryCatch(
    readImage(sprintf("%s/%s_%s.png", input_dir, i, img_stack_ids)), 
    error = function(e) array(0, dim = c(0,0,0))
  )

  ## do the processing

  writeImage(imgs, sprintf("output_imgs/%s.tif", i))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM