1
0
Fork 0
mirror of synced 2024-06-02 19:14:30 +12:00
waifu2x/tools/rebuild_model.lua

45 lines
1.4 KiB
Lua
Raw Normal View History

2016-01-23 13:38:40 +13:00
require 'pl'
local __FILE__ = (function() return string.gsub(debug.getinfo(2, 'S').source, "^@", "") end)()
package.path = path.join(path.dirname(__FILE__), "..", "lib", "?.lua;") .. package.path
require 'os'
require 'w2nn'
local srcnn = require 'srcnn'
2016-04-23 12:18:12 +12:00
local function rebuild(old_model, model)
local new_model = srcnn.create(model, srcnn.backend(old_model), srcnn.color(old_model))
2016-01-23 13:38:40 +13:00
local weight_from = old_model:findModules("nn.SpatialConvolutionMM")
local weight_to = new_model:findModules("nn.SpatialConvolutionMM")
assert(#weight_from == #weight_to)
for i = 1, #weight_from do
local from = weight_from[i]
local to = weight_to[i]
to.weight:copy(from.weight)
to.bias:copy(from.bias)
end
new_model:cuda()
new_model:evaluate()
return new_model
end
local cmd = torch.CmdLine()
cmd:text()
cmd:text("waifu2x rebuild cunn model")
cmd:text("Options:")
cmd:option("-i", "", 'Specify the input model')
cmd:option("-o", "", 'Specify the output model')
2016-04-23 12:18:12 +12:00
cmd:option("-model", "vgg_7", 'Specify the model architecture (vgg_7|vgg_12)')
2016-01-23 13:38:40 +13:00
cmd:option("-iformat", "ascii", 'Specify the input format (ascii|binary)')
cmd:option("-oformat", "ascii", 'Specify the output format (ascii|binary)')
local opt = cmd:parse(arg)
if not path.isfile(opt.i) then
cmd:help()
os.exit(-1)
end
local old_model = torch.load(opt.i, opt.iformat)
2016-04-23 12:18:12 +12:00
local new_model = rebuild(old_model, opt.model)
2016-01-23 13:38:40 +13:00
torch.save(opt.o, new_model, opt.oformat)