benchmark.lua 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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("-file", "", 'test image file list')
  17. cmd:option("-model1_dir", "./models/anime_style_art_rgb", 'model1 directory')
  18. cmd:option("-model2_dir", "", 'model2 directory (optional)')
  19. cmd:option("-method", "scale", '(scale|noise|noise_scale|user|diff|scale4)')
  20. cmd:option("-filter", "Catrom", "downscaling filter (Box|Lanczos|Catrom(Bicubic))")
  21. cmd:option("-resize_blur", 1.0, 'blur parameter for resize')
  22. cmd:option("-color", "y", '(rgb|y|r|g|b)')
  23. cmd:option("-noise_level", 1, 'model noise level')
  24. cmd:option("-jpeg_quality", 75, 'jpeg quality')
  25. cmd:option("-jpeg_times", 1, 'jpeg compression times')
  26. cmd:option("-jpeg_quality_down", 5, 'value of jpeg quality to decrease each times')
  27. cmd:option("-range_bug", 0, 'Reproducing the dynamic range bug that is caused by MATLAB\'s rgb2ycbcr(1|0)')
  28. cmd:option("-save_image", 0, 'save converted images')
  29. cmd:option("-save_baseline_image", 0, 'save baseline images')
  30. cmd:option("-output_dir", "./", 'output directroy')
  31. cmd:option("-show_progress", 1, 'show progressbar')
  32. cmd:option("-baseline_filter", "Catrom", 'baseline interpolation (Box|Lanczos|Catrom(Bicubic))')
  33. cmd:option("-save_info", 0, 'save score and parameters to benchmark.txt')
  34. cmd:option("-save_all", 0, 'group -save_info, -save_image and -save_baseline_image option')
  35. cmd:option("-thread", -1, 'number of CPU threads')
  36. cmd:option("-tta", 0, 'use tta')
  37. cmd:option("-tta_level", 8, 'tta level')
  38. cmd:option("-crop_size", 256, 'patch size per process')
  39. cmd:option("-batch_size", 1, 'batch_size')
  40. cmd:option("-force_cudnn", 0, 'use cuDNN backend')
  41. cmd:option("-yuv420", 0, 'use yuv420 jpeg')
  42. cmd:option("-name", "", 'model name for user method')
  43. cmd:option("-x_dir", "", 'input image for user method')
  44. cmd:option("-y_dir", "", 'groundtruth image for user method. filename must be the same as x_dir')
  45. cmd:option("-x_file", "", 'input image for user method')
  46. cmd:option("-y_file", "", 'groundtruth image for user method. filename must be the same as x_file')
  47. cmd:option("-border", 0, 'border px that will removed')
  48. cmd:option("-metric", "", '(jaccard)')
  49. local function to_bool(settings, name)
  50. if settings[name] == 1 then
  51. settings[name] = true
  52. else
  53. settings[name] = false
  54. end
  55. end
  56. local opt = cmd:parse(arg)
  57. torch.setdefaulttensortype('torch.FloatTensor')
  58. if cudnn then
  59. cudnn.fastest = true
  60. cudnn.benchmark = true
  61. end
  62. to_bool(opt, "force_cudnn")
  63. to_bool(opt, "yuv420")
  64. to_bool(opt, "save_all")
  65. to_bool(opt, "tta")
  66. if opt.save_all then
  67. opt.save_image = true
  68. opt.save_info = true
  69. opt.save_baseline_image = true
  70. else
  71. to_bool(opt, "save_image")
  72. to_bool(opt, "save_info")
  73. to_bool(opt, "save_baseline_image")
  74. end
  75. to_bool(opt, "show_progress")
  76. if opt.thread > 0 then
  77. torch.setnumthreads(tonumber(opt.thread))
  78. end
  79. if opt.output_dir:len() > 0 then
  80. dir.makepath(opt.output_dir)
  81. end
  82. -- patch for lua52
  83. if not math.log10 then
  84. math.log10 = function(x) return math.log(x, 10) end
  85. end
  86. local function rgb2y_matlab(x)
  87. local y = torch.Tensor(1, x:size(2), x:size(3)):zero()
  88. x = iproc.byte2float(x)
  89. y:add(x[1] * 65.481)
  90. y:add(x[2] * 128.553)
  91. y:add(x[3] * 24.966)
  92. y:add(16.0)
  93. return y:byte():float()
  94. end
  95. local function RGBMSE(x1, x2)
  96. x1 = iproc.float2byte(x1):float()
  97. x2 = iproc.float2byte(x2):float()
  98. return (x1 - x2):pow(2):mean()
  99. end
  100. local function CHMSE(x1, x2, ch)
  101. x1 = iproc.float2byte(x1):float()
  102. x2 = iproc.float2byte(x2):float()
  103. return (x1[ch] - x2[ch]):pow(2):mean()
  104. end
  105. local function YMSE(x1, x2)
  106. if opt.range_bug == 1 then
  107. local x1_2 = rgb2y_matlab(x1)
  108. local x2_2 = rgb2y_matlab(x2)
  109. return (x1_2 - x2_2):pow(2):mean()
  110. else
  111. local x1_2 = image.rgb2y(x1):mul(255.0)
  112. local x2_2 = image.rgb2y(x2):mul(255.0)
  113. return (x1_2 - x2_2):pow(2):mean()
  114. end
  115. end
  116. local function MSE(x1, x2, color)
  117. if color == "y" then
  118. return YMSE(x1, x2)
  119. elseif color == "r" then
  120. return CHMSE(x1, x2, 1)
  121. elseif color == "g" then
  122. return CHMSE(x1, x2, 2)
  123. elseif color == "b" then
  124. return CHMSE(x1, x2, 3)
  125. else
  126. return RGBMSE(x1, x2)
  127. end
  128. end
  129. local function PSNR(x1, x2, color)
  130. local mse = math.max(MSE(x1, x2, color), 1)
  131. return 10 * math.log10((255.0 * 255.0) / mse)
  132. end
  133. local function MSE2PSNR(mse)
  134. return 10 * math.log10((255.0 * 255.0) / math.max(mse, 1))
  135. end
  136. local function transform_jpeg(x, opt)
  137. for i = 1, opt.jpeg_times do
  138. jpeg = gm.Image(x, "RGB", "DHW")
  139. jpeg:format("jpeg")
  140. if opt.yuv420 then
  141. jpeg:samplingFactors({2.0, 1.0, 1.0})
  142. else
  143. jpeg:samplingFactors({1.0, 1.0, 1.0})
  144. end
  145. blob, len = jpeg:toBlob(opt.jpeg_quality - (i - 1) * opt.jpeg_quality_down)
  146. jpeg:fromBlob(blob, len)
  147. x = jpeg:toTensor("byte", "RGB", "DHW")
  148. end
  149. return iproc.byte2float(x)
  150. end
  151. local function baseline_scale(x, filter)
  152. return iproc.scale(x,
  153. x:size(3) * 2.0,
  154. x:size(2) * 2.0,
  155. filter)
  156. end
  157. local function baseline_scale4(x, filter)
  158. return iproc.scale(x,
  159. x:size(3) * 4.0,
  160. x:size(2) * 4.0,
  161. filter)
  162. end
  163. local function transform_scale(x, opt)
  164. return iproc.scale(x,
  165. x:size(3) * 0.5,
  166. x:size(2) * 0.5,
  167. opt.filter, opt.resize_blur)
  168. end
  169. local function transform_scale4(x, opt)
  170. return iproc.scale(x,
  171. x:size(3) * 0.25,
  172. x:size(2) * 0.25,
  173. opt.filter, opt.resize_blur)
  174. end
  175. local function transform_scale_jpeg(x, opt)
  176. x = iproc.scale(x,
  177. x:size(3) * 0.5,
  178. x:size(2) * 0.5,
  179. opt.filter, opt.resize_blur)
  180. for i = 1, opt.jpeg_times do
  181. jpeg = gm.Image(x, "RGB", "DHW")
  182. jpeg:format("jpeg")
  183. if opt.yuv420 then
  184. jpeg:samplingFactors({2.0, 1.0, 1.0})
  185. else
  186. jpeg:samplingFactors({1.0, 1.0, 1.0})
  187. end
  188. blob, len = jpeg:toBlob(opt.jpeg_quality - (i - 1) * opt.jpeg_quality_down)
  189. jpeg:fromBlob(blob, len)
  190. x = jpeg:toTensor("byte", "RGB", "DHW")
  191. end
  192. return iproc.byte2float(x)
  193. end
  194. local function remove_border(x, border)
  195. return iproc.crop(x,
  196. border, border,
  197. x:size(3) - border,
  198. x:size(2) - border)
  199. end
  200. local function create_metric(metric)
  201. if metric and metric:len() > 0 then
  202. if metric == "jaccard" then
  203. return {
  204. name = "jaccard",
  205. func = function (a, b)
  206. local ga = iproc.rgb2y(a)
  207. local gb = iproc.rgb2y(b)
  208. local ba = torch.Tensor():resizeAs(ga)
  209. local bb = torch.Tensor():resizeAs(gb)
  210. ba:zero()
  211. bb:zero()
  212. ba[torch.gt(ga, 0.5)] = 1.0
  213. bb[torch.gt(gb, 0.5)] = 1.0
  214. local num_a = ba:sum()
  215. local num_b = bb:sum()
  216. local a_and_b = ba:cmul(bb):sum()
  217. local abab = (num_a + num_b - a_and_b)
  218. if abab > 0 then
  219. return (a_and_b / abab)
  220. else
  221. return 1
  222. end
  223. end}
  224. else
  225. error("unknown metric: " .. metric)
  226. end
  227. else
  228. return nil
  229. end
  230. end
  231. local function benchmark(opt, x, model1, model2)
  232. local mse1, mse2, am1, am2
  233. local won = {0, 0}
  234. local model1_mse = 0
  235. local model2_mse = 0
  236. local baseline_mse = 0
  237. local model1_psnr = 0
  238. local model2_psnr = 0
  239. local baseline_psnr = 0
  240. local model1_time = 0
  241. local model2_time = 0
  242. local scale_f = reconstruct.scale
  243. local image_f = reconstruct.image
  244. local detail_fp = nil
  245. local am = nil
  246. local model1_am = 0
  247. local model2_am = 0
  248. if opt.method == "user" or opt.method == "diff" then
  249. am = create_metric(opt.metric)
  250. end
  251. if opt.save_info then
  252. detail_fp = io.open(path.join(opt.output_dir, "benchmark_details.txt"), "w")
  253. end
  254. if opt.tta then
  255. scale_f = function(model, scale, x, block_size, batch_size)
  256. return reconstruct.scale_tta(model, opt.tta_level,
  257. scale, x, block_size, batch_size)
  258. end
  259. image_f = function(model, x, block_size, batch_size)
  260. return reconstruct.image_tta(model, opt.tta_level,
  261. x, block_size, batch_size)
  262. end
  263. end
  264. for i = 1, #x do
  265. if i % 10 == 0 then
  266. collectgarbage()
  267. end
  268. local basename = x[i].basename
  269. local input, model1_output, model2_output, baseline_output, ground_truth
  270. if opt.method == "scale" then
  271. input = transform_scale(iproc.byte2float(x[i].y), opt)
  272. ground_truth = iproc.byte2float(x[i].y)
  273. if opt.force_cudnn and i == 1 then -- run cuDNN benchmark first
  274. model1_output = scale_f(model1, 2.0, input, opt.crop_size, opt.batch_size)
  275. if model2 then
  276. model2_output = scale_f(model2, 2.0, input, opt.crop_size, opt.batch_size)
  277. end
  278. end
  279. t = sys.clock()
  280. model1_output = scale_f(model1, 2.0, input, opt.crop_size, opt.batch_size)
  281. model1_time = model1_time + (sys.clock() - t)
  282. if model2 then
  283. t = sys.clock()
  284. model2_output = scale_f(model2, 2.0, input, opt.crop_size, opt.batch_size)
  285. model2_time = model2_time + (sys.clock() - t)
  286. end
  287. baseline_output = baseline_scale(input, opt.baseline_filter)
  288. elseif opt.method == "scale4" then
  289. input = transform_scale4(iproc.byte2float(x[i].y), opt)
  290. ground_truth = iproc.byte2float(x[i].y)
  291. if opt.force_cudnn and i == 1 then -- run cuDNN benchmark first
  292. model1_output = scale_f(model1, 2.0, input, opt.crop_size, opt.batch_size)
  293. if model2 then
  294. model2_output = scale_f(model2, 2.0, input, opt.crop_size, opt.batch_size)
  295. end
  296. end
  297. t = sys.clock()
  298. model1_output = scale_f(model1, 2.0, input, opt.crop_size, opt.batch_size)
  299. model1_output = scale_f(model1, 2.0, model1_output, opt.crop_size, opt.batch_size)
  300. model1_time = model1_time + (sys.clock() - t)
  301. if model2 then
  302. t = sys.clock()
  303. model2_output = scale_f(model2, 2.0, input, opt.crop_size, opt.batch_size)
  304. model2_output = scale_f(model2, 2.0, model2_output, opt.crop_size, opt.batch_size)
  305. model2_time = model2_time + (sys.clock() - t)
  306. end
  307. baseline_output = baseline_scale4(input, opt.baseline_filter)
  308. elseif opt.method == "noise" then
  309. input = transform_jpeg(iproc.byte2float(x[i].y), opt)
  310. ground_truth = iproc.byte2float(x[i].y)
  311. if opt.force_cudnn and i == 1 then
  312. model1_output = image_f(model1, input, opt.crop_size, opt.batch_size)
  313. if model2 then
  314. model2_output = image_f(model2, input, opt.crop_size, opt.batch_size)
  315. end
  316. end
  317. t = sys.clock()
  318. model1_output = image_f(model1, input, opt.crop_size, opt.batch_size)
  319. model1_time = model1_time + (sys.clock() - t)
  320. if model2 then
  321. t = sys.clock()
  322. model2_output = image_f(model2, input, opt.crop_size, opt.batch_size)
  323. model2_time = model2_time + (sys.clock() - t)
  324. end
  325. baseline_output = input
  326. elseif opt.method == "noise_scale" then
  327. input = transform_scale_jpeg(iproc.byte2float(x[i].y), opt)
  328. ground_truth = iproc.byte2float(x[i].y)
  329. if opt.force_cudnn and i == 1 then
  330. if model1.noise_scale_model then
  331. model1_output = scale_f(model1.noise_scale_model, 2.0,
  332. input, opt.crop_size, opt.batch_size)
  333. else
  334. if model1.noise_model then
  335. model1_output = image_f(model1.noise_model, input, opt.crop_size, opt.batch_size)
  336. else
  337. model1_output = input
  338. end
  339. model1_output = scale_f(model1.scale_model, 2.0, model1_output,
  340. opt.crop_size, opt.batch_size)
  341. end
  342. if model2 then
  343. if model2.noise_scale_model then
  344. model2_output = scale_f(model2.noise_scale_model, 2.0,
  345. input, opt.crop_size, opt.batch_size)
  346. else
  347. if model2.noise_model then
  348. model2_output = image_f(model2.noise_model, input,
  349. opt.crop_size, opt.batch_size)
  350. else
  351. model2_output = input
  352. end
  353. model2_output = scale_f(model2.scale_model, 2.0, model2_output,
  354. opt.crop_size, opt.batch_size)
  355. end
  356. end
  357. end
  358. t = sys.clock()
  359. if model1.noise_scale_model then
  360. model1_output = scale_f(model1.noise_scale_model, 2.0,
  361. input, opt.crop_size, opt.batch_size)
  362. else
  363. if model1.noise_model then
  364. model1_output = image_f(model1.noise_model, input, opt.crop_size, opt.batch_size)
  365. else
  366. model1_output = input
  367. end
  368. model1_output = scale_f(model1.scale_model, 2.0, model1_output,
  369. opt.crop_size, opt.batch_size)
  370. end
  371. model1_time = model1_time + (sys.clock() - t)
  372. if model2 then
  373. t = sys.clock()
  374. if model2.noise_scale_model then
  375. model2_output = scale_f(model2.noise_scale_model, 2.0,
  376. input, opt.crop_size, opt.batch_size)
  377. else
  378. if model2.noise_model then
  379. model2_output = image_f(model2.noise_model, input,
  380. opt.crop_size, opt.batch_size)
  381. else
  382. model2_output = input
  383. end
  384. model2_output = scale_f(model2.scale_model, 2.0, model2_output,
  385. opt.crop_size, opt.batch_size)
  386. end
  387. model2_time = model2_time + (sys.clock() - t)
  388. end
  389. baseline_output = baseline_scale(input, opt.baseline_filter)
  390. elseif opt.method == "user" then
  391. input = iproc.byte2float(x[i].x)
  392. ground_truth = iproc.byte2float(x[i].y)
  393. local y_scale = ground_truth:size(2) / input:size(2)
  394. if y_scale > 1 then
  395. if opt.force_cudnn and i == 1 then
  396. model1_output = scale_f(model1, y_scale, input, opt.crop_size, opt.batch_size)
  397. if model2 then
  398. model2_output = scale_f(model2, y_scale, input, opt.crop_size, opt.batch_size)
  399. end
  400. end
  401. t = sys.clock()
  402. model1_output = scale_f(model1, y_scale, input, opt.crop_size, opt.batch_size)
  403. model1_time = model1_time + (sys.clock() - t)
  404. if model2 then
  405. t = sys.clock()
  406. model2_output = scale_f(model2, y_scale, input, opt.crop_size, opt.batch_size)
  407. model2_time = model2_time + (sys.clock() - t)
  408. end
  409. else
  410. if opt.force_cudnn and i == 1 then
  411. model1_output = image_f(model1, input, opt.crop_size, opt.batch_size)
  412. if model2 then
  413. model2_output = image_f(model2, input, opt.crop_size, opt.batch_size)
  414. end
  415. end
  416. t = sys.clock()
  417. model1_output = image_f(model1, input, opt.crop_size, opt.batch_size)
  418. model1_time = model1_time + (sys.clock() - t)
  419. if model2 then
  420. t = sys.clock()
  421. model2_output = image_f(model2, input, opt.crop_size, opt.batch_size)
  422. model2_time = model2_time + (sys.clock() - t)
  423. end
  424. end
  425. elseif opt.method == "diff" then
  426. input = iproc.byte2float(x[i].x)
  427. ground_truth = iproc.byte2float(x[i].y)
  428. model1_output = input
  429. end
  430. if opt.border > 0 then
  431. ground_truth = remove_border(ground_truth, opt.border)
  432. model1_output = remove_border(model1_output, opt.border)
  433. end
  434. if am then
  435. am1 = am.func(ground_truth, model1_output)
  436. model1_am = model1_am + am1
  437. else
  438. mse1 = MSE(ground_truth, model1_output, opt.color)
  439. model1_mse = model1_mse + mse1
  440. model1_psnr = model1_psnr + MSE2PSNR(mse1)
  441. end
  442. local won_model = 1
  443. if model2 then
  444. if opt.border > 0 then
  445. model2_output = remove_border(model2_output, opt.border)
  446. end
  447. if am then
  448. am2 = am.func(ground_truth, model2_output)
  449. model2_am = model2_am + am2
  450. else
  451. mse2 = MSE(ground_truth, model2_output, opt.color)
  452. model2_mse = model2_mse + mse2
  453. model2_psnr = model2_psnr + MSE2PSNR(mse2)
  454. end
  455. if am then
  456. if am1 < am2 then
  457. won[1] = won[1] + 1
  458. elseif am1 > am2 then
  459. won[2] = won[2] + 1
  460. won_model = 2
  461. end
  462. else
  463. if mse1 < mse2 then
  464. won[1] = won[1] + 1
  465. elseif mse1 > mse2 then
  466. won[2] = won[2] + 1
  467. won_model = 2
  468. end
  469. end
  470. if detail_fp then
  471. if am then
  472. detail_fp:write(string.format("%s,%f,%d\n", x[i].basename, am1, am2, won_model))
  473. else
  474. detail_fp:write(string.format("%s,%f,%f,%d\n", x[i].basename,
  475. MSE2PSNR(mse1), MSE2PSNR(mse2), won_model))
  476. end
  477. end
  478. else
  479. if detail_fp then
  480. if am then
  481. detail_fp:write(string.format("%s,%f\n", x[i].basename, am1))
  482. else
  483. detail_fp:write(string.format("%s,%f\n", x[i].basename, MSE2PSNR(mse1)))
  484. end
  485. end
  486. end
  487. if baseline_output then
  488. baseline_output = remove_border(baseline_output, opt.border)
  489. mse = MSE(ground_truth, baseline_output, opt.color)
  490. baseline_mse = baseline_mse + mse
  491. baseline_psnr = baseline_psnr + MSE2PSNR(mse)
  492. end
  493. if opt.save_image then
  494. if opt.save_baseline_image and baseline_output then
  495. image.save(path.join(opt.output_dir, string.format("%s_baseline.png", basename)),
  496. baseline_output)
  497. end
  498. if model1_output then
  499. image.save(path.join(opt.output_dir, string.format("%s_model1.png", basename)),
  500. model1_output)
  501. end
  502. if model2_output then
  503. image.save(path.join(opt.output_dir, string.format("%s_model2.png", basename)),
  504. model2_output)
  505. end
  506. end
  507. if opt.show_progress or i == #x then
  508. if am then
  509. if model2 then
  510. io.stdout:write(
  511. string.format("%d/%d; model1_time=%.2f, model2_time=%.2f, model1_%s=%.3f, model2_%s=%.3f \r",
  512. i, #x,
  513. model1_time,
  514. model2_time,
  515. am.name, model1_am / i, am.name, model2_am / i
  516. ))
  517. else
  518. io.stdout:write(
  519. string.format("%d/%d; model1_time=%.2f, model1_%s=%.3f \r",
  520. i, #x,
  521. model1_time,
  522. am.name, model1_am / i
  523. ))
  524. end
  525. else
  526. if model2 then
  527. if baseline_output then
  528. io.stdout:write(
  529. string.format("%d/%d; model1_time=%.2f, model2_time=%.2f, baseline_rmse=%.3f, model1_rmse=%.3f, model2_rmse=%.3f, baseline_psnr=%.3f, model1_psnr=%.3f, model2_psnr=%.3f, model1_won=%d, model2_won=%d \r",
  530. i, #x,
  531. model1_time,
  532. model2_time,
  533. math.sqrt(baseline_mse / i),
  534. math.sqrt(model1_mse / i), math.sqrt(model2_mse / i),
  535. baseline_psnr / i,
  536. model1_psnr / i, model2_psnr / i,
  537. won[1], won[2]
  538. ))
  539. else
  540. io.stdout:write(
  541. string.format("%d/%d; model1_time=%.2f, model2_time=%.2f, model1_rmse=%.3f, model2_rmse=%.3f, model1_psnr=%.3f, model2_psnr=%.3f, model1_own=%d, model2_won=%d \r",
  542. i, #x,
  543. model1_time,
  544. model2_time,
  545. math.sqrt(model1_mse / i), math.sqrt(model2_mse / i),
  546. model1_psnr / i, model2_psnr / i,
  547. won[1], won[2]
  548. ))
  549. end
  550. else
  551. if baseline_output then
  552. io.stdout:write(
  553. string.format("%d/%d; model1_time=%.2f, baseline_rmse=%.3f, model1_rmse=%.3f, baseline_psnr=%.3f, model1_psnr=%.3f \r",
  554. i, #x,
  555. model1_time,
  556. math.sqrt(baseline_mse / i), math.sqrt(model1_mse / i),
  557. baseline_psnr / i, model1_psnr / i
  558. ))
  559. else
  560. io.stdout:write(
  561. string.format("%d/%d; model1_time=%.2f, model1_rmse=%.3f, model1_psnr=%.3f \r",
  562. i, #x,
  563. model1_time,
  564. math.sqrt(model1_mse / i), model1_psnr / i
  565. ))
  566. end
  567. end
  568. end
  569. io.stdout:flush()
  570. end
  571. end
  572. if opt.save_info then
  573. local fp = io.open(path.join(opt.output_dir, "benchmark.txt"), "w")
  574. fp:write("options : " .. cjson.encode(opt) .. "\n")
  575. if baseline_psnr > 0 then
  576. fp:write(string.format("baseline: RMSE = %.3f, PSNR = %.3f\n",
  577. math.sqrt(baseline_mse / #x), baseline_psnr / #x))
  578. end
  579. if model1_psnr > 0 then
  580. fp:write(string.format("model1 : RMSE = %.3f, PSNR = %.3f, evaluation time = %.3f\n",
  581. math.sqrt(model1_mse / #x), model1_psnr / #x, model1_time))
  582. end
  583. if model2_psnr > 0 then
  584. fp:write(string.format("model2 : RMSE = %.3f, PSNR = %.3f, evaluation time = %.3f\n",
  585. math.sqrt(model2_mse / #x), model2_psnr / #x, model2_time))
  586. end
  587. if model1_am > 0 then
  588. fp:write(string.format("model1 : %s = %.3f, evaluation time = %.3f\n",
  589. math.sqrt(model1_am / #x), model1_time))
  590. end
  591. if model2_am > 0 then
  592. fp:write(string.format("model2 : %s = %.3f, evaluation time = %.3f\n",
  593. math.sqrt(model2_am / #x), model2_time))
  594. end
  595. fp:close()
  596. if detail_fp then
  597. detail_fp:close()
  598. end
  599. end
  600. io.stdout:write("\n")
  601. end
  602. local function load_data_from_dir(test_dir)
  603. local test_x = {}
  604. local files = dir.getfiles(test_dir, "*.*")
  605. for i = 1, #files do
  606. local name = path.basename(files[i])
  607. local e = path.extension(name)
  608. local base = name:sub(0, name:len() - e:len())
  609. local img = image_loader.load_byte(files[i])
  610. if img then
  611. table.insert(test_x, {y = iproc.crop_mod4(img),
  612. basename = base})
  613. end
  614. if i % 10 == 0 then
  615. if opt.show_progress then
  616. xlua.progress(i, #files)
  617. end
  618. collectgarbage()
  619. end
  620. end
  621. return test_x
  622. end
  623. local function load_data_from_file(test_file)
  624. local test_x = {}
  625. local files = utils.split(file.read(test_file), "\n")
  626. for i = 1, #files do
  627. local name = path.basename(files[i])
  628. local e = path.extension(name)
  629. local base = name:sub(0, name:len() - e:len())
  630. local img = image_loader.load_byte(files[i])
  631. if img then
  632. table.insert(test_x, {y = iproc.crop_mod4(img),
  633. basename = base})
  634. end
  635. if i % 10 == 0 then
  636. if opt.show_progress then
  637. xlua.progress(i, #files)
  638. end
  639. collectgarbage()
  640. end
  641. end
  642. return test_x
  643. end
  644. local function get_basename(f)
  645. local name = path.basename(f)
  646. local e = path.extension(name)
  647. local base = name:sub(0, name:len() - e:len())
  648. return base
  649. end
  650. local function load_user_data(y_dir, y_file, x_dir, x_file)
  651. local test = {}
  652. local y_files
  653. local x_files
  654. if y_file:len() > 0 then
  655. y_files = utils.split(file.read(y_file), "\n")
  656. else
  657. y_files = dir.getfiles(y_dir, "*.*")
  658. end
  659. if x_file:len() > 0 then
  660. x_files = utils.split(file.read(x_file), "\n")
  661. else
  662. x_files = dir.getfiles(x_dir, "*.*")
  663. end
  664. local basename_db = {}
  665. for i = 1, #y_files do
  666. basename_db[get_basename(y_files[i])] = {y = y_files[i]}
  667. end
  668. for i = 1, #x_files do
  669. local key = get_basename(x_files[i])
  670. if basename_db[key] then
  671. basename_db[key].x = x_files[i]
  672. else
  673. error(string.format("%s is not found in %s", key, y_dir))
  674. end
  675. end
  676. for i = 1, #y_files do
  677. local key = get_basename(y_files[i])
  678. local d = basename_db[key]
  679. if not (d.x and d.y) then
  680. error(string.format("%s is not found in %s", key, x_dir))
  681. end
  682. end
  683. for i = 1, #y_files do
  684. local key = get_basename(y_files[i])
  685. local x = image_loader.load_byte(basename_db[key].x)
  686. local y = image_loader.load_byte(basename_db[key].y)
  687. if x and y then
  688. table.insert(test, {y = y,
  689. x = x,
  690. basename = key})
  691. end
  692. if i % 10 == 0 then
  693. if opt.show_progress then
  694. xlua.progress(i, #y_files)
  695. end
  696. collectgarbage()
  697. end
  698. end
  699. return test
  700. end
  701. function load_noise_scale_model(model_dir, noise_level, force_cudnn)
  702. local f = path.join(model_dir, string.format("noise%d_scale2.0x_model.t7", opt.noise_level))
  703. local s1, noise_scale = pcall(w2nn.load_model, f, force_cudnn)
  704. local model = {}
  705. if not s1 then
  706. f = path.join(model_dir, string.format("noise%d_model.t7", opt.noise_level))
  707. local noise
  708. s1, noise = pcall(w2nn.load_model, f, force_cudnn)
  709. if not s1 then
  710. model.noise_model = nil
  711. print(model_dir .. "'s noise model is not found. benchmark will use only scale model.")
  712. else
  713. model.noise_model = noise
  714. end
  715. f = path.join(model_dir, "scale2.0x_model.t7")
  716. local scale
  717. s1, scale = pcall(w2nn.load_model, f, force_cudnn)
  718. if not s1 then
  719. error(model_dir .. ": load error")
  720. return nil
  721. end
  722. model.scale_model = scale
  723. else
  724. model.noise_scale_model = noise_scale
  725. end
  726. return model
  727. end
  728. if opt.show_progress then
  729. print(opt)
  730. end
  731. if opt.method == "scale" or opt.method == "scale4" then
  732. local f1 = path.join(opt.model1_dir, "scale2.0x_model.t7")
  733. local f2 = path.join(opt.model2_dir, "scale2.0x_model.t7")
  734. local s1, model1 = pcall(w2nn.load_model, f1, opt.force_cudnn)
  735. local s2, model2 = pcall(w2nn.load_model, f2, opt.force_cudnn)
  736. if not s1 then
  737. error("Load error: " .. f1)
  738. end
  739. if not s2 then
  740. model2 = nil
  741. end
  742. local test_x
  743. if opt.file:len() > 0 then
  744. test_x = load_data_from_file(opt.file)
  745. else
  746. test_x = load_data_from_dir(opt.dir)
  747. end
  748. benchmark(opt, test_x, model1, model2)
  749. elseif opt.method == "noise" then
  750. local f1 = path.join(opt.model1_dir, string.format("noise%d_model.t7", opt.noise_level))
  751. local f2 = path.join(opt.model2_dir, string.format("noise%d_model.t7", opt.noise_level))
  752. local s1, model1 = pcall(w2nn.load_model, f1, opt.force_cudnn)
  753. local s2, model2 = pcall(w2nn.load_model, f2, opt.force_cudnn)
  754. if not s1 then
  755. error("Load error: " .. f1)
  756. end
  757. if not s2 then
  758. model2 = nil
  759. end
  760. local test_x
  761. if opt.file:len() > 0 then
  762. test_x = load_data_from_file(opt.file)
  763. else
  764. test_x = load_data_from_dir(opt.dir)
  765. end
  766. benchmark(opt, test_x, model1, model2)
  767. elseif opt.method == "noise_scale" then
  768. local model2 = nil
  769. local model1 = load_noise_scale_model(opt.model1_dir, opt.noise_level, opt.force_cudnn)
  770. if opt.model2_dir:len() > 0 then
  771. model2 = load_noise_scale_model(opt.model2_dir, opt.noise_level, opt.force_cudnn)
  772. end
  773. local test_x
  774. if opt.file:len() > 0 then
  775. test_x = load_data_from_file(opt.file)
  776. else
  777. test_x = load_data_from_dir(opt.dir)
  778. end
  779. benchmark(opt, test_x, model1, model2)
  780. elseif opt.method == "user" then
  781. local f1 = path.join(opt.model1_dir, string.format("%s_model.t7", opt.name))
  782. local f2 = path.join(opt.model2_dir, string.format("%s_model.t7", opt.name))
  783. local s1, model1 = pcall(w2nn.load_model, f1, opt.force_cudnn)
  784. local s2, model2 = pcall(w2nn.load_model, f2, opt.force_cudnn)
  785. if not s1 then
  786. error("Load error: " .. f1)
  787. end
  788. if not s2 then
  789. model2 = nil
  790. end
  791. local test = load_user_data(opt.y_dir, opt.y_file, opt.x_dir, opt.x_file)
  792. benchmark(opt, test, model1, model2)
  793. elseif opt.method == "diff" then
  794. local test = load_user_data(opt.y_dir, opt.y_file, opt.x_dir, opt.x_file)
  795. benchmark(opt, test, nil, nil)
  796. end