미해결
딥러닝 CNN 완벽 가이드 - Fundamental 편
CNN Cifar10 VGG16으로 전이학습 시 val_accuracy가 0.1로 고정되어 나옵니다
안녕하세요. CIFAR10 데이터를 가지고, VGG16으로 전이학습을 해보려고 했습니다. 데이터가 충분하다고 생각해서 뒤에서 2개의 block을 trainable=True로 바꾸고, top 부분은 globalaveragePool 이후에 Dense로 Softmax를 적용했는데, 훈련이 아주 이상하게 동작하는데, 이유를 알 수 있을까요?
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, BatchNormalization, Dropout
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.utils import to_categorical, normalize
import numpy as np
import os
import matplotlib.pyplot as plt
import pandas as pd
%matplotlib inline
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
print(x_train.max(), x_train.min())
x_train = normalize(x_train)
x_test = normalize(x_test)
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)
print(x_train.max(), x_train.min(), x_test.max(), x_test.min())
train_datagen = ImageDataGenerator(
rotation_range = 45,
width_shift_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True
)
train_datagen.fit(x_train)
train_generator = train_datagen.flow(
x_train,
y_train,
batch_size = 128)
model_vgg = VGG16(weights='imagenet', include_top=False)
for layer in model_vgg.layers:
layer.trainable = False
for layer in model_vgg.layers[-8:]:
layer.trainable = True
inputs = model_vgg.output
x = tf.keras.layers.GlobalAveragePooling2D()(inputs)
x = Dense(256, activation='relu')(x)
x = Dropout(0.3)(x)
x = Dense(128, activation='relu')(x)
x = Dropout(0.3)(x)
x = Dense(10, activation='softmax')(x)
new_model = tf.keras.models.Model(model_vgg.input, x)
new_model.summary()
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping
rlr_cb = ReduceLROnPlateau(monitor='val_loss', factor=0.3, patience=3, mode='min', verbose=1)
ely_cb = EarlyStopping(monitor='val_loss', patience=5, mode='min', verbose=1)
new_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
history = new_model.fit_generator(train_generator,
steps_per_epoch = 391,
epochs = 100,
validation_data = (x_test, y_test),
#callbacks=[rlr_cb, ely_cb]
)