Counting 2,870 Big Data & Machine Learning Frameworks, Toolsets, and Examples...
Suggestion? Feedback? Tweet @stkim1


Your first deep neural network in less than 5 minutes

Big picture: Train a neural net with the keras framework to classify images of circles and squares:

c1 s1 c2 s4 c3 s2 s3

Getting started

1. Install libraries

Go ahead and install everything you need (works on Linux, Mac, and Windows).

2. Train

Run python t to train your network. This might take a minute to finish.

3. Classify

Run python c to classify your images. Press spacebar to see the next image and press q to quit.



Here we create our data using OpenCV: images of cirles and squares with random dimensions.

if cls == 0:, (n_cols/2, n_rows/2), random.randint(10, n_cols/2 - 10), (255), -1)
elif cls == 1:
		side = random.randint(10, n_cols/2 - 10)
		cv2.rectangle(img, (n_cols/2 - side, n_cols/2 - side), (n_cols/2 + side, n_cols/2 + side), (255), -1)

Then we train our neural net with the created data.

model.train_on_batch(imgs_train, label_train)


Every couple iterations we test our neural net and calculate it's accuracy up to this point.

score = model.test_on_batch(imgs_test, labels_test)
print  "Accuracy:{0:.0f}%\n".format( score[1]*100)

The Net

Before we can train and classify we need to create our neural net.

model = Sequential()

Then we just .add the layers we want to have in our neural net. In other words, define our neural net architecture.

model.add(Convolution2D(16, 5, 5, border_mode='valid', input_shape=(n_rows, n_cols, 1)))
model.add(Convolution2D(32, 5, 5))


Here's just a short description of the layers used in this neural net:

  • Convolution2d → extracting local image information
  • Activation → evaluate information relevance
  • MaxPooling2D → image compression
  • Dropout → avoiding bias
  • Flatten → reformat
  • Dense → evaluate global image information (fully connected layer)

And last but not least, this defines how our neural net should learn, which we need for training:

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

If this tutorial got you excited about deep learning I recommend you start of looking into this tutorial or the keras documentation. For those of you who have more time and would like a good read here's a good book .


This tutorial was created by Amelie Froessl, an intern and now deep-learning-expert at Mashgin.

At Mashgin, we're creating better human experiences through visual automation. We work on lots of cool stuff in many industries. If you're interested, drop us a line or check out our hiring page.


This tutorial is licensed under the MIT License. See LICENSE for more details. Feel free to clone, fork, or use any parts of the tutorial as long as they are attributed.