pairwise_transform.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. if torch.uniform() > 0.7 then
  141. return pairwise_transform.jpeg_(src, {},
  142. size, offset,
  143. options)
  144. else
  145. return pairwise_transform.jpeg_(src, {torch.random(65, 85)},
  146. size, offset,
  147. options)
  148. end
  149. elseif level == 2 then
  150. if torch.uniform() > 0.7 then
  151. return pairwise_transform.jpeg_(src, {},
  152. size, offset,
  153. options)
  154. else
  155. local r = torch.uniform()
  156. if r > 0.6 then
  157. return pairwise_transform.jpeg_(src, {torch.random(27, 70)},
  158. size, offset,
  159. options)
  160. elseif r > 0.3 then
  161. local quality1 = torch.random(37, 70)
  162. local quality2 = quality1 - torch.random(5, 10)
  163. return pairwise_transform.jpeg_(src, {quality1, quality2},
  164. size, offset,
  165. options)
  166. else
  167. local quality1 = torch.random(52, 70)
  168. return pairwise_transform.jpeg_(src,
  169. {quality1,
  170. quality1 - torch.random(5, 15),
  171. quality1 - torch.random(15, 25)},
  172. size, offset,
  173. options)
  174. end
  175. end
  176. else
  177. error("unknown noise level: " .. level)
  178. end
  179. elseif category == "photo" then
  180. if level == 1 then
  181. if torch.uniform() > 0.7 then
  182. return pairwise_transform.jpeg_(src, {},
  183. size, offset,
  184. options)
  185. else
  186. return pairwise_transform.jpeg_(src, {torch.random(80, 95)},
  187. size, offset,
  188. options)
  189. end
  190. elseif level == 2 then
  191. if torch.uniform() > 0.7 then
  192. return pairwise_transform.jpeg_(src, {},
  193. size, offset,
  194. options)
  195. else
  196. return pairwise_transform.jpeg_(src, {torch.random(65, 85)},
  197. size, offset,
  198. options)
  199. end
  200. end
  201. else
  202. error("unknown category: " .. category)
  203. end
  204. end
  205. function pairwise_transform.jpeg_scale_(src, scale, quality, size, offset, options)
  206. if options.random_half then
  207. src = random_half(src)
  208. end
  209. local down_scale = 1.0 / scale
  210. local filters = {
  211. "Box", -- 0.012756949974688
  212. "Blackman", -- 0.013191924552285
  213. --"Cartom", -- 0.013753536746706
  214. --"Hanning", -- 0.013761314529647
  215. --"Hermite", -- 0.013850225205266
  216. "SincFast", -- 0.014095824314306
  217. "Jinc", -- 0.014244299255442
  218. }
  219. local downscale_filter = filters[torch.random(1, #filters)]
  220. local yi = torch.random(INTERPOLATION_PADDING, src:size(2) - size - INTERPOLATION_PADDING)
  221. local xi = torch.random(INTERPOLATION_PADDING, src:size(3) - size - INTERPOLATION_PADDING)
  222. local y = src
  223. local x
  224. if options.color_noise then
  225. y = color_noise(y)
  226. end
  227. x = y
  228. x = iproc.scale(x, y:size(3) * down_scale, y:size(2) * down_scale, downscale_filter)
  229. for i = 1, #quality do
  230. x = gm.Image(x, "RGB", "DHW")
  231. x:format("jpeg")
  232. x:samplingFactors({1.0, 1.0, 1.0})
  233. local blob, len = x:toBlob(quality[i])
  234. x:fromBlob(blob, len)
  235. x = x:toTensor("byte", "RGB", "DHW")
  236. end
  237. x = iproc.scale(x, y:size(3), y:size(2))
  238. y = image.crop(y,
  239. xi, yi,
  240. xi + size, yi + size)
  241. x = image.crop(x,
  242. xi, yi,
  243. xi + size, yi + size)
  244. x = x:float():div(255)
  245. y = y:float():div(255)
  246. x, y = flip_augment(x, y)
  247. if options.rgb then
  248. else
  249. y = image.rgb2yuv(y)[1]:reshape(1, y:size(2), y:size(3))
  250. x = image.rgb2yuv(x)[1]:reshape(1, x:size(2), x:size(3))
  251. end
  252. return x, image.crop(y, offset, offset, size - offset, size - offset)
  253. end
  254. function pairwise_transform.jpeg_scale(src, scale, category, level, size, offset, options)
  255. options = options or {color_noise = false, random_half = true}
  256. if category == "anime_style_art" then
  257. if level == 1 then
  258. if torch.uniform() > 0.7 then
  259. return pairwise_transform.jpeg_scale_(src, scale, {},
  260. size, offset, options)
  261. else
  262. return pairwise_transform.jpeg_scale_(src, scale, {torch.random(65, 85)},
  263. size, offset, options)
  264. end
  265. elseif level == 2 then
  266. if torch.uniform() > 0.7 then
  267. return pairwise_transform.jpeg_scale_(src, scale, {},
  268. size, offset, options)
  269. else
  270. local r = torch.uniform()
  271. if r > 0.6 then
  272. return pairwise_transform.jpeg_scale_(src, scale, {torch.random(27, 70)},
  273. size, offset, options)
  274. elseif r > 0.3 then
  275. local quality1 = torch.random(37, 70)
  276. local quality2 = quality1 - torch.random(5, 10)
  277. return pairwise_transform.jpeg_scale_(src, scale, {quality1, quality2},
  278. size, offset, options)
  279. else
  280. local quality1 = torch.random(52, 70)
  281. return pairwise_transform.jpeg_scale_(src, scale,
  282. {quality1,
  283. quality1 - torch.random(5, 15),
  284. quality1 - torch.random(15, 25)},
  285. size, offset, options)
  286. end
  287. end
  288. else
  289. error("unknown noise level: " .. level)
  290. end
  291. elseif category == "photo" then
  292. if level == 1 then
  293. if torch.uniform() > 0.7 then
  294. return pairwise_transform.jpeg_scale_(src, scale, {},
  295. size, offset, options)
  296. else
  297. return pairwise_transform.jpeg_scale_(src, scale, {torch.random(80, 95)},
  298. size, offset, options)
  299. end
  300. elseif level == 2 then
  301. return pairwise_transform.jpeg_scale_(src, scale, {torch.random(70, 85)},
  302. size, offset, options)
  303. else
  304. error("unknown noise level: " .. level)
  305. end
  306. else
  307. error("unknown category: " .. category)
  308. end
  309. end
  310. local function test_jpeg()
  311. local loader = require './image_loader'
  312. local src = loader.load_byte("../images/miku_CC_BY-NC.jpg")
  313. local y, x = pairwise_transform.jpeg_(src, {}, 128, 0, false)
  314. image.display({image = y, legend = "y:0"})
  315. image.display({image = x, legend = "x:0"})
  316. for i = 2, 9 do
  317. local y, x = pairwise_transform.jpeg_(pairwise_transform.random_half(src),
  318. {i * 10}, 128, 0, {color_noise = false, random_half = true})
  319. image.display({image = y, legend = "y:" .. (i * 10), max=1,min=0})
  320. image.display({image = x, legend = "x:" .. (i * 10),max=1,min=0})
  321. --print(x:mean(), y:mean())
  322. end
  323. end
  324. local function test_scale()
  325. torch.setdefaulttensortype('torch.FloatTensor')
  326. local loader = require './image_loader'
  327. local src = loader.load_byte("../images/miku_CC_BY-NC.jpg")
  328. for i = 1, 9 do
  329. local y, x = pairwise_transform.scale(src, 2.0, 128, 7, {color_noise = true, random_half = true, rgb = true})
  330. image.display({image = y, legend = "y:" .. (i * 10), min = 0, max = 1})
  331. image.display({image = x, legend = "x:" .. (i * 10), min = 0, max = 1})
  332. print(y:size(), x:size())
  333. --print(x:mean(), y:mean())
  334. end
  335. end
  336. local function test_jpeg_scale()
  337. torch.setdefaulttensortype('torch.FloatTensor')
  338. local loader = require './image_loader'
  339. local src = loader.load_byte("../images/miku_CC_BY-NC.jpg")
  340. for i = 1, 9 do
  341. local y, x = pairwise_transform.jpeg_scale(src, 2.0, 1, 128, 7, {color_noise = true, random_half = true})
  342. image.display({image = y, legend = "y1:" .. (i * 10), min = 0, max = 1})
  343. image.display({image = x, legend = "x1:" .. (i * 10), min = 0, max = 1})
  344. print(y:size(), x:size())
  345. --print(x:mean(), y:mean())
  346. end
  347. for i = 1, 9 do
  348. local y, x = pairwise_transform.jpeg_scale(src, 2.0, 2, 128, 7, {color_noise = true, random_half = true})
  349. image.display({image = y, legend = "y2:" .. (i * 10), min = 0, max = 1})
  350. image.display({image = x, legend = "x2:" .. (i * 10), min = 0, max = 1})
  351. print(y:size(), x:size())
  352. --print(x:mean(), y:mean())
  353. end
  354. end
  355. local function test_color_noise()
  356. torch.setdefaulttensortype('torch.FloatTensor')
  357. local loader = require './image_loader'
  358. local src = loader.load_byte("../images/miku_CC_BY-NC.jpg")
  359. for i = 1, 10 do
  360. image.display(color_noise(src))
  361. end
  362. end
  363. --test_scale()
  364. --test_jpeg()
  365. --test_jpeg_scale()
  366. --test_color_noise()
  367. return pairwise_transform