-
Notifications
You must be signed in to change notification settings - Fork 0
/
CNN_LD.py
99 lines (77 loc) · 3.79 KB
/
CNN_LD.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
#image input size: 80 x 160 x 3 (RGB)
#image output size: 80 x 160 x 1 (just the G channel with a re-drawn lane)
#****import statements****#
import numpy as np
import pickle
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Activation, Dropout, UnSampling2D
from keras.layers import Conv2DTranspose, Conv2D, MaxPooling2D
from keras.layers.normalization import BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras import regularizers
#****loading data****#
train_images=pickle.load(open("/home/ghosh/Documents/full_CNN_train.p","rb"))
labels=pickle.load(open("/home/ghosh/Documents/full_CNN_labels.p","rb"))
train_images=np.array(train_images)
labels=np.array(labels)
labels=labels/255 #normalize labels
#****shuffle and split the training set****#
train_images,labels=shuffle(train_images, labels)
X_train, X_val, Y_train, Y_val= train_test_split(train_images,labels, test_size=0.1)
#****initializations****#
batch_size=128
pool_size= (2,2)
epochs=10
input_shape=X_train.shape[1:]
#*****NEURAL NET ARCHITECTURE*****#
model= Sequential()
#**batch normalization**#
model.add((input_shape=input_shape))
#**encoding layers**#
model.add(Conv2D(8, (3,3), padding='valid', strides=(1,1), activation='relu', name='Conv1'))
model.add(Conv2D(16, (3,3), padding='valid', strides=(1,1), activation='relu', name='Conv2'))
model.add(MaxPooling2D(pool_size=pool_size, name='Pool1'))
model.add(Conv2D(16, (3,3), padding='valid', strides=(1,1), activation='relu', name='Conv3'))
model.add(Dropout(0.2))
model.add(Conv2D(32, (3,3), padding='valid', strides=(1,1), activation='relu', name='Conv4'))
model.add(Dropout(0.2))
model.add(Conv2D(32, (3,3), padding='valid', strides=(1,1), activation='relu', name='Conv5'))
model.add(Dropout(0.2))
model.add(MaxPooling2D(pool_size=pool_size, name='Pool2'))
model.add(Conv2D(64, (3,3), padding='valid', strides=(1,1), activation='relu', name='Conv6'))
model.add(Dropout(0.2))
model.add(Conv2D(64, (3,3), padding='valid', strides=(1,1), activation='relu', name='Conv7'))
model.add(Dropout(0.2))
model.add(MaxPooling2D(pool_size=pool_size, name='Pool3'))
#**decoding layers**#
model.add(UpSampling2D(size=pool_size, name='UpSample1'))
model.add(Conv2DTranspose(64, (3,3), padding='valid', strides=(1,1), activation='relu', name='Deconv1'))
model.add(Dropout(0.2))
model.add(Conv2DTranspose(64, (3,3), padding='valid', strides=(1,1), activation='relu', name='Deconv2'))
model.add(Dropout(0.2))
model.add(UpSampling2D(size=pool_size, name='UpSample2'))
model.add(Conv2DTranspose(32, (3,3), padding='valid', strides=(1,1), activation='relu', name='Deconv3'))
model.add(Dropout(0.2))
model.add(Conv2DTranspose(32, (3,3), padding='valid', strides=(1,1), activation='relu', name='Deconv4'))
model.add(Dropout(0.2))
model.add(Conv2DTranspose(16, (3,3), padding='valid', strides=(1,1), activation='relu', name='Deconv5'))
model.add(Dropout(0.2))
model.add(UpSampling2D(size=pool_size,name='UpSample3'))
model.add(Conv2DTranspose(16, (3,3), padding='valid', strides=(1,1), activation='relu', name='Deconv6'))
model.add(Dropout(0.2))
model.add(Conv2DTranspose(1, (3, 3), padding='valid', strides=(1,1), activation = 'relu', name = 'Final'))
#****data augmentation****#
datagen = ImageDataGenerator(channel_shift_range=0.2)
datagen.fit(X_train)
#****compile and train models****#
model.compile(optimizer='Adam',loss='mean_squared_error')
model.fit_generator(datagen.flow(X_train, y_train, batch_size=batch_size), steps_per_epoch=len(X_train)/batch_size,
epochs=epochs, verbose=1, validation_data=(X_val, y_val))
#****freeze layers****#
model.trainable = False
model.compile(optimizer='Adam', loss='mean_squared_error')
#****save weights****#
model.save('/home/ghosh/Documents/full_CNN_model.h5')
model.summary()