2015-11-08 22:31:46 +13:00
|
|
|
require 'pl'
|
2015-10-28 19:30:47 +13:00
|
|
|
local __FILE__ = (function() return string.gsub(debug.getinfo(2, 'S').source, "^@", "") end)()
|
|
|
|
package.path = path.join(path.dirname(__FILE__), "lib", "?.lua;") .. package.path
|
2015-05-16 17:48:05 +12:00
|
|
|
require 'sys'
|
2015-10-28 19:30:47 +13:00
|
|
|
require 'w2nn'
|
|
|
|
local iproc = require 'iproc'
|
|
|
|
local reconstruct = require 'reconstruct'
|
|
|
|
local image_loader = require 'image_loader'
|
2015-05-16 17:48:05 +12:00
|
|
|
|
|
|
|
torch.setdefaulttensortype('torch.FloatTensor')
|
|
|
|
|
2015-05-25 01:09:42 +12:00
|
|
|
local function convert_image(opt)
|
2015-06-16 23:41:48 +12:00
|
|
|
local x, alpha = image_loader.load_float(opt.i)
|
2015-05-25 01:09:42 +12:00
|
|
|
local new_x = nil
|
|
|
|
local t = sys.clock()
|
2015-11-09 08:01:28 +13:00
|
|
|
local scale_f, image_f
|
|
|
|
if opt.tta == 1 then
|
|
|
|
scale_f = reconstruct.scale_tta
|
|
|
|
image_f = reconstruct.image_tta
|
|
|
|
else
|
|
|
|
scale_f = reconstruct.scale
|
|
|
|
image_f = reconstruct.image
|
|
|
|
end
|
2015-05-16 17:48:05 +12:00
|
|
|
if opt.o == "(auto)" then
|
|
|
|
local name = path.basename(opt.i)
|
|
|
|
local e = path.extension(name)
|
|
|
|
local base = name:sub(0, name:len() - e:len())
|
2015-11-05 20:17:55 +13:00
|
|
|
opt.o = path.join(path.dirname(opt.i), string.format("%s_%s.png", base, opt.m))
|
2015-05-16 17:48:05 +12:00
|
|
|
end
|
|
|
|
if opt.m == "noise" then
|
2015-11-05 20:17:55 +13:00
|
|
|
local model_path = path.join(opt.model_dir, ("noise%d_model.t7"):format(opt.noise_level))
|
|
|
|
local model = torch.load(model_path, "ascii")
|
|
|
|
if not model then
|
|
|
|
error("Load Error: " .. model_path)
|
|
|
|
end
|
2015-11-09 08:01:28 +13:00
|
|
|
new_x = image_f(model, x, opt.crop_size)
|
2015-05-16 17:48:05 +12:00
|
|
|
elseif opt.m == "scale" then
|
2015-11-05 20:17:55 +13:00
|
|
|
local model_path = path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale))
|
|
|
|
local model = torch.load(model_path, "ascii")
|
|
|
|
if not model then
|
|
|
|
error("Load Error: " .. model_path)
|
|
|
|
end
|
2015-11-09 08:01:28 +13:00
|
|
|
new_x = scale_f(model, opt.scale, x, opt.crop_size)
|
2015-05-16 17:48:05 +12:00
|
|
|
elseif opt.m == "noise_scale" then
|
2015-11-05 20:17:55 +13:00
|
|
|
local noise_model_path = path.join(opt.model_dir, ("noise%d_model.t7"):format(opt.noise_level))
|
|
|
|
local noise_model = torch.load(noise_model_path, "ascii")
|
|
|
|
local scale_model_path = path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale))
|
|
|
|
local scale_model = torch.load(scale_model_path, "ascii")
|
|
|
|
|
|
|
|
if not noise_model then
|
|
|
|
error("Load Error: " .. noise_model_path)
|
|
|
|
end
|
|
|
|
if not scale_model then
|
|
|
|
error("Load Error: " .. scale_model_path)
|
|
|
|
end
|
2015-11-09 08:10:51 +13:00
|
|
|
x = image_f(noise_model, x, opt.crop_size)
|
2015-11-09 08:01:28 +13:00
|
|
|
new_x = scale_f(scale_model, opt.scale, x, opt.crop_size)
|
2015-05-16 17:48:05 +12:00
|
|
|
else
|
|
|
|
error("undefined method:" .. opt.method)
|
|
|
|
end
|
2015-11-11 12:00:50 +13:00
|
|
|
if opt.white_noise == 1 then
|
|
|
|
new_x = iproc.white_noise(new_x, opt.white_noise_std, {1.0, 0.8, 1.0})
|
|
|
|
end
|
2015-11-08 03:01:57 +13:00
|
|
|
image_loader.save_png(opt.o, new_x, alpha, opt.depth)
|
2015-05-16 17:48:05 +12:00
|
|
|
print(opt.o .. ": " .. (sys.clock() - t) .. " sec")
|
|
|
|
end
|
2015-05-25 01:09:42 +12:00
|
|
|
local function convert_frames(opt)
|
2015-11-09 08:01:28 +13:00
|
|
|
local model_path, noise1_model, noise2_model, scale_model
|
|
|
|
local scale_f, image_f
|
|
|
|
if opt.tta == 1 then
|
|
|
|
scale_f = reconstruct.scale_tta
|
|
|
|
image_f = reconstruct.image_tta
|
|
|
|
else
|
|
|
|
scale_f = reconstruct.scale
|
|
|
|
image_f = reconstruct.image
|
|
|
|
end
|
2015-11-05 20:17:55 +13:00
|
|
|
if opt.m == "scale" then
|
2015-11-09 08:01:28 +13:00
|
|
|
model_path = path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale))
|
2015-11-05 20:17:55 +13:00
|
|
|
scale_model = torch.load(model_path, "ascii")
|
|
|
|
if not scale_model then
|
|
|
|
error("Load Error: " .. model_path)
|
|
|
|
end
|
|
|
|
elseif opt.m == "noise" and opt.noise_level == 1 then
|
2015-11-09 08:01:28 +13:00
|
|
|
model_path = path.join(opt.model_dir, "noise1_model.t7")
|
2015-11-05 20:17:55 +13:00
|
|
|
noise1_model = torch.load(model_path, "ascii")
|
|
|
|
if not noise1_model then
|
|
|
|
error("Load Error: " .. model_path)
|
|
|
|
end
|
|
|
|
elseif opt.m == "noise" and opt.noise_level == 2 then
|
2015-11-09 08:01:28 +13:00
|
|
|
model_path = path.join(opt.model_dir, "noise2_model.t7")
|
2015-11-05 20:17:55 +13:00
|
|
|
noise2_model = torch.load(model_path, "ascii")
|
|
|
|
if not noise2_model then
|
|
|
|
error("Load Error: " .. model_path)
|
|
|
|
end
|
2015-11-09 08:01:28 +13:00
|
|
|
elseif opt.m == "noise_scale" then
|
|
|
|
model_path = path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale))
|
|
|
|
scale_model = torch.load(model_path, "ascii")
|
|
|
|
if not scale_model then
|
|
|
|
error("Load Error: " .. model_path)
|
|
|
|
end
|
|
|
|
if opt.noise_level == 1 then
|
|
|
|
model_path = path.join(opt.model_dir, "noise1_model.t7")
|
|
|
|
noise1_model = torch.load(model_path, "ascii")
|
|
|
|
if not noise1_model then
|
|
|
|
error("Load Error: " .. model_path)
|
|
|
|
end
|
|
|
|
elseif opt.noise_level == 2 then
|
|
|
|
model_path = path.join(opt.model_dir, "noise2_model.t7")
|
|
|
|
noise2_model = torch.load(model_path, "ascii")
|
|
|
|
if not noise2_model then
|
|
|
|
error("Load Error: " .. model_path)
|
|
|
|
end
|
|
|
|
end
|
2015-11-05 20:17:55 +13:00
|
|
|
end
|
2015-05-25 01:09:42 +12:00
|
|
|
local fp = io.open(opt.l)
|
2015-11-05 20:17:55 +13:00
|
|
|
if not fp then
|
|
|
|
error("Open Error: " .. opt.l)
|
|
|
|
end
|
2015-05-25 01:09:42 +12:00
|
|
|
local count = 0
|
|
|
|
local lines = {}
|
|
|
|
for line in fp:lines() do
|
|
|
|
table.insert(lines, line)
|
|
|
|
end
|
|
|
|
fp:close()
|
|
|
|
for i = 1, #lines do
|
2015-06-16 23:41:48 +12:00
|
|
|
if opt.resume == 0 or path.exists(string.format(opt.o, i)) == false then
|
|
|
|
local x, alpha = image_loader.load_float(lines[i])
|
|
|
|
local new_x = nil
|
|
|
|
if opt.m == "noise" and opt.noise_level == 1 then
|
2015-11-09 08:01:28 +13:00
|
|
|
new_x = image_f(noise1_model, x, opt.crop_size)
|
2015-06-16 23:41:48 +12:00
|
|
|
elseif opt.m == "noise" and opt.noise_level == 2 then
|
2015-11-09 08:10:51 +13:00
|
|
|
new_x = image_func(noise2_model, x, opt.crop_size)
|
2015-06-16 23:41:48 +12:00
|
|
|
elseif opt.m == "scale" then
|
2015-11-09 08:01:28 +13:00
|
|
|
new_x = scale_f(scale_model, opt.scale, x, opt.crop_size)
|
2015-06-16 23:41:48 +12:00
|
|
|
elseif opt.m == "noise_scale" and opt.noise_level == 1 then
|
2015-11-09 08:10:51 +13:00
|
|
|
x = image_f(noise1_model, x, opt.crop_size)
|
2015-11-09 08:01:28 +13:00
|
|
|
new_x = scale_f(scale_model, opt.scale, x, opt.crop_size)
|
2015-06-16 23:41:48 +12:00
|
|
|
elseif opt.m == "noise_scale" and opt.noise_level == 2 then
|
2015-11-09 08:01:28 +13:00
|
|
|
x = image_f(noise2_model, x, opt.crop_size)
|
|
|
|
new_x = scale_f(scale_model, opt.scale, x, opt.crop_size)
|
2015-06-16 23:41:48 +12:00
|
|
|
else
|
|
|
|
error("undefined method:" .. opt.method)
|
|
|
|
end
|
2015-11-11 12:00:50 +13:00
|
|
|
if opt.white_noise == 1 then
|
|
|
|
new_x = iproc.white_noise(new_x, opt.white_noise_std, {1.0, 0.8, 1.0})
|
|
|
|
end
|
|
|
|
|
2015-06-16 23:41:48 +12:00
|
|
|
local output = nil
|
|
|
|
if opt.o == "(auto)" then
|
|
|
|
local name = path.basename(lines[i])
|
|
|
|
local e = path.extension(name)
|
|
|
|
local base = name:sub(0, name:len() - e:len())
|
|
|
|
output = path.join(path.dirname(opt.i), string.format("%s(%s).png", base, opt.m))
|
|
|
|
else
|
|
|
|
output = string.format(opt.o, i)
|
|
|
|
end
|
2015-11-08 03:01:57 +13:00
|
|
|
image_loader.save_png(output, new_x, alpha, opt.depth)
|
2015-06-16 23:41:48 +12:00
|
|
|
xlua.progress(i, #lines)
|
|
|
|
if i % 10 == 0 then
|
|
|
|
collectgarbage()
|
|
|
|
end
|
|
|
|
else
|
|
|
|
xlua.progress(i, #lines)
|
|
|
|
end
|
2015-05-26 02:22:29 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-25 01:09:42 +12:00
|
|
|
local function waifu2x()
|
|
|
|
local cmd = torch.CmdLine()
|
|
|
|
cmd:text()
|
|
|
|
cmd:text("waifu2x")
|
|
|
|
cmd:text("Options:")
|
2015-11-06 14:08:54 +13:00
|
|
|
cmd:option("-i", "images/miku_small.png", 'path to input image')
|
|
|
|
cmd:option("-l", "", 'path to image-list.txt')
|
2015-05-25 01:09:42 +12:00
|
|
|
cmd:option("-scale", 2, 'scale factor')
|
2015-11-06 14:08:54 +13:00
|
|
|
cmd:option("-o", "(auto)", 'path to output file')
|
2015-11-08 03:01:57 +13:00
|
|
|
cmd:option("-depth", 8, 'bit-depth of the output image (8|16)')
|
2015-11-06 14:08:54 +13:00
|
|
|
cmd:option("-model_dir", "./models/anime_style_art_rgb", 'path to model directory')
|
2015-05-25 01:09:42 +12:00
|
|
|
cmd:option("-m", "noise_scale", 'method (noise|scale|noise_scale)')
|
|
|
|
cmd:option("-noise_level", 1, '(1|2)')
|
|
|
|
cmd:option("-crop_size", 128, 'patch size per process')
|
2015-05-26 03:35:34 +12:00
|
|
|
cmd:option("-resume", 0, "skip existing files (0|1)")
|
2015-10-28 19:30:47 +13:00
|
|
|
cmd:option("-thread", -1, "number of CPU threads")
|
2015-11-09 08:01:28 +13:00
|
|
|
cmd:option("-tta", 0, '8x slower and slightly high quality (0|1)')
|
2015-11-11 12:00:50 +13:00
|
|
|
cmd:option("-white_noise", 0, 'adding white noise to output image (0|1)')
|
|
|
|
cmd:option("-white_noise_std", 0.0055, 'standard division of white noise')
|
2015-11-01 02:09:21 +13:00
|
|
|
|
2015-05-25 01:09:42 +12:00
|
|
|
local opt = cmd:parse(arg)
|
2015-10-28 19:30:47 +13:00
|
|
|
if opt.thread > 0 then
|
|
|
|
torch.setnumthreads(opt.thread)
|
|
|
|
end
|
2015-11-01 02:09:21 +13:00
|
|
|
if cudnn then
|
|
|
|
cudnn.fastest = true
|
|
|
|
cudnn.benchmark = false
|
|
|
|
end
|
|
|
|
|
2015-05-25 01:09:42 +12:00
|
|
|
if string.len(opt.l) == 0 then
|
|
|
|
convert_image(opt)
|
|
|
|
else
|
|
|
|
convert_frames(opt)
|
|
|
|
end
|
|
|
|
end
|
2015-05-16 17:48:05 +12:00
|
|
|
waifu2x()
|