[딥러닝] 수어 동작 인식 모델 테스트 (CNN-LSTM)

지현·2022년 6월 15일
0

TensorFlow.js로 진행하다가 오류가 나는 이유를 정확하게 알지 못하겠어서 Python으로 먼저 테스트해보기로 했다.
결과를 먼저 작성하고 Trouble Shooting을 시간순으로 작성하였다.


최종 코드

import tensorflow as tf
import numpy as np
import cv2 

from tensorflow.keras.applications.vgg16 import VGG16
def load_VGG16_model():
  base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224,224,3))
  print("Model loaded")
  print(base_model.summary())
  return base_model
def test_jh():
    x = []
    base_model = load_VGG16_model()
    
    image = cv2.imread('./image.jpeg', 1)
    image = cv2.resize(image, (224,224))
    
    x.append(image)
    x = np.array(x)
    
    x_features = base_model.predict(x)
    x_features = x_features.reshape(x_features.shape[0], x_features.shape[1] * x_features.shape[2], x_features.shape[3])
    
    model = tf.keras.models.load_model('./video_3_LSTM_1_1024.h5')
    answer = model.predict(x_features)
    
    print("result!!!!")
    print(answer)

성공!


Trouble Shooting

첫번째 오류 (해결 못함)

  • 윈도우 환경에서 진행하였는데 다음과 같이 커널이 죽는 오류가 계속 났다. 찾아본 결과 주어진 메모리 할당량을 초과했기 때문이라고 했다. 따라서 맥북으로 환경을 바꿔서 진행했다.

    Canceled future for execute_request message before replies were done
    The Kernel crashed while executing code in the the current cell or a previous cell. Please review the code in the cell(s) to identify a possible cause of the failure. Click here for more info. View Jupyter log for further details.

두번째 오류 (바보 실수)

'str' object has no attribute 'predict'

  • model.json을 load하는 코드
    • 수정 전 (오류)
      model = './video_3_LSTM_1_1024.h5'
    • 수정 후 (오류 해결)
      model = tf.keras.models.load_model('./video_3_LSTM_1_1024.h5')

세번째 오류

  • ValueError: Input 0 of layer "sequential_25" is incompatible with the layer: expected shape=(None, 49, 512), found shape=(None, 7, 7, 512)
    • 이는 VGG16을 거쳐 나온 피쳐값은 (4,) 배열이지만 생성한 LSTM 모델은 (3,) 배열이기 때문이다. 모델 학습을 진행할 때 처럼 아래 코드를 추가하여 행을 맞춰주었다.
      x_features = x_features.reshape(x_features.shape[0], x_features.shape[1] * x_features.shape[2], x_features.shape[3])

=> 최종적으로 Python에서 모델 예측 값을 뽑아내는 데에 성공했다.

참고
https://datascience.stackexchange.com/questions/107357/attributeerror-str-object-has-no-attribute-predict-i-am-developing-garba

profile
화이팅!

2개의 댓글

comment-user-thumbnail
2023년 5월 24일

안녕하세요 글 잘 읽었습니다. 혹시 이전 게시글의 코드에서 많이 짧아지셨는데, 이전 게시글에서 생성한 모델을 테스트 하신건가요? 데이터셋은 AIhub에서 받은 데이터셋을 사용하신건가요?

1개의 답글