본문 바로가기

딥러닝

4장 10. fashion mnist 신경망에 사진 넣고 예측하는지 확인해보기

밑바닥부터 시작하는 딥러닝 4장 참고

 

# 사진한장 잘 맞추는지 확인하기 

#1. 필요한 패키지 로드 
import tensorflow as  tf
import cv2

#2. 이미지 있는 위치 저장 
img_path = '/content/sample_data/ankleboot.png' # 앵커부츠사진 

img = cv2.imread(img_path) # 이미지 숫자로 변환 코드 
#img = cv2.bitwise_not(img) # 색 반전시키는 코드 (흰<->검)
print(img.shape)

# 3. 28 x28 로 reshape 합니다.
import  matplotlib.pyplot  as plt 

resize_img = cv2.resize( img, (28, 28), interpolation = cv2.INTER_CUBIC)
plt.imshow( resize_img)
print( resize_img.shape ) # (28, 28, 3)

# 4. 흑백으로 변경 
import numpy as np
x2=tf.image.rgb_to_grayscale(resize_img)
print(x2.shape)

#5. 학습시킨 모델을 다시 불러오는 코드

from tensorflow.keras.models import load_model

new_model = load_model('/content/sample_data/fashion_model7.h5')

#6. 이미지 사이즈를 신경망에 들어갈 수 있도록 28x28 --> 1x784 로 변경 

x3 = tf.reshape(x2,(1,784))

results = new_model.predict(x3)
np.argmax(results)


#7. 9번의 상품이 뭔지 확인하기 

target_dict = {
 0: 'T-shirt/top',
 1: 'Trouser',
 2: 'Pullover',
 3: 'Dress',
 4: 'Coat',
 5: 'Sandal',
 6: 'Shirt',
 7: 'Sneaker',
 8: 'Bag',
 9: 'Ankle boot',
}

target_dict[9]

Ankle boot

Ankle boot 사진 을 넣어봤는데 잘 맞추었다 

 

다른 사진들도 넣어서 실습해보자