가장 쉬운 인계학습 transfer learning by 바죠

가장 쉬운 인계학습 transfer learning

일반적인 특수 목적에 개발된 인공신경망과 가중치를 대부분 그대로 사용하는 학습방법, 추가로 일부의 신경망을 부착하는 학습방법이다.  VGG, Inception, MobileNet



잘 개발된 인공신경망을 그대로 도입하고 조금의 수정 작업을 통해서 아주 성능이 뛰어난 새로운 신경망을 만들어 사용하는 방법이다.
모델의 출력층을 top layer라고 부른다. 
이러한 최종 단계를 표시하는 층을 대신하여 새로운 layer를 만들어서 자신의 문제에 적용할 수 있다. 
예를 들어, 분류문제를 풀고 있다면, 자신의 상황에 맞는 한층의 분류층을 추가할 수 있다.
아울러, 최종 층뿐만아니라 최종층에 가까운 다수의 층들에 있는 가중치들을 총괄적으로 최적화 할 수 있다.
기존의 잘 정립된 모델들은 다수의 은닉층들을 가진다. 따라서, top layer 이전의 수 층을 추가로 학습할 수 있다.


# Instantiate convolutional base
from keras.applications import VGG16
conv_base = VGG16(weights='imagenet', include_top=False, input_shape=(img_width, img_height, 3))  # 3 = number of channels in RGB pictures
conv_base.summary()

# Extract features
import os, shutil
from keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(rescale=1./255)
batch_size = 32

def extract_features(directory, sample_count):
    features = np.zeros(shape=(sample_count, 7, 7, 512))  # Must be equal to the output of the convolutional base
    labels = np.zeros(shape=(sample_count))
    # Preprocess data
    generator = datagen.flow_from_directory(directory, target_size=(img_width,img_height), batch_size = batch_size, class_mode='binary')
    # Pass data through convolutional base
    i = 0
    for inputs_batch, labels_batch in generator:
        features_batch = conv_base.predict(inputs_batch)
        features[i * batch_size: (i + 1) * batch_size] = features_batch
        labels[i * batch_size: (i + 1) * batch_size] = labels_batch
        i += 1
        if i * batch_size >= sample_count:
            break
    return features, labels
    
train_features, train_labels = extract_features(train_dir, train_size)  # Agree with our small dataset size
validation_features, validation_labels = extract_features(validation_dir, validation_size)
test_features, test_labels = extract_features(test_dir, test_size)

IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3)

# 사전 훈련된 모델 MobileNet V2에서 기본 모델을 생성합니다.
base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,   include_top=False, weights='imagenet')
base_model.trainable = False

base_model.summary()
global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
feature_batch_average = global_average_layer(feature_batch)
print(feature_batch_average.shape)

prediction_layer = keras.layers.Dense(1)
prediction_batch = prediction_layer(feature_batch_average)
print(prediction_batch.shape)

model = tf.keras.Sequential([base_model,  global_average_layer,prediction_layer])
base_learning_rate = 0.0001
model.compile(optimizer=tf.keras.optimizers.RMSprop(lr=base_learning_rate),
              loss=tf.keras.losses.BinaryCrossentropy(from_logits=True), metrics=['accuracy'])
model.summary()
len(model.trainable_variables)
initial_epochs = 10
validation_steps=20
loss0,accuracy0 = model.evaluate(validation_batches, steps = validation_steps)
history = model.fit(train_batches, epochs=initial_epochs, validation_data=validation_batches)

acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
plt.figure(figsize=(8, 8))
plt.subplot(2, 1, 1)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.ylabel('Accuracy')
plt.ylim([min(plt.ylim()),1])
plt.title('Training and Validation Accuracy')

plt.subplot(2, 1, 2)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.ylabel('Cross Entropy')
plt.ylim([0,1.0])
plt.title('Training and Validation Loss')
plt.xlabel('epoch')
plt.show()

base_model.trainable = True
# 기본 모델에 몇 개의 층이 있는지 확인 합니다.
print("Number of layers in the base model: ", len(base_model.layers))

# 해당 층 이후부터 미세 조정
fine_tune_at = 100

# `fine_tune_at` 층 이전의 모든 층을 고정
for layer in base_model.layers[:fine_tune_at]:
    layer.trainable =  False
model.compile(loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
              optimizer = tf.keras.optimizers.RMSprop(lr=base_learning_rate/10),
              metrics=['accuracy'])

model.summary()
len(model.trainable_variables)
fine_tune_epochs = 10
total_epochs =  initial_epochs + fine_tune_epochs

history_fine = model.fit(train_batches,
                         epochs=total_epochs,
                         initial_epoch =  history.epoch[-1],
                         validation_data=validation_batches)
acc += history_fine.history['accuracy']
val_acc += history_fine.history['val_accuracy']

loss += history_fine.history['loss']
val_loss += history_fine.history['val_loss']
plt.figure(figsize=(8, 8))
plt.subplot(2, 1, 1)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.ylim([0.8, 1])
plt.plot([initial_epochs-1,initial_epochs-1],
          plt.ylim(), label='Start Fine Tuning')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(2, 1, 2)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.ylim([0, 1.0])
plt.plot([initial_epochs-1,initial_epochs-1],
         plt.ylim(), label='Start Fine Tuning')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.xlabel('epoch')
plt.show()



------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------
iterative learning

폴더를 만들고 정보를 저장하는 방식. 이것을 이용하면 iterative learning이 가능하다.

if True:    
      if os.path.exists('my_model_tf'):        
        del model        
        model = tf.keras.models.load_model('my_model_tf')           
        print('reading')
if False:    
    history = model.fit(X_train, y_train, batch_size = 100, epochs = 10, validation_split = 0.2, verbose = 1)    
    _, accuracy = model.evaluate(X_test, y_test, batch_size=100, verbose=1)
if True:        
    history = model.fit(X_train, y_train, batch_size = 100, epochs = 10, validation_data = (X_test,y_test), verbose = 1)    
    _, accuracy = model.evaluate(X_test, y_test, batch_size=100, verbose=1)


if True:
    model.save('my_model_tf',save_format='tf')
    print('writing')


----------------------------------------------------------------------------------------------------





------------------------------------------------------------------------------------------------------------------------


------------------------------------------------------------------------------------------------------------------------

from tensorflow.keras.preprocessing.image import ImageDataGenerator

datagen = ImageDataGenerator(
        samplewise_center=True,  # set each sample mean to 0
        rotation_range=10,  # randomly rotate images in the range (degrees, 0 to 180)
        zoom_range = 0.1, # Randomly zoom image 
        width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)
        height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)
        horizontal_flip=True,  # randomly flip images
        vertical_flip=False) # we don't expect Bo to be upside-down so we will not flip vertically

# load and iterate training dataset
train_it = datagen.flow_from_directory('./COVID/train/', 
                                       target_size=(224, 224), 
                                       color_mode='rgb', 
                                       class_mode='binary', 
                                       batch_size=8)
# load and iterate validation dataset
valid_it = datagen.flow_from_directory('./COVID/test/', 
                                      target_size=(224, 224), 
                                      color_mode='rgb', 
                                      class_mode='binary', 
                                      batch_size=8)

------------------------------------------------------------------------------------------------------------------------



multi-class classification의 경우, 5종 분류인 경우, 폴더 5개를 만들어서 준비한다.
train_it.class_indices

from keras.preprocessing.image import ImageDataGenerator
from keras.utils import np_utils
from keras.models import Sequential
from keras.preprocessing.image import array_to_img, img_to_array, load_img
from keras.layers import *
from keras.callbacks import ModelCheckpoint, EarlyStopping
import os
import pandas as pd
import numpy as np
from sklearn.metrics import accuracy_score

trainDataGen = ImageDataGenerator(rescale=1./255,
rotation_range = 30,  width_shift_range=0.1,   height_shift_range=0.1,  shear_range=0.2,  zoom_range=0.2,  horizontal_flip=False,   vertical_flip=False,   fill_mode='nearest')
trainGenSet = trainDataGen.flow_from_directory( path + 'train',    batch_size=32,    target_size =(28,28), class_mode='categorical')

trainDataGen.class_indices
model = Sequential()
model.add(Conv2D(64, kernel_size=(3,3), padding='same', input_shape=(28,28,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(128, kernel_size=(3,3),padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(256, kernel_size=(3,3), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(5, activation='softmax'))
model.summary()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit_generator(   trainGenSet, steps_per_epoch=20, epochs=200,    validation_data=testGenSet, validation_steps=10)
scores = model.evaluate_generator(testGenSet)
print(scores)



-------------------------

from tensorflow.keras.preprocessing.image import ImageDataGenerator

datagen = ImageDataGenerator(
    rotation_range=10,  # randomly rotate images in the range (degrees, 0 to 180)
    zoom_range=0.1,  # Randomly zoom image
    width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)
    height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)
    horizontal_flip=True,  # randomly flip images horizontally
    vertical_flip=False, # Don't randomly flip images vertically
)

batch_size = 32
img_iter = datagen.flow(x_train_conv, y_train, batch_size=batch_size)
img_it.class_indices
model.fit(x_train_conv, y_train, epochs=20, verbose=1, validation_data=(x_test_conv, y_test))

# show the first five filters
filters, biases = model.layers[0].get_weights()
print(filters.shape)
plt.subplot(151)
plt.imshow(filters[:,:,0,0], cmap='gray')
plt.subplot(152)
plt.imshow(filters[:,:,0,1], cmap='gray')
plt.subplot(153)
plt.imshow(filters[:,:,0,2], cmap='gray')
plt.subplot(154)
plt.imshow(filters[:,:,0,3], cmap='gray')
plt.subplot(155)
plt.imshow(filters[:,:,0,4], cmap='gray')
plt.show()



------------------------------------------------------------------------------------------------------------------------


#!/usr/bin/env python
# coding: utf-8

# In[1]:


from tensorflow.keras.applications import VGG16
model = VGG16(weights="imagenet")
model.summary()


# In[2]:


from tensorflow import keras
base_model = keras.applications.VGG16(weights='imagenet',input_shape=(224, 224, 3),include_top=False)
base_model.summary()
base_model.trainable = False


# In[3]:


inputs = keras.Input(shape=(224, 224, 3))
x = base_model(inputs, training=False)
x = keras.layers.GlobalAveragePooling2D()(x)
outputs = keras.layers.Dense(1, activation='sigmoid', name='predictions')(x)
model = keras.Model(inputs, outputs)
#model.compile(loss=keras.losses.BinaryCrossentropy(from_logits=False), metrics=[keras.metrics.BinaryAccuracy()])
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])


# In[4]:


from tensorflow.keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(
        samplewise_center=True,   # set each sample mean to 0
        rotation_range=10,        # randomly rotate images in the range (degrees, 0 to 180)
        zoom_range = 0.1,         # randomly zoom image 
        width_shift_range=0.1,    # randomly shift images horizontally (fraction of total width)
        height_shift_range=0.1,   # randomly shift images vertically (fraction of total height)
        horizontal_flip=True,     # randomly flip images
        vertical_flip=False)      # we don't expect Bo to be upside-down so we will not flip vertically


# In[5]:


# load and iterate training dataset
train_it = datagen.flow_from_directory('C:/Users/ihlee/testAI/data/COVID/train/', 
                                       target_size=(224, 224), 
                                       color_mode='rgb', 
                                       class_mode='binary', batch_size=8)
# load and iterate validation dataset
valid_it = datagen.flow_from_directory('C:/Users/ihlee/testAI/data/COVID/test/', 
                                      target_size=(224, 224), 
                                      color_mode='rgb', 
                                      class_mode='binary', batch_size=8)


# In[ ]:


import matplotlib.pyplot as plt
#model.fit(train_it, steps_per_epoch=12, validation_data=valid_it, validation_steps=4, epochs=20)
history=model.fit(train_it, validation_data=valid_it, epochs=20, verbose=1)
if True:
    plt.plot(history.history['accuracy'])
    plt.plot(history.history['val_accuracy'])
    plt.ylabel('Accuracy')
    plt.xlabel('Epoch')
    plt.legend(['training', 'validation'], loc = 'lower right', bbox_to_anchor=(0.6, 0.2), ncol=1, frameon=True, shadow=True, fontsize=14 )
    plt.show()
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
#    plt.title('model loss')
    plt.ylabel('Loss')
    plt.xlabel('Epoch')
    plt.legend(['train', 'val'], loc='upper left')
    plt.show()


# In[ ]:


import tensorflow as tf
# fine tunning
base_model.trainable = True
# reduce learning rate
#model.compile(optimizer=keras.optimizers.RMSprop(learning_rate = .00001),
#              loss=keras.losses.BinaryCrossentropy(from_logits=False),metrics=[keras.metrics.BinaryAccuracy()])
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.00001),loss='binary_crossentropy', metrics=['accuracy'])


# In[ ]:


import matplotlib.pyplot as plt
import os
if True:
    if os.path.exists('my_model_weights19.h5'):
        model.load_weights('my_model_weights19.h5')
        print('reading weights')
#model.fit(train_it, steps_per_epoch=10, validation_data=valid_it, validation_steps=4, epochs=20)
history=model.fit(train_it, validation_data=valid_it, epochs=20, verbose=1)
if True:
    model.save_weights('my_model_weights19.h5')
    print('writing weights')
if True:
    plt.plot(history.history['accuracy'])
    plt.plot(history.history['val_accuracy'])
    plt.ylabel('Accuracy')
    plt.xlabel('Epoch')
    plt.legend(['training', 'validation'], loc = 'lower right', bbox_to_anchor=(0.6, 0.2), ncol=1, frameon=True, shadow=True, fontsize=14 )
    plt.show()
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
#    plt.title('model loss')
    plt.ylabel('Loss')
    plt.xlabel('Epoch')
    plt.legend(['train', 'val'], loc='upper left')
    plt.show()


# In[ ]:


from keras.preprocessing.image import load_img, img_to_array
from keras.applications.vgg16 import VGG16, preprocess_input
import matplotlib.pyplot as plt

_img = load_img("C:/Users/ihlee/testAI/data/COVID/test/COVID_PATIENTS/covid_90.jpg",target_size=(224,224))
#_img = load_img("C:/Users/ihlee/testAI/data/COVID/test/NORMAL_PATIENTS/normal_70.jpg",target_size=(224,224))
plt.imshow(_img)
plt.show()
import numpy as np
img = np.array(_img).reshape(-1, 224, 224, 3)
y_pred = model.predict(img)
print(y_pred)


# In[ ]:


from keras.preprocessing.image import load_img, img_to_array
from keras.applications.vgg16 import VGG16, preprocess_input
import matplotlib.pyplot as plt

#_img = load_img("C:/Users/ihlee/testAI/data/COVID/test/COVID_PATIENTS/covid_70.jpg",target_size=(224,224))
_img = load_img("C:/Users/ihlee/testAI/data/COVID/test/NORMAL_PATIENTS/normal_88.jpg",target_size=(224,224))
plt.imshow(_img)
plt.show()
import numpy as np
img = np.array(_img).reshape(-1, 224, 224, 3)
y_pred = model.predict(img)
print(y_pred)


덧글

댓글 입력 영역

최근 포토로그



MathJax