05-09 08:16
Recent Posts
Recent Comments
관리 메뉴

너와나의 관심사

Keras 로 학습된 모델로 객체생성(compile) 하고 싶다면 본문

머신러닝/딥러닝

Keras 로 학습된 모델로 객체생성(compile) 하고 싶다면

벤치마킹 2022. 2. 6. 03:05

keras 로 학습된 모델의 결과물로 모델을 재학습 하거나 모델의 구조를 알고 싶을때가 있다. 

모델의 define , model buid 정보를 몰라도  keras model 의 객체를 알수있다. (많은 삽질을 했다)

variable , saved_model.pb index  결과물  SavedModel 을 가지고 있을때 .. 다시 모델을 로딩해서 layer 를 변경 추가 가능하다. 방법은 아래 그림에서 처럼 Keras Model 이 아니라 다른 API 를 사용해야한다. 

 

 

 

 

import os
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import tensorflow as tf
import tensorflow_hub as hub


def create_model():
    # 모델 define
    model = tf.keras.models.Sequential([
        # The first convolution
        tf.keras.layers.Conv2D(16, (3, 3), activation='relu', input_shape=(300, 300, 3)),
        tf.keras.layers.MaxPool2D(2, 2),
        # The second convolution
        tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
        tf.keras.layers.MaxPool2D(2, 2),
        # The third convolution
        tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
        tf.keras.layers.MaxPool2D(2, 2),
        # The fourth convolution
        tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
        tf.keras.layers.MaxPool2D(2, 2),
        # The fifth convolution
        tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
        tf.keras.layers.MaxPool2D(2, 2),
        # Flatten
        tf.keras.layers.Flatten(),
        # 512 Neuron (Hidden layer)
        tf.keras.layers.Dense(512, activation='relu'),
        # 1 Output neuron
        tf.keras.layers.Dense(1, activation='sigmoid')
    ])


    return model;


loaded = tf.keras.models.load_model("./training_2")
loaded.summary()

print("Done")
Comments