David
David

Reputation: 1908

Provide mean pixel values to Caffe's python classify.py

I'd like to test a Caffe model with the Python wrapper:

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

Is there a simple way to give mean_pixel values to the python wrapper? It seems to only support a mean_file argument?

Upvotes: 1

Views: 256

Answers (1)

Anoop K. Prabhu
Anoop K. Prabhu

Reputation: 5635

The code makes use of args.mean_file variable to read a numpy format data to a variable mean. The easiest method will be to bring on a new parser argument named args.mean_pixel which has a single mean value, store it a mean_pixel variable, then create an array called mean which has the same dimensions as that of input data and copy the mean_pixel value to all the elements in the array. The rest of the code will function as normal.

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

The above code segment will try to take a command line argument called mean_pixel.

Replace the code segment:

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

with:

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)

This will make the code to pick the mean_pixel value passed on as an argument, if mean_file is not passed as an argument. The above code will create an array with the dimensions as that of the image and fill it with the mean_pixel value.

The rest of the code needn't be changed.

Upvotes: 1

Related Questions