export_model.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. -- adapted from https://github.com/marcan/cl-waifu2x
  2. require 'pl'
  3. local __FILE__ = (function() return string.gsub(debug.getinfo(2, 'S').source, "^@", "") end)()
  4. package.path = path.join(path.dirname(__FILE__), "..", "lib", "?.lua;") .. package.path
  5. require 'w2nn'
  6. local cjson = require "cjson"
  7. local function meta_data(model, model_path)
  8. local meta = {}
  9. for k, v in pairs(model) do
  10. if k:match("w2nn_") then
  11. meta[k:gsub("w2nn_", "")] = v
  12. end
  13. end
  14. modtime = file.modified_time(model_path)
  15. utc_date = Date('utc')
  16. utc_date:set(modtime)
  17. meta["created_at"] = tostring(utc_date)
  18. return meta
  19. end
  20. local function includes(s, a)
  21. for i = 1, #a do
  22. if s == a[i] then
  23. return true
  24. end
  25. end
  26. return false
  27. end
  28. local function get_bias(mod)
  29. if mod.bias then
  30. return mod.bias:float()
  31. else
  32. -- no bias
  33. return torch.FloatTensor(mod.nOutputPlane):zero()
  34. end
  35. end
  36. local function export_weight(jmodules, seq)
  37. local targets = {"nn.SpatialConvolutionMM",
  38. "cudnn.SpatialConvolution",
  39. "cudnn.SpatialDilatedConvolution",
  40. "nn.SpatialFullConvolution",
  41. "nn.SpatialDilatedConvolution",
  42. "cudnn.SpatialFullConvolution"
  43. }
  44. for k = 1, #seq.modules do
  45. local mod = seq.modules[k]
  46. local name = torch.typename(mod)
  47. if name == "nn.Sequential" or name == "nn.ConcatTable" then
  48. export_weight(jmodules, mod)
  49. elseif includes(name, targets) then
  50. local weight = mod.weight:float()
  51. if name:match("FullConvolution") then
  52. weight = torch.totable(weight:reshape(mod.nInputPlane, mod.nOutputPlane, mod.kH, mod.kW))
  53. else
  54. weight = torch.totable(weight:reshape(mod.nOutputPlane, mod.nInputPlane, mod.kH, mod.kW))
  55. end
  56. local jmod = {
  57. class_name = name,
  58. kW = mod.kW,
  59. kH = mod.kH,
  60. dH = mod.dH,
  61. dW = mod.dW,
  62. padW = mod.padW,
  63. padH = mod.padH,
  64. dilationW = mod.dilationW,
  65. dilationH = mod.dilationH,
  66. nInputPlane = mod.nInputPlane,
  67. nOutputPlane = mod.nOutputPlane,
  68. bias = torch.totable(get_bias(mod)),
  69. weight = weight
  70. }
  71. table.insert(jmodules, jmod)
  72. end
  73. end
  74. end
  75. local function export(model, model_path, output)
  76. local jmodules = {}
  77. local model_config = meta_data(model, model_path)
  78. local first_layer = true
  79. print(model_config)
  80. print(model)
  81. export_weight(jmodules, model)
  82. jmodules[1]["model_config"] = model_config
  83. local fp = io.open(output, "w")
  84. if not fp then
  85. error("IO Error: " .. output)
  86. end
  87. fp:write(cjson.encode(jmodules))
  88. fp:close()
  89. end
  90. local cmd = torch.CmdLine()
  91. cmd:text()
  92. cmd:text("waifu2x export model")
  93. cmd:text("Options:")
  94. cmd:option("-i", "input.t7", 'Specify the input torch model')
  95. cmd:option("-o", "output.json", 'Specify the output json file')
  96. cmd:option("-iformat", "ascii", 'Specify the input format (ascii|binary)')
  97. local opt = cmd:parse(arg)
  98. if not path.isfile(opt.i) then
  99. cmd:help()
  100. os.exit(-1)
  101. end
  102. local model = torch.load(opt.i, opt.iformat)
  103. export(model, opt.i, opt.o)