def predict_vgg16(model, filename) :
# 모델 사이즈로 이미지 파일을 읽기
image = load_img(filename, target_size=(224, 224))
# image = PIL.Image.Image image mode=RGB size=224x224
# 이미지 데이터를 numpy로 변환
image = img_to_array(image)
# [
# [[211. 184. 163.]
# [225. 193. 170.]
# ...
# [237. 202. 180.]]
# ...
# ]
#
# image.shape = (224, 224, 3)
# 화면에 이미지 보여주기
plt.imshow(image.astype(int))
plt.show()
# vgg16.preprocess_input()을 호출하기 위해 차원을 조정
# 보통 모델을 여러 이미지를 한번에 호출.
# 맨 앞의 1 : 이미지 갯수가 1개라는 것.
# 두번째 224 : 가로
# 세번째 224 : 세로
# 네번째 3 : R, G, B 3개
image = image.reshape((1, 224, 224, 3))
# VGG16 모델 호출을 위해 데이터 전처리.
# -255 ~ 255 사이 값으로 정규화한다.
# 그리고 RGB를 BGR순으로 바꾼다.
image = vgg16.preprocess_input(image)
# 이미지를 모델에 적용
yhat = model.predict(image)
# yhat = [[2.03485320e-06 4.21382174e-06 1.45730738e-07 1.04057870e-06
# 6.61934010e-08 2.63145339e-04 4.49358195e-05 2.03222541e-08
# ... ]] # 1000개 클래스에 대한 결과값.
#
# 모델 적용된 결과를 파싱
label = vgg16.decode_predictions(yhat)
# label = [[('n02655020', 'puffer', 0.9612253), ... ]]
# 가장 확률이 높은 결과를 획득
label = label[0][0]
# label = ('n02655020', 'puffer', 0.9612253)
# 라벨과 라벨을 예측한 확률을 출력
print('%s (%.2f%%)' % (label[1], label[2]*100))
※ VGG16
▶ 데이터 준비
* 인식에 사용할 데이터 다운로드.
!rm -rf imagenet
!mkdir imagenet
# 버섯
!wget -O imagenet/mushroom1.jpg http://farm4.static.flickr.com/3023/2822584107_186167ff68.jpg
!wget -O imagenet/mushroom2.jpg http://farm3.static.flickr.com/2416/1593642808_efcef6c9c2.jpg
!wget -O imagenet/mushroom3.jpg http://farm4.static.flickr.com/3003/2536991564_5f9b2f5b53.jpg
# 강아지
!wget -O imagenet/dog1.jpg http://farm1.static.flickr.com/58/160964915_d708f48d0d.jpg
!wget -O imagenet/dog2.jpg http://farm1.static.flickr.com/51/144906086_049df05364.jpg
!wget -O imagenet/dog3.jpg http://farm3.static.flickr.com/2133/2236535445_ca650757f2.jpg
# 고양이
!wget -O imagenet/cat1.jpg http://farm1.static.flickr.com/131/393656824_bd89c512d0.jpg
!wget -O imagenet/cat2.jpg http://farm1.static.flickr.com/213/505539125_d7193beb76.jpg
!wget -O imagenet/cat3.jpg http://farm1.static.flickr.com/24/63785988_c16c10b4e5.jpg
▶ 예측 위한 함수 선언
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.applications import vgg16
import numpy as np
import matplotlib.pyplot as plt
▶분류 실행
from tensorflow.keras.applications import vgg16
# VGG16 모델 불러오기
model = vgg16.VGG16()
# 모델의 모양을 보여준다.
model.summary()
# 테스트 할 이미지 파일들
files = [
'imagenet/mushroom1.jpg',
'imagenet/mushroom2.jpg',
'imagenet/mushroom3.jpg',
'imagenet/dog1.jpg',
'imagenet/dog2.jpg',
'imagenet/dog3.jpg',
'imagenet/cat1.jpg',
'imagenet/cat2.jpg',
'imagenet/cat3.jpg',
]
# 분류 실행
for file in files :
predict_vgg16(model, file)
▶ 커스텀 데이터 학습 - train_x, train_y에 의한
기존 파일은 다음과 같이 구성됨
dogs/
class1/
file1.jpg
file2.jpg
...
class2/
file21.jpg
file22.jpg
...
class3/
file31.jpg
file32.jpg
...
...
▶커스텀 데이터 다운로드
dogs.tar.gz
!rm -rf dogs.tar.gz
!wget https://github.com/dhrim/bmac_seminar/raw/master/material/dogs.tar.gz
!ls -al
!rm -rf dogs # 풀려있는 기존 dogs 파일 지우기
!tar xvfz dogs.tar.gz # dogs파일 압축 해제
!ls -al dogs # dogs파일 불러오기
▶ 데이터 로딩 함수 정의
import os
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.applications import vgg16
# 폴더를 import
def load_file_names_and_category_names(path):
file_names = []
category_names = []
dir_names = os.listdir(path)
for dir_name in dir_names:
file_names_in_a_dir = os.listdir(path+"/"+dir_name)
for a_file_name in file_names_in_a_dir:
full_file_name = path+"/"+dir_name+"/"+a_file_name
file_names.append(full_file_name)
category_names.append(dir_name)
return file_names, category_names
def load_image_files_into_numpy_array(file_names):
file_count = len(file_names)
data = np.ndarray(shape=(file_count,224,224,3), dtype=np.float64)
for i in range(len(file_names)):
image = load_img(file_names[i], target_size=(224, 224))
image = img_to_array(image)
data[i] = image
return data
def load_custom_data(path, train_ratio=0.8):
file_names, category_names = load_file_names_and_category_names(path)
x = load_image_files_into_numpy_array(file_names)
labels, y = np.unique(category_names, return_inverse=True)
s = int(x.shape[0]*train_ratio)
train_x, test_x = x[:s], x[s:]
train_y, test_y = y[:s], y[s:]
return (train_x, train_y), (test_x, test_y), labels
(train_x, train_y), (test_x, test_y), custom_labels = load_custom_data("dogs")
train_x = vgg16.preprocess_input(train_x)
test_x = vgg16.preprocess_input(test_x)
print(train_x.shape)
print(train_y.shape)
print(test_x.shape)
print(test_y.shape)
print(custom_labels)
→ (1535, 224, 224, 3) (1535,)
→ (384, 224, 224, 3) (384,)
▶ 기존에 있던 학습된 모델 파일 삭제
vgg16_model_path = 'new_trained_from_vgg16.h5'
!rm -rf {vgg16_model_path}
▶ 모델 구조 정의
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import optimizers
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dropout, Flatten, Dense
from tensorflow.keras.models import Model
from tensorflow.keras import models
from tensorflow.keras import layers
from tensorflow.keras import optimizers
from tensorflow.keras.applications import VGG16
# 모델 불러오기
conv_layers = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
conv_layers.summary()
# Convolution Layer를 학습되지 않도록 고정
conv_layers.trainable = False
# 새로운 모델 생성하기
model = models.Sequential()
# VGG16모델의 Convolution Layer를 추가
model.add(conv_layers)
# 모델의 Fully Connected 부분을 재구성
model.add(layers.Flatten())
model.add(layers.Dense(1024, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(10, activation='softmax'))
# 모델
model.summary()
model.save(vgg16_model_path)
from tensorflow.keras.models import load_model
model = load_model(vgg16_model_path)
# 모델 컴파일
model.compile(loss='sparse_categorical_crossentropy',
optimizer="RMSprop",
metrics=['acc'])
# 모델 학습
model.fit(train_x, train_y, epochs=10, batch_size=64, shuffle=True, validation_split=0.1)
model.save(vgg16_model_path)
▶ 결과 확인
import matplotlib.pyplot as plt
loss, acc = model.evaluate(test_x, test_y)
print("loss =",loss)
print("acc =",acc)
y_ = model.predict(test_x)
predicted = np.argmax(y_, axis=1)
# train_x, test_x는 vgg16.prerprocess_input()에 의해 변형되었다.
(_, _), (raw_test_x, _), custom_labels = load_custom_data("dogs")
for i in [0,50,100,150,200]:
print(test_y[i], custom_labels[test_y[i]])
print(predicted[i], custom_labels[predicted[i]])
plt.imshow(raw_test_x[i].astype(int))
plt.show()
8 n02087394-Rhodesian_ridgeback
0 n02085620-Chihuahua
8 n02087394-Rhodesian_ridgeback
0 n02085620-Chihuahua
8 n02087394-Rhodesian_ridgeback
0 n02085620-Chihuahua
▶ VGG16 커스텀 학습 최소 코드
def load_file_names_and_category_names(path):
file_names = []
category_names = []
dir_names = os.listdir(path)
for dir_name in dir_names:
file_names_in_a_dir = os.listdir(path+"/"+dir_name)
for a_file_name in file_names_in_a_dir:
full_file_name = path+"/"+dir_name+"/"+a_file_name
file_names.append(full_file_name)
category_names.append(dir_name)
return file_names, category_names
def load_image_files_into_numpy_array(file_names):
file_count = len(file_names)
data = np.ndarray(shape=(file_count,224,224,3), dtype=np.float64)
for i in range(len(file_names)):
image = load_img(file_names[i], target_size=(224, 224))
image = img_to_array(image)
data[i] = image
return data
def load_custom_data(path, train_ratio=0.8):
file_names, category_names = load_file_names_and_category_names(path)
x = load_image_files_into_numpy_array(file_names)
labels, y = np.unique(category_names, return_inverse=True)
s = int(x.shape[0]*train_ratio)
train_x, test_x = x[:s], x[s:]
train_y, test_y = y[:s], y[s:]
return (train_x, train_y), (test_x, test_y), labels
import os
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras import optimizers
from tensorflow.keras import models
from tensorflow.keras import layers
from tensorflow.keras import optimizers
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dropout, Flatten, Dense
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.applications import vgg16
from tensorflow.keras.applications import VGG16
conv_layers = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
conv_layers.trainable = False
model = models.Sequential()
model.add(conv_layers)
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy', optimizer="RMSprop", metrics=['acc'])
(train_x, train_y), (test_x, test_y), custom_labels = load_custom_data("dogs")
train_x = vgg16.preprocess_input(train_x)
test_x = vgg16.preprocess_input(test_x)
model.fit(train_x, train_y, epochs=10, batch_size=64, shuffle=True)
loss, acc = model.evaluate(test_x, test_y)
print("loss =",loss)
print("acc =",acc)
y_ = model.predict(test_x)
predicted = np.argmax(y_, axis=1)
print(predicted[0], custom_labels[predicted[0]])
▶ 커스텀 데이터 학습 - train_x, train_y에 데이터 증강
import os
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras import optimizers
from tensorflow.keras import models
from tensorflow.keras import layers
from tensorflow.keras import optimizers
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dropout, Flatten, Dense
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.applications import vgg16
from tensorflow.keras.applications import VGG16
conv_layers = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
conv_layers.trainable = False
model = models.Sequential()
model.add(conv_layers)
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy', optimizer="RMSprop", metrics=['acc'])
(train_x, train_y), (test_x, test_y), custom_labels = load_custom_data("dogs")
# train_x = vgg16.preprocess_input(train_x)
test_x = vgg16.preprocess_input(test_x)
# ADD START
data_aug_generator = ImageDataGenerator(
rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True,
vertical_flip=False,
preprocessing_function=vgg16.preprocess_input # 여기에 preprocess 함수를 설정
)
# ADD END
#model.fit(train_x, train_y, epochs=10, batch_size=64, shuffle=True)
model.fit_generator(
data_aug_generator.flow(train_x, train_y, batch_size=64),
steps_per_epoch=train_x.shape[0]/64, # data_size / batch_size
epochs=10)
loss, acc = model.evaluate(test_x, test_y)
print("loss =",loss)
print("acc =",acc)
y_ = model.predict(test_x)
predicted = np.argmax(y_, axis=1)
print(predicted[0], custom_labels[predicted[0]])
▶ 커스텀 데이터 학습 - 디렉토리 구조를 사용한
* 기존 데이터는 다음과 같이 구성됨
dogs/
train/
class1/
file1.jpg
file2.jpg
...
class2/
file21.jpg
file22.jpg
...
class3/
file31.jpg
file32.jpg
...
...
test/
class1/
file8.jpg
file9.jpg
...
class2/
file28.jpg
file29.jpg
...
class3/
file38.jpg
file39.jpg
...
...
▶ 커스텀 데이터 다운로드
!rm -rf dogs_prepared.tar.gz
!wget https://github.com/dhrim/bmac_seminar/raw/master/material/dogs_prepared.tar.gz
!ls -al
!rm -rf dogs_prepared
!tar xvfz dogs_prepared.tar.gz
!ls -al dogs_prepared
▶디렉토리로 준비된 데이터로 커스텀 학습
import os
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras import optimizers
from tensorflow.keras import models
from tensorflow.keras import layers
from tensorflow.keras import optimizers
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dropout, Flatten, Dense
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.applications import vgg16
from tensorflow.keras.applications import VGG16
conv_layers = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
conv_layers.trainable = False
model = models.Sequential()
model.add(conv_layers)
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy', optimizer="RMSprop", metrics=['acc'])
# (train_x, train_y), (test_x, test_y), custom_labels = load_custom_data("dogs")
# train_x = vgg16.preprocess_input(train_x)
# test_x = vgg16.preprocess_input(test_x)
# ADD START
data_aug_generator = ImageDataGenerator(
rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True,
vertical_flip=False,
preprocessing_function=vgg16.preprocess_input
)
data_no_aug_generator = ImageDataGenerator(
preprocessing_function=vgg16.preprocess_input
)
train_data_generator = data_aug_generator.flow_from_directory(
"dogs_prepared/train",
target_size=(224,224),
batch_size=64,
class_mode='sparse'
)
test_data_generator = data_no_aug_generator.flow_from_directory(
"dogs_prepared/test",
target_size=(224,224),
class_mode='sparse'
)
# ADD END
#model.fit(train_x, train_y, epochs=10, batch_size=64, shuffle=True)
model.fit_generator(
train_data_generator,
validation_data=test_data_generator,
validation_steps=5,
steps_per_epoch=train_data_generator.samples/64, # data_size / batch_size
epochs=10
)
# y_ = model.predict(test_x)
y_ = model.predict_generator(
test_data_generator,
steps=test_data_generator.samples/64
)
custom_labels = list(test_data_generator.class_indices.keys()) # ADDED
predicted = np.argmax(y_, axis=1)
print(predicted[0], custom_labels[predicted[0]])
▶ 디렉토리 구조의 데이터의 학습 최소 코드
import os
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras import optimizers
from tensorflow.keras import models
from tensorflow.keras import layers
from tensorflow.keras import optimizers
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dropout, Flatten, Dense
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.applications import vgg16
from tensorflow.keras.applications import VGG16
conv_layers = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
conv_layers.trainable = False
model = models.Sequential()
model.add(conv_layers)
model.add(layers.Flatten())
model.add(layers.Dense(1024, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(10, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy', optimizer="RMSprop", metrics=['acc'])
data_aug_generator = ImageDataGenerator(
rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True,
vertical_flip=False,
preprocessing_function=vgg16.preprocess_input
)
data_no_aug_generator = ImageDataGenerator(
preprocessing_function=vgg16.preprocess_input
)
train_data_generator = data_aug_generator.flow_from_directory(
"dogs_prepared/train",
target_size=(224,224),
batch_size=64,
class_mode='sparse'
)
test_data_generator = data_no_aug_generator.flow_from_directory(
"dogs_prepared/test",
target_size=(224,224),
class_mode='sparse'
)
model.fit_generator(
train_data_generator,
validation_data=test_data_generator,
validation_steps=5,
steps_per_epoch=train_data_generator.samples/64,
epochs=10
)
y_ = model.predict_generator(
test_data_generator,
steps=test_data_generator.samples/64
)
custom_labels = list(test_data_generator.class_indices.keys())
predicted = np.argmax(y_, axis=1)
print(predicted[0], custom_labels[predicted[0]])
▶ Resnet으로 전이학습
import os
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras import optimizers
from tensorflow.keras import models
from tensorflow.keras import layers
from tensorflow.keras import optimizers
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dropout, Flatten, Dense
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
# from tensorflow.keras.applications import vgg16
# from tensorflow.keras.applications import VGG16
from tensorflow.keras.applications import resnet50 # 모듈이름은 소문자
from tensorflow.keras.applications import ResNet50 # 클래스이름은 대문자
# conv_layers = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
conv_layers = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
conv_layers.trainable = False
model = models.Sequential()
model.add(conv_layers)
model.add(layers.Flatten())
model.add(layers.Dense(1024, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(10, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy', optimizer="RMSprop", metrics=['acc'])
data_aug_generator = ImageDataGenerator(
rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True,
vertical_flip=False,
# preprocessing_function=vgg16.preprocess_input
preprocessing_function=resnet50.preprocess_input
)
data_no_aug_generator = ImageDataGenerator(
# preprocessing_function=vgg16.preprocess_input
preprocessing_function=resnet50.preprocess_input
)
train_data_generator = data_aug_generator.flow_from_directory(
"dogs_prepared/train",
target_size=(224,224),
batch_size=64,
class_mode='sparse'
)
test_data_generator = data_no_aug_generator.flow_from_directory(
"dogs_prepared/test",
target_size=(224,224),
class_mode='sparse'
)
model.fit_generator(
train_data_generator,
validation_data=test_data_generator,
validation_steps=5,
steps_per_epoch=train_data_generator.samples/64,
epochs=10
)
y_ = model.predict_generator(
test_data_generator,
steps=test_data_generator.samples/64
)
custom_labels = list(test_data_generator.class_indices.keys())
predicted = np.argmax(y_, axis=1)
print(predicted[0], custom_labels[predicted[0]])
'Deep learning > Code' 카테고리의 다른 글
object_detection_yolo_keras (2) | 2020.01.22 |
---|---|
object_detection_yolo_darknet (0) | 2020.01.22 |
2020_01_22 CIFAR10 (0) | 2020.01.22 |
2020_01_21 CNN (0) | 2020.01.21 |
2020_01_21 IRIS (0) | 2020.01.21 |