본문 바로가기

딥러닝

VGG16을 사용해보자

from keras.applications.vgg16 import VGG16
model = VGG16()
from keras.preprocessing.image import img_to_array
from keras.applications.vgg16 import preprocess_input
from keras.applications.vgg16 import decode_predictions
from keras.preprocessing.image import load_img

 

image = load_img('mug.jpg', target_size=(224,224))

이미지를 224,224 크기로 불러온다

 

image = img_to_array(image)

 

 

image= image.reshape((1,image.shape[0], image.shape[1], image.shape[2]))

네트워크는 하나 이상의 이미지를 입력이 요구한다.

이말은 samples, rows, columns, channel  의 형태가 되어야한다

우리는 하나의 이미지만을 가지고있으므로 reshape() 함수를 호출하여 차원을 더함으로써

배열 구조를 다시 만들어야 한다.

 

 

image = preprocess_input(image)

각각의 픽셀로부터 훈련데이터셋에서 계산된 평균 RGB 값을 추출해내는 과정이다.

케라스는 새로운 네트워크에 넣기 위해 preprocess_input()이라는 함수를 제공하고 있다.

 

yhat = model.predict(image)

1000개의 알려진 물체 중 어디에 속할 확률을 예측하는 predict() 함수를 호출할 수 있다.

 

label = decode_predictions(yhat)

이것의 출력값은

output

내림차순으로 가장 확률이 높은것이 최상단에 위치한다.

label = label[0][0]
print('%s (%.2f%%)' % (label[1],label[2]*100))

coffee_mug (58.00%) 불러온사진에 대한 예측이다. 

'딥러닝' 카테고리의 다른 글

개와 고양이를 ResNet을 이용해서 학습해보자.  (0) 2019.09.29