waifu2x.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. local __FILE__ = (function() return string.gsub(debug.getinfo(2, 'S').source, "^@", "") end)()
  2. package.path = path.join(path.dirname(__FILE__), "lib", "?.lua;") .. package.path
  3. require 'sys'
  4. require 'pl'
  5. require 'w2nn'
  6. local iproc = require 'iproc'
  7. local reconstruct = require 'reconstruct'
  8. local image_loader = require 'image_loader'
  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_path = path.join(opt.model_dir, ("noise%d_model.t7"):format(opt.noise_level))
  22. local model = torch.load(model_path, "ascii")
  23. if not model then
  24. error("Load Error: " .. model_path)
  25. end
  26. new_x = reconstruct.image(model, x, opt.crop_size)
  27. elseif opt.m == "scale" then
  28. local model_path = path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale))
  29. local model = torch.load(model_path, "ascii")
  30. if not model then
  31. error("Load Error: " .. model_path)
  32. end
  33. new_x = reconstruct.scale(model, opt.scale, x, opt.crop_size)
  34. elseif opt.m == "noise_scale" then
  35. local noise_model_path = path.join(opt.model_dir, ("noise%d_model.t7"):format(opt.noise_level))
  36. local noise_model = torch.load(noise_model_path, "ascii")
  37. local scale_model_path = path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale))
  38. local scale_model = torch.load(scale_model_path, "ascii")
  39. if not noise_model then
  40. error("Load Error: " .. noise_model_path)
  41. end
  42. if not scale_model then
  43. error("Load Error: " .. scale_model_path)
  44. end
  45. x = reconstruct.image(noise_model, x)
  46. new_x = reconstruct.scale(scale_model, opt.scale, x, opt.crop_size)
  47. else
  48. error("undefined method:" .. opt.method)
  49. end
  50. image_loader.save_png(opt.o, new_x, alpha)
  51. print(opt.o .. ": " .. (sys.clock() - t) .. " sec")
  52. end
  53. local function convert_frames(opt)
  54. local noise1_model, noise2_model, scale_model
  55. if opt.m == "scale" then
  56. local model_path = path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale))
  57. scale_model = torch.load(model_path, "ascii")
  58. if not scale_model then
  59. error("Load Error: " .. model_path)
  60. end
  61. elseif opt.m == "noise" and opt.noise_level == 1 then
  62. local model_path = path.join(opt.model_dir, "noise1_model.t7")
  63. noise1_model = torch.load(model_path, "ascii")
  64. if not noise1_model then
  65. error("Load Error: " .. model_path)
  66. end
  67. elseif opt.m == "noise" and opt.noise_level == 2 then
  68. local model_path = path.join(opt.model_dir, "noise2_model.t7")
  69. noise2_model = torch.load(model_path, "ascii")
  70. if not noise2_model then
  71. error("Load Error: " .. model_path)
  72. end
  73. end
  74. local fp = io.open(opt.l)
  75. if not fp then
  76. error("Open Error: " .. opt.l)
  77. end
  78. local count = 0
  79. local lines = {}
  80. for line in fp:lines() do
  81. table.insert(lines, line)
  82. end
  83. fp:close()
  84. for i = 1, #lines do
  85. if opt.resume == 0 or path.exists(string.format(opt.o, i)) == false then
  86. local x, alpha = image_loader.load_float(lines[i])
  87. local new_x = nil
  88. if opt.m == "noise" and opt.noise_level == 1 then
  89. new_x = reconstruct.image(noise1_model, x, opt.crop_size)
  90. elseif opt.m == "noise" and opt.noise_level == 2 then
  91. new_x = reconstruct.image(noise2_model, x)
  92. elseif opt.m == "scale" then
  93. new_x = reconstruct.scale(scale_model, opt.scale, x, opt.crop_size)
  94. elseif opt.m == "noise_scale" and opt.noise_level == 1 then
  95. x = reconstruct.image(noise1_model, x)
  96. new_x = reconstruct.scale(scale_model, opt.scale, x, opt.crop_size)
  97. elseif opt.m == "noise_scale" and opt.noise_level == 2 then
  98. x = reconstruct.image(noise2_model, x)
  99. new_x = reconstruct.scale(scale_model, opt.scale, x, opt.crop_size)
  100. else
  101. error("undefined method:" .. opt.method)
  102. end
  103. local output = nil
  104. if opt.o == "(auto)" then
  105. local name = path.basename(lines[i])
  106. local e = path.extension(name)
  107. local base = name:sub(0, name:len() - e:len())
  108. output = path.join(path.dirname(opt.i), string.format("%s(%s).png", base, opt.m))
  109. else
  110. output = string.format(opt.o, i)
  111. end
  112. image_loader.save_png(output, new_x, alpha)
  113. xlua.progress(i, #lines)
  114. if i % 10 == 0 then
  115. collectgarbage()
  116. end
  117. else
  118. xlua.progress(i, #lines)
  119. end
  120. end
  121. end
  122. local function waifu2x()
  123. local cmd = torch.CmdLine()
  124. cmd:text()
  125. cmd:text("waifu2x")
  126. cmd:text("Options:")
  127. cmd:option("-i", "images/miku_small.png", 'path of the input image')
  128. cmd:option("-l", "", 'path of the image-list')
  129. cmd:option("-scale", 2, 'scale factor')
  130. cmd:option("-o", "(auto)", 'path of the output file')
  131. cmd:option("-model_dir", "./models/anime_style_art_rgb", 'model directory')
  132. cmd:option("-m", "noise_scale", 'method (noise|scale|noise_scale)')
  133. cmd:option("-noise_level", 1, '(1|2)')
  134. cmd:option("-crop_size", 128, 'patch size per process')
  135. cmd:option("-resume", 0, "skip existing files (0|1)")
  136. cmd:option("-thread", -1, "number of CPU threads")
  137. local opt = cmd:parse(arg)
  138. if opt.thread > 0 then
  139. torch.setnumthreads(opt.thread)
  140. end
  141. if cudnn then
  142. cudnn.fastest = true
  143. cudnn.benchmark = false
  144. end
  145. if string.len(opt.l) == 0 then
  146. convert_image(opt)
  147. else
  148. convert_frames(opt)
  149. end
  150. end
  151. waifu2x()