Author
Contributors
Last Commit
Oct. 9, 2017
Created
Feb. 14, 2017

# 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:

## Getting started

### 1. Install libraries

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

### 2. Train

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

### 3. Classify

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

## Understanding

### Training

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

```if cls == 0:
cv2.circle(img, (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)`

### Testing

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)))

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 .