Upload 7255 files
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- submission.ipynb +584 -0
- test/images/007058.jpg +3 -0
- test/images/007953.jpg +3 -0
- test/images/007997.jpg +3 -0
- test/images/008553.jpg +3 -0
- test/images/008849.jpg +3 -0
- test/images/008867.jpg +3 -0
- test/images/009675.jpg +3 -0
- test/images/010090.jpg +3 -0
- test/images/011397.jpg +3 -0
- test/images/012020.jpg +3 -0
- test/images/012030.jpg +3 -0
- test/images/012804.jpg +3 -0
- test/images/013183.jpg +3 -0
- test/images/013438.jpg +3 -0
- test/images/013676.jpg +3 -0
- test/images/013836.jpg +3 -0
- test/images/014372.jpg +3 -0
- test/images/014868.jpg +3 -0
- test/images/015786.jpg +3 -0
- test/images/016313.jpg +3 -0
- test/images/016840.jpg +3 -0
- test/images/017119.jpg +3 -0
- test/images/017361.jpg +3 -0
- test/images/017733.jpg +3 -0
- test/images/017776.jpg +3 -0
- test/images/018233.jpg +3 -0
- test/images/018530.jpg +3 -0
- test/images/019373.jpg +3 -0
- test/images/019440.jpg +3 -0
- test/images/019600.jpg +3 -0
- test/images/019735.jpg +3 -0
- test/images/020108.jpg +3 -0
- test/images/020144.jpg +3 -0
- test/images/020191.jpg +3 -0
- test/images/020419.jpg +3 -0
- test/images/020478.jpg +3 -0
- test/images/020803.jpg +3 -0
- test/images/020982.jpg +3 -0
- test/images/021228.jpg +3 -0
- test/images/022106.jpg +3 -0
- test/images/023385.jpg +3 -0
- test/images/023428.jpg +3 -0
- test/images/024024.jpg +3 -0
- test/images/024250.jpg +3 -0
- test/images/024446.jpg +3 -0
- test/images/024676.jpg +3 -0
- test/images/025218.jpg +3 -0
- test/images/025319.jpg +3 -0
- test/images/025481.jpg +3 -0
submission.ipynb
ADDED
@@ -0,0 +1,584 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"import pandas as pd\n",
|
10 |
+
"import json\n",
|
11 |
+
"import os\n",
|
12 |
+
"import shutil\n",
|
13 |
+
"import tensorflow as tf\n",
|
14 |
+
"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
|
15 |
+
"from tensorflow.keras.utils import image_dataset_from_directory\n",
|
16 |
+
"from tensorflow.keras.models import Sequential\n",
|
17 |
+
"from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense\n",
|
18 |
+
"from tensorflow.keras.callbacks import Callback"
|
19 |
+
]
|
20 |
+
},
|
21 |
+
{
|
22 |
+
"cell_type": "code",
|
23 |
+
"execution_count": 2,
|
24 |
+
"metadata": {},
|
25 |
+
"outputs": [],
|
26 |
+
"source": [
|
27 |
+
"def create_dataframe(annotations_path):\n",
|
28 |
+
" with open(annotations_path, 'r') as file:\n",
|
29 |
+
" data = json.load(file)\n",
|
30 |
+
"\n",
|
31 |
+
" images = pd.DataFrame(data['images']).rename(columns={'id': 'image_id'})[['image_id', 'file_name']]\n",
|
32 |
+
"\n",
|
33 |
+
" categories = pd.DataFrame(data['categories'])[['id', 'name']]\n",
|
34 |
+
" categories.rename(columns={'id': 'category_id'}, inplace=True)\n",
|
35 |
+
"\n",
|
36 |
+
" usecols = ['image_id', 'category_id']\n",
|
37 |
+
" annotations = pd.DataFrame(data['annotations'])[usecols]\n",
|
38 |
+
"\n",
|
39 |
+
" dataframe = annotations.merge(categories, on='category_id').merge(images, on='image_id')[['file_name', 'name']]\n",
|
40 |
+
" \n",
|
41 |
+
" return dataframe"
|
42 |
+
]
|
43 |
+
},
|
44 |
+
{
|
45 |
+
"cell_type": "code",
|
46 |
+
"execution_count": 3,
|
47 |
+
"metadata": {},
|
48 |
+
"outputs": [],
|
49 |
+
"source": [
|
50 |
+
"def copy_images_to_destination(base_dir, dataframe, split):\n",
|
51 |
+
" images_dir = os.path.join(base_dir, 'images')\n",
|
52 |
+
"\n",
|
53 |
+
" for index, row in dataframe.iterrows():\n",
|
54 |
+
" file_name = row['file_name']\n",
|
55 |
+
" file_class = row['name']\n",
|
56 |
+
"\n",
|
57 |
+
" dest_dir = os.path.join(split, file_class)\n",
|
58 |
+
" os.makedirs(dest_dir, exist_ok=True)\n",
|
59 |
+
"\n",
|
60 |
+
" source_path = os.path.join(images_dir, file_name)\n",
|
61 |
+
" destination_path = os.path.join(dest_dir, file_name)\n",
|
62 |
+
"\n",
|
63 |
+
" shutil.copyfile(source_path, destination_path)\n",
|
64 |
+
"\n",
|
65 |
+
" print(\"Done copying images.\")"
|
66 |
+
]
|
67 |
+
},
|
68 |
+
{
|
69 |
+
"cell_type": "code",
|
70 |
+
"execution_count": 4,
|
71 |
+
"metadata": {},
|
72 |
+
"outputs": [
|
73 |
+
{
|
74 |
+
"data": {
|
75 |
+
"text/html": [
|
76 |
+
"<div>\n",
|
77 |
+
"<style scoped>\n",
|
78 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
79 |
+
" vertical-align: middle;\n",
|
80 |
+
" }\n",
|
81 |
+
"\n",
|
82 |
+
" .dataframe tbody tr th {\n",
|
83 |
+
" vertical-align: top;\n",
|
84 |
+
" }\n",
|
85 |
+
"\n",
|
86 |
+
" .dataframe thead th {\n",
|
87 |
+
" text-align: right;\n",
|
88 |
+
" }\n",
|
89 |
+
"</style>\n",
|
90 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
91 |
+
" <thead>\n",
|
92 |
+
" <tr style=\"text-align: right;\">\n",
|
93 |
+
" <th></th>\n",
|
94 |
+
" <th>file_name</th>\n",
|
95 |
+
" <th>name</th>\n",
|
96 |
+
" </tr>\n",
|
97 |
+
" </thead>\n",
|
98 |
+
" <tbody>\n",
|
99 |
+
" <tr>\n",
|
100 |
+
" <th>0</th>\n",
|
101 |
+
" <td>131094.jpg</td>\n",
|
102 |
+
" <td>soft-cheese</td>\n",
|
103 |
+
" </tr>\n",
|
104 |
+
" <tr>\n",
|
105 |
+
" <th>1</th>\n",
|
106 |
+
" <td>131094.jpg</td>\n",
|
107 |
+
" <td>ham-raw</td>\n",
|
108 |
+
" </tr>\n",
|
109 |
+
" <tr>\n",
|
110 |
+
" <th>2</th>\n",
|
111 |
+
" <td>131094.jpg</td>\n",
|
112 |
+
" <td>hard-cheese</td>\n",
|
113 |
+
" </tr>\n",
|
114 |
+
" <tr>\n",
|
115 |
+
" <th>3</th>\n",
|
116 |
+
" <td>131094.jpg</td>\n",
|
117 |
+
" <td>bread-wholemeal</td>\n",
|
118 |
+
" </tr>\n",
|
119 |
+
" <tr>\n",
|
120 |
+
" <th>4</th>\n",
|
121 |
+
" <td>131094.jpg</td>\n",
|
122 |
+
" <td>cottage-cheese</td>\n",
|
123 |
+
" </tr>\n",
|
124 |
+
" <tr>\n",
|
125 |
+
" <th>...</th>\n",
|
126 |
+
" <td>...</td>\n",
|
127 |
+
" <td>...</td>\n",
|
128 |
+
" </tr>\n",
|
129 |
+
" <tr>\n",
|
130 |
+
" <th>76486</th>\n",
|
131 |
+
" <td>117029.jpg</td>\n",
|
132 |
+
" <td>damson-plum</td>\n",
|
133 |
+
" </tr>\n",
|
134 |
+
" <tr>\n",
|
135 |
+
" <th>76487</th>\n",
|
136 |
+
" <td>117524.jpg</td>\n",
|
137 |
+
" <td>damson-plum</td>\n",
|
138 |
+
" </tr>\n",
|
139 |
+
" <tr>\n",
|
140 |
+
" <th>76488</th>\n",
|
141 |
+
" <td>117849.jpg</td>\n",
|
142 |
+
" <td>damson-plum</td>\n",
|
143 |
+
" </tr>\n",
|
144 |
+
" <tr>\n",
|
145 |
+
" <th>76489</th>\n",
|
146 |
+
" <td>123468.jpg</td>\n",
|
147 |
+
" <td>damson-plum</td>\n",
|
148 |
+
" </tr>\n",
|
149 |
+
" <tr>\n",
|
150 |
+
" <th>76490</th>\n",
|
151 |
+
" <td>095795.jpg</td>\n",
|
152 |
+
" <td>bean-seeds</td>\n",
|
153 |
+
" </tr>\n",
|
154 |
+
" </tbody>\n",
|
155 |
+
"</table>\n",
|
156 |
+
"<p>76491 rows × 2 columns</p>\n",
|
157 |
+
"</div>"
|
158 |
+
],
|
159 |
+
"text/plain": [
|
160 |
+
" file_name name\n",
|
161 |
+
"0 131094.jpg soft-cheese\n",
|
162 |
+
"1 131094.jpg ham-raw\n",
|
163 |
+
"2 131094.jpg hard-cheese\n",
|
164 |
+
"3 131094.jpg bread-wholemeal\n",
|
165 |
+
"4 131094.jpg cottage-cheese\n",
|
166 |
+
"... ... ...\n",
|
167 |
+
"76486 117029.jpg damson-plum\n",
|
168 |
+
"76487 117524.jpg damson-plum\n",
|
169 |
+
"76488 117849.jpg damson-plum\n",
|
170 |
+
"76489 123468.jpg damson-plum\n",
|
171 |
+
"76490 095795.jpg bean-seeds\n",
|
172 |
+
"\n",
|
173 |
+
"[76491 rows x 2 columns]"
|
174 |
+
]
|
175 |
+
},
|
176 |
+
"execution_count": 4,
|
177 |
+
"metadata": {},
|
178 |
+
"output_type": "execute_result"
|
179 |
+
}
|
180 |
+
],
|
181 |
+
"source": [
|
182 |
+
"train_df = create_dataframe('train/annotations.json')\n",
|
183 |
+
"train_df"
|
184 |
+
]
|
185 |
+
},
|
186 |
+
{
|
187 |
+
"cell_type": "code",
|
188 |
+
"execution_count": 5,
|
189 |
+
"metadata": {},
|
190 |
+
"outputs": [],
|
191 |
+
"source": [
|
192 |
+
"splits = ['train', 'val']\n",
|
193 |
+
"\n",
|
194 |
+
"for split in splits:\n",
|
195 |
+
" root = f'{split}'\n",
|
196 |
+
"\n",
|
197 |
+
" for index, row in train_df.iterrows():\n",
|
198 |
+
" directory_name = row['name']\n",
|
199 |
+
" directory_path = os.path.join(root, directory_name)\n",
|
200 |
+
"\n",
|
201 |
+
" if not os.path.exists(directory_path):\n",
|
202 |
+
" os.makedirs(directory_path)"
|
203 |
+
]
|
204 |
+
},
|
205 |
+
{
|
206 |
+
"cell_type": "code",
|
207 |
+
"execution_count": 6,
|
208 |
+
"metadata": {},
|
209 |
+
"outputs": [
|
210 |
+
{
|
211 |
+
"data": {
|
212 |
+
"text/html": [
|
213 |
+
"<div>\n",
|
214 |
+
"<style scoped>\n",
|
215 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
216 |
+
" vertical-align: middle;\n",
|
217 |
+
" }\n",
|
218 |
+
"\n",
|
219 |
+
" .dataframe tbody tr th {\n",
|
220 |
+
" vertical-align: top;\n",
|
221 |
+
" }\n",
|
222 |
+
"\n",
|
223 |
+
" .dataframe thead th {\n",
|
224 |
+
" text-align: right;\n",
|
225 |
+
" }\n",
|
226 |
+
"</style>\n",
|
227 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
228 |
+
" <thead>\n",
|
229 |
+
" <tr style=\"text-align: right;\">\n",
|
230 |
+
" <th></th>\n",
|
231 |
+
" <th>file_name</th>\n",
|
232 |
+
" <th>name</th>\n",
|
233 |
+
" </tr>\n",
|
234 |
+
" </thead>\n",
|
235 |
+
" <tbody>\n",
|
236 |
+
" <tr>\n",
|
237 |
+
" <th>0</th>\n",
|
238 |
+
" <td>149022.jpg</td>\n",
|
239 |
+
" <td>espresso-with-caffeine</td>\n",
|
240 |
+
" </tr>\n",
|
241 |
+
" <tr>\n",
|
242 |
+
" <th>1</th>\n",
|
243 |
+
" <td>149022.jpg</td>\n",
|
244 |
+
" <td>dark-chocolate</td>\n",
|
245 |
+
" </tr>\n",
|
246 |
+
" <tr>\n",
|
247 |
+
" <th>2</th>\n",
|
248 |
+
" <td>167905.jpg</td>\n",
|
249 |
+
" <td>espresso-with-caffeine</td>\n",
|
250 |
+
" </tr>\n",
|
251 |
+
" <tr>\n",
|
252 |
+
" <th>3</th>\n",
|
253 |
+
" <td>121313.jpg</td>\n",
|
254 |
+
" <td>espresso-with-caffeine</td>\n",
|
255 |
+
" </tr>\n",
|
256 |
+
" <tr>\n",
|
257 |
+
" <th>4</th>\n",
|
258 |
+
" <td>153429.jpg</td>\n",
|
259 |
+
" <td>espresso-with-caffeine</td>\n",
|
260 |
+
" </tr>\n",
|
261 |
+
" <tr>\n",
|
262 |
+
" <th>...</th>\n",
|
263 |
+
" <td>...</td>\n",
|
264 |
+
" <td>...</td>\n",
|
265 |
+
" </tr>\n",
|
266 |
+
" <tr>\n",
|
267 |
+
" <th>1825</th>\n",
|
268 |
+
" <td>144675.jpg</td>\n",
|
269 |
+
" <td>oat-milk</td>\n",
|
270 |
+
" </tr>\n",
|
271 |
+
" <tr>\n",
|
272 |
+
" <th>1826</th>\n",
|
273 |
+
" <td>103273.jpg</td>\n",
|
274 |
+
" <td>soup-potato</td>\n",
|
275 |
+
" </tr>\n",
|
276 |
+
" <tr>\n",
|
277 |
+
" <th>1827</th>\n",
|
278 |
+
" <td>159922.jpg</td>\n",
|
279 |
+
" <td>red-cabbage</td>\n",
|
280 |
+
" </tr>\n",
|
281 |
+
" <tr>\n",
|
282 |
+
" <th>1828</th>\n",
|
283 |
+
" <td>011275.jpg</td>\n",
|
284 |
+
" <td>pasta-in-conch-form</td>\n",
|
285 |
+
" </tr>\n",
|
286 |
+
" <tr>\n",
|
287 |
+
" <th>1829</th>\n",
|
288 |
+
" <td>166537.jpg</td>\n",
|
289 |
+
" <td>chocolate</td>\n",
|
290 |
+
" </tr>\n",
|
291 |
+
" </tbody>\n",
|
292 |
+
"</table>\n",
|
293 |
+
"<p>1830 rows × 2 columns</p>\n",
|
294 |
+
"</div>"
|
295 |
+
],
|
296 |
+
"text/plain": [
|
297 |
+
" file_name name\n",
|
298 |
+
"0 149022.jpg espresso-with-caffeine\n",
|
299 |
+
"1 149022.jpg dark-chocolate\n",
|
300 |
+
"2 167905.jpg espresso-with-caffeine\n",
|
301 |
+
"3 121313.jpg espresso-with-caffeine\n",
|
302 |
+
"4 153429.jpg espresso-with-caffeine\n",
|
303 |
+
"... ... ...\n",
|
304 |
+
"1825 144675.jpg oat-milk\n",
|
305 |
+
"1826 103273.jpg soup-potato\n",
|
306 |
+
"1827 159922.jpg red-cabbage\n",
|
307 |
+
"1828 011275.jpg pasta-in-conch-form\n",
|
308 |
+
"1829 166537.jpg chocolate\n",
|
309 |
+
"\n",
|
310 |
+
"[1830 rows x 2 columns]"
|
311 |
+
]
|
312 |
+
},
|
313 |
+
"execution_count": 6,
|
314 |
+
"metadata": {},
|
315 |
+
"output_type": "execute_result"
|
316 |
+
}
|
317 |
+
],
|
318 |
+
"source": [
|
319 |
+
"val_df = create_dataframe('val/annotations.json')\n",
|
320 |
+
"val_df"
|
321 |
+
]
|
322 |
+
},
|
323 |
+
{
|
324 |
+
"cell_type": "code",
|
325 |
+
"execution_count": 7,
|
326 |
+
"metadata": {},
|
327 |
+
"outputs": [
|
328 |
+
{
|
329 |
+
"name": "stdout",
|
330 |
+
"output_type": "stream",
|
331 |
+
"text": [
|
332 |
+
"Done copying images.\n"
|
333 |
+
]
|
334 |
+
}
|
335 |
+
],
|
336 |
+
"source": [
|
337 |
+
"base_dir = 'train'\n",
|
338 |
+
"dataframe = train_df\n",
|
339 |
+
"copy_images_to_destination(base_dir, dataframe, 'train')"
|
340 |
+
]
|
341 |
+
},
|
342 |
+
{
|
343 |
+
"cell_type": "code",
|
344 |
+
"execution_count": 8,
|
345 |
+
"metadata": {},
|
346 |
+
"outputs": [
|
347 |
+
{
|
348 |
+
"name": "stdout",
|
349 |
+
"output_type": "stream",
|
350 |
+
"text": [
|
351 |
+
"Done copying images.\n"
|
352 |
+
]
|
353 |
+
}
|
354 |
+
],
|
355 |
+
"source": [
|
356 |
+
"base_dir = 'val'\n",
|
357 |
+
"dataframe = val_df\n",
|
358 |
+
"copy_images_to_destination(base_dir, dataframe, 'val')"
|
359 |
+
]
|
360 |
+
},
|
361 |
+
{
|
362 |
+
"cell_type": "code",
|
363 |
+
"execution_count": 2,
|
364 |
+
"metadata": {},
|
365 |
+
"outputs": [
|
366 |
+
{
|
367 |
+
"name": "stdout",
|
368 |
+
"output_type": "stream",
|
369 |
+
"text": [
|
370 |
+
"Found 70397 files belonging to 498 classes.\n",
|
371 |
+
"Found 1799 files belonging to 498 classes.\n"
|
372 |
+
]
|
373 |
+
}
|
374 |
+
],
|
375 |
+
"source": [
|
376 |
+
"train = image_dataset_from_directory(\n",
|
377 |
+
" directory='train',\n",
|
378 |
+
" label_mode='categorical',\n",
|
379 |
+
" batch_size=32,\n",
|
380 |
+
" image_size=(299, 299)\n",
|
381 |
+
")\n",
|
382 |
+
"\n",
|
383 |
+
"val = image_dataset_from_directory(\n",
|
384 |
+
" directory='val',\n",
|
385 |
+
" label_mode='categorical',\n",
|
386 |
+
" batch_size=32,\n",
|
387 |
+
" image_size=(299, 299)\n",
|
388 |
+
")"
|
389 |
+
]
|
390 |
+
},
|
391 |
+
{
|
392 |
+
"cell_type": "code",
|
393 |
+
"execution_count": 3,
|
394 |
+
"metadata": {},
|
395 |
+
"outputs": [],
|
396 |
+
"source": [
|
397 |
+
"train_datagen = ImageDataGenerator(\n",
|
398 |
+
" rescale=1./255,\n",
|
399 |
+
" shear_range=0.2,\n",
|
400 |
+
" zoom_range=0.2,\n",
|
401 |
+
" horizontal_flip=True\n",
|
402 |
+
")\n",
|
403 |
+
"\n",
|
404 |
+
"val_datagen = ImageDataGenerator(rescale=1./255)"
|
405 |
+
]
|
406 |
+
},
|
407 |
+
{
|
408 |
+
"cell_type": "code",
|
409 |
+
"execution_count": 4,
|
410 |
+
"metadata": {},
|
411 |
+
"outputs": [],
|
412 |
+
"source": [
|
413 |
+
"class MyCallback(Callback):\n",
|
414 |
+
" def on_epoch_end(self, epoch, logs={}):\n",
|
415 |
+
" if logs.get('val_categorical_accuracy') >= 0.81:\n",
|
416 |
+
" print('Validation accuracy reached 81%. Stopping training.')"
|
417 |
+
]
|
418 |
+
},
|
419 |
+
{
|
420 |
+
"cell_type": "code",
|
421 |
+
"execution_count": 5,
|
422 |
+
"metadata": {},
|
423 |
+
"outputs": [
|
424 |
+
{
|
425 |
+
"name": "stdout",
|
426 |
+
"output_type": "stream",
|
427 |
+
"text": [
|
428 |
+
"Model: \"sequential\"\n",
|
429 |
+
"_________________________________________________________________\n",
|
430 |
+
" Layer (type) Output Shape Param # \n",
|
431 |
+
"=================================================================\n",
|
432 |
+
" conv2d (Conv2D) (None, 297, 297, 32) 896 \n",
|
433 |
+
" \n",
|
434 |
+
" max_pooling2d (MaxPooling2D (None, 148, 148, 32) 0 \n",
|
435 |
+
" ) \n",
|
436 |
+
" \n",
|
437 |
+
" conv2d_1 (Conv2D) (None, 146, 146, 64) 18496 \n",
|
438 |
+
" \n",
|
439 |
+
" max_pooling2d_1 (MaxPooling (None, 73, 73, 64) 0 \n",
|
440 |
+
" 2D) \n",
|
441 |
+
" \n",
|
442 |
+
" conv2d_2 (Conv2D) (None, 71, 71, 128) 73856 \n",
|
443 |
+
" \n",
|
444 |
+
" max_pooling2d_2 (MaxPooling (None, 35, 35, 128) 0 \n",
|
445 |
+
" 2D) \n",
|
446 |
+
" \n",
|
447 |
+
" flatten (Flatten) (None, 156800) 0 \n",
|
448 |
+
" \n",
|
449 |
+
" dense (Dense) (None, 128) 20070528 \n",
|
450 |
+
" \n",
|
451 |
+
" dense_1 (Dense) (None, 498) 64242 \n",
|
452 |
+
" \n",
|
453 |
+
"=================================================================\n",
|
454 |
+
"Total params: 20,228,018\n",
|
455 |
+
"Trainable params: 20,228,018\n",
|
456 |
+
"Non-trainable params: 0\n",
|
457 |
+
"_________________________________________________________________\n"
|
458 |
+
]
|
459 |
+
}
|
460 |
+
],
|
461 |
+
"source": [
|
462 |
+
"model = Sequential()\n",
|
463 |
+
"model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(299, 299, 3)))\n",
|
464 |
+
"model.add(MaxPooling2D((2, 2)))\n",
|
465 |
+
"model.add(Conv2D(64, (3, 3), activation='relu'))\n",
|
466 |
+
"model.add(MaxPooling2D((2, 2)))\n",
|
467 |
+
"model.add(Conv2D(128, (3, 3), activation='relu'))\n",
|
468 |
+
"model.add(MaxPooling2D((2, 2)))\n",
|
469 |
+
"model.add(Flatten())\n",
|
470 |
+
"model.add(Dense(128, activation='relu'))\n",
|
471 |
+
"model.add(Dense(498, activation='softmax'))\n",
|
472 |
+
"\n",
|
473 |
+
"model.summary()\n",
|
474 |
+
"\n",
|
475 |
+
"model.compile(optimizer=tf.keras.optimizers.Adam(),\n",
|
476 |
+
" loss=tf.keras.losses.CategoricalCrossentropy(),\n",
|
477 |
+
" metrics=[tf.keras.metrics.CategoricalAccuracy()])"
|
478 |
+
]
|
479 |
+
},
|
480 |
+
{
|
481 |
+
"cell_type": "code",
|
482 |
+
"execution_count": 6,
|
483 |
+
"metadata": {},
|
484 |
+
"outputs": [
|
485 |
+
{
|
486 |
+
"name": "stdout",
|
487 |
+
"output_type": "stream",
|
488 |
+
"text": [
|
489 |
+
"Epoch 1/32\n",
|
490 |
+
" 6/2200 [..............................] - ETA: 6:25 - loss: 504.5968 - categorical_accuracy: 0.0052 WARNING:tensorflow:Callback method `on_train_batch_end` is slow compared to the batch time (batch time: 0.0608s vs `on_train_batch_end` time: 0.0957s). Check your callbacks.\n",
|
491 |
+
"2200/2200 [==============================] - 291s 130ms/step - loss: 6.9090 - categorical_accuracy: 0.0398 - val_loss: 5.5961 - val_categorical_accuracy: 0.0411\n",
|
492 |
+
"Epoch 2/32\n",
|
493 |
+
"2200/2200 [==============================] - 279s 127ms/step - loss: 5.4654 - categorical_accuracy: 0.0420 - val_loss: 5.5951 - val_categorical_accuracy: 0.0417\n",
|
494 |
+
"Epoch 3/32\n",
|
495 |
+
"2200/2200 [==============================] - 276s 125ms/step - loss: 5.4428 - categorical_accuracy: 0.0449 - val_loss: 5.6058 - val_categorical_accuracy: 0.0417\n",
|
496 |
+
"Epoch 4/32\n",
|
497 |
+
"2200/2200 [==============================] - 285s 130ms/step - loss: 5.3952 - categorical_accuracy: 0.0528 - val_loss: 5.6658 - val_categorical_accuracy: 0.0411\n",
|
498 |
+
"Epoch 5/32\n",
|
499 |
+
"2200/2200 [==============================] - 282s 128ms/step - loss: 5.3362 - categorical_accuracy: 0.0630 - val_loss: 5.7703 - val_categorical_accuracy: 0.0406\n",
|
500 |
+
"Epoch 6/32\n",
|
501 |
+
"2200/2200 [==============================] - 326s 148ms/step - loss: 5.2673 - categorical_accuracy: 0.0755 - val_loss: 5.7254 - val_categorical_accuracy: 0.0411\n",
|
502 |
+
"Epoch 7/32\n",
|
503 |
+
"2200/2200 [==============================] - 300s 136ms/step - loss: 5.2040 - categorical_accuracy: 0.0875 - val_loss: 5.8228 - val_categorical_accuracy: 0.0411\n",
|
504 |
+
"Epoch 8/32\n",
|
505 |
+
"2200/2200 [==============================] - 382s 174ms/step - loss: 5.1794 - categorical_accuracy: 0.0927 - val_loss: 6.0131 - val_categorical_accuracy: 0.0411\n",
|
506 |
+
"Epoch 9/32\n",
|
507 |
+
"2200/2200 [==============================] - 372s 169ms/step - loss: 5.1426 - categorical_accuracy: 0.0984 - val_loss: 6.0550 - val_categorical_accuracy: 0.0406\n",
|
508 |
+
"Epoch 10/32\n",
|
509 |
+
"2200/2200 [==============================] - 335s 152ms/step - loss: 5.0958 - categorical_accuracy: 0.1058 - val_loss: 6.3628 - val_categorical_accuracy: 0.0389\n",
|
510 |
+
"Epoch 11/32\n",
|
511 |
+
"2200/2200 [==============================] - 354s 161ms/step - loss: 5.0727 - categorical_accuracy: 0.1111 - val_loss: 6.4603 - val_categorical_accuracy: 0.0378\n",
|
512 |
+
"Epoch 12/32\n",
|
513 |
+
"2200/2200 [==============================] - 356s 162ms/step - loss: 5.0326 - categorical_accuracy: 0.1166 - val_loss: 6.7461 - val_categorical_accuracy: 0.0417\n",
|
514 |
+
"Epoch 13/32\n",
|
515 |
+
"2200/2200 [==============================] - 354s 161ms/step - loss: 5.0137 - categorical_accuracy: 0.1208 - val_loss: 6.9263 - val_categorical_accuracy: 0.0395\n",
|
516 |
+
"Epoch 14/32\n",
|
517 |
+
"2200/2200 [==============================] - 349s 159ms/step - loss: 4.9708 - categorical_accuracy: 0.1281 - val_loss: 6.9836 - val_categorical_accuracy: 0.0378\n",
|
518 |
+
"Epoch 15/32\n",
|
519 |
+
"2200/2200 [==============================] - 368s 167ms/step - loss: 4.9531 - categorical_accuracy: 0.1318 - val_loss: 6.6221 - val_categorical_accuracy: 0.0384\n",
|
520 |
+
"Epoch 16/32\n",
|
521 |
+
"2200/2200 [==============================] - 360s 164ms/step - loss: 4.9288 - categorical_accuracy: 0.1357 - val_loss: 6.6952 - val_categorical_accuracy: 0.0378\n",
|
522 |
+
"Epoch 17/32\n",
|
523 |
+
"2200/2200 [==============================] - 359s 163ms/step - loss: 4.8955 - categorical_accuracy: 0.1403 - val_loss: 6.6760 - val_categorical_accuracy: 0.0400\n",
|
524 |
+
"Epoch 18/32\n",
|
525 |
+
"2200/2200 [==============================] - 354s 161ms/step - loss: 4.8613 - categorical_accuracy: 0.1455 - val_loss: 7.7695 - val_categorical_accuracy: 0.0384\n",
|
526 |
+
"Epoch 19/32\n",
|
527 |
+
"2200/2200 [==============================] - 327s 148ms/step - loss: 4.8498 - categorical_accuracy: 0.1494 - val_loss: 7.5958 - val_categorical_accuracy: 0.0361\n",
|
528 |
+
"Epoch 20/32\n",
|
529 |
+
"2200/2200 [==============================] - 362s 165ms/step - loss: 4.7999 - categorical_accuracy: 0.1556 - val_loss: 7.8458 - val_categorical_accuracy: 0.0372\n",
|
530 |
+
"Epoch 21/32\n",
|
531 |
+
"2200/2200 [==============================] - 361s 164ms/step - loss: 4.7786 - categorical_accuracy: 0.1594 - val_loss: 8.5637 - val_categorical_accuracy: 0.0389\n",
|
532 |
+
"Epoch 22/32\n",
|
533 |
+
"2200/2200 [==============================] - 360s 164ms/step - loss: 4.7561 - categorical_accuracy: 0.1645 - val_loss: 8.0804 - val_categorical_accuracy: 0.0384\n",
|
534 |
+
"Epoch 23/32\n",
|
535 |
+
"2200/2200 [==============================] - 301s 137ms/step - loss: 4.7279 - categorical_accuracy: 0.1694 - val_loss: 8.9041 - val_categorical_accuracy: 0.0372\n",
|
536 |
+
"Epoch 24/32\n",
|
537 |
+
"2200/2200 [==============================] - 310s 140ms/step - loss: 4.6962 - categorical_accuracy: 0.1732 - val_loss: 9.0381 - val_categorical_accuracy: 0.0361\n",
|
538 |
+
"Epoch 25/32\n",
|
539 |
+
"2200/2200 [==============================] - 314s 142ms/step - loss: 4.6756 - categorical_accuracy: 0.1769 - val_loss: 8.6350 - val_categorical_accuracy: 0.0378\n",
|
540 |
+
"Epoch 26/32\n",
|
541 |
+
"2200/2200 [==============================] - 296s 134ms/step - loss: 4.6531 - categorical_accuracy: 0.1820 - val_loss: 9.3287 - val_categorical_accuracy: 0.0367\n",
|
542 |
+
"Epoch 27/32\n",
|
543 |
+
"2200/2200 [==============================] - 282s 128ms/step - loss: 4.6207 - categorical_accuracy: 0.1875 - val_loss: 9.8095 - val_categorical_accuracy: 0.0361\n",
|
544 |
+
"Epoch 28/32\n",
|
545 |
+
"2200/2200 [==============================] - 349s 158ms/step - loss: 4.6045 - categorical_accuracy: 0.1904 - val_loss: 9.4419 - val_categorical_accuracy: 0.0378\n",
|
546 |
+
"Epoch 29/32\n",
|
547 |
+
"2200/2200 [==============================] - 326s 148ms/step - loss: 4.5832 - categorical_accuracy: 0.1945 - val_loss: 9.4719 - val_categorical_accuracy: 0.0361\n",
|
548 |
+
"Epoch 30/32\n",
|
549 |
+
"2200/2200 [==============================] - 361s 164ms/step - loss: 4.5393 - categorical_accuracy: 0.2010 - val_loss: 9.8935 - val_categorical_accuracy: 0.0395\n",
|
550 |
+
"Epoch 31/32\n",
|
551 |
+
"2200/2200 [==============================] - 334s 152ms/step - loss: 4.5176 - categorical_accuracy: 0.2052 - val_loss: 9.9011 - val_categorical_accuracy: 0.0378\n",
|
552 |
+
"Epoch 32/32\n",
|
553 |
+
"2200/2200 [==============================] - 344s 156ms/step - loss: 4.4989 - categorical_accuracy: 0.2082 - val_loss: 10.2300 - val_categorical_accuracy: 0.0378\n"
|
554 |
+
]
|
555 |
+
}
|
556 |
+
],
|
557 |
+
"source": [
|
558 |
+
"callback = MyCallback()\n",
|
559 |
+
"history = model.fit(train, epochs=32, validation_data=val, callbacks=[callback])"
|
560 |
+
]
|
561 |
+
}
|
562 |
+
],
|
563 |
+
"metadata": {
|
564 |
+
"kernelspec": {
|
565 |
+
"display_name": "gpu",
|
566 |
+
"language": "python",
|
567 |
+
"name": "python3"
|
568 |
+
},
|
569 |
+
"language_info": {
|
570 |
+
"codemirror_mode": {
|
571 |
+
"name": "ipython",
|
572 |
+
"version": 3
|
573 |
+
},
|
574 |
+
"file_extension": ".py",
|
575 |
+
"mimetype": "text/x-python",
|
576 |
+
"name": "python",
|
577 |
+
"nbconvert_exporter": "python",
|
578 |
+
"pygments_lexer": "ipython3",
|
579 |
+
"version": "3.9.18"
|
580 |
+
}
|
581 |
+
},
|
582 |
+
"nbformat": 4,
|
583 |
+
"nbformat_minor": 2
|
584 |
+
}
|
test/images/007058.jpg
ADDED
Git LFS Details
|
test/images/007953.jpg
ADDED
Git LFS Details
|
test/images/007997.jpg
ADDED
Git LFS Details
|
test/images/008553.jpg
ADDED
Git LFS Details
|
test/images/008849.jpg
ADDED
Git LFS Details
|
test/images/008867.jpg
ADDED
Git LFS Details
|
test/images/009675.jpg
ADDED
Git LFS Details
|
test/images/010090.jpg
ADDED
Git LFS Details
|
test/images/011397.jpg
ADDED
Git LFS Details
|
test/images/012020.jpg
ADDED
Git LFS Details
|
test/images/012030.jpg
ADDED
Git LFS Details
|
test/images/012804.jpg
ADDED
Git LFS Details
|
test/images/013183.jpg
ADDED
Git LFS Details
|
test/images/013438.jpg
ADDED
Git LFS Details
|
test/images/013676.jpg
ADDED
Git LFS Details
|
test/images/013836.jpg
ADDED
Git LFS Details
|
test/images/014372.jpg
ADDED
Git LFS Details
|
test/images/014868.jpg
ADDED
Git LFS Details
|
test/images/015786.jpg
ADDED
Git LFS Details
|
test/images/016313.jpg
ADDED
Git LFS Details
|
test/images/016840.jpg
ADDED
Git LFS Details
|
test/images/017119.jpg
ADDED
Git LFS Details
|
test/images/017361.jpg
ADDED
Git LFS Details
|
test/images/017733.jpg
ADDED
Git LFS Details
|
test/images/017776.jpg
ADDED
Git LFS Details
|
test/images/018233.jpg
ADDED
Git LFS Details
|
test/images/018530.jpg
ADDED
Git LFS Details
|
test/images/019373.jpg
ADDED
Git LFS Details
|
test/images/019440.jpg
ADDED
Git LFS Details
|
test/images/019600.jpg
ADDED
Git LFS Details
|
test/images/019735.jpg
ADDED
Git LFS Details
|
test/images/020108.jpg
ADDED
Git LFS Details
|
test/images/020144.jpg
ADDED
Git LFS Details
|
test/images/020191.jpg
ADDED
Git LFS Details
|
test/images/020419.jpg
ADDED
Git LFS Details
|
test/images/020478.jpg
ADDED
Git LFS Details
|
test/images/020803.jpg
ADDED
Git LFS Details
|
test/images/020982.jpg
ADDED
Git LFS Details
|
test/images/021228.jpg
ADDED
Git LFS Details
|
test/images/022106.jpg
ADDED
Git LFS Details
|
test/images/023385.jpg
ADDED
Git LFS Details
|
test/images/023428.jpg
ADDED
Git LFS Details
|
test/images/024024.jpg
ADDED
Git LFS Details
|
test/images/024250.jpg
ADDED
Git LFS Details
|
test/images/024446.jpg
ADDED
Git LFS Details
|
test/images/024676.jpg
ADDED
Git LFS Details
|
test/images/025218.jpg
ADDED
Git LFS Details
|
test/images/025319.jpg
ADDED
Git LFS Details
|
test/images/025481.jpg
ADDED
Git LFS Details
|