인프런 커뮤니티 질문&답변

아쿠아라이드님의 프로필 이미지
아쿠아라이드

작성한 질문수

커피 한 잔으로 입문하는 Convolutional Neural Network

Utility Functions - learning_env_setting2

tf.keras.models.load_model () 함수 질문

작성

·

510

0

model.save('경로') 를 통해 학습시킨 모델을 저장하는 것은 작동이 잘 되는 것을 확인했습니다만... 이미 저장되어 있는 모델을 load_model()함수를 통해 불러오기 할 때  아래와 같은 ValueError 가 발생하는 이유가 무엇인지 알수 있을지요?

아무래 해봐도 저는 아래와 같은 예외가 발생하면서 불러오기가 실패합니다.

구글 검색을 하다보면 Input shape 을 설정해 주면 해결된다는 답글이 있는 것도 같은데 subclassing 으로 모델 정의할 때 각 layer 별로 Input shape 을 계산해서 넣어주어야지만 load_model 기능을 사용할 수 있는건 아닐것 같은데... 답답하네요~~ 

제가 GTX970 으로 GPU 텐서플로우를 돌리려 하다보니 최신 버전은 GPU 가 안먹혀서 tensorflow 2.1 / CUDA10.1 을 설치해서 실습 따라오고 있었는데.... 혹시 tensorflow 나 CUDA버전에 따른 차이인 것일지요?

ValueError: Could not find matching function to call loaded from the SavedModel. Got:
  Positional arguments (2 total):
    * Tensor("x:0", shape=(None, 28, 28, 1), dtype=float32)
    * Tensor("training:0", shape=(), dtype=bool)
  Keyword arguments: {}

Expected these arguments to match one of the following 4 option(s):

Option 1:
  Positional arguments (2 total):
    * TensorSpec(shape=(None, 28, 28, 1), dtype=tf.float32, name='x')
    * False
  Keyword arguments: {}

Option 2:
  Positional arguments (2 total):
    * TensorSpec(shape=(None, 28, 28, 1), dtype=tf.float32, name='x')
    * True
  Keyword arguments: {}

Option 3:
  Positional arguments (2 total):
    * TensorSpec(shape=(None, 28, 28, 1), dtype=tf.float32, name='input_1')
    * True
  Keyword arguments: {}

Option 4:
  Positional arguments (2 total):
    * TensorSpec(shape=(None, 28, 28, 1), dtype=tf.float32, name='input_1')
    * False
  Keyword arguments: {}

===== 아래는 full code 입니다. ====

import numpy as np
import matplotlib.pyplot as plt
from termcolor import colored

import tensorflow as tf
import tensorflow_datasets as tfds
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Activation, Dropout
from tensorflow.keras.losses import SparseCategoricalCrossentropy
from tensorflow.keras.optimizers import Adam, SGD
from tensorflow.keras.metrics import Mean, SparseCategoricalAccuracy
# from utils.learning_env_setting import dir_setting, get_classification_matrics, save_metics_model, continue_setting

# data pre processing
def get_mnist_dataset_and_normalize(ration_batch_trainn_batch_test):
    
    def normalization(imageslabels):
        images = tf.cast(images, tf.float32)/255
        labels = tf.cast(labels, tf.int32)
        return images, labels
    
    (train_validation_ds, test_ds), ds_info = tfds.load(name='mnist', as_supervised = True, shuffle_files = True
                                                        with_info = True, split = ['train''test'])

    n_train_validation = ds_info.splits['train'].num_examples
    n_train = int(ratio * n_train_validation)
    n_validation = n_train_validation - n_train

    train_ds = train_validation_ds.take(n_train)
    validation_ds = train_validation_ds.skip(n_train)

    train_ds = train_ds.map(normalization).shuffle(n_train).batch(n_batch_train)
    validation_ds = validation_ds.map(normalization).batch(n_batch_train)
    test_ds = test_ds.map(normalization).batch(n_batch_test)
    return train_ds, validation_ds, test_ds

class CNN_Model(Model):
    def __init__(self):
        super(CNN_Model, self).__init__()
        
        # feature extractor
        self.conv1 = Conv2D(filters=8, kernel_size=5, padding='same', activation='relu')
        self.conv1_maxpool = MaxPooling2D(pool_size=2, strides=2)
        self.conv2 = Conv2D(filters=8, kernel_size=5, padding='same', activation='relu')
        self.conv2_maxpool = MaxPooling2D(pool_size=2, strides=2)
        
        # Classifier
        self.flatten = Flatten()
        self.dense1 = Dense(units=64, activation='relu')
        self.dense1_dropout = Dropout(0.5)
        self.dense2 = Dense(units=10, activation='softmax')
        
    def call(selfx):
        x = self.conv1(x)
        x = self.conv1_maxpool(x)
        x = self.conv2(x)
        x = self.conv2_maxpool(x)
        x = self.flatten(x)
        x = self.dense1(x)
        x = self.dense2(x)
        return x
    
@tf.function
def trainer(): 
    global model, loss_object, loss_train, acc_train, optimizer
    for images, labels in train_ds:
        with tf.GradientTape() as tape:
            predictions = model(images)
            loss = loss_object(labels, predictions)
        gradients = tape.gradient(loss, model.trainable_variables)
        optimizer.apply_gradients(zip(gradients, model.trainable_variables))
    
    loss_train(loss)
    acc_train(labels, predictions)
    
@tf.function
def validation(): 
    global model, loss_object, loss_validation, acc_validation
    for images, labels in validation_ds:
        predictions = model(images)
        loss = loss_object(labels, predictions)
    
    loss_validation(loss)
    acc_validation(labels, predictions)
    
@tf.function
def tester(): 
    global model, loss_object, loss_test, acc_test
    for images, labels in test_ds:
        predictions = model(images)
        loss = loss_object(labels, predictions)
    
    loss_test(loss)
    acc_test(labels, predictions)

EPOCHS = 10
n_batch_train = 32
n_batch_test = 128
ratio = 0.8

train_ds, validation_ds, test_ds = get_mnist_dataset_and_normalize(ratio, n_batch_train, n_batch_test)

model = CNN_Model()

model.compile(optimizer='adam',loss = 'sparse_categorical_crossentropy', metrics =['accuracy'])
history = model.fit(train_ds, validation_data=validation_ds, epochs=EPOCHS)


loss_object = SparseCategoricalCrossentropy()
loss_test = Mean()
acc_test = SparseCategoricalAccuracy()

def tester(): 
    global model, loss_object, loss_test, acc_test
    for images, labels in test_ds:
        predictions = model(images)
        loss = loss_object(labels, predictions)
    
    loss_test(loss)
    acc_test(labels, predictions)
tester()
print(colored('TEST','cyan','on_white'))
print("Test Loss : {:.4f} / Test Accuracy : {:.2f}".format(loss_test.result(), acc_test.result()*100))

model.save('MyModel')
model1 = tf.keras.models.load_model('MyModel')


def tester(): 
    global model1, loss_object, loss_test, acc_test
    for images, labels in test_ds:
        predictions = model1(images)
        loss = loss_object(labels, predictions)
    
    loss_test(loss)
    acc_test(labels, predictions)
tester()
print(colored('TEST','cyan','on_white'))
print("Test Loss : {:.4f} / Test Accuracy : {:.2f}".format(loss_test.result(), acc_test.result()*100))

답변

답변을 기다리고 있는 질문이에요
첫번째 답변을 남겨보세요!
아쿠아라이드님의 프로필 이미지
아쿠아라이드

작성한 질문수

질문하기