UOMOP

성능 향상 using shuffle 본문

Ai/DL

성능 향상 using shuffle

Happy PinGu 2022. 2. 9. 15:31

1. 각종 모듈 호출

import numpy as np
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Input, Conv2D, Activation, MaxPooling2D, Flatten, Dense,\
					                                       Dropout, BatchNormalization
from tensorflow.keras.optimizers import Adam, RMSprop

2. 각종 함수 정의

def make_zero_to_one(images, labels) :
    
    images = np.array(images/255., dtype = np.float32)
    labels = np.array(labels, dtype = np.float32)
    
    return images, labels


def ohe(labels) :
    
    labels = to_categorical(labels)
    
    return labels


def sper_tr_val(train_images, train_labels, test_images, test_labels, validation_rate) :
    
    tr_images, val_images, tr_labels, val_labels = \
                    train_test_split(train_images, train_labels, test_size = validation_rate)
    
    return (tr_images, tr_labels), (val_images, val_labels), (test_images, test_labels)


def create_model():
    
    input_size = (train_images.shape[1])
    input_tensor = Input(shape = (input_size, input_size, 3)) 

    x = Conv2D(filters = 32, kernel_size = (3, 3), padding = "same")(input_tensor)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)

    x = Conv2D(filters = 32, kernel_size = (3, 3), padding = "same")(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = MaxPooling2D(pool_size = (2, 2))(x)

    x = Conv2D(filters = 64, kernel_size = (3, 3), padding = "same")(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)

    x = Conv2D(filters = 64, kernel_size = (3, 3), padding = "same")(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = MaxPooling2D(pool_size = (2, 2))(x)

    x = Conv2D(filters = 128, kernel_size = (3, 3), padding = "same")(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)

    x = Conv2D(filters = 128, kernel_size = (3, 3), padding = "same")(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    x = MaxPooling2D(pool_size = (2, 2))(x)

    x = Flatten(name = "flatten")(x)
    x = Dropout(rate = 0.5)(x)
    x = Dense(300, activation = "relu", name = "fc1")(x)
    x = Dropout(rate = 0.3)(x)
    output = Dense(10, activation = "softmax", name = "output")(x)

    model = Model(inputs = input_tensor, outputs = output)
    
    return model


def compare_graph(result_1, result_2) :
    
    fig, axs = plt.subplots(nrows = 1, ncols = 2, figsize = (22, 6))
    
    axs[0].plot(result_1.history["val_accuracy"], label = "before_shuffle")
    axs[0].plot(result_2.history["val_accuracy"], label = "after_shuffle")
    axs[0].set_title("val_accuracy")
    axs[0].set_xlabel("epochs")
    axs[0].set_ylabel("accuracy")
    axs[0].legend()
    
    axs[1].plot(result_1.history["val_loss"], label = "before_shuffle")
    axs[1].plot(result_2.history["val_loss"], label = "after_shuffle")
    axs[1].set_title("val_loss")
    axs[1].set_xlabel("epochs")
    axs[1].set_ylabel("loss")
    axs[1].legend()
    
    plt.show()

3. fit 수행 시 shuffle을 적용하지 않았을 경우

(train_images, train_labels), (test_images, test_labels) = cifar10.load_data()

train_images, train_labels = make_zero_to_one(train_images, train_labels)
test_images, test_labels = make_zero_to_one(test_images, test_labels)

train_labels = ohe(train_labels)
test_labels = ohe(test_labels)

(tr_images, tr_labels), (val_images, val_labels), (test_images, test_labels) = \
         sper_tr_val(train_images, train_labels, test_images, test_labels, validation_rate = 0.15)

model_1 = create_model()
model_1.compile(optimizer = Adam(), loss = "categorical_crossentropy", metrics = ["accuracy"])
result_1 = model_1.fit(tr_images, tr_labels, batch_size = 64, epochs = 30, shuffle = False, \
                                                       validation_data= (val_images, val_labels))
                                                       
model_1.evaluate(test_images, test_labels)

4. fit 수행 시 shuffle을 적용했을 경우

(train_images, train_labels), (test_images, test_labels) = cifar10.load_data()

train_images, train_labels = make_zero_to_one(train_images, train_labels)
test_images, test_labels = make_zero_to_one(test_images, test_labels)

train_labels = ohe(train_labels)
test_labels = ohe(test_labels)

(tr_images, tr_labels), (val_images, val_labels), (test_images, test_labels) = \
                sper_tr_val(train_images, train_labels, test_images, test_labels, validation_rate = 0.15)

model_2 = create_model()
model_2.compile(optimizer = Adam(), loss = "categorical_crossentropy", metrics = ["accuracy"])
result_2 = model_2.fit(tr_images, tr_labels, batch_size = 64, epochs = 30, shuffle = True, \
                                                         validation_data= (val_images, val_labels))
                                                         
                                                         
model_2.evaluate(test_images, test_labels)

5. 비교

compare_graph(result_1, result_2)

fit 수행 시 shuffle을 적용했을 때가 비교적 성능이 좋아진 것을 확인할 수 있었다. default값은 True이지만 이미지 처리를 할 때는 shuffle에 대한 조정일 필요할 때가 있다.

 

 

 

'Ai > DL' 카테고리의 다른 글

성능 향상 using callback  (0) 2022.02.09
성능 향상 using batch size  (0) 2022.02.09
성능 향상 using Batch_normalization  (0) 2022.02.07
feature map size 계산  (0) 2022.02.07
CNN model 생성  (0) 2022.02.06
Comments