Caffe深度学习入门——python调用caffe训练好的模型检测单帧图片
来源:互联网 发布:河北11选五任6遗漏数据 编辑:程序博客网 时间:2024/05/16 09:57
python调用caffe训练好的cifar10_quick_iter_4000.caffemodel模型检测单帧图片
python直接调用caffe训练好的模型,进行单帧图片检测,并显示检测结果。caffe自带的classify.py文件检测结果直接保存到了foo文件,不能直观显示,这里加入几行显示的代码,方便直接测试查看结果,运行OK,笔记mark。
#!/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 npimport osimport sysimport argparseimport globimport timeimport caffedef 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( "--labels_file", default=os.path.join("../python/classify_words.txt"), help="cifar result words file") parser.add_argument( "--force_grayscale", action='store_true', help="Convert RGB images down to single-channel grayscale versions," + "usefull for single-channel networks like MNIST.") parser.add_argument( "--print_results", action='store_true', help="write output text to stdout rather than serializing to a 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." ) 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) mean = mean.mean(1).mean(1) 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("Predictions: %s" % predictions) #新增显示部分 if args.print_results: scores = predictions.flatten() with open(args.labels_file) as f: labels_df = pd.DataFrame([ { 'synset_id': l.strip().split(' ')[0], 'name': ' '.jion(l.strip().split(' ')[1:]).split(',')[0] } for l in f.readlines()]) labels = labels_df.sort('synset_id')['name'].values indices = (-scores).argsort()[:5] ps = labels[indices] meta = [(p, '%.5f' % scores[i]) for i, p in zip(indices, ps)] print meta #新增显示部分 # Save print("Saving results into %s" % args.output_file) np.save(args.output_file, predictions)if __name__ == '__main__': main(sys.argv)
调用cifar10_quick_iter_4000.caffemodel 模型进行检测
cd ~/caffe/pythonppython classify02.py --model_def ~/caffe/examples/cifar10/cifar10_quick.prototxt --pretrained_model ~/caffe/examples/cifar10/cifar10_quick_iter_4000.caffemodel --labels_file ~/caffe/data/cifar10/cifar10_words.txt --center_only ~/caffe/examples/images/fish-bike.jpg foo
运行结果如下:
直接用caffe默认用的ImageNet的模型进行检测
cd ~/caffe/pythonpython python/classify.py ~/caffe/examples/images/cat.jpg foo
阅读全文
0 0
- Caffe深度学习入门——python调用caffe训练好的模型检测单帧图片
- 【caffe】caffe在windows用训练好的模型对单张图片测试——【caffe学习三】
- Caffe——python接口学习(6):用训练好的模型来分类新的图片
- Python调用已训练好的caffe模型进行分类
- 使用python调用训练好的caffe模型来分类
- caffe学习(六):使用python调用训练好的模型来分类(Ubuntu)
- 【caffe】c++中使用训练好的caffe模型,classification工程生成动态链接库——【caffe学习六】
- caffe的python接口学习:用训练好的模型(caffemodel或者h5)来分类新的图片
- caffe的python接口学习(6):用训练好的模型(caffemodel)来分类新的图片
- caffe的python接口学习(6):用训练好的模型(caffemodel)来分类新的图片
- caffe的python接口学习(6):用训练好的模型(caffemodel)来分类新的图片
- 【深度学习框架Caffe学习与应用】第三课 使用训练好的模型
- 深度学习Caffe实战笔记(21)Windows平台 Faster-RCNN 训练好的模型测试数据
- 使用训练好的caffe模型识别图片
- Caffe用训练好的模型测试图片
- python接口调用已训练好的caffe模型测试分类
- caffe+python 使用训练好的VGG16模型 对 单张图片进行分类,输出置信度
- caffe+python 使用训练好的VGG16模型 对 单张图片进行分类,输出置信度
- RADOS:一种可扩展高可用的PB级存储集群(Ceph)
- Mysql 导入psc 文件时,数据列过多,提示Row size too large(>8126)
- android7.0的ram和rom显示
- softmax VS softmax-loss:数值稳定性
- SpringBoot之Spring Data REST
- Caffe深度学习入门——python调用caffe训练好的模型检测单帧图片
- mongodb重启问题
- zabbix+httpd访问时报403
- Linux下彻底卸载mysql详解
- java实现简单的学生信息管理系统
- 日常2.1--关于EasyUI的坑(datagrid_2)
- on条件与where条件的区别
- Maven构建SpringBoot项目
- 惭愧呀!工作这么多年了,才知道“性能优化”是这么一回事!