waifu2x.lua 9.3 KB

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