티스토리 뷰

반응형

딥러닝 모델 개발 관련 패키지 불러오기

import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import random
%matplotlib inline

from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split

from tensorflow.keras.datasets import fashion_mnist

from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense, Flatten


1. 데이터 전처리 함수 만틀기

1) 리스케일, 레이블 넘파이 배열

def get_preprocessed_data(img, label):
    img = np.array(img/255.0, dtype=np.float32)
    label = np.array(label, dtype=np.float32)
    return img, label


2) 레이블 원핫인코딩

def get_preprocessed_ohe(img,label):
    img, label = get_preprocessed_data(img, label)
    label_ohe = to_categorical(label)
    return img, label_ohe

3) 학습, 검증, 평가데이터 만들기

def get_train_valid_test_dataset(train_images, train_labels, test_images, test_labels, valid_size=0.15, random_state=1024):
    train_images, train_lables_ohe = get_preprocessed_ohe(train_images,train_labels)
    test_images, test_lables_ohe = get_preprocessed_ohe(test_images,test_labels)
    
    tr_images, valid_images, tr_labels_ohe, valid_labels_ohe =train_test_split(train_images, train_lables_ohe, test_size=valid_size, random_state=random_state)
    
    return (tr_images, tr_labels_ohe), (valid_images, valid_labels_ohe), (test_images, test_lables_ohe)

    
2. 데이터 로드 하기

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

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
               
print(train_images.shape, train_labels.shape, test_images.shape, test_labels.shape)


(tr_images, tr_labels_ohe), (valid_images, valid_labels_ohe), (test_images, test_labels_ohe) = \
    get_train_valid_test_dataset(train_images, train_labels, test_images, test_labels, valid_size=0.15, random_state=1024)

print((tr_images.shape, tr_labels_ohe.shape), (valid_images.shape, valid_labels_ohe.shape), (test_images.shape, test_labels_ohe.shape))

 

3. 데이터 시각화

plt.figure(figsize=(20,10))
for i in range(5):
    random_number = random.randint(0, train_images.shape[0])
    plt.subplot(1, 5, i+1)
    plt.imshow(train_images[random_number], cmap="gray")
    plt.title(class_names[train_labels[random_number]])


4. functional api 기반 모델 개발하기

input_size=28

def create_model():
    input_tensor = Input(shape=(input_size, input_size))
    
    x= Flatten()(input_tensor)
    x = Dense(100, activation='relu')(x)
    x = Dense(30, activation= 'relu')(x)
    output = Dense(10, activation='softmax')(x)
    
    model = Model(inputs=input_tensor, outputs=output)
    return model
model = create_model()
model.summary()

model.compile(optimizer='adam', loss="categorical_crossentropy", metrics =["accuracy"])


5. 모델 학습하기

from tensorflow.keras.callbacks import ModelCheckpoint
filepath = './check_point/epoch_{epoch:2d}_val_loss_{val_loss:0.2f}.hdf5'
callback = ModelCheckpoint(
    filepath,
    save_freq = 'epoch',
    monitor='val_loss',
    verbose=1,
    save_best_only=True,
    save_weights_only=True,
    mode='min',
     # 과거 구문: period =1, 인데 save_freq는 반드시 save_freq = 'epoch'로 사용해야 함
)

history=model.fit(tr_images, tr_labels_ohe, epochs=20, batch_size=256, verbose=1, validation_data=(valid_images, valid_labels_ohe), callbacks=[callback])

6. 모델 평가하기

model.evaluate(test_images, test_labels_ohe)

7. 모델 평가 그래프 그리기

 

plt.figure(figsize=(20,10))

plt.subplot(121)
plt.plot(history.history["loss"], label="train loss")
plt.plot(history.history["val_loss"], label ="validation loss")
plt.legend()
plt.subplot(122)
plt.plot(history.history["accuracy"], label="train accuracy")
plt.plot(history.history["val_accuracy"], label ="validation accuracy")
plt.legend()

plt.show()

8. 모델 예측하기

def show_image(img, label, predicted_value, conf):    
    plt.imshow(img, cmap="gray")
    plt.title("Actual label: " + class_names[label] +'\n' + "predicted: " + class_names[predicted_value]\
             +'\n' + "confidence: " + str(round(conf*100,2)))    
             
def show_predicted_result():    
    rand_num = random.randint(0, len(test_images))    
    orign_images = test_images[rand_num]    
    predict_image = np.expand_dims(test_images[rand_num], 0)
    predicted_value = model.predict(predict_image)
    class_id = np.argmax(predicted_value)
    confidence = np.max(predicted_value) 
    show_image(orign_images, test_labels[rand_num], class_id, confidence)
    return  
    
 show_predicted_result()

 

반응형
댓글