benchmark.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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)')
  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)')
  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. local function to_bool(settings, name)
  40. if settings[name] == 1 then
  41. settings[name] = true
  42. else
  43. settings[name] = false
  44. end
  45. end
  46. local opt = cmd:parse(arg)
  47. torch.setdefaulttensortype('torch.FloatTensor')
  48. if cudnn then
  49. cudnn.fastest = true
  50. cudnn.benchmark = false
  51. end
  52. to_bool(opt, "save_all")
  53. to_bool(opt, "tta")
  54. if opt.save_all then
  55. opt.save_image = true
  56. opt.save_info = true
  57. opt.save_baseline_image = true
  58. else
  59. to_bool(opt, "save_image")
  60. to_bool(opt, "save_info")
  61. to_bool(opt, "save_baseline_image")
  62. end
  63. to_bool(opt, "show_progress")
  64. if opt.thread > 0 then
  65. torch.setnumthreads(tonumber(opt.thread))
  66. end
  67. local function rgb2y_matlab(x)
  68. local y = torch.Tensor(1, x:size(2), x:size(3)):zero()
  69. x = iproc.byte2float(x)
  70. y:add(x[1] * 65.481)
  71. y:add(x[2] * 128.553)
  72. y:add(x[3] * 24.966)
  73. y:add(16.0)
  74. return y:byte():float()
  75. end
  76. local function RGBMSE(x1, x2)
  77. x1 = iproc.float2byte(x1):float()
  78. x2 = iproc.float2byte(x2):float()
  79. return (x1 - x2):pow(2):mean()
  80. end
  81. local function YMSE(x1, x2)
  82. if opt.range_bug == 1 then
  83. local x1_2 = rgb2y_matlab(x1)
  84. local x2_2 = rgb2y_matlab(x2)
  85. return (x1_2 - x2_2):pow(2):mean()
  86. else
  87. local x1_2 = image.rgb2y(x1):mul(255.0)
  88. local x2_2 = image.rgb2y(x2):mul(255.0)
  89. return (x1_2 - x2_2):pow(2):mean()
  90. end
  91. end
  92. local function MSE(x1, x2, color)
  93. if color == "y" then
  94. return YMSE(x1, x2)
  95. else
  96. return RGBMSE(x1, x2)
  97. end
  98. end
  99. local function PSNR(x1, x2, color)
  100. local mse = math.max(MSE(x1, x2, color), 1)
  101. return 10 * math.log10((255.0 * 255.0) / mse)
  102. end
  103. local function transform_jpeg(x, opt)
  104. for i = 1, opt.jpeg_times do
  105. jpeg = gm.Image(x, "RGB", "DHW")
  106. jpeg:format("jpeg")
  107. jpeg:samplingFactors({1.0, 1.0, 1.0})
  108. blob, len = jpeg:toBlob(opt.jpeg_quality - (i - 1) * opt.jpeg_quality_down)
  109. jpeg:fromBlob(blob, len)
  110. x = jpeg:toTensor("byte", "RGB", "DHW")
  111. end
  112. return iproc.byte2float(x)
  113. end
  114. local function baseline_scale(x, filter)
  115. return iproc.scale(x,
  116. x:size(3) * 2.0,
  117. x:size(2) * 2.0,
  118. filter)
  119. end
  120. local function transform_scale(x, opt)
  121. return iproc.scale(x,
  122. x:size(3) * 0.5,
  123. x:size(2) * 0.5,
  124. opt.filter, opt.resize_blur)
  125. end
  126. local function transform_scale_jpeg(x, opt)
  127. x = iproc.scale(x,
  128. x:size(3) * 0.5,
  129. x:size(2) * 0.5,
  130. opt.filter, opt.resize_blur)
  131. for i = 1, opt.jpeg_times do
  132. jpeg = gm.Image(x, "RGB", "DHW")
  133. jpeg:format("jpeg")
  134. jpeg:samplingFactors({1.0, 1.0, 1.0})
  135. blob, len = jpeg:toBlob(opt.jpeg_quality - (i - 1) * opt.jpeg_quality_down)
  136. jpeg:fromBlob(blob, len)
  137. x = jpeg:toTensor("byte", "RGB", "DHW")
  138. end
  139. return iproc.byte2float(x)
  140. end
  141. local function benchmark(opt, x, input_func, model1, model2)
  142. local model1_mse = 0
  143. local model2_mse = 0
  144. local baseline_mse = 0
  145. local model1_psnr = 0
  146. local model2_psnr = 0
  147. local baseline_psnr = 0
  148. local scale_f = reconstruct.scale
  149. local image_f = reconstruct.image
  150. if opt.tta then
  151. scale_f = function(model, scale, x, block_size, batch_size)
  152. return reconstruct.scale_tta(model, opt.tta_level,
  153. scale, x, block_size, batch_size)
  154. end
  155. image_f = function(model, x, block_size, batch_size)
  156. return reconstruct.image_tta(model, opt.tta_level,
  157. x, block_size, batch_size)
  158. end
  159. end
  160. for i = 1, #x do
  161. local ground_truth = x[i].image
  162. local basename = x[i].basename
  163. local input, model1_output, model2_output, baseline_output
  164. input = input_func(ground_truth, opt)
  165. t = sys.clock()
  166. if opt.method == "scale" then
  167. model1_output = scale_f(model1, 2.0, input, opt.crop_size, opt.batch_size)
  168. if model2 then
  169. model2_output = scale_f(model2, 2.0, input, opt.crop_size, opt.batch_size)
  170. end
  171. baseline_output = baseline_scale(input, opt.baseline_filter)
  172. elseif opt.method == "noise" then
  173. model1_output = image_f(model1, input, opt.crop_size, opt.batch_size)
  174. if model2 then
  175. model2_output = image_f(model2, input, opt.crop_size, opt.batch_size)
  176. end
  177. baseline_output = input
  178. elseif opt.method == "noise_scale" then
  179. if model1.noise_scale_model then
  180. model1_output = scale_f(model1.noise_scale_model, 2.0,
  181. input, opt.crop_size, opt.batch_size)
  182. else
  183. if model1.noise_model then
  184. model1_output = image_f(model1.noise_model, input, opt.crop_size, opt.batch_size)
  185. else
  186. model1_output = input
  187. end
  188. model1_output = scale_f(model1.scale_model, 2.0, model1_output,
  189. opt.crop_size, opt.batch_size)
  190. end
  191. if model2 then
  192. if model2.noise_scale_model then
  193. model2_output = scale_f(model2.noise_scale_model, 2.0,
  194. input, opt.crop_size, opt.batch_size)
  195. else
  196. if model2.noise_model then
  197. model2_output = image_f(model2.noise_model, input,
  198. opt.crop_size, opt.batch_size)
  199. else
  200. model2_output = input
  201. end
  202. model2_output = scale_f(model2.scale_model, 2.0, model2_output,
  203. opt.crop_size, opt.batch_size)
  204. end
  205. end
  206. baseline_output = baseline_scale(input, opt.baseline_filter)
  207. end
  208. model1_mse = model1_mse + MSE(ground_truth, model1_output, opt.color)
  209. model1_psnr = model1_psnr + PSNR(ground_truth, model1_output, opt.color)
  210. if model2 then
  211. model2_mse = model2_mse + MSE(ground_truth, model2_output, opt.color)
  212. model2_psnr = model2_psnr + PSNR(ground_truth, model2_output, opt.color)
  213. end
  214. if baseline_output then
  215. baseline_mse = baseline_mse + MSE(ground_truth, baseline_output, opt.color)
  216. baseline_psnr = baseline_psnr + PSNR(ground_truth, baseline_output, opt.color)
  217. end
  218. if opt.save_image then
  219. if opt.save_baseline_image and baseline_output then
  220. image.save(path.join(opt.output_dir, string.format("%s_baseline.png", basename)),
  221. baseline_output)
  222. end
  223. if model1_output then
  224. image.save(path.join(opt.output_dir, string.format("%s_model1.png", basename)),
  225. model1_output)
  226. end
  227. if model2_output then
  228. image.save(path.join(opt.output_dir, string.format("%s_model2.png", basename)),
  229. model2_output)
  230. end
  231. end
  232. if opt.show_progress or i == #x then
  233. if model2 then
  234. if baseline_output then
  235. io.stdout:write(
  236. string.format("%d/%d; baseline_rmse=%f, model1_rmse=%f, model2_rmse=%f, baseline_psnr=%f, model1_psnr=%f, model2_psnr=%f \r",
  237. i, #x,
  238. math.sqrt(baseline_mse / i),
  239. math.sqrt(model1_mse / i), math.sqrt(model2_mse / i),
  240. baseline_psnr / i,
  241. model1_psnr / i, model2_psnr / i
  242. ))
  243. else
  244. io.stdout:write(
  245. string.format("%d/%d; model1_rmse=%f, model2_rmse=%f, model1_psnr=%f, model2_psnr=%f \r",
  246. i, #x,
  247. math.sqrt(model1_mse / i), math.sqrt(model2_mse / i),
  248. model1_psnr / i, model2_psnr / i
  249. ))
  250. end
  251. else
  252. if baseline_output then
  253. io.stdout:write(
  254. string.format("%d/%d; baseline_rmse=%f, model1_rmse=%f, baseline_psnr=%f, model1_psnr=%f \r",
  255. i, #x,
  256. math.sqrt(baseline_mse / i), math.sqrt(model1_mse / i),
  257. baseline_psnr / i, model1_psnr / i
  258. ))
  259. else
  260. io.stdout:write(
  261. string.format("%d/%d; model1_rmse=%f, model1_psnr=%f \r",
  262. i, #x,
  263. math.sqrt(model1_mse / i), model1_psnr / i
  264. ))
  265. end
  266. end
  267. io.stdout:flush()
  268. end
  269. end
  270. if opt.save_info then
  271. local fp = io.open(path.join(opt.output_dir, "benchmark.txt"), "w")
  272. fp:write("options : " .. cjson.encode(opt) .. "\n")
  273. if baseline_psnr > 0 then
  274. fp:write(string.format("baseline: RMSE = %.3f, PSNR = %.3f\n",
  275. math.sqrt(baseline_mse / #x), baseline_psnr / #x))
  276. end
  277. if model1_psnr > 0 then
  278. fp:write(string.format("model1 : RMSE = %.3f, PSNR = %.3f\n",
  279. math.sqrt(model1_mse / #x), model1_psnr / #x))
  280. end
  281. if model2_psnr > 0 then
  282. fp:write(string.format("model2 : RMSE = %.3f, PSNR = %.3f\n",
  283. math.sqrt(model2_mse / #x), model2_psnr / #x))
  284. end
  285. fp:close()
  286. end
  287. io.stdout:write("\n")
  288. end
  289. local function load_data(test_dir)
  290. local test_x = {}
  291. local files = dir.getfiles(test_dir, "*.*")
  292. for i = 1, #files do
  293. local name = path.basename(files[i])
  294. local e = path.extension(name)
  295. local base = name:sub(0, name:len() - e:len())
  296. local img = image_loader.load_float(files[i])
  297. if img then
  298. table.insert(test_x, {image = iproc.crop_mod4(img),
  299. basename = base})
  300. end
  301. if opt.show_progress then
  302. xlua.progress(i, #files)
  303. end
  304. end
  305. return test_x
  306. end
  307. function load_model(filename)
  308. return torch.load(filename, "ascii")
  309. end
  310. function load_noise_scale_model(model_dir, noise_level)
  311. local f = path.join(model_dir, string.format("noise%d_scale2.0x_model.t7", opt.noise_level))
  312. local s1, noise_scale = pcall(load_model, f)
  313. local model = {}
  314. if not s1 then
  315. f = path.join(model_dir, string.format("noise%d_model.t7", opt.noise_level))
  316. local noise
  317. s1, noise = pcall(load_model, f)
  318. if not s1 then
  319. model.noise_model = nil
  320. print(model_dir .. "'s noise model is not found. benchmark will use only scale model.")
  321. else
  322. model.noise_model = noise
  323. end
  324. f = path.join(model_dir, "scale2.0x_model.t7")
  325. local scale
  326. s1, scale = pcall(load_model, f)
  327. if not s1 then
  328. return nil
  329. end
  330. model.scale_model = scale
  331. else
  332. model.noise_scale_model = noise_scale
  333. end
  334. return model
  335. end
  336. if opt.show_progress then
  337. print(opt)
  338. end
  339. if opt.method == "scale" then
  340. local f1 = path.join(opt.model1_dir, "scale2.0x_model.t7")
  341. local f2 = path.join(opt.model2_dir, "scale2.0x_model.t7")
  342. local s1, model1 = pcall(load_model, f1)
  343. local s2, model2 = pcall(load_model, f2)
  344. if not s1 then
  345. error("Load error: " .. f1)
  346. end
  347. if not s2 then
  348. model2 = nil
  349. end
  350. local test_x = load_data(opt.dir)
  351. benchmark(opt, test_x, transform_scale, model1, model2)
  352. elseif opt.method == "noise" then
  353. local f1 = path.join(opt.model1_dir, string.format("noise%d_model.t7", opt.noise_level))
  354. local f2 = path.join(opt.model2_dir, string.format("noise%d_model.t7", opt.noise_level))
  355. local s1, model1 = pcall(load_model, f1)
  356. local s2, model2 = pcall(load_model, f2)
  357. if not s1 then
  358. error("Load error: " .. f1)
  359. end
  360. if not s2 then
  361. model2 = nil
  362. end
  363. local test_x = load_data(opt.dir)
  364. benchmark(opt, test_x, transform_jpeg, model1, model2)
  365. elseif opt.method == "noise_scale" then
  366. local model2 = nil
  367. local model1 = load_noise_scale_model(opt.model1_dir, opt.noise_level)
  368. if opt.model2_dir:len() > 0 then
  369. model2 = load_noise_scale_model(opt.model2_dir, opt.noise_level)
  370. end
  371. local test_x = load_data(opt.dir)
  372. benchmark(opt, test_x, transform_scale_jpeg, model1, model2)
  373. end