convert_data.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. require 'pl'
  2. local __FILE__ = (function() return string.gsub(debug.getinfo(2, 'S').source, "^@", "") end)()
  3. package.path = path.join(path.dirname(__FILE__), "lib", "?.lua;") .. package.path
  4. require 'image'
  5. local cjson = require 'cjson'
  6. local csvigo = require 'csvigo'
  7. local compression = require 'compression'
  8. local settings = require 'settings'
  9. local image_loader = require 'image_loader'
  10. local iproc = require 'iproc'
  11. local alpha_util = require 'alpha_util'
  12. local function crop_if_large(src, max_size)
  13. if max_size < 0 then
  14. return src
  15. end
  16. local tries = 4
  17. if src:size(2) >= max_size and src:size(3) >= max_size then
  18. local rect
  19. for i = 1, tries do
  20. local yi = torch.random(0, src:size(2) - max_size)
  21. local xi = torch.random(0, src:size(3) - max_size)
  22. rect = iproc.crop(src, xi, yi, xi + max_size, yi + max_size)
  23. -- ignore simple background
  24. if rect:float():std() >= 0 then
  25. break
  26. end
  27. end
  28. return rect
  29. else
  30. return src
  31. end
  32. end
  33. local function crop_if_large_pair(x, y, max_size)
  34. if max_size < 0 then
  35. return x, y
  36. end
  37. local scale_y = y:size(2) / x:size(2)
  38. local mod = 4
  39. assert(x:size(3) == (y:size(3) / scale_y))
  40. local tries = 4
  41. if y:size(2) > max_size and y:size(3) > max_size then
  42. assert(max_size % 4 == 0)
  43. local rect_x, rect_y
  44. for i = 1, tries do
  45. local yi = torch.random(0, y:size(2) - max_size)
  46. local xi = torch.random(0, y:size(3) - max_size)
  47. if mod then
  48. yi = yi - (yi % mod)
  49. xi = xi - (xi % mod)
  50. end
  51. rect_y = iproc.crop(y, xi, yi, xi + max_size, yi + max_size)
  52. rect_x = iproc.crop(y, xi / scale_y, yi / scale_y, xi / scale_y + max_size / scale_y, yi / scale_y + max_size / scale_y)
  53. -- ignore simple background
  54. if rect_y:float():std() >= 0 then
  55. break
  56. end
  57. end
  58. return rect_x, rect_y
  59. else
  60. return x, y
  61. end
  62. end
  63. local function padding_x(x, pad, x_zero)
  64. if pad > 0 then
  65. if x_zero then
  66. x = iproc.zero_padding(x, pad, pad, pad, pad)
  67. else
  68. x = iproc.padding(x, pad, pad, pad, pad)
  69. end
  70. end
  71. return x
  72. end
  73. local function padding_xy(x, y, pad, x_zero, y_zero)
  74. local scale = y:size(2) / x:size(2)
  75. if pad > 0 then
  76. if x_zero then
  77. x = iproc.zero_padding(x, pad, pad, pad, pad)
  78. else
  79. x = iproc.padding(x, pad, pad, pad, pad)
  80. end
  81. if y_zero then
  82. y = iproc.zero_padding(y, pad * scale, pad * scale, pad * scale, pad * scale)
  83. else
  84. y = iproc.padding(y, pad * scale, pad * scale, pad * scale, pad * scale)
  85. end
  86. end
  87. return x, y
  88. end
  89. local function load_images(list)
  90. local MARGIN = 32
  91. local csv = csvigo.load({path = list, verbose = false, mode = "raw"})
  92. local x = {}
  93. local skip_notice = false
  94. for i = 1, #csv do
  95. local filters = nil
  96. local filename = csv[i][1]
  97. local csv_meta = csv[i][2]
  98. if csv_meta and csv_meta:len() > 0 then
  99. csv_meta = cjson.decode(csv_meta)
  100. end
  101. if csv_meta and csv_meta.filters then
  102. filters = csv_meta.filters
  103. end
  104. local basename_y = path.basename(filename)
  105. local im, meta = image_loader.load_byte(filename)
  106. local skip = false
  107. local alpha_color = torch.random(0, 1)
  108. if im then
  109. if meta and meta.alpha then
  110. if settings.use_transparent_png then
  111. im = alpha_util.fill(im, meta.alpha, alpha_color)
  112. else
  113. skip = true
  114. end
  115. end
  116. if skip then
  117. if not skip_notice then
  118. io.stderr:write("skip transparent png (settings.use_transparent_png=0)\n")
  119. skip_notice = true
  120. end
  121. else
  122. if csv_meta and csv_meta.x then
  123. -- method == user
  124. local yy = im
  125. local xx, meta2 = image_loader.load_byte(csv_meta.x)
  126. if settings.invert_x then
  127. xx = (-(xx:long()) + 255):byte()
  128. end
  129. if xx then
  130. if meta2 and meta2.alpha then
  131. xx = alpha_util.fill(xx, meta2.alpha, alpha_color)
  132. end
  133. xx, yy = crop_if_large_pair(xx, yy, settings.max_training_image_size)
  134. xx, yy = padding_xy(xx, yy, settings.padding, settings.padding_x_zero, settings.padding_y_zero)
  135. if settings.grayscale then
  136. xx = iproc.rgb2y(xx)
  137. yy = iproc.rgb2y(yy)
  138. end
  139. table.insert(x, {{y = compression.compress(yy), x = compression.compress(xx)},
  140. {data = {filters = filters, has_x = true, basename = basename_y}}})
  141. else
  142. io.stderr:write(string.format("\n%s: skip: load error.\n", csv_meta.x))
  143. end
  144. else
  145. im = crop_if_large(im, settings.max_training_image_size)
  146. im = iproc.crop_mod4(im)
  147. im = padding_x(im, settings.padding, settings.padding_x_zero)
  148. local scale = 1.0
  149. if settings.random_half_rate > 0.0 then
  150. scale = 2.0
  151. end
  152. if im:size(2) > (settings.crop_size * scale + MARGIN) and im:size(3) > (settings.crop_size * scale + MARGIN) then
  153. if settings.grayscale then
  154. im = iproc.rgb2y(im)
  155. end
  156. table.insert(x, {compression.compress(im), {data = {filters = filters, basename = basename_y}}})
  157. else
  158. io.stderr:write(string.format("\n%s: skip: image is too small (%d > size).\n", filename, settings.crop_size * scale + MARGIN))
  159. end
  160. end
  161. end
  162. else
  163. io.stderr:write(string.format("\n%s: skip: load error.\n", filename))
  164. end
  165. xlua.progress(i, #csv)
  166. if i % 10 == 0 then
  167. collectgarbage()
  168. end
  169. end
  170. return x
  171. end
  172. torch.manualSeed(settings.seed)
  173. print(settings)
  174. local x = load_images(settings.image_list)
  175. torch.save(settings.images, x)