将CIFAR-10数据集导入R

4
我正试图下载CIFAR-10图像数据集http://www.cs.toronto.edu/~kriz/cifar.html,但在R中似乎无法提取文件。我已经尝试了所有三种格式:.bin、.mat和python。有人能给出一些建议如何提取它们吗?
非常感谢,Will

您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Gaurav
我尝试过使用Python接口,但是我以前从未使用过这种语言,所以我没有成功地完成这个任务。我尝试使用提供的Python示例代码,但无法使其正常工作。 - Will
1个回答

5
与任何事情一样,我认为最简单的方法通常是依靠别人的努力。对于这种情况,这意味着寻找已经转换好的人。一个快速的谷歌搜索可以得到这个网站(其中包含图像的R数据文件),是使用该方法的绝佳候选。
或者,如果你想直接使用CIFAR-10数据,这里有一个我刚刚快速创建的脚本,可以从Alex在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))

希望这能帮到您!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接