-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpure_convolutional.py
More file actions
105 lines (75 loc) · 2.93 KB
/
pure_convolutional.py
File metadata and controls
105 lines (75 loc) · 2.93 KB
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
#!/usr/bin/env python
# coding: utf-8
import cv2
import glob
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import pandas as pd
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense,GlobalAveragePooling2D, Dropout
import numpy as np
from keras.utils.np_utils import to_categorical
from sklearn.preprocessing import LabelEncoder,OneHotEncoder
from keras.optimizers import Adam, SGD
from keras.utils import to_categorical
#Convolutional neural network
def pure_cnn():
model = Sequential()
model.add(Conv2D(96, (3, 3), activation='relu', padding = 'same', input_shape=(64,64,3)))
model.add(Dropout(0.2))
model.add(Conv2D(96, (3, 3), activation='relu', padding = 'same'))
model.add(Conv2D(96, (3, 3), activation='relu', padding = 'same', strides = 2))
model.add(Dropout(0.5))
model.add(Conv2D(192, (3, 3), activation='relu', padding = 'same'))
model.add(Conv2D(192, (3, 3), activation='relu', padding = 'same'))
model.add(Conv2D(192, (3, 3), activation='relu', padding = 'same', strides = 2))
model.add(Dropout(0.5))
model.add(Conv2D(192, (3, 3), padding = 'same'))
model.add(Activation('relu'))
model.add(Conv2D(192, (1, 1),padding='valid'))
model.add(Activation('relu'))
model.add(Conv2D(4, (1, 1), padding='valid'))
model.add(GlobalAveragePooling2D())
model.add(Activation('softmax'))
model.summary()
return model
if __name__ == '__main__':
target = pd.read_csv('./train.truth.csv')
names = target['fn']
#get all images from the dataset(same indexes as target['fn'])
images = []
for name in names:
aux = './train/' + name
images.append(cv2.imread(aux,1))
#encode directions
LE = LabelEncoder()
target = LE.fit_transform(target['label'])
target = np.array(target)
#split data intro train and test
train_x,test_x,train_y,test_y = train_test_split(images,target,test_size = 0.33)
train_x = np.array(train_x)
train_y = np.array(train_y)
test_x = np.array(test_x)
test_y = np.array(test_y)
#one hot encoding
train_y = to_categorical(train_y)
test_y = to_categorical(test_y)
train_x = train_x.astype('float32')
train_y = train_y.astype('float32')
test_x = test_x.astype('float32')
test_y = test_y.astype('float32')
#normalization of input
train_x /= 255
test_x /= 255
#create model
model = pure_cnn()
model.compile(loss='categorical_crossentropy',
optimizer=Adam(lr=0.0001), # LR = learning rate
metrics = ['accuracy']) # Metrics to be evaluated by the model
model_details = model.fit(train_x, train_y,
batch_size = 32,
epochs = 20, # number of iterations
validation_data= (test_x,test_y),
verbose=1)
model.save('final_model.h5')