Keras 모델 저장(save_model) 및 불러오기 (load_model) 방법
Keras 모델을 파일로 저장하고 불러오는 방법 정리
from keras.models import load_model, save_model
Keras 모델 파일로 저장하기
keras.models.save_model(model, filepath, overwrite=True,
include_optimizer=True)
arguments:
model : 저장할 Keras 모델
filepath : 파일 저장 경로
overwrite : 덮어쓰기 여부
include_optimizer : True인 경우 optimizer의 상태를 함께 저장
Keras 모델 파일에서 불러오기
keras.models.load_model(filepath, custom_objects=None, compile=True)
arguments:
filepath : 자장된 파일 경로
custom_objects : deserialization 동안 고려되어야할 사용자 정의
class 또는 함수에 대한 옵셔널 디럭토리 매핑 이름(문자열)
compile : 로드 후 모델 컴파일 여부
return:
파일에서 불러온 keras model
save_model, load_model 사용 예제 코드
from keras.layers import Input, Dense, Dropout, regularizers
from keras.models import Model, load_model, save_model
from keras.optimizers import Adam
def build_model(in_size, out_sizes, depth=2, layer_units = 128, lr=0.01):
in_layer = Input(shape=(in_size,),name='input_layer')
prev = in_layer
for fc in range(depth):
prev = Dense(units=layer_units,
activation='relu', kernel_regularizer=regularizers.l2(l=0.01),
name='dense%d'%fc)(prev)
prev = Dropout(0.1,name='droupout%d'%fc)(prev)
out_layer = Dense(out_sizes,activation='softmax',
kernel_regularizer=regularizers.l2(l=0.01), name='out_layers')(prev)
model = Model(inputs=[in_layer],outputs=[out_layer])
optimizer = Adam(lr=lr)
model.compile(loss='mean_squared_error',
optimizer=optimizer,metrics=['accuracy'])
return model
def excode_save_model(filepath):
print('*** save_model')
model = build_model(10,2)
model.summary()
save_model(model=model,filepath=filepath)
del model
def excode_load_model(filepath):
print('*** load_model')
model = load_model(filepath=filepath)
model.summary()
del model
if __name__ == "__main__":
excode_save_model('model_save_test.h5')
excode_load_model('model_save_test.h5')
실행 결과
*** save_model
_________________________________________________________________
Layer (type)
Output Shape Param
#
=================================================================
input_layer (InputLayer) (None, 10)
0
_________________________________________________________________
dense0 (Dense) (None,
128) 1408
_________________________________________________________________
droupout0 (Dropout) (None, 128)
0
_________________________________________________________________
dense1 (Dense) (None,
128) 16512
_________________________________________________________________
droupout1 (Dropout) (None, 128)
0
_________________________________________________________________
out_layers (Dense) (None, 2)
258
=================================================================
Total params: 18,178
Trainable params: 18,178
Non-trainable params: 0
_________________________________________________________________
*** load_model
_________________________________________________________________
Layer (type)
Output Shape Param
#
=================================================================
input_layer (InputLayer) (None, 10)
0
_________________________________________________________________
dense0 (Dense) (None,
128) 1408
_________________________________________________________________
droupout0 (Dropout) (None, 128)
0
_________________________________________________________________
dense1 (Dense) (None,
128) 16512
_________________________________________________________________
droupout1 (Dropout) (None, 128)
0
_________________________________________________________________
out_layers (Dense) (None, 2)
258
=================================================================
Total params: 18,178
Trainable params: 18,178
Non-trainable params: 0
_________________________________________________________________
HDF5 포멧으로 Keras 모델이 파일로 저장된 것을 볼 수 있다.
[관련 포스트]
댓글
댓글 쓰기