forked from Gautam-J/Self-Driving-Car
-
Notifications
You must be signed in to change notification settings - Fork 1
/
crashnet.py
67 lines (51 loc) · 2.55 KB
/
crashnet.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
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Conv2D, Reshape
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.layers import Conv2DTranspose
from tensorflow.keras.layers import Flatten, Dense
from tensorflow.keras import backend as K
from tensorflow.keras.utils import plot_model
import numpy as np
'''
AutoEncoder for detecting crashes and other anomalies.
'''
def CrashNet():
road_map = Input(shape=(80, 200, 3))
x = Conv2D(24, kernel_size=5, strides=2, padding='same', activation='relu')(road_map)
x = BatchNormalization()(x)
x = Conv2D(36, kernel_size=5, strides=2, padding='same', activation='relu')(x)
x = BatchNormalization()(x)
x = Conv2D(48, kernel_size=3, strides=2, padding='same', activation='relu')(x)
x = BatchNormalization()(x)
x = Conv2D(64, kernel_size=3, strides=1, padding='same', activation='relu')(x)
x = BatchNormalization()(x)
volumeSize = K.int_shape(x)
x = Flatten()(x)
latent = Dense(100)(x)
encoder = Model(inputs=road_map, outputs=latent, name='encoder')
latentInputs = Input(shape=(100,))
x = Dense(np.prod(volumeSize[1:]))(latentInputs)
x = Reshape((volumeSize[1], volumeSize[2], volumeSize[3]))(x)
x = Conv2DTranspose(64, kernel_size=3, strides=1, padding='same', activation='relu')(x)
x = BatchNormalization()(x)
x = Conv2DTranspose(48, kernel_size=3, strides=2, padding='same', activation='relu')(x)
x = BatchNormalization()(x)
x = Conv2DTranspose(36, kernel_size=5, strides=2, padding='same', activation='relu')(x)
x = BatchNormalization()(x)
x = Conv2DTranspose(24, kernel_size=5, strides=2, padding='same', activation='relu')(x)
x = BatchNormalization()(x)
outputs = Conv2D(3, (3, 3), activation='sigmoid', padding='same')(x)
decoder = Model(latentInputs, outputs, name='decoder')
autoencoder = Model(road_map, decoder(encoder(road_map)), name='autoencoder')
return encoder, decoder, autoencoder
if __name__ == '__main__':
encoder, decoder, autoencoder = CrashNet()
encoder.summary(line_length=100)
decoder.summary(line_length=100)
autoencoder.summary(line_length=100)
plot_model(encoder, to_file='CrashNet_encoder.png', show_shapes=True,
show_layer_names=True, dpi=200)
plot_model(decoder, to_file='CrashNet_decoder.png', show_shapes=True,
show_layer_names=True, dpi=200)
plot_model(autoencoder, to_file='CrashNet_autoencoder.png', show_shapes=True,
show_layer_names=True, dpi=200, expand_nested=True)