๋ฆฌ๋ทฐ
ํ์ต๋ชฉํ
๋ชฉ์ฐจ
ํ๋ก์ ํธ
์ค๋น
$ pip install pillow $
import warnings
warnings.filterwarnings("ignore")
print("์๋ฃ!")
์๋ฃ!
import tensorflow as tf
print(tf.__version__) # 2.6.0 ์ผ๋ก ์งํ
2.6.0
import tensorflow_datasets as tfds
tfds.__version__
'4.4.0'
tensorflow_datasets์ ๊ดํ ์ธ๋ถ ๋ด์ฉ์ ํ์ธ
tensorflow_datasets ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ tfds๋ก ๊ฐ์ ธ์์ผ๋, ๊ทธ ์ค cats_vs_dogs ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉ
# ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
(raw_train, raw_validation, raw_test), metadata = tfds.load(
'cats_vs_dogs',
split=['train[:80%]', 'train[80%:90%]', 'train[90%:]'],
with_info=True,
as_supervised=True,
)
[1mDownloading and preparing dataset 786.68 MiB (download: 786.68 MiB, generated: Unknown size, total: 786.68 MiB) to /aiffel/tensorflow_datasets/cats_vs_dogs/4.0.0...[0m
Dl Completed...: 0 url [00:00, ? url/s]
Dl Size...: 0 MiB [00:00, ? MiB/s]
Generating splits...: 0%| | 0/1 [00:00<?, ? splits/s]
Generating train examples...: 0%| | 0/23262 [00:00<?, ? examples/s]
WARNING:absl:1738 images were corrupted and were skipped
Shuffling cats_vs_dogs-train.tfrecord...: 0%| | 0/23262 [00:00<?, ? examples/s]
[1mDataset cats_vs_dogs downloaded and prepared to /aiffel/tensorflow_datasets/cats_vs_dogs/4.0.0. Subsequent calls will reuse this data.[0m
print(raw_train)
print(raw_validation)
print(raw_test)
<PrefetchDataset shapes: ((None, None, 3), ()), types: (tf.uint8, tf.int64)>
<PrefetchDataset shapes: ((None, None, 3), ()), types: (tf.uint8, tf.int64)>
<PrefetchDataset shapes: ((None, None, 3), ()), types: (tf.uint8, tf.int64)>
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
print("์~")
์~
raw_train ์์ ์๋ ๋ฐ์ดํฐ๋ฅผ ํ์ธํด ๋ณด๊ธฐ ์ํด tf.data.Dataset์์ ์ ๊ณตํ๋ take๋ผ๋ ํจ์๋ฅผ ์ฌ์ฉ
์ด ํจ์๋ ์ธ์๋ก ๋ฐ์ ๋งํผ์ ๊ฐ์๋งํผ์ ๋ฐ์ดํฐ๋ฅผ ์ถ์ถํ์ฌ ์๋ก์ด ๋ฐ์ดํฐ์ ์ธ์คํด์ค๋ฅผ ์์ฑํ์ฌ ๋ฆฌํดํ๋ ํจ์.
๊ฐ์์ง๋ label 1๋ก, ๊ณ ์์ด๋ label 0์ผ๋ก ์ค์
IMG_SIZE = 160 # ๋ฆฌ์ฌ์ด์งํ ์ด๋ฏธ์ง์ ํฌ๊ธฐ
def format_example(image, label):
image = tf.cast(image, tf.float32) # image=float(image)๊ฐ์ ํ์
์บ์คํ
์ ํ
์ํ๋ก์ฐ ๋ฒ์ ์
๋๋ค.
image = (image/127.5) - 1 # ํฝ์
๊ฐ์ scale ์์
image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE))
return image, label
print("์~")
์~
์ด๋ฏธ์ง์ ์ฌ์ด์ฆ๋ฅผ 160x160 ํฝ์ ๋ก ํต์ผ์ํฌ ๋ฟ๋ง ์๋๋ผ, ๊ฐ ํฝ์ ๊ฐ์ scale์ ์์ ํด์ฃผ๋ ์ญํ
format_example() ํจ์๋ฅผ raw_train, raw_validation, raw_test ์ map() ํจ์๋ก ์ ์ฉ์์ผ์ ์ํ๋ ๋ชจ์์ train, validataion, test ๋ฐ์ดํฐ์ ์ผ๋ก ๋ณํ
๋ฆฌ์คํธ ์์ ์ ์ฒด์ ๋์ผํ ๋ณํ ํจ์๋ฅผ for ๋ฌธ์ ์ฌ์ฉํ์ง ์๊ณ ์์ฝ๊ฒ ์ ์ฉํ๊ฒ ํด์ฃผ๋ map ํจ์ ์ฌ์ฉ
train = raw_train.map(format_example)
validation = raw_validation.map(format_example)
test = raw_test.map(format_example)
print(train)
print(validation)
print(test)
<MapDataset shapes: ((160, 160, 3), ()), types: (tf.float32, tf.int64)>
<MapDataset shapes: ((160, 160, 3), ()), types: (tf.float32, tf.int64)>
<MapDataset shapes: ((160, 160, 3), ()), types: (tf.float32, tf.int64)>
IMG_SIZE๋ฅผ 160์ผ๋ก ์ง์ ํด ์ค์ผ๋ก์จ, ๋ชจ๋ ์ด๋ฏธ์ง์ ํฌ๊ธฐ๋ฅผ (160, 160, 3)
plt.figure(figsize=(10, 5))
get_label_name = metadata.features['label'].int2str
for idx, (image, label) in enumerate(raw_train.take(10)): # 10๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ ์ต๋๋ค.
plt.subplot(2, 5, idx+1)
plt.imshow(image)
plt.title(f'label {label}: {get_label_name(label)}')
plt.axis('off')
plt.figure(figsize=(10, 5))
get_label_name = metadata.features['label'].int2str
for idx, (image, label) in enumerate(train.take(10)):
plt.subplot(2, 5, idx+1)
image = (image + 1) / 2
plt.imshow(image)
plt.title(f'label {label}: {get_label_name(label)}')
plt.axis('off')
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten, MaxPooling2D
print("์~")
์~
model = Sequential([
Conv2D(filters=16, kernel_size=3, padding='same', activation='relu', input_shape=(160, 160, 3)),
MaxPooling2D(),
Conv2D(filters=32, kernel_size=3, padding='same', activation='relu'),
MaxPooling2D(),
Conv2D(filters=64, kernel_size=3, padding='same', activation='relu'),
MaxPooling2D(),
Flatten(),
Dense(units=512, activation='relu'),
Dense(units=2, activation='softmax')
])
print("์~")
์~
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 160, 160, 16) 448
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 80, 80, 16) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 80, 80, 32) 4640
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 40, 40, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 40, 40, 64) 18496
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 20, 20, 64) 0
_________________________________________________________________
flatten (Flatten) (None, 25600) 0
_________________________________________________________________
dense (Dense) (None, 512) 13107712
_________________________________________________________________
dense_1 (Dense) (None, 2) 1026
=================================================================
Total params: 13,132,322
Trainable params: 13,132,322
Non-trainable params: 0
_________________________________________________________________
- 4๊ฐ์ง ์ข ๋ฅ์ ๋ ์ด์ด
- Conv2D, MaxPooling2D, Flatten, Dense
- ๊ฐ ๋ ์ด์ด๋ฅผ ์ง๋๋ฉด์ ์ค์ด๋ ๋ค
- flatten ๊ณ์ธต์์ 1์ฐจ์์ shape๊ฐ ์ค์ด๋ ๋ค.
- CNN ๋ฅ๋ฌ๋ ๋ชจ๋ธ์ ํน์ง
๋งจ ์ผ์ชฝ์ฒ๋ผ ์ด๋ฏธ์ง ํ ์ฅ์ด ์ ๋ ฅ๋๋ฉด ๊ทธ ์ด๋ฏธ์ง๋ Convolutional(ํฉ์ฑ๊ณฑ) ์ฐ์ฐ์ ํตํด ๊ทธ ํํ๊ฐ ์ ์ ๊ธธ์ญํด์ง๋ค๊ฐ, Flatten ๋ ์ด์ด๋ฅผ ๋ง๋๋ฉด ์ค๋ฅธ์ชฝ์ฒ๋ผ ํ ์ค๋ก ํด์ง๋๋ค. 3์ฐจ์์ ์ด๋ฏธ์ง๋ฅผ 1์ฐจ์์ผ๋ก
Flatten์ ์กฐ๊ธ ๋ ์ง๊ด์ ์ผ๋ก ์ดํดํ๊ธฐ ์ํด ๋ฐฐ์ด์ ๋ค๋ฃจ๊ธฐ ์ฉ์ดํ numpy๋ฅผ ํ์ฉ
import numpy as np
image = np.array([[1, 2], [3, 4]])
print(image.shape)
image
(2, 2)
array([[1, 2],
[3, 4]])
์์ ๊ฐ์ด (2,2) ํฌ๊ธฐ์ ์ด๋ฏธ์ง๊ฐ ์์ ๋ ์ด๊ฒ์ Flatten์ํค๋ฉด.
.
image.flatten()
array([1, 2, 3, 4])
๋ชจ๋ ์ซ์๋ฅผ ์ผ๋ ฌ๋ก ํธ ์ํ๋ก
๋ฅ๋ฌ๋ ๋ชจ๋ธ์ (160, 160, 3) ํฌ๊ธฐ์ 3์ฐจ์ ์ด๋ฏธ์ง๋ฅผ ์ ๋ ฅ๋ฐ์ ์ฌ๋ฌ ๋ ์ด์ด๋ฅผ ๊ฑฐ์น๋ฉฐ ํํ๋ฅผ ๋ฐ๊พธ๋ค๊ฐ ์ต์ข ์ ์ผ๋ก๋ ๋ช ๊ฐ์ ์ซ์๋ฅผ ์ถ๋ ฅํด๋ด๋ ํจ์
learning_rate = 0.0001
model.compile(optimizer=tf.keras.optimizers.RMSprop(lr=learning_rate),
loss=tf.keras.losses.sparse_categorical_crossentropy,
metrics=['accuracy'])
print("์~")
์~
BATCH_SIZE = 32
SHUFFLE_BUFFER_SIZE = 1000
print("์~")
์~
train_batches = train.shuffle(SHUFFLE_BUFFER_SIZE).batch(BATCH_SIZE)
validation_batches = validation.batch(BATCH_SIZE)
test_batches = test.batch(BATCH_SIZE)
print("์~")
์~
train_batches์์ ํ๋์ batch๋ง ๊บผ๋ด ๋ฐ์ดํฐ๋ฅผ ํ์ธ
for image_batch, label_batch in train_batches.take(1):
pass
image_batch.shape, label_batch.shape
(TensorShape([32, 160, 160, 3]), TensorShape([32]))
# image_batch์ shape๋ [32, 160, 160, 3]์, label_batch์ shape๋ [32]
image_batch์ shape๋ (160, 160, 3)์ shape์ธ 32๊ฐ์ ๋ฐ์ดํฐ๊ฐ ์กด์ฌํ๋ค๋ ๋ป์ด๋ค. ์ฆ, ๋ฐ์ดํฐ ํ๋์ ํฌ๊ธฐ๋ (160, 160, 3)์ด๊ณ , ๊ทธ ๊ฐ์๊ฐ 32๊ฐ
label์ ๊ฐ์์ง์ด๋ฉด 1, ๊ณ ์์ด์ด๋ฉด 0์ผ๋ก ์ ๋ต label์ ๋ํ๋ด๊ธฐ ๋๋ฌธ์ ํ batch์ ๋ฐ์ดํฐ๊ฐ 32๊ฐ๋ผ๋ฉด label์ 0 ๋๋ 1์ 32๊ฐ์ ์ซ์๋ก๋ง ๊ตฌ์ฑ
validation_steps = 20
loss0, accuracy0 = model.evaluate(validation_batches, steps=validation_steps)
print("initial loss: {:.2f}".format(loss0))
print("initial accuracy: {:.2f}".format(accuracy0))
11/20 [===============>..............] - ETA: 0s - loss: 0.6959 - accuracy: 0.4858
Corrupt JPEG data: 162 extraneous bytes before marker 0xd9
20/20 [==============================] - 26s 31ms/step - loss: 0.6949 - accuracy: 0.5047
initial loss: 0.69
initial accuracy: 0.50
Corrupt JPEG data: 252 extraneous bytes before marker 0xd9
loss๋ ๋ง ๊ทธ๋๋ก "์์ค"์ด๋ผ๋ ๋ป์ผ๋ก, ์ผ๋ง๋ ๋ชจ๋ธ์ด ํ๋ ธ๋์ง ๋ํ๋ ๋๋ค. ๋ฐ๋ผ์ loss๋ ๋ฎ์์๋ก ์ข์ ๊ฒ์ด์ฃ . ๋ํ accuracy๋ ๋ช ํผ์ผํธ์ ์ ํ๋๋ฅผ ๋ณด์ด๋๋์ ๋ํ ์์น์ ๋๋ค. ์ฐ๋ฆฌ๋ ๊ฐ์์ง์ ๊ณ ์์ด๋ฅผ ๋ถ๋ฅํ๋ ค๊ณ ํ๋๋ฐ, ๋ ์ฅ ์ค ํ๋๋ฅผ ์ฐ์ด๋ 50%๋ ๋์ฌ ํ ๋ ์ง๊ธ ๋ชจ๋ธ์ ์ ํ ์๋ฏธ ์๋ ์์ธก์ ํ๋ ๊ฒ
10 epoch๋ฅผ ํ์ต์์ผ์ ์ ํ๋๊ฐ ์ด๋ป๊ฒ ๋ณํ๋์ง ํ์ธํด ๋ณด๊ฒ ์ต๋๋ค. ์๋ ์ฝ๋๋ ํ์ต ํ๊ฒฝ์ ๋ฐ๋ผ ์ฝ 10~20๋ถ ๋ด์ธ
EPOCHS = 10
history = model.fit(train_batches,
epochs=EPOCHS,
validation_data=validation_batches)
Epoch 1/10
171/582 [=======>......................] - ETA: 18s - loss: 0.6723 - accuracy: 0.5932
Corrupt JPEG data: 99 extraneous bytes before marker 0xd9
201/582 [=========>....................] - ETA: 17s - loss: 0.6652 - accuracy: 0.6028
Warning: unknown JFIF revision number 0.00
213/582 [=========>....................] - ETA: 16s - loss: 0.6634 - accuracy: 0.6037
Corrupt JPEG data: 396 extraneous bytes before marker 0xd9
285/582 [=============>................] - ETA: 13s - loss: 0.6488 - accuracy: 0.6197
Corrupt JPEG data: 65 extraneous bytes before marker 0xd9
493/582 [========================>.....] - ETA: 3s - loss: 0.6145 - accuracy: 0.6561
Corrupt JPEG data: 2226 extraneous bytes before marker 0xd9
505/582 [=========================>....] - ETA: 3s - loss: 0.6120 - accuracy: 0.6583
Corrupt JPEG data: 128 extraneous bytes before marker 0xd9
516/582 [=========================>....] - ETA: 2s - loss: 0.6109 - accuracy: 0.6590
Corrupt JPEG data: 239 extraneous bytes before marker 0xd9
542/582 [==========================>...] - ETA: 1s - loss: 0.6073 - accuracy: 0.6632
Corrupt JPEG data: 1153 extraneous bytes before marker 0xd9
550/582 [===========================>..] - ETA: 1s - loss: 0.6063 - accuracy: 0.6638
Corrupt JPEG data: 228 extraneous bytes before marker 0xd9
582/582 [==============================] - ETA: 0s - loss: 0.6021 - accuracy: 0.6680
Corrupt JPEG data: 162 extraneous bytes before marker 0xd9
Corrupt JPEG data: 252 extraneous bytes before marker 0xd9
Corrupt JPEG data: 214 extraneous bytes before marker 0xd9
582/582 [==============================] - 32s 46ms/step - loss: 0.6021 - accuracy: 0.6680 - val_loss: 0.5294 - val_accuracy: 0.7506
Epoch 2/10
169/582 [=======>......................] - ETA: 17s - loss: 0.5214 - accuracy: 0.7378
Corrupt JPEG data: 99 extraneous bytes before marker 0xd9
201/582 [=========>....................] - ETA: 15s - loss: 0.5185 - accuracy: 0.7414
Warning: unknown JFIF revision number 0.00
213/582 [=========>....................] - ETA: 15s - loss: 0.5170 - accuracy: 0.7435
Corrupt JPEG data: 396 extraneous bytes before marker 0xd9
285/582 [=============>................] - ETA: 12s - loss: 0.5090 - accuracy: 0.7491
Corrupt JPEG data: 65 extraneous bytes before marker 0xd9
492/582 [========================>.....] - ETA: 3s - loss: 0.4906 - accuracy: 0.7606
Corrupt JPEG data: 2226 extraneous bytes before marker 0xd9
506/582 [=========================>....] - ETA: 3s - loss: 0.4910 - accuracy: 0.7604
Corrupt JPEG data: 128 extraneous bytes before marker 0xd9
516/582 [=========================>....] - ETA: 2s - loss: 0.4899 - accuracy: 0.7612
Corrupt JPEG data: 239 extraneous bytes before marker 0xd9
544/582 [===========================>..] - ETA: 1s - loss: 0.4868 - accuracy: 0.7644
Corrupt JPEG data: 1153 extraneous bytes before marker 0xd9
550/582 [===========================>..] - ETA: 1s - loss: 0.4872 - accuracy: 0.7641
Corrupt JPEG data: 228 extraneous bytes before marker 0xd9
580/582 [============================>.] - ETA: 0s - loss: 0.4848 - accuracy: 0.7657
Corrupt JPEG data: 162 extraneous bytes before marker 0xd9
Corrupt JPEG data: 252 extraneous bytes before marker 0xd9
Corrupt JPEG data: 214 extraneous bytes before marker 0xd9
582/582 [==============================] - 27s 44ms/step - loss: 0.4848 - accuracy: 0.7658 - val_loss: 0.4952 - val_accuracy: 0.7584
Epoch 3/10
169/582 [=======>......................] - ETA: 17s - loss: 0.4497 - accuracy: 0.8014
Corrupt JPEG data: 99 extraneous bytes before marker 0xd9
201/582 [=========>....................] - ETA: 16s - loss: 0.4425 - accuracy: 0.8019
Warning: unknown JFIF revision number 0.00
213/582 [=========>....................] - ETA: 15s - loss: 0.4430 - accuracy: 0.8003
Corrupt JPEG data: 396 extraneous bytes before marker 0xd9
284/582 [=============>................] - ETA: 12s - loss: 0.4345 - accuracy: 0.8040
Corrupt JPEG data: 65 extraneous bytes before marker 0xd9
492/582 [========================>.....] - ETA: 3s - loss: 0.4231 - accuracy: 0.8095
Corrupt JPEG data: 2226 extraneous bytes before marker 0xd9
506/582 [=========================>....] - ETA: 3s - loss: 0.4229 - accuracy: 0.8100
Corrupt JPEG data: 128 extraneous bytes before marker 0xd9
516/582 [=========================>....] - ETA: 2s - loss: 0.4223 - accuracy: 0.8102
Corrupt JPEG data: 239 extraneous bytes before marker 0xd9
543/582 [==========================>...] - ETA: 1s - loss: 0.4205 - accuracy: 0.8111
Corrupt JPEG data: 1153 extraneous bytes before marker 0xd9
549/582 [===========================>..] - ETA: 1s - loss: 0.4204 - accuracy: 0.8112
Corrupt JPEG data: 228 extraneous bytes before marker 0xd9
581/582 [============================>.] - ETA: 0s - loss: 0.4183 - accuracy: 0.8117
Corrupt JPEG data: 162 extraneous bytes before marker 0xd9
Corrupt JPEG data: 252 extraneous bytes before marker 0xd9
Corrupt JPEG data: 214 extraneous bytes before marker 0xd9
582/582 [==============================] - 27s 45ms/step - loss: 0.4182 - accuracy: 0.8118 - val_loss: 0.5108 - val_accuracy: 0.7554
Epoch 4/10
170/582 [=======>......................] - ETA: 18s - loss: 0.3974 - accuracy: 0.8208
Corrupt JPEG data: 99 extraneous bytes before marker 0xd9
202/582 [=========>....................] - ETA: 16s - loss: 0.3959 - accuracy: 0.8232
Warning: unknown JFIF revision number 0.00
212/582 [=========>....................] - ETA: 16s - loss: 0.3946 - accuracy: 0.8230
Corrupt JPEG data: 396 extraneous bytes before marker 0xd9
283/582 [=============>................] - ETA: 12s - loss: 0.3861 - accuracy: 0.8287
Corrupt JPEG data: 65 extraneous bytes before marker 0xd9
493/582 [========================>.....] - ETA: 3s - loss: 0.3710 - accuracy: 0.8370
Corrupt JPEG data: 2226 extraneous bytes before marker 0xd9
505/582 [=========================>....] - ETA: 3s - loss: 0.3701 - accuracy: 0.8373
Corrupt JPEG data: 128 extraneous bytes before marker 0xd9
515/582 [=========================>....] - ETA: 2s - loss: 0.3690 - accuracy: 0.8377
Corrupt JPEG data: 239 extraneous bytes before marker 0xd9
543/582 [==========================>...] - ETA: 1s - loss: 0.3684 - accuracy: 0.8390
Corrupt JPEG data: 1153 extraneous bytes before marker 0xd9
549/582 [===========================>..] - ETA: 1s - loss: 0.3690 - accuracy: 0.8386
Corrupt JPEG data: 228 extraneous bytes before marker 0xd9
581/582 [============================>.] - ETA: 0s - loss: 0.3667 - accuracy: 0.8396
Corrupt JPEG data: 162 extraneous bytes before marker 0xd9
Corrupt JPEG data: 252 extraneous bytes before marker 0xd9
Corrupt JPEG data: 214 extraneous bytes before marker 0xd9
582/582 [==============================] - 27s 45ms/step - loss: 0.3667 - accuracy: 0.8395 - val_loss: 0.5042 - val_accuracy: 0.7562
Epoch 5/10
170/582 [=======>......................] - ETA: 17s - loss: 0.3392 - accuracy: 0.8524
Corrupt JPEG data: 99 extraneous bytes before marker 0xd9
202/582 [=========>....................] - ETA: 15s - loss: 0.3368 - accuracy: 0.8524
Warning: unknown JFIF revision number 0.00
212/582 [=========>....................] - ETA: 15s - loss: 0.3363 - accuracy: 0.8526
Corrupt JPEG data: 396 extraneous bytes before marker 0xd9
284/582 [=============>................] - ETA: 12s - loss: 0.3276 - accuracy: 0.8587
Corrupt JPEG data: 65 extraneous bytes before marker 0xd9
492/582 [========================>.....] - ETA: 3s - loss: 0.3172 - accuracy: 0.8657
Corrupt JPEG data: 2226 extraneous bytes before marker 0xd9
506/582 [=========================>....] - ETA: 3s - loss: 0.3175 - accuracy: 0.8654
Corrupt JPEG data: 128 extraneous bytes before marker 0xd9
516/582 [=========================>....] - ETA: 2s - loss: 0.3165 - accuracy: 0.8658
Corrupt JPEG data: 239 extraneous bytes before marker 0xd9
544/582 [===========================>..] - ETA: 1s - loss: 0.3148 - accuracy: 0.8668
Corrupt JPEG data: 1153 extraneous bytes before marker 0xd9
550/582 [===========================>..] - ETA: 1s - loss: 0.3144 - accuracy: 0.8672
Corrupt JPEG data: 228 extraneous bytes before marker 0xd9
580/582 [============================>.] - ETA: 0s - loss: 0.3125 - accuracy: 0.8677
Corrupt JPEG data: 162 extraneous bytes before marker 0xd9
Corrupt JPEG data: 252 extraneous bytes before marker 0xd9
Corrupt JPEG data: 214 extraneous bytes before marker 0xd9
582/582 [==============================] - 27s 44ms/step - loss: 0.3123 - accuracy: 0.8678 - val_loss: 0.5062 - val_accuracy: 0.7743
Epoch 6/10
170/582 [=======>......................] - ETA: 17s - loss: 0.2838 - accuracy: 0.8801
Corrupt JPEG data: 99 extraneous bytes before marker 0xd9
202/582 [=========>....................] - ETA: 16s - loss: 0.2851 - accuracy: 0.8796
Warning: unknown JFIF revision number 0.00
212/582 [=========>....................] - ETA: 15s - loss: 0.2843 - accuracy: 0.8805
Corrupt JPEG data: 396 extraneous bytes before marker 0xd9
284/582 [=============>................] - ETA: 12s - loss: 0.2780 - accuracy: 0.8845
Corrupt JPEG data: 65 extraneous bytes before marker 0xd9
492/582 [========================>.....] - ETA: 3s - loss: 0.2680 - accuracy: 0.8888
Corrupt JPEG data: 2226 extraneous bytes before marker 0xd9
506/582 [=========================>....] - ETA: 3s - loss: 0.2670 - accuracy: 0.8888
Corrupt JPEG data: 128 extraneous bytes before marker 0xd9
516/582 [=========================>....] - ETA: 2s - loss: 0.2663 - accuracy: 0.8895
Corrupt JPEG data: 239 extraneous bytes before marker 0xd9
544/582 [===========================>..] - ETA: 1s - loss: 0.2651 - accuracy: 0.8899
Corrupt JPEG data: 1153 extraneous bytes before marker 0xd9
550/582 [===========================>..] - ETA: 1s - loss: 0.2643 - accuracy: 0.8905
Corrupt JPEG data: 228 extraneous bytes before marker 0xd9
580/582 [============================>.] - ETA: 0s - loss: 0.2630 - accuracy: 0.8904
Corrupt JPEG data: 162 extraneous bytes before marker 0xd9
Corrupt JPEG data: 252 extraneous bytes before marker 0xd9
Corrupt JPEG data: 214 extraneous bytes before marker 0xd9
582/582 [==============================] - 27s 44ms/step - loss: 0.2633 - accuracy: 0.8903 - val_loss: 0.5072 - val_accuracy: 0.7829
Epoch 7/10
169/582 [=======>......................] - ETA: 17s - loss: 0.2425 - accuracy: 0.9042
Corrupt JPEG data: 99 extraneous bytes before marker 0xd9
201/582 [=========>....................] - ETA: 15s - loss: 0.2393 - accuracy: 0.9053
Warning: unknown JFIF revision number 0.00
213/582 [=========>....................] - ETA: 15s - loss: 0.2379 - accuracy: 0.9068
Corrupt JPEG data: 396 extraneous bytes before marker 0xd9
285/582 [=============>................] - ETA: 12s - loss: 0.2295 - accuracy: 0.9116
Corrupt JPEG data: 65 extraneous bytes before marker 0xd9
493/582 [========================>.....] - ETA: 3s - loss: 0.2236 - accuracy: 0.9123
Corrupt JPEG data: 2226 extraneous bytes before marker 0xd9
505/582 [=========================>....] - ETA: 3s - loss: 0.2229 - accuracy: 0.9126
Corrupt JPEG data: 128 extraneous bytes before marker 0xd9
517/582 [=========================>....] - ETA: 2s - loss: 0.2222 - accuracy: 0.9131
Corrupt JPEG data: 239 extraneous bytes before marker 0xd9
543/582 [==========================>...] - ETA: 1s - loss: 0.2204 - accuracy: 0.9138
Corrupt JPEG data: 1153 extraneous bytes before marker 0xd9
549/582 [===========================>..] - ETA: 1s - loss: 0.2201 - accuracy: 0.9139
Corrupt JPEG data: 228 extraneous bytes before marker 0xd9
581/582 [============================>.] - ETA: 0s - loss: 0.2192 - accuracy: 0.9142
Corrupt JPEG data: 162 extraneous bytes before marker 0xd9
Corrupt JPEG data: 252 extraneous bytes before marker 0xd9
Corrupt JPEG data: 214 extraneous bytes before marker 0xd9
582/582 [==============================] - 26s 44ms/step - loss: 0.2191 - accuracy: 0.9142 - val_loss: 0.4732 - val_accuracy: 0.7966
Epoch 8/10
170/582 [=======>......................] - ETA: 17s - loss: 0.1912 - accuracy: 0.9316
Corrupt JPEG data: 99 extraneous bytes before marker 0xd9
202/582 [=========>....................] - ETA: 15s - loss: 0.1879 - accuracy: 0.9325
Warning: unknown JFIF revision number 0.00
212/582 [=========>....................] - ETA: 15s - loss: 0.1866 - accuracy: 0.9325
Corrupt JPEG data: 396 extraneous bytes before marker 0xd9
284/582 [=============>................] - ETA: 12s - loss: 0.1836 - accuracy: 0.9329
Corrupt JPEG data: 65 extraneous bytes before marker 0xd9
493/582 [========================>.....] - ETA: 3s - loss: 0.1747 - accuracy: 0.9358
Corrupt JPEG data: 2226 extraneous bytes before marker 0xd9
505/582 [=========================>....] - ETA: 3s - loss: 0.1742 - accuracy: 0.9360
Corrupt JPEG data: 128 extraneous bytes before marker 0xd9
517/582 [=========================>....] - ETA: 2s - loss: 0.1738 - accuracy: 0.9360
Corrupt JPEG data: 239 extraneous bytes before marker 0xd9
543/582 [==========================>...] - ETA: 1s - loss: 0.1730 - accuracy: 0.9364
Corrupt JPEG data: 1153 extraneous bytes before marker 0xd9
549/582 [===========================>..] - ETA: 1s - loss: 0.1728 - accuracy: 0.9365
Corrupt JPEG data: 228 extraneous bytes before marker 0xd9
581/582 [============================>.] - ETA: 0s - loss: 0.1714 - accuracy: 0.9368
Corrupt JPEG data: 162 extraneous bytes before marker 0xd9
Corrupt JPEG data: 252 extraneous bytes before marker 0xd9
Corrupt JPEG data: 214 extraneous bytes before marker 0xd9
582/582 [==============================] - 27s 44ms/step - loss: 0.1714 - accuracy: 0.9368 - val_loss: 0.5210 - val_accuracy: 0.7966
Epoch 9/10
169/582 [=======>......................] - ETA: 17s - loss: 0.1495 - accuracy: 0.9482
Corrupt JPEG data: 99 extraneous bytes before marker 0xd9
202/582 [=========>....................] - ETA: 15s - loss: 0.1501 - accuracy: 0.9476
Warning: unknown JFIF revision number 0.00
212/582 [=========>....................] - ETA: 15s - loss: 0.1515 - accuracy: 0.9462
Corrupt JPEG data: 396 extraneous bytes before marker 0xd9
283/582 [=============>................] - ETA: 12s - loss: 0.1447 - accuracy: 0.9483
Corrupt JPEG data: 65 extraneous bytes before marker 0xd9
493/582 [========================>.....] - ETA: 3s - loss: 0.1369 - accuracy: 0.9519
Corrupt JPEG data: 2226 extraneous bytes before marker 0xd9
505/582 [=========================>....] - ETA: 3s - loss: 0.1363 - accuracy: 0.9522
Corrupt JPEG data: 128 extraneous bytes before marker 0xd9
515/582 [=========================>....] - ETA: 2s - loss: 0.1356 - accuracy: 0.9523
Corrupt JPEG data: 239 extraneous bytes before marker 0xd9
543/582 [==========================>...] - ETA: 1s - loss: 0.1334 - accuracy: 0.9534
Corrupt JPEG data: 1153 extraneous bytes before marker 0xd9
549/582 [===========================>..] - ETA: 1s - loss: 0.1332 - accuracy: 0.9535
Corrupt JPEG data: 228 extraneous bytes before marker 0xd9
581/582 [============================>.] - ETA: 0s - loss: 0.1325 - accuracy: 0.9539
Corrupt JPEG data: 162 extraneous bytes before marker 0xd9
Corrupt JPEG data: 252 extraneous bytes before marker 0xd9
Corrupt JPEG data: 214 extraneous bytes before marker 0xd9
582/582 [==============================] - 27s 44ms/step - loss: 0.1325 - accuracy: 0.9538 - val_loss: 0.5836 - val_accuracy: 0.7928
Epoch 10/10
170/582 [=======>......................] - ETA: 17s - loss: 0.1139 - accuracy: 0.9601
Corrupt JPEG data: 99 extraneous bytes before marker 0xd9
202/582 [=========>....................] - ETA: 15s - loss: 0.1123 - accuracy: 0.9618
Warning: unknown JFIF revision number 0.00
212/582 [=========>....................] - ETA: 15s - loss: 0.1119 - accuracy: 0.9617
Corrupt JPEG data: 396 extraneous bytes before marker 0xd9
284/582 [=============>................] - ETA: 12s - loss: 0.1117 - accuracy: 0.9624
Corrupt JPEG data: 65 extraneous bytes before marker 0xd9
493/582 [========================>.....] - ETA: 3s - loss: 0.1040 - accuracy: 0.9649
Corrupt JPEG data: 2226 extraneous bytes before marker 0xd9
505/582 [=========================>....] - ETA: 3s - loss: 0.1033 - accuracy: 0.9653
Corrupt JPEG data: 128 extraneous bytes before marker 0xd9
517/582 [=========================>....] - ETA: 2s - loss: 0.1036 - accuracy: 0.9652
Corrupt JPEG data: 239 extraneous bytes before marker 0xd9
543/582 [==========================>...] - ETA: 1s - loss: 0.1018 - accuracy: 0.9660
Corrupt JPEG data: 1153 extraneous bytes before marker 0xd9
549/582 [===========================>..] - ETA: 1s - loss: 0.1015 - accuracy: 0.9660
Corrupt JPEG data: 228 extraneous bytes before marker 0xd9
581/582 [============================>.] - ETA: 0s - loss: 0.1013 - accuracy: 0.9663
Corrupt JPEG data: 162 extraneous bytes before marker 0xd9
Corrupt JPEG data: 252 extraneous bytes before marker 0xd9
Corrupt JPEG data: 214 extraneous bytes before marker 0xd9
582/582 [==============================] - 27s 44ms/step - loss: 0.1013 - accuracy: 0.9663 - val_loss: 0.6586 - val_accuracy: 0.7829
๋ชจ๋ ํ์ต์ด ๋์๋์? ์ด 10 epoch๋ฅผ ํ์ตํ ํ, ์ ํ๋๊ฐ ์ด๋ ์ ๋๊น์ง ์ฌ๋๋
Accuracy
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss=history.history['loss']
val_loss=history.history['val_loss']
epochs_range = range(EPOCHS)
plt.figure(figsize=(12, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend()
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()
plt.title('Training and Validation Loss')
plt.show()
์ ํ๋(accuracy)์ ์์ค๊ฐ(loss)์ ๋ํ ๋ ๊ฐ์ง ๊ทธ๋ํ๋ฅผ ๊ทธ๋ ค๋ณด์์ต๋๋ค. ๋ ๊ทธ๋ํ ๋ชจ๋ ํ๋ จ ๋ฐ์ดํฐ์ ์ ๋ํ ์์น์ ๊ฒ์ฆ ๋ฐ์ดํฐ์ ์ ๋ํ ์์น
์ด 10 epoch๋ฅผ ์งํํ๋ ๋์ training accuracy์ validation accuracy๋ ์ด๋ป๊ฒ ๋ณํํ๋์? ๊ฐ accuracy๊ฐ ๊ทธ๋ ๊ฒ ๋ณํ๋ ์ด์
training accuracy๋ 10 epoch๋ฅผ ์งํํ๋ ๋์ ๊พธ์คํ ์ฆ๊ฐํ๋ค. ๋ง์ง๋ง์๋ ๊ฑฐ์ 90%๋ฅผ ๋์ด์๋ ์์น๋ฅผ ๋ณด์ธ๋ค.
๋ฐ๋ฉด, validation accuracy๋ ์ด๋ฐ์ 75%~80% ์ฌ์ด๊น์ง๋ ์ฆ๊ฐํ์ง๋ง ์ฆ๊ฐํญ์ด training dataset์ ๋นํด ๋งค์ฐ ์ ๊ณ , ์ฆ๊ฐํ๋ ์์๋ ๋ถ์์ ํ๋ค.
training accuracy๋ ํ์ฌ ํ์ตํ๋ ๋ฐ์ดํฐ์ ์ ๋ํ ์ ํ๋์ด๊ธฐ ๋๋ฌธ์ ๋ชจ๋ธ์ ๊ตฌ์กฐ๋ ๋ฐ์ดํฐ์ ๋ฑ์ ๋ฌธ์ ๊ฐ ์๋ค๋ฉด ์ผ๋ฐ์ ์ผ๋ก ํ์ตํ๋ฉด ํ ์๋ก ๊พธ์คํ ๊ณ์ ์ค๋ฅธ๋ค. ๋ฐ๋ฉด validation accuracy๋ ํ์ตํ์ง ์์ ๋ฐ์ดํฐ์ ์ ๋ํ ์ ํ๋์ด๊ธฐ ๋๋ฌธ์ ์ผ์ ์์ค๊น์ง ์ค๋ฅธ ํ์๋ ๊ณ์ ์ค๋ฅผ์ง ์ฅ๋ดํ ์ ์๋ค.
training accuracy๋ ๊พธ์คํ ์ค๋ฅด์ง๋ง validation accuracy๋ ์ด๋ค ํ๊ณ์ ์ ๋์ง ๋ชปํ๋ ๊ฒ
loss ๊ทธ๋ํ์์ training loss๋ ๊ณ์ ์์ ์ ์ผ๋ก ์ค์ด๋ค์ง๋ง, validation loss๊ฐ์ ํน์ ์๊ฐ ์ดํ๋ก ๋ค์ ์ปค์ง๋ ๋ชจ์ต
์ด๋ฌํ ๋ฌธ์ ๋ฅผ ์นญํ๋ ์ฉ์ด๋
for image_batch, label_batch in test_batches.take(1):
images = image_batch
labels = label_batch
predictions = model.predict(image_batch)
pass
predictions
array([[9.9761641e-01, 2.3835611e-03],
[5.9462464e-01, 4.0537530e-01],
[1.3369282e-01, 8.6630714e-01],
[9.9969792e-01, 3.0216199e-04],
[5.5195206e-01, 4.4804800e-01],
[8.3197273e-02, 9.1680270e-01],
[2.3315031e-04, 9.9976689e-01],
[8.1265157e-01, 1.8734843e-01],
[1.0383534e-01, 8.9616460e-01],
[5.6698918e-01, 4.3301082e-01],
[5.5843964e-03, 9.9441564e-01],
[9.9648523e-01, 3.5148002e-03],
[9.9378610e-01, 6.2139085e-03],
[8.3754794e-04, 9.9916244e-01],
[9.9590296e-01, 4.0970314e-03],
[9.9773228e-01, 2.2677223e-03],
[2.1256688e-01, 7.8743309e-01],
[4.8445108e-06, 9.9999511e-01],
[5.8668250e-01, 4.1331753e-01],
[9.9996424e-01, 3.5768771e-05],
[3.8094068e-01, 6.1905932e-01],
[6.3675117e-01, 3.6324883e-01],
[9.9929571e-01, 7.0431735e-04],
[9.3341851e-01, 6.6581517e-02],
[2.9821005e-01, 7.0178992e-01],
[1.7958991e-02, 9.8204100e-01],
[9.9986172e-01, 1.3824736e-04],
[1.5848826e-01, 8.4151173e-01],
[9.6713167e-01, 3.2868307e-02],
[9.4930458e-01, 5.0695356e-02],
[9.7832263e-01, 2.1677295e-02],
[5.2817100e-01, 4.7182900e-01]], dtype=float32)
predictions๊ฐ ์์ฒญ๋ ์์์ ๊ฐ๋ค๋ก ์ด๋ฃจ์ด์ ธ ์๊ตฐ์. ์ด ๊ฐ์ ๋ชจ๋ธ์ด ํ๋จํ [๊ณ ์์ด์ผ ํ๋ฅ , ๊ฐ์์ง์ ํ๋ฅ ]์ธ๋ฐ, [1.0, 0.0]์ ๊ฐ๊น์ธ์๋ก label์ด 0์ธ ๊ณ ์์ด๋ก, [0.0, 1.0]์ ๊ฐ๊น์ธ์๋ก label์ด 1์ธ ๊ฐ์์ง๋ก ์์ธกํ๋ค๊ณ ๋ณผ ์ ์์
prediction ๊ฐ๋ค์ ์ค์ ์ถ๋ก ํ ๋ผ๋ฒจ(๊ณ ์์ด:0, ๊ฐ์์ง:1)๋ก ๋ณํํด ๋ณด๊ฒ ์ต๋๋ค. ์ค์ ๋ก ๋ชจ๋ธ์ด ๊ฐ ์ด๋ฏธ์ง๋ฅผ ๊ฐ์์ง๋ผ๊ณ ํ๋จํ๋์ง, ๊ณ ์์ด๋ก ํ๋จํ๋์ง ๋ณด๊ธฐ ์ํด
predictions = np.argmax(predictions, axis=1)
predictions
array([0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0,
0, 0, 1, 1, 0, 1, 0, 0, 0, 0])
plt.figure(figsize=(20, 12))
for idx, (image, label, prediction) in enumerate(zip(images, labels, predictions)):
plt.subplot(4, 8, idx+1)
image = (image + 1) / 2
plt.imshow(image)
correct = label == prediction
title = f'real: {label} / pred :{prediction}\n {correct}!'
if not correct:
plt.title(title, fontdict={'color': 'red'})
else:
plt.title(title, fontdict={'color': 'blue'})
plt.axis('off')
count = 0 # ์ ๋ต์ ๋ง์ถ ๊ฐ์
for image, label, prediction in zip(images, labels, predictions):
correct = label == prediction
if correct:
count = count + 1
print(count / 32 * 100)