In [1]:
import caffe
caffe.set_mode_cpu()
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['image.cmap'] = 'gray'
%matplotlib inline
from keras.datasets import mnist
Preprocess dataset.
In [2]:
data = mnist.load_data()[1][0]
# Normalize data.
data = data / data.max()
plt.imshow(data[0, :, :])
Out[2]:
Reshape the data so it fits into the caffe net.
In [3]:
seven = data[0, :, :]
print(seven.shape)
seven = seven[np.newaxis, ...]
print(seven.shape)
In Caffe the inference model is often seperate from the model for training and suffixed wit _deploy
. It ususally has an Input layer
that can be freely filled with data, instead of a Data layer
with a fixed dataset.
Load the trained model and check that the inference is correct.
In [6]:
model_def = 'example_caffe_mnist_model_deploy.prototxt'
model_weights = 'mnist.caffemodel'
deploy_net = caffe.Net(model_def,
model_weights,
caffe.TEST)
deploy_net.blobs
Out[6]:
In [14]:
deploy_net.blobs['data'].data[...] = seven
output = deploy_net.forward()
output['prob'][0].argmax()
Out[14]:
In [ ]:
In [ ]: