train.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 'optim'
  5. require 'xlua'
  6. require 'w2nn'
  7. local settings = require 'settings'
  8. local srcnn = require 'srcnn'
  9. local minibatch_adam = require 'minibatch_adam'
  10. local iproc = require 'iproc'
  11. local reconstruct = require 'reconstruct'
  12. local compression = require 'compression'
  13. local pairwise_transform = require 'pairwise_transform'
  14. local image_loader = require 'image_loader'
  15. local function save_test_scale(model, rgb, file)
  16. local up = reconstruct.scale(model, settings.scale, rgb)
  17. image.save(file, up)
  18. end
  19. local function save_test_jpeg(model, rgb, file)
  20. local im, count = reconstruct.image(model, rgb)
  21. image.save(file, im)
  22. end
  23. local function split_data(x, test_size)
  24. local index = torch.randperm(#x)
  25. local train_size = #x - test_size
  26. local train_x = {}
  27. local valid_x = {}
  28. for i = 1, train_size do
  29. train_x[i] = x[index[i]]
  30. end
  31. for i = 1, test_size do
  32. valid_x[i] = x[index[train_size + i]]
  33. end
  34. return train_x, valid_x
  35. end
  36. local function make_validation_set(x, transformer, n, patches)
  37. n = n or 4
  38. local validation_patches = math.min(16, patches or 16)
  39. local data = {}
  40. for i = 1, #x do
  41. for k = 1, math.max(n / validation_patches, 1) do
  42. local xy = transformer(x[i], true, validation_patches)
  43. for j = 1, #xy do
  44. table.insert(data, {x = xy[j][1], y = xy[j][2]})
  45. end
  46. end
  47. xlua.progress(i, #x)
  48. collectgarbage()
  49. end
  50. local new_data = {}
  51. local perm = torch.randperm(#data)
  52. for i = 1, perm:size(1) do
  53. new_data[i] = data[perm[i]]
  54. end
  55. data = new_data
  56. return data
  57. end
  58. local function validate(model, criterion, eval_metric, data, batch_size)
  59. local loss = 0
  60. local mse = 0
  61. local loss_count = 0
  62. local inputs_tmp = torch.Tensor(batch_size,
  63. data[1].x:size(1),
  64. data[1].x:size(2),
  65. data[1].x:size(3)):zero()
  66. local targets_tmp = torch.Tensor(batch_size,
  67. data[1].y:size(1),
  68. data[1].y:size(2),
  69. data[1].y:size(3)):zero()
  70. local inputs = inputs_tmp:clone():cuda()
  71. local targets = targets_tmp:clone():cuda()
  72. for t = 1, #data, batch_size do
  73. if t + batch_size -1 > #data then
  74. break
  75. end
  76. for i = 1, batch_size do
  77. inputs_tmp[i]:copy(data[t + i - 1].x)
  78. targets_tmp[i]:copy(data[t + i - 1].y)
  79. end
  80. inputs:copy(inputs_tmp)
  81. targets:copy(targets_tmp)
  82. local z = model:forward(inputs)
  83. loss = loss + criterion:forward(z, targets)
  84. mse = mse + eval_metric:forward(z, targets)
  85. loss_count = loss_count + 1
  86. if loss_count % 10 == 0 then
  87. xlua.progress(t, #data)
  88. collectgarbage()
  89. end
  90. end
  91. xlua.progress(#data, #data)
  92. return {loss = loss / loss_count, MSE = mse / loss_count, PSNR = 10 * math.log10(1 / (mse / loss_count))}
  93. end
  94. local function create_criterion(model, loss)
  95. if reconstruct.is_rgb(model) then
  96. local offset = reconstruct.offset_size(model)
  97. local output_w = settings.crop_size - offset * 2
  98. local weight = torch.Tensor(3, output_w * output_w)
  99. if loss == "y" then
  100. weight[1]:fill(0.29891 * 3) -- R
  101. weight[2]:fill(0.58661 * 3) -- G
  102. weight[3]:fill(0.11448 * 3) -- B
  103. else
  104. weight:fill(1)
  105. end
  106. return w2nn.ClippedWeightedHuberCriterion(weight, 0.1, {0.0, 1.0}):cuda()
  107. else
  108. local offset = reconstruct.offset_size(model)
  109. local output_w = settings.crop_size - offset * 2
  110. local weight = torch.Tensor(1, output_w * output_w)
  111. weight[1]:fill(1.0)
  112. return w2nn.ClippedWeightedHuberCriterion(weight, 0.1, {0.0, 1.0}):cuda()
  113. end
  114. end
  115. local function transformer(model, x, is_validation, n, offset)
  116. local meta = {data = {}}
  117. if type(x) == "table" and type(x[2]) == "table" then
  118. meta = x[2]
  119. x = compression.decompress(x[1])
  120. else
  121. x = compression.decompress(x)
  122. end
  123. n = n or settings.patches
  124. if is_validation == nil then is_validation = false end
  125. local random_color_noise_rate = nil
  126. local random_overlay_rate = nil
  127. local active_cropping_rate = nil
  128. local active_cropping_tries = nil
  129. if is_validation then
  130. active_cropping_rate = settings.active_cropping_rate
  131. active_cropping_tries = settings.active_cropping_tries
  132. random_color_noise_rate = 0.0
  133. random_overlay_rate = 0.0
  134. else
  135. active_cropping_rate = settings.active_cropping_rate
  136. active_cropping_tries = settings.active_cropping_tries
  137. random_color_noise_rate = settings.random_color_noise_rate
  138. random_overlay_rate = settings.random_overlay_rate
  139. end
  140. if settings.method == "scale" then
  141. local conf = tablex.update({
  142. downsampling_filters = settings.downsampling_filters,
  143. random_half_rate = settings.random_half_rate,
  144. random_color_noise_rate = random_color_noise_rate,
  145. random_overlay_rate = random_overlay_rate,
  146. random_unsharp_mask_rate = settings.random_unsharp_mask_rate,
  147. max_size = settings.max_size,
  148. active_cropping_rate = active_cropping_rate,
  149. active_cropping_tries = active_cropping_tries,
  150. rgb = (settings.color == "rgb"),
  151. x_upsampling = not reconstruct.has_resize(model),
  152. resize_blur_min = settings.resize_blur_min,
  153. resize_blur_max = settings.resize_blur_max}, meta)
  154. return pairwise_transform.scale(x,
  155. settings.scale,
  156. settings.crop_size, offset,
  157. n, conf)
  158. elseif settings.method == "noise" then
  159. local conf = tablex.update({
  160. random_half_rate = settings.random_half_rate,
  161. random_color_noise_rate = random_color_noise_rate,
  162. random_overlay_rate = random_overlay_rate,
  163. random_unsharp_mask_rate = settings.random_unsharp_mask_rate,
  164. max_size = settings.max_size,
  165. jpeg_chroma_subsampling_rate = settings.jpeg_chroma_subsampling_rate,
  166. active_cropping_rate = active_cropping_rate,
  167. active_cropping_tries = active_cropping_tries,
  168. nr_rate = settings.nr_rate,
  169. rgb = (settings.color == "rgb")}, meta)
  170. return pairwise_transform.jpeg(x,
  171. settings.style,
  172. settings.noise_level,
  173. settings.crop_size, offset,
  174. n, conf)
  175. elseif settings.method == "noise_scale" then
  176. local conf = tablex.update({
  177. downsampling_filters = settings.downsampling_filters,
  178. random_half_rate = settings.random_half_rate,
  179. random_color_noise_rate = random_color_noise_rate,
  180. random_overlay_rate = random_overlay_rate,
  181. random_unsharp_mask_rate = settings.random_unsharp_mask_rate,
  182. max_size = settings.max_size,
  183. jpeg_chroma_subsampling_rate = settings.jpeg_chroma_subsampling_rate,
  184. nr_rate = settings.nr_rate,
  185. active_cropping_rate = active_cropping_rate,
  186. active_cropping_tries = active_cropping_tries,
  187. rgb = (settings.color == "rgb"),
  188. x_upsampling = not reconstruct.has_resize(model),
  189. resize_blur_min = settings.resize_blur_min,
  190. resize_blur_max = settings.resize_blur_max}, meta)
  191. return pairwise_transform.jpeg_scale(x,
  192. settings.scale,
  193. settings.style,
  194. settings.noise_level,
  195. settings.crop_size, offset,
  196. n, conf)
  197. end
  198. end
  199. local function resampling(x, y, train_x, transformer, input_size, target_size)
  200. local c = 1
  201. local shuffle = torch.randperm(#train_x)
  202. for t = 1, #train_x do
  203. xlua.progress(t, #train_x)
  204. local xy = transformer(train_x[shuffle[t]], false, settings.patches)
  205. for i = 1, #xy do
  206. x[c]:copy(xy[i][1])
  207. y[c]:copy(xy[i][2])
  208. c = c + 1
  209. if c > x:size(1) then
  210. break
  211. end
  212. end
  213. if c > x:size(1) then
  214. break
  215. end
  216. if t % 50 == 0 then
  217. collectgarbage()
  218. end
  219. end
  220. xlua.progress(#train_x, #train_x)
  221. end
  222. local function get_oracle_data(x, y, instance_loss, k, samples)
  223. local index = torch.LongTensor(instance_loss:size(1))
  224. local dummy = torch.Tensor(instance_loss:size(1))
  225. torch.topk(dummy, index, instance_loss, k, 1, true)
  226. print("MSE of all data: " ..instance_loss:mean() .. ", MSE of oracle data: " .. dummy:mean())
  227. local shuffle = torch.randperm(k)
  228. local x_s = x:size()
  229. local y_s = y:size()
  230. x_s[1] = samples
  231. y_s[1] = samples
  232. local oracle_x = torch.Tensor(table.unpack(torch.totable(x_s)))
  233. local oracle_y = torch.Tensor(table.unpack(torch.totable(y_s)))
  234. for i = 1, samples do
  235. oracle_x[i]:copy(x[index[shuffle[i]]])
  236. oracle_y[i]:copy(y[index[shuffle[i]]])
  237. end
  238. return oracle_x, oracle_y
  239. end
  240. local function remove_small_image(x)
  241. local new_x = {}
  242. for i = 1, #x do
  243. local xe, meta, x_s
  244. xe = x[i]
  245. if type(xe) == "table" and type(xe[2]) == "table" then
  246. x_s = compression.size(xe[1])
  247. else
  248. x_s = compression.size(xe)
  249. end
  250. if x_s[2] / settings.scale > settings.crop_size + 32 and
  251. x_s[3] / settings.scale > settings.crop_size + 32 then
  252. table.insert(new_x, x[i])
  253. end
  254. if i % 100 == 0 then
  255. collectgarbage()
  256. end
  257. end
  258. print(string.format("%d small images are removed", #x - #new_x))
  259. return new_x
  260. end
  261. local function plot(train, valid)
  262. gnuplot.plot({
  263. {'training', torch.Tensor(train), '-'},
  264. {'validation', torch.Tensor(valid), '-'}})
  265. end
  266. local function train()
  267. local hist_train = {}
  268. local hist_valid = {}
  269. local model
  270. if settings.resume:len() > 0 then
  271. model = torch.load(settings.resume, "ascii")
  272. else
  273. model = srcnn.create(settings.model, settings.backend, settings.color)
  274. end
  275. local offset = reconstruct.offset_size(model)
  276. local pairwise_func = function(x, is_validation, n)
  277. return transformer(model, x, is_validation, n, offset)
  278. end
  279. local criterion = create_criterion(model, settings.loss)
  280. local eval_metric = w2nn.ClippedMSECriterion(0, 1):cuda()
  281. local x = remove_small_image(torch.load(settings.images))
  282. local train_x, valid_x = split_data(x, math.max(math.floor(settings.validation_rate * #x), 1))
  283. local adam_config = {
  284. xLearningRate = settings.learning_rate,
  285. xBatchSize = settings.batch_size,
  286. xLearningRateDecay = settings.learning_rate_decay
  287. }
  288. local ch = nil
  289. if settings.color == "y" then
  290. ch = 1
  291. elseif settings.color == "rgb" then
  292. ch = 3
  293. end
  294. local best_score = 1000.0
  295. print("# make validation-set")
  296. local valid_xy = make_validation_set(valid_x, pairwise_func,
  297. settings.validation_crops,
  298. settings.patches)
  299. valid_x = nil
  300. collectgarbage()
  301. model:cuda()
  302. print("load .. " .. #train_x)
  303. local x = nil
  304. local y = torch.Tensor(settings.patches * #train_x,
  305. ch * (settings.crop_size - offset * 2) * (settings.crop_size - offset * 2)):zero()
  306. if reconstruct.has_resize(model) then
  307. x = torch.Tensor(settings.patches * #train_x,
  308. ch, settings.crop_size / settings.scale, settings.crop_size / settings.scale)
  309. else
  310. x = torch.Tensor(settings.patches * #train_x,
  311. ch, settings.crop_size, settings.crop_size)
  312. end
  313. local instance_loss = nil
  314. for epoch = 1, settings.epoch do
  315. model:training()
  316. print("# " .. epoch)
  317. if adam_config.learningRate then
  318. print("learning rate: " .. adam_config.learningRate)
  319. end
  320. print("## resampling")
  321. if instance_loss then
  322. -- active learning
  323. local oracle_k = math.min(x:size(1) * (settings.oracle_rate * (1 / (1 - settings.oracle_drop_rate))), x:size(1))
  324. local oracle_n = math.min(x:size(1) * settings.oracle_rate, x:size(1))
  325. if oracle_n > 0 then
  326. local oracle_x, oracle_y = get_oracle_data(x, y, instance_loss, oracle_k, oracle_n)
  327. resampling(x:narrow(1, oracle_x:size(1) + 1, x:size(1)-oracle_x:size(1)),
  328. y:narrow(1, oracle_x:size(1) + 1, x:size(1) - oracle_x:size(1)), train_x, pairwise_func)
  329. x:narrow(1, 1, oracle_x:size(1)):copy(oracle_x)
  330. y:narrow(1, 1, oracle_y:size(1)):copy(oracle_y)
  331. local draw_n = math.floor(math.sqrt(oracle_x:size(1), 0.5))
  332. if draw_n > 100 then
  333. draw_n = 100
  334. end
  335. image.save(path.join(settings.model_dir, "oracle_x.png"),
  336. image.toDisplayTensor({
  337. input = oracle_x:narrow(1, 1, draw_n * draw_n),
  338. padding = 2,
  339. nrow = draw_n,
  340. min = 0,
  341. max = 1}))
  342. else
  343. resampling(x, y, train_x, pairwise_func)
  344. end
  345. else
  346. resampling(x, y, train_x, pairwise_func)
  347. end
  348. collectgarbage()
  349. instance_loss = torch.Tensor(x:size(1)):zero()
  350. for i = 1, settings.inner_epoch do
  351. model:training()
  352. local train_score, il = minibatch_adam(model, criterion, eval_metric, x, y, adam_config)
  353. instance_loss:copy(il)
  354. print(train_score)
  355. model:evaluate()
  356. print("# validation")
  357. local score = validate(model, criterion, eval_metric, valid_xy, adam_config.xBatchSize)
  358. table.insert(hist_train, train_score.loss)
  359. table.insert(hist_valid, score.loss)
  360. if settings.plot then
  361. plot(hist_train, hist_valid)
  362. end
  363. if score.MSE < best_score then
  364. local test_image = image_loader.load_float(settings.test) -- reload
  365. best_score = score.MSE
  366. print("* Best model is updated")
  367. if settings.save_history then
  368. torch.save(settings.model_file_best, model:clearState(), "ascii")
  369. torch.save(string.format(settings.model_file, epoch, i), model:clearState(), "ascii")
  370. if settings.method == "noise" then
  371. local log = path.join(settings.model_dir,
  372. ("noise%d_best.%d-%d.png"):format(settings.noise_level,
  373. epoch, i))
  374. save_test_jpeg(model, test_image, log)
  375. elseif settings.method == "scale" then
  376. local log = path.join(settings.model_dir,
  377. ("scale%.1f_best.%d-%d.png"):format(settings.scale,
  378. epoch, i))
  379. save_test_scale(model, test_image, log)
  380. elseif settings.method == "noise_scale" then
  381. local log = path.join(settings.model_dir,
  382. ("noise%d_scale%.1f_best.%d-%d.png"):format(settings.noise_level,
  383. settings.scale,
  384. epoch, i))
  385. save_test_scale(model, test_image, log)
  386. end
  387. else
  388. torch.save(settings.model_file, model:clearState(), "ascii")
  389. if settings.method == "noise" then
  390. local log = path.join(settings.model_dir,
  391. ("noise%d_best.png"):format(settings.noise_level))
  392. save_test_jpeg(model, test_image, log)
  393. elseif settings.method == "scale" then
  394. local log = path.join(settings.model_dir,
  395. ("scale%.1f_best.png"):format(settings.scale))
  396. save_test_scale(model, test_image, log)
  397. elseif settings.method == "noise_scale" then
  398. local log = path.join(settings.model_dir,
  399. ("noise%d_scale%.1f_best.png"):format(settings.noise_level,
  400. settings.scale))
  401. save_test_scale(model, test_image, log)
  402. end
  403. end
  404. end
  405. print("Batch-wise PSNR: " .. score.PSNR .. ", loss: " .. score.loss .. ", MSE: " .. score.MSE .. ", Minimum MSE: " .. best_score)
  406. collectgarbage()
  407. end
  408. end
  409. end
  410. if settings.gpu > 0 then
  411. cutorch.setDevice(settings.gpu)
  412. end
  413. torch.manualSeed(settings.seed)
  414. cutorch.manualSeed(settings.seed)
  415. print(settings)
  416. train()