18
18
"metadata" : {},
19
19
"outputs" : [],
20
20
"source" : [
21
+ " import itertools\n " ,
22
+ " import json\n " ,
23
+ " import math\n " ,
21
24
" import os\n " ,
25
+ " import random\n " ,
26
+ " import tarfile\n " ,
22
27
" import time\n " ,
28
+ " import urllib.request\n " ,
29
+ " import zipfile\n " ,
30
+ " from shutil import copyfile\n " ,
31
+ " \n " ,
23
32
" import numpy as np\n " ,
24
- " from PIL import Image\n " ,
25
- " from torch.utils.data.dataset import Dataset\n " ,
26
- " from tqdm import tqdm\n " ,
27
- " from torchvision import transforms\n " ,
28
- " from torchvision import models\n " ,
33
+ " import requests\n " ,
29
34
" import torch\n " ,
30
- " from torch.utils.tensorboard import SummaryWriter\n " ,
31
- " from sklearn.metrics import precision_score, recall_score, f1_score\n " ,
32
- " from torch import nn\n " ,
33
- " from torch.utils.data.dataloader import DataLoader\n " ,
35
+ " from PIL import Image\n " ,
34
36
" from matplotlib import pyplot as plt\n " ,
35
37
" from numpy import printoptions\n " ,
36
- " import requests\n " ,
37
- " import tarfile\n " ,
38
- " import random\n " ,
39
- " import json\n " ,
40
- " from shutil import copyfile\n " ,
41
- " import zipfile\n " ,
42
38
" from sklearn.manifold import TSNE\n " ,
43
- " import itertools\n " ,
39
+ " from sklearn.metrics import precision_score, recall_score, f1_score\n " ,
40
+ " from torch import nn\n " ,
44
41
" from torch.nn import Parameter\n " ,
45
- " import math\n " ,
46
- " import urllib.request"
42
+ " from torch.utils.data.dataloader import DataLoader\n " ,
43
+ " from torch.utils.data.dataset import Dataset\n " ,
44
+ " from torch.utils.tensorboard import SummaryWriter\n " ,
45
+ " from torchvision import models\n " ,
46
+ " from torchvision import transforms\n " ,
47
+ " from tqdm import tqdm"
47
48
]
48
49
},
49
50
{
102
103
" with tarfile.open(path_to_tar_file) as tar_ref:\n " ,
103
104
" tar_ref.extractall(os.path.dirname(img_folder))\n " ,
104
105
" os.remove(path_to_tar_file)\n " ,
105
- " # Also, copy our pre-processed annotations to the dataset folder. \n " ,
106
- " # Note: you can find script for generating such annotations in attachments\n " ,
107
- " copyfile('../PyTorch-Multi-Label-Image-Classification:-Image-Tagging/nus_wide/small_test.json', os.path.join(img_folder, 'small_test.json'))\n " ,
108
- " copyfile('../PyTorch-Multi-Label-Image-Classification:-Image-Tagging/nus_wide/small_train.json', os.path.join(img_folder, 'small_train.json'))"
106
+ " # Also, copy our pre-processed annotations to the dataset folder.\n " ,
107
+ " copyfile('../PyTorch-Multi-Label-Image-Classification-Image-Tagging/nus_wide/small_test.json', os.path.join(img_folder, 'small_test.json'))\n " ,
108
+ " copyfile('../PyTorch-Multi-Label-Image-Classification-Image-Tagging/nus_wide/small_train.json', os.path.join(img_folder, 'small_train.json'))"
109
109
]
110
110
},
111
111
{
116
116
},
117
117
"outputs" : [],
118
118
"source" : [
119
- " # Download Glove model trained on wikipedia.\n " ,
120
119
" # We want to represent our label names as vectors in order to use them as features further.\n " ,
121
120
" # To do that we decided to use GloVe model (https://nlp.stanford.edu/projects/glove/).\n " ,
122
121
" # Let's download GloVe model trained on a Wikipedia Text Corpus.\n " ,
162
161
" 'nighttime', 'boats', 'mountain', 'tree', 'snow', 'beach', 'vehicle', 'rocks',\n " ,
163
162
" 'reflection', 'sunset', 'road', 'flowers', 'ocean', 'lake', 'window', 'plants',\n " ,
164
163
" 'buildings', 'grass', 'water', 'animal', 'person', 'clouds', 'sky']\n " ,
165
- " vectorised_labels = [embeddings_dict[label].tolist() for label in small_labels]\n " ,
164
+ " vectorized_labels = [embeddings_dict[label].tolist() for label in small_labels]\n " ,
166
165
" \n " ,
167
166
" # Save them for further use.\n " ,
168
167
" word_2_vec_path = 'word_2_vec_glow_classes.json'\n " ,
169
168
" with open(word_2_vec_path, 'w') as fp:\n " ,
170
169
" json.dump({\n " ,
171
- " 'vect_labels': vectorised_labels ,\n " ,
170
+ " 'vect_labels': vectorized_labels ,\n " ,
172
171
" }, fp, indent=3)\n "
173
172
]
174
173
},
182
181
" # It would be hard to visualize vectors with 300 values, but luckly we have t-SNE for that.\n " ,
183
182
" # This function builds a t-SNE model(https://www.learnopencv.com/t-sne-for-feature-visualization/) \n " ,
184
183
" # for label embeddings and visualizes them.\n " ,
185
- " def tsne_plot(model):\n " ,
186
- " labels = []\n " ,
187
- " tokens = []\n " ,
188
- " \n " ,
189
- " for word in model:\n " ,
190
- " tokens.append(model[word])\n " ,
191
- " labels.append(word)\n " ,
192
- " \n " ,
184
+ " def tsne_plot(tokens, labels):\n " ,
193
185
" tsne_model = TSNE(perplexity=2, n_components=2, init='pca', n_iter=25000, random_state=2020, n_jobs=4)\n " ,
194
186
" new_values = tsne_model.fit_transform(tokens)\n " ,
195
- " \n " ,
196
187
" x = []\n " ,
197
188
" y = []\n " ,
198
189
" for value in new_values:\n " ,
201
192
" \n " ,
202
193
" plt.figure(figsize=(13, 13)) \n " ,
203
194
" for i in range(len(x)):\n " ,
204
- " plt.scatter(x[i],y[i])\n " ,
195
+ " plt.scatter(x[i], y[i])\n " ,
205
196
" plt.annotate(labels[i],\n " ,
206
197
" xy=(x[i], y[i]),\n " ,
207
198
" xytext=(5, 2),\n " ,
218
209
"metadata" : {},
219
210
"outputs" : [],
220
211
"source" : [
221
- " # Now we can draw t-SNE visualisation .\n " ,
222
- " tsne_plot(dict(zip(small_labels, vectorised_labels)) )"
212
+ " # Now we can draw t-SNE visualization .\n " ,
213
+ " tsne_plot(vectorized_labels, small_labels )"
223
214
]
224
215
},
225
216
{
322
313
" adj_matrix_path = 'adjacency_matrix.json'\n " ,
323
314
" # Count all labels.\n " ,
324
315
" nums = np.sum(np.array(dataset_train.annos), axis=0)\n " ,
325
- " adj = np.zeros((len(small_labels), len(small_labels)), dtype=int)\n " ,
316
+ " label_len = len(small_labels)\n " ,
317
+ " adj = np.zeros((label_len, label_len), dtype=int)\n " ,
326
318
" # Now iterate over the whole training set and consider all pairs of labels in sample annotation.\n " ,
327
319
" for sample in dataset_train.annos:\n " ,
328
320
" sample_idx = np.argwhere(sample > 0)[:, 0]\n " ,
381
373
" + str(self.in_features) + ' -> ' \\\n " ,
382
374
" + str(self.out_features) + ')'\n " ,
383
375
" \n " ,
384
- " # Create adjacency matrix from probabilities .\n " ,
376
+ " # Create adjacency matrix from statistics .\n " ,
385
377
" def gen_A(num_classes, t, p, adj_data):\n " ,
386
378
" adj = np.array(adj_data['adj']).astype(np.float32)\n " ,
387
379
" nums = np.array(adj_data['nums']).astype(np.float32)\n " ,
393
385
" adj = adj + np.identity(num_classes, np.int)\n " ,
394
386
" return adj\n " ,
395
387
" \n " ,
396
- " # Apply adjacency matrix renormalisation .\n " ,
388
+ " # Apply adjacency matrix re-normalization .\n " ,
397
389
" def gen_adj(A):\n " ,
398
390
" D = torch.pow(A.sum(1).float(), -0.5)\n " ,
399
391
" D = torch.diag(D).type_as(A)\n " ,
413
405
" self.gc1 = GraphConvolution(in_channel, 1024)\n " ,
414
406
" self.gc2 = GraphConvolution(1024, 2048)\n " ,
415
407
" self.relu = nn.LeakyReLU(0.2)\n " ,
416
- " # Load probability data for adjacency matrix\n " ,
408
+ " # Load data for adjacency matrix\n " ,
417
409
" with open(adj_path) as fp:\n " ,
418
410
" adj_data = json.load(fp)\n " ,
419
411
" # Compute adjacency matrix\n " ,
474
466
" batch_size = 32\n " ,
475
467
" save_freq = 1 # Save checkpoint frequency (epochs)\n " ,
476
468
" test_freq = 200 # Test model frequency (iterations)\n " ,
477
- " max_epoch_number = 50 # Number of epochs for training \n " ,
469
+ " max_epoch_number = 35 # Number of epochs for training \n " ,
478
470
" # Note: on the small subset of data overfitting happens after 30-35 epochs.\n " ,
479
471
" \n " ,
480
472
" mean = [0.485, 0.456, 0.406]\n " ,
527
519
" transforms.ColorJitter(),\n " ,
528
520
" transforms.RandomAffine(degrees=20, translate=(0.2, 0.2), scale=(0.5, 1.5),\n " ,
529
521
" shear=None, resample=False, \n " ,
530
- " fillcolor=tuple(np.array(np.array(mean)* 255).astype(int).tolist())),\n " ,
522
+ " fillcolor=tuple(np.array(np.array(mean) * 255).astype(int).tolist())),\n " ,
531
523
" transforms.ToTensor(),\n " ,
532
524
" transforms.Normalize(mean, std)\n " ,
533
525
" ])"
650
642
"source" : [
651
643
" # Run inference on the test data.\n " ,
652
644
" model.eval()\n " ,
653
- " for sample_id in [1,2,3,4, 6]:\n " ,
645
+ " for sample_id in [1, 2, 3, 4, 6]:\n " ,
654
646
" test_img, test_labels, gcn_input = test_dataset[sample_id]\n " ,
655
647
" test_img_path = os.path.join(img_folder, test_dataset.imgs[sample_id])\n " ,
656
648
" with torch.no_grad():\n " ,
689
681
},
690
682
"nbformat" : 4 ,
691
683
"nbformat_minor" : 4
692
- }
684
+ }
0 commit comments