Skip to content

Commit 91e3525

Browse files
committed
Update ch9
1 parent 1d8db23 commit 91e3525

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

chapter09_part02_modern-convnet-architecture-patterns.ipynb

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,74 @@
150150
"### Putting it together: a mini Xception-like model"
151151
]
152152
},
153+
{
154+
"cell_type": "code",
155+
"execution_count": 0,
156+
"metadata": {
157+
"colab_type": "code"
158+
},
159+
"outputs": [],
160+
"source": [
161+
"from google.colab import files\n",
162+
"files.upload()"
163+
]
164+
},
165+
{
166+
"cell_type": "code",
167+
"execution_count": 0,
168+
"metadata": {
169+
"colab_type": "code"
170+
},
171+
"outputs": [],
172+
"source": [
173+
"!mkdir ~/.kaggle\n",
174+
"!cp kaggle.json ~/.kaggle/\n",
175+
"!chmod 600 ~/.kaggle/kaggle.json\n",
176+
"!kaggle competitions download -c dogs-vs-cats\n",
177+
"!unzip -qq train.zip"
178+
]
179+
},
180+
{
181+
"cell_type": "code",
182+
"execution_count": 0,
183+
"metadata": {
184+
"colab_type": "code"
185+
},
186+
"outputs": [],
187+
"source": [
188+
"import os, shutil, pathlib\n",
189+
"from tensorflow.keras.preprocessing import image_dataset_from_directory\n",
190+
"\n",
191+
"original_dir = pathlib.Path(\"train\")\n",
192+
"new_base_dir = pathlib.Path(\"cats_vs_dogs_small\")\n",
193+
"\n",
194+
"def make_subset(subset_name, start_index, end_index):\n",
195+
" for category in (\"cat\", \"dog\"):\n",
196+
" dir = new_base_dir / subset_name / category\n",
197+
" os.makedirs(dir)\n",
198+
" fnames = [f\"{category}.{i}.jpg\" for i in range(start_index, end_index)]\n",
199+
" for fname in fnames:\n",
200+
" shutil.copyfile(src=original_dir / fname,\n",
201+
" dst=dir / fname)\n",
202+
"\n",
203+
"make_subset(\"train\", start_index=0, end_index=1000)\n",
204+
"make_subset(\"validation\", start_index=1000, end_index=1500)\n",
205+
"make_subset(\"test\", start_index=1500, end_index=2500)\n",
206+
"\n",
207+
"train_dataset = image_dataset_from_directory(\n",
208+
" new_base_dir / \"train\",\n",
209+
" image_size=(180, 180),\n",
210+
" batch_size=32)\n",
211+
"validation_dataset = image_dataset_from_directory(\n",
212+
" new_base_dir / \"validation\",\n",
213+
" image_size=(180, 180),\n",
214+
" batch_size=32)\n",
215+
"test_dataset = image_dataset_from_directory(\n",
216+
" new_base_dir / \"test\",\n",
217+
" image_size=(180, 180),\n",
218+
" batch_size=32)"
219+
]
220+
},
153221
{
154222
"cell_type": "code",
155223
"execution_count": 0,
@@ -203,6 +271,23 @@
203271
"outputs = layers.Dense(1, activation=\"sigmoid\")(x)\n",
204272
"model = keras.Model(inputs=inputs, outputs=outputs)"
205273
]
274+
},
275+
{
276+
"cell_type": "code",
277+
"execution_count": 0,
278+
"metadata": {
279+
"colab_type": "code"
280+
},
281+
"outputs": [],
282+
"source": [
283+
"model.compile(loss=\"binary_crossentropy\",\n",
284+
" optimizer=\"rmsprop\",\n",
285+
" metrics=[\"accuracy\"])\n",
286+
"history = model.fit(\n",
287+
" train_dataset,\n",
288+
" epochs=100,\n",
289+
" validation_data=validation_dataset)"
290+
]
206291
}
207292
],
208293
"metadata": {

0 commit comments

Comments
 (0)