Skip to content

Commit 3d18e2c

Browse files
committed
py3 fix (ty stamax@)
1 parent 516240c commit 3d18e2c

File tree

2 files changed

+35
-81
lines changed

2 files changed

+35
-81
lines changed

week7/captioning_theano.ipynb

Lines changed: 33 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"metadata": {
6-
"collapsed": false
7-
},
5+
"metadata": {},
86
"source": [
97
"DeepLarning Couse HSE 2016 fall: \n",
108
"* Arseniy Ashuha, you can text me ```ars.ashuha@gmail.com```,\n",
@@ -22,9 +20,7 @@
2220
{
2321
"cell_type": "code",
2422
"execution_count": null,
25-
"metadata": {
26-
"collapsed": false
27-
},
23+
"metadata": {},
2824
"outputs": [],
2925
"source": [
3026
"!wget https://www.dropbox.com/s/3hj16b0fj6yw7cc/data.tar.gz?dl=1 -O data.tar.gz\n",
@@ -41,9 +37,7 @@
4137
{
4238
"cell_type": "code",
4339
"execution_count": null,
44-
"metadata": {
45-
"collapsed": false
46-
},
40+
"metadata": {},
4741
"outputs": [],
4842
"source": [
4943
"%%time\n",
@@ -58,9 +52,7 @@
5852
{
5953
"cell_type": "code",
6054
"execution_count": null,
61-
"metadata": {
62-
"collapsed": false
63-
},
55+
"metadata": {},
6456
"outputs": [],
6557
"source": [
6658
"print \"each image code is a 1000-unit vector:\", img_codes.shape\n",
@@ -73,9 +65,7 @@
7365
{
7466
"cell_type": "code",
7567
"execution_count": null,
76-
"metadata": {
77-
"collapsed": false
78-
},
68+
"metadata": {},
7969
"outputs": [],
8070
"source": [
8171
"#split descriptions into tokens\n",
@@ -88,9 +78,7 @@
8878
{
8979
"cell_type": "code",
9080
"execution_count": null,
91-
"metadata": {
92-
"collapsed": false
93-
},
81+
"metadata": {},
9482
"outputs": [],
9583
"source": [
9684
"# Build a Vocabulary\n",
@@ -144,9 +132,7 @@
144132
{
145133
"cell_type": "code",
146134
"execution_count": null,
147-
"metadata": {
148-
"collapsed": false
149-
},
135+
"metadata": {},
150136
"outputs": [],
151137
"source": [
152138
"#try it out on several descriptions of a random image\n",
@@ -163,9 +149,7 @@
163149
{
164150
"cell_type": "code",
165151
"execution_count": null,
166-
"metadata": {
167-
"collapsed": false
168-
},
152+
"metadata": {},
169153
"outputs": [],
170154
"source": [
171155
"# network shapes. \n",
@@ -189,9 +173,7 @@
189173
{
190174
"cell_type": "code",
191175
"execution_count": null,
192-
"metadata": {
193-
"collapsed": false
194-
},
176+
"metadata": {},
195177
"outputs": [],
196178
"source": [
197179
"# Input Variable\n",
@@ -215,9 +197,7 @@
215197
{
216198
"cell_type": "code",
217199
"execution_count": null,
218-
"metadata": {
219-
"collapsed": false
220-
},
200+
"metadata": {},
221201
"outputs": [],
222202
"source": [
223203
"#network inputs\n",
@@ -233,9 +213,7 @@
233213
{
234214
"cell_type": "code",
235215
"execution_count": null,
236-
"metadata": {
237-
"collapsed": false
238-
},
216+
"metadata": {},
239217
"outputs": [],
240218
"source": [
241219
"# input layer for image features\n",
@@ -251,9 +229,7 @@
251229
{
252230
"cell_type": "code",
253231
"execution_count": null,
254-
"metadata": {
255-
"collapsed": false
256-
},
232+
"metadata": {},
257233
"outputs": [],
258234
"source": [
259235
"# Concatinate image features and word embedings in one sequence \n",
@@ -270,9 +246,7 @@
270246
{
271247
"cell_type": "code",
272248
"execution_count": null,
273-
"metadata": {
274-
"collapsed": false
275-
},
249+
"metadata": {},
276250
"outputs": [],
277251
"source": [
278252
"# Decoding of rnn hiden states\n",
@@ -332,9 +306,7 @@
332306
{
333307
"cell_type": "code",
334308
"execution_count": null,
335-
"metadata": {
336-
"collapsed": false
337-
},
309+
"metadata": {},
338310
"outputs": [],
339311
"source": [
340312
"next_word_probas = <get network output>\n",
@@ -343,12 +315,8 @@
343315
"predictions_flat = next_word_probas[:,:-1].reshape((-1,n_tokens))\n",
344316
"reference_answers = sentences[:,1:].reshape((-1,))\n",
345317
"\n",
346-
"#write symbolic loss function to train NN for\n",
347-
"loss = <compute elementwise loss function>\n",
348-
"\n",
349-
"#mean over non-PAD tokens\n",
350-
"output_mask = sentence_mask[:,1:]\n",
351-
"loss = (loss.reshape(reference_answers.shape)*output_mask).sum() / output_mask.sum()"
318+
"#write symbolic loss function to minimize over NN params\n",
319+
"loss = <compute elementwise loss function>"
352320
]
353321
},
354322
{
@@ -367,9 +335,7 @@
367335
{
368336
"cell_type": "code",
369337
"execution_count": null,
370-
"metadata": {
371-
"collapsed": false
372-
},
338+
"metadata": {},
373339
"outputs": [],
374340
"source": [
375341
"#compile a functions for training and evaluation\n",
@@ -381,9 +347,7 @@
381347
},
382348
{
383349
"cell_type": "markdown",
384-
"metadata": {
385-
"collapsed": false
386-
},
350+
"metadata": {},
387351
"source": [
388352
"# Training\n",
389353
"\n",
@@ -436,9 +400,7 @@
436400
{
437401
"cell_type": "code",
438402
"execution_count": null,
439-
"metadata": {
440-
"collapsed": false
441-
},
403+
"metadata": {},
442404
"outputs": [],
443405
"source": [
444406
"generate_batch(img_codes,captions,3)"
@@ -471,7 +433,6 @@
471433
"cell_type": "code",
472434
"execution_count": null,
473435
"metadata": {
474-
"collapsed": false,
475436
"scrolled": false
476437
},
477438
"outputs": [],
@@ -497,19 +458,15 @@
497458
},
498459
{
499460
"cell_type": "markdown",
500-
"metadata": {
501-
"collapsed": false
502-
},
461+
"metadata": {},
503462
"source": [
504463
"### apply trained model"
505464
]
506465
},
507466
{
508467
"cell_type": "code",
509468
"execution_count": null,
510-
"metadata": {
511-
"collapsed": false
512-
},
469+
"metadata": {},
513470
"outputs": [],
514471
"source": [
515472
"#the same kind you did last week, but a bit smaller\n",
@@ -520,6 +477,9 @@
520477
"\n",
521478
"#load weights\n",
522479
"lenet_weights = pickle.load(open('data/blvc_googlenet.pkl'))['param values']\n",
480+
"#python3: pickle.load(open('data/blvc_googlenet.pkl', 'rb'), encoding='latin1')['param values']\n",
481+
"\n",
482+
"\n",
523483
"set_all_param_values(lenet[\"prob\"], lenet_weights)\n",
524484
"\n",
525485
"#compile get_features\n",
@@ -531,9 +491,7 @@
531491
{
532492
"cell_type": "code",
533493
"execution_count": null,
534-
"metadata": {
535-
"collapsed": false
536-
},
494+
"metadata": {},
537495
"outputs": [],
538496
"source": [
539497
"from matplotlib import pyplot as plt\n",
@@ -547,9 +505,7 @@
547505
{
548506
"cell_type": "code",
549507
"execution_count": null,
550-
"metadata": {
551-
"collapsed": false
552-
},
508+
"metadata": {},
553509
"outputs": [],
554510
"source": [
555511
"#deprocess and show, one line :)\n",
@@ -605,9 +561,7 @@
605561
{
606562
"cell_type": "code",
607563
"execution_count": null,
608-
"metadata": {
609-
"collapsed": false
610-
},
564+
"metadata": {},
611565
"outputs": [],
612566
"source": [
613567
"for i in range(10):\n",
@@ -685,23 +639,23 @@
685639
],
686640
"metadata": {
687641
"kernelspec": {
688-
"display_name": "Python [Root]",
642+
"display_name": "Python 3",
689643
"language": "python",
690-
"name": "Python [Root]"
644+
"name": "python3"
691645
},
692646
"language_info": {
693647
"codemirror_mode": {
694648
"name": "ipython",
695-
"version": 2
649+
"version": 3
696650
},
697651
"file_extension": ".py",
698652
"mimetype": "text/x-python",
699653
"name": "python",
700654
"nbconvert_exporter": "python",
701-
"pygments_lexer": "ipython2",
702-
"version": "2.7.12"
655+
"pygments_lexer": "ipython3",
656+
"version": "3.6.2"
703657
}
704658
},
705659
"nbformat": 4,
706-
"nbformat_minor": 0
660+
"nbformat_minor": 1
707661
}

week7/pretrained_lenet.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ def preprocess(im):
101101
# Resize so smallest dim = 224, preserving aspect ratio
102102
h, w, _ = im.shape
103103
if h < w:
104-
im = skimage.transform.resize(im, (224, w*224/h), preserve_range=True)
104+
im = skimage.transform.resize(im, (224, w*224//h), preserve_range=True)
105105
else:
106-
im = skimage.transform.resize(im, (h*224/w, 224), preserve_range=True)
106+
im = skimage.transform.resize(im, (h*224//w, 224), preserve_range=True)
107107

108108
# Central crop to 224x224
109109
h, w, _ = im.shape

0 commit comments

Comments
 (0)