waifu2x.lua 11 KB

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