waifu2x.lua 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 t = sys.clock()
  41. local scale_f, image_f
  42. if opt.tta == 1 then
  43. scale_f = reconstruct.scale_tta
  44. image_f = reconstruct.image_tta
  45. else
  46. scale_f = reconstruct.scale
  47. image_f = reconstruct.image
  48. end
  49. opt.o = format_output(opt, opt.i)
  50. if opt.m == "noise" then
  51. local model_path = path.join(opt.model_dir, ("noise%d_model.t7"):format(opt.noise_level))
  52. local model = torch.load(model_path, "ascii")
  53. if not model then
  54. error("Load Error: " .. model_path)
  55. end
  56. new_x = image_f(model, x, opt.crop_size)
  57. new_x = alpha_util.composite(new_x, alpha)
  58. elseif opt.m == "scale" then
  59. local model_path = path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale))
  60. local model = torch.load(model_path, "ascii")
  61. if not model then
  62. error("Load Error: " .. model_path)
  63. end
  64. x = alpha_util.make_border(x, alpha, reconstruct.offset_size(model))
  65. new_x = scale_f(model, opt.scale, x, opt.crop_size, opt.upsampling_filter)
  66. new_x = alpha_util.composite(new_x, alpha, model)
  67. elseif opt.m == "noise_scale" then
  68. local noise_model_path = path.join(opt.model_dir, ("noise%d_model.t7"):format(opt.noise_level))
  69. local noise_model = torch.load(noise_model_path, "ascii")
  70. local scale_model_path = path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale))
  71. local scale_model = torch.load(scale_model_path, "ascii")
  72. if not noise_model then
  73. error("Load Error: " .. noise_model_path)
  74. end
  75. if not scale_model then
  76. error("Load Error: " .. scale_model_path)
  77. end
  78. x = alpha_util.make_border(x, alpha, reconstruct.offset_size(scale_model))
  79. x = image_f(noise_model, x, opt.crop_size)
  80. new_x = scale_f(scale_model, opt.scale, x, opt.crop_size, opt.upsampling_filter)
  81. new_x = alpha_util.composite(new_x, alpha, scale_model)
  82. else
  83. error("undefined method:" .. opt.method)
  84. end
  85. image_loader.save_png(opt.o, new_x, tablex.update({depth = opt.depth, inplace = true}, meta))
  86. print(opt.o .. ": " .. (sys.clock() - t) .. " sec")
  87. end
  88. local function convert_frames(opt)
  89. local model_path, scale_model
  90. local noise_model = {}
  91. local scale_f, image_f
  92. if opt.tta == 1 then
  93. scale_f = reconstruct.scale_tta
  94. image_f = reconstruct.image_tta
  95. else
  96. scale_f = reconstruct.scale
  97. image_f = reconstruct.image
  98. end
  99. if opt.m == "scale" then
  100. model_path = path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale))
  101. scale_model = torch.load(model_path, "ascii")
  102. if not scale_model then
  103. error("Load Error: " .. model_path)
  104. end
  105. elseif opt.m == "noise" then
  106. model_path = path.join(opt.model_dir, string.format("noise%d_model.t7", opt.noise_level))
  107. noise_model[opt.noise_level] = torch.load(model_path, "ascii")
  108. if not noise_model[opt.noise_level] then
  109. error("Load Error: " .. model_path)
  110. end
  111. elseif opt.m == "noise_scale" then
  112. model_path = path.join(opt.model_dir, ("scale%.1fx_model.t7"):format(opt.scale))
  113. scale_model = torch.load(model_path, "ascii")
  114. if not scale_model then
  115. error("Load Error: " .. model_path)
  116. end
  117. model_path = path.join(opt.model_dir, string.format("noise%d_model.t7", opt.noise_level))
  118. noise_model[opt.noise_level] = torch.load(model_path, "ascii")
  119. if not noise_model[opt.noise_level] then
  120. error("Load Error: " .. model_path)
  121. end
  122. end
  123. local fp = io.open(opt.l)
  124. if not fp then
  125. error("Open Error: " .. opt.l)
  126. end
  127. local count = 0
  128. local lines = {}
  129. for line in fp:lines() do
  130. table.insert(lines, line)
  131. end
  132. fp:close()
  133. for i = 1, #lines do
  134. local output = format_output(opt, lines[i], i)
  135. if opt.resume == 0 or path.exists(output) == false then
  136. local x, meta = image_loader.load_float(lines[i])
  137. local alpha = meta.alpha
  138. local new_x = nil
  139. if opt.m == "noise" then
  140. new_x = image_f(noise_model[opt.noise_level], x, opt.crop_size)
  141. new_x = alpha_util.composite(new_x, alpha)
  142. elseif opt.m == "scale" then
  143. x = alpha_util.make_border(x, alpha, reconstruct.offset_size(scale_model))
  144. new_x = scale_f(scale_model, opt.scale, x, opt.crop_size, opt.upsampling_filter)
  145. new_x = alpha_util.composite(new_x, alpha, scale_model)
  146. elseif opt.m == "noise_scale" then
  147. x = alpha_util.make_border(x, alpha, reconstruct.offset_size(scale_model))
  148. x = image_f(noise_model[opt.noise_level], x, opt.crop_size)
  149. new_x = scale_f(scale_model, opt.scale, x, opt.crop_size, upsampling_filter)
  150. new_x = alpha_util.composite(new_x, alpha, scale_model)
  151. else
  152. error("undefined method:" .. opt.method)
  153. end
  154. image_loader.save_png(output, new_x,
  155. tablex.update({depth = opt.depth, inplace = true}, meta))
  156. xlua.progress(i, #lines)
  157. if i % 10 == 0 then
  158. collectgarbage()
  159. end
  160. else
  161. xlua.progress(i, #lines)
  162. end
  163. end
  164. end
  165. local function waifu2x()
  166. local cmd = torch.CmdLine()
  167. cmd:text()
  168. cmd:text("waifu2x")
  169. cmd:text("Options:")
  170. cmd:option("-i", "images/miku_small.png", 'path to input image')
  171. cmd:option("-l", "", 'path to image-list.txt')
  172. cmd:option("-scale", 2, 'scale factor')
  173. cmd:option("-o", "(auto)", 'path to output file')
  174. cmd:option("-depth", 8, 'bit-depth of the output image (8|16)')
  175. cmd:option("-model_dir", "./models/upconv_7/art", 'path to model directory')
  176. cmd:option("-m", "noise_scale", 'method (noise|scale|noise_scale)')
  177. cmd:option("-noise_level", 1, '(1|2|3)')
  178. cmd:option("-crop_size", 128, 'patch size per process')
  179. cmd:option("-resume", 0, "skip existing files (0|1)")
  180. cmd:option("-thread", -1, "number of CPU threads")
  181. cmd:option("-tta", 0, '8x slower and slightly high quality (0|1)')
  182. cmd:option("-upsampling_filter", "Box", 'upsampling filter (for dev)')
  183. local opt = cmd:parse(arg)
  184. if opt.thread > 0 then
  185. torch.setnumthreads(opt.thread)
  186. end
  187. if cudnn then
  188. cudnn.fastest = true
  189. cudnn.benchmark = false
  190. end
  191. if string.len(opt.l) == 0 then
  192. convert_image(opt)
  193. else
  194. convert_frames(opt)
  195. end
  196. end
  197. waifu2x()