waifu2x.lua 6.7 KB

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