2016-05-15 15:34:03 +12:00
|
|
|
require 'pl'
|
2015-10-28 19:30:47 +13:00
|
|
|
local __FILE__ = (function() return string.gsub(debug.getinfo(2, 'S').source, "^@", "") end)()
|
|
|
|
package.path = path.join(path.dirname(__FILE__), "lib", "?.lua;") .. package.path
|
|
|
|
|
2015-06-13 18:02:02 +12:00
|
|
|
require 'image'
|
2015-10-28 19:30:47 +13:00
|
|
|
local compression = require 'compression'
|
|
|
|
local settings = require 'settings'
|
|
|
|
local image_loader = require 'image_loader'
|
2015-10-30 02:44:15 +13:00
|
|
|
local iproc = require 'iproc'
|
2016-05-15 15:34:03 +12:00
|
|
|
local alpha_util = require 'alpha_util'
|
2015-06-13 18:02:02 +12:00
|
|
|
|
2016-04-11 02:06:39 +12:00
|
|
|
local function crop_if_large(src, max_size)
|
|
|
|
local tries = 4
|
|
|
|
if src:size(2) >= max_size and src:size(3) >= max_size then
|
|
|
|
local rect
|
|
|
|
for i = 1, tries do
|
|
|
|
local yi = torch.random(0, src:size(2) - max_size)
|
|
|
|
local xi = torch.random(0, src:size(3) - max_size)
|
|
|
|
rect = iproc.crop(src, xi, yi, xi + max_size, yi + max_size)
|
|
|
|
-- ignore simple background
|
|
|
|
if rect:float():std() >= 0 then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return rect
|
|
|
|
else
|
|
|
|
return src
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-17 17:42:53 +12:00
|
|
|
local function load_images(list)
|
2015-07-12 00:53:45 +12:00
|
|
|
local MARGIN = 32
|
2015-10-28 19:30:47 +13:00
|
|
|
local lines = utils.split(file.read(list), "\n")
|
2015-05-17 17:42:53 +12:00
|
|
|
local x = {}
|
2016-05-20 02:02:02 +12:00
|
|
|
local skip_notice = false
|
2015-10-28 19:30:47 +13:00
|
|
|
for i = 1, #lines do
|
|
|
|
local line = lines[i]
|
2016-06-06 17:04:13 +12:00
|
|
|
local v = utils.split(line, ",")
|
|
|
|
local filename = v[1]
|
|
|
|
local filters = v[2]
|
|
|
|
if filters then
|
|
|
|
filters = utils.split(filters, ":")
|
|
|
|
end
|
|
|
|
local im, meta = image_loader.load_byte(filename)
|
2016-05-20 02:02:02 +12:00
|
|
|
local skip = false
|
2016-04-15 12:13:37 +12:00
|
|
|
if meta and meta.alpha then
|
2016-05-20 02:02:02 +12:00
|
|
|
if settings.use_transparent_png then
|
|
|
|
im = alpha_util.fill(im, meta.alpha, torch.random(0, 1))
|
2015-10-26 13:23:52 +13:00
|
|
|
else
|
2016-05-20 02:02:02 +12:00
|
|
|
skip = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if skip then
|
|
|
|
if not skip_notice then
|
|
|
|
io.stderr:write("skip transparent png (settings.use_transparent_png=0)\n")
|
|
|
|
skip_notice = true
|
2015-10-26 13:23:52 +13:00
|
|
|
end
|
2016-05-15 15:34:03 +12:00
|
|
|
else
|
2016-05-20 02:02:02 +12:00
|
|
|
if settings.max_training_image_size > 0 then
|
|
|
|
im = crop_if_large(im, settings.max_training_image_size)
|
|
|
|
end
|
|
|
|
im = iproc.crop_mod4(im)
|
|
|
|
local scale = 1.0
|
|
|
|
if settings.random_half_rate > 0.0 then
|
|
|
|
scale = 2.0
|
|
|
|
end
|
|
|
|
if im then
|
|
|
|
if im:size(2) > (settings.crop_size * scale + MARGIN) and im:size(3) > (settings.crop_size * scale + MARGIN) then
|
2016-06-06 17:04:13 +12:00
|
|
|
table.insert(x, {compression.compress(im), {data = {filters = filters}}})
|
2016-05-20 02:02:02 +12:00
|
|
|
else
|
2016-06-06 17:04:13 +12:00
|
|
|
io.stderr:write(string.format("\n%s: skip: image is too small (%d > size).\n", filename, settings.crop_size * scale + MARGIN))
|
2016-05-20 02:02:02 +12:00
|
|
|
end
|
|
|
|
else
|
2016-06-06 17:04:13 +12:00
|
|
|
io.stderr:write(string.format("\n%s: skip: load error.\n", filename))
|
2016-05-20 02:02:02 +12:00
|
|
|
end
|
2015-05-17 17:42:53 +12:00
|
|
|
end
|
2015-10-28 19:30:47 +13:00
|
|
|
xlua.progress(i, #lines)
|
|
|
|
if i % 10 == 0 then
|
2015-05-17 17:42:53 +12:00
|
|
|
collectgarbage()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return x
|
|
|
|
end
|
2015-10-28 19:30:47 +13:00
|
|
|
|
2015-10-26 13:23:52 +13:00
|
|
|
torch.manualSeed(settings.seed)
|
2015-05-17 17:42:53 +12:00
|
|
|
print(settings)
|
|
|
|
local x = load_images(settings.image_list)
|
|
|
|
torch.save(settings.images, x)
|