Created
October 20, 2017 09:31
-
-
Save Shashi18/8e9a8d7735b798f809f4de636e7e36e6 to your computer and use it in GitHub Desktop.
Image classifier in 5 minutes. For explanation click http://www.hellocodings.com/2017/10/build-neural-network-classifier-in-5.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Tue Oct 10 18:44:48 2017 | |
@author: shash | |
""" | |
from keras.preprocessing.image import ImageDataGenerator | |
from keras.models import Sequential | |
from keras.layers import * | |
#from keras.layers import Conv2D, MaxPooling2D | |
#Step 2 Load all imageas as data and testing images | |
''' gathered_data | |
/train | |
/Men | |
/Women | |
/Validate | |
/Men | |
/Women | |
Tree Structure of my data sets | |
''' | |
train_directory = r'gathered_data/train'#Directory of Training Data | |
check_directory = r'gathered_data/validate' #Directory of data that will be checked after training | |
no_of_training_samples = 1410 #No of images of training | |
no_of_validation_samples = 400 #No of images to check after training | |
epoch = 10 | |
batch_size = 20 | |
# data input shape | |
height, width = 150, 150 | |
#Neural Network | |
model = Sequential() | |
model.add(Conv2D(32, (3, 3), input_shape = (width, height, 3))) | |
model.add(Activation('relu')) | |
model.add(Conv2D(32, (3, 3))) | |
model.add(Activation('relu')) | |
model.add(MaxPooling2D(pool_size = (2, 2))) | |
'''Pooling Layers further downsamples the images | |
model.add(Conv2D(128, (3, 3))) | |
model.add(Activation('relu'))''' | |
model.add(Conv2D(64, (3, 3))) | |
model.add(Activation('relu')) | |
model.add(MaxPooling2D(pool_size = (2, 2))) | |
model.add(Flatten()) | |
'''Dense - It deals with 2D tensor and outputs a 2d tensor | |
every node gets connected''' | |
model.add(Dense(64)) | |
model.add(Activation('relu')) | |
model.add(Dropout(0.4)) | |
model.add(Dense(1)) | |
model.add(Activation('sigmoid')) | |
model.compile(loss='binary_crossentropy', optimizer='adam', metrics = ['accuracy']) | |
inp_data = ImageDataGenerator(rescale = 1./255, shear_range = 0.2, zoom_range = 0.2) | |
valid_datax = ImageDataGenerator(rescale = 1. /255, horizontal_flip = True) | |
#Convert the image directory contents in a readable form for Keras using image data generator | |
input_data = inp_data.flow_from_directory(train_directory, target_size=(150, 150), batch_size = 20, class_mode = 'binary') | |
#Convert the validation directory contents into a readable form for Keras using the same image data generator | |
valid_data = valid_datax.flow_from_directory(check_directory, target_size = (150, 150), batch_size = 20, class_mode = 'binary') | |
model.fit_generator(input_data, steps_per_epoch = 70, epochs = 50, | |
validation_data = valid_data, validation_steps = 20) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment