pairwise_transform.lua 15 KB

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