train.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. require './lib/portable'
  2. require 'optim'
  3. require 'xlua'
  4. require 'pl'
  5. local settings = require './lib/settings'
  6. local minibatch_adam = require './lib/minibatch_adam'
  7. local iproc = require './lib/iproc'
  8. local reconstruct = require './lib/reconstruct'
  9. local pairwise_transform = require './lib/pairwise_transform'
  10. local image_loader = require './lib/image_loader'
  11. local function save_test_scale(model, rgb, file)
  12. local up = reconstruct.scale(model, settings.scale, rgb, settings.block_offset)
  13. image.save(file, up)
  14. end
  15. local function save_test_jpeg(model, rgb, file)
  16. local im, count = reconstruct.image(model, rgb, settings.block_offset)
  17. image.save(file, im)
  18. end
  19. local function split_data(x, test_size)
  20. local index = torch.randperm(#x)
  21. local train_size = #x - test_size
  22. local train_x = {}
  23. local valid_x = {}
  24. for i = 1, train_size do
  25. train_x[i] = x[index[i]]
  26. end
  27. for i = 1, test_size do
  28. valid_x[i] = x[index[train_size + i]]
  29. end
  30. return train_x, valid_x
  31. end
  32. local function make_validation_set(x, transformer, n)
  33. n = n or 4
  34. local data = {}
  35. for i = 1, #x do
  36. for k = 1, n do
  37. local x, y = transformer(x[i], true)
  38. table.insert(data, {x = x:reshape(1, x:size(1), x:size(2), x:size(3)),
  39. y = y:reshape(1, y:size(1), y:size(2), y:size(3))})
  40. end
  41. xlua.progress(i, #x)
  42. collectgarbage()
  43. end
  44. return data
  45. end
  46. local function validate(model, criterion, data)
  47. local loss = 0
  48. for i = 1, #data do
  49. local z = model:forward(data[i].x:cuda())
  50. loss = loss + criterion:forward(z, data[i].y:cuda())
  51. xlua.progress(i, #data)
  52. if i % 10 == 0 then
  53. collectgarbage()
  54. end
  55. end
  56. return loss / #data
  57. end
  58. local function train()
  59. local model, offset = settings.create_model(settings.color)
  60. assert(offset == settings.block_offset)
  61. local criterion = nn.MSECriterion():cuda()
  62. local x = torch.load(settings.images)
  63. local lrd_count = 0
  64. local train_x, valid_x = split_data(x,
  65. math.floor(settings.validation_ratio * #x),
  66. settings.validation_crops)
  67. local test = image_loader.load_float(settings.test)
  68. local adam_config = {
  69. learningRate = settings.learning_rate,
  70. xBatchSize = settings.batch_size,
  71. }
  72. local ch = nil
  73. if settings.color == "y" then
  74. ch = 1
  75. elseif settings.color == "rgb" then
  76. ch = 3
  77. end
  78. local transformer = function(x, is_validation)
  79. if is_validation == nil then is_validation = false end
  80. if settings.method == "scale" then
  81. return pairwise_transform.scale(x,
  82. settings.scale,
  83. settings.crop_size, offset,
  84. { color_augment = not is_validation,
  85. random_half = settings.random_half,
  86. rgb = (settings.color == "rgb")
  87. })
  88. elseif settings.method == "noise" then
  89. return pairwise_transform.jpeg(x,
  90. settings.noise_level,
  91. settings.crop_size, offset,
  92. { color_augment = not is_validation,
  93. random_half = settings.random_half,
  94. rgb = (settings.color == "rgb")
  95. })
  96. elseif settings.method == "noise_scale" then
  97. return pairwise_transform.jpeg_scale(x,
  98. settings.scale,
  99. settings.noise_level,
  100. settings.crop_size, offset,
  101. { color_augment = not is_validation,
  102. random_half = settings.random_half,
  103. rgb = (settings.color == "rgb")
  104. })
  105. end
  106. end
  107. local best_score = 100000.0
  108. print("# make validation-set")
  109. local valid_xy = make_validation_set(valid_x, transformer, 20)
  110. valid_x = nil
  111. collectgarbage()
  112. model:cuda()
  113. print("load .. " .. #train_x)
  114. for epoch = 1, settings.epoch do
  115. model:training()
  116. print("# " .. epoch)
  117. print(minibatch_adam(model, criterion, train_x, adam_config,
  118. transformer,
  119. {ch, settings.crop_size, settings.crop_size},
  120. {ch, settings.crop_size - offset * 2, settings.crop_size - offset * 2}
  121. ))
  122. model:evaluate()
  123. print("# validation")
  124. local score = validate(model, criterion, valid_xy)
  125. if score < best_score then
  126. lrd_count = 0
  127. best_score = score
  128. print("* update best model")
  129. torch.save(settings.model_file, model)
  130. if settings.method == "noise" then
  131. local log = path.join(settings.model_dir,
  132. ("noise%d_best.png"):format(settings.noise_level))
  133. save_test_jpeg(model, test, log)
  134. elseif settings.method == "scale" then
  135. local log = path.join(settings.model_dir,
  136. ("scale%.1f_best.png"):format(settings.scale))
  137. save_test_scale(model, test, log)
  138. elseif settings.method == "noise_scale" then
  139. local log = path.join(settings.model_dir,
  140. ("noise%d_scale%.1f_best.png"):format(settings.noise_level,
  141. settings.scale))
  142. save_test_scale(model, test, log)
  143. end
  144. else
  145. lrd_count = lrd_count + 1
  146. if lrd_count > 5 then
  147. lrd_count = 0
  148. adam_config.learningRate = adam_config.learningRate * 0.8
  149. print("* learning rate decay: " .. adam_config.learningRate)
  150. end
  151. end
  152. print("current: " .. score .. ", best: " .. best_score)
  153. collectgarbage()
  154. end
  155. end
  156. torch.manualSeed(settings.seed)
  157. cutorch.manualSeed(settings.seed)
  158. print(settings)
  159. train()