-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCNN.py
151 lines (119 loc) · 4.91 KB
/
CNN.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- coding: utf-8 -*-
"""
Based on tflearn example:
https://github.com/tflearn/tflearn/blob/master/examples/images/convnet_cifar10.py
"""
from __future__ import division, print_function, absolute_import
import h5py
import tensorflow as tf
import tflearn
from tflearn.data_utils import shuffle, to_categorical
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.estimator import regression
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_augmentation import ImageAugmentation
from zipfile import ZipFile
from io import BytesIO
import PIL.Image
from IPython.display import display
import pickle
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import itemfreq
from sklearn.model_selection import train_test_split
tf.__version__
nwidth = 64
nheight = 64
archive_train = ZipFile("Data/train.zip", 'r')
archive_test = ZipFile("Data/test.zip", 'r')
s = (len(archive_train.namelist()[:])-1, nwidth, nheight,3)
allImage = np.zeros(s)
for i in range(1,len(archive_train.namelist()[:])):
filename = BytesIO(archive_train.read(archive_train.namelist()[i]))
image = PIL.Image.open(filename)
image = image.resize((nwidth, nheight))
image = np.array(image)
image = np.clip(image/255.0, 0.0, 1.0)
allImage[i-1]=image
pickle.dump(allImage, open( "Data/train" + '.p', "wb" ) )
s = (len(archive_test.namelist()[:])-1, nwidth, nheight,3)
allImage = np.zeros(s)
for i in range(1,len(archive_test.namelist()[:])):
filename = BytesIO(archive_test.read(archive_test.namelist()[i]))
image = PIL.Image.open(filename)
image = image.resize((nwidth, nheight))
image = np.array(image)
image = np.clip(image/255.0, 0.0, 1.0)
allImage[i-1]=image
pickle.dump(allImage, open( "Data/test" + '.p', "wb" ) )
train = pickle.load( open( "Data/train.p", "rb" ) )
test = pickle.load( open( "Data/test.p", "rb" ) )
train_label_raw = pd.read_csv('Data/labels.csv')
train_label = train_label_raw["breed"].as_matrix()
train_label = train_label.reshape(train_label.shape[0],1)
print(train_label.shape)
##We convert our labels into binary arrays to give CNN
##Thanks to:
##https://www.kaggle.com/kaggleslayer/simple-convolutional-n-network-with-tensorflow
def matrix_Bin(train_label):
labels_bin = np.array([])
labels_name, labels0 = np.unique(train_label, return_inverse=True)
#print(labels0)
for _, i in enumerate(itemfreq(labels0)[:,0].astype(int)):
labels_bin0 = np.where(labels0 == itemfreq(labels0)[:,0][i], 1.0, 0.0)
labels_bin0 = labels_bin0.reshape(1, labels_bin0.shape[0])
if(labels_bin.shape[0] == 0):
labels_bin = labels_bin0
else:
labels_bin = np.concatenate((labels_bin, labels_bin0), axis=0)
#print("Nber SubVariables {0}".format(itemfreq(labels0)[:,0].shape[0]))
labels_bin = labels_bin.transpose()
#print("Shape : {0}".format(labels_bin.shape))
return labels_name, labels_bin
labels_name, labels_bin = matrix_Bin(train_label = train_label)
"""
exm_img = train[62,:,:,:]
plt.imshow(lum_img)
plt.show()
"""
#num_validation = 0.30
num_validation = 0.15
X_train, X_test, Y_train, Y_test = train_test_split(train, labels_bin, test_size=num_validation, random_state=6)
# Real-time data preprocessing
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()
# Real-time data augmentation
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=25.)
# Convolutional network building
network = input_data(shape=[None, nwidth, nheight, 3],
data_preprocessing=img_prep,
data_augmentation=img_aug)
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)
network = fully_connected(network, 512, activation='relu')
network = dropout(network, 0.5)
network = fully_connected(network, 512, activation='relu')
network = dropout(network, 0.5)
network = fully_connected(network, 512, activation='relu')
network = dropout(network, 0.5)
network = fully_connected(network, 512, activation='relu')
network = dropout(network, 0.5)
network = fully_connected(network, 512, activation='relu')
network = dropout(network, 0.5)
network = fully_connected(network, 120, activation='softmax')
network = regression(network, optimizer='adam',
loss='categorical_crossentropy',
learning_rate=0.001)
# Train using classifier
model = tflearn.DNN(network, tensorboard_verbose=0, checkpoint_path='dog_breed_identification.tfl.ckpt')
model.fit(X_train, Y_train, n_epoch=50, shuffle=True, validation_set=(X_test, Y_test),
show_metric=True, run_id='dog_breed')
model.save("dog_breed_identification.tfl")