benchmark.lua 20 KB

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