benchmark.lua 6.7 KB

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