10.11.2016 Views

Learning Data Mining with Python

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Classifying Objects in Images Using Deep <strong>Learning</strong><br />

Implementing neural networks <strong>with</strong> nolearn<br />

The nolearn package provides wrappers for Lasagne. We lose some of the<br />

fine-tuning that can go <strong>with</strong> building a neural network by hand in Lasagne,<br />

but the code is much more readable and much easier to manage.<br />

The nolearn package implements the normal sorts of complex neural networks you<br />

are likely to want to build. If you want more control than nolearn gives you, you can<br />

revert to using Lasagne, but at the cost of having to manage a bit more of the training<br />

and building process.<br />

To get started <strong>with</strong> nolearn, we are going to reimplement the example we used<br />

in Chapter 8, Beating CAPTCHAs <strong>with</strong> Neural Networks, to predict which letter was<br />

represented in an image. We will recreate the dense neural network we used in<br />

Chapter 8, Beating CAPTCHAs <strong>with</strong> Neural Networks. To start <strong>with</strong>, we need to enter<br />

our dataset building code again in our notebook. For a description of what this code<br />

does, refer to Chapter 8, Beating CAPTCHAs <strong>with</strong> Neural Networks:<br />

import numpy as np<br />

from PIL import Image, ImageDraw, ImageFont<br />

from skimage.transform import resize<br />

from skimage import transform as tf<br />

from skimage.measure import label, regionprops<br />

from sklearn.utils import check_random_state<br />

from sklearn.preprocessing import OneHotEncoder<br />

from sklearn.cross_validation import train_test_split<br />

def create_captcha(text, shear=0, size=(100, 24)):<br />

im = Image.new("L", size, "black")<br />

draw = ImageDraw.Draw(im)<br />

font = ImageFont.truetype(r"Coval.otf", 22)<br />

draw.text((2, 2), text, fill=1, font=font)<br />

image = np.array(im)<br />

affine_tf = tf.AffineTransform(shear=shear)<br />

image = tf.warp(image, affine_tf)<br />

return image / image.max()<br />

def segment_image(image):<br />

labeled_image = label(image > 0)<br />

subimages = []<br />

for region in regionprops(labeled_image):<br />

start_x, start_y, end_x, end_y = region.bbox<br />

subimages.append(image[start_x:end_x,start_y:end_y])<br />

if len(subimages) == 0:<br />

[ 254 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!