从lmdb数据库中读取在caffe中编码的图像数据

3

我对使用caffe比较新,正在尝试创建最小的工作示例,以后可以进行调整。使用MNIST数据时,我没有遇到任何困难。我下载了图像网数据(ILSVRC12),并使用caffe工具将其转换为lmdb数据库:

$CAFFE_ROOT/build/install/bin/convert_imageset -shuffle -encoded=true top_level_data_dir/ fileNames.txt lmdb_name

创建一个包含编码(jpeg)图像数据的lmdb。原因是编码后,lmdb 大约为64GB,而未编码的大小约为240GB。

我的.prototxt文件描述网络相对简单(由一对内积层组成,大部分借鉴自MNIST示例 - 不追求准确性,只想让其正常工作)。

name: "example"
layer {
  name: "imagenet"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "train-lmdb"
    batch_size: 100
    backend: LMDB
  }
}
layer {
  name: "imagenet"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TEST
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "test-lmdb"
    batch_size: 100
    backend: LMDB
  }
}
layer {
  name: "ip1"
  type: "InnerProduct"
  bottom: "data"
  top: "ip1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 1000
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "relu1"
  type: "ReLU"
  bottom: "ip1"
  top: "ip1"
}
layer {
  name: "ip2"
  type: "InnerProduct"
  bottom: "ip1"
  top: "ip2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 1000
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "accuracy"
  type: "Accuracy"
  bottom: "ip2"
  bottom: "label"
  top: "accuracy"
  include {
    phase: TEST
  }
}
layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "ip2"
  bottom: "label"
  top: "loss"
}

当train-lmdb未编码时,这个.prototxt文件可以正常工作(准确率很低,但是caffe不会崩溃)。然而,如果train-lmdb被编码了,我会得到以下错误:

data_transformer.cpp:239] Check failed: channels == img_channels (3 vs. 1)

问题:在.prototxt文件中有没有必须设置的“标志”,表明train-lmdb是编码图像?(同样的标志可能还必须赋予测试数据层,test-lmdb。)

一些研究:

在谷歌上搜索时,我发现已解决的问题似乎很有前途。然而,将“force_encoded_color”设置为true并没有解决我的问题。

我还发现这个答案非常有用,可以创建lmdb(具体来说,可以启用编码),但是,并没有提到应该做什么,让caffe知道图像被编码了。

1个回答

2
您收到的错误信息:
data_transformer.cpp:239] Check failed: channels == img_channels (3 vs. 1)
意思是caffe数据转换器期望输入具有3个通道(即彩色图像),但得到的图像只有1个img_channels(即灰度图像)。
查看caffe.proto,似乎应该在transformation_param参数中设置该参数:
layer {
  name: "imagenet"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
    scale: 0.00390625
    force_color: true  ##  try this
  }
  data_param {
    source: "train-lmdb"
    batch_size: 100
    backend: LMDB
    force_encoded_color: true  ## cannot hurt...
  }
}

这似乎有效!这是否意味着数据中混有一些灰度图像?我认为/假设它们都是彩色的(我查看的少数几个肯定是彩色的)。也许如果我在创建lmdb时使用了--check_size选项,我就能发现这个问题了?之前我已经将所有图像调整为256x256大小,所以我没有费心进行检查。 - TravisJ
@TravisJ 我不确定 --check_size 是否检查通道数,据我所知它只检查宽度和高度,但我可能错了。 - Shai
我假设大小将是宽度x高度x深度(即通道数)...但我可能错了...对于Caffe非常陌生。 - TravisJ

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