Demo entry 6358907

Training Script

   

Submitted by anonymous on Apr 27, 2017 at 01:16
Language: Python. Code size: 3.6 kB.

#### Author: Georgios Karapanagos ####
# Training script for 2-node classification network
#
# For references see below:
"""
Based on the tflearn example located here:
https://github.com/tflearn/tflearn/blob/master/examples/images/convnet_cifar10.py

Loosely following Adam Geitgey's suggested network architucre, found at:
https://medium.com/@ageitgey/machine-learning-is-fun-part-3-deep-learning-and-convolutional-neural-networks-f40359318721
"""

# # -*- coding: utf-8 -*-
from __future__ import division, print_function, absolute_import
import tensorflow as tf
# Import tflearn and some helpers
from tflearnmaster.tflearn.models import dnn
from tflearnmaster.tflearn.data_utils import shuffle, to_categorical
from tflearnmaster.tflearn.layers.core import input_data, dropout, fully_connected
from tflearnmaster.tflearn.layers.conv import conv_2d, max_pool_2d
from tflearnmaster.tflearn.layers.estimator import regression
from tflearnmaster.tflearn.data_preprocessing import ImagePreprocessing
from tflearnmaster.tflearn.data_augmentation import ImageAugmentation
import util_helpers
import time

start_time = time.time()

# Load the data set
trainSize = 1775
testSize = 443
size = 32
(X, Y), (X_test, Y_test) = util_helpers.prepMat(trainSize, testSize, 80 ,45)

# Convert to 2 classes boolean array
Y = to_categorical(Y, 2)
Y_test = to_categorical(Y_test, 2)

# Shuffle the data
X, Y = shuffle(X, Y)

# Make sure the data is normalized
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

# Populate the dataset by flipping, rotating and blurring
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=25.)
img_aug.add_random_blur(sigma_max=3.)

# Definition of our Network Architecture
# Input is a 32x32 image with 3 color channels (red, green and blue)
network = input_data(shape=[None, 80, 45, 3],
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug,
                     name="input_node")
network = conv_2d(network, 32, 3, activation='relu')
network = max_pool_2d(network, 2)
network = conv_2d(network, 64, 3, activation='relu')
network = conv_2d(network, 64, 3, activation='relu')
network = max_pool_2d(network, 2)

# Fully-connected 512 node neural network
network = fully_connected(network, 512, activation='relu')

# Drop a percentage to reduce over-fitting
network = dropout(network, 0.15)

# Final 2-node network that predicts output of '0' or '1' for two classes 
network = fully_connected(network, 2, activation='softmax', name="out")
	
# Set parameters for training method
network = regression(network, optimizer='adam',
                     loss='categorical_crossentropy',
                     learning_rate=0.001)

# Initialize DNN model
model = dnn.DNN(network, tensorboard_verbose=0, checkpoint_path='newDemo_checkpoint')

train_time = time.time()

# Train the model, by fitting data for n_epoch steps
model.fit(X, Y, n_epoch=50, shuffle=True, validation_set=(X_test, Y_test),
          show_metric=True, batch_size=96,
          snapshot_epoch=True,
          run_id='det_cnn')

# Export trained model to file
model.save("newDemo_checkpoint")
util_helpers.save_graph(model.session,"output","newDemo_checkpoint","checkpoint_state","input_graph.pb","output_graph.pb")


print("--- %s seconds training ---" % (time.time() - train_time))
print("--- %s seconds total ---" % (time.time() - start_time))

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).