waifu2x.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. require './lib/portable'
  2. require 'sys'
  3. require 'pl'
  4. require './lib/LeakyReLU'
  5. local iproc = require './lib/iproc'
  6. local reconstruct = require './lib/reconstruct'
  7. local image_loader = require './lib/image_loader'
  8. local BLOCK_OFFSET = 7
  9. torch.setdefaulttensortype('torch.FloatTensor')
  10. local function convert_image(opt)
  11. local x, alpha = image_loader.load_float(opt.i)
  12. local new_x = nil
  13. local t = sys.clock()
  14. if opt.o == "(auto)" then
  15. local name = path.basename(opt.i)
  16. local e = path.extension(name)
  17. local base = name:sub(0, name:len() - e:len())
  18. opt.o = path.join(path.dirname(opt.i), string.format("%s(%s).png", base, opt.m))
  19. end
  20. if opt.m == "noise" then
  21. local model = torch.load(path.join(opt.model_dir, ("noise%d_model.t7"):format(opt.noise_level)), "ascii")
  22. model:evaluate()
  23. new_x = reconstruct.image(model, x, BLOCK_OFFSET, opt.crop_size)
  24. elseif opt.m == "scale" then
  25. local model = torch.load(path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale)), "ascii")
  26. model:evaluate()
  27. new_x = reconstruct.scale(model, opt.scale, x, BLOCK_OFFSET, opt.crop_size)
  28. elseif opt.m == "noise_scale" then
  29. local noise_model = torch.load(path.join(opt.model_dir, ("noise%d_model.t7"):format(opt.noise_level)), "ascii")
  30. local scale_model = torch.load(path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale)), "ascii")
  31. noise_model:evaluate()
  32. scale_model:evaluate()
  33. x = reconstruct.image(noise_model, x, BLOCK_OFFSET)
  34. new_x = reconstruct.scale(scale_model, opt.scale, x, BLOCK_OFFSET, opt.crop_size)
  35. else
  36. error("undefined method:" .. opt.method)
  37. end
  38. image_loader.save_png(opt.o, new_x, alpha)
  39. print(opt.o .. ": " .. (sys.clock() - t) .. " sec")
  40. end
  41. local function convert_frames(opt)
  42. local noise1_model = torch.load(path.join(opt.model_dir, "noise1_model.t7"), "ascii")
  43. local noise2_model = torch.load(path.join(opt.model_dir, "noise2_model.t7"), "ascii")
  44. local scale_model = torch.load(path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale)), "ascii")
  45. noise1_model:evaluate()
  46. noise2_model:evaluate()
  47. scale_model:evaluate()
  48. local fp = io.open(opt.l)
  49. local count = 0
  50. local lines = {}
  51. for line in fp:lines() do
  52. table.insert(lines, line)
  53. end
  54. fp:close()
  55. for i = 1, #lines do
  56. if opt.resume == 0 or path.exists(string.format(opt.o, i)) == false then
  57. local x, alpha = image_loader.load_float(lines[i])
  58. local new_x = nil
  59. if opt.m == "noise" and opt.noise_level == 1 then
  60. new_x = reconstruct.image(noise1_model, x, BLOCK_OFFSET, opt.crop_size)
  61. elseif opt.m == "noise" and opt.noise_level == 2 then
  62. new_x = reconstruct.image(noise2_model, x, BLOCK_OFFSET)
  63. elseif opt.m == "scale" then
  64. new_x = reconstruct.scale(scale_model, opt.scale, x, BLOCK_OFFSET, opt.crop_size)
  65. elseif opt.m == "noise_scale" and opt.noise_level == 1 then
  66. x = reconstruct.image(noise1_model, x, BLOCK_OFFSET)
  67. new_x = reconstruct.scale(scale_model, opt.scale, x, BLOCK_OFFSET, opt.crop_size)
  68. elseif opt.m == "noise_scale" and opt.noise_level == 2 then
  69. x = reconstruct.image(noise2_model, x, BLOCK_OFFSET)
  70. new_x = reconstruct.scale(scale_model, opt.scale, x, BLOCK_OFFSET, opt.crop_size)
  71. else
  72. error("undefined method:" .. opt.method)
  73. end
  74. local output = nil
  75. if opt.o == "(auto)" then
  76. local name = path.basename(lines[i])
  77. local e = path.extension(name)
  78. local base = name:sub(0, name:len() - e:len())
  79. output = path.join(path.dirname(opt.i), string.format("%s(%s).png", base, opt.m))
  80. else
  81. output = string.format(opt.o, i)
  82. end
  83. image_loader.save_png(output, new_x, alpha)
  84. xlua.progress(i, #lines)
  85. if i % 10 == 0 then
  86. collectgarbage()
  87. end
  88. else
  89. xlua.progress(i, #lines)
  90. end
  91. end
  92. end
  93. local function waifu2x()
  94. local cmd = torch.CmdLine()
  95. cmd:text()
  96. cmd:text("waifu2x")
  97. cmd:text("Options:")
  98. cmd:option("-i", "images/miku_small.png", 'path of the input image')
  99. cmd:option("-l", "", 'path of the image-list')
  100. cmd:option("-scale", 2, 'scale factor')
  101. cmd:option("-o", "(auto)", 'path of the output file')
  102. cmd:option("-model_dir", "./models/anime_style_art_rgb", 'model directory')
  103. cmd:option("-m", "noise_scale", 'method (noise|scale|noise_scale)')
  104. cmd:option("-noise_level", 1, '(1|2)')
  105. cmd:option("-crop_size", 128, 'patch size per process')
  106. cmd:option("-resume", 0, "skip existing files (0|1)")
  107. local opt = cmd:parse(arg)
  108. if string.len(opt.l) == 0 then
  109. convert_image(opt)
  110. else
  111. convert_frames(opt)
  112. end
  113. end
  114. waifu2x()