benchmark.lua 21 KB

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