본문 바로가기
IT 프로그래밍 관련/딥러닝

CNN으로 이미지 파일 시험코드

by 지나는행인 2021. 3. 3.
728x90

코랩 환경.

 

다 만들어진 CNN 모델이 얼마나 잘 맞나 테스트를 위해, 내 컴퓨터에 있는 이미지 파일로 시험해본다.

import numpy as np
from google.colab import files
from tensorflow.keras.preprocessing import image


uploaded = files.upload()
for fn in uploaded.keys() :
  path = '/content/'+ fn
  img = image.load_img( path, target_size=( 300, 300) ) 
  x = image.img_to_array(img)

  print(x.shape)
  x=np.expand_dims( x, axis = 0 )               
  print(x.shape)
  images = np.vstack( [ x ]  )

  classes = model.predict(  images, batch_size = 10 )

  print(classes)

  if classes[0] < 0.5 :                             
    print(fn + 'is a horse')
  else :
    print(fn + 'is a human')

## target_size 는 훈련용 이미지 파일과 맞춘다. 

## if classes[0] > 0.5 :                             
    print(fn + 'is a horse')
    else :
    print(fn + 'is a human')              ## 여기서 0과,1은 코랩환경에서 작업한 폴더의 순서이다.

 

위의 코드의 작업은 말이냐 사람이냐의 문제로, 폴더는 이름순으로, house가 먼저.  human이  그 다음으로 순서서 매겨졌다.  따라서 house가 0 , human이 1이 되고,  classese[0]이 0.5보다 작을때. 즉 0일때 ,  순서상 0인 horse가 출력,

반대의 경우에 human이 출력된다.

댓글