benchmark.lua 19 KB

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