반응형
Recent Posts
Recent Comments
관리 메뉴

개발잡부

[cnn] Residual block 본문

강좌

[cnn] Residual block

닉의네임 2022. 8. 7. 12:25
반응형
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt

(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
#32x32x3
#Norma

train_images, test_images = train_images/255.0, test_images/255.0
class_names = ['airplane','automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
plt.figure(figsize=(10,10))
for i in range(25) :
  plt.subplot(5,5,i+1)
  plt.xticks([])
  plt.yticks([])
  plt.grid(False)
  plt.imshow(train_images[i], cmap=plt.cm.binary)
  plt.xlabel(class_names[train_labels[i][0]])
plt.show()
def add_residual_block(inputs, block_number, in_channels, out_channels):
  _=str(block_number)
  skip = tf.identity(inputs)
  down = 1
  if in_channels != out_channels:
    skip = tf.keras.layers.Conv2D(filters=out_channels,
                                  kernel_size=1,
                                  strides=(2,2),
                                  padding="same")(skip)
    down = 2
    x = tf.keras.layers.Conv2D(filters=out_channels,
                               kernel_size=3,
                               padding="same",
                               strides=down)(inputs)

    x = tf.keras.layers.BatchNormalization()(x)
    x = tf.nn.relu(x)
    x = tf.keras.layers.Conv2D(filters=out_channels,
                               kernel_size=3,
                               padding="same"
                               )(x)   
    x = tf.keras.layers.BatchNormalization()(x)                                                       
    x = tf.keras.layers.Add()([x, skip])
    x = tf.nn.relu(x)
    return x
inputs = tf.keras.Input(shape=(32,32,3))
data = tf.keras.layers.Conv2D(filters=64,
                            kernel_size=7,
                            strides=2,
                            padding="same")(inputs)

data = tf.keras.layers.BatchNormalization()(data)
data = tf.keras.layers.Activation('relu')(data)
data = tf.keras.layers.MaxPooling2D(pool_size=(3,3), padding="same", strides=2)(data)

data = add_residual_block(inputs=data, block_number='2_1', in_channels=64, out_channels=64)
data = add_residual_block(inputs=data, block_number='2_2', in_channels=64, out_channels=64)

data = add_residual_block(inputs=data, block_number='3_1', in_channels=64, out_channels=128)
data = add_residual_block(inputs=data, block_number='3_2', in_channels=128, out_channels=128)

data = add_residual_block(inputs=data, block_number='4_1', in_channels=128, out_channels=256)
data = add_residual_block(inputs=data, block_number='4_2', in_channels=256, out_channels=256)

data = add_residual_block(inputs=data, block_number='5_1', in_channels=256, out_channels=512)
data = add_residual_block(inputs=data, block_number='5_2', in_channels=512, out_channels=512)

data = tf.keras.layers.GlobalAveragePooling2D()(data)
data = tf.keras.layers.Dense(10)(data)
model = tf.keras.Model(inputs=inputs, outputs=data)
model.summary()
반응형

'강좌' 카테고리의 다른 글

[tf] 11. RNN  (0) 2022.07.31
[tf] 3. Convolution Layer  (0) 2022.07.31
선형회귀  (0) 2022.05.30
12.Multi-Classification 실습  (0) 2022.05.14
google colab  (0) 2022.03.24
Comments