benchmark.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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|noise_scale)')
  19. cmd:option("-filter", "Catrom", "downscaling filter (Box|Lanczos|Catrom(Bicubic))")
  20. cmd:option("-resize_blur", 1.0, 'blur parameter for resize')
  21. cmd:option("-color", "y", '(rgb|y)')
  22. cmd:option("-noise_level", 1, 'model noise level')
  23. cmd:option("-jpeg_quality", 75, 'jpeg quality')
  24. cmd:option("-jpeg_times", 1, 'jpeg compression times')
  25. cmd:option("-jpeg_quality_down", 5, 'value of jpeg quality to decrease each times')
  26. cmd:option("-range_bug", 0, 'Reproducing the dynamic range bug that is caused by MATLAB\'s rgb2ycbcr(1|0)')
  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. cmd:option("-tta", 0, 'use tta')
  36. cmd:option("-tta_level", 8, 'tta level')
  37. cmd:option("-crop_size", 128, 'patch size per process')
  38. cmd:option("-batch_size", 1, 'batch_size')
  39. cmd:option("-force_cudnn", 0, 'use cuDNN backend')
  40. cmd:option("-yuv420", 0, 'use yuv420 jpeg')
  41. local function to_bool(settings, name)
  42. if settings[name] == 1 then
  43. settings[name] = true
  44. else
  45. settings[name] = false
  46. end
  47. end
  48. local opt = cmd:parse(arg)
  49. torch.setdefaulttensortype('torch.FloatTensor')
  50. if cudnn then
  51. cudnn.fastest = true
  52. cudnn.benchmark = true
  53. end
  54. to_bool(opt, "force_cudnn")
  55. to_bool(opt, "yuv420")
  56. to_bool(opt, "save_all")
  57. to_bool(opt, "tta")
  58. if opt.save_all then
  59. opt.save_image = true
  60. opt.save_info = true
  61. opt.save_baseline_image = true
  62. else
  63. to_bool(opt, "save_image")
  64. to_bool(opt, "save_info")
  65. to_bool(opt, "save_baseline_image")
  66. end
  67. to_bool(opt, "show_progress")
  68. if opt.thread > 0 then
  69. torch.setnumthreads(tonumber(opt.thread))
  70. end
  71. local function rgb2y_matlab(x)
  72. local y = torch.Tensor(1, x:size(2), x:size(3)):zero()
  73. x = iproc.byte2float(x)
  74. y:add(x[1] * 65.481)
  75. y:add(x[2] * 128.553)
  76. y:add(x[3] * 24.966)
  77. y:add(16.0)
  78. return y:byte():float()
  79. end
  80. local function RGBMSE(x1, x2)
  81. x1 = iproc.float2byte(x1):float()
  82. x2 = iproc.float2byte(x2):float()
  83. return (x1 - x2):pow(2):mean()
  84. end
  85. local function YMSE(x1, x2)
  86. if opt.range_bug == 1 then
  87. local x1_2 = rgb2y_matlab(x1)
  88. local x2_2 = rgb2y_matlab(x2)
  89. return (x1_2 - x2_2):pow(2):mean()
  90. else
  91. local x1_2 = image.rgb2y(x1):mul(255.0)
  92. local x2_2 = image.rgb2y(x2):mul(255.0)
  93. return (x1_2 - x2_2):pow(2):mean()
  94. end
  95. end
  96. local function MSE(x1, x2, color)
  97. if color == "y" then
  98. return YMSE(x1, x2)
  99. else
  100. return RGBMSE(x1, x2)
  101. end
  102. end
  103. local function PSNR(x1, x2, color)
  104. local mse = math.max(MSE(x1, x2, color), 1)
  105. return 10 * math.log10((255.0 * 255.0) / mse)
  106. end
  107. local function MSE2PSNR(mse)
  108. return 10 * math.log10((255.0 * 255.0) / math.max(mse, 1))
  109. end
  110. local function transform_jpeg(x, opt)
  111. for i = 1, opt.jpeg_times do
  112. jpeg = gm.Image(x, "RGB", "DHW")
  113. jpeg:format("jpeg")
  114. if opt.yuv420 then
  115. jpeg:samplingFactors({2.0, 1.0, 1.0})
  116. else
  117. jpeg:samplingFactors({1.0, 1.0, 1.0})
  118. end
  119. blob, len = jpeg:toBlob(opt.jpeg_quality - (i - 1) * opt.jpeg_quality_down)
  120. jpeg:fromBlob(blob, len)
  121. x = jpeg:toTensor("byte", "RGB", "DHW")
  122. end
  123. return iproc.byte2float(x)
  124. end
  125. local function baseline_scale(x, filter)
  126. return iproc.scale(x,
  127. x:size(3) * 2.0,
  128. x:size(2) * 2.0,
  129. filter)
  130. end
  131. local function transform_scale(x, opt)
  132. return iproc.scale(x,
  133. x:size(3) * 0.5,
  134. x:size(2) * 0.5,
  135. opt.filter, opt.resize_blur)
  136. end
  137. local function transform_scale_jpeg(x, opt)
  138. x = iproc.scale(x,
  139. x:size(3) * 0.5,
  140. x:size(2) * 0.5,
  141. opt.filter, opt.resize_blur)
  142. for i = 1, opt.jpeg_times do
  143. jpeg = gm.Image(x, "RGB", "DHW")
  144. jpeg:format("jpeg")
  145. if opt.yuv420 then
  146. jpeg:samplingFactors({2.0, 1.0, 1.0})
  147. else
  148. jpeg:samplingFactors({1.0, 1.0, 1.0})
  149. end
  150. blob, len = jpeg:toBlob(opt.jpeg_quality - (i - 1) * opt.jpeg_quality_down)
  151. jpeg:fromBlob(blob, len)
  152. x = jpeg:toTensor("byte", "RGB", "DHW")
  153. end
  154. return iproc.byte2float(x)
  155. end
  156. local function benchmark(opt, x, input_func, model1, model2)
  157. local mse
  158. local model1_mse = 0
  159. local model2_mse = 0
  160. local baseline_mse = 0
  161. local model1_psnr = 0
  162. local model2_psnr = 0
  163. local baseline_psnr = 0
  164. local model1_time = 0
  165. local model2_time = 0
  166. local scale_f = reconstruct.scale
  167. local image_f = reconstruct.image
  168. if opt.tta then
  169. scale_f = function(model, scale, x, block_size, batch_size)
  170. return reconstruct.scale_tta(model, opt.tta_level,
  171. scale, x, block_size, batch_size)
  172. end
  173. image_f = function(model, x, block_size, batch_size)
  174. return reconstruct.image_tta(model, opt.tta_level,
  175. x, block_size, batch_size)
  176. end
  177. end
  178. for i = 1, #x do
  179. local ground_truth = x[i].image
  180. local basename = x[i].basename
  181. local input, model1_output, model2_output, baseline_output
  182. input = input_func(ground_truth, opt)
  183. if opt.method == "scale" then
  184. if opt.force_cudnn and i == 1 then -- run cuDNN benchmark first
  185. model1_output = scale_f(model1, 2.0, input, opt.crop_size, opt.batch_size)
  186. if model2 then
  187. model2_output = scale_f(model2, 2.0, input, opt.crop_size, opt.batch_size)
  188. end
  189. end
  190. t = sys.clock()
  191. model1_output = scale_f(model1, 2.0, input, opt.crop_size, opt.batch_size)
  192. model1_time = model1_time + (sys.clock() - t)
  193. if model2 then
  194. t = sys.clock()
  195. model2_output = scale_f(model2, 2.0, input, opt.crop_size, opt.batch_size)
  196. model2_time = model2_time + (sys.clock() - t)
  197. end
  198. baseline_output = baseline_scale(input, opt.baseline_filter)
  199. elseif opt.method == "noise" then
  200. if opt.force_cudnn and i == 1 then -- run cuDNN benchmark first
  201. model1_output = image_f(model1, input, opt.crop_size, opt.batch_size)
  202. if model2 then
  203. model2_output = image_f(model2, input, opt.crop_size, opt.batch_size)
  204. end
  205. end
  206. t = sys.clock()
  207. model1_output = image_f(model1, input, opt.crop_size, opt.batch_size)
  208. model1_time = model1_time + (sys.clock() - t)
  209. if model2 then
  210. t = sys.clock()
  211. model2_output = image_f(model2, input, opt.crop_size, opt.batch_size)
  212. model2_time = model2_time + (sys.clock() - t)
  213. end
  214. baseline_output = input
  215. elseif opt.method == "noise_scale" then
  216. if opt.force_cudnn and i == 1 then -- run cuDNN benchmark first
  217. if model1.noise_scale_model then
  218. model1_output = scale_f(model1.noise_scale_model, 2.0,
  219. input, opt.crop_size, opt.batch_size)
  220. else
  221. if model1.noise_model then
  222. model1_output = image_f(model1.noise_model, input, opt.crop_size, opt.batch_size)
  223. else
  224. model1_output = input
  225. end
  226. model1_output = scale_f(model1.scale_model, 2.0, model1_output,
  227. opt.crop_size, opt.batch_size)
  228. end
  229. if model2 then
  230. if model2.noise_scale_model then
  231. model2_output = scale_f(model2.noise_scale_model, 2.0,
  232. input, opt.crop_size, opt.batch_size)
  233. else
  234. if model2.noise_model then
  235. model2_output = image_f(model2.noise_model, input,
  236. opt.crop_size, opt.batch_size)
  237. else
  238. model2_output = input
  239. end
  240. model2_output = scale_f(model2.scale_model, 2.0, model2_output,
  241. opt.crop_size, opt.batch_size)
  242. end
  243. end
  244. end
  245. t = sys.clock()
  246. if model1.noise_scale_model then
  247. model1_output = scale_f(model1.noise_scale_model, 2.0,
  248. input, opt.crop_size, opt.batch_size)
  249. else
  250. if model1.noise_model then
  251. model1_output = image_f(model1.noise_model, input, opt.crop_size, opt.batch_size)
  252. else
  253. model1_output = input
  254. end
  255. model1_output = scale_f(model1.scale_model, 2.0, model1_output,
  256. opt.crop_size, opt.batch_size)
  257. end
  258. model1_time = model1_time + (sys.clock() - t)
  259. if model2 then
  260. t = sys.clock()
  261. if model2.noise_scale_model then
  262. model2_output = scale_f(model2.noise_scale_model, 2.0,
  263. input, opt.crop_size, opt.batch_size)
  264. else
  265. if model2.noise_model then
  266. model2_output = image_f(model2.noise_model, input,
  267. opt.crop_size, opt.batch_size)
  268. else
  269. model2_output = input
  270. end
  271. model2_output = scale_f(model2.scale_model, 2.0, model2_output,
  272. opt.crop_size, opt.batch_size)
  273. end
  274. model2_time = model2_time + (sys.clock() - t)
  275. end
  276. baseline_output = baseline_scale(input, opt.baseline_filter)
  277. end
  278. mse = MSE(ground_truth, model1_output, opt.color)
  279. model1_mse = model1_mse + mse
  280. model1_psnr = model1_psnr + MSE2PSNR(mse)
  281. if model2 then
  282. mse = MSE(ground_truth, model2_output, opt.color)
  283. model2_mse = model2_mse + mse
  284. model2_psnr = model2_psnr + MSE2PSNR(mse)
  285. end
  286. if baseline_output then
  287. mse = MSE(ground_truth, baseline_output, opt.color)
  288. baseline_mse = baseline_mse + mse
  289. baseline_psnr = baseline_psnr + MSE2PSNR(mse)
  290. end
  291. if opt.save_image then
  292. if opt.save_baseline_image and baseline_output then
  293. image.save(path.join(opt.output_dir, string.format("%s_baseline.png", basename)),
  294. baseline_output)
  295. end
  296. if model1_output then
  297. image.save(path.join(opt.output_dir, string.format("%s_model1.png", basename)),
  298. model1_output)
  299. end
  300. if model2_output then
  301. image.save(path.join(opt.output_dir, string.format("%s_model2.png", basename)),
  302. model2_output)
  303. end
  304. end
  305. if opt.show_progress or i == #x then
  306. if model2 then
  307. if baseline_output then
  308. io.stdout:write(
  309. string.format("%d/%d; model1_time=%.2f, model2_time=%.2f, baseline_rmse=%f, model1_rmse=%f, model2_rmse=%f, baseline_psnr=%f, model1_psnr=%f, model2_psnr=%f \r",
  310. i, #x,
  311. model1_time,
  312. model2_time,
  313. math.sqrt(baseline_mse / i),
  314. math.sqrt(model1_mse / i), math.sqrt(model2_mse / i),
  315. baseline_psnr / i,
  316. model1_psnr / i, model2_psnr / i
  317. ))
  318. else
  319. io.stdout:write(
  320. string.format("%d/%d; model1_time=%.2f, model2_time=%.2f, model1_rmse=%f, model2_rmse=%f, model1_psnr=%f, model2_psnr=%f \r",
  321. i, #x,
  322. model1_time,
  323. model2_time,
  324. math.sqrt(model1_mse / i), math.sqrt(model2_mse / i),
  325. model1_psnr / i, model2_psnr / i
  326. ))
  327. end
  328. else
  329. if baseline_output then
  330. io.stdout:write(
  331. string.format("%d/%d; model1_time=%.2f, baseline_rmse=%f, model1_rmse=%f, baseline_psnr=%f, model1_psnr=%f \r",
  332. i, #x,
  333. model1_time,
  334. math.sqrt(baseline_mse / i), math.sqrt(model1_mse / i),
  335. baseline_psnr / i, model1_psnr / i
  336. ))
  337. else
  338. io.stdout:write(
  339. string.format("%d/%d; model1_time=%.2f, model1_rmse=%f, model1_psnr=%f \r",
  340. i, #x,
  341. model1_time,
  342. math.sqrt(model1_mse / i), model1_psnr / i
  343. ))
  344. end
  345. end
  346. io.stdout:flush()
  347. end
  348. end
  349. if opt.save_info then
  350. local fp = io.open(path.join(opt.output_dir, "benchmark.txt"), "w")
  351. fp:write("options : " .. cjson.encode(opt) .. "\n")
  352. if baseline_psnr > 0 then
  353. fp:write(string.format("baseline: RMSE = %.3f, PSNR = %.3f\n",
  354. math.sqrt(baseline_mse / #x), baseline_psnr / #x))
  355. end
  356. if model1_psnr > 0 then
  357. fp:write(string.format("model1 : RMSE = %.3f, PSNR = %.3f, evaluation time = %.3f\n",
  358. math.sqrt(model1_mse / #x), model1_psnr / #x, model1_time))
  359. end
  360. if model2_psnr > 0 then
  361. fp:write(string.format("model2 : RMSE = %.3f, PSNR = %.3f, evaluation time = %.3f\n",
  362. math.sqrt(model2_mse / #x), model2_psnr / #x, model2_time))
  363. end
  364. fp:close()
  365. end
  366. io.stdout:write("\n")
  367. end
  368. local function load_data(test_dir)
  369. local test_x = {}
  370. local files = dir.getfiles(test_dir, "*.*")
  371. for i = 1, #files do
  372. local name = path.basename(files[i])
  373. local e = path.extension(name)
  374. local base = name:sub(0, name:len() - e:len())
  375. local img = image_loader.load_float(files[i])
  376. if img then
  377. table.insert(test_x, {image = iproc.crop_mod4(img),
  378. basename = base})
  379. end
  380. if opt.show_progress then
  381. xlua.progress(i, #files)
  382. end
  383. end
  384. return test_x
  385. end
  386. function load_noise_scale_model(model_dir, noise_level, force_cudnn)
  387. local f = path.join(model_dir, string.format("noise%d_scale2.0x_model.t7", opt.noise_level))
  388. local s1, noise_scale = pcall(w2nn.load_model, f, force_cudnn)
  389. local model = {}
  390. if not s1 then
  391. f = path.join(model_dir, string.format("noise%d_model.t7", opt.noise_level))
  392. local noise
  393. s1, noise = pcall(w2nn.load_model, f, force_cudnn)
  394. if not s1 then
  395. model.noise_model = nil
  396. print(model_dir .. "'s noise model is not found. benchmark will use only scale model.")
  397. else
  398. model.noise_model = noise
  399. end
  400. f = path.join(model_dir, "scale2.0x_model.t7")
  401. local scale
  402. s1, scale = pcall(w2nn.load_model, f, force_cudnn)
  403. if not s1 then
  404. error(model_dir .. ": load error")
  405. return nil
  406. end
  407. model.scale_model = scale
  408. else
  409. model.noise_scale_model = noise_scale
  410. end
  411. return model
  412. end
  413. if opt.show_progress then
  414. print(opt)
  415. end
  416. if opt.method == "scale" then
  417. local f1 = path.join(opt.model1_dir, "scale2.0x_model.t7")
  418. local f2 = path.join(opt.model2_dir, "scale2.0x_model.t7")
  419. local s1, model1 = pcall(w2nn.load_model, f1, opt.force_cudnn)
  420. local s2, model2 = pcall(w2nn.load_model, f2, opt.force_cudnn)
  421. if not s1 then
  422. error("Load error: " .. f1)
  423. end
  424. if not s2 then
  425. model2 = nil
  426. end
  427. local test_x = load_data(opt.dir)
  428. benchmark(opt, test_x, transform_scale, model1, model2)
  429. elseif opt.method == "noise" then
  430. local f1 = path.join(opt.model1_dir, string.format("noise%d_model.t7", opt.noise_level))
  431. local f2 = path.join(opt.model2_dir, string.format("noise%d_model.t7", opt.noise_level))
  432. local s1, model1 = pcall(w2nn.load_model, f1, opt.force_cudnn)
  433. local s2, model2 = pcall(w2nn.load_model, f2, opt.force_cudnn)
  434. if not s1 then
  435. error("Load error: " .. f1)
  436. end
  437. if not s2 then
  438. model2 = nil
  439. end
  440. local test_x = load_data(opt.dir)
  441. benchmark(opt, test_x, transform_jpeg, model1, model2)
  442. elseif opt.method == "noise_scale" then
  443. local model2 = nil
  444. local model1 = load_noise_scale_model(opt.model1_dir, opt.noise_level, opt.force_cudnn)
  445. if opt.model2_dir:len() > 0 then
  446. model2 = load_noise_scale_model(opt.model2_dir, opt.noise_level, opt.force_cudnn)
  447. end
  448. local test_x = load_data(opt.dir)
  449. benchmark(opt, test_x, transform_scale_jpeg, model1, model2)
  450. end