forked from nagadomi/waifu2x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
waifu2x.lua
62 lines (56 loc) · 2.17 KB
/
waifu2x.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
require 'cudnn'
require 'sys'
require 'pl'
require './lib/LeakyReLU'
local iproc = require './lib/iproc'
local reconstract = require './lib/reconstract'
local image_loader = require './lib/image_loader'
local BLOCK_OFFSET = 7
torch.setdefaulttensortype('torch.FloatTensor')
local function waifu2x()
local cmd = torch.CmdLine()
cmd:text()
cmd:text("waifu2x")
cmd:text("Options:")
cmd:option("-i", "images/miku_small.png", 'path of input image')
cmd:option("-o", "(auto)", 'path of output')
cmd:option("-model_dir", "./models", 'model directory')
cmd:option("-m", "noise_scale", 'method (noise|scale|noise_scale)')
cmd:option("-noise_level", 1, '(1|2)')
cmd:option("-crop_size", 128, 'crop size')
local opt = cmd:parse(arg)
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
local x = image_loader.load_float(opt.i)
local new_x = nil
local t = sys.clock()
if opt.m == "noise" then
local model = torch.load(path.join(opt.model_dir,
("noise%d_model.t7"):format(opt.noise_level)), "ascii")
model:evaluate()
new_x = reconstract(model, x, BLOCK_OFFSET)
elseif opt.m == "scale" then
local model = torch.load(path.join(opt.model_dir, "scale2.0x_model.t7"), "ascii")
model:evaluate()
x = iproc.scale(x, x:size(3) * 2.0, x:size(2) * 2.0)
new_x = reconstract(model, x, BLOCK_OFFSET)
elseif opt.m == "noise_scale" then
local noise_model = torch.load(path.join(opt.model_dir,
("noise%d_model.t7"):format(opt.noise_level)), "ascii")
local scale_model = torch.load(path.join(opt.model_dir, "scale2.0x_model.t7"), "ascii")
noise_model:evaluate()
scale_model:evaluate()
x = reconstract(noise_model, x, BLOCK_OFFSET)
x = iproc.scale(x, x:size(3) * 2.0, x:size(2) * 2.0)
new_x = reconstract(scale_model, x, BLOCK_OFFSET)
else
error("undefined method:" .. opt.method)
end
image.save(opt.o, new_x)
print(opt.o .. ": " .. (sys.clock() - t) .. " sec")
end
waifu2x()