waifu2x.lua 9.5 KB

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