【问题标题】:BVLC/caffe giving same prediction everytimeBVLC/caffe 每次都给出相同的预测
【发布时间】:2015-12-17 08:55:33
【问题描述】:

我正在尝试运行 BVLC/caffe 模型(仅限 CPU)。 我已经完成了所有安装。 当我运行以下命令运行时:

python python/classify.py examples/images/cat.jpg foo

然后给出以下输出:

Classifying 1 inputs.
Done in 2.68 s.
prediction shape: 1000
predicted class: 0
n01440764 tench, Tinca tinca

以上输出对于任何图像都是相同的。

classify.py 文件:

#!/usr/bin/env python
"""
classify.py is an out-of-the-box image classifer callable from the command  line.

By default it configures and runs the Caffe reference ImageNet model.
"""

import numpy as np

import os

import sys

import argparse

import glob

import time

import caffe

def main(argv):
    pycaffe_dir = os.path.dirname(__file__)

    parser = argparse.ArgumentParser()
    # Required arguments: input and output files.
    parser.add_argument(
        "input_file",
        help="Input image, directory, or npy."
    )
parser.add_argument(
    "output_file",
    help="Output npy filename."
)
# Optional arguments.
parser.add_argument(
    "--model_def",
    default=os.path.join(pycaffe_dir,
            "../models/bvlc_reference_caffenet/deploy.prototxt"),
    help="Model definition file."
)
parser.add_argument(
    "--pretrained_model",
    default=os.path.join(pycaffe_dir,
            "../models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel"),
    help="Trained model weights file."
)
parser.add_argument(
    "--gpu",
    action='store_true',
    help="Switch for gpu computation."
)
parser.add_argument(
    "--center_only",
    action='store_true',
    help="Switch for prediction from center crop alone instead of " +
         "averaging predictions across crops (default)."
)
parser.add_argument(
    "--images_dim",
    default='256,256',
    help="Canonical 'height,width' dimensions of input images."
)
parser.add_argument(
    "--mean_file",
    default=os.path.join(pycaffe_dir,
                         'caffe/imagenet/ilsvrc_2012_mean.npy'),
    help="Data set image mean of [Channels x Height x Width] dimensions " +
         "(numpy array). Set to '' for no mean subtraction."
)
parser.add_argument(
    "--input_scale",
    type=float,
    help="Multiply input features by this scale to finish preprocessing."
)
parser.add_argument(
    "--raw_scale",
    type=float,
    default=255.0,
    help="Multiply raw input by this scale before preprocessing."
)
parser.add_argument(
    "--channel_swap",
    default='2,1,0',
    help="Order to permute input channels. The default converts " +
         "RGB -> BGR since BGR is the Caffe default by way of OpenCV."
)
parser.add_argument(
    "--ext",
    default='jpg',
    help="Image file extension to take as input when a directory " +
         "is given as the input file."
)
parser.add_argument(
"--labels_file",
default=os.path.join(pycaffe_dir,"../data/ilsvrc12/synset_words.txt"),help="Readable label definition file."
)
args = parser.parse_args()

image_dims = [int(s) for s in args.images_dim.split(',')]

mean, channel_swap = None, None
if args.mean_file:
    mean = np.load(args.mean_file)
if args.channel_swap:
    channel_swap = [int(s) for s in args.channel_swap.split(',')]

if args.gpu:
    caffe.set_mode_gpu()
    print("GPU mode")
else:
    caffe.set_mode_cpu()
    print("CPU mode")

# Make classifier.
classifier = caffe.Classifier(args.model_def, args.pretrained_model,
        image_dims=image_dims, mean=mean,
        input_scale=args.input_scale, raw_scale=args.raw_scale,
        channel_swap=channel_swap)

# Load numpy array (.npy), directory glob (*.jpg), or image file.
args.input_file = os.path.expanduser(args.input_file)
if args.input_file.endswith('npy'):
    print("Loading file: %s" % args.input_file)
    inputs = np.load(args.input_file)
elif os.path.isdir(args.input_file):
    print("Loading folder: %s" % args.input_file)
    inputs =[caffe.io.load_image(im_f)
             for im_f in glob.glob(args.input_file + '/*.' + args.ext)]
else:
    print("Loading file: %s" % args.input_file)
    inputs = [caffe.io.load_image(args.input_file)]

print("Classifying %d inputs." % len(inputs))

# Classify.
start = time.time()
predictions = classifier.predict(inputs, not args.center_only)
print("Done in %.2f s." % (time.time() - start))
print 'prediction shape:', predictions[0].shape[0]
print 'predicted class:', predictions[0].argmax()

with open(args.labels_file) as f:
    labels = f.readlines()

print labels[predictions[0].argmax()]

# Save
print("Saving results into %s" % args.output_file)
np.save(args.output_file, predictions)



if __name__ == '__main__':
    main(sys.argv)

【问题讨论】:

    标签: python machine-learning deep-learning caffe pycaffe


    【解决方案1】:

    我已经尝试使用类似的classify.py 来执行相同的代码。为什么不试试呢?

    #Import all the nessesary libraries
    import numpy as np
    import matplotlib
    matplotlib.use('Agg')
    import matplotlib.pyplot as plt
    import caffe
    import os
    from os import listdir
    import time
    
    #Set caffe to cpu or gpu mode, either is caffe.set_mode_cpu() or caffe.set_mode_gpu()
    caffe.set_mode_cpu()
    
    #Define variable for location of required files
    MODEL_FILE = '../models/bvlc_reference_caffenet/deploy.prototxt'
    PRETRAINED = '../models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'
    MEAN_FILE = './caffe/imagenet/ilsvrc_2012_mean.npy'
    LABEL_FILE = '../data/ilsvrc12/synset_words.txt'
    
    #Load the BVLC Reference Caffenet models
    net = caffe.Classifier(MODEL_FILE, PRETRAINED,
                           mean=np.load(MEAN_FILE).mean(1).mean(1),
                           channel_swap=(2,1,0),
                           raw_scale=255,
                           image_dims=(256, 256))
    
    '''net = caffe.Classifier(MODEL_FILE, PRETRAINED)
    net.set_channel_swap('data',(2,1,0))
    net.set_raw_scale('data',255)
    net.set_mean('data',np.load(MEAN_FILE))'''
    
    pred_features = []
    
    input_image = caffe.io.load_image('../examples/images/cat.jpg')
    
    '''
    #Let plot out the image
    plt.imshow(input_image)
    plt.savefig('../examples/images/cat.jpg')
    plt.close()
    '''
    
    #Predict class
    start = time.time()
    prediction = net.predict([input_image])
    print("Done in %.2f s." % (time.time() - start))
    print 'prediction shape:', prediction[0].shape[0]
    print 'predicted class:', prediction[0].argmax()
    
    #Predict label
    fi = open(LABEL_FILE)
    labels = fi.readlines()
    print 'predicted name:', labels[prediction[0].argmax()],
    
    '''
    
    #Plot the polygon frequency
    plt.plot(prediction[0])
    plt.savefig('../examples/images/cat.jpg')
    plt.close()
    '''
    

    您只需要修改它,因为此代码不会将输入图像作为命令行参数。

    【讨论】:

      猜你喜欢
      • 2018-09-02
      • 2020-01-27
      • 2020-08-02
      • 1970-01-01
      • 2018-07-08
      • 1970-01-01
      • 2019-01-06
      • 2015-11-21
      • 2023-03-26
      相关资源
      最近更新 更多