waifu2x.lua 7.1 KB

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