waifu2x.lua 4.5 KB

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