benchmark.lua 9.2 KB

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