[caffe with docker][02] use the mnist caffemodel with python

2 篇文章 0 订阅
2 篇文章 0 订阅

here I’ll tell you how to use the mnist caffemodel with opencv-python 3.3+

1. transport the vaild files from caffe container to workdir

the valid files are the two files marked with red rectangle.
这里写图片描述
copy them from the caffe container with docker cp command. here the caffe container id is ba11a9c223f2, thus the commands are, the . means current dir:

docker cp ba11a9c223f2:/opt/caffe/examples/mnist/lenet.prototxt .
docker cp ba11a9c223f2:/opt/caffe/examples/mnist/lenet_iter_10000.caffemodel .

then use sz * to transport the two files to windows (we use them in win, for linux users, this step is not needed).
这里写图片描述

2. create test dataset and classes desc file

make some images with pixels 28*28 / black backgroud / white numbers
这里写图片描述
create a classes description file synset_words.txt, with content

0
1
2
3
4
5
6
7
8
9

3. python script: mnist.py

the project file structure is
这里写图片描述

the mnist.py content. Oringinal Article link: Deep learning: How OpenCV’s blobFromImage works

# import the necessary packages
from imutils import paths
import numpy as np
import cv2

# load the class labels from disk
rows = open("synset_words.txt").read().strip().split("\n")
classes = [r[r.find(" ") + 1:].split(",")[0] for r in rows]

# load our serialized model from disk
net = cv2.dnn.readNetFromCaffe("lenet.prototxt",
                               "lenet_iter_10000.caffemodel")

# grab the paths to the input images
imagePaths = sorted(list(paths.list_images("numbers/")))

for imagePath in imagePaths:

    # (1) load the first image from disk, (2) pre-process it by resizing
    # it to 224x224 pixels, and (3) construct a blob that can be passed
    # through the pre-trained network
    image = cv2.imread(imagePath, cv2.IMREAD_GRAYSCALE)
    # resized = cv2.resize(image, (28, 28))
    blob = cv2.dnn.blobFromImage(image, 1, (28, 28), 127)

    # set the input to the pre-trained deep learning network and obtain
    # the output predicted probabilities for each of the 1,000 ImageNet
    # classes
    net.setInput(blob)
    preds = net.forward()

    # sort the probabilities (in descending) order, grab the index of the
    # top predicted label, and draw it on the input image
    idx = np.argsort(preds[0])[::-1][0]
    text = "Label: {}, {:.2f}%".format(classes[idx], preds[0][idx] * 100)
    print imagePath, text

4. test result

run the mnist.py we can get the result below. the result is very good.

C:\Python27\python.exe D:/workspace/caffe/mnist.py
numbers/0.jpg Label: 0, 100.00%
numbers/1.jpg Label: 1, 100.00%
numbers/2.jpg Label: 2, 100.00%
numbers/3.jpg Label: 3, 100.00%
numbers/4.jpg Label: 4, 100.00%
numbers/5.jpg Label: 5, 100.00%
numbers/6.jpg Label: 6, 100.00%
numbers/7.jpg Label: 7, 100.00%
numbers/8.jpg Label: 8, 100.00%
numbers/9.jpg Label: 9, 100.00%
[ INFO:0] Initialize OpenCL runtime...

Process finished with exit code 0
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值