Pre-trained Models
import tensorflow as tf
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras import models
from tensorflow.keras import layers
from tensorflow.keras import optimizers
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras.preprocessing.image import img_to_array, array_to_img
from tensorflow.keras.applications.vgg16 import decode_predictions
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train = x_train[:200].reshape(-1,28,28)
x_train = np.stack([x_train] * 3, axis=3)
train_X = np.asarray([img_to_array(array_to_img(im, scale=False).resize((32,32))) for im in x_train])
y_train = y_train[:200]
train_Y = tf.one_hot(y_train, 10, on_value=1.0, off_value=0.0)
x_test = x_test[:50].reshape(-1,28,28)
x_test = np.stack([x_test] * 3, axis=3)
test_X = np.asarray([img_to_array(array_to_img(im, scale=False).resize((32,32))) for im in x_test])
y_test = y_test[:50]
test_Y = tf.one_hot(y_test, 10, on_value=1.0, off_value=0.0)
x_show = x_test[:3]
x_show = np.asarray([img_to_array(array_to_img(im, scale=False).resize((224,224))) for im in x_show]).astype(int)
y_show = tf.one_hot(y_test[:3], 1000, on_value=1.0, off_value=0.0)
model = VGG16()
pred = decode_predictions(model.predict(x_show), top=1)
for i in range(3):
print("예측 : ", pred[i][0][1])
print("정답 : ", np.argmax(y_show[i]))
plt.imshow(x_show[i])
plt.show()
(1) 사전 학습된 모델에 새로운 분류기를 추가 및 변경
(2) 사전 학습된 네트워크를 고정 (Freeze)
(3) 추가된 새 분류기 학습
x_train = np.asarray([img_to_array(array_to_img(im, scale=False).resize((32,32))) for im in x_train])
x_test = np.asarray([img_to_array(array_to_img(im, scale=False).resize((32,32))) for im in x_test])
y_train = tf.one_hot(y_train, 10, on_value=1.0, off_value=0.0)
y_test = tf.one_hot(y_test, 10, on_value=1.0, off_value=0.0)
conv_base = VGG16(weights='imagenet',
include_top=False,
input_shape=(32, 32, 3))
model = models.Sequential()
model.add(conv_base)
model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dense(10,activation = 'softmax'))
conv_base.trainable = False
model.compile(loss='categorical_crossentropy',
optimizer=optimizers.Adam(learning_rate=2e-5),
metrics=['acc'])
history = model.fit(
x_train,
y_train,
epochs=50,
validation_data=(x_test, y_test),
verbose = 2)
plt.plot(range(len(history.history['acc'])), history.history['acc'], label='Training acc')
plt.plot(range(len(history.history['acc'])), history.history['val_acc'], label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()
plt.plot(range(len(history.history['acc'])), history.history['loss'], label='Training loss')
plt.plot(range(len(history.history['acc'])), history.history['val_loss'], label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()
pred = model.predict(x_test[:3])
for i in range(3):
print("예측 : ", np.argmax(pred[i]))
print("정답 : ", np.argmax(y_test[i]))
plt.imshow(x_test[i].astype(int))
plt.show()
(4) 사전 학습된 네트워크의 일부 레이어를 고정 해제 (Unfreeze)
(5) 추가된 분류기와 고정 해제된 일부 레이어를 학습
model.trainable = True
set_trainable = False
for layer in conv_base.layers:
if layer.name == 'block5_conv1':
set_trainable = True
if set_trainable:
layer.trainable = True
else:
layer.trainable = False
model.compile(loss='categorical_crossentropy',
optimizer=optimizers.Adam(learning_rate=2e-5),
metrics=['acc'])
history = model.fit(
x_train,
y_train,
epochs=10,
validation_data=(x_test, y_test),
verbose = 2)
plt.plot(range(len(history.history['acc'])), history.history['acc'], label='Training acc')
plt.plot(range(len(history.history['acc'])), history.history['val_acc'], label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()
plt.plot(range(len(history.history['acc'])), history.history['loss'], label='Training loss')
plt.plot(range(len(history.history['acc'])), history.history['val_loss'], label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()
pred = model.predict(x_test[:3])
for i in range(3):
print("예측 : ", np.argmax(pred[i]))
print("정답 : ", np.argmax(y_test[i]))
plt.imshow(x_test[i].astype(int))
plt.show()