export_model.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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)
  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. return meta
  15. end
  16. local function includes(s, a)
  17. for i = 1, #a do
  18. if s == a[i] then
  19. return true
  20. end
  21. end
  22. return false
  23. end
  24. local function get_bias(mod)
  25. if mod.bias then
  26. return mod.bias:float()
  27. else
  28. -- no bias
  29. return torch.FloatTensor(mod.nOutputPlane):zero()
  30. end
  31. end
  32. local function export_weight(jmodules, seq)
  33. local targets = {"nn.SpatialConvolutionMM",
  34. "cudnn.SpatialConvolution",
  35. "nn.SpatialFullConvolution",
  36. "cudnn.SpatialFullConvolution"
  37. }
  38. for k = 1, #seq.modules do
  39. local mod = seq.modules[k]
  40. local name = torch.typename(mod)
  41. if name == "nn.Sequential" or name == "nn.ConcatTable" then
  42. export_weight(jmodules, mod)
  43. elseif includes(name, targets) then
  44. local weight = mod.weight:float()
  45. if name:match("FullConvolution") then
  46. weight = torch.totable(weight:reshape(mod.nInputPlane, mod.nOutputPlane, mod.kH, mod.kW))
  47. else
  48. weight = torch.totable(weight:reshape(mod.nOutputPlane, mod.nInputPlane, mod.kH, mod.kW))
  49. end
  50. local jmod = {
  51. class_name = name,
  52. kW = mod.kW,
  53. kH = mod.kH,
  54. dH = mod.dH,
  55. dW = mod.dW,
  56. padW = mod.padW,
  57. padH = mod.padH,
  58. nInputPlane = mod.nInputPlane,
  59. nOutputPlane = mod.nOutputPlane,
  60. bias = torch.totable(get_bias(mod)),
  61. weight = weight
  62. }
  63. if first_layer then
  64. first_layer = false
  65. jmod.model_config = model_config
  66. end
  67. table.insert(jmodules, jmod)
  68. end
  69. end
  70. end
  71. local function export(model, output)
  72. local jmodules = {}
  73. local model_config = meta_data(model)
  74. local first_layer = true
  75. export_weight(jmodules, model)
  76. local fp = io.open(output, "w")
  77. if not fp then
  78. error("IO Error: " .. output)
  79. end
  80. fp:write(cjson.encode(jmodules))
  81. fp:close()
  82. end
  83. local cmd = torch.CmdLine()
  84. cmd:text()
  85. cmd:text("waifu2x export model")
  86. cmd:text("Options:")
  87. cmd:option("-i", "input.t7", 'Specify the input torch model')
  88. cmd:option("-o", "output.json", 'Specify the output json file')
  89. cmd:option("-iformat", "ascii", 'Specify the input format (ascii|binary)')
  90. local opt = cmd:parse(arg)
  91. if not path.isfile(opt.i) then
  92. cmd:help()
  93. os.exit(-1)
  94. end
  95. local model = torch.load(opt.i, opt.iformat)
  96. export(model, opt.o)