pairwise_transform.lua 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. require 'image'
  2. local gm = require 'graphicsmagick'
  3. local iproc = require './iproc'
  4. local reconstruct = require './reconstruct'
  5. local pairwise_transform = {}
  6. local function random_half(src, p, min_size)
  7. p = p or 0.5
  8. local filter = ({"Box","Blackman", "SincFast", "Jinc"})[torch.random(1, 4)]
  9. if p > torch.uniform() then
  10. return iproc.scale(src, src:size(3) * 0.5, src:size(2) * 0.5, filter)
  11. else
  12. return src
  13. end
  14. end
  15. local function color_augment(x)
  16. local color_scale = torch.Tensor(3):uniform(0.8, 1.2)
  17. x = x:float():div(255)
  18. for i = 1, 3 do
  19. x[i]:mul(color_scale[i])
  20. end
  21. x[torch.lt(x, 0.0)] = 0.0
  22. x[torch.gt(x, 1.0)] = 1.0
  23. return x:mul(255):byte()
  24. end
  25. local function flip_augment(x, y)
  26. local flip = torch.random(1, 4)
  27. if y then
  28. if flip == 1 then
  29. x = image.hflip(x)
  30. y = image.hflip(y)
  31. elseif flip == 2 then
  32. x = image.vflip(x)
  33. y = image.vflip(y)
  34. elseif flip == 3 then
  35. x = image.hflip(image.vflip(x))
  36. y = image.hflip(image.vflip(y))
  37. elseif flip == 4 then
  38. end
  39. return x, y
  40. else
  41. if flip == 1 then
  42. x = image.hflip(x)
  43. elseif flip == 2 then
  44. x = image.vflip(x)
  45. elseif flip == 3 then
  46. x = image.hflip(image.vflip(x))
  47. elseif flip == 4 then
  48. end
  49. return x
  50. end
  51. end
  52. local INTERPOLATION_PADDING = 16
  53. function pairwise_transform.scale(src, scale, size, offset, options)
  54. options = options or {color_augment = true, random_half = true, rgb = true}
  55. if options.random_half then
  56. src = random_half(src)
  57. end
  58. local yi = torch.random(INTERPOLATION_PADDING, src:size(2) - size - INTERPOLATION_PADDING)
  59. local xi = torch.random(INTERPOLATION_PADDING, src:size(3) - size - INTERPOLATION_PADDING)
  60. local down_scale = 1.0 / scale
  61. local y = image.crop(src,
  62. xi - INTERPOLATION_PADDING, yi - INTERPOLATION_PADDING,
  63. xi + size + INTERPOLATION_PADDING, yi + size + INTERPOLATION_PADDING)
  64. local filters = {
  65. "Box", -- 0.012756949974688
  66. "Blackman", -- 0.013191924552285
  67. --"Cartom", -- 0.013753536746706
  68. --"Hanning", -- 0.013761314529647
  69. --"Hermite", -- 0.013850225205266
  70. "SincFast", -- 0.014095824314306
  71. "Jinc", -- 0.014244299255442
  72. }
  73. local downscale_filter = filters[torch.random(1, #filters)]
  74. y = flip_augment(y)
  75. if options.color_augment then
  76. y = color_augment(y)
  77. end
  78. local x = iproc.scale(y, y:size(3) * down_scale, y:size(2) * down_scale, downscale_filter)
  79. x = iproc.scale(x, y:size(3), y:size(2))
  80. y = y:float():div(255)
  81. x = x:float():div(255)
  82. if options.rgb then
  83. else
  84. y = image.rgb2yuv(y)[1]:reshape(1, y:size(2), y:size(3))
  85. x = image.rgb2yuv(x)[1]:reshape(1, x:size(2), x:size(3))
  86. end
  87. y = image.crop(y, INTERPOLATION_PADDING + offset, INTERPOLATION_PADDING + offset, y:size(3) - offset - INTERPOLATION_PADDING, y:size(2) - offset - INTERPOLATION_PADDING)
  88. x = image.crop(x, INTERPOLATION_PADDING, INTERPOLATION_PADDING, x:size(3) - INTERPOLATION_PADDING, x:size(2) - INTERPOLATION_PADDING)
  89. return x, y
  90. end
  91. function pairwise_transform.jpeg_(src, quality, size, offset, options)
  92. options = options or {color_augment = true, random_half = true, rgb = true}
  93. if options.random_half then
  94. src = random_half(src)
  95. end
  96. local yi = torch.random(0, src:size(2) - size - 1)
  97. local xi = torch.random(0, src:size(3) - size - 1)
  98. local y = src
  99. local x
  100. if options.color_augment then
  101. y = color_augment(y)
  102. end
  103. x = y
  104. for i = 1, #quality do
  105. x = gm.Image(x, "RGB", "DHW")
  106. x:format("jpeg")
  107. x:samplingFactors({1.0, 1.0, 1.0})
  108. local blob, len = x:toBlob(quality[i])
  109. x:fromBlob(blob, len)
  110. x = x:toTensor("byte", "RGB", "DHW")
  111. end
  112. y = image.crop(y, xi, yi, xi + size, yi + size)
  113. x = image.crop(x, xi, yi, xi + size, yi + size)
  114. y = y:float():div(255)
  115. x = x:float():div(255)
  116. x, y = flip_augment(x, y)
  117. if options.rgb then
  118. else
  119. y = image.rgb2yuv(y)[1]:reshape(1, y:size(2), y:size(3))
  120. x = image.rgb2yuv(x)[1]:reshape(1, x:size(2), x:size(3))
  121. end
  122. return x, image.crop(y, offset, offset, size - offset, size - offset)
  123. end
  124. function pairwise_transform.jpeg(src, level, size, offset, options)
  125. if level == 1 then
  126. return pairwise_transform.jpeg_(src, {torch.random(65, 85)},
  127. size, offset,
  128. options)
  129. elseif level == 2 then
  130. local r = torch.uniform()
  131. if r > 0.6 then
  132. return pairwise_transform.jpeg_(src, {torch.random(27, 70)},
  133. size, offset,
  134. options)
  135. elseif r > 0.3 then
  136. local quality1 = torch.random(37, 70)
  137. local quality2 = quality1 - torch.random(5, 10)
  138. return pairwise_transform.jpeg_(src, {quality1, quality2},
  139. size, offset,
  140. options)
  141. else
  142. local quality1 = torch.random(52, 70)
  143. return pairwise_transform.jpeg_(src,
  144. {quality1,
  145. quality1 - torch.random(5, 15),
  146. quality1 - torch.random(15, 25)},
  147. size, offset,
  148. options)
  149. end
  150. else
  151. error("unknown noise level: " .. level)
  152. end
  153. end
  154. function pairwise_transform.jpeg_scale_(src, scale, quality, size, offset, options)
  155. if options.random_half then
  156. src = random_half(src)
  157. end
  158. local down_scale = 1.0 / scale
  159. local filters = {
  160. "Box", -- 0.012756949974688
  161. "Blackman", -- 0.013191924552285
  162. --"Cartom", -- 0.013753536746706
  163. --"Hanning", -- 0.013761314529647
  164. --"Hermite", -- 0.013850225205266
  165. "SincFast", -- 0.014095824314306
  166. "Jinc", -- 0.014244299255442
  167. }
  168. local downscale_filter = filters[torch.random(1, #filters)]
  169. local yi = torch.random(INTERPOLATION_PADDING, src:size(2) - size - INTERPOLATION_PADDING)
  170. local xi = torch.random(INTERPOLATION_PADDING, src:size(3) - size - INTERPOLATION_PADDING)
  171. local y = src
  172. local x
  173. if options.color_augment then
  174. y = color_augment(y)
  175. end
  176. x = y
  177. x = iproc.scale(x, y:size(3) * down_scale, y:size(2) * down_scale, downscale_filter)
  178. for i = 1, #quality do
  179. x = gm.Image(x, "RGB", "DHW")
  180. x:format("jpeg")
  181. x:samplingFactors({1.0, 1.0, 1.0})
  182. local blob, len = x:toBlob(quality[i])
  183. x:fromBlob(blob, len)
  184. x = x:toTensor("byte", "RGB", "DHW")
  185. end
  186. x = iproc.scale(x, y:size(3), y:size(2))
  187. y = image.crop(y,
  188. xi, yi,
  189. xi + size, yi + size)
  190. x = image.crop(x,
  191. xi, yi,
  192. xi + size, yi + size)
  193. x = x:float():div(255)
  194. y = y:float():div(255)
  195. x, y = flip_augment(x, y)
  196. if options.rgb then
  197. else
  198. y = image.rgb2yuv(y)[1]:reshape(1, y:size(2), y:size(3))
  199. x = image.rgb2yuv(x)[1]:reshape(1, x:size(2), x:size(3))
  200. end
  201. return x, image.crop(y, offset, offset, size - offset, size - offset)
  202. end
  203. function pairwise_transform.jpeg_scale(src, scale, level, size, offset, options)
  204. options = options or {color_augment = true, random_half = true}
  205. if level == 1 then
  206. return pairwise_transform.jpeg_scale_(src, scale, {torch.random(65, 85)},
  207. size, offset, options)
  208. elseif level == 2 then
  209. local r = torch.uniform()
  210. if r > 0.6 then
  211. return pairwise_transform.jpeg_scale_(src, scale, {torch.random(27, 70)},
  212. size, offset, options)
  213. elseif r > 0.3 then
  214. local quality1 = torch.random(37, 70)
  215. local quality2 = quality1 - torch.random(5, 10)
  216. return pairwise_transform.jpeg_scale_(src, scale, {quality1, quality2},
  217. size, offset, options)
  218. else
  219. local quality1 = torch.random(52, 70)
  220. return pairwise_transform.jpeg_scale_(src, scale,
  221. {quality1,
  222. quality1 - torch.random(5, 15),
  223. quality1 - torch.random(15, 25)},
  224. size, offset, options)
  225. end
  226. else
  227. error("unknown noise level: " .. level)
  228. end
  229. end
  230. local function test_jpeg()
  231. local loader = require './image_loader'
  232. local src = loader.load_byte("../images/miku_CC_BY-NC.jpg")
  233. local y, x = pairwise_transform.jpeg_(src, {}, 128, 0, false)
  234. image.display({image = y, legend = "y:0"})
  235. image.display({image = x, legend = "x:0"})
  236. for i = 2, 9 do
  237. local y, x = pairwise_transform.jpeg_(pairwise_transform.random_half(src),
  238. {i * 10}, 128, 0, {color_augment = false, random_half = true})
  239. image.display({image = y, legend = "y:" .. (i * 10), max=1,min=0})
  240. image.display({image = x, legend = "x:" .. (i * 10),max=1,min=0})
  241. --print(x:mean(), y:mean())
  242. end
  243. end
  244. local function test_scale()
  245. local loader = require './image_loader'
  246. local src = loader.load_byte("../images/miku_CC_BY-NC.jpg")
  247. for i = 1, 9 do
  248. local y, x = pairwise_transform.scale(src, 2.0, 128, 7, {color_augment = true, random_half = true, rgb = true})
  249. image.display({image = y, legend = "y:" .. (i * 10), min = 0, max = 1})
  250. image.display({image = x, legend = "x:" .. (i * 10), min = 0, max = 1})
  251. print(y:size(), x:size())
  252. --print(x:mean(), y:mean())
  253. end
  254. end
  255. local function test_jpeg_scale()
  256. local loader = require './image_loader'
  257. local src = loader.load_byte("../images/miku_CC_BY-NC.jpg")
  258. for i = 1, 9 do
  259. local y, x = pairwise_transform.jpeg_scale(src, 2.0, 1, 128, 7, {color_augment = true, random_half = true})
  260. image.display({image = y, legend = "y1:" .. (i * 10), min = 0, max = 1})
  261. image.display({image = x, legend = "x1:" .. (i * 10), min = 0, max = 1})
  262. print(y:size(), x:size())
  263. --print(x:mean(), y:mean())
  264. end
  265. for i = 1, 9 do
  266. local y, x = pairwise_transform.jpeg_scale(src, 2.0, 2, 128, 7, {color_augment = true, random_half = true})
  267. image.display({image = y, legend = "y2:" .. (i * 10), min = 0, max = 1})
  268. image.display({image = x, legend = "x2:" .. (i * 10), min = 0, max = 1})
  269. print(y:size(), x:size())
  270. --print(x:mean(), y:mean())
  271. end
  272. end
  273. --test_scale()
  274. --test_jpeg()
  275. --test_jpeg_scale()
  276. return pairwise_transform