pairwise_transform.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 function overlay_augment(src, p)
  66. p = p or 0.25
  67. if torch.uniform() > (1.0 - p) then
  68. local r = torch.uniform(0.2, 0.8)
  69. local t = "float"
  70. if src:type() == "torch.ByteTensor" then
  71. src = src:float():div(255)
  72. t = "byte"
  73. end
  74. local flip = flip_augment(src)
  75. flip:mul(r):add(src * (1.0 - r))
  76. if t == "byte" then
  77. flip = flip:mul(255):byte()
  78. end
  79. return flip
  80. else
  81. return src
  82. end
  83. end
  84. local INTERPOLATION_PADDING = 16
  85. function pairwise_transform.scale(src, scale, size, offset, options)
  86. options = options or {color_noise = false, overlay = false, random_half = true, rgb = true}
  87. if options.random_half then
  88. src = random_half(src)
  89. end
  90. local yi = torch.random(INTERPOLATION_PADDING, src:size(2) - size - INTERPOLATION_PADDING)
  91. local xi = torch.random(INTERPOLATION_PADDING, src:size(3) - size - INTERPOLATION_PADDING)
  92. local down_scale = 1.0 / scale
  93. local y = image.crop(src,
  94. xi - INTERPOLATION_PADDING, yi - INTERPOLATION_PADDING,
  95. xi + size + INTERPOLATION_PADDING, yi + size + INTERPOLATION_PADDING)
  96. local filters = {
  97. "Box", -- 0.012756949974688
  98. "Blackman", -- 0.013191924552285
  99. --"Cartom", -- 0.013753536746706
  100. --"Hanning", -- 0.013761314529647
  101. --"Hermite", -- 0.013850225205266
  102. "SincFast", -- 0.014095824314306
  103. "Jinc", -- 0.014244299255442
  104. }
  105. local downscale_filter = filters[torch.random(1, #filters)]
  106. y = flip_augment(y)
  107. if options.color_noise then
  108. y = color_noise(y)
  109. end
  110. if options.overlay then
  111. y = overlay_augment(y)
  112. end
  113. local x = iproc.scale(y, y:size(3) * down_scale, y:size(2) * down_scale, downscale_filter)
  114. x = iproc.scale(x, y:size(3), y:size(2))
  115. y = y:float():div(255)
  116. x = x:float():div(255)
  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. y = image.crop(y, INTERPOLATION_PADDING + offset, INTERPOLATION_PADDING + offset, y:size(3) - offset - INTERPOLATION_PADDING, y:size(2) - offset - INTERPOLATION_PADDING)
  123. x = image.crop(x, INTERPOLATION_PADDING, INTERPOLATION_PADDING, x:size(3) - INTERPOLATION_PADDING, x:size(2) - INTERPOLATION_PADDING)
  124. return x, y
  125. end
  126. function pairwise_transform.jpeg_(src, quality, size, offset, options)
  127. options = options or {color_noise = false, overlay = false, random_half = true, rgb = true}
  128. if options.random_half then
  129. src = random_half(src)
  130. end
  131. local yi = torch.random(0, src:size(2) - size - 1)
  132. local xi = torch.random(0, src:size(3) - size - 1)
  133. local y = src
  134. local x
  135. if options.color_noise then
  136. y = color_noise(y)
  137. end
  138. if options.overlay then
  139. y = overlay_augment(y)
  140. end
  141. x = y
  142. for i = 1, #quality do
  143. x = gm.Image(x, "RGB", "DHW")
  144. x:format("jpeg")
  145. x:samplingFactors({1.0, 1.0, 1.0})
  146. local blob, len = x:toBlob(quality[i])
  147. x:fromBlob(blob, len)
  148. x = x:toTensor("byte", "RGB", "DHW")
  149. end
  150. y = image.crop(y, xi, yi, xi + size, yi + size)
  151. x = image.crop(x, xi, yi, xi + size, yi + size)
  152. y = y:float():div(255)
  153. x = x:float():div(255)
  154. x, y = flip_augment(x, y)
  155. if options.rgb then
  156. else
  157. y = image.rgb2yuv(y)[1]:reshape(1, y:size(2), y:size(3))
  158. x = image.rgb2yuv(x)[1]:reshape(1, x:size(2), x:size(3))
  159. end
  160. return x, image.crop(y, offset, offset, size - offset, size - offset)
  161. end
  162. function pairwise_transform.jpeg(src, category, level, size, offset, options)
  163. if category == "anime_style_art" then
  164. if level == 1 then
  165. if torch.uniform() > 0.7 then
  166. return pairwise_transform.jpeg_(src, {},
  167. size, offset,
  168. options)
  169. else
  170. return pairwise_transform.jpeg_(src, {torch.random(65, 85)},
  171. size, offset,
  172. options)
  173. end
  174. elseif level == 2 then
  175. if torch.uniform() > 0.7 then
  176. return pairwise_transform.jpeg_(src, {},
  177. size, offset,
  178. options)
  179. else
  180. local r = torch.uniform()
  181. if r > 0.6 then
  182. return pairwise_transform.jpeg_(src, {torch.random(27, 70)},
  183. size, offset,
  184. options)
  185. elseif r > 0.3 then
  186. local quality1 = torch.random(37, 70)
  187. local quality2 = quality1 - torch.random(5, 10)
  188. return pairwise_transform.jpeg_(src, {quality1, quality2},
  189. size, offset,
  190. options)
  191. else
  192. local quality1 = torch.random(52, 70)
  193. return pairwise_transform.jpeg_(src,
  194. {quality1,
  195. quality1 - torch.random(5, 15),
  196. quality1 - torch.random(15, 25)},
  197. size, offset,
  198. options)
  199. end
  200. end
  201. else
  202. error("unknown noise level: " .. level)
  203. end
  204. elseif category == "photo" then
  205. if level == 1 then
  206. if torch.uniform() > 0.7 then
  207. return pairwise_transform.jpeg_(src, {},
  208. size, offset,
  209. options)
  210. else
  211. return pairwise_transform.jpeg_(src, {torch.random(80, 95)},
  212. size, offset,
  213. options)
  214. end
  215. elseif level == 2 then
  216. if torch.uniform() > 0.7 then
  217. return pairwise_transform.jpeg_(src, {},
  218. size, offset,
  219. options)
  220. else
  221. return pairwise_transform.jpeg_(src, {torch.random(65, 85)},
  222. size, offset,
  223. options)
  224. end
  225. end
  226. else
  227. error("unknown category: " .. category)
  228. end
  229. end
  230. function pairwise_transform.jpeg_scale_(src, scale, quality, size, offset, options)
  231. if options.random_half then
  232. src = random_half(src)
  233. end
  234. local down_scale = 1.0 / scale
  235. local filters = {
  236. "Box", -- 0.012756949974688
  237. "Blackman", -- 0.013191924552285
  238. --"Cartom", -- 0.013753536746706
  239. --"Hanning", -- 0.013761314529647
  240. --"Hermite", -- 0.013850225205266
  241. "SincFast", -- 0.014095824314306
  242. "Jinc", -- 0.014244299255442
  243. }
  244. local downscale_filter = filters[torch.random(1, #filters)]
  245. local yi = torch.random(INTERPOLATION_PADDING, src:size(2) - size - INTERPOLATION_PADDING)
  246. local xi = torch.random(INTERPOLATION_PADDING, src:size(3) - size - INTERPOLATION_PADDING)
  247. local y = src
  248. local x
  249. if options.color_noise then
  250. y = color_noise(y)
  251. end
  252. if options.overlay then
  253. y = overlay_augment(y)
  254. end
  255. x = y
  256. x = iproc.scale(x, y:size(3) * down_scale, y:size(2) * down_scale, downscale_filter)
  257. for i = 1, #quality do
  258. x = gm.Image(x, "RGB", "DHW")
  259. x:format("jpeg")
  260. x:samplingFactors({1.0, 1.0, 1.0})
  261. local blob, len = x:toBlob(quality[i])
  262. x:fromBlob(blob, len)
  263. x = x:toTensor("byte", "RGB", "DHW")
  264. end
  265. x = iproc.scale(x, y:size(3), y:size(2))
  266. y = image.crop(y,
  267. xi, yi,
  268. xi + size, yi + size)
  269. x = image.crop(x,
  270. xi, yi,
  271. xi + size, yi + size)
  272. x = x:float():div(255)
  273. y = y:float():div(255)
  274. x, y = flip_augment(x, y)
  275. if options.rgb then
  276. else
  277. y = image.rgb2yuv(y)[1]:reshape(1, y:size(2), y:size(3))
  278. x = image.rgb2yuv(x)[1]:reshape(1, x:size(2), x:size(3))
  279. end
  280. return x, image.crop(y, offset, offset, size - offset, size - offset)
  281. end
  282. function pairwise_transform.jpeg_scale(src, scale, category, level, size, offset, options)
  283. options = options or {color_noise = false, random_half = true}
  284. if category == "anime_style_art" then
  285. if level == 1 then
  286. if torch.uniform() > 0.7 then
  287. return pairwise_transform.jpeg_scale_(src, scale, {},
  288. size, offset, options)
  289. else
  290. return pairwise_transform.jpeg_scale_(src, scale, {torch.random(65, 85)},
  291. size, offset, options)
  292. end
  293. elseif level == 2 then
  294. if torch.uniform() > 0.7 then
  295. return pairwise_transform.jpeg_scale_(src, scale, {},
  296. size, offset, options)
  297. else
  298. local r = torch.uniform()
  299. if r > 0.6 then
  300. return pairwise_transform.jpeg_scale_(src, scale, {torch.random(27, 70)},
  301. size, offset, options)
  302. elseif r > 0.3 then
  303. local quality1 = torch.random(37, 70)
  304. local quality2 = quality1 - torch.random(5, 10)
  305. return pairwise_transform.jpeg_scale_(src, scale, {quality1, quality2},
  306. size, offset, options)
  307. else
  308. local quality1 = torch.random(52, 70)
  309. return pairwise_transform.jpeg_scale_(src, scale,
  310. {quality1,
  311. quality1 - torch.random(5, 15),
  312. quality1 - torch.random(15, 25)},
  313. size, offset, options)
  314. end
  315. end
  316. else
  317. error("unknown noise level: " .. level)
  318. end
  319. elseif category == "photo" then
  320. if level == 1 then
  321. if torch.uniform() > 0.7 then
  322. return pairwise_transform.jpeg_scale_(src, scale, {},
  323. size, offset, options)
  324. else
  325. return pairwise_transform.jpeg_scale_(src, scale, {torch.random(80, 95)},
  326. size, offset, options)
  327. end
  328. elseif level == 2 then
  329. return pairwise_transform.jpeg_scale_(src, scale, {torch.random(70, 85)},
  330. size, offset, options)
  331. else
  332. error("unknown noise level: " .. level)
  333. end
  334. else
  335. error("unknown category: " .. category)
  336. end
  337. end
  338. local function test_jpeg()
  339. local loader = require './image_loader'
  340. local src = loader.load_byte("../images/miku_CC_BY-NC.jpg")
  341. local y, x = pairwise_transform.jpeg_(src, {}, 128, 0, {})
  342. image.display({image = y, legend = "y:0"})
  343. image.display({image = x, legend = "x:0"})
  344. for i = 2, 9 do
  345. local y, x = pairwise_transform.jpeg_(random_half(src),
  346. {i * 10}, 128, 0, {color_noise = false, random_half = true, overlay = true, rgb = true})
  347. image.display({image = y, legend = "y:" .. (i * 10), max=1,min=0})
  348. image.display({image = x, legend = "x:" .. (i * 10),max=1,min=0})
  349. --print(x:mean(), y:mean())
  350. end
  351. end
  352. local function test_scale()
  353. torch.setdefaulttensortype('torch.FloatTensor')
  354. local loader = require './image_loader'
  355. local src = loader.load_byte("../images/miku_CC_BY-NC.jpg")
  356. for i = 1, 9 do
  357. local y, x = pairwise_transform.scale(src, 2.0, 128, 7, {color_noise = true, random_half = true, rgb = true, overlay = true})
  358. image.display({image = y, legend = "y:" .. (i * 10), min = 0, max = 1})
  359. image.display({image = x, legend = "x:" .. (i * 10), min = 0, max = 1})
  360. print(y:size(), x:size())
  361. --print(x:mean(), y:mean())
  362. end
  363. end
  364. local function test_jpeg_scale()
  365. torch.setdefaulttensortype('torch.FloatTensor')
  366. local loader = require './image_loader'
  367. local src = loader.load_byte("../images/miku_CC_BY-NC.jpg")
  368. for i = 1, 9 do
  369. local y, x = pairwise_transform.jpeg_scale(src, 2.0, 1, 128, 7, {color_noise = true, random_half = true, overlay = true})
  370. image.display({image = y, legend = "y1:" .. (i * 10), min = 0, max = 1})
  371. image.display({image = x, legend = "x1:" .. (i * 10), min = 0, max = 1})
  372. print(y:size(), x:size())
  373. --print(x:mean(), y:mean())
  374. end
  375. for i = 1, 9 do
  376. local y, x = pairwise_transform.jpeg_scale(src, 2.0, 2, 128, 7, {color_noise = true, random_half = true, overlay = true})
  377. image.display({image = y, legend = "y2:" .. (i * 10), min = 0, max = 1})
  378. image.display({image = x, legend = "x2:" .. (i * 10), min = 0, max = 1})
  379. print(y:size(), x:size())
  380. --print(x:mean(), y:mean())
  381. end
  382. end
  383. local function test_color_noise()
  384. torch.setdefaulttensortype('torch.FloatTensor')
  385. local loader = require './image_loader'
  386. local src = loader.load_byte("../images/miku_CC_BY-NC.jpg")
  387. for i = 1, 10 do
  388. image.display(color_noise(src))
  389. end
  390. end
  391. local function test_overlay()
  392. torch.setdefaulttensortype('torch.FloatTensor')
  393. local loader = require './image_loader'
  394. local src = loader.load_byte("../images/miku_CC_BY-NC.jpg")
  395. for i = 1, 10 do
  396. image.display(overlay_augment(src, 1.0))
  397. end
  398. end
  399. --test_scale()
  400. --test_jpeg()
  401. --test_jpeg_scale()
  402. --test_color_noise()
  403. --test_overlay()
  404. return pairwise_transform