benchmark.lua 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 'xlua'
  5. require 'w2nn'
  6. local iproc = require 'iproc'
  7. local reconstruct = require 'reconstruct'
  8. local image_loader = require 'image_loader'
  9. local gm = require 'graphicsmagick'
  10. local cmd = torch.CmdLine()
  11. cmd:text()
  12. cmd:text("waifu2x-benchmark")
  13. cmd:text("Options:")
  14. cmd:option("-dir", "./data/test", 'test image directory')
  15. cmd:option("-model1_dir", "./models/anime_style_art_rgb", 'model1 directory')
  16. cmd:option("-model2_dir", "", 'model2 directory (optional)')
  17. cmd:option("-method", "scale", '(scale|noise)')
  18. cmd:option("-filter", "Catrom", "downscaling filter (Box|Lanczos|Catrom(Bicubic))")
  19. cmd:option("-color", "y", '(rgb|y)')
  20. cmd:option("-noise_level", 1, 'model noise level')
  21. cmd:option("-jpeg_quality", 75, 'jpeg quality')
  22. cmd:option("-jpeg_times", 1, 'jpeg compression times')
  23. cmd:option("-jpeg_quality_down", 5, 'value of jpeg quality to decrease each times')
  24. cmd:option("-range_bug", 0, 'Reproducing the dynamic range bug that is caused by MATLAB\'s rgb2ycbcr(1|0)')
  25. cmd:option("-gamma_correction", 0, 'Resizing with colorspace correction(sRGB:gamma 2.2) (0|1)')
  26. cmd:option("-save_image", 0, 'save converted images')
  27. cmd:option("-save_baseline_image", 0, 'save baseline images')
  28. cmd:option("-output_dir", "./", 'output directroy')
  29. cmd:option("-show_progress", 1, 'show progressbar')
  30. cmd:option("-baseline_filter", "Catrom", 'baseline interpolation (Box|Lanczos|Catrom(Bicubic))')
  31. local function to_bool(settings, name)
  32. if settings[name] == 1 then
  33. settings[name] = true
  34. else
  35. settings[name] = false
  36. end
  37. end
  38. local opt = cmd:parse(arg)
  39. torch.setdefaulttensortype('torch.FloatTensor')
  40. if cudnn then
  41. cudnn.fastest = true
  42. cudnn.benchmark = false
  43. end
  44. to_bool(opt, "gamma_correction")
  45. to_bool(opt, "save_image")
  46. to_bool(opt, "save_baseline_image")
  47. to_bool(opt, "show_progress")
  48. local function rgb2y_matlab(x)
  49. local y = torch.Tensor(1, x:size(2), x:size(3)):zero()
  50. x = iproc.byte2float(x)
  51. y:add(x[1] * 65.481)
  52. y:add(x[2] * 128.553)
  53. y:add(x[3] * 24.966)
  54. y:add(16.0)
  55. return y:byte():float()
  56. end
  57. local function RGBMSE(x1, x2)
  58. x1 = iproc.float2byte(x1):float()
  59. x2 = iproc.float2byte(x2):float()
  60. return (x1 - x2):pow(2):mean()
  61. end
  62. local function YMSE(x1, x2)
  63. if opt.range_bug == 1 then
  64. local x1_2 = rgb2y_matlab(x1)
  65. local x2_2 = rgb2y_matlab(x2)
  66. return (x1_2 - x2_2):pow(2):mean()
  67. else
  68. local x1_2 = image.rgb2y(x1):mul(255.0)
  69. local x2_2 = image.rgb2y(x2):mul(255.0)
  70. return (x1_2 - x2_2):pow(2):mean()
  71. end
  72. end
  73. local function MSE(x1, x2, color)
  74. if color == "y" then
  75. return YMSE(x1, x2)
  76. else
  77. return RGBMSE(x1, x2)
  78. end
  79. end
  80. local function PSNR(x1, x2, color)
  81. local mse = math.max(MSE(x1, x2, color), 1)
  82. return 10 * math.log10((255.0 * 255.0) / mse)
  83. end
  84. local function transform_jpeg(x, opt)
  85. for i = 1, opt.jpeg_times do
  86. jpeg = gm.Image(x, "RGB", "DHW")
  87. jpeg:format("jpeg")
  88. jpeg:samplingFactors({1.0, 1.0, 1.0})
  89. blob, len = jpeg:toBlob(opt.jpeg_quality - (i - 1) * opt.jpeg_quality_down)
  90. jpeg:fromBlob(blob, len)
  91. x = jpeg:toTensor("byte", "RGB", "DHW")
  92. end
  93. return iproc.byte2float(x)
  94. end
  95. local function baseline_scale(x, filter)
  96. return iproc.scale(x,
  97. x:size(3) * 2.0,
  98. x:size(2) * 2.0,
  99. filter)
  100. end
  101. local function transform_scale(x, opt)
  102. if opt.gamma_correction then
  103. return iproc.scale_with_gamma22(x,
  104. x:size(3) * 0.5,
  105. x:size(2) * 0.5,
  106. opt.filter)
  107. else
  108. return iproc.scale(x,
  109. x:size(3) * 0.5,
  110. x:size(2) * 0.5,
  111. opt.filter)
  112. end
  113. end
  114. local function benchmark(opt, x, input_func, model1, model2)
  115. local model1_mse = 0
  116. local model2_mse = 0
  117. local baseline_mse = 0
  118. local model1_psnr = 0
  119. local model2_psnr = 0
  120. local baseline_psnr = 0
  121. for i = 1, #x do
  122. local ground_truth = x[i].image
  123. local basename = x[i].basename
  124. local input, model1_output, model2_output, baseline_output
  125. input = input_func(ground_truth, opt)
  126. t = sys.clock()
  127. if input:size(3) == ground_truth:size(3) then
  128. model1_output = reconstruct.image(model1, input)
  129. if model2 then
  130. model2_output = reconstruct.image(model2, input)
  131. end
  132. else
  133. model1_output = reconstruct.scale(model1, 2.0, input)
  134. if model2 then
  135. model2_output = reconstruct.scale(model2, 2.0, input)
  136. end
  137. baseline_output = baseline_scale(input, opt.baseline_filter)
  138. end
  139. model1_mse = model1_mse + MSE(ground_truth, model1_output, opt.color)
  140. model1_psnr = model1_psnr + PSNR(ground_truth, model1_output, opt.color)
  141. if model2 then
  142. model2_mse = model2_mse + MSE(ground_truth, model2_output, opt.color)
  143. model2_psnr = model2_psnr + PSNR(ground_truth, model2_output, opt.color)
  144. end
  145. if baseline_output then
  146. baseline_mse = baseline_mse + MSE(ground_truth, baseline_output, opt.color)
  147. baseline_psnr = baseline_psnr + PSNR(ground_truth, baseline_output, opt.color)
  148. end
  149. if opt.save_image then
  150. if opt.save_baseline_image and baseline_output then
  151. image.save(path.join(opt.output_dir, string.format("%s_baseline.png", basename)),
  152. baseline_output)
  153. end
  154. if model1_output then
  155. image.save(path.join(opt.output_dir, string.format("%s_model1.png", basename)),
  156. model1_output)
  157. end
  158. if model2_output then
  159. image.save(path.join(opt.output_dir, string.format("%s_model2.png", basename)),
  160. model2_output)
  161. end
  162. end
  163. if opt.show_progress or i == #x then
  164. if model2 then
  165. if baseline_output then
  166. io.stdout:write(
  167. string.format("%d/%d; baseline_rmse=%f, model1_rmse=%f, model2_rmse=%f, baseline_psnr=%f, model1_psnr=%f, model2_psnr=%f \r",
  168. i, #x,
  169. math.sqrt(baseline_mse / i),
  170. math.sqrt(model1_mse / i), math.sqrt(model2_mse / i),
  171. baseline_psnr / i,
  172. model1_psnr / i, model2_psnr / i
  173. ))
  174. else
  175. io.stdout:write(
  176. string.format("%d/%d; model1_rmse=%f, model2_rmse=%f, model1_psnr=%f, model2_psnr=%f \r",
  177. i, #x,
  178. math.sqrt(model1_mse / i), math.sqrt(model2_mse / i),
  179. model1_psnr / i, model2_psnr / i
  180. ))
  181. end
  182. else
  183. if baseline_output then
  184. io.stdout:write(
  185. string.format("%d/%d; baseline_rmse=%f, model1_rmse=%f, baseline_psnr=%f, model1_psnr=%f \r",
  186. i, #x,
  187. math.sqrt(baseline_mse / i), math.sqrt(model1_mse / i),
  188. baseline_psnr / i, model1_psnr / i
  189. ))
  190. else
  191. io.stdout:write(
  192. string.format("%d/%d; model1_rmse=%f, model1_psnr=%f \r",
  193. i, #x,
  194. math.sqrt(model1_mse / i), model1_psnr / i
  195. ))
  196. end
  197. end
  198. io.stdout:flush()
  199. end
  200. end
  201. io.stdout:write("\n")
  202. end
  203. local function load_data(test_dir)
  204. local test_x = {}
  205. local files = dir.getfiles(test_dir, "*.*")
  206. for i = 1, #files do
  207. local name = path.basename(files[i])
  208. local e = path.extension(name)
  209. local base = name:sub(0, name:len() - e:len())
  210. table.insert(test_x, {image = iproc.crop_mod4(image_loader.load_float(files[i])),
  211. basename = base})
  212. if opt.show_progress then
  213. xlua.progress(i, #files)
  214. end
  215. end
  216. return test_x
  217. end
  218. function load_model(filename)
  219. return torch.load(filename, "ascii")
  220. end
  221. if opt.show_progress then
  222. print(opt)
  223. end
  224. if opt.method == "scale" then
  225. local f1 = path.join(opt.model1_dir, "scale2.0x_model.t7")
  226. local f2 = path.join(opt.model2_dir, "scale2.0x_model.t7")
  227. local s1, model1 = pcall(load_model, f1)
  228. local s2, model2 = pcall(load_model, f2)
  229. if not s1 then
  230. error("Load error: " .. f1)
  231. end
  232. if not s2 then
  233. model2 = nil
  234. end
  235. local test_x = load_data(opt.dir)
  236. benchmark(opt, test_x, transform_scale, model1, model2)
  237. elseif opt.method == "noise" then
  238. local f1 = path.join(opt.model1_dir, string.format("noise%d_model.t7", opt.noise_level))
  239. local f2 = path.join(opt.model2_dir, string.format("noise%d_model.t7", opt.noise_level))
  240. local s1, model1 = pcall(load_model, f1)
  241. local s2, model2 = pcall(load_model, f2)
  242. if not s1 then
  243. error("Load error: " .. f1)
  244. end
  245. if not s2 then
  246. model2 = nil
  247. end
  248. local test_x = load_data(opt.dir)
  249. benchmark(opt, test_x, transform_jpeg, model1, model2)
  250. end