【问题标题】:Importing CIFAR - 10 Data set to R将 CIFAR - 10 数据集导入 R
【发布时间】:2015-11-13 20:11:52
【问题描述】:

我正在尝试下载 CIFAR - 10 图像数据集; http://www.cs.toronto.edu/~kriz/cifar.html 在 R 中,但我似乎无法提取文件。我已经尝试了所有三种格式 .bin、.mat 和 python。任何人都可以提供一些关于如何提取它们的建议吗?

非常感谢,威尔

【问题讨论】:

  • 它似乎没有显示任何R支持的数据格式。您是否尝试过使用python接口(如Rpy2)通过R读取python数据格式?
  • 我尝试了一个 python 接口,但是我以前从未使用过该语言,所以我没有设法做到这一点。我尝试使用为 python 提供的示例代码,但无法使其工作。

标签: python r image-processing


【解决方案1】:

与任何事情一样,我想说最简单的方法通常是依靠别人的勤奋。对于这种情况,这意味着寻找已经转换它的其他人。快速的 google 搜索显示 this site(其中包含图像的 R 数据文件)非常适合该方法。

或者,如果您想直接使用 CIFAR-10 数据,这是我刚刚创建的一个脚本,用于从 Alex 在 the original page for cifar-10 上链接到的二进制文件中读取数据:

# Read binary file and convert to integer vectors
# [Necessary because reading directly as integer() 
# reads first bit as signed otherwise]
#
# File format is 10000 records following the pattern:
# [label x 1][red x 1024][green x 1024][blue x 1024]
# NOT broken into rows, so need to be careful with "size" and "n"
#
# (See http://www.cs.toronto.edu/~kriz/cifar.html)
labels <- read.table("cifar-10-batches-bin/batches.meta.txt")
images.rgb <- list()
images.lab <- list()
num.images = 10000 # Set to 10000 to retrieve all images per file to memory

# Cycle through all 5 binary files
for (f in 1:5) {
  to.read <- file(paste("cifar-10-batches-bin/data_batch_", f, ".bin", sep=""), "rb")
  for(i in 1:num.images) {
    l <- readBin(to.read, integer(), size=1, n=1, endian="big")
    r <- as.integer(readBin(to.read, raw(), size=1, n=1024, endian="big"))
    g <- as.integer(readBin(to.read, raw(), size=1, n=1024, endian="big"))
    b <- as.integer(readBin(to.read, raw(), size=1, n=1024, endian="big"))
    index <- num.images * (f-1) + i
    images.rgb[[index]] = data.frame(r, g, b)
    images.lab[[index]] = l+1
  }
  close(to.read)
  remove(l,r,g,b,f,i,index, to.read)
}

# function to run sanity check on photos & labels import
drawImage <- function(index) {
  # Testing the parsing: Convert each color layer into a matrix,
  # combine into an rgb object, and display as a plot
  img <- images.rgb[[index]]
  img.r.mat <- matrix(img$r, ncol=32, byrow = TRUE)
  img.g.mat <- matrix(img$g, ncol=32, byrow = TRUE)
  img.b.mat <- matrix(img$b, ncol=32, byrow = TRUE)
  img.col.mat <- rgb(img.r.mat, img.g.mat, img.b.mat, maxColorValue = 255)
  dim(img.col.mat) <- dim(img.r.mat)

  # Plot and output label
  library(grid)
  grid.raster(img.col.mat, interpolate=FALSE)

  # clean up
  remove(img, img.r.mat, img.g.mat, img.b.mat, img.col.mat)

  labels[[1]][images.lab[[index]]]
}

drawImage(sample(1:(num.images*5), size=1))

希望有帮助!

【讨论】:

    猜你喜欢
    • 2018-06-22
    • 2022-12-04
    • 2017-12-19
    • 1970-01-01
    • 2021-08-29
    • 2017-05-15
    • 1970-01-01
    • 2020-12-16
    • 2020-03-10
    相关资源
    最近更新 更多