핸즈온 머신러닝을 참고하여 Resnet-34를 직접 구현해보았다.
class ResidualUnit(keras.layers.Layer):
def __init__(self, filters, strides=1, activation="relu", **kwargs):
super().__init__(**kwargs)
self.activation = keras.activations.get(activation)
self.main_layers = [
keras.layers.Conv2D(filters, 3, strides=strides, padding="same", use_bias=False),
keras.layers.BatchNormalization(),
self.activation,
keras.layers.Conv2D(filters, 3, strides =1, padding="same", use_bias = False),
keras.layers.BatchNormalization()]
self.skip_layers = []
if strides > 1:
self.skip_layers = [
keras.layers.Conv2D(filters, 1, strides = strides, padding="same", use_bias = False),
keras.layers.BatchNormalization()]
def call(self, inputs):
Z = inputs
for layer in self.main_layers:
Z = layer(Z)
skip_Z = inputs
for layer in self.skip_layers:
skip_Z = layer(skip_Z)
return self.activation(Z + skip_Z)
model = keras.models.Sequential()
model.add(keras.layers.Conv2D(64,7, strides=2, input_shape=[32,32,3], padding="same", use_bias = False))
model.add(keras.layers.BatchNormalization())
model.add(keras.layers.Activation("relu"))
model.add(keras.layers.MaxPool2D(pool_size=3, strides=2, padding="same"))
prev_filters = 64
for filters in [64]*3+[128]*4 + [256]*6 + [512]*3:
strides = 1 if filters == prev_filters else 2
model.add(ResidualUnit(filters, strides=strides))
prev_filters = filters
model.add(keras.layers.GlobalAvgPool2D())
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(10, activation="softmax"))
model.summary()
(x_train, y_train), (x_test, y_test) = tk.datasets.mnist.load_data()
x_train = tf.pad(x_train, [[0, 0], [2,2], [2,2]])/255
x_test = tf.pad(x_test, [[0, 0], [2,2], [2,2]])/255
x_train = tf.expand_dims(x_train, axis=3, name=None)
x_test = tf.expand_dims(x_test, axis=3, name=None)
x_train = tf.repeat(x_train, 3, axis=3)
x_test = tf.repeat(x_test, 3, axis=3)
print(x_train.shape, y_train.shape, x_test.shape, y_test.shape)
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.summary()
print(model.fit(x_train, y_train, epochs=100, batch_size = 20))
print(model.evaluate(x_test, y_test))
학습 중 GPU사용하는지 여부 확인 코드
'모각코(모여서 각자 코딩)' 카테고리의 다른 글
원격 GPU서버 jupyter notebook으로 접속하기 (0) | 2023.02.06 |
---|---|
22-23 동계 모각코 4회차 개인 목표 및 결과 (0) | 2023.02.06 |
22-23 동계 모각코 3회차 개인 목표 및 결과 (0) | 2023.02.02 |
Ubuntu - SSH (0) | 2023.01.31 |
22-23 동계 모각코 2회차 개인 목표 및 결과 (0) | 2023.01.30 |