Skip to content

Commit 1d25818

Browse files
committed
Update after review
1 parent 5752b16 commit 1d25818

File tree

17 files changed

+40
-48
lines changed

17 files changed

+40
-48
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Training
22
For training run [jupyter notebook](graph_convolutional_networks_model_relations_in_data.ipynb)
33

4-
_Note: we use dataset from_ [_this post_](../PyTorch-Multi-Label-Image-Classification-Image-Tagging/README.md)
4+
_Note: we use the dataset from [this post](../PyTorch-Multi-Label-Image-Classification-Image-Tagging/README.md), so please refer to it for a detailed description and instructions._

Graph-Convolutional-Networks-Model-Relations-In-Data/graph_convolutional_networks_model_relations_in_data.ipynb

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,33 @@
1818
"metadata": {},
1919
"outputs": [],
2020
"source": [
21+
"import itertools\n",
22+
"import json\n",
23+
"import math\n",
2124
"import os\n",
25+
"import random\n",
26+
"import tarfile\n",
2227
"import time\n",
28+
"import urllib.request\n",
29+
"import zipfile\n",
30+
"from shutil import copyfile\n",
31+
"\n",
2332
"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",
2934
"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",
3436
"from matplotlib import pyplot as plt\n",
3537
"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",
4238
"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",
4441
"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"
4748
]
4849
},
4950
{
@@ -102,10 +103,9 @@
102103
" with tarfile.open(path_to_tar_file) as tar_ref:\n",
103104
" tar_ref.extractall(os.path.dirname(img_folder))\n",
104105
" 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'))"
109109
]
110110
},
111111
{
@@ -116,7 +116,6 @@
116116
},
117117
"outputs": [],
118118
"source": [
119-
"# Download Glove model trained on wikipedia.\n",
120119
"# We want to represent our label names as vectors in order to use them as features further.\n",
121120
"# To do that we decided to use GloVe model (https://nlp.stanford.edu/projects/glove/).\n",
122121
"# Let's download GloVe model trained on a Wikipedia Text Corpus.\n",
@@ -162,13 +161,13 @@
162161
" 'nighttime', 'boats', 'mountain', 'tree', 'snow', 'beach', 'vehicle', 'rocks',\n",
163162
" 'reflection', 'sunset', 'road', 'flowers', 'ocean', 'lake', 'window', 'plants',\n",
164163
" '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",
166165
"\n",
167166
"# Save them for further use.\n",
168167
"word_2_vec_path = 'word_2_vec_glow_classes.json'\n",
169168
"with open(word_2_vec_path, 'w') as fp:\n",
170169
" json.dump({\n",
171-
" 'vect_labels': vectorised_labels,\n",
170+
" 'vect_labels': vectorized_labels,\n",
172171
" }, fp, indent=3)\n"
173172
]
174173
},
@@ -182,17 +181,9 @@
182181
"# It would be hard to visualize vectors with 300 values, but luckly we have t-SNE for that.\n",
183182
"# This function builds a t-SNE model(https://www.learnopencv.com/t-sne-for-feature-visualization/) \n",
184183
"# 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",
193185
" tsne_model = TSNE(perplexity=2, n_components=2, init='pca', n_iter=25000, random_state=2020, n_jobs=4)\n",
194186
" new_values = tsne_model.fit_transform(tokens)\n",
195-
"\n",
196187
" x = []\n",
197188
" y = []\n",
198189
" for value in new_values:\n",
@@ -201,7 +192,7 @@
201192
" \n",
202193
" plt.figure(figsize=(13, 13)) \n",
203194
" for i in range(len(x)):\n",
204-
" plt.scatter(x[i],y[i])\n",
195+
" plt.scatter(x[i], y[i])\n",
205196
" plt.annotate(labels[i],\n",
206197
" xy=(x[i], y[i]),\n",
207198
" xytext=(5, 2),\n",
@@ -218,8 +209,8 @@
218209
"metadata": {},
219210
"outputs": [],
220211
"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)"
223214
]
224215
},
225216
{
@@ -322,7 +313,8 @@
322313
"adj_matrix_path = 'adjacency_matrix.json'\n",
323314
"# Count all labels.\n",
324315
"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",
326318
"# Now iterate over the whole training set and consider all pairs of labels in sample annotation.\n",
327319
"for sample in dataset_train.annos:\n",
328320
" sample_idx = np.argwhere(sample > 0)[:, 0]\n",
@@ -381,7 +373,7 @@
381373
" + str(self.in_features) + ' -> ' \\\n",
382374
" + str(self.out_features) + ')'\n",
383375
"\n",
384-
"# Create adjacency matrix from probabilities.\n",
376+
"# Create adjacency matrix from statistics.\n",
385377
"def gen_A(num_classes, t, p, adj_data):\n",
386378
" adj = np.array(adj_data['adj']).astype(np.float32)\n",
387379
" nums = np.array(adj_data['nums']).astype(np.float32)\n",
@@ -393,7 +385,7 @@
393385
" adj = adj + np.identity(num_classes, np.int)\n",
394386
" return adj\n",
395387
"\n",
396-
"# Apply adjacency matrix renormalisation.\n",
388+
"# Apply adjacency matrix re-normalization.\n",
397389
"def gen_adj(A):\n",
398390
" D = torch.pow(A.sum(1).float(), -0.5)\n",
399391
" D = torch.diag(D).type_as(A)\n",
@@ -413,7 +405,7 @@
413405
" self.gc1 = GraphConvolution(in_channel, 1024)\n",
414406
" self.gc2 = GraphConvolution(1024, 2048)\n",
415407
" self.relu = nn.LeakyReLU(0.2)\n",
416-
" # Load probability data for adjacency matrix\n",
408+
" # Load data for adjacency matrix\n",
417409
" with open(adj_path) as fp:\n",
418410
" adj_data = json.load(fp)\n",
419411
" # Compute adjacency matrix\n",
@@ -474,7 +466,7 @@
474466
"batch_size = 32\n",
475467
"save_freq = 1 # Save checkpoint frequency (epochs)\n",
476468
"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",
478470
"# Note: on the small subset of data overfitting happens after 30-35 epochs.\n",
479471
"\n",
480472
"mean = [0.485, 0.456, 0.406]\n",
@@ -527,7 +519,7 @@
527519
" transforms.ColorJitter(),\n",
528520
" transforms.RandomAffine(degrees=20, translate=(0.2, 0.2), scale=(0.5, 1.5),\n",
529521
" 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",
531523
" transforms.ToTensor(),\n",
532524
" transforms.Normalize(mean, std)\n",
533525
"])"
@@ -650,7 +642,7 @@
650642
"source": [
651643
"# Run inference on the test data.\n",
652644
"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",
654646
" test_img, test_labels, gcn_input = test_dataset[sample_id]\n",
655647
" test_img_path = os.path.join(img_folder, test_dataset.imgs[sample_id])\n",
656648
" with torch.no_grad():\n",
@@ -689,4 +681,4 @@
689681
},
690682
"nbformat": 4,
691683
"nbformat_minor": 4
692-
}
684+
}

0 commit comments

Comments
 (0)