04-29 00:57
Recent Posts
Recent Comments
관리 메뉴

너와나의 관심사

keras model 만들고 학습 후 저장하기 본문

사는 이야기/IT 이야기

keras model 만들고 학습 후 저장하기

벤치마킹 2023. 3. 19. 17:43

keras model 만드는 예제 

다음 포스팅에서 아래 relu 없는 (actviation layer 없는) conv layer 를 합치는 모델 최적화 내용에 대해서 posting 하기 위해서다.

입력에 출력이 같지만 아래 conv2D 를 하나의 conv 로 합치는 스킴을 포스팅 예정이다.

 

model.add(Conv2D(64, (1, 1), padding='same'))
model.add(Conv2D(16, (3, 3), activation='relu', padding='same'))

 

# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

import keras
from tensorflow.keras import Sequential
import tensorflow as tf
from tensorflow.python.keras.layers import Input, Dense, Dropout, MaxPooling2D, Conv2D, Flatten
import numpy as np
import matplotlib.pyplot as plt
from keras.callbacks import ModelCheckpoint,ReduceLROnPlateau,EarlyStopping, CSVLogger


# Callbacks
backup_cb = keras.callbacks.BackupAndRestore(backup_dir='./tmp_ckpoint/backup_dir')
ckpt_cb = keras.callbacks.ModelCheckpoint('./checkpoint_2/best_model', save_best_only=True, monitor='val_loss', verbose=1)

#model param

num_classes = 10
img_rows = 28
img_cols = 28
input_shape = (img_rows, img_cols, 1)


def train_test_input():

    np.random.seed(7)

    img_rows = 28
    img_cols = 28

    (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)

    x_train = x_train.astype('float32') / 255.
    x_test = x_test.astype('float32') / 255.

    print('x_train shape:', x_train.shape)
    print(x_train.shape[0], 'train samples')
    print(x_test.shape[0], 'test samples')

    y_train = keras.utils.to_categorical(y_train, num_classes)
    y_test = keras.utils.to_categorical(y_test, num_classes)

    return x_train, x_test, y_train, y_test


def model_gen():

    model = Sequential()

    model.add(Conv2D(32, kernel_size=(5, 5), strides=(1, 1), padding='same', activation='relu', input_shape=input_shape))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

    model.add(Conv2D(64, (1, 1), padding='same'))
    model.add(Conv2D(16, (3, 3), activation='relu', padding='same'))

    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(1000, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(num_classes, activation='softmax'))


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

def training(model, x_train, x_test, y_train, y_test):

    hist = model.fit(x_train, y_train, batch_size=128, epochs=5, verbose=1, validation_data=(x_test, y_test),
                     callbacks=[backup_cb, ckpt_cb])
    score = model.evaluate(x_test, y_test, verbose=0)
    print('Test loss:', score[0])
    print('Test accuracy:', score[1])

    return model


def inference(model, x_test):
    n = 0
    plt.imshow(x_test[n].reshape(28, 28), cmap='Greys', interpolation='bicubic')
    plt.show()
    #np.argmax(model.predict(x), axis=-1)
    output_softmax = model.predict(x_test[n].reshape((1, 28, 28, 1)))

    print(output_softmax)

    print('The Answer is ', np.argmax(output_softmax, axis = -1))

if __name__ == '__main__':

    x_train, x_test, y_train, y_test = train_test_input()
    model  = model_gen()
    trained_model = training(model, x_train, x_test, y_train, y_test)
    trained_model.summary()

    inference(trained_model, x_test)
Comments