inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

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

Utility Functions - learning_env_setting2

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

611

아쿠아라이드

작성한 질문수 22

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))

머신러닝 배워볼래요? 인공신경망 딥러닝

답변 0

numpy의 shape

0

580

1

tfjs-node 안깔려서

0

1097

1

강의자료 요청드립니다!

0

536

1

하이퍼 파라미터 튜닝 범위

0

605

1

딥러닝 코드에 Batch Normalization 적용해보기 질문입니다

0

576

1

flyctl 관련 재문의 드립니다.

0

591

1

mac os ) zip 파일 dataframe 오류

0

659

1

flyctl 배포 관련 에러 문의드립니다.

0

749

1

딥러닝으로 Regression 문제 적용해보기 (House Price Kaggle 문제) 질문입니다

0

475

1

numpy.random.default_rng/hyperopt버젼

0

1166

1

Binary Classfication 딥러닝 적용해보기 질문입니다

0

363

1

업로드 후 홈화면에서 이미지가 보이지 않습니다

0

581

1

n_iter 횟수 넘음 질문

0

484

2

image 파일에서 vscode로 드래그 앤 드롭을 처리할 수 없다고 합니다

0

1009

1

upload 페이지에 아무것도 안 뜨는 현상

0

433

1

Convolution Implementation 강좌 내용 질문있습니다

2

213

2

learning_env_setting1 질문

0

167

1

강사님, 4개의 질문 드립니다.

0

184

1

강의 감사합니다. 질문 드립니다.

0

171

1

강사님! 두 가지 질문 드립니다.

0

198

1

sequential 함수를 못불러 옵니다

0

194

1

convolution, correlation 연산에 대한 질문

0

464

1

@tf.function 데코레이터 사용시 ValueError 문제 질문

0

222

0

LeNet 구조에 대해서 질문이 있습니다

0

178

1