2020/09/23 - [Python/Deep Learning] - [Keras] 모델 저장하기
[Keras] 모델 저장하기
딥러닝은 모델을 학습시기며 학습된 모델을 이용하여 결과를 예측하거나 결과물을 생성해냅니다. 이러한 모델들은 학습이 완료된 뒤(혹은 학습중) 저장하여 사용할 수 있습니다. 모델을 저장하
hidden-loca.tistory.com
저장하는 방법엔 3가지 방법이 있습니다.
- ModelCheckpoint
- model.save()
- to_json(), to_yaml() and save_weight
1,2번째 방법은 따로 가중치만 저장(weights)을 설정하지 않았다면 불러오는 방법이 같고 밑의 방법은 약간 다른 방법을 써야 합니다.
1. load.model
from keras.models import load_model
model = load_model('save_model.h5')
h5 형식으로 저장된 모델을 불러올 수 있습니다. 이렇게 불러오는 모델은 컴파일까지 다된 상태로 불러오는 것이라 바로 사용할 수 있습니다.
2.from_json(), from_yaml() and load_weight
from keras.models import model_from_json
with open("./save/model_json.json", "r") as model_json :
loaded_model = model_json.read()
loaded_model = model_from_json(loaded_model)
loaded_model.load_weights("./save/save_weight.h5")
print("Loaded model from disk")
loaded_model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
json 파일과 yaml파일은 from_json, yaml을 통해 모델 아키텍처를 불러오고 load_weight를 불러와 가중치를 더한뒤
컴파일하여 사용합니다. 예시는 json 파일을 불러오는 경우 입니다.
Model: "model_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 12, 12, 3) 0
_________________________________________________________________
separable_conv2d_1 (Separabl (None, 10, 10, 256) 1051
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 5, 5, 256) 0
_________________________________________________________________
separable_conv2d_2 (Separabl (None, 5, 5, 512) 133888
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 2, 2, 512) 0
_________________________________________________________________
separable_conv2d_3 (Separabl (None, 2, 2, 1024) 527360
_________________________________________________________________
global_max_pooling2d_1 (Glob (None, 1024) 0
_________________________________________________________________
dense_1 (Dense) (None, 256) 262400
_________________________________________________________________
re_lu_1 (ReLU) (None, 256) 0
_________________________________________________________________
dense_2 (Dense) (None, 64) 16448
_________________________________________________________________
re_lu_2 (ReLU) (None, 64) 0
_________________________________________________________________
dense_3 (Dense) (None, 27) 1755
=================================================================
Total params: 942,902
Trainable params: 942,902
Non-trainable params: 0
_________________________________________________________________
Loaded model from disk
Model: "model_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 12, 12, 3) 0
_________________________________________________________________
separable_conv2d_1 (Separabl (None, 10, 10, 256) 1051
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 5, 5, 256) 0
_________________________________________________________________
separable_conv2d_2 (Separabl (None, 5, 5, 512) 133888
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 2, 2, 512) 0
_________________________________________________________________
separable_conv2d_3 (Separabl (None, 2, 2, 1024) 527360
_________________________________________________________________
global_max_pooling2d_1 (Glob (None, 1024) 0
_________________________________________________________________
dense_1 (Dense) (None, 256) 262400
_________________________________________________________________
re_lu_1 (ReLU) (None, 256) 0
_________________________________________________________________
dense_2 (Dense) (None, 64) 16448
_________________________________________________________________
re_lu_2 (ReLU) (None, 64) 0
_________________________________________________________________
dense_3 (Dense) (None, 27) 1755
=================================================================
Total params: 942,902
Trainable params: 942,902
Non-trainable params: 0
_________________________________________________________________
두 가지 방법으로 저장된 모델을 model.summay()를 통해 비교해보면 똑같음을 알 수 있습니다.
'Back > Deep Learning' 카테고리의 다른 글
[Tensorflow] Image classification model maker예측하기. (2) | 2020.10.20 |
---|---|
[Tensorflow] Tensorflowlite를 이용한 Image classification model maker (1) | 2020.10.08 |
[Keras] 모델 저장하기 (0) | 2020.09.23 |
[Keras]ImageGenerator 사용하기. (0) | 2020.09.18 |
[Python] [CNN]점자번역 프로그램(7) - 정리 (1) | 2020.09.03 |