waifu2x.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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(w2nn.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. elseif opt.m == "user" then
  111. local model_path = opt.model_path
  112. local model = w2nn.load_model(model_path, opt.force_cudnn)
  113. if not model then
  114. error("Load Error: " .. model_path)
  115. end
  116. local t = sys.clock()
  117. x = alpha_util.make_border(x, alpha, reconstruct.offset_size(model))
  118. if opt.scale == 1 then
  119. new_x = image_f(model, x, opt.crop_size, opt.batch_size)
  120. else
  121. new_x = scale_f(model, opt.scale, x, opt.crop_size, opt.batch_size)
  122. end
  123. new_x = alpha_util.composite(new_x, alpha) -- TODO: should it use model?
  124. if not opt.q then
  125. print(opt.o .. ": " .. (sys.clock() - t) .. " sec")
  126. end
  127. else
  128. error("undefined method:" .. opt.method)
  129. end
  130. image_loader.save_png(opt.o, new_x, tablex.update({depth = opt.depth, inplace = true}, meta))
  131. end
  132. local function convert_frames(opt)
  133. local model_path, scale_model, t
  134. local noise_scale_model = {}
  135. local noise_model = {}
  136. local user_model = nil
  137. local scale_f, image_f
  138. if opt.tta == 1 then
  139. scale_f = function(model, scale, x, block_size, batch_size)
  140. return reconstruct.scale_tta(model, opt.tta_level,
  141. scale, x, block_size, batch_size)
  142. end
  143. image_f = function(model, x, block_size, batch_size)
  144. return reconstruct.image_tta(model, opt.tta_level,
  145. x, block_size, batch_size)
  146. end
  147. else
  148. scale_f = reconstruct.scale
  149. image_f = reconstruct.image
  150. end
  151. if opt.m == "scale" then
  152. model_path = path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale))
  153. scale_model = w2nn.load_model(model_path, opt.force_cudnn)
  154. elseif opt.m == "noise" then
  155. model_path = path.join(opt.model_dir, string.format("noise%d_model.t7", opt.noise_level))
  156. noise_model[opt.noise_level] = w2nn.load_model(model_path, opt.force_cudnn)
  157. elseif opt.m == "noise_scale" then
  158. local model_path = path.join(opt.model_dir, ("noise%d_scale%.1fx_model.t7"):format(opt.noise_level, opt.scale))
  159. if path.exists(model_path) then
  160. noise_scale_model[opt.noise_level] = w2nn.load_model(model_path, opt.force_cudnn)
  161. model_path = path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale))
  162. t, scale_model = pcall(w2nn.load_model, model_path, opt.force_cudnn)
  163. if not t then
  164. scale_model = noise_scale_model[opt.noise_level]
  165. end
  166. else
  167. model_path = path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale))
  168. scale_model = w2nn.load_model(model_path, opt.force_cudnn)
  169. model_path = path.join(opt.model_dir, string.format("noise%d_model.t7", opt.noise_level))
  170. noise_model[opt.noise_level] = w2nn.load_model(model_path, opt.force_cudnn)
  171. end
  172. elseif opt.m == "user" then
  173. user_model = w2nn.load_model(opt.model_path, opt.force_cudnn)
  174. end
  175. local fp = io.open(opt.l)
  176. if not fp then
  177. error("Open Error: " .. opt.l)
  178. end
  179. local count = 0
  180. local lines = {}
  181. for line in fp:lines() do
  182. table.insert(lines, line)
  183. end
  184. fp:close()
  185. for i = 1, #lines do
  186. local output = format_output(opt, lines[i], i)
  187. if opt.resume == 0 or path.exists(output) == false then
  188. local x, meta = image_loader.load_float(lines[i])
  189. local alpha = meta.alpha
  190. local new_x = nil
  191. if opt.m == "noise" then
  192. new_x = image_f(noise_model[opt.noise_level], x, opt.crop_size, opt.batch_size)
  193. new_x = alpha_util.composite(new_x, alpha)
  194. elseif opt.m == "scale" then
  195. x = alpha_util.make_border(x, alpha, reconstruct.offset_size(scale_model))
  196. new_x = scale_f(scale_model, opt.scale, x, opt.crop_size, opt.batch_size)
  197. new_x = alpha_util.composite(new_x, alpha, scale_model)
  198. elseif opt.m == "noise_scale" then
  199. x = alpha_util.make_border(x, alpha, reconstruct.offset_size(scale_model))
  200. if noise_scale_model[opt.noise_level] then
  201. new_x = scale_f(noise_scale_model[opt.noise_level], opt.scale, x, opt.crop_size, opt.batch_size)
  202. else
  203. x = image_f(noise_model[opt.noise_level], x, opt.crop_size, opt.batch_size)
  204. new_x = scale_f(scale_model, opt.scale, x, opt.crop_size, opt.batch_size)
  205. end
  206. new_x = alpha_util.composite(new_x, alpha, scale_model)
  207. elseif opt.m == "user" then
  208. x = alpha_util.make_border(x, alpha, reconstruct.offset_size(user_model))
  209. if opt.scale == 1 then
  210. new_x = image_f(user_model, x, opt.crop_size, opt.batch_size)
  211. else
  212. new_x = scale_f(user_model, opt.scale, x, opt.crop_size, opt.batch_size)
  213. end
  214. new_x = alpha_util.composite(new_x, alpha)
  215. else
  216. error("undefined method:" .. opt.method)
  217. end
  218. image_loader.save_png(output, new_x,
  219. tablex.update({depth = opt.depth, inplace = true}, meta))
  220. if not opt.q then
  221. xlua.progress(i, #lines)
  222. end
  223. if i % 10 == 0 then
  224. collectgarbage()
  225. end
  226. else
  227. if not opt.q then
  228. xlua.progress(i, #lines)
  229. end
  230. end
  231. end
  232. end
  233. local function waifu2x()
  234. local cmd = torch.CmdLine()
  235. cmd:text()
  236. cmd:text("waifu2x")
  237. cmd:text("Options:")
  238. cmd:option("-i", "images/miku_small.png", 'path to input image')
  239. cmd:option("-l", "", 'path to image-list.txt')
  240. cmd:option("-scale", 2, 'scale factor')
  241. cmd:option("-o", "(auto)", 'path to output file')
  242. cmd:option("-depth", 8, 'bit-depth of the output image (8|16)')
  243. cmd:option("-model_dir", "./models/upconv_7/art", 'path to model directory')
  244. cmd:option("-name", "user", 'model name for user method')
  245. cmd:option("-m", "noise_scale", 'method (noise|scale|noise_scale|user)')
  246. cmd:option("-method", "", 'same as -m')
  247. cmd:option("-noise_level", 1, '(1|2|3)')
  248. cmd:option("-crop_size", 128, 'patch size per process')
  249. cmd:option("-batch_size", 1, 'batch_size')
  250. cmd:option("-resume", 0, "skip existing files (0|1)")
  251. cmd:option("-thread", -1, "number of CPU threads")
  252. cmd:option("-tta", 0, 'use TTA mode. It is slow but slightly high quality (0|1)')
  253. cmd:option("-tta_level", 8, 'TTA level (2|4|8). A higher value makes better quality output but slow')
  254. cmd:option("-force_cudnn", 0, 'use cuDNN backend (0|1)')
  255. cmd:option("-q", 0, 'quiet (0|1)')
  256. local opt = cmd:parse(arg)
  257. if opt.method:len() > 0 then
  258. opt.m = opt.method
  259. end
  260. if opt.thread > 0 then
  261. torch.setnumthreads(opt.thread)
  262. end
  263. if cudnn then
  264. cudnn.fastest = true
  265. if opt.l:len() > 0 then
  266. cudnn.benchmark = true -- find fastest algo
  267. else
  268. cudnn.benchmark = false
  269. end
  270. end
  271. opt.force_cudnn = opt.force_cudnn == 1
  272. opt.q = opt.q == 1
  273. opt.model_path = path.join(opt.model_dir, string.format("%s_model.t7", opt.name))
  274. if string.len(opt.l) == 0 then
  275. convert_image(opt)
  276. else
  277. convert_frames(opt)
  278. end
  279. end
  280. waifu2x()