Skip to content

Commit

Permalink
KerasKorea#40: Translate 5.3 - paragraph 7.1 / 11
Browse files Browse the repository at this point in the history
  • Loading branch information
visionNoob committed Oct 1, 2018
1 parent 8add7df commit 7c4ed71
Showing 1 changed file with 60 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,18 +210,9 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5\n",
"58892288/58889256 [==============================] - 28s 0us/step\n"
]
}
],
"outputs": [],
"source": [
"from keras.applications import VGG16\n",
"\n",
Expand Down Expand Up @@ -265,7 +256,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand All @@ -275,7 +266,7 @@
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"input_1 (InputLayer) (None, 150, 150, 3) 0 \n",
"input_2 (InputLayer) (None, 150, 150, 3) 0 \n",
"_________________________________________________________________\n",
"block1_conv1 (Conv2D) (None, 150, 150, 64) 1792 \n",
"_________________________________________________________________\n",
Expand Down Expand Up @@ -367,16 +358,21 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Found 2000 images belonging to 2 classes.\n",
"Found 1000 images belonging to 2 classes.\n",
"Found 1000 images belonging to 2 classes.\n"
"ename": "FileNotFoundError",
"evalue": "[Errno 2] No such file or directory: '/Users/fchollet/Downloads/cats_and_dogs_small/train'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-8-338451f49bca>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 32\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mfeatures\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlabels\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 33\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 34\u001b[0;31m \u001b[0mtrain_features\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtrain_labels\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mextract_features\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtrain_dir\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2000\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 35\u001b[0m \u001b[0mvalidation_features\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalidation_labels\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mextract_features\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalidation_dir\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1000\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 36\u001b[0m \u001b[0mtest_features\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtest_labels\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mextract_features\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtest_dir\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1000\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-8-338451f49bca>\u001b[0m in \u001b[0;36mextract_features\u001b[0;34m(directory, sample_count)\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0mtarget_size\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m150\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m150\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 20\u001b[0m \u001b[0mbatch_size\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mbatch_size\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 21\u001b[0;31m class_mode='binary')\n\u001b[0m\u001b[1;32m 22\u001b[0m \u001b[0mi\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 23\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0minputs_batch\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlabels_batch\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mgenerator\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/Users/homme/anaconda/lib/python3.6/site-packages/keras_preprocessing/image.py\u001b[0m in \u001b[0;36mflow_from_directory\u001b[0;34m(self, directory, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, save_to_dir, save_prefix, save_format, follow_links, subset, interpolation)\u001b[0m\n\u001b[1;32m 962\u001b[0m \u001b[0mfollow_links\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mfollow_links\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 963\u001b[0m \u001b[0msubset\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0msubset\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 964\u001b[0;31m interpolation=interpolation)\n\u001b[0m\u001b[1;32m 965\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 966\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mstandardize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/Users/homme/anaconda/lib/python3.6/site-packages/keras_preprocessing/image.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, directory, image_data_generator, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, data_format, save_to_dir, save_prefix, save_format, follow_links, subset, interpolation)\u001b[0m\n\u001b[1;32m 1718\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mclasses\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1719\u001b[0m \u001b[0mclasses\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1720\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0msubdir\u001b[0m \u001b[0;32min\u001b[0m \u001b[0msorted\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlistdir\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdirectory\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1721\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0misdir\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdirectory\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msubdir\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1722\u001b[0m \u001b[0mclasses\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msubdir\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: '/Users/fchollet/Downloads/cats_and_dogs_small/train'"
]
}
],
Expand Down Expand Up @@ -435,7 +431,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {
"collapsed": true
},
Expand Down Expand Up @@ -675,7 +671,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"현재 우리 모델은 이렇게 생겼습니다. "
"현재 우리 모델은 다음과 같습니다."
]
},
{
Expand Down Expand Up @@ -725,16 +721,29 @@
"In Keras, freezing a network is done by setting its `trainable` attribute to `False`:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pragraph 7\n",
"\n",
"보시다시피 VGG16의 convolutional base에는 14,714,688개의 파라마티거 있습니다. 어마어마하게 많습니다. 여기에 우리가 추가시킬 분류기에 있는 2백만 개의 파라미터가 추가됩니다. 모델을 컴파일하고 학습시키기에 앞서 convolution base를 freeze시키는 것이 중요합니다. 어떤 레이어를 `freezing` 한다는 것은 해당 레이어의 가중치가 학습 도중 업데이트되지 않도록 해주는 것입니다. freezing을 하지 않으면 사전에 학습했던 파라미터들이 수정되고 맙니다. `Dense` 레이어는 앞서 임의로 초기화된 값이기 때문에, 이 레이어를 사전에 학습된 레이어로 역전파시키면 사전에 학습된 특징들(representations)이 손상되고 맙니다. 케라스에서 네트워크를 freezing하는 방법은 `trainable`을 `False`로 세팅하면 됩니다:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is the number of trainable weights before freezing the conv base: 30\n"
"ename": "NameError",
"evalue": "name 'model' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-9-f0048016af66>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m print('This is the number of trainable weights '\n\u001b[0;32m----> 2\u001b[0;31m 'before freezing the conv base:', len(model.trainable_weights))\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'model' is not defined"
]
}
],
Expand Down Expand Up @@ -783,6 +792,16 @@
"Now we can start training our model, with the same data augmentation configuration that we used in our previous example:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"이렇게 설정하면 우리가 추가한 `Dense` 레이어의 가중치만 학습이 진행됩니다. 전체 4개의 가중치 텐서입니다 : 레이어당 두개입니다(하나는 가중치 행렬, 하나는 바이어스 벡터입니다). 새롭게 추가시킨 설정을 적용하려면 우선 모델을 컴파일해야 합니다. 만약 여러분이 컴파일 이후에 가중치의 학습여부 설정을 바꾼다면, 반드시 모델을 다시 컴파일해야 합니다. 그렇지 않으면 변경 사항은 적용되지 않습니다. \n",
"\n",
"자 이제 모델을 학습시킬 준비가 되었습니다. 이전에 사용했던 것과 동일한 data augmentation 을 이용할 것입니다. \n",
"가령 : "
]
},
{
"cell_type": "code",
"execution_count": 13,
Expand Down Expand Up @@ -919,6 +938,13 @@
"Let's plot our results again:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"결과를 다시 한번 출력해보겠습니다."
]
},
{
"cell_type": "code",
"execution_count": 15,
Expand Down Expand Up @@ -975,6 +1001,13 @@
"As you can see, we reach a validation accuracy of about 96%. This is much better than our small convnet trained from scratch."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"보시다시피 검증셋의 정확도 96%를 달성했습니다. 밑바닥부터 학습시킨 작은 컨볼루션 네트워크보다 훨씬 더 좋은 성능입니다!"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down

0 comments on commit 7c4ed71

Please sign in to comment.