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()