export_model.lua 2.5 KB

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