04-08 17:32
벤치마킹
Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 사진에서 글자추출
- 양양솔비치아침
- 검색완료
- 영통외식
- 싱가폴중학교수학문제
- 파이썬
- 양양솔비치조식
- 가족소고기외식
- 영통역소고기
- 고마워다음
- 에어아시아
- 주차넉넉
- 종이캐리어
- 푸르지오포레피스
- 결항전문
- 커피쏟음
- 홍시스무디
- 결항
- 아이혼자다녀옴
- 양양솔비치 뷔페
- 당근마켓중고차
- 사진문자추출
- 양양솔비치세프스키친
- 편도수술
- 커피
- 영통칠프로칠백식당
- 중학교입학수학문제
- 사진문자추출하기
- DFS
- 오트눈썰매장
Archives
- Today
- Total
너와나의 관심사
keras model 만들고 학습 후 저장하기 본문
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)
'사는 이야기 > IT 이야기' 카테고리의 다른 글
python study 시작 (1) | 2023.10.09 |
---|---|
python dict key, value iteration (0) | 2023.03.11 |
priority queue c++ 사용 법 (0) | 2022.07.11 |
Transfer Learning weight reuse vs initialization (random) (0) | 2021.09.11 |
우분투 무선랜 카드 드라이버 설치 (아이피타임 A2000U0A) (0) | 2021.02.23 |
Comments