pairwise_transform.lua 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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}
  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. y = image.rgb2yuv(y)[1]:reshape(1, y:size(2), y:size(3))
  83. x = image.rgb2yuv(x)[1]:reshape(1, x:size(2), x:size(3))
  84. y = image.crop(y, INTERPOLATION_PADDING + offset, INTERPOLATION_PADDING + offset, y:size(3) - offset - INTERPOLATION_PADDING, y:size(2) - offset - INTERPOLATION_PADDING)
  85. x = image.crop(x, INTERPOLATION_PADDING, INTERPOLATION_PADDING, x:size(3) - INTERPOLATION_PADDING, x:size(2) - INTERPOLATION_PADDING)
  86. return x, y
  87. end
  88. function pairwise_transform.jpeg_(src, quality, size, offset, options)
  89. options = options or {color_augment = true, random_half = true}
  90. if options.random_half then
  91. src = random_half(src)
  92. end
  93. local yi = torch.random(0, src:size(2) - size - 1)
  94. local xi = torch.random(0, src:size(3) - size - 1)
  95. local y = src
  96. local x
  97. if options.color_augment then
  98. y = color_augment(y)
  99. end
  100. x = y
  101. for i = 1, #quality do
  102. x = gm.Image(x, "RGB", "DHW")
  103. x:format("jpeg")
  104. local blob, len = x:toBlob(quality[i])
  105. x:fromBlob(blob, len)
  106. x = x:toTensor("byte", "RGB", "DHW")
  107. end
  108. y = image.crop(y, xi, yi, xi + size, yi + size)
  109. x = image.crop(x, xi, yi, xi + size, yi + size)
  110. y = y:float():div(255)
  111. x = x:float():div(255)
  112. x, y = flip_augment(x, y)
  113. y = image.rgb2yuv(y)[1]:reshape(1, y:size(2), y:size(3))
  114. x = image.rgb2yuv(x)[1]:reshape(1, x:size(2), x:size(3))
  115. return x, image.crop(y, offset, offset, size - offset, size - offset)
  116. end
  117. function pairwise_transform.jpeg(src, level, size, offset, options)
  118. if level == 1 then
  119. return pairwise_transform.jpeg_(src, {torch.random(65, 85)},
  120. size, offset,
  121. options)
  122. elseif level == 2 then
  123. local r = torch.uniform()
  124. if r > 0.6 then
  125. return pairwise_transform.jpeg_(src, {torch.random(27, 70)},
  126. size, offset,
  127. options)
  128. elseif r > 0.3 then
  129. local quality1 = torch.random(37, 70)
  130. local quality2 = quality1 - torch.random(5, 10)
  131. return pairwise_transform.jpeg_(src, {quality1, quality2},
  132. size, offset,
  133. options)
  134. else
  135. local quality1 = torch.random(52, 70)
  136. return pairwise_transform.jpeg_(src,
  137. {quality1,
  138. quality1 - torch.random(5, 15),
  139. quality1 - torch.random(15, 25)},
  140. size, offset,
  141. options)
  142. end
  143. else
  144. error("unknown noise level: " .. level)
  145. end
  146. end
  147. function pairwise_transform.jpeg_scale_(src, scale, quality, size, offset, options)
  148. if options.random_half then
  149. src = random_half(src)
  150. end
  151. local down_scale = 1.0 / scale
  152. local filters = {
  153. "Box", -- 0.012756949974688
  154. --"Blackman", -- 0.013191924552285
  155. --"Cartom", -- 0.013753536746706
  156. --"Hanning", -- 0.013761314529647
  157. --"Hermite", -- 0.013850225205266
  158. --"SincFast", -- 0.014095824314306
  159. --"Jinc", -- 0.014244299255442
  160. }
  161. local downscale_filter = filters[torch.random(1, #filters)]
  162. local yi = torch.random(INTERPOLATION_PADDING, src:size(2) - size - INTERPOLATION_PADDING)
  163. local xi = torch.random(INTERPOLATION_PADDING, src:size(3) - size - INTERPOLATION_PADDING)
  164. local y = src
  165. local x
  166. if options.color_augment then
  167. y = color_augment(y)
  168. end
  169. x = y
  170. x = iproc.scale(x, y:size(3) * down_scale, y:size(2) * down_scale, downscale_filter)
  171. for i = 1, #quality do
  172. x = gm.Image(x, "RGB", "DHW")
  173. x:format("jpeg")
  174. local blob, len = x:toBlob(quality[i])
  175. x:fromBlob(blob, len)
  176. x = x:toTensor("byte", "RGB", "DHW")
  177. end
  178. x = iproc.scale(x, y:size(3), y:size(2))
  179. y = image.crop(y,
  180. xi, yi,
  181. xi + size, yi + size)
  182. x = image.crop(x,
  183. xi, yi,
  184. xi + size, yi + size)
  185. x = x:float():div(255)
  186. y = y:float():div(255)
  187. x, y = flip_augment(x, y)
  188. y = image.rgb2yuv(y)[1]:reshape(1, y:size(2), y:size(3))
  189. x = image.rgb2yuv(x)[1]:reshape(1, x:size(2), x:size(3))
  190. return x, image.crop(y, offset, offset, size - offset, size - offset)
  191. end
  192. function pairwise_transform.jpeg_scale(src, scale, level, size, offset, options)
  193. options = options or {color_augment = true, random_half = true}
  194. if level == 1 then
  195. return pairwise_transform.jpeg_scale_(src, scale, {torch.random(65, 85)},
  196. size, offset, options)
  197. elseif level == 2 then
  198. local r = torch.uniform()
  199. if r > 0.6 then
  200. return pairwise_transform.jpeg_scale_(src, scale, {torch.random(27, 70)},
  201. size, offset, options)
  202. elseif r > 0.3 then
  203. local quality1 = torch.random(37, 70)
  204. local quality2 = quality1 - torch.random(5, 10)
  205. return pairwise_transform.jpeg_scale_(src, scale, {quality1, quality2},
  206. size, offset, options)
  207. else
  208. local quality1 = torch.random(52, 70)
  209. return pairwise_transform.jpeg_scale_(src, scale,
  210. {quality1,
  211. quality1 - torch.random(5, 15),
  212. quality1 - torch.random(15, 25)},
  213. size, offset, options)
  214. end
  215. else
  216. error("unknown noise level: " .. level)
  217. end
  218. end
  219. local function test_jpeg()
  220. local loader = require './image_loader'
  221. local src = loader.load_byte("../images/miku_CC_BY-NC.jpg")
  222. local y, x = pairwise_transform.jpeg_(src, {}, 128, 0, false)
  223. image.display({image = y, legend = "y:0"})
  224. image.display({image = x, legend = "x:0"})
  225. for i = 2, 9 do
  226. local y, x = pairwise_transform.jpeg_(pairwise_transform.random_half(src),
  227. {i * 10}, 128, 0, {color_augment = false, random_half = true})
  228. image.display({image = y, legend = "y:" .. (i * 10), max=1,min=0})
  229. image.display({image = x, legend = "x:" .. (i * 10),max=1,min=0})
  230. --print(x:mean(), y:mean())
  231. end
  232. end
  233. local function test_scale()
  234. local loader = require './image_loader'
  235. local src = loader.load_byte("../images/miku_CC_BY-NC.jpg")
  236. for i = 1, 9 do
  237. local y, x = pairwise_transform.scale(src, 2.0, 128, 7, {color_augment = true, random_half = true})
  238. image.display({image = y, legend = "y:" .. (i * 10), min = 0, max = 1})
  239. image.display({image = x, legend = "x:" .. (i * 10), min = 0, max = 1})
  240. print(y:size(), x:size())
  241. --print(x:mean(), y:mean())
  242. end
  243. end
  244. local function test_jpeg_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.jpeg_scale(src, 2.0, 1, 128, 7, {color_augment = true, random_half = true})
  249. image.display({image = y, legend = "y1:" .. (i * 10), min = 0, max = 1})
  250. image.display({image = x, legend = "x1:" .. (i * 10), min = 0, max = 1})
  251. print(y:size(), x:size())
  252. --print(x:mean(), y:mean())
  253. end
  254. for i = 1, 9 do
  255. local y, x = pairwise_transform.jpeg_scale(src, 2.0, 2, 128, 7, {color_augment = true, random_half = true})
  256. image.display({image = y, legend = "y2:" .. (i * 10), min = 0, max = 1})
  257. image.display({image = x, legend = "x2:" .. (i * 10), min = 0, max = 1})
  258. print(y:size(), x:size())
  259. --print(x:mean(), y:mean())
  260. end
  261. end
  262. --test_jpeg()
  263. --test_scale()
  264. --test_jpeg_scale()
  265. return pairwise_transform