waifu2x.lua 6.8 KB

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