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