#14. Keras tutorial - the Happy House
- Tensorflow가 higher-level framework라면 Keras는 더욱 higher-level framwork이며 추가적인 abstraction을 제공한다.
- 제한적인 부분들이 많아 Tensorflow만큼 복잡한 모델을을 실행시키진 못하지만 많은 common model들에 대해 잘 작동한다.
- 예시는 (64, 64, 3) 이미지에 600개의 training set과 150개의 test set으로 구성되어 있으며 label은 1(happy)/0(unhappy)
* Building a model in Keras
def model(input_shape):
# Define the input placeholder as a tensor with shape input_shape. Think of this as your input image!
X_input = Input(input_shape)
# Zero-Padding: pads the border of X_input with zeroes
X = ZeroPadding2D((3, 3))(X_input)
# CONV -> BN -> RELU Block applied to X
X = Conv2D(32, (7, 7), strides = (1, 1), name = 'conv0')(X)
X = BatchNormalization(axis = 3, name = 'bn0')(X)
X = Activation('relu')(X)
# MAXPOOL
X = MaxPooling2D((2, 2), name='max_pool')(X)
# FLATTEN X (means convert it to a vector) + FULLYCONNECTED
X = Flatten()(X)
X = Dense(1, activation='sigmoid', name='fc')(X)
# Create model. This creates your Keras model instance, you'll use this instance to train/test the model.
model = Model(inputs = X_input, outputs = X, name='HappyModel')
return model
- 텐서플로우와는 달리 하나의 변수만을 사용해 가장 latest 값을 계산한다.
- 총 4개의 step으로 모델을 러닝시키게 된다.
(1) create the model
(2) compile the model by model.compile(optimizer = "...", loss = "...", metrics = ["accuracy"])
(3) trian the model on train data by model.fit(x = ..., y = ..., epochs = ..., batch_size = ...)
(4) test the model on test data by model.evaluate(x = ..., y = ...)
- Keras는 빠른 prototyping을 위해 사용하면 좋다. 서로 다른 모델 구조를 간단하고 빠르게 시도할 수 있기 때문이다.
- Create->Compile->Fit/Train->Evaluate/Test 순으로 진행된다. 이를 기억해 모델을 만들자.
- model.summary(): 레이어들에 대한 정보를 알려주는 함수
- model.plot_model(): 그래프를 시각화해 저장할 수 있도록 만들어준다.