waifu2x.lua 7.5 KB

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