为Caffe的python classify.py提供平均像素值

时间:2016-04-19 17:31:25

标签: pickle caffe pycaffe

我想用Python包装器测试Caffe模型:

python classify.py --model_del ./deploy.prototxt --pretrained_model ./mymodel.caffemodel input.png output

是否有一种简单的方法可以将mean_pixel值赋予python包装器?它似乎只支持mean_file参数?

1 个答案:

答案 0 :(得分:1)

代码使用args.mean_file变量将numpy格式数据读取到变量mean。最简单的方法是引入一个名为args.mean_pixel的新解析器参数,该参数具有单个均值,将其存储为mean_pixel变量,然后创建一个名为mean的数组,该数组具有相同的维度作为输入数据的值,并将mean_pixel值复制到数组中的所有元素。其余代码将正常运行。

parser.add_argument(
    "--mean_pixel",
    type=float,
    default=128.0,
    help="Enter the mean pixel value to be subtracted."
)

上面的代码段将尝试使用名为mean_pixel的命令行参数。

替换代码段:

if args.mean_file:
    mean = np.load(args.mean_file)

使用:

if args.mean_file:
    mean = np.load(args.mean_file)
elif args.mean_pixel:
    mean_pixel = args.mean_pixel
    mean = np.array([image_dims[0],image_dims[1],channels]) #where channels is the number of channels of the image
    mean.fill(mean_pixel)

如果mean_pixel未作为参数传递,这将使代码选择作为参数传递的mean_file值。上面的代码将创建一个尺寸与图像相同的数组,并用mean_pixel值填充。

其余代码无需更改。