Skip to content

Commit 9c0d691

Browse files
committed
update fixes
1 parent bc17397 commit 9c0d691

8 files changed

+99
-28
lines changed

utils/setup_colab.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,24 @@ def setup_week03_1():
2626

2727

2828
def setup_week03_2():
29+
download_github_code('utils/tqdm_utils.py')
2930
download_github_code('utils/download_utils.py')
3031
download_github_code('utils/grading.py')
3132

3233

3334
def setup_week04():
3435
download_github_code('week04/lfw_dataset.py')
36+
download_github_code('utils/tqdm_utils.py')
3537
download_github_code('utils/download_utils.py')
3638
download_github_code('utils/grading.py')
3739

3840

3941
def setup_week05():
4042
download_github_code('utils/grading.py')
4143
download_github_code('week05/names.txt')
44+
45+
46+
def setup_week06():
47+
download_github_code('utils/grading.py')
48+
download_github_code('week06/beheaded_inception3.py')
49+
download_github_code('week06/grading_utils.py')

week03/week03_finetuning_inception.ipynb

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,19 @@
6969
},
7070
"outputs": [],
7171
"source": [
72-
"import numpy as np\n",
73-
"\n",
7472
"%matplotlib inline\n",
7573
"import matplotlib.pyplot as plt\n",
7674
"\n",
7775
"import random\n",
7876
"import tqdm\n",
79-
"import itertools\n",
8077
"import collections\n",
81-
"from copy import deepcopy\n",
8278
"from IPython import display\n",
8379
"\n",
8480
"import torch\n",
8581
"from torch import nn\n",
8682
"from torch import optim\n",
8783
"from torch.utils.data import DataLoader\n",
8884
"\n",
89-
"import torchvision\n",
9085
"from torchvision import datasets, transforms, models\n",
9186
"\n",
9287
"from sklearn.model_selection import train_test_split"
@@ -376,7 +371,7 @@
376371
"source": [
377372
"image, label = dataset[232]\n",
378373
"print(label)\n",
379-
"imshow(image.permute(1, 2, 0))"
374+
"plt.imshow(image.permute(1, 2, 0))"
380375
]
381376
},
382377
{
@@ -501,7 +496,7 @@
501496
"source": [
502497
"image, label = dataset[232]\n",
503498
"print(image.shape, label)\n",
504-
"imshow(image.permute(1, 2, 0))"
499+
"plt.imshow(image.permute(1, 2, 0))"
505500
]
506501
},
507502
{
@@ -1038,4 +1033,4 @@
10381033
},
10391034
"nbformat": 4,
10401035
"nbformat_minor": 1
1041-
}
1036+
}

week03/week03_first_cnn_cifar10.ipynb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@
137137
},
138138
"outputs": [],
139139
"source": [
140+
"import grading\n",
141+
"\n",
140142
"grader = grading.Grader(\n",
141143
" assignment_key=\"s1B1I5DuEeeyLAqI7dCYkg\",\n",
142144
" all_parts=[\"7W4tu\", \"nQOsg\", \"96eco\"]\n",
@@ -1215,4 +1217,4 @@
12151217
},
12161218
"nbformat": 4,
12171219
"nbformat_minor": 1
1218-
}
1220+
}

week04/week04_adversarial.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"import grading\n",
6666
"\n",
6767
"grader = grading.Grader(\n",
68-
" assignment_key=\"9TShnp1JEeeGGAoCUnhvuA\",\n",
68+
" assignment_key=\"FCl7G51lEeeeZQ4xJ2nzLA\",\n",
6969
" all_parts=[\"ryO01\"]\n",
7070
")"
7171
]
@@ -643,7 +643,7 @@
643643
"outputs": [],
644644
"source": [
645645
"## GRADED PART, DO NOT CHANGE!\n",
646-
"grader.set_answer(\"ryO01\", ans_part1)"
646+
"grader.set_answer(\"ryO01\", ans)"
647647
]
648648
},
649649
{

week04/week04_autoencoders.ipynb

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -779,19 +779,6 @@
779779
"# Submit to Coursera"
780780
]
781781
},
782-
{
783-
"cell_type": "code",
784-
"execution_count": null,
785-
"metadata": {
786-
"id": "sNCPK31xnpy-"
787-
},
788-
"outputs": [],
789-
"source": [
790-
"# token expires every 30 min\n",
791-
"COURSERA_TOKEN = ### YOUR TOKEN HERE\n",
792-
"COURSERA_EMAIL = ### YOUR EMAIL HERE"
793-
]
794-
},
795782
{
796783
"cell_type": "code",
797784
"execution_count": null,
@@ -1309,4 +1296,4 @@
13091296
},
13101297
"nbformat": 4,
13111298
"nbformat_minor": 1
1312-
}
1299+
}

week05/week05_generating_names_with_rnn.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
"# to make them of equal size for further batching\n",
124124
"pad_token = \"#\"\n",
125125
"\n",
126-
"with open(\"names\") as f:\n",
126+
"with open(\"names.txt\") as f:\n",
127127
" names = f.read()[:-1].split('\\n')\n",
128128
" names = [start_token + name for name in names]"
129129
]

week06/beheaded_inception3.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import torch, torch.nn as nn
2+
import torch.nn.functional as F
3+
from torch.autograd import Variable
4+
from torchvision.models.inception import Inception3
5+
from warnings import warn
6+
from torch.utils.model_zoo import load_url
7+
8+
9+
class BeheadedInception3(Inception3):
10+
""" Like torchvision.models.inception.Inception3 but the head goes separately """
11+
12+
def forward(self, x):
13+
if self.transform_input:
14+
x = x.clone()
15+
x[:, 0] = x[:, 0] * (0.229 / 0.5) + (0.485 - 0.5) / 0.5
16+
x[:, 1] = x[:, 1] * (0.224 / 0.5) + (0.456 - 0.5) / 0.5
17+
x[:, 2] = x[:, 2] * (0.225 / 0.5) + (0.406 - 0.5) / 0.5
18+
else: warn("Input isn't transformed")
19+
x = self.Conv2d_1a_3x3(x)
20+
x = self.Conv2d_2a_3x3(x)
21+
x = self.Conv2d_2b_3x3(x)
22+
x = F.max_pool2d(x, kernel_size=3, stride=2)
23+
x = self.Conv2d_3b_1x1(x)
24+
x = self.Conv2d_4a_3x3(x)
25+
x = F.max_pool2d(x, kernel_size=3, stride=2)
26+
x = self.Mixed_5b(x)
27+
x = self.Mixed_5c(x)
28+
x = self.Mixed_5d(x)
29+
x = self.Mixed_6a(x)
30+
x = self.Mixed_6b(x)
31+
x = self.Mixed_6c(x)
32+
x = self.Mixed_6d(x)
33+
x = self.Mixed_6e(x)
34+
x = self.Mixed_7a(x)
35+
x = self.Mixed_7b(x)
36+
x_for_attn = x = self.Mixed_7c(x)
37+
# 8 x 8 x 2048
38+
x = F.avg_pool2d(x, kernel_size=8)
39+
# 1 x 1 x 2048
40+
x_for_capt = x = x.view(x.size(0), -1)
41+
# 2048
42+
x = self.fc(x)
43+
# 1000 (num_classes)
44+
return x_for_attn, x_for_capt, x
45+
46+
47+
def beheaded_inception_v3(transform_input=True):
48+
model= BeheadedInception3(transform_input=transform_input)
49+
inception_url = 'https://download.pytorch.org/models/inception_v3_google-1a9a5a14.pth'
50+
model.load_state_dict(load_url(inception_url))
51+
return model
52+

week06/week06_final_project_image_captioning.ipynb

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,33 @@
758758
"grader.submit(COURSERA_EMAIL, COURSERA_TOKEN)"
759759
]
760760
},
761+
{
762+
"cell_type": "markdown",
763+
"metadata": {},
764+
"source": [
765+
"# Apply trained model\n",
766+
"\n",
767+
"Let's unpack our pre-trained inception network and see what our model is capable of!"
768+
]
769+
},
770+
{
771+
"cell_type": "code",
772+
"execution_count": null,
773+
"metadata": {},
774+
"outputs": [],
775+
"source": [
776+
"from beheaded_inception3 import beheaded_inception_v3"
777+
]
778+
},
779+
{
780+
"cell_type": "code",
781+
"execution_count": null,
782+
"metadata": {},
783+
"outputs": [],
784+
"source": [
785+
"inception = beheaded_inception_v3().eval()"
786+
]
787+
},
761788
{
762789
"cell_type": "markdown",
763790
"metadata": {
@@ -784,7 +811,7 @@
784811
" t=1, sample=True, max_len=100):\n",
785812
" \n",
786813
" assert isinstance(image, np.ndarray) and np.max(image) <= 1\\\n",
787-
" and np.min(image) >=0 and image.shape[-1] == 3\n",
814+
" and np.min(image) >= 0 and image.shape[-1] == 3\n",
788815
" \n",
789816
" image = torch.tensor(image.transpose([2, 0, 1]), dtype=torch.float32)\n",
790817
" \n",
@@ -799,7 +826,7 @@
799826
" next_word_probs = F.softmax(next_word_logits, -1).detach().numpy()\n",
800827
" \n",
801828
" \n",
802-
" assert len(next_word_probs.shape) ==1, 'probs must be one-dimensional'\n",
829+
" assert len(next_word_probs.shape) == 1, 'probs must be one-dimensional'\n",
803830
" next_word_probs = next_word_probs ** t / np.sum(next_word_probs ** t) # apply temperature\n",
804831
"\n",
805832
" if sample:\n",

0 commit comments

Comments
 (0)