Skip to content

Instantly share code, notes, and snippets.

@dniku
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save dniku/8bb78417edea86df746c to your computer and use it in GitHub Desktop.

Select an option

Save dniku/8bb78417edea86df746c to your computer and use it in GitHub Desktop.
Loading external digit images for pylearn2 convnet example
import cv2, numpy as np
import theano
from pylearn2.utils import serial
def load_pl2_recognizer(model_path):
model = serial.load(model_path)
X = model.get_input_space().make_theano_batch()
Y = model.fprop(X)
recognize = theano.function([X], Y)
return recognize
pl2recognizer = load_pl2_recognizer('C:\\model.pkl')
def recognize_digit(digit, probabilities=False):
# Assuming 28x28 uint8 white digit with black background
digit = cv2.normalize(digit, alpha=0.0, beta=1.0, norm_type=cv2.cv.CV_MINMAX, dtype=cv2.cv.CV_64F)
digit = digit.reshape(-1, 28, 28, 1)
ans = pl2recognizer(digit)
if not probabilities:
ans = np.argmax(ans, axis=1)
return ans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment