pairwise_transform.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 pcacov(x)
  16. local mean = torch.mean(x, 1)
  17. local xm = x - torch.ger(torch.ones(x:size(1)), mean:squeeze())
  18. local c = torch.mm(xm:t(), xm)
  19. c:div(x:size(1) - 1)
  20. local ce, cv = torch.symeig(c, 'V')
  21. return ce, cv
  22. end
  23. local function color_noise(src)
  24. local p = 0.1
  25. src = src:float():div(255)
  26. local src_t = src:reshape(src:size(1), src:nElement() / src:size(1)):t():contiguous()
  27. local ce, cv = pcacov(src_t)
  28. local color_scale = torch.Tensor(3):uniform(1 / (1 + p), 1 + p)
  29. pca_space = torch.mm(src_t, cv):t():contiguous()
  30. for i = 1, 3 do
  31. pca_space[i]:mul(color_scale[i])
  32. end
  33. x = torch.mm(pca_space:t(), cv:t()):t():contiguous():resizeAs(src)
  34. x[torch.lt(x, 0.0)] = 0.0
  35. x[torch.gt(x, 1.0)] = 1.0
  36. return x:mul(255):byte()
  37. end
  38. local function flip_augment(x, y)
  39. local flip = torch.random(1, 4)
  40. if y then
  41. if flip == 1 then
  42. x = image.hflip(x)
  43. y = image.hflip(y)
  44. elseif flip == 2 then
  45. x = image.vflip(x)
  46. y = image.vflip(y)
  47. elseif flip == 3 then
  48. x = image.hflip(image.vflip(x))
  49. y = image.hflip(image.vflip(y))
  50. elseif flip == 4 then
  51. end
  52. return x, y
  53. else
  54. if flip == 1 then
  55. x = image.hflip(x)
  56. elseif flip == 2 then
  57. x = image.vflip(x)
  58. elseif flip == 3 then
  59. x = image.hflip(image.vflip(x))
  60. elseif flip == 4 then
  61. end
  62. return x
  63. end
  64. end
  65. local INTERPOLATION_PADDING = 16
  66. function pairwise_transform.scale(src, scale, size, offset, options)
  67. options = options or {color_noise = false, random_half = true, rgb = true}
  68. if options.random_half then
  69. src = random_half(src)
  70. end
  71. local yi = torch.random(INTERPOLATION_PADDING, src:size(2) - size - INTERPOLATION_PADDING)
  72. local xi = torch.random(INTERPOLATION_PADDING, src:size(3) - size - INTERPOLATION_PADDING)
  73. local down_scale = 1.0 / scale
  74. local y = image.crop(src,
  75. xi - INTERPOLATION_PADDING, yi - INTERPOLATION_PADDING,
  76. xi + size + INTERPOLATION_PADDING, yi + size + INTERPOLATION_PADDING)
  77. local filters = {
  78. "Box", -- 0.012756949974688
  79. "Blackman", -- 0.013191924552285
  80. --"Cartom", -- 0.013753536746706
  81. --"Hanning", -- 0.013761314529647
  82. --"Hermite", -- 0.013850225205266
  83. "SincFast", -- 0.014095824314306
  84. "Jinc", -- 0.014244299255442
  85. }
  86. local downscale_filter = filters[torch.random(1, #filters)]
  87. y = flip_augment(y)
  88. if options.color_noise then
  89. y = color_noise(y)
  90. end
  91. local x = iproc.scale(y, y:size(3) * down_scale, y:size(2) * down_scale, downscale_filter)
  92. x = iproc.scale(x, y:size(3), y:size(2))
  93. y = y:float():div(255)
  94. x = x:float():div(255)
  95. if options.rgb then
  96. else
  97. y = image.rgb2yuv(y)[1]:reshape(1, y:size(2), y:size(3))
  98. x = image.rgb2yuv(x)[1]:reshape(1, x:size(2), x:size(3))
  99. end
  100. y = image.crop(y, INTERPOLATION_PADDING + offset, INTERPOLATION_PADDING + offset, y:size(3) - offset - INTERPOLATION_PADDING, y:size(2) - offset - INTERPOLATION_PADDING)
  101. x = image.crop(x, INTERPOLATION_PADDING, INTERPOLATION_PADDING, x:size(3) - INTERPOLATION_PADDING, x:size(2) - INTERPOLATION_PADDING)
  102. return x, y
  103. end
  104. function pairwise_transform.jpeg_(src, quality, size, offset, options)
  105. options = options or {color_noise = false, random_half = true, rgb = true}
  106. if options.random_half then
  107. src = random_half(src)
  108. end
  109. local yi = torch.random(0, src:size(2) - size - 1)
  110. local xi = torch.random(0, src:size(3) - size - 1)
  111. local y = src
  112. local x
  113. if options.color_noise then
  114. y = color_noise(y)
  115. end
  116. x = y
  117. for i = 1, #quality do
  118. x = gm.Image(x, "RGB", "DHW")
  119. x:format("jpeg")
  120. x:samplingFactors({1.0, 1.0, 1.0})
  121. local blob, len = x:toBlob(quality[i])
  122. x:fromBlob(blob, len)
  123. x = x:toTensor("byte", "RGB", "DHW")
  124. end
  125. y = image.crop(y, xi, yi, xi + size, yi + size)
  126. x = image.crop(x, xi, yi, xi + size, yi + size)
  127. y = y:float():div(255)
  128. x = x:float():div(255)
  129. x, y = flip_augment(x, y)
  130. if options.rgb then
  131. else
  132. y = image.rgb2yuv(y)[1]:reshape(1, y:size(2), y:size(3))
  133. x = image.rgb2yuv(x)[1]:reshape(1, x:size(2), x:size(3))
  134. end
  135. return x, image.crop(y, offset, offset, size - offset, size - offset)
  136. end
  137. function pairwise_transform.jpeg(src, category, level, size, offset, options)
  138. if category == "anime_style_art" then
  139. if level == 1 then
  140. return pairwise_transform.jpeg_(src, {torch.random(65, 85)},
  141. size, offset,
  142. options)
  143. elseif level == 2 then
  144. local r = torch.uniform()
  145. if r > 0.6 then
  146. return pairwise_transform.jpeg_(src, {torch.random(27, 70)},
  147. size, offset,
  148. options)
  149. elseif r > 0.3 then
  150. local quality1 = torch.random(37, 70)
  151. local quality2 = quality1 - torch.random(5, 10)
  152. return pairwise_transform.jpeg_(src, {quality1, quality2},
  153. size, offset,
  154. options)
  155. else
  156. local quality1 = torch.random(52, 70)
  157. return pairwise_transform.jpeg_(src,
  158. {quality1,
  159. quality1 - torch.random(5, 15),
  160. quality1 - torch.random(15, 25)},
  161. size, offset,
  162. options)
  163. end
  164. else
  165. error("unknown noise level: " .. level)
  166. end
  167. elseif category == "photo" then
  168. if level == 1 then
  169. if torch.uniform() > 0.75 then
  170. return pairwise_transform.jpeg_(src, {},
  171. size, offset,
  172. options)
  173. else
  174. return pairwise_transform.jpeg_(src, {torch.random(80, 95)},
  175. size, offset,
  176. options)
  177. end
  178. elseif level == 2 then
  179. return pairwise_transform.jpeg_(src, {torch.random(70, 85)},
  180. size, offset,
  181. options)
  182. end
  183. else
  184. error("unknown category: " .. category)
  185. end
  186. end
  187. function pairwise_transform.jpeg_scale_(src, scale, quality, size, offset, options)
  188. if options.random_half then
  189. src = random_half(src)
  190. end
  191. local down_scale = 1.0 / scale
  192. local filters = {
  193. "Box", -- 0.012756949974688
  194. "Blackman", -- 0.013191924552285
  195. --"Cartom", -- 0.013753536746706
  196. --"Hanning", -- 0.013761314529647
  197. --"Hermite", -- 0.013850225205266
  198. "SincFast", -- 0.014095824314306
  199. "Jinc", -- 0.014244299255442
  200. }
  201. local downscale_filter = filters[torch.random(1, #filters)]
  202. local yi = torch.random(INTERPOLATION_PADDING, src:size(2) - size - INTERPOLATION_PADDING)
  203. local xi = torch.random(INTERPOLATION_PADDING, src:size(3) - size - INTERPOLATION_PADDING)
  204. local y = src
  205. local x
  206. if options.color_noise then
  207. y = color_noise(y)
  208. end
  209. x = y
  210. x = iproc.scale(x, y:size(3) * down_scale, y:size(2) * down_scale, downscale_filter)
  211. for i = 1, #quality do
  212. x = gm.Image(x, "RGB", "DHW")
  213. x:format("jpeg")
  214. x:samplingFactors({1.0, 1.0, 1.0})
  215. local blob, len = x:toBlob(quality[i])
  216. x:fromBlob(blob, len)
  217. x = x:toTensor("byte", "RGB", "DHW")
  218. end
  219. x = iproc.scale(x, y:size(3), y:size(2))
  220. y = image.crop(y,
  221. xi, yi,
  222. xi + size, yi + size)
  223. x = image.crop(x,
  224. xi, yi,
  225. xi + size, yi + size)
  226. x = x:float():div(255)
  227. y = y:float():div(255)
  228. x, y = flip_augment(x, y)
  229. if options.rgb then
  230. else
  231. y = image.rgb2yuv(y)[1]:reshape(1, y:size(2), y:size(3))
  232. x = image.rgb2yuv(x)[1]:reshape(1, x:size(2), x:size(3))
  233. end
  234. return x, image.crop(y, offset, offset, size - offset, size - offset)
  235. end
  236. function pairwise_transform.jpeg_scale(src, scale, category, level, size, offset, options)
  237. options = options or {color_noise = false, random_half = true}
  238. if category == "anime_style_art" then
  239. if level == 1 then
  240. return pairwise_transform.jpeg_scale_(src, scale, {torch.random(65, 85)},
  241. size, offset, options)
  242. elseif level == 2 then
  243. local r = torch.uniform()
  244. if r > 0.6 then
  245. return pairwise_transform.jpeg_scale_(src, scale, {torch.random(27, 70)},
  246. size, offset, options)
  247. elseif r > 0.3 then
  248. local quality1 = torch.random(37, 70)
  249. local quality2 = quality1 - torch.random(5, 10)
  250. return pairwise_transform.jpeg_scale_(src, scale, {quality1, quality2},
  251. size, offset, options)
  252. else
  253. local quality1 = torch.random(52, 70)
  254. return pairwise_transform.jpeg_scale_(src, scale,
  255. {quality1,
  256. quality1 - torch.random(5, 15),
  257. quality1 - torch.random(15, 25)},
  258. size, offset, options)
  259. end
  260. else
  261. error("unknown noise level: " .. level)
  262. end
  263. elseif category == "photo" then
  264. if level == 1 then
  265. if torch.uniform() > 0.75 then
  266. return pairwise_transform.jpeg_scale_(src, scale, {},
  267. size, offset, options)
  268. else
  269. return pairwise_transform.jpeg_scale_(src, scale, {torch.random(80, 95)},
  270. size, offset, options)
  271. end
  272. elseif level == 2 then
  273. return pairwise_transform.jpeg_scale_(src, scale, {torch.random(70, 85)},
  274. size, offset, options)
  275. else
  276. error("unknown noise level: " .. level)
  277. end
  278. else
  279. error("unknown category: " .. category)
  280. end
  281. end
  282. local function test_jpeg()
  283. local loader = require './image_loader'
  284. local src = loader.load_byte("../images/miku_CC_BY-NC.jpg")
  285. local y, x = pairwise_transform.jpeg_(src, {}, 128, 0, false)
  286. image.display({image = y, legend = "y:0"})
  287. image.display({image = x, legend = "x:0"})
  288. for i = 2, 9 do
  289. local y, x = pairwise_transform.jpeg_(pairwise_transform.random_half(src),
  290. {i * 10}, 128, 0, {color_noise = false, random_half = true})
  291. image.display({image = y, legend = "y:" .. (i * 10), max=1,min=0})
  292. image.display({image = x, legend = "x:" .. (i * 10),max=1,min=0})
  293. --print(x:mean(), y:mean())
  294. end
  295. end
  296. local function test_scale()
  297. torch.setdefaulttensortype('torch.FloatTensor')
  298. local loader = require './image_loader'
  299. local src = loader.load_byte("../images/miku_CC_BY-NC.jpg")
  300. for i = 1, 9 do
  301. local y, x = pairwise_transform.scale(src, 2.0, 128, 7, {color_noise = true, random_half = true, rgb = true})
  302. image.display({image = y, legend = "y:" .. (i * 10), min = 0, max = 1})
  303. image.display({image = x, legend = "x:" .. (i * 10), min = 0, max = 1})
  304. print(y:size(), x:size())
  305. --print(x:mean(), y:mean())
  306. end
  307. end
  308. local function test_jpeg_scale()
  309. torch.setdefaulttensortype('torch.FloatTensor')
  310. local loader = require './image_loader'
  311. local src = loader.load_byte("../images/miku_CC_BY-NC.jpg")
  312. for i = 1, 9 do
  313. local y, x = pairwise_transform.jpeg_scale(src, 2.0, 1, 128, 7, {color_noise = true, random_half = true})
  314. image.display({image = y, legend = "y1:" .. (i * 10), min = 0, max = 1})
  315. image.display({image = x, legend = "x1:" .. (i * 10), min = 0, max = 1})
  316. print(y:size(), x:size())
  317. --print(x:mean(), y:mean())
  318. end
  319. for i = 1, 9 do
  320. local y, x = pairwise_transform.jpeg_scale(src, 2.0, 2, 128, 7, {color_noise = true, random_half = true})
  321. image.display({image = y, legend = "y2:" .. (i * 10), min = 0, max = 1})
  322. image.display({image = x, legend = "x2:" .. (i * 10), min = 0, max = 1})
  323. print(y:size(), x:size())
  324. --print(x:mean(), y:mean())
  325. end
  326. end
  327. local function test_color_noise()
  328. torch.setdefaulttensortype('torch.FloatTensor')
  329. local loader = require './image_loader'
  330. local src = loader.load_byte("../images/miku_CC_BY-NC.jpg")
  331. for i = 1, 10 do
  332. image.display(color_noise(src))
  333. end
  334. end
  335. --test_scale()
  336. --test_jpeg()
  337. --test_jpeg_scale()
  338. --test_color_noise()
  339. return pairwise_transform