train.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. local test = image_loader.load_float(settings.test)
  67. local adam_config = {
  68. learningRate = settings.learning_rate,
  69. xBatchSize = settings.batch_size,
  70. }
  71. local ch = nil
  72. if settings.color == "y" then
  73. ch = 1
  74. elseif settings.color == "rgb" then
  75. ch = 3
  76. end
  77. local transformer = function(x, is_validation)
  78. if is_validation == nil then is_validation = false end
  79. local color_noise = (not is_validation) and settings.color_noise
  80. if settings.method == "scale" then
  81. return pairwise_transform.scale(x,
  82. settings.scale,
  83. settings.crop_size, offset,
  84. { color_noise = color_noise,
  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.category,
  91. settings.noise_level,
  92. settings.crop_size, offset,
  93. { color_noise = color_noise,
  94. random_half = settings.random_half,
  95. rgb = (settings.color == "rgb")
  96. })
  97. elseif settings.method == "noise_scale" then
  98. return pairwise_transform.jpeg_scale(x,
  99. settings.scale,
  100. settings.category,
  101. settings.noise_level,
  102. settings.crop_size, offset,
  103. { color_noise = color_noise,
  104. random_half = settings.random_half,
  105. rgb = (settings.color == "rgb")
  106. })
  107. end
  108. end
  109. local best_score = 100000.0
  110. print("# make validation-set")
  111. local valid_xy = make_validation_set(valid_x, transformer, settings.validation_crops)
  112. valid_x = nil
  113. collectgarbage()
  114. model:cuda()
  115. print("load .. " .. #train_x)
  116. for epoch = 1, settings.epoch do
  117. model:training()
  118. print("# " .. epoch)
  119. print(minibatch_adam(model, criterion, train_x, adam_config,
  120. transformer,
  121. {ch, settings.crop_size, settings.crop_size},
  122. {ch, settings.crop_size - offset * 2, settings.crop_size - offset * 2}
  123. ))
  124. model:evaluate()
  125. print("# validation")
  126. local score = validate(model, criterion, valid_xy)
  127. if score < best_score then
  128. lrd_count = 0
  129. best_score = score
  130. print("* update best model")
  131. torch.save(settings.model_file, model)
  132. if settings.method == "noise" then
  133. local log = path.join(settings.model_dir,
  134. ("noise%d_best.png"):format(settings.noise_level))
  135. save_test_jpeg(model, test, log)
  136. elseif settings.method == "scale" then
  137. local log = path.join(settings.model_dir,
  138. ("scale%.1f_best.png"):format(settings.scale))
  139. save_test_scale(model, test, log)
  140. elseif settings.method == "noise_scale" then
  141. local log = path.join(settings.model_dir,
  142. ("noise%d_scale%.1f_best.png"):format(settings.noise_level,
  143. settings.scale))
  144. save_test_scale(model, test, log)
  145. end
  146. else
  147. lrd_count = lrd_count + 1
  148. if lrd_count > 5 then
  149. lrd_count = 0
  150. adam_config.learningRate = adam_config.learningRate * 0.9
  151. print("* learning rate decay: " .. adam_config.learningRate)
  152. end
  153. end
  154. print("current: " .. score .. ", best: " .. best_score)
  155. collectgarbage()
  156. end
  157. end
  158. torch.manualSeed(settings.seed)
  159. cutorch.manualSeed(settings.seed)
  160. print(settings)
  161. train()