benchmark.lua 9.3 KB

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