【caffe】用训练好的imagenet模型分类图像

来源:互联网 发布:淘宝旺铺智能版退出 编辑:程序博客网 时间:2024/04/27 17:31

因为毕设需要,我首先是用ffmpeg抽取某个宠物视频的关键帧,然后用caffe对这个关键帧中的物体进行分类。

1.抽取关键帧的命令:

E:\graduation design\FFMPEG\bin>ffmpeg -i .\3.mp4 -vf select='eq(pict_type\,I)',setpts='N/(25*TB)' .\%09d.jpg

2.用python编写脚本,利用在imagenet上训练的模型分类视频帧中的物体。

抽取得到的视频关键帧都存放在文件夹"/home/sunshineatnoon/Downloads/dogs/dogs/"中,利用python的walk函数遍历文件夹中的图像并分类。

代码如下:

复制代码
 1 import numpy as np 2 import matplotlib.pyplot as plt 3 import os 4  5 caffe_root = '/home/sunshineatnoon/Downloads/caffe/' 6 import sys 7 sys.path.insert(0,caffe_root+'python') 8  9 import caffe10 11 MODEL_FILE = caffe_root+'models/bvlc_reference_caffenet/deploy.prototxt'12 PRETRAINED = caffe_root+'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'13 14 #cpu模式15 caffe.set_mode_cpu()16 #定义使用的神经网络模型17 net = caffe.Classifier(MODEL_FILE, PRETRAINED,18                mean=np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy').mean(1).mean(1),19                channel_swap=(2,1,0),20                raw_scale=255,21                image_dims=(256, 256))22 imagenet_labels_filename = caffe_root + 'data/ilsvrc12/synset_words.txt'23 labels = np.loadtxt(imagenet_labels_filename, str, delimiter='\t')24 25 #对目标路径中的图像,遍历并分类26 for root,dirs,files in os.walk("/home/sunshineatnoon/Downloads/dogs/dogs/"):27     for file in files:28         #加载要分类的图片29         IMAGE_FILE = os.path.join(root,file).decode('gbk').encode('utf-8');30         input_image = caffe.io.load_image(IMAGE_FILE)31         32         #预测图片类别33         prediction = net.predict([input_image])34         print 'predicted class:',prediction[0].argmax()35 36         # 输出概率最大的前5个预测结果37         top_k = net.blobs['prob'].data[0].flatten().argsort()[-1:-6:-1]38         print labels[top_k]
复制代码

一张图像的分类结果如下图所示:

分类结果:

这里不得不感叹下caffe和神经网络的强大,尽管视频帧的分辨率已经这么低了,还是在前5个预测中得到了正确的分类:corgi

还有一张特别惊讶的:

分类结果:

这样都能检测出giant panda和cat,太牛了!

0 0
原创粉丝点击