benchmark.lua 9.3 KB

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