AlexNet/ZFNet/NIN
2012
- AlexNet
2013
- ZFNet(Visaulization : 진단을 통해)
- NIN (1 x 1, stacking, GAP)
2014
- VGG / GoogLeNet
import tensorflow as tf
input_ = tf.keras.Input((227,227,3)) # 논문에는 224 로 나와 있지만 data augmentation 으로 실제 227
Alexnet
import tensorflow_addons as tfa
x = tf.keras.layers.Conv2D(96, 11, 4)(input_)
x = tf.keras.layers.MaxPool2D(3, 2)(x)
x = tf.keras.layers.Conv2D(256, 5, padding='same', activation='relu')(x)
x = tf.keras.layers.MaxPool2D(3, 2)(x)
x = tf.keras.layers.Conv2D(384, 3, padding='same', activation='relu', bias_initializer='ones')(x)
x = tf.keras.layers.Conv2D(384, 3, padding='same', activation='relu')(x)
x = tf.keras.layers.Conv2D(256, 3, padding='same', activation='relu')(x)
x = tf.keras.layers.MaxPool2D(3, 2)(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(4096)(x)
x = tf.keras.layers.Dropout(0.5)(x)
x = tf.keras.layers.Dense(4096)(x)
x = tf.keras.layers.Dropout(0.5)(x)
x = tf.keras.layers.Dense(1000, activation='softmax')(x)
model = tf.keras.Model(input_,x)
model.compile(loss=tf.keras.losses.CategoricalCrossentropy(), optimizer=tfa.optimizers.SGDW(weight_decay=0.0005, momentum=0.9))
tf.keras.initializers.GlorotNormal
keras.initializers.initializers_v2.GlorotNormal
len(model.layers)
15
# model.weights
# model.weights[0][...,0] #
model.summary()
Model: "model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 227, 227, 3)] 0
conv2d (Conv2D) (None, 55, 55, 96) 34944
max_pooling2d (MaxPooling2D (None, 27, 27, 96) 0
)
conv2d_1 (Conv2D) (None, 27, 27, 256) 614656
max_pooling2d_1 (MaxPooling (None, 13, 13, 256) 0
2D)
conv2d_2 (Conv2D) (None, 13, 13, 384) 885120
conv2d_3 (Conv2D) (None, 13, 13, 384) 1327488
conv2d_4 (Conv2D) (None, 13, 13, 256) 884992
max_pooling2d_2 (MaxPooling (None, 6, 6, 256) 0
2D)
flatten (Flatten) (None, 9216) 0
dense (Dense) (None, 4096) 37752832
dropout (Dropout) (None, 4096) 0
dense_1 (Dense) (None, 4096) 16781312
dropout_1 (Dropout) (None, 4096) 0
dense_2 (Dense) (None, 1000) 4097000
=================================================================
Total params: 62,378,344
Trainable params: 62,378,344
Non-trainable params: 0
_________________________________________________________________
Alexnet 2
x = tf.keras.layers.Conv2D(48, 11, 4)(input_)
x = tf.keras.layers.MaxPool2D(3, 2)(x)
xx = tf.keras.layers.Conv2D(128, 5, padding='same')(x)
y = tf.keras.layers.Conv2D(48, 11, 4)(input_)
y = tf.keras.layers.MaxPool2D(3, 2)(y)
yy = tf.keras.layers.Conv2D(128, 5, padding='same')(y)
i = tf.keras.layers.Concatenate()([xx,yy])
i = tf.keras.layers.Conv2D(192, 3, padding='same')(i)
i = tf.keras.layers.Conv2D(192, 3, padding='same')(i)
i = tf.keras.layers.Conv2D(128, 3, padding='same')(i)
i = tf.keras.layers.MaxPool2D(3, 2)(i)
i = tf.keras.layers.Flatten()(i)
j = tf.keras.layers.Concatenate()([xx,yy])
j = tf.keras.layers.Conv2D(192, 3, padding='same')(j)
j = tf.keras.layers.Conv2D(192, 3, padding='same')(j)
j = tf.keras.layers.Conv2D(128, 3, padding='same')(j)
j = tf.keras.layers.MaxPool2D(3, 2)(j)
j = tf.keras.layers.Flatten()(j)
k = tf.keras.layers.Concatenate()([i,j])
k = tf.keras.layers.Dense(2048)(k)
l = tf.keras.layers.Concatenate()([i,j])
l = tf.keras.layers.Dense(2048)(l)
m = tf.keras.layers.Concatenate()([k,l])
m = tf.keras.layers.Dense(2048)(m)
n = tf.keras.layers.Concatenate()([k,l])
n = tf.keras.layers.Dense(2048)(n)
t = tf.keras.layers.Concatenate()([n,m])
t = tf.keras.layers.Dense(1000, activation='softmax')(t)
model2 = tf.keras.Model(input_, t)
tf.keras.utils.plot_model(model2, rankdir='LR', show_shapes=True)
model2.summary()
Model: "model_1"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) [(None, 227, 227, 3 0 []
)]
conv2d_5 (Conv2D) (None, 55, 55, 48) 17472 ['input_1[0][0]']
conv2d_7 (Conv2D) (None, 55, 55, 48) 17472 ['input_1[0][0]']
max_pooling2d_3 (MaxPooling2D) (None, 27, 27, 48) 0 ['conv2d_5[0][0]']
max_pooling2d_4 (MaxPooling2D) (None, 27, 27, 48) 0 ['conv2d_7[0][0]']
conv2d_6 (Conv2D) (None, 27, 27, 128) 153728 ['max_pooling2d_3[0][0]']
conv2d_8 (Conv2D) (None, 27, 27, 128) 153728 ['max_pooling2d_4[0][0]']
concatenate (Concatenate) (None, 27, 27, 256) 0 ['conv2d_6[0][0]',
'conv2d_8[0][0]']
concatenate_1 (Concatenate) (None, 27, 27, 256) 0 ['conv2d_6[0][0]',
'conv2d_8[0][0]']
conv2d_9 (Conv2D) (None, 27, 27, 192) 442560 ['concatenate[0][0]']
conv2d_12 (Conv2D) (None, 27, 27, 192) 442560 ['concatenate_1[0][0]']
conv2d_10 (Conv2D) (None, 27, 27, 192) 331968 ['conv2d_9[0][0]']
conv2d_13 (Conv2D) (None, 27, 27, 192) 331968 ['conv2d_12[0][0]']
conv2d_11 (Conv2D) (None, 27, 27, 128) 221312 ['conv2d_10[0][0]']
conv2d_14 (Conv2D) (None, 27, 27, 128) 221312 ['conv2d_13[0][0]']
max_pooling2d_5 (MaxPooling2D) (None, 13, 13, 128) 0 ['conv2d_11[0][0]']
max_pooling2d_6 (MaxPooling2D) (None, 13, 13, 128) 0 ['conv2d_14[0][0]']
flatten_1 (Flatten) (None, 21632) 0 ['max_pooling2d_5[0][0]']
flatten_2 (Flatten) (None, 21632) 0 ['max_pooling2d_6[0][0]']
concatenate_2 (Concatenate) (None, 43264) 0 ['flatten_1[0][0]',
'flatten_2[0][0]']
concatenate_3 (Concatenate) (None, 43264) 0 ['flatten_1[0][0]',
'flatten_2[0][0]']
dense_3 (Dense) (None, 2048) 88606720 ['concatenate_2[0][0]']
dense_4 (Dense) (None, 2048) 88606720 ['concatenate_3[0][0]']
concatenate_5 (Concatenate) (None, 4096) 0 ['dense_3[0][0]',
'dense_4[0][0]']
concatenate_4 (Concatenate) (None, 4096) 0 ['dense_3[0][0]',
'dense_4[0][0]']
dense_6 (Dense) (None, 2048) 8390656 ['concatenate_5[0][0]']
dense_5 (Dense) (None, 2048) 8390656 ['concatenate_4[0][0]']
concatenate_6 (Concatenate) (None, 4096) 0 ['dense_6[0][0]',
'dense_5[0][0]']
dense_7 (Dense) (None, 1000) 4097000 ['concatenate_6[0][0]']
==================================================================================================
Total params: 200,425,832
Trainable params: 200,425,832
Non-trainable params: 0
__________________________________________________________________________________________________
Visualizations 논문
ZFnet
x = tf.keras.layers.Conv2D(96, 7, 2)(input_)
x = tf.keras.layers.MaxPool2D(3, 2)(x)
x = tf.keras.layers.Conv2D(256, 5, padding='same', activation='relu')(x)
x = tf.keras.layers.MaxPool2D(3, 2)(x)
x = tf.keras.layers.Conv2D(384, 3, padding='same', activation='relu', bias_initializer='ones')(x)
x = tf.keras.layers.Conv2D(384, 3, padding='same', activation='relu')(x)
x = tf.keras.layers.Conv2D(256, 3, padding='same', activation='relu')(x)
x = tf.keras.layers.MaxPool2D(3, 2)(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(4096)(x)
x = tf.keras.layers.Dropout(0.5)(x)
x = tf.keras.layers.Dense(4096)(x)
x = tf.keras.layers.Dropout(0.5)(x)
x = tf.keras.layers.Dense(1000, activation='softmax')(x)
NIN
x = tf.keras.layers.Conv2D(96, 11, 4)(input_)
x = tf.keras.layers.MaxPool2D(3, 2)(x)
x = tf.keras.layers.Conv2D(256, 5, padding='same', activation='relu')(x)
x = tf.keras.layers.MaxPool2D(3, 2)(x)
x = tf.keras.layers.Conv2D(384, 3, padding='same', activation='relu', bias_initializer='ones')(x)
x = tf.keras.layers.Conv2D(384, 3, padding='same', activation='relu')(x)
x = tf.keras.layers.Conv2D(256, 3, padding='same', activation='relu')(x)
x = tf.keras.layers.MaxPool2D(3, 2)(x)
# x = tf.keras.layers.Flatten()(x) # 6 x6 x 256 = 9216
x = tf.keras.layers.GlobalAveragePooling2D()(x) # layer 수 만큰만 늘어남 256개
# x = tf.keras.layers.Dense(4096)(x)
# x = tf.keras.layers.Dropout(0.5)(x)
# x = tf.keras.layers.Dense(4096)(x)
# x = tf.keras.layers.Dropout(0.5)(x)
# x = tf.keras.layers.Dense(1000, activation='softmax')(x)
model3 = tf.keras.Model(input_, x)
model3.summary()
Model: "model_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 227, 227, 3)] 0
conv2d_20 (Conv2D) (None, 55, 55, 96) 34944
max_pooling2d_10 (MaxPoolin (None, 27, 27, 96) 0
g2D)
conv2d_21 (Conv2D) (None, 27, 27, 256) 614656
max_pooling2d_11 (MaxPoolin (None, 13, 13, 256) 0
g2D)
conv2d_22 (Conv2D) (None, 13, 13, 384) 885120
conv2d_23 (Conv2D) (None, 13, 13, 384) 1327488
conv2d_24 (Conv2D) (None, 13, 13, 256) 884992
max_pooling2d_12 (MaxPoolin (None, 6, 6, 256) 0
g2D)
global_average_pooling2d (G (None, 256) 0
lobalAveragePooling2D)
=================================================================
Total params: 3,747,200
Trainable params: 3,747,200
Non-trainable params: 0
_________________________________________________________________