[ํ”„๋กœ์ ํŠธ] ๊ตํ†ต ํ‘œ์ง€ํŒ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜

ํ”„๋กœ์ ํŠธ ๋ชฉํ‘œ

  • ๊ตํ†ต ํ‘œ์ง€ํŒ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•˜๊ณ  ๋”ฅ๋Ÿฌ๋”ฉ ๋ชจ๋ธ์„ ํ†ตํ•˜์—ฌ ํ‘œ์ง€ํŒ ์ข…๋ฅ˜๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ถ„๋ฅ˜ ๋ชจ๋ธ ์ˆ˜ํ–‰
  • ๋Œ€๋Ÿ‰์˜ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๋ฅผ ์ „ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ณผ์ •๊ณผ ์ด์— ๋”ฐ๋ฅธ CNN ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ ๋ณ€ํ™”๋ฅผ ํ•™์Šต

 

ํ”„๋กœ์ ํŠธ ๋ชฉ์ฐจ

  1. ๋ฐ์ดํ„ฐ ๋ถ„์„: ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๋ฅผ ์ด๋ฃจ๊ณ  ์žˆ๋Š” ์š”์†Œ์— ๋Œ€ํ•ด์„œ Dataframe๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถ„์„ ๋ฐ ํ™•์ธ
    1-1. ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ ์ •๋ณด ํŒŒ์•…ํ•˜๊ธฐ - Meta
    1-2. ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ ์ •๋ณด ํŒŒ์•…ํ•˜๊ธฐ - Train
    1-3. ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ ์ •๋ณด ํŒŒ์•…ํ•˜๊ธฐ - Test
  1. ๋ฐ์ดํ„ฐ ์ „ ์ฒ˜๋ฆฌ: ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์˜ค๊ณ  ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ์˜ ์ž…๋ ฅ์œผ๋กœ ์ „ ์ฒ˜๋ฆฌ
    2-1. ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ
    2-2. label ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ
    2-3. ๋ฐ์ดํ„ฐ ๋ถ„๋ฆฌํ•˜๊ธฐ
  1. ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ: CNN ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•˜๊ณ  ํ•™์Šต, ํ‰๊ฐ€ ๋ฐ ์˜ˆ์ธก์„ ์ˆ˜ํ–‰
    3-1. CNN ๋ชจ๋ธ ์„ค์ •
    3-2. ํ•™์Šต ์ˆ˜ํ–‰
    3-3. ๋ชจ๋ธ ์„ฑ๋Šฅ ํ‰๊ฐ€ ๋ฐ ์˜ˆ์ธก

 

๋ฐ์ดํ„ฐ ์ถœ์ฒ˜

https://www.kaggle.com/meowmeowmeowmeowmeow/gtsrb-german-traffic-sign

https://2021nipa.elice.io/explore

 

 

ํ”„๋กœ์ ํŠธ ๊ฐœ์š”

์ฐจ๋Ÿ‰ ์šด์ „์„ ํ•˜๋ฉด์„œ ๋„๋กœ ๊ตํ†ต ํ‘œ์ง€ํŒ์„ ๋ณด๊ณ  ๊ทœ์น™์„ ์ง€ํ‚ค๋Š” ๊ฒƒ์€ ์šด์ „์ž์˜ ๋ฐ ๊ตํ†ต ์•ˆ์ „์„ ์œ„ํ•ด์„œ ์ค‘์š”ํ•œ ์ผ์ž…๋‹ˆ๋‹ค. ๋งŒ์ผ ์‚ฌ๋žŒ์ด ์•„๋‹Œ ๊ธฐ๊ณ„๊ฐ€ ์ด๋ฅผ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•œ๋‹ค๋ฉด, ์–ด๋–ป๊ฒŒ ํ‘œ์ง€ํŒ์„ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”? ์ด๋Ÿฌํ•œ ๋ฌผ์Œ์€ ์ž์œจ ์ฃผํ–‰์ฐจ ๊ธฐ์ˆ ์ด ๋ฐœ์ „ํ•˜๋ฉด์„œ ์ค‘์š”ํ•œ ์ด์Šˆ๊ฐ€ ๋˜์—ˆ๊ณ , ๋”ฅ๋Ÿฌ๋‹ ๊ธฐ์ˆ  ๋ฐ”ํƒ•์˜ ๋ถ„๋ฅ˜ ๋ชจ๋ธ์ด ์ƒ๋‹นํ•œ ์ˆ˜์ค€์˜ ์ •ํ™•๋„๋ฅผ ๋ณด์ด๋ฉฐ ์ ์šฉ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋ฒˆ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ๊ตํ†ต ํ‘œ์ง€ํŒ ๋ถ„๋ฅ˜์˜ ์ฒซ ๋ฒˆ์งธ ์Šคํ…์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตํ†ต ํ‘œ์ง€ํŒ ์ด๋ฏธ์ง€๊ฐ€ ์ž…๋ ฅ ๋˜์—ˆ์„ ๋•Œ ์ด ๊ฒƒ์ด 43 ์ข…์˜ ํ‘œ์ง€ํŒ ์ค‘ ์–ด๋–ค ๊ฒƒ์ธ๊ฐ€๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋Š” ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•˜์—ฌ ๊ตํ†ต ํ‘œ์ง€ํŒ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๋“ค์˜ ํŠน์ง•๊ณผ CNN ๋ชจ๋ธ์„ ํ†ตํ•˜์—ฌ ๋ถ„๋ฅ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์„ ํ•™์Šตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฒซ ๋ฒˆ์งธ ์Šคํ…์„ ํ™•์žฅํ•˜์—ฌ, ์ถ” ํ›„์—๋Š” ๊ตํ†ต ํ‘œ์ง€ํŒ๋งŒ ์žˆ๋Š” ์ด๋ฏธ์ง€๋ฅผ ์ž…๋ ฅ์œผ๋กœ ํ•˜์ง€ ์•Š๊ณ  ๋„๋กœ ์ด๋ฏธ์ง€์—์„œ ๊ตํ†ต ํ‘œ์ง€ํŒ์„ ๋ถ„๋ฆฌํ•˜์—ฌ ๋ถ„๋ฅ˜ํ•˜๋Š” Object detection ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ฉฐ, ์ตœ์ข…์ ์œผ๋กœ๋Š” ์„ผ์„œ๋กœ ์ž…๋ ฅ๋ฐ›๋Š” ๋น„๋””์˜ค ์ด๋ฏธ์ง€์—์„œ์˜ ๊ตํ†ต ํ‘œ์ง€ํŒ์„ ๋ถ„๋ฆฌํ•˜๊ณ  ๋ถ„๋ฅ˜ํ•˜๋Š” ๋ชจ๋ธ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

import os
import pathlib
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
from sklearn.model_selection import train_test_split
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator, img_to_array, array_to_img, load_img
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.layers import Conv2D, MaxPool2D, Dense, Flatten, Dropout
from tensorflow.keras.models import Sequential


%matplotlib inline

 

1. ๋ฐ์ดํ„ฐ ๋ถ„์„

1-1. ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ ์ •๋ณด ํŒŒ์•…ํ•˜๊ธฐ - Meta

file_list = os.listdir('./data')
file_list

Meta_images = []
Meta_labels = []

plt.figure(figsize=(16,16))
for i in range(len(df_Meta)):
    img = load_img('./data/'+df_Meta['Path'][i])
    plt.subplot(1, 3, i+1)
    plt.imshow(img)
    Meta_images.append(img)
    Meta_labels.append(df_Meta['ClassId'][i])

 

1-2. ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ ์ •๋ณด ํŒŒ์•…ํ•˜๊ธฐ - Train

df_Train = pd.read_csv('./data/Train.csv')
df_Train

Width์™€ Height ์ •๋ณด๋Š” ์ด๋ฏธ์ง€์˜ ํญ๊ณผ ๋†’์ด์— ๋Œ€ํ•œ ์ •๋ณด๋กœ ๊ฐ„๋‹จํžˆ ์ƒ˜ํ”Œ๋งŒ ๋ด๋„ ๋‹ค์–‘ํ•œ ํฌ๊ธฐ๋ฅผ ๊ฐ–๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ํฌ๊ธฐ๊ฐ€ ๋ชจ๋‘ ๋‹ค๋ฅด๋‹ค๋ฉด ์ด๋ฏธ์ง€๋งˆ๋‹ค ์„œ๋กœ ๋‹ค๋ฅธ feature์˜ ๊ฐœ์ˆ˜๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ด๊ธฐ์— ์ด๋ฅผ ํ†ต์ผํ•ด์ฃผ๋Š” ์ „ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์–ด๋–ค ํฌ๊ธฐ๋กœ ํ†ต์ผ์„ ํ•ด์•ผ ํ• ๊นŒ์š”? ์ด๋ฏธ์ง€ ํฌ๊ธฐ์˜ ๋ถ„ํฌ๋ฅผ ๋ณด๊ณ  ํŒ๋‹จํ•ด๋ด…์‹œ๋‹ค.

 

import seaborn as sns

plt.figure(figsize=(20,10))
ax = sns.countplot(x="Width", data=df_Train)

df_cutWidth = pd.cut(df_Train['Width'], np.arange(0,200,10)).value_counts(sort=False)

fig, ax = plt.subplots(figsize=(20,10))
ax.bar(range(len(df_cutWidth)),df_cutWidth.values)
ax.set_xticks(range(len(df_cutWidth)))
ax.set_xticklabels(df_cutWidth.index)
fig.show()

*  30~35์˜ ํญ ๋˜๋Š” ๋†’์ด๋ฅผ ๊ฐ–๋Š” ์ด๋ฏธ์ง€๊ฐ€ ์ œ์ผ ๋งŽ์Œ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ํฌ๊ธฐ๋ฅผ ํ†ต์ผํ•˜๋Š” ๋ฐ ์žˆ์–ด์„œ ๋„ˆ๋ฌด ์ž‘์€ ์ด๋ฏธ์ง€๋Š” ํฐ ์ด๋ฏธ์ง€์˜ ์ •๋ณด ์†์‹ค์„ ๋ฐœ์ƒํ•˜๋ฉฐ, ๋„ˆ๋ฌด ํฐ ์ด๋ฏธ์ง€๋Š” ์ž‘์€ ์ด๋ฏธ์ง€์˜ ์ •๋ณด ๋ถ€์กฑํ•œ ์ •๋ณด๋Ÿ‰์„ ๋ถ€๊ฐํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ ์ ˆํ•œ ์ด๋ฏธ์ง€ ํฌ๊ธฐ๋ฅผ ์žก๋Š” ๊ฒƒ์€ ํ•˜๋‚˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์กฐ์ •์ด ๋˜๋ฉฐ, ์ด๋ฒˆ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ์ด๋ฏธ์ง€ ๋ถ„ํฌ ๊ธฐ๋ฐ˜์œผ๋กœ ๋Œ€๋‹ค์ˆ˜๋ฅผ ์ฐจ์ง€ํ•˜๋Š” ํฌ๊ธฐ์ธ 33x33 ํฌ๊ธฐ๋กœ ํ†ต์ผํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. (์ดํ›„, ๊ฐœ์ธ์ ์ธ ์‹ค์Šต์—์„œ ๋ฐ์ดํ„ฐ ํฌ๊ธฐ ์กฐ์ ˆ์— ๋”ฐ๋ฅธ ์„ฑ๋Šฅ ๋ณ€ํ™”๋ฅผ ์‚ดํŽด๋ณด๋Š” ๊ฒƒ๋„ ์ข‹์€ ํ•™์Šต์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.)

image_height = 33
image_width = 33
image_channel = 3 # ์ปฌ๋Ÿฌ ์ด๋ฏธ์ง€์ด๊ธฐ์— 3์ฑ„๋„

from PIL import Image
from PIL import ImageDraw

img_sample = Image.open('./data/'+df_Train['Path'][0])

draw = ImageDraw.Draw(img_sample)
draw.rectangle([df_Train['Roi.X1'][0], df_Train['Roi.Y1'][0], df_Train['Roi.X2'][0], df_Train['Roi.Y2'][0]], outline="red")
img_sample_resized = img_sample.resize((300,300))
img_sample_resized

* Roi ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ณด๋‹ค ๋ช…ํ™•ํ•˜๊ฒŒ ํ‘œ์ง€ํŒ ๋ถ€๋ถ„๋งŒ์„ crop ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋Ÿฌํ•œ ๋ฐ์ดํ„ฐ ์ „ ์ฒ˜๋ฆฌ๋ฅผ ํ†ตํ•˜์—ฌ ๋ถ„๋ฅ˜์˜ ์„ฑ๋Šฅ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (์ด๋ฒˆ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ํ•ด๋‹น ์ „ ์ฒ˜๋ฆฌ๋ฅผ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค.)

img_sample_crop = img_sample.crop((df_Train['Roi.X1'][0], df_Train['Roi.Y1'][0], df_Train['Roi.X2'][0], df_Train['Roi.Y2'][0]))
 
# Shows the image in image viewer
img_sample_crop_resized = img_sample_crop.resize((300,300))
img_sample_crop_resized

 

1-3. ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ ์ •๋ณด ํŒŒ์•…ํ•˜๊ธฐ - Test

df_Test = pd.read_csv('./data/Test.csv')
df_Test

Train.csv์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. test ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋Š” ๋ถ„์„ํ•˜๋Š” ๊ฒƒ์€ ํ‘œ์ง€ํŒ์„ ๋ถ„๋ฅ˜ ํ•˜๋Š” ๋ชฉ์ ์— ์žˆ์–ด ํฐ ์˜๋ฏธ๊ฐ€ ์žˆ์ง€ ์•Š๊ธฐ์— ์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค.

ํ€ด์ฆˆ1. Roi ์˜์—ญ์— ํฌ๊ธฐ์— ๋งž๊ฒŒ df_Test['Path'][0] ํ•ด๋‹น๋˜๋Š” ์ด๋ฏธ์ง€์— crop์„ ์ˆ˜ํ–‰ํ•œ ํ›„ (300, 300) ํ•ด์ƒ๋„๋กœ resize ํ•ฉ๋‹ˆ๋‹ค.

# cropํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ ์ž˜๋ผ๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
# ์ด ํ›„, resizeํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์‚ฌ์ด์ฆˆ ๋ณ€๊ฒฝ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

img_sample_test = Image.open('./data/'+df_Train['Path'][0])
img_sample_crop_test = img_sample_test.crop((df_Train['Roi.X1'][0], df_Train['Roi.Y1'][0], df_Train['Roi.X2'][0], df_Train['Roi.Y2'][0]))
 
# Shows the image in image viewer
img_sample_crop_resized = img_sample_crop.resize((300,300))
img_sample_crop_resized

 

 

 

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

2-1. ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ

image_height = 33
image_width = 33
image_channel = 3

Train_images = []
Train_labels = []

for i in tqdm(range(len(df_Train))):
    img = load_img('./data/'+df_Train['Path'][i], target_size = (image_height, image_width))
    img = img_to_array(img)
    Train_images.append(img)
    
    
Test_images = []
Test_labels = []

for i in tqdm(range(len(df_Test))):
    img = load_img('./data/'+df_Test['Path'][i], target_size = (image_height, image_width))
    img = img_to_array(img)
    Test_images.append(img)

 

2-2. label ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ

Train_labels = df_Train['ClassId'].values
Train_labels #array([0, 0, 0, ..., 2, 2, 2])

Test_labels = df_Test['ClassId'].values
Test_labels
'''
array([1, 1, 2, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1, 2, 2, 1, 2, 1, 2, 1, 2, 2,
       1, 0, 0, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 2, 2, 2, 2, 1, 1, 2, 1, 0,
       2, 2, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 1, 1, 1, 1, 1, 1, 2, 0, 1, 2,
'''

 

2-3. ๋ฐ์ดํ„ฐ ๋ถ„๋ฆฌํ•˜๊ธฐ

x_train, x_val, y_train, y_val = train_test_split(np.array(Train_images), np.array(Train_labels), test_size=0.4)

x_test = np.array(Test_images)
y_test = np.array(Test_labels)

 

 

 

3. ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ

3-1. CNN ๋ชจ๋ธ ์„ค์ •

CNN์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ชจ๋ธ์„ ๊ตฌํ˜„ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. filters, kernel ๋“ฑ์˜ ์‚ฌ์ด์ฆˆ๋Š” ํ•˜์ดํผ ํŒŒ๋ฆฌ๋ฏธํ„ฐ๋กœ ์ž์‹ ๋งŒ์˜ ๋ชจ๋ธ๋กœ ํŠœ๋‹์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

model = Sequential([    
    Conv2D(filters=32, kernel_size=(3,3), activation='relu', input_shape=(image_height, image_width, image_channel)),
    MaxPool2D(pool_size=(2, 2)),
    Dropout(rate=0.25),
    
    Conv2D(filters=64, kernel_size=(3,3), activation='relu'),
    MaxPool2D(pool_size=(2, 2)),
    Dropout(rate=0.25),
    
    Flatten(),
    Dense(512, activation='relu'),
    Dropout(rate=0.25),
    Dense(3, activation='softmax')
])

model.summary()

 

3-2. ํ•™์Šต ์ˆ˜ํ–‰

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

# ์ฒ˜์Œ ๋งŒ๋“  ๋ชจ๋ธ์ด๋ผ๋ฉด EPOCHS๋ฅผ 1~5๊ฐœ๋กœ ํ•˜์—ฌ ์ž˜ ๋Œ์•„๊ฐ€๋Š”์ง€ ์„ฑ๋Šฅ์„ ํ™•์ธํ•ด๋ณด๊ณ  ๊ฐ’์„ ์ฆ๊ฐ€ ์‹œ์ผœ ๋ด…์‹œ๋‹ค. 
EPOCHS = 30

# EPOCHS์— ๋”ฐ๋ฅธ ์„ฑ๋Šฅ์„ ๋ณด๊ธฐ ์œ„ํ•˜์—ฌ history ์‚ฌ์šฉ
history = model.fit(x_train, 
                    y_train,
                    validation_data = (x_val, y_val), # validation ๋ฐ์ดํ„ฐ ์‚ฌ์šฉ
                    epochs=EPOCHS, 
                   )
'''
Epoch 30/30
51/51 [==============================] - 1s 28ms/step - loss: 0.4736 - accuracy: 0.7948 - val_loss: 0.3615 - val_accuracy: 0.8511
'''

ํ•™์Šต์„ ์ˆ˜ํ–‰ํ•˜๋ฉด์„œ Accuracy์™€ Loss์˜ ๋ณ€ํ™”๋ฅผ ๊ทธ๋ž˜ํ”„๋กœ ์ถœ๋ ฅํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

accuracy = history.history['accuracy']
val_accuracy = history.history['val_accuracy']

loss=history.history['loss']
val_loss=history.history['val_loss']

epochs_range = range(EPOCHS)

plt.figure(figsize=(16, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, accuracy, label='Training Accuracy')
plt.plot(epochs_range, val_accuracy, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()

 

 

3-3. ๋ชจ๋ธ ์„ฑ๋Šฅ ํ‰๊ฐ€ ๋ฐ ์˜ˆ์ธก

test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=0)

print('test set accuracy: ', test_accuracy)
#test set accuracy:  0.7814285755157471

ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์˜ˆ์ธก๋œ ๊ฒฐ๊ณผ๋ฅผ ๋น„๊ตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 25๊ฐœ์˜ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์™€ ์‹ค์ œ class์™€ ์˜ˆ์ธก class๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

test_prediction = np.argmax(model.predict(x_test), axis=-1)

plt.figure(figsize = (13, 13))

start_index = 0
for i in range(25):
    plt.subplot(5, 5, i + 1)
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])
    prediction = test_prediction[start_index + i]
    actual = y_test[start_index + i]
    col = 'g'
    if prediction != actual:
        col = 'r'
    plt.xlabel('Actual={} || Pred={}'.format(actual, prediction), color = col)
    plt.imshow(array_to_img(x_test[start_index + i]))
plt.show()

confusion matrix๋ฅผ ์‹œ๊ฐํ™” ํ•˜์—ฌ ๋ถ„๋ฅ˜ ํ•™์Šต ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด๋ด…๋‹ˆ๋‹ค.

import seaborn as sns
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, test_prediction)
plt.figure(figsize = (20, 20))
sns.heatmap(cm, annot = True)

 

ํ€ด์ฆˆ2. ์œ„ ํ•™์Šตํ•œ CNN ๋ชจ๋ธ์—์„œ ํ•™์Šต์šฉ ๋ฐ์ดํ„ฐ(x_train, y_train)์˜ confusion matrix๋ฅผ ๊ตฌํ•˜์„ธ์š”.

# CNN ๋ชจ๋ธ์˜ x_train์— ๋Œ€ํ•œ ์˜ˆ์ธก๊ฐ’์„ ๊ตฌํ•˜๊ณ  confusion_matrix() ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด confusion matrix๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
train_prediction = np.argmax(model.predict(x_train), axis=-1)
cm_train = confusion_matrix(y_train, train_prediction)

# confusion_matrix() ๊ฒฐ๊ณผ๊ฐ’์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. 
quiz_2 = cm_train

 

์ฑŒ๋ฆฐ์ง€

์ด๋ฒˆ ํ”„๋กœ์ ํŠธ์—์„œ 3๊ฐœ์˜ label์— ๋Œ€ํ•ด์„œ๋งŒ ํ•™์Šต์„ ์ˆ˜ํ–‰ํ–ˆ์ง€๋งŒ, ์›๋ณธ ๋ฐ์ดํ„ฐ์—์„œ๋Š” 43๊ฐœ์˜ label๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. label ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์•„์ง„ ๋งŒํผ ์ •ํ™•๋„๋ฅผ ๋†’์ด๊ธฐ ์œ„ํ•ด์„œ ์ „ ์ฒ˜๋ฆฌ ๋‹จ๊ณ„์—์„œ crop ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ Data augmentation์„ ํ†ตํ•ด์„œ ์ƒ๋Œ€์ ์œผ๋กœ ์ ์€ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜๋ฅผ ๋Š˜๋ฆฌ๋Š” ๋ฐฉ๋ฒ• ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ์—์„œ๋Š” ์œ„ ์ปค์Šคํ…€ CNN ๋ชจ๋ธ ์ด์™ธ๋กœ ์–ด๋А ์ •๋„ ์„ฑ๋Šฅ์ด ๊ฒ€์ฆ๋œ VGG, GoogLeNet, ResNet ๋“ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์„ฑ๋Šฅ์„ ๋†’์—ฌ๋ณด์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค.

+ Recent posts