benchmark.lua 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 cmd = torch.CmdLine()
  11. cmd:text()
  12. cmd:text("waifu2x-benchmark")
  13. cmd:text("Options:")
  14. cmd:option("-dir", "./data/test", 'test image directory')
  15. cmd:option("-model1_dir", "./models/anime_style_art_rgb", 'model1 directory')
  16. cmd:option("-model2_dir", "", 'model2 directory (optional)')
  17. cmd:option("-method", "scale", '(scale|noise)')
  18. cmd:option("-filter", "Catrom", "downscaling filter (Box|Lanczos|Catrom(Bicubic))")
  19. cmd:option("-color", "y", '(rgb|y)')
  20. cmd:option("-noise_level", 1, 'model noise level')
  21. cmd:option("-jpeg_quality", 75, 'jpeg quality')
  22. cmd:option("-jpeg_times", 1, 'jpeg compression times')
  23. cmd:option("-jpeg_quality_down", 5, 'value of jpeg quality to decrease each times')
  24. cmd:option("-range_bug", 0, 'Reproducing the dynamic range bug that is caused by MATLAB\'s rgb2ycbcr(1|0)')
  25. local opt = cmd:parse(arg)
  26. torch.setdefaulttensortype('torch.FloatTensor')
  27. if cudnn then
  28. cudnn.fastest = true
  29. cudnn.benchmark = false
  30. end
  31. local function rgb2y_matlab(x)
  32. local y = torch.Tensor(1, x:size(2), x:size(3)):zero()
  33. x = iproc.byte2float(x)
  34. y:add(x[1] * 65.481)
  35. y:add(x[2] * 128.553)
  36. y:add(x[3] * 24.966)
  37. y:add(16.0)
  38. return y:byte():float()
  39. end
  40. local function MSE(x1, x2)
  41. x1 = iproc.float2byte(x1):float()
  42. x2 = iproc.float2byte(x2):float()
  43. return (x1 - x2):pow(2):mean()
  44. end
  45. local function YMSE(x1, x2)
  46. if opt.range_bug == 1 then
  47. local x1_2 = rgb2y_matlab(x1)
  48. local x2_2 = rgb2y_matlab(x2)
  49. return (x1_2 - x2_2):pow(2):mean()
  50. else
  51. local x1_2 = image.rgb2y(x1):mul(255.0)
  52. local x2_2 = image.rgb2y(x2):mul(255.0)
  53. return (x1_2 - x2_2):pow(2):mean()
  54. end
  55. end
  56. local function PSNR(x1, x2)
  57. local mse = MSE(x1, x2)
  58. return 10 * math.log10((255.0 * 255.0) / mse)
  59. end
  60. local function YPSNR(x1, x2)
  61. local mse = YMSE(x1, x2)
  62. return 10 * math.log10((255.0 * 255.0) / mse)
  63. end
  64. local function transform_jpeg(x, opt)
  65. for i = 1, opt.jpeg_times do
  66. jpeg = gm.Image(x, "RGB", "DHW")
  67. jpeg:format("jpeg")
  68. jpeg:samplingFactors({1.0, 1.0, 1.0})
  69. blob, len = jpeg:toBlob(opt.jpeg_quality - (i - 1) * opt.jpeg_quality_down)
  70. jpeg:fromBlob(blob, len)
  71. x = jpeg:toTensor("byte", "RGB", "DHW")
  72. end
  73. return x
  74. end
  75. local function baseline_scale(x, filter)
  76. return iproc.scale(x,
  77. x:size(3) * 2.0,
  78. x:size(2) * 2.0,
  79. filter)
  80. end
  81. local function transform_scale(x, opt)
  82. return iproc.scale(x,
  83. x:size(3) * 0.5,
  84. x:size(2) * 0.5,
  85. opt.filter)
  86. end
  87. local function benchmark(opt, x, input_func, model1, model2)
  88. local model1_mse = 0
  89. local model2_mse = 0
  90. local baseline_mse = 0
  91. local model1_psnr = 0
  92. local model2_psnr = 0
  93. local baseline_psnr = 0
  94. for i = 1, #x do
  95. local ground_truth = x[i]
  96. local input, model1_output, model2_output, baseline_output
  97. input = input_func(ground_truth, opt)
  98. t = sys.clock()
  99. if input:size(3) == ground_truth:size(3) then
  100. model1_output = reconstruct.image(model1, input)
  101. if model2 then
  102. model2_output = reconstruct.image(model2, input)
  103. end
  104. else
  105. model1_output = reconstruct.scale(model1, 2.0, input)
  106. if model2 then
  107. model2_output = reconstruct.scale(model2, 2.0, input)
  108. end
  109. baseline_output = baseline_scale(input, opt.filter)
  110. end
  111. if opt.color == "y" then
  112. model1_mse = model1_mse + YMSE(ground_truth, model1_output)
  113. model1_psnr = model1_psnr + YPSNR(ground_truth, model1_output)
  114. if model2 then
  115. model2_mse = model2_mse + YMSE(ground_truth, model2_output)
  116. model2_psnr = model2_psnr + YPSNR(ground_truth, model2_output)
  117. end
  118. if baseline_output then
  119. baseline_mse = baseline_mse + YMSE(ground_truth, baseline_output)
  120. baseline_psnr = baseline_psnr + YPSNR(ground_truth, baseline_output)
  121. end
  122. elseif opt.color == "rgb" then
  123. model1_mse = model1_mse + MSE(ground_truth, model1_output)
  124. model1_psnr = model1_psnr + PSNR(ground_truth, model1_output)
  125. if model2 then
  126. model2_mse = model2_mse + MSE(ground_truth, model2_output)
  127. model2_psnr = model2_psnr + PSNR(ground_truth, model2_output)
  128. end
  129. if baseline_output then
  130. baseline_mse = baseline_mse + MSE(ground_truth, baseline_output)
  131. baseline_psnr = baseline_psnr + PSNR(ground_truth, baseline_output)
  132. end
  133. else
  134. error("Unknown color: " .. opt.color)
  135. end
  136. if model2 then
  137. if baseline_output then
  138. io.stdout:write(
  139. string.format("%d/%d; baseline_mse=%f, model1_mse=%f, model2_mse=%f, baseline_psnr=%f, model1_psnr=%f, model2_psnr=%f \r",
  140. i, #x,
  141. baseline_mse / i,
  142. model1_mse / i, model2_mse / i,
  143. baseline_psnr / i,
  144. model1_psnr / i, model2_psnr / i
  145. ))
  146. else
  147. io.stdout:write(
  148. string.format("%d/%d; model1_mse=%f, model2_mse=%f, model1_psnr=%f, model2_psnr=%f \r",
  149. i, #x,
  150. model1_mse / i, model2_mse / i,
  151. model1_psnr / i, model2_psnr / i
  152. ))
  153. end
  154. else
  155. if baseline_output then
  156. io.stdout:write(
  157. string.format("%d/%d; baseline_mse=%f, model1_mse=%f, baseline_psnr=%f, model1_psnr=%f \r",
  158. i, #x,
  159. baseline_mse / i, model1_mse / i,
  160. baseline_psnr / i, model1_psnr / i
  161. ))
  162. else
  163. io.stdout:write(
  164. string.format("%d/%d; model1_mse=%f, model1_psnr=%f \r",
  165. i, #x,
  166. model1_mse / i, model1_psnr / i
  167. ))
  168. end
  169. end
  170. io.stdout:flush()
  171. end
  172. io.stdout:write("\n")
  173. end
  174. local function load_data(test_dir)
  175. local test_x = {}
  176. local files = dir.getfiles(test_dir, "*.*")
  177. for i = 1, #files do
  178. table.insert(test_x, iproc.crop_mod4(image_loader.load_float(files[i])))
  179. xlua.progress(i, #files)
  180. end
  181. return test_x
  182. end
  183. function load_model(filename)
  184. return torch.load(filename, "ascii")
  185. end
  186. print(opt)
  187. if opt.method == "scale" then
  188. local f1 = path.join(opt.model1_dir, "scale2.0x_model.t7")
  189. local f2 = path.join(opt.model2_dir, "scale2.0x_model.t7")
  190. local s1, model1 = pcall(load_model, f1)
  191. local s2, model2 = pcall(load_model, f2)
  192. if not s1 then
  193. error("Load error: " .. f1)
  194. end
  195. if not s2 then
  196. model2 = nil
  197. end
  198. local test_x = load_data(opt.dir)
  199. benchmark(opt, test_x, transform_scale, model1, model2)
  200. elseif opt.method == "noise" then
  201. local f1 = path.join(opt.model1_dir, string.format("noise%d_model.t7", opt.noise_level))
  202. local f2 = path.join(opt.model2_dir, string.format("noise%d_model.t7", opt.noise_level))
  203. local s1, model1 = pcall(load_model, f1)
  204. local s2, model2 = pcall(load_model, f2)
  205. if not s1 then
  206. error("Load error: " .. f1)
  207. end
  208. if not s2 then
  209. model2 = nil
  210. end
  211. local test_x = load_data(opt.dir)
  212. benchmark(opt, test_x, transform_jpeg, model1, model2)
  213. end