2015-06-13 18:02:02 +12:00
|
|
|
require './lib/portable'
|
2015-05-16 17:48:05 +12:00
|
|
|
require 'sys'
|
|
|
|
require 'pl'
|
|
|
|
require './lib/LeakyReLU'
|
|
|
|
|
|
|
|
local iproc = require './lib/iproc'
|
2015-05-19 19:47:52 +12:00
|
|
|
local reconstruct = require './lib/reconstruct'
|
2015-05-16 17:48:05 +12:00
|
|
|
local image_loader = require './lib/image_loader'
|
|
|
|
local BLOCK_OFFSET = 7
|
|
|
|
|
|
|
|
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-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())
|
|
|
|
opt.o = path.join(path.dirname(opt.i), string.format("%s(%s).png", base, opt.m))
|
|
|
|
end
|
|
|
|
if opt.m == "noise" then
|
2015-05-26 02:22:29 +12:00
|
|
|
local model = torch.load(path.join(opt.model_dir, ("noise%d_model.t7"):format(opt.noise_level)), "ascii")
|
2015-05-16 17:48:05 +12:00
|
|
|
model:evaluate()
|
2015-06-13 18:02:02 +12:00
|
|
|
new_x = reconstruct.image(model, x, BLOCK_OFFSET, opt.crop_size)
|
2015-05-16 17:48:05 +12:00
|
|
|
elseif opt.m == "scale" then
|
2015-05-25 01:09:42 +12:00
|
|
|
local model = torch.load(path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale)), "ascii")
|
2015-05-16 17:48:05 +12:00
|
|
|
model:evaluate()
|
2015-06-13 18:02:02 +12:00
|
|
|
new_x = reconstruct.scale(model, opt.scale, x, BLOCK_OFFSET, opt.crop_size)
|
2015-05-16 17:48:05 +12:00
|
|
|
elseif opt.m == "noise_scale" then
|
2015-05-26 02:22:29 +12:00
|
|
|
local noise_model = torch.load(path.join(opt.model_dir, ("noise%d_model.t7"):format(opt.noise_level)), "ascii")
|
2015-05-25 01:09:42 +12:00
|
|
|
local scale_model = torch.load(path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale)), "ascii")
|
2015-05-16 17:48:05 +12:00
|
|
|
noise_model:evaluate()
|
|
|
|
scale_model:evaluate()
|
2015-05-22 23:06:25 +12:00
|
|
|
x = reconstruct.image(noise_model, x, BLOCK_OFFSET)
|
2015-06-13 18:02:02 +12:00
|
|
|
new_x = reconstruct.scale(scale_model, opt.scale, x, BLOCK_OFFSET, opt.crop_size)
|
2015-05-16 17:48:05 +12:00
|
|
|
else
|
|
|
|
error("undefined method:" .. opt.method)
|
|
|
|
end
|
2015-06-16 23:41:48 +12:00
|
|
|
image_loader.save_png(opt.o, new_x, alpha)
|
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)
|
|
|
|
local noise1_model = torch.load(path.join(opt.model_dir, "noise1_model.t7"), "ascii")
|
|
|
|
local noise2_model = torch.load(path.join(opt.model_dir, "noise2_model.t7"), "ascii")
|
|
|
|
local scale_model = torch.load(path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale)), "ascii")
|
|
|
|
|
|
|
|
noise1_model:evaluate()
|
|
|
|
noise2_model:evaluate()
|
|
|
|
scale_model:evaluate()
|
|
|
|
|
|
|
|
local fp = io.open(opt.l)
|
|
|
|
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
|
|
|
|
new_x = reconstruct.image(noise1_model, x, BLOCK_OFFSET, opt.crop_size)
|
|
|
|
elseif opt.m == "noise" and opt.noise_level == 2 then
|
|
|
|
new_x = reconstruct.image(noise2_model, x, BLOCK_OFFSET)
|
|
|
|
elseif opt.m == "scale" then
|
|
|
|
new_x = reconstruct.scale(scale_model, opt.scale, x, BLOCK_OFFSET, opt.crop_size)
|
|
|
|
elseif opt.m == "noise_scale" and opt.noise_level == 1 then
|
|
|
|
x = reconstruct.image(noise1_model, x, BLOCK_OFFSET)
|
|
|
|
new_x = reconstruct.scale(scale_model, opt.scale, x, BLOCK_OFFSET, opt.crop_size)
|
|
|
|
elseif opt.m == "noise_scale" and opt.noise_level == 2 then
|
|
|
|
x = reconstruct.image(noise2_model, x, BLOCK_OFFSET)
|
|
|
|
new_x = reconstruct.scale(scale_model, opt.scale, x, BLOCK_OFFSET, opt.crop_size)
|
|
|
|
else
|
|
|
|
error("undefined method:" .. opt.method)
|
|
|
|
end
|
|
|
|
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
|
|
|
|
image_loader.save_png(output, new_x, alpha)
|
|
|
|
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:")
|
|
|
|
cmd:option("-i", "images/miku_small.png", 'path of the input image')
|
|
|
|
cmd:option("-l", "", 'path of the image-list')
|
|
|
|
cmd:option("-scale", 2, 'scale factor')
|
|
|
|
cmd:option("-o", "(auto)", 'path of the output file')
|
2015-06-23 05:27:28 +12:00
|
|
|
cmd:option("-model_dir", "./models/anime_style_art_rgb", '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-05-25 01:09:42 +12:00
|
|
|
|
|
|
|
local opt = cmd:parse(arg)
|
|
|
|
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()
|