forked from Vladkryvoruchko/PSPNet-Keras-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_cityscapes_gpu1.py
346 lines (310 loc) · 14.2 KB
/
new_cityscapes_gpu1.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
'''
In this script, we addopt a lot of things from : Pradyumna Reddy. We use
cityscapes dataset that provided at: https://www.cityscapes-dataset.com/.
In this training process, we uses both fine annotation and coarse annotation.
Detail amount of the dataset that we use are:
===========================================================
|No | Name | type | amount | Original Amount |
===========================================================
| 1 | train | fine | 2.975 | 2.975 |
| 2 | val | fine | 500 | 500 |
| 3 | train | coarse | 2.975 | 2.975 |
| 4 | train+extra| coarse | 19.000 | 19.998 |
| 5 | val | coarse | 500 | 500 |
===========================================================
Pretrained model was used to accelerate our training process. However, due to
pretrained model availability, we need to choose the most popular model. In the
first step, we take pretrained model from model zoo, and load the matrix into
our model, we pop the last layer to our output layer and use that model matrix
as initial value for our model. This layer really important, while we have no
significant differences data in our experiment. In the first layer of the model,
we will have such as line, edge and several esensial feature. For example, in
Cityscapes dataset, we try to recognize human form, while in the initial model
they have the data characteristic.
Afterward, we use coarse dataset and its coarse annotation, to gain our model
accuracies. The use of free trained model accelerate our learning process. So,
we dont need to train from the scratch, in other word we dont need to train
zeros matrix. As mentioned in Table \ref{table1} we use 2975 + 19.000 coarse
data, and 500 coarse data for validation. In this training process, we use
several optimizer such as : Adam, SGD, RMSprop. The initial learning rate $\alpha$
is 0.001 and decay 0.9. Due to our limitation resource, we use small batch size : 1.
Our training process use full size image size : 2048 x 1080 x 3. Regarding to this
image size and resource limitation, we devide data into 5 steps for both train with
fine annotation and train with coarse annotaion, and we devide 20 steps for train extra
with coarse annotation. In this dataset, there is no test data. So, we change validation
data into testing data. validation with coarse annotation will be use in testing process
with coarse annotation, and validation data with fine annotaton will be use in final testing.
During the training process, we use 20% dataset as validation and 80% training.
This precentage will be use in every steps in this training process.
Our system use Ubuntu 17.04 as operating system and several main librry such as : tensorflow,
keras, numpy etc. Our machine use 2xTitanx 1080 with 12 GB of RAM. Due to this limitation,
we use initial epoch persteps is 200 epochs.
'''
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import print_function
import os
os.environ['KERAS_BACKEND'] = 'tensorflow'
#os.environ["CUDA_VISIBLE_DEVICES"]="0,1"
#os.environ['THEANO_FLAGS']='mode=FAST_RUN,device=cuda,floatX=float32,optimizer=fast_compile'
import time
import pylab as pl
import gc
import matplotlib.cm as cm
import itertools
import numpy as np
import matplotlib.image as mpimg
from keras import optimizers
from keras.models import model_from_json
#import theano.tensor as T
#np.random.seed(1337) # for reproducibility
from keras.callbacks import CSVLogger
import pickle
from six.moves import cPickle
from keras.layers.noise import GaussianNoise
import keras.models as models
# from keras.models import load_model
from keras.layers.core import Layer, Dense, Dropout, Activation, Flatten, Reshape, Permute
from keras.layers.convolutional import Convolution2D, MaxPooling2D, UpSampling2D, ZeroPadding2D
from keras.layers.normalization import BatchNormalization
from keras.utils import np_utils
#from keras.regularizers import ActivityRegularizer
from keras import metrics
#from keras.utils.visualize_util import plot
from numpy import array
from keras import backend as K
K.set_image_dim_ordering('tf')
import numpy as np
from scipy.misc import toimage
import matplotlib.pyplot as plt
import layers_builder as layers
import cv2
# General Parameter
# ===================
# Dataset root folder
path = '/home/adi005/Cityscapes/'
training_is = 'fine'
optim = 'adam'
# Image Dimension
im_width = 713
im_height = 713
data_shape = (im_height*im_width)
im_dimension = 3
pretrainded = 1
nb_classes = 35
batch_size = 2
nb_epoch = 1
if training_is == 'fine' :
# Image Number
train_sample = 2970
steps = 10
val_sample = 500
split = train_sample/steps
train_txt = 'train_fine_cityscapes.txt'
val_txt = 'val_fine_cityscapes.txt'
elif training_is == 'coarse':
train_sample = 2800
nb_epoch = nb_epoch - 80
steps = 10
val_sample = 500
split = train_sample/steps
train_txt = 'train_coarse_cityscapes.txt'
val_txt = 'val_coarse_cityscapes.txt'
else:
train_sample = 19000
nb_epoch = nb_epoch - 80
steps = 50
val_sample = 500
split = train_sample/steps
train_txt = 'trainExtra_coarse_cityscapes.txt'
val_txt = 'val_coarse_cityscapes.txt'
print("================================")
print ('Training Sample : ', train_sample)
print ('Steps : ', steps)
print ('split per steps : ', split)
print ('train_txt : ', train_txt)
print ('train_txt : ', val_txt)
print ('Epoch : ', nb_epoch)
print("================================")
def new():
model = models.Sequential()
model.add(Layer(input_shape=(im_dimension, im_height, im_width)))
return model
def segnet():
autoencoder = models.Sequential()
# Add a noise layer to get a denoising autoencoder. This helps avoid overfitting
autoencoder.add(Layer(input_shape=(im_dimension, im_height, im_width)))
autoencoder.add(ZeroPadding2D(padding=(1,1)))
autoencoder.add(Convolution2D(64, 3, 3, subsample = (1,1),border_mode='valid'))
# print (autoencoder.summary())
autoencoder.add(BatchNormalization())
autoencoder.add(Activation('relu'))
autoencoder.add(MaxPooling2D(pool_size=(2, 2)))
# print (autoencoder.summary())
autoencoder.add(ZeroPadding2D(padding=(1,1)))
autoencoder.add(Convolution2D(64, 3, 3, border_mode='valid'))
autoencoder.add(BatchNormalization())
autoencoder.add(Activation('relu'))
autoencoder.add(MaxPooling2D(pool_size=(2, 2)))
autoencoder.add(ZeroPadding2D(padding=(1,1)))
autoencoder.add(Convolution2D(128, 3, 3, border_mode='valid'))
autoencoder.add(BatchNormalization())
autoencoder.add(Activation('relu'))
autoencoder.add(MaxPooling2D(pool_size=(2, 2)))
autoencoder.add(ZeroPadding2D(padding=(1,1)))
autoencoder.add(Convolution2D(256, 3, 3, border_mode='valid'))
autoencoder.add(BatchNormalization())
autoencoder.add(Activation('relu'))
autoencoder.add(ZeroPadding2D(padding=(1,1)))
autoencoder.add(Convolution2D(256, 3, 3, border_mode='valid'))
autoencoder.add(BatchNormalization())
autoencoder.add(UpSampling2D(size=(2,2)))
autoencoder.add(ZeroPadding2D(padding=(1,1)))
autoencoder.add(Convolution2D(128, 3, 3, border_mode='valid'))
autoencoder.add(BatchNormalization())
autoencoder.add(UpSampling2D(size=(2,2)))
autoencoder.add(ZeroPadding2D(padding=(1,1)))
autoencoder.add(Convolution2D(64, 3, 3, border_mode='valid'))
autoencoder.add(BatchNormalization())
autoencoder.add(UpSampling2D(size=(2,2)))
autoencoder.add(ZeroPadding2D(padding=(1,1)))
autoencoder.add(Convolution2D(64, 3, 3, border_mode='valid'))
autoencoder.add(BatchNormalization())
# autoencoder.add(Layer(input_shape=(im_dimension, im_height,im_width)))
autoencoder.add(Convolution2D(nb_classes, 1, 1, border_mode='valid',))
#import ipdb; ipdb.set_trace()
autoencoder.add(Reshape((nb_classes,data_shape), input_shape=(nb_classes,im_height,im_width)))
autoencoder.add(Permute((2, 1)))
autoencoder.add(Activation('softmax'))
return autoencoder
def binarylab(labels):
x = np.zeros([im_height,im_width,nb_classes],dtype="uint8")
for i in range(im_height):
for j in range(im_width):
x[i,j,labels[i][j]]=1
return x
def prep_train(j,k,train_txt):
train_data = np.zeros((int(split), im_height, im_width, im_dimension), dtype="uint8")
train_label = np.zeros((int(split), im_height, im_width,nb_classes),dtype="uint8")
# train_data = []
#load Image from directory /Cityscapes/
print("================================")
print("Loading Training Image ... \n")
print("================================")
with open(path+train_txt) as f:
txt = f.readlines()
# print(txt,'\n')
txt = [line.split(',') for line in txt]
# print('Loaded Training Image from',j, ' to ', k)
n = 0
for i in range(j,k):
# print (path + txt[i][0][1:])
# print (path + txt[i][1][1:][:-1])
# train_data[n] = np.rollaxis(cv2.imread(path + txt[i][0][1:]),2)
train_data[n] = cv2.imread(path+txt[i][0][1:])
train_label[n] = binarylab(cv2.imread(path + txt[i][1][1:][:-1])[:,:,0])
# train_label.append(binarylab(cv2.imread(path + txt[i][1][1:][:-1])[:,:,0]))
# print(n, ' : ' ,path + txt[i][1][1:][:-1])
n = n + 1
print('Loaded Training Image from',j, ' to ', k)
return train_data, train_label
def prep_val():
val_data = np.zeros((val_sample, im_dimension, im_height, im_width), dtype="uint8")
val_label = np.zeros((val_sample, im_height, im_width,nb_classes),dtype="uint8")
# train_data = []
#load Image from directory /Cityscapes/
print("================================")
print("Loading Validation Image ... \n")
print("================================")
with open(path+val_txt) as f:
txt = f.readlines()
txt = [line.split(',') for line in txt]
for i in range(len(txt)):
# print(path + txt[1][0][1:])
# print(path + txt[i][1][1:][:-1])
val_data[i] = np.rollaxis(cv2.imread(path + txt[i][0][1:]),2)
#val_data[i] = cv2.imread(path+txt[i][0][1:])
val_label [i] = binarylab(cv2.imread(path + txt[i][1][1:][:-1])[:,:,0])
# train_label.append(binarylab(cv2.imread(path + txt[i][1][1:][:-1])[:,:,0]))
# print(i, ' : ' , path + txt[i][1][1:][:-1])
print('Loaded Training Image from',i, ' to ', len(txt))
return val_data, val_label
#autoencoder = segnet()
resnet_layers = 50
model = layers.build_pspnet(nb_classes=nb_classes,
resnet_layers=resnet_layers,
input_shape=(713, 713))
# autoencoder = segnet()
# print (autoencoder.summary())
if pretrainded == 1 :
model.load_weights('Training_fine_adam_2970_samples_3_splits.h5')
print ('Wights Loaded...')
# print(autoencoder.summary())
#create log in csv style
csv_logger = CSVLogger('log/Training_'+training_is+'_'+str(train_sample)+'_samples.log',append=True)
#create optimizer
if optim == 'sgd' :
optimi = optimizers.SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True)
elif optim == 'rmsprop':
optimi = optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=None, decay=0.0)
elif optim == 'adam':
optimi = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
elif optim == 'adadelta':
optimi = optimizers.Adadelta(lr=1.0, rho=0.95, epsilon=None, decay=0.0)
elif optim == 'adagrad':
optimi = optimizers.Adagrad(lr=0.01, epsilon=None, decay=0.0)
else:
optimi = optimizers.Adamax(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0)
#comipile generated model
model.compile(loss="categorical_crossentropy", optimizer=optimi, metrics=['accuracy'])
time_start = time.clock()
j=0
k=0
for i in range(0,steps):
j = i*(split)
k = j+(split)
txt = train_txt
print('============================================================= \n')
print('Training and Validation Data From = ', int(j),' : ',int(k)-1,'\n')
print('============================================================= \n')
# load splitted training image
train, train_label = prep_train(int(j),int(k),txt)
# reshape label image to appropiate model we proposed
train_label = np.reshape(train_label,(int(split),data_shape,nb_classes))
# fitting the the model
history = model.fit(train, train_label, batch_size=batch_size, epochs=nb_epoch,callbacks = [csv_logger],
# uncomment this if you want use validation data
# verbose=1, validation_data=(val,val_label), shuffle=True)
# use split to validate model
verbose=1, validation_split=0.2, shuffle=True)
model.save_weights('Training_'+training_is+'_'+optim+'_'+str(train_sample)+'_samples_'+str(i)+'_splits.h5')
print ('This is ',i,'of splits, and',split-i,'remaining')
# history = autoencoder.fit_generator(datagen.flow(train, train_label,
# batch_size=batch_size),
# epochs=nb_epoch,
# validation_data=(val,val_label),
# workers=4)
print('\n============================================================= \n')
print('Load Testing images and Creating Validation label \n ')
print('============================================================= \n')
val, val_label = prep_val()
val_label = np.reshape(val_label,(val_sample,data_shape,nb_classes))
score = model.evaluate(val,val_label, batch_size=batch_size, show_accuracy=True, verbose=1)
print("==========================================")
print ('Loss : ',score[0])
print ('ACCS : ',score[1]*100)
print("==========================================")
time_elapsed = (time.clock() - time_start)
print('Time : ', time_elapsed,'s, or ',round(time_elapsed/3600,2),'h or , ',round(time_elapsed/3600,2)/24,'days' )
print("==========================================")
model.save_weights('Training_'+training_is+'_'+optim+'_'+str(train_sample)+'_samples.h5')
# height = np.size(train_label[0], 0)
# width = np.size(train_label[0], 1)
# depth = np.size(train_label[0], 2)
# print(height)
# print(width)
# print(depth)
# print(train_label[1].shape)
# print(np.rollaxis(train[1],0,3).shape)
# # print(train[1])
# mpimg.imsave('coba.jpg',np.rollaxis(train[1],0,3))