Owos commited on
Commit
4652abb
1 Parent(s): 2eedf66

Upload neural_style.py

Browse files
Files changed (1) hide show
  1. neural_style.py +18 -11
neural_style.py CHANGED
@@ -59,7 +59,7 @@ class TransferParams():
59
  style_blend_weights = None
60
  content_image = 'examples/inputs/tubingen.jpg'
61
  image_size = 512
62
- gpu = 0
63
  content_weight = 5e0
64
  style_weight = 1e2
65
  normalize_weights = False
@@ -71,7 +71,7 @@ class TransferParams():
71
  learning_rate = 1e0
72
  lbfgs_num_correction = 100
73
  print_iter = 50
74
- save_iter = 100
75
  output_image = 'out.png'
76
  log_level = 10
77
  style_scale = 1.0
@@ -79,7 +79,7 @@ class TransferParams():
79
  pooling = 'max'
80
  model_file = 'models/vgg19-d01eb7cb.pth'
81
  disable_check = False
82
- backend = 'nn'
83
  cudnn_autotune = False
84
  seed = -1
85
  content_layers = 'relu4_2'
@@ -91,6 +91,7 @@ def main():
91
 
92
  def transfer(params):
93
  dtype, multidevice, backward_device = setup_gpu()
 
94
 
95
  cnn, layerList = loadCaffemodel(params.model_file, params.pooling, params.gpu, params.disable_check)
96
 
@@ -238,7 +239,7 @@ def transfer(params):
238
  else:
239
  img = content_image.clone()
240
  img = nn.Parameter(img)
241
-
242
  def maybe_print(t, loss):
243
  if params.print_iter > 0 and t % params.print_iter == 0:
244
  print("Iteration " + str(t) + " / "+ str(params.num_iterations))
@@ -248,8 +249,9 @@ def transfer(params):
248
  print(" Style " + str(i+1) + " loss: " + str(loss_module.loss.item()))
249
  print(" Total loss: " + str(loss.item()))
250
 
 
251
  def maybe_save(t):
252
- should_save = params.save_iter > 0 and t % params.save_iter == 0
253
  should_save = should_save or t == params.num_iterations
254
  if should_save:
255
  output_filename, file_extension = os.path.splitext(params.output_image)
@@ -263,7 +265,11 @@ def transfer(params):
263
  if params.original_colors == 1:
264
  disp = original_colors(deprocess(content_image.clone()), disp)
265
 
 
266
  disp.save(str(filename))
 
 
 
267
 
268
  # Function to evaluate loss and gradient. We run the net forward and
269
  # backward to get the gradient, and sum up losses from the loss modules.
@@ -271,6 +277,7 @@ def transfer(params):
271
  # times, so we manually count the number of iterations to handle printing
272
  # and saving intermediate results.
273
  num_calls = [0]
 
274
  def feval():
275
  num_calls[0] += 1
276
  optimizer.zero_grad()
@@ -287,16 +294,16 @@ def transfer(params):
287
 
288
  loss.backward()
289
 
290
- maybe_save(num_calls[0])
291
  maybe_print(num_calls[0], loss)
292
 
293
  return loss
294
-
295
  optimizer, loopVal = setup_optimizer(img)
296
  while num_calls[0] <= loopVal:
297
  optimizer.step(feval)
298
 
299
-
300
  # Configure the optimizer
301
  def setup_optimizer(img):
302
  if params.optimizer == 'lbfgs':
@@ -347,9 +354,9 @@ def setup_gpu():
347
  setup_cuda()
348
  dtype = torch.FloatTensor
349
 
350
- elif "c" not in str(params.gpu).lower():
351
- setup_cuda()
352
- dtype, backward_device = torch.cuda.FloatTensor, "cuda:" + str(params.gpu)
353
  else:
354
  setup_cpu()
355
  dtype, backward_device = torch.FloatTensor, "cpu"
 
59
  style_blend_weights = None
60
  content_image = 'examples/inputs/tubingen.jpg'
61
  image_size = 512
62
+ gpu = "c"
63
  content_weight = 5e0
64
  style_weight = 1e2
65
  normalize_weights = False
 
71
  learning_rate = 1e0
72
  lbfgs_num_correction = 100
73
  print_iter = 50
74
+ save_iter = 1000
75
  output_image = 'out.png'
76
  log_level = 10
77
  style_scale = 1.0
 
79
  pooling = 'max'
80
  model_file = 'models/vgg19-d01eb7cb.pth'
81
  disable_check = False
82
+ backend = 'mkl'
83
  cudnn_autotune = False
84
  seed = -1
85
  content_layers = 'relu4_2'
 
91
 
92
  def transfer(params):
93
  dtype, multidevice, backward_device = setup_gpu()
94
+ print(dtype)
95
 
96
  cnn, layerList = loadCaffemodel(params.model_file, params.pooling, params.gpu, params.disable_check)
97
 
 
239
  else:
240
  img = content_image.clone()
241
  img = nn.Parameter(img)
242
+
243
  def maybe_print(t, loss):
244
  if params.print_iter > 0 and t % params.print_iter == 0:
245
  print("Iteration " + str(t) + " / "+ str(params.num_iterations))
 
249
  print(" Style " + str(i+1) + " loss: " + str(loss_module.loss.item()))
250
  print(" Total loss: " + str(loss.item()))
251
 
252
+ final_image = ''
253
  def maybe_save(t):
254
+ should_save = params.save_iter > 950 and t % params.save_iter == 0
255
  should_save = should_save or t == params.num_iterations
256
  if should_save:
257
  output_filename, file_extension = os.path.splitext(params.output_image)
 
265
  if params.original_colors == 1:
266
  disp = original_colors(deprocess(content_image.clone()), disp)
267
 
268
+ print('Saving')
269
  disp.save(str(filename))
270
+ print('the final image is:', disp)
271
+ final_image = disp
272
+ return disp
273
 
274
  # Function to evaluate loss and gradient. We run the net forward and
275
  # backward to get the gradient, and sum up losses from the loss modules.
 
277
  # times, so we manually count the number of iterations to handle printing
278
  # and saving intermediate results.
279
  num_calls = [0]
280
+
281
  def feval():
282
  num_calls[0] += 1
283
  optimizer.zero_grad()
 
294
 
295
  loss.backward()
296
 
297
+ final_image = maybe_save(num_calls[0])
298
  maybe_print(num_calls[0], loss)
299
 
300
  return loss
301
+ print('the final image is', final_image)
302
  optimizer, loopVal = setup_optimizer(img)
303
  while num_calls[0] <= loopVal:
304
  optimizer.step(feval)
305
 
306
+
307
  # Configure the optimizer
308
  def setup_optimizer(img):
309
  if params.optimizer == 'lbfgs':
 
354
  setup_cuda()
355
  dtype = torch.FloatTensor
356
 
357
+ #elif "c" not in str(params.gpu).lower():
358
+ #setup_cuda()
359
+ #dtype, backward_device = torch.cuda.FloatTensor, "cuda:" + str(params.gpu)
360
  else:
361
  setup_cpu()
362
  dtype, backward_device = torch.FloatTensor, "cpu"