24 Sat
๋ฅ๋ฌ๋ CNN ์๋ฒฝ ๊ฐ์ด๋ - Fundamental ํธ
Tensorflow 2.X ์ tf.keras ์๊ฐ
ํ ์ํ๋ก์ฐ 2.0์ด 19๋ ๋ 1์์ ๋์์
์ด ๋ฒ์ ์ด์ ์๋ ์ผ๋ผ์ค๊ฐ ๋จ๋ ์ผ๋ก ์์๋๋ฐ ๋ฒ์ ์ดํ์๋ ํ ์ํ๋ก์ฐ ๋ด๋ถ ํจํค์ง๋ก ํฌํจ๋๋ค.
๊ธฐ์กด ์ผ๋ผ์ค ๋ณด๋ค ํ ์ํ๋ก์ฐ ์๋ธ ํจํค์ง์ธ ์ผ๋ผ์ค๋ฅผ ๊ถ์ฅํ๋ค
# 2.0 ์ด์
from keras.layers import Dense
# 2.0 ์ดํ
from tensorflow.keras.layers import Dense
์ด๋ฏธ์ง ๋ฐฐ์ด์ ์ดํด
Image ์์ ๋ชจ๋ธ
์ด๋ฏธ์ง๋ 0๋ถํฐ 255๊น์ง์ ์๋ก ์ด๋ฃจ์ด์ง ํฝ์ ๋ก ํํํ๋ค
0์ Black, 255๋ White
Grayscale : height * width ํํ์ 2์ฐจ์ ๋ฐฐ์ด
Color : height * width * channel ํํ์ 3์ฐจ์ ๋ฐฐ์ด
๋ฐฐ์ด ์ฐจ์
์ฌ๋ฌ ์ฅ์ Grayscale ์ด๋ฏธ์ง : 3์ฐจ์
์ฌ๋ฌ ์ฅ์ RGB ์ด๋ฏธ์ง : 4์ฐจ์
ํ ์ ๋ชจ๋ธ ๊ตฌ์ถ์ ํญ์ 3์ฐจ์์ ํํ๋ก ๋ฃ์ด์ผ ํ๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก ๋ฑ์ฅ์ ๊ธฐ์ค์ 3์ฐจ์์ผ๋ก ๋ณด๊ธฐ ๋๋ฌธ(?)
๋ค์์ ๋ค์ ๋ค๋ฃฐ ๊ฒ
Dense Layer๋ก Fashion MNIST ์์ธก ๋ชจ๋ธ ๊ตฌํํ๊ธฐ - ์ด๋ฏธ์ง ๋ฐ์ดํฐ ํ์ธ ๋ฐ ์ฌ์ ๋ฐ์ดํฐ ์ฒ๋ฆฌ
import matplotlib.pyplot as plt
%matplotlib inline
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat','Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
def show_images(images, labels, ncols=8):
figure, axs = plt.subplots(figsize=(22, 6), nrows=1, ncols=ncols)
for i in range(ncols):
axs[i].imshow(images[i], cmap='gray')
axs[i].set_title(class_names[labels[i]])
show_images(train_images[:8], train_labels[:8], ncols=8)
show_images(train_images[8:16], train_labels[8:16], ncols=8)
plt.subplots
์ ๋ง๋ ๊ทธ๋ํ๋ฅผ ์์ฑํ๋ค. axs๋ ๊ฐ n๋ฒ์งธ ๋ง๋๊ทธ๋ํ
์ฑ๋ฅ์ด ์ค์ํ๊ฒ ๋์จ ์ด์ ๋ ๋ฌผ์ฒด๊ฐ ์ด๋ฏธ์ง ์ ์ค์์ ์์นํ๊ธฐ ๋๋ฌธ.
1์ฐจ์์ผ๋ก ์ ๋ ฅ์ ๋ฐ๊ธฐ ๋๋ฌธ์ ์ฑ๋ฅ์ด ์์ข๊ฒ ๋์์ผ ์ ์!
2์ฐจ์์ผ๋ก ์ ๋ ฅ๋ฐ์ง ์์์ผ๋ฏ๋ก ์ด๋ฏธ์ง์ ์ง์ญ์ ํน์ฑ์ ์ ํ ๊ณ ๋ คํ์ง ์์ ์
Dense Layer๋ก Fashion MNIST ์์ธก ๋ชจ๋ธ ๊ตฌํํ๊ธฐ - ๋ชจ๋ธ ์ค๊ณ ๋ฐ ํ์ต ์ํ
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.models import Sequential
model = Sequential([
Flatten(input_shape=(INPUT_SIZE, INPUT_SIZE)),
Dense(100, activation='relu'),
Dense(30, activation='relu'),
Dense(10, activation='softmax')
])
model.summary()
Dense๋ ๋ ์ด์ด๋ฅผ ์์ฑํ๋ ํจ์
Dense๋ 1์ฐจ์ ๋ฐ์ดํฐ๋ง ์ ๋ ฅ ๋ฐ์ ์ ์๋ค. ๋ฐ๋ผ์ Flatten์ผ๋ก 1์ฐจ์ ๋ฐ์ดํฐ๋ก ๋ณํํ๋ค
Keras Layer API ๊ฐ์
์ผ๋ผ์ค๊ฐ ์ข์ ์ด์ ๋ ๋ ์ด์ด ๊ตฌ์ฑ์ด ํธํ๊ธฐ ๋๋ฌธ
Dense layer(activate=None) + Activation layer(sigmoid)= Dense layer(activate=sigmoid)
Core layers, Pooling layes, Convolution layes, Activation layers ๋ฑ์ด ๋ค์ํ๊ฒ ์กด์ฌํ๋ค.
Dense Layer๋ก Fashion MNIST ์์ธก ๋ชจ๋ธ ๊ตฌํํ๊ธฐ - ์์ธก ๋ฐ ์ฑ๋ฅ ํ๊ฐ
history = model.fit(x=train_images, y=train_oh_labels, batch_size=32, epochs=20, verbose=1)
๋ชจ๋ธ์์ ์ ๋ ฅ์ 3์ฐจ์(2์ฐจ์ ๊ทธ๋ ์ด์ค์ผ์ผ ์ฌ๋ฌ์ฅ)์ผ๋ก ๋ฃ์์ง๋ง ์ค์ ๋ก ๋ชจ๋ธ์ 2์ฐจ์์ผ๋ก ์ ๋ ฅ์ ๊ฐ์ฃผํ๊ฒ ๋๋ค.
๋ฐ๋ผ์, ์ฌ์ง์ ๋จ์ผ๋ก ์ ๋ ฅํ ๋๋ ์ฐจ์ ๋ณํ์ ํด์ค์ผ ํ๋ค.
๋ง์ฝ (10000, 28, 28) ์ด ์๋ค๋ฉด ํ์ฅ์ ๋ฃ์ ๋ (28, 28)๋ก ์ ๋ ฅ์ด ๋๋ฏ๋ก ์๋ฌ๊ฐ ๋๋ค.
(1, 28, 28)๋ก ๋ฐ๊ฟ์ ์ ๋ ฅ๊ฐ์ ๋ฃ์ด์ค์ผ ํ๋ค.
์ด๊ฒ์ ๋ด๋นํ๋ ํจ์๊ฐ
np.expand_dims
์ด๋ค.np.expand_dims(test-images[0], axis=0).shape
(1, 28, 28)
์ด ๋ ํ์ํ๋ค๋ฉด ์ถ๋ ฅ๊ฐ์ ๋ชจ์๋ ๋ฐ๊ฟ์ค์ผ ํ๋ค. ์ถ๋ ฅ์ด (1, 28, 28)๋ก ๋์ฌ ๊ฒ์ด๊ธฐ ๋๋ฌธ
๋ชจ๋ธ ์ฑ๋ฅ ๊ฒ์ฆ
model.evaluate
๋ฅผ ์ฌ์ฉํ๋ค.ํ ์คํธ ๋ฐ์ดํฐ์ ๋ํ ํ๊ฐ๋ฅผ ํ๋ค.
Dense Layer๋ก Fashion MNIST ์์ธก ๋ชจ๋ธ ๊ตฌํํ๊ธฐ - ๊ฒ์ฆ ๋ฐ์ดํฐ๋ฅผ ํ์ฉํ์ฌ ํ์ต ์ํ
ํ์ต ๋ฐ์ดํฐ๊ฐ ๊ณผ์ ํฉ ๋์๋์ง๋ฅผ ํ์ธํ๊ธฐ ์ํด ๊ฒ์ฆ ๋ฐ์ดํฐ๋ฅผ ํ๊ฐํด๋ด์ผ ํ๋ค. ์ด ๋ ํ์ต ๋ฐ์ดํฐ๋ฅผ ๋ชจ๋ํฐ๋ง ํ๋ ๊ฒ์ด ์๋๋ผ ๊ฒ์ฆ ๋ฐ์ดํฐ๋ฅผ ๋ชจ๋ํฐ๋ง ํ๋ค.
์ด์ฐจํผ ํ์ต ๋ฐ์ดํฐ์ ์๋ฌ๋ ์ค์ด๋ค ๊ฒ์ด๊ธฐ ๋๋ฌธ์
๋ํ ๊ฒ์ฆ ๋ฐ์ดํฐ์ ์ฑ๋ฅ์ด ์ข์ ์ง์ง ์์ ๋ Callback์ ์ฌ์ฉํ์ฌ ํ์ต๋ฅ ์ ๋ณด์ ์์ ํ ์ ์๋ค.
์ผ๋ฐ์ ์ผ๋ก ๊ฒ์ฆ ๋ฐ์ดํฐ๋ ํ ์คํธ ๋ฐ์ดํฐ์์ ์ชผ๊ฐ์ ์ฌ์ฉํ๋ค.
์ด๋ฅผ ๊ฐ๋ฅํ๊ฒ ํ๋ ํจ์๋ ์ฌ์ดํท๋ฐ์์ ์ง์ํ๋ค.
from sklearn.model_selection import train_test_split
from tensorflow.keras.utils import to_categorical
# ๊ธฐ์กด ํ์ต ๋ฐ์ดํฐ๋ฅผ ๋ค์ ํ์ต๊ณผ ๊ฒ์ฆ ๋ฐ์ดํฐ ์ธํธ๋ก ๋ถ๋ฆฌ
tr_images, val_images, tr_labels, val_labels = train_test_split(train_images, train_labels, test_size=0.15, random_state=2021)
print('train๊ณผ validation shape:', tr_images.shape, tr_labels.shape, val_images.shape, val_labels.shape)
random_state=2021
: ํ์ต ๋ฐ์ดํฐ์ ๊ฒ์ฆ ๋ฐ์ดํฐ๊ฐ ๋ถ๋ฆฌ๋ ๋ ๋งค๋ฒ ๋ค๋ฅด๊ฒ ๋ถ๋ฆฌ๋๋ ๊ฒ์ ๋ง๊ธฐ ์ํด seed๋ฅผ ์ ํ๋ค.
history = model.fit(x=tr_images, y=tr_oh_labels, batch_size=128, validation_data=(val_images, val_oh_labels),
epochs=20, verbose=1)

ํ์ต ๋ฐ์ดํฐ๊ฐ์ ๊ฒฝ์ฐ๋ ๋ฐ์ดํฐ์ ๋ผ๋ฒจ์ด ๋ถ๋ฆฌ๋์ ์ ๋ ฅ๋์ง๋ง ๊ฒ์ฆ ๋ฐ์ดํฐ ๊ฐ์ ๊ฒฝ์ฐ๋ ํํ๋ก ์ ๋ ฅํ๋ค.
๋ํ,
model.fit
์์ฒด์์๋ ๊ฒ์ฆ๋ฐ์ดํฐ ๋ถ๋ฆฌ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.๋ฏธ๋ฆฌ ๋ง๋ค์ด์ validation_date์ ๋ฃ์ด๋ ๋๊ณ
๋ง๋ค์ง ์๊ณ validation_split์ ์ค์ ํด๋ ๋๋ค.
์ผ๋ฐ์ ์ผ๋ก ํ์ต ๋ฐ์ดํฐ๋ฅผ ๊ธฐ์ค์ผ๋ก GD๋ฅผ ํ๊ธฐ ๋๋ฌธ์ ๋ณดํต ํ์ต ๋ฐ์ดํฐ์ ๋ํ ์ ํ๋๊ฐ ๋ ๋์ ํธ์ด๋ค. ๋ฐ๋์งํ ๊ฒ์ ํ์ต ๋ฐ์ดํฐ์ ์ ํ๋๋ณด๋ค ๊ต์ฐจ ๊ฒ์ฆ ๋ฐ์ดํฐ์ ์ ํ๋๊ฐ ๋ ๋๊ฑฐ๋ ํน์ ๋์ผํ ๊ฒฝ์ฐ์ด๋ค.
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot(history.history['accuracy'], label='train')
plt.plot(history.history['val_accuracy'], label='valid')
plt.legend()

๋ค์๊ณผ ๊ฐ์ด
plt.plot
๊ณผplt.legend()
๋ฅผ ์ด์ฉํ์ฌ ๊ทธ๋ํ๋ฅผ ๋ณผ ์ ์๋ค.ํ์ต์ด ์งํ๋ ์๋ก ๊ฐ๊ฒฉ์ด ๋ฉ์ด์ง๋ ๊ฒ์ ์ ์ ์๋ค.
Last updated
Was this helpful?