pairwise_transform.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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)
  7. p = p or 0.25
  8. --local filter = ({"Box","Blackman", "SincFast", "Jinc"})[torch.random(1, 4)]
  9. local filter = "Box"
  10. if p < torch.uniform() and (src:size(2) > 768 and src:size(3) > 1024) then
  11. return iproc.scale(src, src:size(3) * 0.5, src:size(2) * 0.5, filter)
  12. else
  13. return src
  14. end
  15. end
  16. local function pcacov(x)
  17. local mean = torch.mean(x, 1)
  18. local xm = x - torch.ger(torch.ones(x:size(1)), mean:squeeze())
  19. local c = torch.mm(xm:t(), xm)
  20. c:div(x:size(1) - 1)
  21. local ce, cv = torch.symeig(c, 'V')
  22. return ce, cv
  23. end
  24. local function crop_if_large(src, max_size)
  25. if src:size(2) > max_size and src:size(3) > max_size then
  26. local yi = torch.random(0, src:size(2) - max_size)
  27. local xi = torch.random(0, src:size(3) - max_size)
  28. return image.crop(src, xi, yi, xi + max_size, yi + max_size)
  29. else
  30. return src
  31. end
  32. end
  33. local function active_cropping(x, y, size, offset, p, tries)
  34. assert("x:size == y:size", x:size(2) == y:size(2) and x:size(3) == y:size(3))
  35. local r = torch.uniform()
  36. if p < r then
  37. local xi = torch.random(offset, y:size(3) - (size + offset + 1))
  38. local yi = torch.random(offset, y:size(2) - (size + offset + 1))
  39. local xc = image.crop(x, xi, yi, xi + size, yi + size)
  40. local yc = image.crop(y, xi, yi, xi + size, yi + size)
  41. yc = yc:float():div(255)
  42. xc = xc:float():div(255)
  43. return xc, yc
  44. else
  45. local samples = {}
  46. local sum_mse = 0
  47. for i = 1, tries do
  48. local xi = torch.random(offset, y:size(3) - (size + offset + 1))
  49. local yi = torch.random(offset, y:size(2) - (size + offset + 1))
  50. local xc = image.crop(x, xi, yi, xi + size, yi + size):float():div(255)
  51. local yc = image.crop(y, xi, yi, xi + size, yi + size):float():div(255)
  52. local mse = (xc - yc):pow(2):mean()
  53. sum_mse = sum_mse + mse
  54. table.insert(samples, {xc = xc, yc = yc, mse = mse})
  55. end
  56. if sum_mse > 0 then
  57. table.sort(samples,
  58. function (a, b)
  59. return a.mse > b.mse
  60. end)
  61. end
  62. return samples[1].xc, samples[1].yc
  63. end
  64. end
  65. local function color_noise(src)
  66. local p = 0.1
  67. src = src:float():div(255)
  68. local src_t = src:reshape(src:size(1), src:nElement() / src:size(1)):t():contiguous()
  69. local ce, cv = pcacov(src_t)
  70. local color_scale = torch.Tensor(3):uniform(1 / (1 + p), 1 + p)
  71. pca_space = torch.mm(src_t, cv):t():contiguous()
  72. for i = 1, 3 do
  73. pca_space[i]:mul(color_scale[i])
  74. end
  75. x = torch.mm(pca_space:t(), cv:t()):t():contiguous():resizeAs(src)
  76. x[torch.lt(x, 0.0)] = 0.0
  77. x[torch.gt(x, 1.0)] = 1.0
  78. return x:mul(255):byte()
  79. end
  80. local function shift_1px(src)
  81. -- reducing the even/odd issue in nearest neighbor.
  82. local r = torch.random(1, 4)
  83. end
  84. local function flip_augment(x, y)
  85. local flip = torch.random(1, 4)
  86. if y then
  87. if flip == 1 then
  88. x = image.hflip(x)
  89. y = image.hflip(y)
  90. elseif flip == 2 then
  91. x = image.vflip(x)
  92. y = image.vflip(y)
  93. elseif flip == 3 then
  94. x = image.hflip(image.vflip(x))
  95. y = image.hflip(image.vflip(y))
  96. elseif flip == 4 then
  97. end
  98. return x, y
  99. else
  100. if flip == 1 then
  101. x = image.hflip(x)
  102. elseif flip == 2 then
  103. x = image.vflip(x)
  104. elseif flip == 3 then
  105. x = image.hflip(image.vflip(x))
  106. elseif flip == 4 then
  107. end
  108. return x
  109. end
  110. end
  111. local function overlay_augment(src, p)
  112. p = p or 0.25
  113. if torch.uniform() > (1.0 - p) then
  114. local r = torch.uniform(0.2, 0.8)
  115. local t = "float"
  116. if src:type() == "torch.ByteTensor" then
  117. src = src:float():div(255)
  118. t = "byte"
  119. end
  120. local flip = flip_augment(src)
  121. flip:mul(r):add(src * (1.0 - r))
  122. if t == "byte" then
  123. flip = flip:mul(255):byte()
  124. end
  125. return flip
  126. else
  127. return src
  128. end
  129. end
  130. local function data_augment(y, options)
  131. y = flip_augment(y)
  132. if options.color_noise then
  133. y = color_noise(y)
  134. end
  135. if options.overlay then
  136. y = overlay_augment(y)
  137. end
  138. return y
  139. end
  140. local INTERPOLATION_PADDING = 16
  141. function pairwise_transform.scale(src, scale, size, offset, n, options)
  142. local filters = {
  143. "Box","Box", -- 0.012756949974688
  144. "Blackman", -- 0.013191924552285
  145. --"Cartom", -- 0.013753536746706
  146. --"Hanning", -- 0.013761314529647
  147. --"Hermite", -- 0.013850225205266
  148. "SincFast", -- 0.014095824314306
  149. --"Jinc", -- 0.014244299255442
  150. }
  151. if options.random_half then
  152. src = random_half(src)
  153. end
  154. local downscale_filter = filters[torch.random(1, #filters)]
  155. local y = data_augment(crop_if_large(src, math.max(size * 4, 512)), options)
  156. local down_scale = 1.0 / scale
  157. local x = iproc.scale(iproc.scale(y, y:size(3) * down_scale,
  158. y:size(2) * down_scale, downscale_filter),
  159. y:size(3), y:size(2))
  160. local batch = {}
  161. for i = 1, n do
  162. local xc, yc = active_cropping(x, y,
  163. size,
  164. INTERPOLATION_PADDING,
  165. options.active_cropping_rate,
  166. options.active_cropping_tries)
  167. if options.rgb then
  168. else
  169. yc = image.rgb2yuv(yc)[1]:reshape(1, yc:size(2), yc:size(3))
  170. xc = image.rgb2yuv(xc)[1]:reshape(1, xc:size(2), xc:size(3))
  171. end
  172. table.insert(batch, {xc, image.crop(yc, offset, offset, size - offset, size - offset)})
  173. end
  174. return batch
  175. end
  176. function pairwise_transform.jpeg_(src, quality, size, offset, n, options)
  177. local y = data_augment(crop_if_large(src, math.max(size * 4, 512)), options)
  178. local x = y
  179. for i = 1, #quality do
  180. x = gm.Image(x, "RGB", "DHW")
  181. x:format("jpeg")
  182. if options.jpeg_sampling_factors == 444 then
  183. x:samplingFactors({1.0, 1.0, 1.0})
  184. else -- 420
  185. x:samplingFactors({2.0, 1.0, 1.0})
  186. end
  187. local blob, len = x:toBlob(quality[i])
  188. x:fromBlob(blob, len)
  189. x = x:toTensor("byte", "RGB", "DHW")
  190. end
  191. local batch = {}
  192. for i = 1, n do
  193. local xc, yc = active_cropping(x, y, size, 0,
  194. options.active_cropping_rate,
  195. options.active_cropping_tries)
  196. xc, yc = flip_augment(xc, yc)
  197. if options.rgb then
  198. else
  199. yc = image.rgb2yuv(yc)[1]:reshape(1, yc:size(2), yc:size(3))
  200. xc = image.rgb2yuv(xc)[1]:reshape(1, xc:size(2), xc:size(3))
  201. end
  202. table.insert(batch, {xc, image.crop(yc, offset, offset, size - offset, size - offset)})
  203. end
  204. return batch
  205. end
  206. function pairwise_transform.jpeg(src, category, level, size, offset, n, options)
  207. if category == "anime_style_art" then
  208. if level == 1 then
  209. if torch.uniform() > 0.8 then
  210. return pairwise_transform.jpeg_(src, {},
  211. size, offset, n, options)
  212. else
  213. return pairwise_transform.jpeg_(src, {torch.random(65, 85)},
  214. size, offset, n, options)
  215. end
  216. elseif level == 2 then
  217. local r = torch.uniform()
  218. if torch.uniform() > 0.8 then
  219. return pairwise_transform.jpeg_(src, {},
  220. size, offset, n, options)
  221. else
  222. if r > 0.6 then
  223. return pairwise_transform.jpeg_(src, {torch.random(27, 70)},
  224. size, offset, n, options)
  225. elseif r > 0.3 then
  226. local quality1 = torch.random(37, 70)
  227. local quality2 = quality1 - torch.random(5, 10)
  228. return pairwise_transform.jpeg_(src, {quality1, quality2},
  229. size, offset, n, options)
  230. else
  231. local quality1 = torch.random(52, 70)
  232. local quality2 = quality1 - torch.random(5, 15)
  233. local quality3 = quality1 - torch.random(15, 25)
  234. return pairwise_transform.jpeg_(src,
  235. {quality1, quality2, quality3},
  236. size, offset, n, options)
  237. end
  238. end
  239. else
  240. error("unknown noise level: " .. level)
  241. end
  242. elseif category == "photo" then
  243. if level == 1 then
  244. if torch.uniform() > 0.7 then
  245. return pairwise_transform.jpeg_(src, {},
  246. size, offset, n,
  247. options)
  248. else
  249. return pairwise_transform.jpeg_(src, {torch.random(80, 95)},
  250. size, offset, n,
  251. options)
  252. end
  253. elseif level == 2 then
  254. if torch.uniform() > 0.7 then
  255. return pairwise_transform.jpeg_(src, {},
  256. size, offset, n,
  257. options)
  258. else
  259. return pairwise_transform.jpeg_(src, {torch.random(65, 85)},
  260. size, offset, n,
  261. options)
  262. end
  263. else
  264. error("unknown noise level: " .. level)
  265. end
  266. else
  267. error("unknown category: " .. category)
  268. end
  269. end
  270. function pairwise_transform.jpeg_scale_(src, scale, quality, size, offset, options)
  271. if options.random_half then
  272. src = random_half(src)
  273. end
  274. src = crop_if_large(src, math.max(size * 4, 512))
  275. local down_scale = 1.0 / scale
  276. local filters = {
  277. "Box", -- 0.012756949974688
  278. "Blackman", -- 0.013191924552285
  279. --"Cartom", -- 0.013753536746706
  280. --"Hanning", -- 0.013761314529647
  281. --"Hermite", -- 0.013850225205266
  282. "SincFast", -- 0.014095824314306
  283. "Jinc", -- 0.014244299255442
  284. }
  285. local downscale_filter = filters[torch.random(1, #filters)]
  286. local yi = torch.random(INTERPOLATION_PADDING, src:size(2) - size - INTERPOLATION_PADDING)
  287. local xi = torch.random(INTERPOLATION_PADDING, src:size(3) - size - INTERPOLATION_PADDING)
  288. local y = src
  289. local x
  290. if options.color_noise then
  291. y = color_noise(y)
  292. end
  293. if options.overlay then
  294. y = overlay_augment(y)
  295. end
  296. x = y
  297. x = iproc.scale(x, y:size(3) * down_scale, y:size(2) * down_scale, downscale_filter)
  298. for i = 1, #quality do
  299. x = gm.Image(x, "RGB", "DHW")
  300. x:format("jpeg")
  301. if options.jpeg_sampling_factors == 444 then
  302. x:samplingFactors({1.0, 1.0, 1.0})
  303. else -- 422
  304. x:samplingFactors({2.0, 1.0, 1.0})
  305. end
  306. local blob, len = x:toBlob(quality[i])
  307. x:fromBlob(blob, len)
  308. x = x:toTensor("byte", "RGB", "DHW")
  309. end
  310. x = iproc.scale(x, y:size(3), y:size(2))
  311. y = image.crop(y,
  312. xi, yi,
  313. xi + size, yi + size)
  314. x = image.crop(x,
  315. xi, yi,
  316. xi + size, yi + size)
  317. x = x:float():div(255)
  318. y = y:float():div(255)
  319. x, y = flip_augment(x, y)
  320. if options.rgb then
  321. else
  322. y = image.rgb2yuv(y)[1]:reshape(1, y:size(2), y:size(3))
  323. x = image.rgb2yuv(x)[1]:reshape(1, x:size(2), x:size(3))
  324. end
  325. return x, image.crop(y, offset, offset, size - offset, size - offset)
  326. end
  327. function pairwise_transform.jpeg_scale(src, scale, category, level, size, offset, options)
  328. options = options or {color_noise = false, random_half = true}
  329. if category == "anime_style_art" then
  330. if level == 1 then
  331. if torch.uniform() > 0.7 then
  332. return pairwise_transform.jpeg_scale_(src, scale, {},
  333. size, offset, options)
  334. else
  335. return pairwise_transform.jpeg_scale_(src, scale, {torch.random(65, 85)},
  336. size, offset, options)
  337. end
  338. elseif level == 2 then
  339. if torch.uniform() > 0.7 then
  340. return pairwise_transform.jpeg_scale_(src, scale, {},
  341. size, offset, options)
  342. else
  343. local r = torch.uniform()
  344. if r > 0.6 then
  345. return pairwise_transform.jpeg_scale_(src, scale, {torch.random(27, 70)},
  346. size, offset, options)
  347. elseif r > 0.3 then
  348. local quality1 = torch.random(37, 70)
  349. local quality2 = quality1 - torch.random(5, 10)
  350. return pairwise_transform.jpeg_scale_(src, scale, {quality1, quality2},
  351. size, offset, options)
  352. else
  353. local quality1 = torch.random(52, 70)
  354. local quality2 = quality1 - torch.random(5, 15)
  355. local quality3 = quality1 - torch.random(15, 25)
  356. return pairwise_transform.jpeg_scale_(src, scale,
  357. {quality1, quality2, quality3 },
  358. size, offset, options)
  359. end
  360. end
  361. else
  362. error("unknown noise level: " .. level)
  363. end
  364. elseif category == "photo" then
  365. if level == 1 then
  366. if torch.uniform() > 0.7 then
  367. return pairwise_transform.jpeg_scale_(src, scale, {},
  368. size, offset, options)
  369. else
  370. return pairwise_transform.jpeg_scale_(src, scale, {torch.random(80, 95)},
  371. size, offset, options)
  372. end
  373. elseif level == 2 then
  374. return pairwise_transform.jpeg_scale_(src, scale, {torch.random(70, 85)},
  375. size, offset, options)
  376. else
  377. error("unknown noise level: " .. level)
  378. end
  379. else
  380. error("unknown category: " .. category)
  381. end
  382. end
  383. local function test_jpeg()
  384. local loader = require './image_loader'
  385. local src = loader.load_byte("../images/miku_CC_BY-NC.jpg")
  386. for i = 2, 9 do
  387. local xy = pairwise_transform.jpeg_(random_half(src),
  388. {i * 10}, 128, 0, 2, {color_noise = false, random_half = true, overlay = true, rgb = true})
  389. for i = 1, #xy do
  390. image.display({image = xy[i][1], legend = "y:" .. (i * 10), max=1,min=0})
  391. image.display({image = xy[i][2], legend = "x:" .. (i * 10),max=1,min=0})
  392. end
  393. --print(x:mean(), y:mean())
  394. end
  395. end
  396. local function test_scale()
  397. torch.setdefaulttensortype('torch.FloatTensor')
  398. local loader = require './image_loader'
  399. local src = loader.load_byte("../images/miku_CC_BY-NC.jpg")
  400. local options = {color_noise = true,
  401. random_half = true,
  402. overlay = false,
  403. active_cropping_rate = 1.5,
  404. active_cropping_tries = 10,
  405. rgb = true
  406. }
  407. for i = 1, 9 do
  408. local xy = pairwise_transform.scale(src, 2.0, 128, 7, 1, options)
  409. image.display({image = xy[1][1], legend = "y:" .. (i * 10), min = 0, max = 1})
  410. image.display({image = xy[1][2], legend = "x:" .. (i * 10), min = 0, max = 1})
  411. print(xy[1][1]:size(), xy[1][2]:size())
  412. --print(x:mean(), y:mean())
  413. end
  414. end
  415. local function test_jpeg_scale()
  416. torch.setdefaulttensortype('torch.FloatTensor')
  417. local loader = require './image_loader'
  418. local src = loader.load_byte("../images/miku_CC_BY-NC.jpg")
  419. local options = {color_noise = true,
  420. random_half = true,
  421. overlay = true,
  422. active_cropping_ratio = 0.5,
  423. active_cropping_times = 10
  424. }
  425. for i = 1, 9 do
  426. local y, x = pairwise_transform.jpeg_scale(src, 2.0, 1, 128, 7, options)
  427. image.display({image = y, legend = "y1:" .. (i * 10), min = 0, max = 1})
  428. image.display({image = x, legend = "x1:" .. (i * 10), min = 0, max = 1})
  429. print(y:size(), x:size())
  430. --print(x:mean(), y:mean())
  431. end
  432. for i = 1, 9 do
  433. local y, x = pairwise_transform.jpeg_scale(src, 2.0, 2, 128, 7, options)
  434. image.display({image = y, legend = "y2:" .. (i * 10), min = 0, max = 1})
  435. image.display({image = x, legend = "x2:" .. (i * 10), min = 0, max = 1})
  436. print(y:size(), x:size())
  437. --print(x:mean(), y:mean())
  438. end
  439. end
  440. local function test_color_noise()
  441. torch.setdefaulttensortype('torch.FloatTensor')
  442. local loader = require './image_loader'
  443. local src = loader.load_byte("../images/miku_CC_BY-NC.jpg")
  444. for i = 1, 10 do
  445. image.display(color_noise(src))
  446. end
  447. end
  448. local function test_overlay()
  449. torch.setdefaulttensortype('torch.FloatTensor')
  450. local loader = require './image_loader'
  451. local src = loader.load_byte("../images/miku_CC_BY-NC.jpg")
  452. for i = 1, 10 do
  453. image.display(overlay_augment(src, 1.0))
  454. end
  455. end
  456. --test_scale()
  457. --test_jpeg()
  458. --test_jpeg_scale()
  459. --test_color_noise()
  460. --test_overlay()
  461. return pairwise_transform