Browse Source

Remove -upsampling_filter option

nagadomi 9 years ago
parent
commit
01b2e6d441
7 changed files with 31 additions and 43 deletions
  1. 4 5
      lib/pairwise_transform_jpeg_scale.lua
  2. 2 2
      lib/pairwise_transform_scale.lua
  3. 10 14
      lib/reconstruct.lua
  4. 0 1
      lib/settings.lua
  5. 1 5
      train.lua
  6. 10 11
      waifu2x.lua
  7. 4 5
      web.lua

+ 4 - 5
lib/pairwise_transform_jpeg_scale.lua

@@ -28,9 +28,9 @@ local function add_jpeg_noise(src, style, level, options)
       elseif level == 2 or level == 3 then
 	 -- level 2/3 adjusting by -nr_rate. for level3, -nr_rate=1
 	 local r = torch.uniform()
-	 if r > 0.6 then
+	 if r > 0.4 then
 	    return add_jpeg_noise_(src, {torch.random(27, 70)}, options)
-	 elseif r > 0.3 then
+	 elseif r > 0.1 then
 	    local quality1 = torch.random(37, 70)
 	    local quality2 = quality1 - torch.random(5, 10)
 	    return add_jpeg_noise_(src, {quality1, quality2}, options)
@@ -38,7 +38,6 @@ local function add_jpeg_noise(src, style, level, options)
 	    local quality1 = torch.random(52, 70)
 	    local quality2 = quality1 - torch.random(5, 15)
 	    local quality3 = quality1 - torch.random(15, 25)
-
 	    return add_jpeg_noise_(src, {quality1, quality2, quality3}, options)
 	 end
       else
@@ -68,7 +67,7 @@ function pairwise_transform.jpeg_scale(src, scale, style, noise_level, size, off
       local small = iproc.scale_with_gamma22(y, y:size(3) * down_scale,
 					     y:size(2) * down_scale, downsampling_filter, blur)
       if options.x_upsampling then
-	 x = iproc.scale(small, y:size(3), y:size(2), options.upsampling_filter)
+	 x = iproc.scale(small, y:size(3), y:size(2), "Box")
       else
 	 x = small
       end
@@ -76,7 +75,7 @@ function pairwise_transform.jpeg_scale(src, scale, style, noise_level, size, off
       local small = iproc.scale(y, y:size(3) * down_scale,
 				  y:size(2) * down_scale, downsampling_filter, blur)
       if options.x_upsampling then
-	 x = iproc.scale(small, y:size(3), y:size(2), options.upsampling_filter)
+	 x = iproc.scale(small, y:size(3), y:size(2), "Box")
       else
 	 x = small
       end

+ 2 - 2
lib/pairwise_transform_scale.lua

@@ -19,7 +19,7 @@ function pairwise_transform.scale(src, scale, size, offset, n, options)
       local small = iproc.scale_with_gamma22(y, y:size(3) * down_scale,
 					     y:size(2) * down_scale, downsampling_filter, blur)
       if options.x_upsampling then
-	 x = iproc.scale(small, y:size(3), y:size(2), options.upsampling_filter)
+	 x = iproc.scale(small, y:size(3), y:size(2), "Box")
       else
 	 x = small
       end
@@ -27,7 +27,7 @@ function pairwise_transform.scale(src, scale, size, offset, n, options)
       local small = iproc.scale(y, y:size(3) * down_scale,
 				  y:size(2) * down_scale, downsampling_filter, blur)
       if options.x_upsampling then
-	 x = iproc.scale(small, y:size(3), y:size(2), options.upsampling_filter)
+	 x = iproc.scale(small, y:size(3), y:size(2), "Box")
       else
 	 x = small
       end

+ 10 - 14
lib/reconstruct.lua

@@ -114,15 +114,14 @@ function reconstruct.image_y(model, x, offset, block_size, batch_size)
    
    return output
 end
-function reconstruct.scale_y(model, scale, x, offset, block_size, batch_size, upsampling_filter)
-   upsampling_filter = upsampling_filter or "Box"
+function reconstruct.scale_y(model, scale, x, offset, block_size, batch_size)
    block_size = block_size or 128
    local x_lanczos
    if reconstruct.has_resize(model) then
       x_lanczos = iproc.scale(x, x:size(3) * scale, x:size(2) * scale, "Lanczos")
    else
       x_lanczos = iproc.scale(x, x:size(3) * scale, x:size(2) * scale, "Lanczos")
-      x = iproc.scale(x, x:size(3) * scale, x:size(2) * scale, upsampling_filter)
+      x = iproc.scale(x, x:size(3) * scale, x:size(2) * scale, "Box")
    end
    local p = padding_params(x, model, block_size)
    if p.x_w * p.x_h > 2048*2048 then
@@ -162,11 +161,10 @@ function reconstruct.image_rgb(model, x, offset, block_size, batch_size)
 
    return output
 end
-function reconstruct.scale_rgb(model, scale, x, offset, block_size, batch_size, upsampling_filter)
-   upsampling_filter = upsampling_filter or "Box"
+function reconstruct.scale_rgb(model, scale, x, offset, block_size, batch_size)
    block_size = block_size or 128
    if not reconstruct.has_resize(model) then
-      x = iproc.scale(x, x:size(3) * scale, x:size(2) * scale, upsampling_filter)
+      x = iproc.scale(x, x:size(3) * scale, x:size(2) * scale, "Box")
    end
    local p = padding_params(x, model, block_size)
    x = iproc.padding(x, p.pad_w1, p.pad_w2, p.pad_h1, p.pad_h2)
@@ -206,7 +204,7 @@ function reconstruct.image(model, x, block_size)
    end
    return x
 end
-function reconstruct.scale(model, scale, x, block_size, upsampling_filter)
+function reconstruct.scale(model, scale, x, block_size)
    local i2rgb = false
    if x:size(1) == 1 then
       local new_x = torch.Tensor(3, x:size(2), x:size(3))
@@ -219,13 +217,11 @@ function reconstruct.scale(model, scale, x, block_size, upsampling_filter)
    if reconstruct.is_rgb(model) then
       x = reconstruct.scale_rgb(model, scale, x,
 				reconstruct.offset_size(model),
-				block_size,
-				upsampling_filter)
+				block_size)
    else
       x = reconstruct.scale_y(model, scale, x,
 			      reconstruct.offset_size(model),
-			      block_size,
-			      upsampling_filter)
+			      block_size)
    end
    if i2rgb then
       x = image.rgb2y(x)
@@ -305,15 +301,15 @@ function reconstruct.image_tta(model, n, x, block_size)
       return tta(reconstruct.image_y, n, model, x, block_size)
    end
 end
-function reconstruct.scale_tta(model, n, scale, x, block_size, upsampling_filter)
+function reconstruct.scale_tta(model, n, scale, x, block_size)
    if reconstruct.is_rgb(model) then
       local f = function (model, x, offset, block_size)
-	 return reconstruct.scale_rgb(model, scale, x, offset, block_size, upsampling_filter)
+	 return reconstruct.scale_rgb(model, scale, x, offset, block_size)
       end
       return tta(f, n, model, x, block_size)
    else
       local f = function (model, x, offset, block_size)
-	 return reconstruct.scale_y(model, scale, x, offset, block_size, upsampling_filter)
+	 return reconstruct.scale_y(model, scale, x, offset, block_size)
       end
       return tta(f, n, model, x, block_size)
    end

+ 0 - 1
lib/settings.lua

@@ -51,7 +51,6 @@ cmd:option("-save_history", 0, 'save all model (0|1)')
 cmd:option("-plot", 0, 'plot loss chart(0|1)')
 cmd:option("-downsampling_filters", "Box,Lanczos,Sinc", '(comma separated)downsampling filters for 2x scale training. (Point,Box,Triangle,Hermite,Hanning,Hamming,Blackman,Gaussian,Quadratic,Cubic,Catrom,Mitchell,Lanczos,Bessel,Sinc)')
 cmd:option("-gamma_correction", 0, 'Resizing with colorspace correction(sRGB:gamma 2.2) in scale training (0|1)')
-cmd:option("-upsampling_filter", "Box", 'upsampling filter for 2x scale training (dev)')
 cmd:option("-max_training_image_size", -1, 'if training image is larger than N, image will be crop randomly when data converting')
 cmd:option("-use_transparent_png", 0, 'use transparent png (0|1)')
 cmd:option("-resize_blur_min", 0.95, 'min blur parameter for ResizeImage')

+ 1 - 5
train.lua

@@ -15,9 +15,7 @@ local pairwise_transform = require 'pairwise_transform'
 local image_loader = require 'image_loader'
 
 local function save_test_scale(model, rgb, file)
-   local up = reconstruct.scale(model, settings.scale, rgb,
-				settings.scale * settings.crop_size,
-				settings.upsampling_filter)
+   local up = reconstruct.scale(model, settings.scale, rgb)
    image.save(file, up)
 end
 local function save_test_jpeg(model, rgb, file)
@@ -144,7 +142,6 @@ local function transformer(model, x, is_validation, n, offset)
    if settings.method == "scale" then
       local conf = tablex.update({
 	    downsampling_filters = settings.downsampling_filters,
-	    upsampling_filter = settings.upsampling_filter,
 	    random_half_rate = settings.random_half_rate,
 	    random_color_noise_rate = random_color_noise_rate,
 	    random_overlay_rate = random_overlay_rate,
@@ -181,7 +178,6 @@ local function transformer(model, x, is_validation, n, offset)
    elseif settings.method == "noise_scale" then
       local conf = tablex.update({
 	    downsampling_filters = settings.downsampling_filters,
-	    upsampling_filter = settings.upsampling_filter,
 	    random_half_rate = settings.random_half_rate,
 	    random_color_noise_rate = random_color_noise_rate,
 	    random_overlay_rate = random_overlay_rate,

+ 10 - 11
waifu2x.lua

@@ -44,9 +44,9 @@ local function convert_image(opt)
    local scale_f, image_f
 
    if opt.tta == 1 then
-      scale_f = function(model, scale, x, block_size, batch_size, batupsampling_filter)
+      scale_f = function(model, scale, x, block_size, batch_size)
 	 return reconstruct.scale_tta(model, opt.tta_level,
-				      scale, x, block_size, batch_size, upsampling_filter)
+				      scale, x, block_size, batch_size)
       end
       image_f = function(model, x, block_size, batch_size)
 	 return reconstruct.image_tta(model, opt.tta_level,
@@ -75,7 +75,7 @@ local function convert_image(opt)
       end
       local t = sys.clock()
       x = alpha_util.make_border(x, alpha, reconstruct.offset_size(model))
-      new_x = scale_f(model, opt.scale, x, opt.crop_size, opt.batch_size, opt.batch_size, opt.upsampling_filter)
+      new_x = scale_f(model, opt.scale, x, opt.crop_size, opt.batch_size, opt.batch_size)
       new_x = alpha_util.composite(new_x, alpha, model)
       print(opt.o .. ": " .. (sys.clock() - t) .. " sec")
    elseif opt.m == "noise_scale" then
@@ -92,7 +92,7 @@ local function convert_image(opt)
 	 end
 	 local t = sys.clock()
 	 x = alpha_util.make_border(x, alpha, reconstruct.offset_size(scale_model))
-	 new_x = scale_f(model, opt.scale, x, opt.crop_size, opt.batch_size, opt.upsampling_filter)
+	 new_x = scale_f(model, opt.scale, x, opt.crop_size, opt.batch_size)
 	 new_x = alpha_util.composite(new_x, alpha, scale_model)
 	 print(opt.o .. ": " .. (sys.clock() - t) .. " sec")
       else
@@ -110,7 +110,7 @@ local function convert_image(opt)
 	 local t = sys.clock()
 	 x = alpha_util.make_border(x, alpha, reconstruct.offset_size(scale_model))
 	 x = image_f(noise_model, x, opt.crop_size, opt.batch_size)
-	 new_x = scale_f(scale_model, opt.scale, x, opt.crop_size, opt.batch_size, opt.upsampling_filter)
+	 new_x = scale_f(scale_model, opt.scale, x, opt.crop_size, opt.batch_size)
 	 new_x = alpha_util.composite(new_x, alpha, scale_model)
 	 print(opt.o .. ": " .. (sys.clock() - t) .. " sec")
       end
@@ -125,9 +125,9 @@ local function convert_frames(opt)
    local noise_model = {}
    local scale_f, image_f
    if opt.tta == 1 then
-      scale_f = function(model, scale, x, block_size, batch_size, upsampling_filter)
+      scale_f = function(model, scale, x, block_size, batch_size)
 	 return reconstruct.scale_tta(model, opt.tta_level,
-				      scale, x, block_size, batch_size, upsampling_filter)
+				      scale, x, block_size, batch_size)
       end
       image_f = function(model, x, block_size, batch_size)
 	 return reconstruct.image_tta(model, opt.tta_level,
@@ -195,15 +195,15 @@ local function convert_frames(opt)
 	    new_x = alpha_util.composite(new_x, alpha)
 	 elseif opt.m == "scale" then
 	    x = alpha_util.make_border(x, alpha, reconstruct.offset_size(scale_model))
-	    new_x = scale_f(scale_model, opt.scale, x, opt.crop_size, opt.batch_size, opt.upsampling_filter)
+	    new_x = scale_f(scale_model, opt.scale, x, opt.crop_size, opt.batch_size)
 	    new_x = alpha_util.composite(new_x, alpha, scale_model)
 	 elseif opt.m == "noise_scale" then
 	    x = alpha_util.make_border(x, alpha, reconstruct.offset_size(scale_model))
 	    if noise_scale_model[opt.noise_level] then
-	       new_x = scale_f(noise_scale_model[opt.noise_level], opt.scale, x, opt.crop_size, opt.batch_size, upsampling_filter)
+	       new_x = scale_f(noise_scale_model[opt.noise_level], opt.scale, x, opt.crop_size, opt.batch_size)
 	    else
 	       x = image_f(noise_model[opt.noise_level], x, opt.crop_size, opt.batch_size)
-	       new_x = scale_f(scale_model, opt.scale, x, opt.crop_size, opt.batch_size, upsampling_filter)
+	       new_x = scale_f(scale_model, opt.scale, x, opt.crop_size, opt.batch_size)
 	    end
 	    new_x = alpha_util.composite(new_x, alpha, scale_model)
 	 else
@@ -239,7 +239,6 @@ local function waifu2x()
    cmd:option("-thread", -1, "number of CPU threads")
    cmd:option("-tta", 0, '8x slower and slightly high quality (0|1)')
    cmd:option("-tta_level", 8, 'TTA level (2|4|8)')
-   cmd:option("-upsampling_filter", "Box", 'upsampling filter (for dev)')
    
    local opt = cmd:parse(arg)
    if opt.thread > 0 then

+ 4 - 5
web.lua

@@ -25,7 +25,6 @@ cmd:text("waifu2x-api")
 cmd:text("Options:")
 cmd:option("-port", 8812, 'listen port')
 cmd:option("-gpu", 1, 'Device ID')
-cmd:option("-upsampling_filter", "Box", 'Upsampling filter (for dev)')
 cmd:option("-crop_size", 128, 'patch size per process')
 cmd:option("-batch_size", 1, 'batch size')
 cmd:option("-thread", -1, 'number of CPU threads')
@@ -149,11 +148,11 @@ local function convert(x, meta, options)
 	 end
 	 if options.method == "scale" then
 	    x = reconstruct.scale(art_scale2_model, 2.0, x,
-				  opt.crop_size, opt.batch_size, opt.upsampling_filter)
+				  opt.crop_size, opt.batch_size)
 	    if alpha then
 	       if not (alpha:size(2) == x:size(2) and alpha:size(3) == x:size(3)) then
 		  alpha = reconstruct.scale(art_scale2_model, 2.0, alpha,
-					    opt.crop_size, opt.batch_size, opt.upsampling_filter)
+					    opt.crop_size, opt.batch_size)
 		  image_loader.save_png(alpha_cache_file, alpha)
 	       end
 	    end
@@ -174,11 +173,11 @@ local function convert(x, meta, options)
 	 end
 	 if options.method == "scale" then
 	    x = reconstruct.scale(photo_scale2_model, 2.0, x,
-				  opt.crop_size, opt.batch_size, opt.upsampling_filter)
+				  opt.crop_size, opt.batch_size)
 	    if alpha then
 	       if not (alpha:size(2) == x:size(2) and alpha:size(3) == x:size(3)) then
 		  alpha = reconstruct.scale(photo_scale2_model, 2.0, alpha,
-					    opt.crop_size, opt.batch_size, opt.upsampling_filter)
+					    opt.crop_size, opt.batch_size)
 		  image_loader.save_png(alpha_cache_file, alpha)
 	       end
 	    end