-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvggf.py
161 lines (118 loc) · 5.09 KB
/
vggf.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
151
152
153
154
155
156
157
158
159
160
161
# Importing the Keras libraries and packages
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.layers import BatchNormalization
from keras.optimizers import Adam
from keras import regularizers
from keras.layers import ZeroPadding2D
from keras.callbacks import CSVLogger
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
from keras import backend as K
K.tensorflow_backend._get_available_gpus()
print(K.tensorflow_backend._get_available_gpus())
# Initialising the CNN
classifier = Sequential()
# Step 1 - Convolution
# Step 1 - Convolution
classifier.add(Conv2D(32, (2, 2), input_shape = (64, 64, 3), activation = 'relu'))
classifier.add(BatchNormalization())
classifier.add(Conv2D(32, (2, 1), activation = 'relu'))
classifier.add(BatchNormalization())
classifier.add(Conv2D(32, (1, 2), activation = 'relu'))
classifier.add(BatchNormalization())
# Adding more convolutional layer
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Conv2D(48, (2, 2), activation = 'relu'))
classifier.add(BatchNormalization())
classifier.add(Conv2D(48, (2, 2), activation = 'relu'))
classifier.add(BatchNormalization())
classifier.add(Conv2D(48, (2, 2), activation = 'relu'))
classifier.add(BatchNormalization())
# Adding more convolutional layer
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Conv2D(64, (2, 2), activation = 'relu'))
classifier.add(BatchNormalization())
classifier.add(Conv2D(64, (2, 2), activation = 'relu'))
classifier.add(BatchNormalization())
classifier.add(Conv2D(64, (2, 2), activation = 'relu'))
classifier.add(BatchNormalization())
# Adding more convolutional layer
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Conv2D(80, (2, 2), activation = 'relu'))
classifier.add(BatchNormalization())
classifier.add(Conv2D(80, (2, 2), activation = 'relu'))
classifier.add(BatchNormalization())
classifier.add(Conv2D(80, (2, 2), activation = 'relu'))
classifier.add(BatchNormalization())
# Adding more convolutional layer
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(ZeroPadding2D((1,1)))
classifier.add(Conv2D(96, (2, 2), activation = 'relu'))
classifier.add(BatchNormalization())
classifier.add(ZeroPadding2D((1,1)))
classifier.add(Conv2D(96, (2, 2), activation = 'relu'))
classifier.add(BatchNormalization())
classifier.add(ZeroPadding2D((1,1)))
classifier.add(Conv2D(96, (2, 2), activation = 'relu'))
classifier.add(BatchNormalization())
classifier.add(MaxPooling2D(pool_size = (2, 2)))
# Step 3 - Flattening
classifier.add(Flatten())
# Step 4 - Full connection
classifier.add(Dense(units = 800, activation = 'relu', kernel_initializer='glorot_uniform', kernel_regularizer=regularizers.l2(0.00001)))
classifier.add(Dense(units = 800, activation = 'relu', kernel_initializer='glorot_uniform', kernel_regularizer=regularizers.l2(0.00001)))
classifier.add(Dense(units = 200, activation = 'softmax'))
# Compiling the CNN
from keras.callbacks import LearningRateScheduler
learning_rate = 1e-4
lr = 0.001
def updateLR(epoch, lr):
if(epoch % 10 == 0 and epoch !=0):
lr *= 0.5
print("update",lr)
return lr
lrate = LearningRateScheduler(updateLR)
Adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.00014, amsgrad=False)
classifier.compile(optimizer = Adam, loss = 'categorical_crossentropy', metrics = ['accuracy'])
# Part 2 - Fitting the CNN to the images
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('tiny-imagenet-200/train',
target_size = (64, 64),
batch_size = 23,
class_mode = 'categorical')
test_set = test_datagen.flow_from_directory('tiny-imagenet-200/validation',
target_size = (64, 64),
batch_size = 23,
class_mode = 'categorical')
csv_logger = CSVLogger('training.log')
history = classifier.fit_generator(training_set,
epochs = 30,
validation_data = test_set,
verbose=1,
callbacks=[lrate])
import matplotlib.pyplot as plt
print(history.history.keys())
# summarize history for accuracy
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()