benchmark.lua 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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 mse
  178. local model1_mse = 0
  179. local model2_mse = 0
  180. local baseline_mse = 0
  181. local model1_psnr = 0
  182. local model2_psnr = 0
  183. local baseline_psnr = 0
  184. local model1_time = 0
  185. local model2_time = 0
  186. local scale_f = reconstruct.scale
  187. local image_f = reconstruct.image
  188. if opt.tta then
  189. scale_f = function(model, scale, x, block_size, batch_size)
  190. return reconstruct.scale_tta(model, opt.tta_level,
  191. scale, x, block_size, batch_size)
  192. end
  193. image_f = function(model, x, block_size, batch_size)
  194. return reconstruct.image_tta(model, opt.tta_level,
  195. x, block_size, batch_size)
  196. end
  197. end
  198. for i = 1, #x do
  199. local basename = x[i].basename
  200. local input, model1_output, model2_output, baseline_output, ground_truth
  201. if opt.method == "scale" then
  202. input = transform_scale(x[i].y, opt)
  203. ground_truth = x[i].y
  204. if opt.force_cudnn and i == 1 then -- run cuDNN benchmark first
  205. model1_output = scale_f(model1, 2.0, input, opt.crop_size, opt.batch_size)
  206. if model2 then
  207. model2_output = scale_f(model2, 2.0, input, opt.crop_size, opt.batch_size)
  208. end
  209. end
  210. t = sys.clock()
  211. model1_output = scale_f(model1, 2.0, input, opt.crop_size, opt.batch_size)
  212. model1_time = model1_time + (sys.clock() - t)
  213. if model2 then
  214. t = sys.clock()
  215. model2_output = scale_f(model2, 2.0, input, opt.crop_size, opt.batch_size)
  216. model2_time = model2_time + (sys.clock() - t)
  217. end
  218. baseline_output = baseline_scale(input, opt.baseline_filter)
  219. elseif opt.method == "noise" then
  220. input = transform_jpeg(x[i].y, opt)
  221. ground_truth = x[i].y
  222. if opt.force_cudnn and i == 1 then
  223. model1_output = image_f(model1, input, opt.crop_size, opt.batch_size)
  224. if model2 then
  225. model2_output = image_f(model2, input, opt.crop_size, opt.batch_size)
  226. end
  227. end
  228. t = sys.clock()
  229. model1_output = image_f(model1, input, opt.crop_size, opt.batch_size)
  230. model1_time = model1_time + (sys.clock() - t)
  231. if model2 then
  232. t = sys.clock()
  233. model2_output = image_f(model2, input, opt.crop_size, opt.batch_size)
  234. model2_time = model2_time + (sys.clock() - t)
  235. end
  236. baseline_output = input
  237. elseif opt.method == "noise_scale" then
  238. input = transform_scale_jpeg(x[i].y, opt)
  239. ground_truth = x[i].y
  240. if opt.force_cudnn and i == 1 then
  241. if model1.noise_scale_model then
  242. model1_output = scale_f(model1.noise_scale_model, 2.0,
  243. input, opt.crop_size, opt.batch_size)
  244. else
  245. if model1.noise_model then
  246. model1_output = image_f(model1.noise_model, input, opt.crop_size, opt.batch_size)
  247. else
  248. model1_output = input
  249. end
  250. model1_output = scale_f(model1.scale_model, 2.0, model1_output,
  251. opt.crop_size, opt.batch_size)
  252. end
  253. if model2 then
  254. if model2.noise_scale_model then
  255. model2_output = scale_f(model2.noise_scale_model, 2.0,
  256. input, opt.crop_size, opt.batch_size)
  257. else
  258. if model2.noise_model then
  259. model2_output = image_f(model2.noise_model, input,
  260. opt.crop_size, opt.batch_size)
  261. else
  262. model2_output = input
  263. end
  264. model2_output = scale_f(model2.scale_model, 2.0, model2_output,
  265. opt.crop_size, opt.batch_size)
  266. end
  267. end
  268. end
  269. t = sys.clock()
  270. if model1.noise_scale_model then
  271. model1_output = scale_f(model1.noise_scale_model, 2.0,
  272. input, opt.crop_size, opt.batch_size)
  273. else
  274. if model1.noise_model then
  275. model1_output = image_f(model1.noise_model, input, opt.crop_size, opt.batch_size)
  276. else
  277. model1_output = input
  278. end
  279. model1_output = scale_f(model1.scale_model, 2.0, model1_output,
  280. opt.crop_size, opt.batch_size)
  281. end
  282. model1_time = model1_time + (sys.clock() - t)
  283. if model2 then
  284. t = sys.clock()
  285. if model2.noise_scale_model then
  286. model2_output = scale_f(model2.noise_scale_model, 2.0,
  287. input, opt.crop_size, opt.batch_size)
  288. else
  289. if model2.noise_model then
  290. model2_output = image_f(model2.noise_model, input,
  291. opt.crop_size, opt.batch_size)
  292. else
  293. model2_output = input
  294. end
  295. model2_output = scale_f(model2.scale_model, 2.0, model2_output,
  296. opt.crop_size, opt.batch_size)
  297. end
  298. model2_time = model2_time + (sys.clock() - t)
  299. end
  300. baseline_output = baseline_scale(input, opt.baseline_filter)
  301. elseif opt.method == "user" then
  302. input = x[i].x
  303. ground_truth = x[i].y
  304. local y_scale = ground_truth:size(2) / input:size(2)
  305. if y_scale > 1 then
  306. if opt.force_cudnn and i == 1 then
  307. model1_output = scale_f(model1, y_scale, input, opt.crop_size, opt.batch_size)
  308. if model2 then
  309. model2_output = scale_f(model2, y_scale, input, opt.crop_size, opt.batch_size)
  310. end
  311. end
  312. t = sys.clock()
  313. model1_output = scale_f(model1, y_scale, input, opt.crop_size, opt.batch_size)
  314. model1_time = model1_time + (sys.clock() - t)
  315. if model2 then
  316. t = sys.clock()
  317. model2_output = scale_f(model2, y_scale, input, opt.crop_size, opt.batch_size)
  318. model2_time = model2_time + (sys.clock() - t)
  319. end
  320. else
  321. if opt.force_cudnn and i == 1 then
  322. model1_output = image_f(model1, input, opt.crop_size, opt.batch_size)
  323. if model2 then
  324. model2_output = image_f(model2, input, opt.crop_size, opt.batch_size)
  325. end
  326. end
  327. t = sys.clock()
  328. model1_output = image_f(model1, input, opt.crop_size, opt.batch_size)
  329. model1_time = model1_time + (sys.clock() - t)
  330. if model2 then
  331. t = sys.clock()
  332. model2_output = image_f(model2, input, opt.crop_size, opt.batch_size)
  333. model2_time = model2_time + (sys.clock() - t)
  334. end
  335. end
  336. elseif opt.method == "diff" then
  337. input = x[i].x
  338. ground_truth = x[i].y
  339. model1_output = input
  340. end
  341. mse = MSE(ground_truth, model1_output, opt.color)
  342. model1_mse = model1_mse + mse
  343. model1_psnr = model1_psnr + MSE2PSNR(mse)
  344. if model2 then
  345. mse = MSE(ground_truth, model2_output, opt.color)
  346. model2_mse = model2_mse + mse
  347. model2_psnr = model2_psnr + MSE2PSNR(mse)
  348. end
  349. if baseline_output then
  350. mse = MSE(ground_truth, baseline_output, opt.color)
  351. baseline_mse = baseline_mse + mse
  352. baseline_psnr = baseline_psnr + MSE2PSNR(mse)
  353. end
  354. if opt.save_image then
  355. if opt.save_baseline_image and baseline_output then
  356. image.save(path.join(opt.output_dir, string.format("%s_baseline.png", basename)),
  357. baseline_output)
  358. end
  359. if model1_output then
  360. image.save(path.join(opt.output_dir, string.format("%s_model1.png", basename)),
  361. model1_output)
  362. end
  363. if model2_output then
  364. image.save(path.join(opt.output_dir, string.format("%s_model2.png", basename)),
  365. model2_output)
  366. end
  367. end
  368. if opt.show_progress or i == #x then
  369. if model2 then
  370. if baseline_output then
  371. io.stdout:write(
  372. 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",
  373. i, #x,
  374. model1_time,
  375. model2_time,
  376. math.sqrt(baseline_mse / i),
  377. math.sqrt(model1_mse / i), math.sqrt(model2_mse / i),
  378. baseline_psnr / i,
  379. model1_psnr / i, model2_psnr / i
  380. ))
  381. else
  382. io.stdout:write(
  383. string.format("%d/%d; model1_time=%.2f, model2_time=%.2f, model1_rmse=%f, model2_rmse=%f, model1_psnr=%f, model2_psnr=%f \r",
  384. i, #x,
  385. model1_time,
  386. model2_time,
  387. math.sqrt(model1_mse / i), math.sqrt(model2_mse / i),
  388. model1_psnr / i, model2_psnr / i
  389. ))
  390. end
  391. else
  392. if baseline_output then
  393. io.stdout:write(
  394. string.format("%d/%d; model1_time=%.2f, baseline_rmse=%f, model1_rmse=%f, baseline_psnr=%f, model1_psnr=%f \r",
  395. i, #x,
  396. model1_time,
  397. math.sqrt(baseline_mse / i), math.sqrt(model1_mse / i),
  398. baseline_psnr / i, model1_psnr / i
  399. ))
  400. else
  401. io.stdout:write(
  402. string.format("%d/%d; model1_time=%.2f, model1_rmse=%f, model1_psnr=%f \r",
  403. i, #x,
  404. model1_time,
  405. math.sqrt(model1_mse / i), model1_psnr / i
  406. ))
  407. end
  408. end
  409. io.stdout:flush()
  410. end
  411. end
  412. if opt.save_info then
  413. local fp = io.open(path.join(opt.output_dir, "benchmark.txt"), "w")
  414. fp:write("options : " .. cjson.encode(opt) .. "\n")
  415. if baseline_psnr > 0 then
  416. fp:write(string.format("baseline: RMSE = %.3f, PSNR = %.3f\n",
  417. math.sqrt(baseline_mse / #x), baseline_psnr / #x))
  418. end
  419. if model1_psnr > 0 then
  420. fp:write(string.format("model1 : RMSE = %.3f, PSNR = %.3f, evaluation time = %.3f\n",
  421. math.sqrt(model1_mse / #x), model1_psnr / #x, model1_time))
  422. end
  423. if model2_psnr > 0 then
  424. fp:write(string.format("model2 : RMSE = %.3f, PSNR = %.3f, evaluation time = %.3f\n",
  425. math.sqrt(model2_mse / #x), model2_psnr / #x, model2_time))
  426. end
  427. fp:close()
  428. end
  429. io.stdout:write("\n")
  430. end
  431. local function load_data_from_dir(test_dir)
  432. local test_x = {}
  433. local files = dir.getfiles(test_dir, "*.*")
  434. for i = 1, #files do
  435. local name = path.basename(files[i])
  436. local e = path.extension(name)
  437. local base = name:sub(0, name:len() - e:len())
  438. local img = image_loader.load_float(files[i])
  439. if img then
  440. table.insert(test_x, {y = iproc.crop_mod4(img),
  441. basename = base})
  442. end
  443. if opt.show_progress then
  444. xlua.progress(i, #files)
  445. end
  446. end
  447. return test_x
  448. end
  449. local function load_data_from_file(test_file)
  450. local test_x = {}
  451. local files = utils.split(file.read(test_file), "\n")
  452. for i = 1, #files do
  453. local name = path.basename(files[i])
  454. local e = path.extension(name)
  455. local base = name:sub(0, name:len() - e:len())
  456. local img = image_loader.load_float(files[i])
  457. if img then
  458. table.insert(test_x, {y = iproc.crop_mod4(img),
  459. basename = base})
  460. end
  461. if opt.show_progress then
  462. xlua.progress(i, #files)
  463. end
  464. end
  465. return test_x
  466. end
  467. local function get_basename(f)
  468. local name = path.basename(f)
  469. local e = path.extension(name)
  470. local base = name:sub(0, name:len() - e:len())
  471. return base
  472. end
  473. local function load_user_data(y_dir, y_file, x_dir, x_file)
  474. local test = {}
  475. local y_files
  476. local x_files
  477. if y_file:len() > 0 then
  478. y_files = utils.split(file.read(y_file), "\n")
  479. else
  480. y_files = dir.getfiles(y_dir, "*.*")
  481. end
  482. if x_file:len() > 0 then
  483. x_files = utils.split(file.read(x_file), "\n")
  484. else
  485. x_files = dir.getfiles(x_dir, "*.*")
  486. end
  487. local basename_db = {}
  488. for i = 1, #y_files do
  489. basename_db[get_basename(y_files[i])] = {y = y_files[i]}
  490. end
  491. for i = 1, #x_files do
  492. local key = get_basename(x_files[i])
  493. if basename_db[key] then
  494. basename_db[key].x = x_files[i]
  495. else
  496. error(string.format("%s is not found in %s", key, y_dir))
  497. end
  498. end
  499. for i = 1, #y_files do
  500. local key = get_basename(y_files[i])
  501. local d = basename_db[key]
  502. if not (d.x and d.y) then
  503. error(string.format("%s is not found in %s", key, x_dir))
  504. end
  505. end
  506. for i = 1, #y_files do
  507. local key = get_basename(y_files[i])
  508. local x = image_loader.load_float(basename_db[key].x)
  509. local y = image_loader.load_float(basename_db[key].y)
  510. if x and y then
  511. table.insert(test, {y = y,
  512. x = x,
  513. basename = base})
  514. end
  515. if opt.show_progress then
  516. xlua.progress(i, #y_files)
  517. end
  518. end
  519. return test
  520. end
  521. function load_noise_scale_model(model_dir, noise_level, force_cudnn)
  522. local f = path.join(model_dir, string.format("noise%d_scale2.0x_model.t7", opt.noise_level))
  523. local s1, noise_scale = pcall(w2nn.load_model, f, force_cudnn)
  524. local model = {}
  525. if not s1 then
  526. f = path.join(model_dir, string.format("noise%d_model.t7", opt.noise_level))
  527. local noise
  528. s1, noise = pcall(w2nn.load_model, f, force_cudnn)
  529. if not s1 then
  530. model.noise_model = nil
  531. print(model_dir .. "'s noise model is not found. benchmark will use only scale model.")
  532. else
  533. model.noise_model = noise
  534. end
  535. f = path.join(model_dir, "scale2.0x_model.t7")
  536. local scale
  537. s1, scale = pcall(w2nn.load_model, f, force_cudnn)
  538. if not s1 then
  539. error(model_dir .. ": load error")
  540. return nil
  541. end
  542. model.scale_model = scale
  543. else
  544. model.noise_scale_model = noise_scale
  545. end
  546. return model
  547. end
  548. if opt.show_progress then
  549. print(opt)
  550. end
  551. if opt.method == "scale" then
  552. local f1 = path.join(opt.model1_dir, "scale2.0x_model.t7")
  553. local f2 = path.join(opt.model2_dir, "scale2.0x_model.t7")
  554. local s1, model1 = pcall(w2nn.load_model, f1, opt.force_cudnn)
  555. local s2, model2 = pcall(w2nn.load_model, f2, opt.force_cudnn)
  556. if not s1 then
  557. error("Load error: " .. f1)
  558. end
  559. if not s2 then
  560. model2 = nil
  561. end
  562. local test_x
  563. if opt.file:len() > 0 then
  564. test_x = load_data_from_file(opt.file)
  565. else
  566. test_x = load_data_from_dir(opt.dir)
  567. end
  568. benchmark(opt, test_x, model1, model2)
  569. elseif opt.method == "noise" then
  570. local f1 = path.join(opt.model1_dir, string.format("noise%d_model.t7", opt.noise_level))
  571. local f2 = path.join(opt.model2_dir, string.format("noise%d_model.t7", opt.noise_level))
  572. local s1, model1 = pcall(w2nn.load_model, f1, opt.force_cudnn)
  573. local s2, model2 = pcall(w2nn.load_model, f2, opt.force_cudnn)
  574. if not s1 then
  575. error("Load error: " .. f1)
  576. end
  577. if not s2 then
  578. model2 = nil
  579. end
  580. local test_x
  581. if opt.file:len() > 0 then
  582. test_x = load_data_from_file(opt.file)
  583. else
  584. test_x = load_data_from_dir(opt.dir)
  585. end
  586. benchmark(opt, test_x, model1, model2)
  587. elseif opt.method == "noise_scale" then
  588. local model2 = nil
  589. local model1 = load_noise_scale_model(opt.model1_dir, opt.noise_level, opt.force_cudnn)
  590. if opt.model2_dir:len() > 0 then
  591. model2 = load_noise_scale_model(opt.model2_dir, opt.noise_level, opt.force_cudnn)
  592. end
  593. local test_x
  594. if opt.file:len() > 0 then
  595. test_x = load_data_from_file(opt.file)
  596. else
  597. test_x = load_data_from_dir(opt.dir)
  598. end
  599. benchmark(opt, test_x, model1, model2)
  600. elseif opt.method == "user" then
  601. local f1 = path.join(opt.model1_dir, string.format("%s_model.t7", opt.name))
  602. local f2 = path.join(opt.model2_dir, string.format("%s_model.t7", opt.name))
  603. local s1, model1 = pcall(w2nn.load_model, f1, opt.force_cudnn)
  604. local s2, model2 = pcall(w2nn.load_model, f2, opt.force_cudnn)
  605. if not s1 then
  606. error("Load error: " .. f1)
  607. end
  608. if not s2 then
  609. model2 = nil
  610. end
  611. local test = load_user_data(opt.y_dir, opt.y_file, opt.x_dir, opt.x_file)
  612. benchmark(opt, test, model1, model2)
  613. elseif opt.method == "diff" then
  614. local test = load_user_data(opt.y_dir, opt.y_file, opt.x_dir, opt.x_file)
  615. benchmark(opt, test, nil, nil)
  616. end