waifu2x.lua 7.1 KB

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