@@ -31,6 +31,7 @@ cmd:option('--smoothness', 0, 'Total variation norm regularization s
31
31
cmd :option (' --init' , ' image' , ' {image, random}. Initialization mode for optimized image.' )
32
32
cmd :option (' --backend' , ' cunn' , ' {cunn, cudnn}. Neural network CUDA backend.' )
33
33
cmd :option (' --optimizer' , ' lbfgs' , ' {sgd, lbfgs}. Optimization algorithm.' )
34
+ cmd :option (' --cpu' , false , ' Optimize on CPU (only with VGG network).' )
34
35
opt = cmd :parse (arg )
35
36
if opt .size <= 0 then
36
37
opt .size = nil
@@ -51,6 +52,10 @@ if opt.model == 'inception' then
51
52
print (' run download_models.sh to download model weights' )
52
53
error (' ' )
53
54
end
55
+
56
+ if opt .cpu then
57
+ error (' CPU optimization only works with VGG model' )
58
+ end
54
59
elseif opt .model == ' vgg' then
55
60
if not paths .filep (vgg_path ) then
56
61
print (' ERROR: could not find VGG model weights at ' .. vgg_path )
@@ -97,6 +102,13 @@ elseif opt.model == 'vgg' then
97
102
98
103
model = create_vgg (vgg_path , opt .backend )
99
104
end
105
+
106
+ -- run on GPU
107
+ if opt .cpu then
108
+ model :float ()
109
+ else
110
+ model :cuda ()
111
+ end
100
112
collectgarbage ()
101
113
102
114
-- compute normalization factor
@@ -111,14 +123,20 @@ for k, v in pairs(content_weights) do
111
123
end
112
124
113
125
-- load content image
114
- local img = preprocess (image .load (opt .content ), opt .size ):cuda ()
126
+ local img = preprocess (image .load (opt .content ), opt .size )
127
+ if not opt .cpu then
128
+ img = img :cuda ()
129
+ end
115
130
model :forward (img )
116
131
local img_activations , _ = collect_activations (model , content_weights , {})
117
132
118
133
-- load style image
119
134
local art = preprocess (
120
135
image .load (opt .style ), math.max (img :size (3 ), img :size (4 ))
121
- ):cuda ()
136
+ )
137
+ if not opt .cpu then
138
+ art = art :cuda ()
139
+ end
122
140
model :forward (art )
123
141
local _ , art_grams = collect_activations (model , {}, style_weights )
124
142
art = nil
@@ -130,7 +148,8 @@ function opfunc(input)
130
148
131
149
-- backpropagate
132
150
local loss = 0
133
- local grad = torch .CudaTensor (model .output :size ()):zero ()
151
+ local grad = opt .cpu and torch .FloatTensor () or torch .CudaTensor ()
152
+ grad :resize (model .output :size ()):zero ()
134
153
for i = # model .modules , 1 , - 1 do
135
154
local module_input = (i == 1 ) and input or model .modules [i - 1 ].output
136
155
local module = model .modules [i ]
@@ -168,7 +187,11 @@ if opt.init == 'image' then
168
187
elseif opt .init == ' random' then
169
188
input = preprocess (
170
189
torch .randn (3 , img :size (3 ), img :size (4 )):mul (0.1 ):add (0.5 ):clamp (0 , 1 )
171
- ):cuda ()
190
+ )
191
+
192
+ if not opt .cpu then
193
+ input = input :cuda ()
194
+ end
172
195
else
173
196
error (' unrecognized initialization option: ' .. opt .init )
174
197
end
@@ -190,10 +213,15 @@ image.save(paths.concat(frames_dir, '0.jpg'), output)
190
213
local optim_state
191
214
if opt .optimizer == ' sgd' then
192
215
optim_state = {
193
- learningRate = 0.1 ,
194
216
momentum = 0.9 ,
195
217
dampening = 0.0 ,
196
218
}
219
+
220
+ if opt .model == ' inception' then
221
+ optim_state .learningRate = 5e-2
222
+ else
223
+ optim_state .learningRate = 1e-3
224
+ end
197
225
elseif opt .optimizer == ' lbfgs' then
198
226
optim_state = {
199
227
maxIter = 3 ,
0 commit comments