benchmark.lua 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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)')
  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", 128, '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. local function to_bool(settings, name)
  48. if settings[name] == 1 then
  49. settings[name] = true
  50. else
  51. settings[name] = false
  52. end
  53. end
  54. local opt = cmd:parse(arg)
  55. torch.setdefaulttensortype('torch.FloatTensor')
  56. if cudnn then
  57. cudnn.fastest = true
  58. cudnn.benchmark = true
  59. end
  60. to_bool(opt, "force_cudnn")
  61. to_bool(opt, "yuv420")
  62. to_bool(opt, "save_all")
  63. to_bool(opt, "tta")
  64. if opt.save_all then
  65. opt.save_image = true
  66. opt.save_info = true
  67. opt.save_baseline_image = true
  68. else
  69. to_bool(opt, "save_image")
  70. to_bool(opt, "save_info")
  71. to_bool(opt, "save_baseline_image")
  72. end
  73. to_bool(opt, "show_progress")
  74. if opt.thread > 0 then
  75. torch.setnumthreads(tonumber(opt.thread))
  76. end
  77. if opt.output_dir:len() > 0 then
  78. dir.makepath(opt.output_dir)
  79. end
  80. local function rgb2y_matlab(x)
  81. local y = torch.Tensor(1, x:size(2), x:size(3)):zero()
  82. x = iproc.byte2float(x)
  83. y:add(x[1] * 65.481)
  84. y:add(x[2] * 128.553)
  85. y:add(x[3] * 24.966)
  86. y:add(16.0)
  87. return y:byte():float()
  88. end
  89. local function RGBMSE(x1, x2)
  90. x1 = iproc.float2byte(x1):float()
  91. x2 = iproc.float2byte(x2):float()
  92. return (x1 - x2):pow(2):mean()
  93. end
  94. local function CHMSE(x1, x2, ch)
  95. x1 = iproc.float2byte(x1):float()
  96. x2 = iproc.float2byte(x2):float()
  97. return (x1[ch] - x2[ch]):pow(2):mean()
  98. end
  99. local function YMSE(x1, x2)
  100. if opt.range_bug == 1 then
  101. local x1_2 = rgb2y_matlab(x1)
  102. local x2_2 = rgb2y_matlab(x2)
  103. return (x1_2 - x2_2):pow(2):mean()
  104. else
  105. local x1_2 = image.rgb2y(x1):mul(255.0)
  106. local x2_2 = image.rgb2y(x2):mul(255.0)
  107. return (x1_2 - x2_2):pow(2):mean()
  108. end
  109. end
  110. local function MSE(x1, x2, color)
  111. if color == "y" then
  112. return YMSE(x1, x2)
  113. elseif color == "r" then
  114. return CHMSE(x1, x2, 1)
  115. elseif color == "g" then
  116. return CHMSE(x1, x2, 2)
  117. elseif color == "b" then
  118. return CHMSE(x1, x2, 3)
  119. else
  120. return RGBMSE(x1, x2)
  121. end
  122. end
  123. local function PSNR(x1, x2, color)
  124. local mse = math.max(MSE(x1, x2, color), 1)
  125. return 10 * math.log10((255.0 * 255.0) / mse)
  126. end
  127. local function MSE2PSNR(mse)
  128. return 10 * math.log10((255.0 * 255.0) / math.max(mse, 1))
  129. end
  130. local function transform_jpeg(x, opt)
  131. for i = 1, opt.jpeg_times do
  132. jpeg = gm.Image(x, "RGB", "DHW")
  133. jpeg:format("jpeg")
  134. if opt.yuv420 then
  135. jpeg:samplingFactors({2.0, 1.0, 1.0})
  136. else
  137. jpeg:samplingFactors({1.0, 1.0, 1.0})
  138. end
  139. blob, len = jpeg:toBlob(opt.jpeg_quality - (i - 1) * opt.jpeg_quality_down)
  140. jpeg:fromBlob(blob, len)
  141. x = jpeg:toTensor("byte", "RGB", "DHW")
  142. end
  143. return iproc.byte2float(x)
  144. end
  145. local function baseline_scale(x, filter)
  146. return iproc.scale(x,
  147. x:size(3) * 2.0,
  148. x:size(2) * 2.0,
  149. filter)
  150. end
  151. local function transform_scale(x, opt)
  152. return iproc.scale(x,
  153. x:size(3) * 0.5,
  154. x:size(2) * 0.5,
  155. opt.filter, opt.resize_blur)
  156. end
  157. local function transform_scale_jpeg(x, opt)
  158. x = iproc.scale(x,
  159. x:size(3) * 0.5,
  160. x:size(2) * 0.5,
  161. opt.filter, opt.resize_blur)
  162. for i = 1, opt.jpeg_times do
  163. jpeg = gm.Image(x, "RGB", "DHW")
  164. jpeg:format("jpeg")
  165. if opt.yuv420 then
  166. jpeg:samplingFactors({2.0, 1.0, 1.0})
  167. else
  168. jpeg:samplingFactors({1.0, 1.0, 1.0})
  169. end
  170. blob, len = jpeg:toBlob(opt.jpeg_quality - (i - 1) * opt.jpeg_quality_down)
  171. jpeg:fromBlob(blob, len)
  172. x = jpeg:toTensor("byte", "RGB", "DHW")
  173. end
  174. return iproc.byte2float(x)
  175. end
  176. local function benchmark(opt, x, model1, model2)
  177. local mse1, mse2
  178. local won = {0, 0}
  179. local model1_mse = 0
  180. local model2_mse = 0
  181. local baseline_mse = 0
  182. local model1_psnr = 0
  183. local model2_psnr = 0
  184. local baseline_psnr = 0
  185. local model1_time = 0
  186. local model2_time = 0
  187. local scale_f = reconstruct.scale
  188. local image_f = reconstruct.image
  189. local detail_fp = nil
  190. if opt.save_info then
  191. detail_fp = io.open(path.join(opt.output_dir, "benchmark_details.txt"), "w")
  192. end
  193. if opt.tta then
  194. scale_f = function(model, scale, x, block_size, batch_size)
  195. return reconstruct.scale_tta(model, opt.tta_level,
  196. scale, x, block_size, batch_size)
  197. end
  198. image_f = function(model, x, block_size, batch_size)
  199. return reconstruct.image_tta(model, opt.tta_level,
  200. x, block_size, batch_size)
  201. end
  202. end
  203. for i = 1, #x do
  204. local basename = x[i].basename
  205. local input, model1_output, model2_output, baseline_output, ground_truth
  206. if opt.method == "scale" then
  207. input = transform_scale(x[i].y, opt)
  208. ground_truth = x[i].y
  209. if opt.force_cudnn and i == 1 then -- run cuDNN benchmark first
  210. model1_output = scale_f(model1, 2.0, input, opt.crop_size, opt.batch_size)
  211. if model2 then
  212. model2_output = scale_f(model2, 2.0, input, opt.crop_size, opt.batch_size)
  213. end
  214. end
  215. t = sys.clock()
  216. model1_output = scale_f(model1, 2.0, input, opt.crop_size, opt.batch_size)
  217. model1_time = model1_time + (sys.clock() - t)
  218. if model2 then
  219. t = sys.clock()
  220. model2_output = scale_f(model2, 2.0, input, opt.crop_size, opt.batch_size)
  221. model2_time = model2_time + (sys.clock() - t)
  222. end
  223. baseline_output = baseline_scale(input, opt.baseline_filter)
  224. elseif opt.method == "noise" then
  225. input = transform_jpeg(x[i].y, opt)
  226. ground_truth = x[i].y
  227. if opt.force_cudnn and i == 1 then
  228. model1_output = image_f(model1, input, opt.crop_size, opt.batch_size)
  229. if model2 then
  230. model2_output = image_f(model2, input, opt.crop_size, opt.batch_size)
  231. end
  232. end
  233. t = sys.clock()
  234. model1_output = image_f(model1, input, opt.crop_size, opt.batch_size)
  235. model1_time = model1_time + (sys.clock() - t)
  236. if model2 then
  237. t = sys.clock()
  238. model2_output = image_f(model2, input, opt.crop_size, opt.batch_size)
  239. model2_time = model2_time + (sys.clock() - t)
  240. end
  241. baseline_output = input
  242. elseif opt.method == "noise_scale" then
  243. input = transform_scale_jpeg(x[i].y, opt)
  244. ground_truth = x[i].y
  245. if opt.force_cudnn and i == 1 then
  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. if model2 then
  259. if model2.noise_scale_model then
  260. model2_output = scale_f(model2.noise_scale_model, 2.0,
  261. input, opt.crop_size, opt.batch_size)
  262. else
  263. if model2.noise_model then
  264. model2_output = image_f(model2.noise_model, input,
  265. opt.crop_size, opt.batch_size)
  266. else
  267. model2_output = input
  268. end
  269. model2_output = scale_f(model2.scale_model, 2.0, model2_output,
  270. opt.crop_size, opt.batch_size)
  271. end
  272. end
  273. end
  274. t = sys.clock()
  275. if model1.noise_scale_model then
  276. model1_output = scale_f(model1.noise_scale_model, 2.0,
  277. input, opt.crop_size, opt.batch_size)
  278. else
  279. if model1.noise_model then
  280. model1_output = image_f(model1.noise_model, input, opt.crop_size, opt.batch_size)
  281. else
  282. model1_output = input
  283. end
  284. model1_output = scale_f(model1.scale_model, 2.0, model1_output,
  285. opt.crop_size, opt.batch_size)
  286. end
  287. model1_time = model1_time + (sys.clock() - t)
  288. if model2 then
  289. t = sys.clock()
  290. if model2.noise_scale_model then
  291. model2_output = scale_f(model2.noise_scale_model, 2.0,
  292. input, opt.crop_size, opt.batch_size)
  293. else
  294. if model2.noise_model then
  295. model2_output = image_f(model2.noise_model, input,
  296. opt.crop_size, opt.batch_size)
  297. else
  298. model2_output = input
  299. end
  300. model2_output = scale_f(model2.scale_model, 2.0, model2_output,
  301. opt.crop_size, opt.batch_size)
  302. end
  303. model2_time = model2_time + (sys.clock() - t)
  304. end
  305. baseline_output = baseline_scale(input, opt.baseline_filter)
  306. elseif opt.method == "user" then
  307. input = x[i].x
  308. ground_truth = x[i].y
  309. local y_scale = ground_truth:size(2) / input:size(2)
  310. if y_scale > 1 then
  311. if opt.force_cudnn and i == 1 then
  312. model1_output = scale_f(model1, y_scale, input, opt.crop_size, opt.batch_size)
  313. if model2 then
  314. model2_output = scale_f(model2, y_scale, input, opt.crop_size, opt.batch_size)
  315. end
  316. end
  317. t = sys.clock()
  318. model1_output = scale_f(model1, y_scale, 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 = scale_f(model2, y_scale, input, opt.crop_size, opt.batch_size)
  323. model2_time = model2_time + (sys.clock() - t)
  324. end
  325. else
  326. if opt.force_cudnn and i == 1 then
  327. model1_output = image_f(model1, input, opt.crop_size, opt.batch_size)
  328. if model2 then
  329. model2_output = image_f(model2, input, opt.crop_size, opt.batch_size)
  330. end
  331. end
  332. t = sys.clock()
  333. model1_output = image_f(model1, input, opt.crop_size, opt.batch_size)
  334. model1_time = model1_time + (sys.clock() - t)
  335. if model2 then
  336. t = sys.clock()
  337. model2_output = image_f(model2, input, opt.crop_size, opt.batch_size)
  338. model2_time = model2_time + (sys.clock() - t)
  339. end
  340. end
  341. elseif opt.method == "diff" then
  342. input = x[i].x
  343. ground_truth = x[i].y
  344. model1_output = input
  345. end
  346. mse1 = MSE(ground_truth, model1_output, opt.color)
  347. model1_mse = model1_mse + mse1
  348. model1_psnr = model1_psnr + MSE2PSNR(mse1)
  349. local won_model = 1
  350. if model2 then
  351. mse2 = MSE(ground_truth, model2_output, opt.color)
  352. model2_mse = model2_mse + mse2
  353. model2_psnr = model2_psnr + MSE2PSNR(mse2)
  354. if mse1 < mse2 then
  355. won[1] = won[1] + 1
  356. elseif mse1 > mse2 then
  357. won[2] = won[2] + 1
  358. won_model = 2
  359. end
  360. if detail_fp then
  361. detail_fp:write(string.format("%s,%f,%f,%d\n", x[i].basename,
  362. MSE2PSNR(mse1), MSE2PSNR(mse2), won_model))
  363. end
  364. else
  365. if detail_fp then
  366. detail_fp:write(string.format("%s,%f\n", x[i].basename, MSE2PSNR(mse1)))
  367. end
  368. end
  369. if baseline_output then
  370. mse = MSE(ground_truth, baseline_output, opt.color)
  371. baseline_mse = baseline_mse + mse
  372. baseline_psnr = baseline_psnr + MSE2PSNR(mse)
  373. end
  374. if opt.save_image then
  375. if opt.save_baseline_image and baseline_output then
  376. image.save(path.join(opt.output_dir, string.format("%s_baseline.png", basename)),
  377. baseline_output)
  378. end
  379. if model1_output then
  380. image.save(path.join(opt.output_dir, string.format("%s_model1.png", basename)),
  381. model1_output)
  382. end
  383. if model2_output then
  384. image.save(path.join(opt.output_dir, string.format("%s_model2.png", basename)),
  385. model2_output)
  386. end
  387. end
  388. if opt.show_progress or i == #x then
  389. if model2 then
  390. if baseline_output then
  391. io.stdout:write(
  392. 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, model1_won=%d, model2_won=%d \r",
  393. i, #x,
  394. model1_time,
  395. model2_time,
  396. math.sqrt(baseline_mse / i),
  397. math.sqrt(model1_mse / i), math.sqrt(model2_mse / i),
  398. baseline_psnr / i,
  399. model1_psnr / i, model2_psnr / i,
  400. won[1], won[2]
  401. ))
  402. else
  403. io.stdout:write(
  404. string.format("%d/%d; model1_time=%.2f, model2_time=%.2f, model1_rmse=%f, model2_rmse=%f, model1_psnr=%f, model2_psnr=%f, model1_own=%d, model2_won=%d \r",
  405. i, #x,
  406. model1_time,
  407. model2_time,
  408. math.sqrt(model1_mse / i), math.sqrt(model2_mse / i),
  409. model1_psnr / i, model2_psnr / i,
  410. won[1], won[2]
  411. ))
  412. end
  413. else
  414. if baseline_output then
  415. io.stdout:write(
  416. string.format("%d/%d; model1_time=%.2f, baseline_rmse=%f, model1_rmse=%f, baseline_psnr=%f, model1_psnr=%f \r",
  417. i, #x,
  418. model1_time,
  419. math.sqrt(baseline_mse / i), math.sqrt(model1_mse / i),
  420. baseline_psnr / i, model1_psnr / i
  421. ))
  422. else
  423. io.stdout:write(
  424. string.format("%d/%d; model1_time=%.2f, model1_rmse=%f, model1_psnr=%f \r",
  425. i, #x,
  426. model1_time,
  427. math.sqrt(model1_mse / i), model1_psnr / i
  428. ))
  429. end
  430. end
  431. io.stdout:flush()
  432. end
  433. end
  434. if opt.save_info then
  435. local fp = io.open(path.join(opt.output_dir, "benchmark.txt"), "w")
  436. fp:write("options : " .. cjson.encode(opt) .. "\n")
  437. if baseline_psnr > 0 then
  438. fp:write(string.format("baseline: RMSE = %.3f, PSNR = %.3f\n",
  439. math.sqrt(baseline_mse / #x), baseline_psnr / #x))
  440. end
  441. if model1_psnr > 0 then
  442. fp:write(string.format("model1 : RMSE = %.3f, PSNR = %.3f, evaluation time = %.3f\n",
  443. math.sqrt(model1_mse / #x), model1_psnr / #x, model1_time))
  444. end
  445. if model2_psnr > 0 then
  446. fp:write(string.format("model2 : RMSE = %.3f, PSNR = %.3f, evaluation time = %.3f\n",
  447. math.sqrt(model2_mse / #x), model2_psnr / #x, model2_time))
  448. end
  449. fp:close()
  450. if detail_fp then
  451. detail_fp:close()
  452. end
  453. end
  454. io.stdout:write("\n")
  455. end
  456. local function load_data_from_dir(test_dir)
  457. local test_x = {}
  458. local files = dir.getfiles(test_dir, "*.*")
  459. for i = 1, #files do
  460. local name = path.basename(files[i])
  461. local e = path.extension(name)
  462. local base = name:sub(0, name:len() - e:len())
  463. local img = image_loader.load_float(files[i])
  464. if img then
  465. table.insert(test_x, {y = iproc.crop_mod4(img),
  466. basename = base})
  467. end
  468. if opt.show_progress then
  469. xlua.progress(i, #files)
  470. end
  471. end
  472. return test_x
  473. end
  474. local function load_data_from_file(test_file)
  475. local test_x = {}
  476. local files = utils.split(file.read(test_file), "\n")
  477. for i = 1, #files do
  478. local name = path.basename(files[i])
  479. local e = path.extension(name)
  480. local base = name:sub(0, name:len() - e:len())
  481. local img = image_loader.load_float(files[i])
  482. if img then
  483. table.insert(test_x, {y = iproc.crop_mod4(img),
  484. basename = base})
  485. end
  486. if opt.show_progress then
  487. xlua.progress(i, #files)
  488. end
  489. end
  490. return test_x
  491. end
  492. local function get_basename(f)
  493. local name = path.basename(f)
  494. local e = path.extension(name)
  495. local base = name:sub(0, name:len() - e:len())
  496. return base
  497. end
  498. local function load_user_data(y_dir, y_file, x_dir, x_file)
  499. local test = {}
  500. local y_files
  501. local x_files
  502. if y_file:len() > 0 then
  503. y_files = utils.split(file.read(y_file), "\n")
  504. else
  505. y_files = dir.getfiles(y_dir, "*.*")
  506. end
  507. if x_file:len() > 0 then
  508. x_files = utils.split(file.read(x_file), "\n")
  509. else
  510. x_files = dir.getfiles(x_dir, "*.*")
  511. end
  512. local basename_db = {}
  513. for i = 1, #y_files do
  514. basename_db[get_basename(y_files[i])] = {y = y_files[i]}
  515. end
  516. for i = 1, #x_files do
  517. local key = get_basename(x_files[i])
  518. if basename_db[key] then
  519. basename_db[key].x = x_files[i]
  520. else
  521. error(string.format("%s is not found in %s", key, y_dir))
  522. end
  523. end
  524. for i = 1, #y_files do
  525. local key = get_basename(y_files[i])
  526. local d = basename_db[key]
  527. if not (d.x and d.y) then
  528. error(string.format("%s is not found in %s", key, x_dir))
  529. end
  530. end
  531. for i = 1, #y_files do
  532. local key = get_basename(y_files[i])
  533. local x = image_loader.load_float(basename_db[key].x)
  534. local y = image_loader.load_float(basename_db[key].y)
  535. if x and y then
  536. table.insert(test, {y = y,
  537. x = x,
  538. basename = key})
  539. end
  540. if opt.show_progress then
  541. xlua.progress(i, #y_files)
  542. end
  543. end
  544. return test
  545. end
  546. function load_noise_scale_model(model_dir, noise_level, force_cudnn)
  547. local f = path.join(model_dir, string.format("noise%d_scale2.0x_model.t7", opt.noise_level))
  548. local s1, noise_scale = pcall(w2nn.load_model, f, force_cudnn)
  549. local model = {}
  550. if not s1 then
  551. f = path.join(model_dir, string.format("noise%d_model.t7", opt.noise_level))
  552. local noise
  553. s1, noise = pcall(w2nn.load_model, f, force_cudnn)
  554. if not s1 then
  555. model.noise_model = nil
  556. print(model_dir .. "'s noise model is not found. benchmark will use only scale model.")
  557. else
  558. model.noise_model = noise
  559. end
  560. f = path.join(model_dir, "scale2.0x_model.t7")
  561. local scale
  562. s1, scale = pcall(w2nn.load_model, f, force_cudnn)
  563. if not s1 then
  564. error(model_dir .. ": load error")
  565. return nil
  566. end
  567. model.scale_model = scale
  568. else
  569. model.noise_scale_model = noise_scale
  570. end
  571. return model
  572. end
  573. if opt.show_progress then
  574. print(opt)
  575. end
  576. if opt.method == "scale" then
  577. local f1 = path.join(opt.model1_dir, "scale2.0x_model.t7")
  578. local f2 = path.join(opt.model2_dir, "scale2.0x_model.t7")
  579. local s1, model1 = pcall(w2nn.load_model, f1, opt.force_cudnn)
  580. local s2, model2 = pcall(w2nn.load_model, f2, opt.force_cudnn)
  581. if not s1 then
  582. error("Load error: " .. f1)
  583. end
  584. if not s2 then
  585. model2 = nil
  586. end
  587. local test_x
  588. if opt.file:len() > 0 then
  589. test_x = load_data_from_file(opt.file)
  590. else
  591. test_x = load_data_from_dir(opt.dir)
  592. end
  593. benchmark(opt, test_x, model1, model2)
  594. elseif opt.method == "noise" then
  595. local f1 = path.join(opt.model1_dir, string.format("noise%d_model.t7", opt.noise_level))
  596. local f2 = path.join(opt.model2_dir, string.format("noise%d_model.t7", opt.noise_level))
  597. local s1, model1 = pcall(w2nn.load_model, f1, opt.force_cudnn)
  598. local s2, model2 = pcall(w2nn.load_model, f2, opt.force_cudnn)
  599. if not s1 then
  600. error("Load error: " .. f1)
  601. end
  602. if not s2 then
  603. model2 = nil
  604. end
  605. local test_x
  606. if opt.file:len() > 0 then
  607. test_x = load_data_from_file(opt.file)
  608. else
  609. test_x = load_data_from_dir(opt.dir)
  610. end
  611. benchmark(opt, test_x, model1, model2)
  612. elseif opt.method == "noise_scale" then
  613. local model2 = nil
  614. local model1 = load_noise_scale_model(opt.model1_dir, opt.noise_level, opt.force_cudnn)
  615. if opt.model2_dir:len() > 0 then
  616. model2 = load_noise_scale_model(opt.model2_dir, opt.noise_level, opt.force_cudnn)
  617. end
  618. local test_x
  619. if opt.file:len() > 0 then
  620. test_x = load_data_from_file(opt.file)
  621. else
  622. test_x = load_data_from_dir(opt.dir)
  623. end
  624. benchmark(opt, test_x, model1, model2)
  625. elseif opt.method == "user" then
  626. local f1 = path.join(opt.model1_dir, string.format("%s_model.t7", opt.name))
  627. local f2 = path.join(opt.model2_dir, string.format("%s_model.t7", opt.name))
  628. local s1, model1 = pcall(w2nn.load_model, f1, opt.force_cudnn)
  629. local s2, model2 = pcall(w2nn.load_model, f2, opt.force_cudnn)
  630. if not s1 then
  631. error("Load error: " .. f1)
  632. end
  633. if not s2 then
  634. model2 = nil
  635. end
  636. local test = load_user_data(opt.y_dir, opt.y_file, opt.x_dir, opt.x_file)
  637. benchmark(opt, test, model1, model2)
  638. elseif opt.method == "diff" then
  639. local test = load_user_data(opt.y_dir, opt.y_file, opt.x_dir, opt.x_file)
  640. benchmark(opt, test, nil, nil)
  641. end