benchmark.lua 13 KB

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