๐Ÿ”ง ๋”ฅ๋Ÿฌ๋‹ - ๋‹ค์ค‘๋ถ„๋ฅ˜๐Ÿ”ฉ

parkeuยท2022๋…„ 9์›” 28์ผ
0

ABC๋ถ€ํŠธ์บ ํ”„

๋ชฉ๋ก ๋ณด๊ธฐ
35/55

To . . ์–ธ๋‹ˆ ๐Ÿผ ์ €์˜ค๋Š˜๋„์—ด์‹ฌํžˆ์ผ์–ด์—ฌ!!์นญ์ฐฌํ•ด์ฃผ์„ธ์•ผ!~!~!~!~!โค๏ธ

๐Ÿ—ƒ๏ธ ๋‹ค์ค‘๋ถ„๋ฅ˜

โœ๏ธ MNIST 0์—์„œ 9๊นŒ์ง€ ์ˆซ์ž ์˜ˆ์ธก

  • ๊ณผ๊ฑฐ NIST์—์„œ ์ˆ˜์ง‘ํ•œ ์†์œผ๋กœ ์ง์ ‘ ์“ด ํ‘๋ฐฑ์ˆซ์ž
  • ๋ฐ์ดํ„ฐ๋Š” ์ˆซ์ž ์ด๋ฏธ์ง€(28, 28)์™€ ์ˆซ์ž์— ํ•ด๋‹นํ•˜๋Š” ๋ ˆ์ด๋ธ”๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Œ

MNIST ๋ฐ์ดํ„ฐ ์ค€๋น„ํ•˜๊ธฐ

from keras.datasets.mnist import load_data

# Keras ์ €์žฅ์†Œ์—์„œ ๋ฐ์ดํ„ฐ ๋‹ค์šด
(X_train, y_train), (X_test, y_test) = load_data(path='mnist.npz')

๋ฐ์ดํ„ฐ ํ˜•ํƒœ ํ™•์ธํ•˜๊ธฐ

X_train.shape

# ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ
print(X_train.shape, y_train.shape)
print(y_train)

# ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ
print(X_test.shape, y_test.shape)
print(y_test)

๋ฐ์ดํ„ฐ ๊ทธ๋ ค๋ณด๊ธฐ

import matplotlib.pyplot as plt
import numpy as np

idx = 0
img = X_train[idx, :]
label = y_train[idx]
plt.figure()
plt.imshow(img)
plt.title('%d-th data, label is %d' %(idx, label))

๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ๋งŒ๋“ค๊ธฐ

from sklearn.model_selection import train_test_split

# ํ›ˆ๋ จ/๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋ฅผ 70/30 ๋น„์œจ๋กœ ๋ถ„๋ฆฌ
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.3, random_state=777)
print(X_train.shape) # ํ›ˆ๋ จ๋ฐ์ดํ„ฐ 42000
print(X_val.shape) # ๊ฒ€์ฆ๋ฐ์ดํ„ฐ 18000

๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ

๋ชจ๋ธ ์ž…๋ ฅ์„ ์œ„ํ•œ ํ•™์Šต๋ฐ์ดํ„ฐ(์†๊ธ€์”จ ์ด๋ฏธ์ง€ ๋ฒกํ„ฐ) ์ „์ฒ˜๋ฆฌ

  1. 2์ฐจ์› ๋ฐฐ์—ด -> input_dim -> 1์ฐจ์› ๋ฐฐ์—ด(28*28=784)
  2. ์ •๊ทœํ™” -> 0 ~ 255 -> 0 ~ 1

1์ฐจ์› ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค๊ธฐ

num_x_train = X_train.shape[0] # 42000
num_x_val = X_val.shape[0] # 18000
num_x_test = X_test.shape[0] # 10000

# 1. (28, 28) -> 1์ฐจ์› ๋ฐฐ์—ด๋กœ ์ฒ˜๋ฆฌ
X_train = (X_train.reshape(num_x_train, 28*28))
X_val = (X_val.reshape(num_x_val, 28*28))
X_test = (X_test.reshape(num_x_test, 28*28))

print(X_train.shape) -> (42000, 784)
print(X_val.shape) -> (18000, 784)
print(X_test.shape) -> (10000, 784)

์ •๊ทœํ™” -> ์Šค์ผ€์ผ๋ง

์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ „์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ• - ์Šค์ผ€์ผ๋ง

Normalization(MinMax)
Robust Normalization
Standardization

# MinMax 0~255 -> 0~1
X_train = X_train / 255
X_val = X_val / 255
X_test = X_test / 255

print(X_train[0])


๋ชจ๋ธ ์ž…๋ ฅ์„ ์œ„ํ•œ ๋ ˆ์ด๋ธ”(์ •๋‹ต) ์ „์ฒ˜๋ฆฌ

from tensorflow.keras.utils import to_categorical

# ์ˆ˜์น˜ ์ •๋‹ต ๋ฐ์ดํ„ฐ -> ๋ฒ”์ฃผํ˜• ๋ฐ์ดํ„ฐ๋กœ ๋ณ€๊ฒฝ
y_train = to_categorical(y_train)
y_val = to_categorical(y_val)
y_test = to_categorical(y_test)

y_train

๋ชจ๋ธ ๋งˆ์ง€๋ง‰ ์ธต์—์„œ ์†Œํ”„ํŠธ๋งฅ์Šค(softmax) ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ๋ฒ”์ฃผํ˜• ๋ ˆ์ด๋ธ”๋กœ ๋ณ€ํ™˜

๋ชจ๋ธ ๊ตฌ์„ฑํ•˜๊ธฐ

from keras.models import Sequential
from keras.layers import Dense

model = Sequential()
model.add(Dense(64, activation='relu', input_dim=784))
model.add(Dense(32, activation='relu'))
# ์ด์ค‘๋ถ„๋ฅ˜ sigmoid ๋‹ค์ค‘๋ถ„๋ฅ˜ softmax
model.add(Dense(10, activation='softmax')) # 10๊ฐœ์˜ ์ถœ๋ ฅ์„ ๊ฐ€์ง„ ์‹ ๊ฒฝ๋ง -> ์ •๋‹ต์˜ shape์™€ ๋™์ผ
  • ์†Œํ”„ํŠธ๋งฅ์Šค ํ•จ์ˆ˜๋Š” ์ถœ๋ ฅ๊ฐ’์˜ ๋ฒ”์œ„ ์•ˆ์—์„œ ํ™•๋ฅ ๋กœ์จ ํ•ด์„ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ณผ์˜ ํ•ด์„์ด ๋”์šฑ ์šฉ์ดํ•จ
    -> ๋‹ค๋ฅธ ํ‘œํ˜„ : ์ผ๋ฐ˜์ ์œผ๋กœ ํ™•๋ฅ ์„ ๊ตฌํ•˜๋Š” ๋ฐฉ์‹๊ณผ ๋น„์Šทํ•˜๋ฏ€๋กœ ๊ฐ ํด๋ž˜์Šค์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’๋“ค์ด ์„œ๋กœ ์˜ํ–ฅ์„ ์ค„ ์ˆ˜ ์žˆ์–ด ๋น„๊ต์— ์šฉ์ด

๋ชจ๋ธ ์„ค์ •ํ•˜๊ธฐ

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

๋ชจ๋ธ ํ•™์Šตํ•˜๊ธฐ

history = model.fit(X_train, y_train, epochs=30, batch_size=128, validation_data=(X_val, y_val))

ํ•™์Šต๊ฒฐ๊ณผ ๊ทธ๋ฆฌ๊ธฐ

import matplotlib.pyplot as plt

his_dict = history.history
loss = his_dict['loss']
val_loss = his_dict['val_loss'] # ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ โ€˜val_โ€™ ์ˆ˜์‹์–ด๊ฐ€ ๋ถ™์Šต๋‹ˆ๋‹ค.

epochs = range(1, len(loss) + 1)
fig = plt.figure(figsize = (10, 5))

# ํ›ˆ๋ จ ๋ฐ ๊ฒ€์ฆ ์†์‹ค ๊ทธ๋ฆฌ๊ธฐ
ax1 = fig.add_subplot(1, 2, 1)
ax1.plot(epochs, loss, color = 'blue', label = 'train_loss')
ax1.plot(epochs, val_loss, color = 'orange', label = 'val_loss')
ax1.set_title('train and val loss')
ax1.set_xlabel('epochs')
ax1.set_ylabel('loss')
ax1.legend()

acc = his_dict['acc']
val_acc = his_dict['val_acc']

# ํ›ˆ๋ จ ๋ฐ ๊ฒ€์ฆ ์ •ํ™•๋„ ๊ทธ๋ฆฌ๊ธฐ
ax2 = fig.add_subplot(1, 2, 2)
ax2.plot(epochs, acc, color = 'blue', label = 'train_acc')
ax2.plot(epochs, val_acc, color = 'orange', label = 'val_acc')
ax2.set_title('train and val acc')
ax2.set_xlabel('epochs')
ax2.set_ylabel('loss')
ax2.legend()

plt.show()

๋‘ ๊ทธ๋ž˜ํ”„๊ฐ€ ์–ด๋””์„œ๋ถ€ํ„ฐ ๋ฒŒ์–ด์ง€๋Š”๊ฐ€?

  • ๊ณผ๋Œ€์ ํ•ฉ ๋ฌธ์ œ๊ฐ€ ๋‚˜ํƒ€๋‚œ ๊ฒƒ
  • ๋ฐ์ดํ„ฐ ํŠน์„ฑ, ๋ชจ๋ธ ๊ตฌ์กฐ ๋“ฑ์„ ์ˆ˜์ •ํ•ด๋ณด๊ณ  ์žฌํ•™์Šต
  • ๋ฒŒ์–ด์ง€๊ธฐ ์ „๊นŒ์ง€ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜๊ณ  ์ด๋ฅผ ์ €์žฅ ๋ฐ ๊ธฐ๋ก

๋ชจ๋ธ ํ‰๊ฐ€ํ•˜๊ธฐ

model.evaluate(X_test, y_test)

์˜ˆ์ธก๊ฐ’ ๊ทธ๋ ค์„œ ํ™•์ธ

results = model.predict(X_test)
print(results.shape)
print(results[5])
np.argmax(results[5], axis=-1)

idx = 5
arg_results = np.argmax(results, axis=-1)
plt.imshow(X_test[idx].reshape(28,28))
plt.title('predict value of the image : ' + str(arg_results[idx]))
plt.show()

results[9] , idx = 9๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด

0~9๊นŒ์ง€ ๊ฐ’ ์ค‘ 9๊ฐ€ ๊ฐ€์žฅ ํฌ๋ฏ€๋กœ 9๋กœ ์˜ˆ์ธก


๋ชจ๋ธ ํ‰๊ฐ€

1) ํ˜ผ๋™ํ–‰๋ ฌ

  • ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‚˜ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•  ๋•Œ ์‚ฌ์šฉ
  • ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ๋ชจ๋ธ์˜ ๊ฐ•์ ๊ณผ ์•ฝ์  ํŒŒ์•…์— ์œ ์šฉ
from sklearn.metrics import classification_report, confusion_matrix
import seaborn as sns

plt.figure(figsize=(7,7))
cm = confusion_matrix(np.argmax(y_test, axis=-1), np.argmax(results, axis=-1)) # ์‹ค์ œ ์ •๋‹ต๊ณผ ๋น„๊ต
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.xlabel('predicted label')
plt.ylabel('true label')
plt.show()

๐Ÿ‘€ 4์™€ 9๋ฅผ ๋ถ„๋ฅ˜ํ•  ๋•Œ ํ˜ผ๋ž€์Šค๋Ÿฌ์›Œ ํ•จ

2) ๋ถ„๋ฅ˜ ๋ณด๊ณ ์„œ

print(classification_report(np.argmax(y_test, axis=-1), np.argmax(results, axis=-1)))


๐Ÿ“Œ ์ฃผ๋ง๋ชฉํ‘œ
๋”ฅ๋Ÿฌ๋‹ ์ž…๋ฌธ ์ฑ…๋ณด๊ณ  ๋ฐฐ์šด๊ณณ๊นŒ์ง€ ์ •๋ฆฌํ•ด๋ณด๊ฑฐ๋‚˜ ๋ชจ๋ฅด๋Š” ๋ถ€๋ถ„ ์ดํ•ดํ•˜๊ธฐ @ @!

  • ๊ณผ๋Œ€์ ํ•ฉ, ๊ณผ์†Œ์ ํ•ฉ
  • ๋ถ„๋ฅ˜ ๋ณด๊ณ ์„œ
  • ์ฝ”๋“œ๋ถ„์„

์ด๋ฒˆ์ฃผ์—์ดํ•ด๋ชปํ•˜๋ฉดํ”„๋กœ์ ํŠธ์ฃฝ์Œ,,,,,,

profile
๋ฐฐ๊ณ ํŒŒ์šฉ.

0๊ฐœ์˜ ๋Œ“๊ธ€