<jupyter_start><jupyter_text>케라스로 VGGNet 만들기 이 노트북에서 "매우 깊은" 합성곱 신경망 [VGGNet](https://arxiv.org/pdf/1409.1556.pdf)과 비슷한 모델을 훈련하여 옥스포드 꽃 데이터셋을 17개 카테고리로 분류합니다. [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/rickiepark/dl-illustrated/blob/master/notebooks/10-3.vggnet_in_keras.ipynb)  동일하게 재현하기 위해 랜덤 시드 값을 지정합니다.<jupyter_code>import numpy as np
np.random.seed(42)<jupyter_output><empty_output><jupyter_text>라이브러리를 적재합니다.<jupyter_code>from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.callbacks import TensorBoard<jupyter_output><empty_output><jupyter_text>데이터를 적재하고 전처리합니다. 이미 `oxflower17.npz` 파일을 다운받았다고 가정합니다. 이 파일이 없다면 `10-2.alexnet_in_keras.ipynb` 노트북을 다시 실행하세요.<jupyter_code># 코랩을 사용할 경우 다음 셀을 실행하세요.
!rm oxflower17*
!wget https://bit.ly/36QytdH -O oxflower17.npz
ls -al
import numpy as np

data = np.load('oxflower17.npz', allow_pickle=True)
X = data['X']
Y = data['Y']<jupyter_output><empty_output><jupyter_text>신경망 모델을 만듭니다.<jupyter_code>model = Sequential()

model.add(Conv2D(64, 3, activation='relu', input_shape=(224, 224, 3)))
model.add(Conv2D(64, 3, activation='relu'))
model.add(MaxPooling2D(2, 2))
model.add(BatchNormalization())

model.add(Conv2D(128, 3, activation='relu'))
model.add(Conv2D(128, 3, activation='relu'))
model.add(MaxPooling2D(2, 2))
model.add(BatchNormalization())

model.add(Conv2D(256, 3, activation='relu'))
model.add(Conv2D(256, 3, activation='relu'))
model.add(Conv2D(256, 3, activation='relu'))
model.add(MaxPooling2D(2, 2))
model.add(BatchNormalization())

model.add(Conv2D(512, 3, activation='relu'))
model.add(Conv2D(512, 3, activation='relu'))
model.add(Conv2D(512, 3, activation='relu'))
model.add(MaxPooling2D(2, 2))
model.add(BatchNormalization())

model.add(Conv2D(512, 3, activation='relu'))
model.add(Conv2D(512, 3, activation='relu'))
model.add(Conv2D(512, 3, activation='relu'))
model.add(MaxPooling2D(2, 2))
model.add(BatchNormalization())

model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))

model.add(Dense(17, activation='softmax'))
model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 222, 222, 64)      1792      
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 220, 220, 64)      36928     
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 110, 110, 64)      0         
_________________________________________________________________
batch_normalization (BatchNo (None, 110, 110, 64)      256       
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 108, 108, 128)     73856     
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 106, 106, 128)     147584    
________________________________________________________[...]<jupyter_text>모델을 설정합니다.<jupyter_code>model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>텐서보드를 설정합니다.<jupyter_code>tensorbrd = TensorBoard('logs/vggnet')<jupyter_output><empty_output><jupyter_text>훈련!<jupyter_code>model.fit(X, Y, batch_size=64, epochs=250, verbose=1, validation_split=0.1, shuffle=True, callbacks=[tensorbrd])<jupyter_output>Epoch 1/250
20/20 [==============================] - 31s 451ms/step - loss: 2.9429 - accuracy: 0.1642 - val_loss: 31.5923 - val_accuracy: 0.0515
Epoch 2/250
20/20 [==============================] - 7s 337ms/step - loss: 2.7096 - accuracy: 0.2239 - val_loss: 4.7099 - val_accuracy: 0.1029
Epoch 3/250
20/20 [==============================] - 7s 337ms/step - loss: 2.9013 - accuracy: 0.2116 - val_loss: 3.1221 - val_accuracy: 0.1471
Epoch 4/250
20/20 [==============================] - 7s 336ms/step - loss: 2.3329 - accuracy: 0.2525 - val_loss: 3.3452 - val_accuracy: 0.1103
Epoch 5/250
20/20 [==============================] - 7s 336ms/step - loss: 2.1524 - accuracy: 0.2663 - val_loss: 3.3944 - val_accuracy: 0.1103
Epoch 6/250
20/20 [==============================] - 7s 335ms/step - loss: 1.9633 - accuracy: 0.3252 - val_loss: 2.8286 - val_accuracy: 0.1765
Epoch 7/250
20/20 [==============================] - 7s 337ms/step - loss: 1.9303 - accuracy: 0.3309 - val_loss: 2.9324 - val_accuracy: 0.11[...]
{'fraction_non_alphanumeric': 0.18930726024547534, 'fraction_numerical': 0.0931974204285417, 'mean_word_length': 5.733893557422969, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '333007', 'license': 'MIT', 'n_tokens_mistral': 2160, 'n_tokens_neox': 1929, 'n_words': 317, 'path': 'notebooks/10-3.vggnet_in_keras.ipynb', 'repo_name': 'rickiepark/dl-illustrated', 'size': '4807'}