waifu2x.lua 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 = function(model, scale, x, block_size, batch_size)
  43. return reconstruct.scale_tta(model, opt.tta_level,
  44. scale, x, block_size, batch_size)
  45. end
  46. image_f = function(model, x, block_size, batch_size)
  47. return reconstruct.image_tta(model, opt.tta_level,
  48. x, block_size, batch_size)
  49. end
  50. else
  51. scale_f = reconstruct.scale
  52. image_f = reconstruct.image
  53. end
  54. opt.o = format_output(opt, opt.i)
  55. if opt.m == "noise" then
  56. local model_path = path.join(opt.model_dir, ("noise%d_model.t7"):format(opt.noise_level))
  57. local model = w2nn.load_model(model_path, opt.force_cudnn)
  58. if not model then
  59. error("Load Error: " .. model_path)
  60. end
  61. local t = sys.clock()
  62. new_x = image_f(model, x, opt.crop_size, opt.batch_size)
  63. new_x = alpha_util.composite(new_x, alpha)
  64. if not opt.q then
  65. print(opt.o .. ": " .. (sys.clock() - t) .. " sec")
  66. end
  67. elseif opt.m == "scale" then
  68. local model_path = path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale))
  69. local model = w2nn.load_model(model_path, opt.force_cudnn)
  70. if not model then
  71. error("Load Error: " .. model_path)
  72. end
  73. local t = sys.clock()
  74. x = alpha_util.make_border(x, alpha, reconstruct.offset_size(model))
  75. new_x = scale_f(model, opt.scale, x, opt.crop_size, opt.batch_size, opt.batch_size)
  76. new_x = alpha_util.composite(new_x, alpha, model)
  77. if not opt.q then
  78. print(opt.o .. ": " .. (sys.clock() - t) .. " sec")
  79. end
  80. elseif opt.m == "noise_scale" then
  81. local model_path = path.join(opt.model_dir, ("noise%d_scale%.1fx_model.t7"):format(opt.noise_level, opt.scale))
  82. if path.exists(model_path) then
  83. local scale_model_path = path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale))
  84. local t, scale_model = pcall(load_model, scale_model_path, opt.force_cudnn)
  85. local model = w2nn.load_model(model_path, opt.force_cudnn)
  86. if not t then
  87. scale_model = model
  88. end
  89. local t = sys.clock()
  90. x = alpha_util.make_border(x, alpha, reconstruct.offset_size(scale_model))
  91. new_x = scale_f(model, opt.scale, x, opt.crop_size, opt.batch_size)
  92. new_x = alpha_util.composite(new_x, alpha, scale_model)
  93. if not opt.q then
  94. print(opt.o .. ": " .. (sys.clock() - t) .. " sec")
  95. end
  96. else
  97. local noise_model_path = path.join(opt.model_dir, ("noise%d_model.t7"):format(opt.noise_level))
  98. local noise_model = w2nn.load_model(noise_model_path, opt.force_cudnn)
  99. local scale_model_path = path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale))
  100. local scale_model = w2nn.load_model(scale_model_path, opt.force_cudnn)
  101. local t = sys.clock()
  102. x = alpha_util.make_border(x, alpha, reconstruct.offset_size(scale_model))
  103. x = image_f(noise_model, x, opt.crop_size, opt.batch_size)
  104. new_x = scale_f(scale_model, opt.scale, x, opt.crop_size, opt.batch_size)
  105. new_x = alpha_util.composite(new_x, alpha, scale_model)
  106. if not opt.q then
  107. print(opt.o .. ": " .. (sys.clock() - t) .. " sec")
  108. end
  109. end
  110. else
  111. error("undefined method:" .. opt.method)
  112. end
  113. image_loader.save_png(opt.o, new_x, tablex.update({depth = opt.depth, inplace = true}, meta))
  114. end
  115. local function convert_frames(opt)
  116. local model_path, scale_model, t
  117. local noise_scale_model = {}
  118. local noise_model = {}
  119. local scale_f, image_f
  120. if opt.tta == 1 then
  121. scale_f = function(model, scale, x, block_size, batch_size)
  122. return reconstruct.scale_tta(model, opt.tta_level,
  123. scale, x, block_size, batch_size)
  124. end
  125. image_f = function(model, x, block_size, batch_size)
  126. return reconstruct.image_tta(model, opt.tta_level,
  127. x, block_size, batch_size)
  128. end
  129. else
  130. scale_f = reconstruct.scale
  131. image_f = reconstruct.image
  132. end
  133. if opt.m == "scale" then
  134. model_path = path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale))
  135. scale_model = w2nn.load_model(model_path, opt.force_cudnn)
  136. elseif opt.m == "noise" then
  137. model_path = path.join(opt.model_dir, string.format("noise%d_model.t7", opt.noise_level))
  138. noise_model[opt.noise_level] = w2nn.load_model(model_path, opt.force_cudnn)
  139. elseif opt.m == "noise_scale" then
  140. local model_path = path.join(opt.model_dir, ("noise%d_scale%.1fx_model.t7"):format(opt.noise_level, opt.scale))
  141. if path.exists(model_path) then
  142. noise_scale_model[opt.noise_level] = w2nn.load_model(model_path, opt.force_cudnn)
  143. model_path = path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale))
  144. t, scale_model = pcall(load_model, model_path, opt.force_cudnn)
  145. if not t then
  146. scale_model = noise_scale_model[opt.noise_level]
  147. end
  148. else
  149. model_path = path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale))
  150. scale_model = w2nn.load_model(model_path, opt.force_cudnn)
  151. model_path = path.join(opt.model_dir, string.format("noise%d_model.t7", opt.noise_level))
  152. noise_model[opt.noise_level] = w2nn.load_model(model_path, opt.force_cudnn)
  153. end
  154. end
  155. local fp = io.open(opt.l)
  156. if not fp then
  157. error("Open Error: " .. opt.l)
  158. end
  159. local count = 0
  160. local lines = {}
  161. for line in fp:lines() do
  162. table.insert(lines, line)
  163. end
  164. fp:close()
  165. for i = 1, #lines do
  166. local output = format_output(opt, lines[i], i)
  167. if opt.resume == 0 or path.exists(output) == false then
  168. local x, meta = image_loader.load_float(lines[i])
  169. local alpha = meta.alpha
  170. local new_x = nil
  171. if opt.m == "noise" then
  172. new_x = image_f(noise_model[opt.noise_level], x, opt.crop_size, opt.batch_size)
  173. new_x = alpha_util.composite(new_x, alpha)
  174. elseif opt.m == "scale" then
  175. x = alpha_util.make_border(x, alpha, reconstruct.offset_size(scale_model))
  176. new_x = scale_f(scale_model, opt.scale, x, opt.crop_size, opt.batch_size)
  177. new_x = alpha_util.composite(new_x, alpha, scale_model)
  178. elseif opt.m == "noise_scale" then
  179. x = alpha_util.make_border(x, alpha, reconstruct.offset_size(scale_model))
  180. if noise_scale_model[opt.noise_level] then
  181. new_x = scale_f(noise_scale_model[opt.noise_level], opt.scale, x, opt.crop_size, opt.batch_size)
  182. else
  183. x = image_f(noise_model[opt.noise_level], x, opt.crop_size, opt.batch_size)
  184. new_x = scale_f(scale_model, opt.scale, x, opt.crop_size, opt.batch_size)
  185. end
  186. new_x = alpha_util.composite(new_x, alpha, scale_model)
  187. else
  188. error("undefined method:" .. opt.method)
  189. end
  190. image_loader.save_png(output, new_x,
  191. tablex.update({depth = opt.depth, inplace = true}, meta))
  192. if not opt.q then
  193. xlua.progress(i, #lines)
  194. end
  195. if i % 10 == 0 then
  196. collectgarbage()
  197. end
  198. else
  199. if not opt.q then
  200. xlua.progress(i, #lines)
  201. end
  202. end
  203. end
  204. end
  205. local function waifu2x()
  206. local cmd = torch.CmdLine()
  207. cmd:text()
  208. cmd:text("waifu2x")
  209. cmd:text("Options:")
  210. cmd:option("-i", "images/miku_small.png", 'path to input image')
  211. cmd:option("-l", "", 'path to image-list.txt')
  212. cmd:option("-scale", 2, 'scale factor')
  213. cmd:option("-o", "(auto)", 'path to output file')
  214. cmd:option("-depth", 8, 'bit-depth of the output image (8|16)')
  215. cmd:option("-model_dir", "./models/upconv_7/art", 'path to model directory')
  216. cmd:option("-m", "noise_scale", 'method (noise|scale|noise_scale)')
  217. cmd:option("-method", "", 'same as -m')
  218. cmd:option("-noise_level", 1, '(1|2|3)')
  219. cmd:option("-crop_size", 128, 'patch size per process')
  220. cmd:option("-batch_size", 1, 'batch_size')
  221. cmd:option("-resume", 0, "skip existing files (0|1)")
  222. cmd:option("-thread", -1, "number of CPU threads")
  223. cmd:option("-tta", 0, 'use TTA mode. It is slow but slightly high quality (0|1)')
  224. cmd:option("-tta_level", 8, 'TTA level (2|4|8). A higher value makes better quality output but slow')
  225. cmd:option("-force_cudnn", 0, 'use cuDNN backend (0|1)')
  226. cmd:option("-q", 0, 'quiet (0|1)')
  227. local opt = cmd:parse(arg)
  228. if opt.method:len() > 0 then
  229. opt.m = opt.method
  230. end
  231. if opt.thread > 0 then
  232. torch.setnumthreads(opt.thread)
  233. end
  234. if cudnn then
  235. cudnn.fastest = true
  236. if opt.l:len() > 0 then
  237. cudnn.benchmark = true -- find fastest algo
  238. else
  239. cudnn.benchmark = false
  240. end
  241. end
  242. opt.force_cudnn = opt.force_cudnn == 1
  243. opt.q = opt.q == 1
  244. if string.len(opt.l) == 0 then
  245. convert_image(opt)
  246. else
  247. convert_frames(opt)
  248. end
  249. end
  250. waifu2x()