转自
http://blog.csdn.net/renhanchi/article/details/71713227
场景分类模型
安装文件github地址: https://github.com/metalbubble/places365
下载后解压缩
也可以直接运行:
- git clone https://github.com/metalbubble/places365
同时在上面地址中选择并下载好你要用的pre_trained model。
1. 安装docker
下载地址: https://www.docker.com/community-edition
Ubuntu安装手册地址:
https://store.docker.com/editions/community/docker-ce-server-ubuntu?tab=description
先安装资源库:
- sudo apt-get -y install \
- apt-transport-https \
- ca-certificates \
- curl
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
- sudo add-apt-repository \
- "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
- $(lsb_release -cs) \
- stable"
- sudo apt-get update
在安装Docker CE
- sudo apt-get -y install docker-ce
最后测试是否安装成功:
- sudo docker run hello-world
测试后把测试image删掉:
- sudo su
- docker images
- docker rmi -f iamge_name or image ID
2. 创建places365_container
- cd places365/docker
打开README.md
我们可以看到这个容器里包含Ubuntu 14.04系统, Caffe和其相关依赖。
执行:
- docker build -t places365_container .
需要一些时间来下载和安装,一共有3.5G多。
选择好的网络prototxt和对应的model复制到docker/models_places 目录下。
搞定后运行:
- docker run places365_container python run_scene.py images/mountains.jpg
这里需要注意一下,可能是哪点我搞错了,我运行上面的命令说找不到places365_container,后来我改了一下。运行下面命令出结果了。
- docker run places365_contrainer python run_scene.py images/mountains.jpg
3.调用本地caffe
我定义本地caffe目录为$ROOT
把Places365文件夹复制到$ROOT目录下
这里有两种版本,一个是用caffe中python自带的classfy.py, 一个是用Places365的run_scene.py。这里我都讲一下。
3.1.使用classfier.py
先修改一下classfy.py
默认的这个程序输出结果是存到一个npy文件中,不在terminal显示,所以需要继续修改让结果在terminal显示出来。
找到这一行:
- mean = np.load(args.mean_file)
在下面加上:
- mean=mean.mean(1).mean(1)
再定位到:
- # Classify
- start = time.time()
- predictions = classifier.predict(inputs, not args.center_only)
- print("Done in %.2f s." % (time.time() - start))
在下面加上:
- imagenet_labels_filename = '../places365/docker/resources/labels.txt' #注意这里要改成你自己labels.txt的路径
- labels = np.loadtxt(imagenet_labels_filename, str, delimiter='\t')
- top_k = predictions.flatten().argsort()[-1:-6:-1]
- for i in np.arange(top_k.size):
- print labels[top_k[i]]
最后把132行的路径改成我们自己的文件路径。
- imagenet_labels_filename = '$ROOT/places365/docker/resources/labels.txt'
3.1.1.生成labels.txt
对比就很容易发现,caffe需要的文件是每行只有类别名就好啦。把places365/docker/resources中的的labels.pkl改一下。
我写了下面的脚本,运行就会自动生成labels.txt啦
- #!/bin/sh
- cp labels.pkl labels.txt
- sed -i 1d labels.txt
- sed -i 731d labels.txt
- for num in {1..365};
- do
- sed -i "/p${num}/d" labels.txt
- done
- sed -i 's/aS//g' labels.txt
- sed -i 's/S//g' labels.txt
- sed -i 's/^.//g' labels.txt
- sed -i 's/.$//g' labels.txt
3.1.2.运行
使用下面命令运行
- cd $ROOT/python
- python classify.py $ROOT/places365/docker/images/mountains.jpg result.npy \
- --model_def $ROOT/places365/docker/models_places/deploy_alexnet_places365.prototxt \
- --pretrained_model $ROOT/places365/docker/models_places/alexnet_places365.caffemodel
3.2使用run_scene.py
先把这个文件复制到$ROOT/python文件夹下面,
然后直接运行:
- python run_scene.py $ROOT/places365/docker/images/mountains.jpg
搞定!