Skip to content

Instantly share code, notes, and snippets.

@ih4cku
Last active August 29, 2015 14:12
Show Gist options
  • Save ih4cku/bc00fd7838846ffd0ae4 to your computer and use it in GitHub Desktop.
Save ih4cku/bc00fd7838846ffd0ae4 to your computer and use it in GitHub Desktop.
Predict image with trained model.
name: "LeNet"
input: "data"
input_dim: 1
input_dim: 1
input_dim: 28
input_dim: 28
layers {
name: "conv1"
type: CONVOLUTION
bottom: "data"
top: "conv1"
blobs_lr: 1
blobs_lr: 2
convolution_param {
num_output: 20
kernel_size: 5
stride: 1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layers {
name: "pool1"
type: POOLING
bottom: "conv1"
top: "pool1"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
layers {
name: "conv2"
type: CONVOLUTION
bottom: "pool1"
top: "conv2"
blobs_lr: 1
blobs_lr: 2
convolution_param {
num_output: 50
kernel_size: 5
stride: 1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layers {
name: "pool2"
type: POOLING
bottom: "conv2"
top: "pool2"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
layers {
name: "ip1"
type: INNER_PRODUCT
bottom: "pool2"
top: "ip1"
blobs_lr: 1
blobs_lr: 2
inner_product_param {
num_output: 500
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layers {
name: "relu1"
type: RELU
bottom: "ip1"
top: "ip1"
}
layers {
name: "ip2"
type: INNER_PRODUCT
bottom: "ip1"
top: "ip2"
blobs_lr: 1
blobs_lr: 2
inner_product_param {
num_output: 10
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layers {
name: "prob"
type: SOFTMAX
bottom: "ip2"
top: "prob"
}
import numpy as np
import matplotlib.pyplot as plt
import caffe
from caffe.proto import caffe_pb2
import cPickle
import leveldb
def get_db_image():
db = leveldb.LevelDB('mnist_test_leveldb')
it = db.RangeIter()
key, value = it.next()
datum = caffe_pb2.Datum()
datum.ParseFromString(value)
arr = caffe.io.datum_to_array(datum)
image = arr.astype(np.float32).squeeze()
image /= 255
return key, image
def main():
MODEL_FILE = 'proto/lenet_deploy.prototxt'
PRETRAINED = 'model/lenet_iter_10000.caffemodel'
net = caffe.Net(MODEL_FILE, PRETRAINED)
net.set_phase_test()
net.set_mode_cpu()
IMAGE_FILE, gray = get_db_image()
plt.figure()
plt.imshow(gray)
plt.title(IMAGE_FILE)
im_input = gray[None, None, :, :]
out = net.forward(**{net.inputs[0]: im_input})
probs = out[net.outputs[0]].squeeze()
plt.figure()
plt.plot(probs, '-o')
plt.show()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment