Last active
November 6, 2017 16:35
-
-
Save souravsingh/6a29f870e98215cc6ec21d53171b4c53 to your computer and use it in GitHub Desktop.
CNN for Deep Learning Homework
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
import numpy as np | |
import pandas as pd | |
import os | |
import cv2 | |
from tqdm import tqdm | |
from keras.models import Sequential | |
from keras.layers import Dense, Dropout, Flatten, Convolution2D, MaxPooling2D | |
from keras.callbacks import EarlyStopping | |
from keras.utils import to_categorical | |
train = pd.read_csv('train.csv') | |
test = pd.read_csv('test.csv') | |
TRAIN_PATH = 'train_img/' | |
TEST_PATH = 'test_img/' | |
# function to read images as arrays | |
def read_image(img_path): | |
img = cv2.imread(img_path, cv2.IMREAD_COLOR) | |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | |
img = cv2.resize(img, (64,64)) # you can resize to (128,128) or (256,256) | |
return img | |
train_data = [] | |
test_data = [] | |
train_labels = train['label'].values | |
# Load Images | |
for img in tqdm(train['image_id'].values): | |
train_data.append(read_image(TRAIN_PATH + '{}.png'.format(img))) | |
for img in tqdm(test['image_id'].values): | |
test_data.append(read_image(TEST_PATH + '{}.png'.format(img))) | |
# normalize the images | |
x_train = np.array(train_data, np.float32) / 255. | |
x_test = np.array(test_data, np.float32) / 255. | |
# target variable - encoding numeric value | |
label_list = train['label'].tolist() | |
Y_train = {k:v+1 for v,k in enumerate(set(label_list))} | |
y_train = [Y_train[k] for k in label_list] | |
## keras accepts target variable as a ndarray so that we can set one output neuron per class | |
y_train = to_categorical(y_train) | |
## neural net architechture | |
model = Sequential() | |
model.add(Convolution2D(32, (3,3), activation='relu', padding='same',input_shape = (64,64,3))) | |
model.add(Convolution2D(32, (3,3), activation='relu')) | |
model.add(MaxPooling2D(pool_size=(2,2))) | |
model.add(Convolution2D(64, (3,3), activation='relu', padding='same')) | |
model.add(Convolution2D(64, (3,3), activation='relu')) | |
model.add(MaxPooling2D(pool_size=(2,2))) | |
model.add(Dropout(0.25)) | |
model.add(Convolution2D(128, (3,3), activation='relu', padding='same')) | |
model.add(Convolution2D(128, (3,3), activation='relu')) | |
model.add(MaxPooling2D(pool_size=(2,2))) | |
model.add(Dropout(0.25)) | |
model.add(Flatten()) | |
model.add(Dense(128, activation='relu')) | |
model.add(Dense(256, activation='relu')) | |
model.add(Dropout(0.25)) | |
model.add(Dense(y_train.shape[1], activation='softmax')) | |
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy']) | |
early_stops = EarlyStopping(patience=2, monitor='val_acc') | |
model.fit(x_train, y_train, batch_size=32, epochs=25, validation_split=0.3, callbacks=[early_stops]) | |
# make prediction | |
predictions = model.predict(x_test) | |
predictions = np.argmax(predictions, axis= 1) | |
# get predicted labels | |
y_maps = dict() | |
y_maps = {v:k for k, v in Y_train.items()} | |
pred_labels = [y_maps[k] for k in predictions] | |
# make submission | |
sub1 = pd.DataFrame({'image_id':test.image_id, 'label':pred_labels}) | |
sub1.to_csv('submission_one.csv', index=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment