From 203ac2465050c3cafd2bb926d257d5ae7e87ddc4 Mon Sep 17 00:00:00 2001 From: ProGamerGov Date: Sat, 11 Feb 2017 14:43:03 -0700 Subject: [PATCH 01/13] Added new -reflectance parameter --- neural_style.lua | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/neural_style.lua b/neural_style.lua index adc7621..dc28d77 100644 --- a/neural_style.lua +++ b/neural_style.lua @@ -48,6 +48,7 @@ cmd:option('-seed', -1) cmd:option('-content_layers', 'relu4_2', 'layers for content') cmd:option('-style_layers', 'relu1_1,relu2_1,relu3_1,relu4_1,relu5_1', 'layers for style') +cmd:option('-reflectance', false, 'if true, use reflectance padding') local function main(params) local dtype, multigpu = setup_gpu(params) @@ -129,6 +130,14 @@ local function main(params) else net:add(layer) end + if is_convolution and params.reflectance then + local padW, padH = layer.padW, layer.padH + local pad_layer = nn.SpatialReflectionPadding(padW, padW, padH, padH) + pad_layer = set_datatype(pad_layer, params.gpu) + net:add(pad_layer) + layer.padW = 0 + layer.padH = 0 + end if name == content_layers[next_content_idx] then print("Setting up content layer", i, ":", layer.name) local norm = params.normalize_gradients From d5bd670f638c4ecde8d698d9991481c905d1fc79 Mon Sep 17 00:00:00 2001 From: ProGamerGov Date: Sat, 11 Feb 2017 14:44:31 -0700 Subject: [PATCH 02/13] reflectance padding option --- neural_style.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/neural_style.lua b/neural_style.lua index dc28d77..440ca20 100644 --- a/neural_style.lua +++ b/neural_style.lua @@ -130,6 +130,7 @@ local function main(params) else net:add(layer) end + --reflectance padding option from leongatys/NeuralImageSynthesis if is_convolution and params.reflectance then local padW, padH = layer.padW, layer.padH local pad_layer = nn.SpatialReflectionPadding(padW, padW, padH, padH) From 4f5278f2efc64a3d0187c1e93a3d7104528b2c45 Mon Sep 17 00:00:00 2001 From: ProGamerGov Date: Sat, 11 Feb 2017 16:41:58 -0700 Subject: [PATCH 03/13] Update neural_style.lua --- neural_style.lua | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/neural_style.lua b/neural_style.lua index 440ca20..68298dd 100644 --- a/neural_style.lua +++ b/neural_style.lua @@ -130,7 +130,8 @@ local function main(params) else net:add(layer) end - --reflectance padding option from leongatys/NeuralImageSynthesis + --reflectance padding option from leongatys/NeuralImageSynthesis + local is_convolution = (layer_type == 'cudnn.SpatialConvolution' or layer_type == 'nn.SpatialConvolution') if is_convolution and params.reflectance then local padW, padH = layer.padW, layer.padH local pad_layer = nn.SpatialReflectionPadding(padW, padW, padH, padH) @@ -606,6 +607,17 @@ function TVLoss:updateGradInput(input, gradOutput) return self.gradInput end +-- Function to set gpu/cpu datatype +function set_datatype(data, gpu) + if gpu >= 0 then + data = data:cuda() + else + data = data:double() + end + return data +end + + local params = cmd:parse(arg) main(params) From cd5c6e5a1ec7b9e049850bd43415e66c086bd90e Mon Sep 17 00:00:00 2001 From: ProGamerGov Date: Sat, 11 Feb 2017 16:48:33 -0700 Subject: [PATCH 04/13] Update neural_style.lua --- neural_style.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/neural_style.lua b/neural_style.lua index 68298dd..9464c09 100644 --- a/neural_style.lua +++ b/neural_style.lua @@ -56,6 +56,7 @@ local function main(params) local loadcaffe_backend = params.backend if params.backend == 'clnn' then loadcaffe_backend = 'nn' end local cnn = loadcaffe.load(params.proto_file, params.model_file, loadcaffe_backend):type(dtype) + cnn = set_datatype(cnn, params.gpu) local content_image = image.load(params.content_image, 3) content_image = image.scale(content_image, params.image_size, 'bilinear') From 80f527f58455f3ef0a8f213c9077660fc0862c81 Mon Sep 17 00:00:00 2001 From: ProGamerGov Date: Sat, 11 Feb 2017 18:06:42 -0700 Subject: [PATCH 05/13] Update neural_style.lua --- neural_style.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/neural_style.lua b/neural_style.lua index 9464c09..68298dd 100644 --- a/neural_style.lua +++ b/neural_style.lua @@ -56,7 +56,6 @@ local function main(params) local loadcaffe_backend = params.backend if params.backend == 'clnn' then loadcaffe_backend = 'nn' end local cnn = loadcaffe.load(params.proto_file, params.model_file, loadcaffe_backend):type(dtype) - cnn = set_datatype(cnn, params.gpu) local content_image = image.load(params.content_image, 3) content_image = image.scale(content_image, params.image_size, 'bilinear') From 6814479c8ebcc11498b7c123ee2ba7ef9f0fe09f Mon Sep 17 00:00:00 2001 From: ProGamerGov Date: Sun, 12 Feb 2017 13:12:20 -0700 Subject: [PATCH 06/13] Update neural_style.lua --- neural_style.lua | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/neural_style.lua b/neural_style.lua index 68298dd..5860708 100644 --- a/neural_style.lua +++ b/neural_style.lua @@ -118,6 +118,16 @@ local function main(params) local layer = cnn:get(i) local name = layer.name local layer_type = torch.type(layer) + --reflectance padding option from leongatys/NeuralImageSynthesis + local is_convolution = (layer_type == 'cudnn.SpatialConvolution' or layer_type == 'nn.SpatialConvolution') + if is_convolution and params.reflectance then + local padW, padH = layer.padW, layer.padH + local pad_layer = nn.SpatialReflectionPadding(padW, padW, padH, padH) + pad_layer = set_datatype(pad_layer, params.gpu) + net:add(pad_layer) + layer.padW = 0 + layer.padH = 0 + end local is_pooling = (layer_type == 'cudnn.SpatialMaxPooling' or layer_type == 'nn.SpatialMaxPooling') if is_pooling and params.pooling == 'avg' then assert(layer.padW == 0 and layer.padH == 0) @@ -129,17 +139,7 @@ local function main(params) net:add(avg_pool_layer) else net:add(layer) - end - --reflectance padding option from leongatys/NeuralImageSynthesis - local is_convolution = (layer_type == 'cudnn.SpatialConvolution' or layer_type == 'nn.SpatialConvolution') - if is_convolution and params.reflectance then - local padW, padH = layer.padW, layer.padH - local pad_layer = nn.SpatialReflectionPadding(padW, padW, padH, padH) - pad_layer = set_datatype(pad_layer, params.gpu) - net:add(pad_layer) - layer.padW = 0 - layer.padH = 0 - end + end if name == content_layers[next_content_idx] then print("Setting up content layer", i, ":", layer.name) local norm = params.normalize_gradients From 3cf346cac9b2ebdbf50a6e7b23a08670f7887085 Mon Sep 17 00:00:00 2001 From: ProGamerGov Date: Tue, 14 Feb 2017 09:58:48 -0700 Subject: [PATCH 07/13] Removed unneeded code --- neural_style.lua | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/neural_style.lua b/neural_style.lua index 5860708..19a2947 100644 --- a/neural_style.lua +++ b/neural_style.lua @@ -122,8 +122,7 @@ local function main(params) local is_convolution = (layer_type == 'cudnn.SpatialConvolution' or layer_type == 'nn.SpatialConvolution') if is_convolution and params.reflectance then local padW, padH = layer.padW, layer.padH - local pad_layer = nn.SpatialReflectionPadding(padW, padW, padH, padH) - pad_layer = set_datatype(pad_layer, params.gpu) + local pad_layer = nn.SpatialReflectionPadding(padW, padW, padH, padH):type(dtype) net:add(pad_layer) layer.padW = 0 layer.padH = 0 @@ -607,17 +606,6 @@ function TVLoss:updateGradInput(input, gradOutput) return self.gradInput end --- Function to set gpu/cpu datatype -function set_datatype(data, gpu) - if gpu >= 0 then - data = data:cuda() - else - data = data:double() - end - return data -end - - local params = cmd:parse(arg) main(params) From fdd059b15cc5c0425ec6d260083a0e35168e5593 Mon Sep 17 00:00:00 2001 From: ProGamerGov Date: Tue, 14 Feb 2017 15:17:31 -0700 Subject: [PATCH 08/13] -reflectance is now -padding --- neural_style.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/neural_style.lua b/neural_style.lua index 19a2947..8996cd7 100644 --- a/neural_style.lua +++ b/neural_style.lua @@ -48,7 +48,7 @@ cmd:option('-seed', -1) cmd:option('-content_layers', 'relu4_2', 'layers for content') cmd:option('-style_layers', 'relu1_1,relu2_1,relu3_1,relu4_1,relu5_1', 'layers for style') -cmd:option('-reflectance', false, 'if true, use reflectance padding') +cmd:option('-padding', 'default', 'default|reflect') local function main(params) local dtype, multigpu = setup_gpu(params) @@ -120,7 +120,7 @@ local function main(params) local layer_type = torch.type(layer) --reflectance padding option from leongatys/NeuralImageSynthesis local is_convolution = (layer_type == 'cudnn.SpatialConvolution' or layer_type == 'nn.SpatialConvolution') - if is_convolution and params.reflectance then + if is_convolution and params.reflectance == 'reflect' then local padW, padH = layer.padW, layer.padH local pad_layer = nn.SpatialReflectionPadding(padW, padW, padH, padH):type(dtype) net:add(pad_layer) From 166daa24cd75a8cea8dcf41a3de132ef6a2d6480 Mon Sep 17 00:00:00 2001 From: ProGamerGov Date: Tue, 14 Feb 2017 18:15:39 -0700 Subject: [PATCH 09/13] Update neural_style.lua --- neural_style.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neural_style.lua b/neural_style.lua index 8996cd7..fc77ab8 100644 --- a/neural_style.lua +++ b/neural_style.lua @@ -120,7 +120,7 @@ local function main(params) local layer_type = torch.type(layer) --reflectance padding option from leongatys/NeuralImageSynthesis local is_convolution = (layer_type == 'cudnn.SpatialConvolution' or layer_type == 'nn.SpatialConvolution') - if is_convolution and params.reflectance == 'reflect' then + if is_convolution and params.padding == 'reflect' then local padW, padH = layer.padW, layer.padH local pad_layer = nn.SpatialReflectionPadding(padW, padW, padH, padH):type(dtype) net:add(pad_layer) From 4bcf625b6e478d35b00901f1fa0f8b80f94a90cd Mon Sep 17 00:00:00 2001 From: ProGamerGov Date: Wed, 15 Feb 2017 10:13:29 -0700 Subject: [PATCH 10/13] Updated to include "replicate" padding option As per htoyryla's comment here: https://github.com/jcjohnson/neural-style/issues/376#issuecomment-279926009 --- neural_style.lua | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/neural_style.lua b/neural_style.lua index fc77ab8..a799839 100644 --- a/neural_style.lua +++ b/neural_style.lua @@ -120,13 +120,19 @@ local function main(params) local layer_type = torch.type(layer) --reflectance padding option from leongatys/NeuralImageSynthesis local is_convolution = (layer_type == 'cudnn.SpatialConvolution' or layer_type == 'nn.SpatialConvolution') - if is_convolution and params.padding == 'reflect' then - local padW, padH = layer.padW, layer.padH - local pad_layer = nn.SpatialReflectionPadding(padW, padW, padH, padH):type(dtype) - net:add(pad_layer) - layer.padW = 0 - layer.padH = 0 - end + if is_convolution and params.padding ~= 'default' then + local padW, padH = layer.padW, layer.padH + if params.padding == 'reflect' then + local pad_layer = nn.SpatialReflectionPadding(padW, padW, padH, padH):type(dtype) + elseif params.padding == 'replicate' then + local pad_layer = nn.SpatialReplicationPadding(padW, padW, padH, padH):type(dtype) + else + error('Unknown padding type') + end + net:add(pad_layer) + layer.padW = 0 + layer.padH = 0 + end local is_pooling = (layer_type == 'cudnn.SpatialMaxPooling' or layer_type == 'nn.SpatialMaxPooling') if is_pooling and params.pooling == 'avg' then assert(layer.padW == 0 and layer.padH == 0) From cb39e5f2ad483b2f7a3972dea39417dd7558e332 Mon Sep 17 00:00:00 2001 From: ProGamerGov Date: Sun, 26 Feb 2017 11:45:14 -0700 Subject: [PATCH 11/13] Formatting fix --- neural_style.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neural_style.lua b/neural_style.lua index a799839..65fd4c8 100644 --- a/neural_style.lua +++ b/neural_style.lua @@ -128,7 +128,7 @@ local function main(params) local pad_layer = nn.SpatialReplicationPadding(padW, padW, padH, padH):type(dtype) else error('Unknown padding type') - end + end net:add(pad_layer) layer.padW = 0 layer.padH = 0 From 1d65060faadd977ef172f20fce0ed4280719a315 Mon Sep 17 00:00:00 2001 From: ProGamerGov Date: Sun, 26 Feb 2017 11:45:56 -0700 Subject: [PATCH 12/13] formatting fix --- neural_style.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/neural_style.lua b/neural_style.lua index 65fd4c8..3e58933 100644 --- a/neural_style.lua +++ b/neural_style.lua @@ -129,9 +129,9 @@ local function main(params) else error('Unknown padding type') end - net:add(pad_layer) - layer.padW = 0 - layer.padH = 0 + net:add(pad_layer) + layer.padW = 0 + layer.padH = 0 end local is_pooling = (layer_type == 'cudnn.SpatialMaxPooling' or layer_type == 'nn.SpatialMaxPooling') if is_pooling and params.pooling == 'avg' then From 722181b9e1e7b9958ff0d8c19b998f12060e0424 Mon Sep 17 00:00:00 2001 From: ProGamerGov Date: Sun, 26 Feb 2017 14:56:42 -0700 Subject: [PATCH 13/13] error fix --- neural_style.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/neural_style.lua b/neural_style.lua index 3e58933..1564d80 100644 --- a/neural_style.lua +++ b/neural_style.lua @@ -124,12 +124,13 @@ local function main(params) local padW, padH = layer.padW, layer.padH if params.padding == 'reflect' then local pad_layer = nn.SpatialReflectionPadding(padW, padW, padH, padH):type(dtype) + net:add(pad_layer) elseif params.padding == 'replicate' then local pad_layer = nn.SpatialReplicationPadding(padW, padW, padH, padH):type(dtype) + net:add(pad_layer) else error('Unknown padding type') end - net:add(pad_layer) layer.padW = 0 layer.padH = 0 end