Skip to content

Commit b5da0e5

Browse files
committed
remove cuda from imagenet example
1 parent 6d490cd commit b5da0e5

File tree

2 files changed

+64
-105
lines changed

2 files changed

+64
-105
lines changed

7_imagenet_classification.ipynb

Lines changed: 62 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
{
22
"metadata": {
3-
"language": "lua",
4-
"name": "",
5-
"signature": "sha256:09d55f40fef1e7c13f11a514fe6a93ec9de3c599d77c239d25bbe6ecf5de96fa"
3+
"kernelspec": {
4+
"display_name": "iTorch",
5+
"language": "lua",
6+
"name": "itorch"
7+
},
8+
"language_info": {
9+
"name": "lua",
10+
"version": "20100"
11+
},
12+
"name": ""
613
},
714
"nbformat": 3,
815
"nbformat_minor": 0,
@@ -21,11 +28,8 @@
2128
"\n",
2229
"-- Loads the mapping from net outputs to human readable labels\n",
2330
"function load_synset()\n",
24-
" local file = io.open '7_imagenet_classification/synset_words.txt'\n",
2531
" local list = {}\n",
26-
" while true do\n",
27-
" local line = file:read()\n",
28-
" if not line then break end\n",
32+
" for line in io.lines'7_imagenet_classification/synset_words.txt' do\n",
2933
" table.insert(list, string.sub(line,11))\n",
3034
" end\n",
3135
" return list\n",
@@ -37,18 +41,15 @@
3741
" -- rescale the image\n",
3842
" local im3 = image.scale(im,224,224,'bilinear')*255\n",
3943
" -- RGB2BGR\n",
40-
" local im4 = im3:clone()\n",
41-
" im4[{1,{},{}}] = im3[{3,{},{}}]\n",
42-
" im4[{3,{},{}}] = im3[{1,{},{}}]\n",
43-
"\n",
44+
" local im4 = im3:index(1,torch.LongTensor{3,2,1})\n",
4445
" -- subtract imagenet mean\n",
4546
" return im4 - image.scale(img_mean, 224, 224, 'bilinear')\n",
4647
"end"
4748
],
4849
"language": "python",
4950
"metadata": {},
5051
"outputs": [],
51-
"prompt_number": 1
52+
"prompt_number": 16
5253
},
5354
{
5455
"cell_type": "code",
@@ -73,8 +74,7 @@
7374
"\n",
7475
"print '==> Loading network'\n",
7576
"-- Using network in network http://openreview.net/document/9b05a3bb-3a5e-49cb-91f7-0f482af65aea\n",
76-
"net = loadcaffe.load(proto_name, './nin_imagenet.caffemodel'):cuda()\n",
77-
"net.modules[#net.modules] = nil -- remove the top softmax\n",
77+
"net = loadcaffe.load(proto_name, './nin_imagenet.caffemodel')\n",
7878
"\n",
7979
"-- as we want to classify, let's disable dropouts by enabling evaluation mode\n",
8080
"net:evaluate()"
@@ -85,169 +85,135 @@
8585
{
8686
"metadata": {},
8787
"output_type": "pyout",
88-
"prompt_number": 2,
88+
"prompt_number": 3,
8989
"text": [
9090
"==> Loading network\t\n"
9191
]
9292
},
9393
{
9494
"metadata": {},
9595
"output_type": "pyout",
96-
"prompt_number": 2,
96+
"prompt_number": 3,
9797
"text": [
9898
"Successfully loaded ./nin_imagenet.caffemodel\n"
9999
]
100100
},
101101
{
102102
"metadata": {},
103103
"output_type": "pyout",
104-
"prompt_number": 2,
104+
"prompt_number": 3,
105105
"text": [
106106
"MODULE data UNDEFINED\n",
107-
"module 'data' not found\n"
108-
]
109-
},
110-
{
111-
"metadata": {},
112-
"output_type": "pyout",
113-
"prompt_number": 2,
114-
"text": [
115-
"conv1: 96 3 11 11\n"
107+
"warning: module 'data [type 5]' not found\n"
116108
]
117109
},
118110
{
119111
"metadata": {},
120112
"output_type": "pyout",
121-
"prompt_number": 2,
113+
"prompt_number": 3,
122114
"text": [
115+
"conv1: 96 3 11 11\n",
123116
"cccp1: 96 96 1 1\n",
124-
"cccp2: 96 96 1 1\n"
125-
]
126-
},
127-
{
128-
"metadata": {},
129-
"output_type": "pyout",
130-
"prompt_number": 2,
131-
"text": [
117+
"cccp2: 96 96 1 1\n",
132118
"conv2: 256 96 5 5\n"
133119
]
134120
},
135121
{
136122
"metadata": {},
137123
"output_type": "pyout",
138-
"prompt_number": 2,
139-
"text": [
140-
"cccp3: 256 256 1 1\n"
141-
]
142-
},
143-
{
144-
"metadata": {},
145-
"output_type": "pyout",
146-
"prompt_number": 2,
147-
"text": [
148-
"cccp4: 256 256 1 1\n"
149-
]
150-
},
151-
{
152-
"metadata": {},
153-
"output_type": "pyout",
154-
"prompt_number": 2,
124+
"prompt_number": 3,
155125
"text": [
126+
"cccp3: 256 256 1 1\n",
127+
"cccp4: 256 256 1 1\n",
156128
"conv3: 384 256 3 3\n"
157129
]
158130
},
159131
{
160132
"metadata": {},
161133
"output_type": "pyout",
162-
"prompt_number": 2,
163-
"text": [
164-
"cccp5: 384 384 1 1\n"
165-
]
166-
},
167-
{
168-
"metadata": {},
169-
"output_type": "pyout",
170-
"prompt_number": 2,
134+
"prompt_number": 3,
171135
"text": [
136+
"cccp5: 384 384 1 1\n",
172137
"cccp6: 384 384 1 1\n"
173138
]
174139
},
175140
{
176141
"metadata": {},
177142
"output_type": "pyout",
178-
"prompt_number": 2,
143+
"prompt_number": 3,
179144
"text": [
180145
"conv4-1024: 1024 384 3 3\n"
181146
]
182147
},
183148
{
184149
"metadata": {},
185150
"output_type": "pyout",
186-
"prompt_number": 2,
151+
"prompt_number": 3,
187152
"text": [
188153
"cccp7-1024: 1024 1024 1 1\n"
189154
]
190155
},
191156
{
192157
"metadata": {},
193158
"output_type": "pyout",
194-
"prompt_number": 2,
159+
"prompt_number": 3,
195160
"text": [
196161
"cccp8-1024: 1000 1024 1 1\n"
197162
]
198163
}
199164
],
200-
"prompt_number": 2
165+
"prompt_number": 3
201166
},
202167
{
203168
"cell_type": "code",
204169
"collapsed": false,
205170
"input": [
206-
"-- print net structure:\n",
171+
"-- print net structure\n",
207172
"print(tostring(net))\n",
208173
"\n",
209174
"-- visualize the weights of conv1\n",
210-
"itorch.image(net:get(1).weight:reshape(96,3,11,11))"
175+
"itorch.image(net:get(1).weight)"
211176
],
212177
"language": "python",
213178
"metadata": {},
214179
"outputs": [
215180
{
216181
"metadata": {},
217182
"output_type": "pyout",
218-
"prompt_number": 3,
183+
"prompt_number": 6,
219184
"text": [
220185
"nn.Sequential {\n",
221-
" [input -> (1) -> (2) -> (3) -> (4) -> (5) -> (6) -> (7) -> (8) -> (9) -> (10) -> (11) -> (12) -> (13) -> (14) -> (15) -> (16) -> (17) -> (18) -> (19) -> (20) -> (21) -> (22) -> (23) -> (24) -> (25) -> (26) -> (27) -> (28) -> (29) -> output]\n",
222-
" (1): nn.SpatialConvolutionMM(3 -> 96, 11x11, 4,4)\n",
186+
" [input -> (1) -> (2) -> (3) -> (4) -> (5) -> (6) -> (7) -> (8) -> (9) -> (10) -> (11) -> (12) -> (13) -> (14) -> (15) -> (16) -> (17) -> (18) -> (19) -> (20) -> (21) -> (22) -> (23) -> (24) -> (25) -> (26) -> (27) -> (28) -> (29) -> (30) -> output]\n",
187+
" (1): nn.SpatialConvolution(3 -> 96, 11x11, 4,4)\n",
223188
" (2): nn.ReLU\n",
224-
" (3): nn.SpatialConvolutionMM(96 -> 96, 1x1)\n",
189+
" (3): nn.SpatialConvolution(96 -> 96, 1x1)\n",
225190
" (4): nn.ReLU\n",
226-
" (5): nn.SpatialConvolutionMM(96 -> 96, 1x1)\n",
191+
" (5): nn.SpatialConvolution(96 -> 96, 1x1)\n",
227192
" (6): nn.ReLU\n",
228-
" (7): inn.SpatialMaxPooling(3,3,2,2)\n",
229-
" (8): nn.SpatialConvolutionMM(96 -> 256, 5x5, 2,2)\n",
193+
" (7): nn.SpatialMaxPooling(3,3,2,2)\n",
194+
" (8): nn.SpatialConvolution(96 -> 256, 5x5, 1,1, 2,2)\n",
230195
" (9): nn.ReLU\n",
231-
" (10): nn.SpatialConvolutionMM(256 -> 256, 1x1)\n",
196+
" (10): nn.SpatialConvolution(256 -> 256, 1x1)\n",
232197
" (11): nn.ReLU\n",
233-
" (12): nn.SpatialConvolutionMM(256 -> 256, 1x1)\n",
198+
" (12): nn.SpatialConvolution(256 -> 256, 1x1)\n",
234199
" (13): nn.ReLU\n",
235-
" (14): inn.SpatialMaxPooling(3,3,2,2)\n",
236-
" (15): nn.SpatialConvolutionMM(256 -> 384, 3x3, 1,1)\n",
200+
" (14): nn.SpatialMaxPooling(3,3,2,2)\n",
201+
" (15): nn.SpatialConvolution(256 -> 384, 3x3, 1,1, 1,1)\n",
237202
" (16): nn.ReLU\n",
238-
" (17): nn.SpatialConvolutionMM(384 -> 384, 1x1)\n",
203+
" (17): nn.SpatialConvolution(384 -> 384, 1x1)\n",
239204
" (18): nn.ReLU\n",
240-
" (19): nn.SpatialConvolutionMM(384 -> 384, 1x1)\n",
205+
" (19): nn.SpatialConvolution(384 -> 384, 1x1)\n",
241206
" (20): nn.ReLU\n",
242-
" (21): inn.SpatialMaxPooling(3,3,2,2)\n",
207+
" (21): nn.SpatialMaxPooling(3,3,2,2)\n",
243208
" (22): nn.Dropout(0.500000)\n",
244-
" (23): nn.SpatialConvolutionMM(384 -> 1024, 3x3, 1,1)\n",
209+
" (23): nn.SpatialConvolution(384 -> 1024, 3x3, 1,1, 1,1)\n",
245210
" (24): nn.ReLU\n",
246-
" (25): nn.SpatialConvolutionMM(1024 -> 1024, 1x1)\n",
211+
" (25): nn.SpatialConvolution(1024 -> 1024, 1x1)\n",
247212
" (26): nn.ReLU\n",
248-
" (27): nn.SpatialConvolutionMM(1024 -> 1000, 1x1)\n",
213+
" (27): nn.SpatialConvolution(1024 -> 1000, 1x1)\n",
249214
" (28): nn.ReLU\n",
250-
" (29): inn.SpatialAveragePooling(6,6,1,1)\n",
215+
" (29): nn.SpatialAveragePooling(6,6,1,1)\n",
216+
" (30): nn.SoftMax\n",
251217
"}\t\n"
252218
]
253219
},
@@ -265,7 +231,7 @@
265231
]
266232
}
267233
],
268-
"prompt_number": 3
234+
"prompt_number": 6
269235
},
270236
{
271237
"cell_type": "code",
@@ -291,7 +257,7 @@
291257
]
292258
}
293259
],
294-
"prompt_number": 4
260+
"prompt_number": 7
295261
},
296262
{
297263
"cell_type": "code",
@@ -317,7 +283,7 @@
317283
]
318284
}
319285
],
320-
"prompt_number": 5
286+
"prompt_number": 8
321287
},
322288
{
323289
"cell_type": "code",
@@ -344,7 +310,7 @@
344310
]
345311
}
346312
],
347-
"prompt_number": 6
313+
"prompt_number": 9
348314
},
349315
{
350316
"cell_type": "code",
@@ -358,7 +324,7 @@
358324
"synset_words = load_synset()\n",
359325
"\n",
360326
"-- sort outputs in decreasing order and show 5 best classes\n",
361-
"_,classes = net:forward(I:cuda()):view(-1):float():sort(true)\n",
327+
"_,classes = net:forward(I):view(-1):sort(true)\n",
362328
"for i=1,5 do\n",
363329
" print('predicted class '..tostring(i)..': ', synset_words[classes[i]])\n",
364330
"end"
@@ -369,21 +335,21 @@
369335
{
370336
"metadata": {},
371337
"output_type": "pyout",
372-
"prompt_number": 7,
338+
"prompt_number": 20,
373339
"text": [
374340
"==> Preprocessing\t\n",
375341
"predicted class 1: \tgoldfish, Carassius auratus\t\n",
376342
"predicted class 2: \tmacaw\t\n",
377-
"predicted class 3: \tlorikeet\t\n",
378-
"predicted class 4: \tflamingo\t\n",
379-
"predicted class 5: \tpinwheel\t\n"
343+
"predicted class 3: \tmoped\t\n",
344+
"predicted class 4: \tmonastery\t\n",
345+
"predicted class 5: \tmosquito net\t\n"
380346
]
381347
}
382348
],
383-
"prompt_number": 7
349+
"prompt_number": 20
384350
}
385351
],
386352
"metadata": {}
387353
}
388354
]
389-
}
355+
}

7_imagenet_classification/classify.lua

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,13 @@ require 'image'
66

77
-- Loads the mapping from net outputs to human readable labels
88
function load_synset()
9-
local file = io.open 'synset_words.txt'
109
local list = {}
11-
while true do
12-
local line = file:read()
13-
if not line then break end
10+
for line in io.lines'synset_words.txt' do
1411
table.insert(list, string.sub(line,11))
1512
end
1613
return list
1714
end
1815

19-
2016
-- Converts an image from RGB to BGR format and subtracts mean
2117
function preprocess(im, img_mean)
2218
-- rescale the image
@@ -27,8 +23,6 @@ function preprocess(im, img_mean)
2723
return im4 - image.scale(img_mean, 224, 224, 'bilinear')
2824
end
2925

30-
31-
3226
-- Setting up networks and downloading stuff if needed
3327
proto_name = 'deploy.prototxt'
3428
model_name = 'nin_imagenet.caffemodel'
@@ -46,7 +40,6 @@ if not paths.filep(img_mean_name) then os.execute('wget '..img_mean_url) end
4640
if not paths.filep(image_name) then os.execute('wget '..image_url) end
4741

4842

49-
5043
print '==> Loading network'
5144
-- Using network in network http://openreview.net/document/9b05a3bb-3a5e-49cb-91f7-0f482af65aea
5245
net = loadcaffe.load(proto_name, './nin_imagenet.caffemodel')
@@ -67,7 +60,7 @@ print '==> Preprocessing'
6760
I = preprocess(im, img_mean)
6861

6962
-- Propagate through the network and sort outputs in decreasing order and show 5 best classes
70-
_,classes = net:forward(I):view(-1):float():sort(true)
63+
_,classes = net:forward(I):view(-1):sort(true)
7164
for i=1,5 do
7265
print('predicted class '..tostring(i)..': ', synset_words[classes[i] ])
7366
end

0 commit comments

Comments
 (0)