Skip to content

Commit 26332a6

Browse files
committed
Fixed data loading pipeline and projection dimension bug
1 parent ffd43b7 commit 26332a6

3 files changed

Lines changed: 55 additions & 16 deletions

File tree

examples/vision/ipynb/object_detection_using_vision_transformer.ipynb

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5+
"id": "39db6022",
56
"metadata": {
67
"colab_type": "text"
78
},
@@ -16,6 +17,7 @@
1617
},
1718
{
1819
"cell_type": "markdown",
20+
"id": "4baf89fb",
1921
"metadata": {
2022
"colab_type": "text"
2123
},
@@ -36,6 +38,7 @@
3638
},
3739
{
3840
"cell_type": "markdown",
41+
"id": "021c1f47",
3942
"metadata": {
4043
"colab_type": "text"
4144
},
@@ -46,6 +49,7 @@
4649
{
4750
"cell_type": "code",
4851
"execution_count": 0,
52+
"id": "1a686205",
4953
"metadata": {
5054
"colab_type": "code"
5155
},
@@ -70,6 +74,7 @@
7074
},
7175
{
7276
"cell_type": "markdown",
77+
"id": "4ded145b",
7378
"metadata": {
7479
"colab_type": "text"
7580
},
@@ -82,6 +87,7 @@
8287
{
8388
"cell_type": "code",
8489
"execution_count": 0,
90+
"id": "37944a37",
8591
"metadata": {
8692
"colab_type": "code"
8793
},
@@ -102,10 +108,10 @@
102108
"\n",
103109
"# Extracting tar files found inside main zip file\n",
104110
"shutil.unpack_archive(\n",
105-
" os.path.join(download_base_dir, \"caltech-101\", \"101_ObjectCategories.tar.gz\"), \".\"\n",
111+
" os.path.join(download_base_dir, \"caltech_101_zipped\", \"caltech-101\", \"101_ObjectCategories.tar.gz\"), \".\"\n",
106112
")\n",
107113
"shutil.unpack_archive(\n",
108-
" os.path.join(download_base_dir, \"caltech-101\", \"Annotations.tar\"), \".\"\n",
114+
" os.path.join(download_base_dir, \"caltech_101_zipped\", \"caltech-101\", \"Annotations.tar\"), \".\"\n",
109115
")\n",
110116
"\n",
111117
"# list of paths to images and annotations\n",
@@ -165,6 +171,7 @@
165171
},
166172
{
167173
"cell_type": "markdown",
174+
"id": "60309cdb",
168175
"metadata": {
169176
"colab_type": "text"
170177
},
@@ -179,6 +186,7 @@
179186
{
180187
"cell_type": "code",
181188
"execution_count": 0,
189+
"id": "5f584e87",
182190
"metadata": {
183191
"colab_type": "code"
184192
},
@@ -193,6 +201,7 @@
193201
},
194202
{
195203
"cell_type": "markdown",
204+
"id": "e8b8d2e1",
196205
"metadata": {
197206
"colab_type": "text"
198207
},
@@ -203,6 +212,7 @@
203212
{
204213
"cell_type": "code",
205214
"execution_count": 0,
215+
"id": "387f4dfe",
206216
"metadata": {
207217
"colab_type": "code"
208218
},
@@ -240,6 +250,7 @@
240250
},
241251
{
242252
"cell_type": "markdown",
253+
"id": "e6b5b2c0",
243254
"metadata": {
244255
"colab_type": "text"
245256
},
@@ -250,6 +261,7 @@
250261
{
251262
"cell_type": "code",
252263
"execution_count": 0,
264+
"id": "ef90f17d",
253265
"metadata": {
254266
"colab_type": "code"
255267
},
@@ -278,6 +290,7 @@
278290
},
279291
{
280292
"cell_type": "markdown",
293+
"id": "bc775a39",
281294
"metadata": {
282295
"colab_type": "text"
283296
},
@@ -292,6 +305,7 @@
292305
{
293306
"cell_type": "code",
294307
"execution_count": 0,
308+
"id": "096d74d8",
295309
"metadata": {
296310
"colab_type": "code"
297311
},
@@ -334,6 +348,7 @@
334348
},
335349
{
336350
"cell_type": "markdown",
351+
"id": "54c4ba4e",
337352
"metadata": {
338353
"colab_type": "text"
339354
},
@@ -352,6 +367,7 @@
352367
{
353368
"cell_type": "code",
354369
"execution_count": 0,
370+
"id": "a61642a3",
355371
"metadata": {
356372
"colab_type": "code"
357373
},
@@ -366,12 +382,13 @@
366382
" transformer_units,\n",
367383
" transformer_layers,\n",
368384
" mlp_head_units,\n",
385+
" hidden_dim,\n",
369386
"):\n",
370387
" inputs = keras.Input(shape=input_shape)\n",
371388
" # Create patches\n",
372389
" patches = Patches(patch_size)(inputs)\n",
373390
" # Encode patches\n",
374-
" encoded_patches = PatchEncoder(num_patches, projection_dim)(patches)\n",
391+
" encoded_patches = PatchEncoder(num_patches, hidden_dim)(patches)\n",
375392
"\n",
376393
" # Create multiple layers of the Transformer block.\n",
377394
" for _ in range(transformer_layers):\n",
@@ -407,6 +424,7 @@
407424
},
408425
{
409426
"cell_type": "markdown",
427+
"id": "43dcd366",
410428
"metadata": {
411429
"colab_type": "text"
412430
},
@@ -417,6 +435,7 @@
417435
{
418436
"cell_type": "code",
419437
"execution_count": 0,
438+
"id": "3751e9ba",
420439
"metadata": {
421440
"colab_type": "code"
422441
},
@@ -461,10 +480,11 @@
461480
"num_patches = (image_size // patch_size) ** 2\n",
462481
"projection_dim = 64\n",
463482
"num_heads = 4\n",
483+
"hidden_dim = projection_dim * num_heads\n",
464484
"# Size of the transformer layers\n",
465485
"transformer_units = [\n",
466-
" projection_dim * 2,\n",
467-
" projection_dim,\n",
486+
" hidden_dim * 2,\n",
487+
" hidden_dim,\n",
468488
"]\n",
469489
"transformer_layers = 4\n",
470490
"mlp_head_units = [2048, 1024, 512, 64, 32] # Size of the dense layers\n",
@@ -482,6 +502,7 @@
482502
" transformer_units,\n",
483503
" transformer_layers,\n",
484504
" mlp_head_units,\n",
505+
" hidden_dim\n",
485506
")\n",
486507
"\n",
487508
"# Train model\n",
@@ -506,6 +527,7 @@
506527
},
507528
{
508529
"cell_type": "markdown",
530+
"id": "3bde0c6b",
509531
"metadata": {
510532
"colab_type": "text"
511533
},
@@ -516,6 +538,7 @@
516538
{
517539
"cell_type": "code",
518540
"execution_count": 0,
541+
"id": "dd6fcf2a",
519542
"metadata": {
520543
"colab_type": "code"
521544
},
@@ -643,6 +666,7 @@
643666
},
644667
{
645668
"cell_type": "markdown",
669+
"id": "72ff599a",
646670
"metadata": {
647671
"colab_type": "text"
648672
},
@@ -655,6 +679,7 @@
655679
},
656680
{
657681
"cell_type": "markdown",
682+
"id": "2d60f09c",
658683
"metadata": {},
659684
"source": [
660685
"## Relevant Chapters from Deep Learning with Python\n",

examples/vision/md/object_detection_using_vision_transformer.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ download_base_dir = os.path.dirname(path_to_downloaded_file)
6868

6969
# Extracting tar files found inside main zip file
7070
shutil.unpack_archive(
71-
os.path.join(download_base_dir, "caltech-101", "101_ObjectCategories.tar.gz"), "."
71+
os.path.join(download_base_dir, "caltech_101_zipped", "caltech-101", "101_ObjectCategories.tar.gz"), "."
7272
)
7373
shutil.unpack_archive(
74-
os.path.join(download_base_dir, "caltech-101", "Annotations.tar"), "."
74+
os.path.join(download_base_dir, "caltech_101_zipped", "caltech-101", "Annotations.tar"), "."
7575
)
7676

7777
# list of paths to images and annotations
@@ -299,12 +299,13 @@ def create_vit_object_detector(
299299
transformer_units,
300300
transformer_layers,
301301
mlp_head_units,
302+
hidden_dim,
302303
):
303304
inputs = keras.Input(shape=input_shape)
304305
# Create patches
305306
patches = Patches(patch_size)(inputs)
306307
# Encode patches
307-
encoded_patches = PatchEncoder(num_patches, projection_dim)(patches)
308+
encoded_patches = PatchEncoder(num_patches, hidden_dim)(patches)
308309

309310
# Create multiple layers of the Transformer block.
310311
for _ in range(transformer_layers):
@@ -384,10 +385,11 @@ num_epochs = 100
384385
num_patches = (image_size // patch_size) ** 2
385386
projection_dim = 64
386387
num_heads = 4
388+
hidden_dim = projection_dim * num_heads
387389
# Size of the transformer layers
388390
transformer_units = [
389-
projection_dim * 2,
390-
projection_dim,
391+
hidden_dim * 2,
392+
hidden_dim,
391393
]
392394
transformer_layers = 4
393395
mlp_head_units = [2048, 1024, 512, 64, 32] # Size of the dense layers
@@ -405,6 +407,7 @@ vit_object_detector = create_vit_object_detector(
405407
transformer_units,
406408
transformer_layers,
407409
mlp_head_units,
410+
hidden_dim,
408411
)
409412

410413
# Train model

examples/vision/object_detection_using_vision_transformer.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,19 @@
6363

6464
# Extracting tar files found inside main zip file
6565
shutil.unpack_archive(
66-
os.path.join(download_base_dir, "caltech-101", "101_ObjectCategories.tar.gz"), "."
66+
os.path.join(
67+
download_base_dir,
68+
"caltech_101_zipped",
69+
"caltech-101",
70+
"101_ObjectCategories.tar.gz",
71+
),
72+
".",
6773
)
6874
shutil.unpack_archive(
69-
os.path.join(download_base_dir, "caltech-101", "Annotations.tar"), "."
75+
os.path.join(
76+
download_base_dir, "caltech_101_zipped", "caltech-101", "Annotations.tar"
77+
),
78+
".",
7079
)
7180

7281
# list of paths to images and annotations
@@ -264,12 +273,13 @@ def create_vit_object_detector(
264273
transformer_units,
265274
transformer_layers,
266275
mlp_head_units,
276+
hidden_dim,
267277
):
268278
inputs = keras.Input(shape=input_shape)
269279
# Create patches
270280
patches = Patches(patch_size)(inputs)
271281
# Encode patches
272-
encoded_patches = PatchEncoder(num_patches, projection_dim)(patches)
282+
encoded_patches = PatchEncoder(num_patches, hidden_dim)(patches)
273283

274284
# Create multiple layers of the Transformer block.
275285
for _ in range(transformer_layers):
@@ -347,10 +357,11 @@ def run_experiment(model, learning_rate, weight_decay, batch_size, num_epochs):
347357
num_patches = (image_size // patch_size) ** 2
348358
projection_dim = 64
349359
num_heads = 4
360+
hidden_dim = projection_dim * num_heads
350361
# Size of the transformer layers
351362
transformer_units = [
352-
projection_dim * 2,
353-
projection_dim,
363+
hidden_dim * 2,
364+
hidden_dim,
354365
]
355366
transformer_layers = 4
356367
mlp_head_units = [2048, 1024, 512, 64, 32] # Size of the dense layers
@@ -368,6 +379,7 @@ def run_experiment(model, learning_rate, weight_decay, batch_size, num_epochs):
368379
transformer_units,
369380
transformer_layers,
370381
mlp_head_units,
382+
hidden_dim,
371383
)
372384

373385
# Train model
@@ -389,7 +401,6 @@ def plot_history(item):
389401

390402
plot_history("loss")
391403

392-
393404
"""
394405
## Evaluate the model
395406
"""

0 commit comments

Comments
 (0)