train.lua 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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, 128, settings.upsampling_filter)
  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 data = {}
  39. for i = 1, #x do
  40. for k = 1, math.max(n / patches, 1) do
  41. local xy = transformer(x[i], true, patches)
  42. for j = 1, #xy do
  43. table.insert(data, {x = xy[j][1], y = xy[j][2]})
  44. end
  45. end
  46. xlua.progress(i, #x)
  47. collectgarbage()
  48. end
  49. local new_data = {}
  50. local perm = torch.randperm(#data)
  51. for i = 1, perm:size(1) do
  52. new_data[i] = data[perm[i]]
  53. end
  54. data = new_data
  55. return data
  56. end
  57. local function validate(model, criterion, data, batch_size)
  58. local loss = 0
  59. local loss_count = 0
  60. local inputs_tmp = torch.Tensor(batch_size,
  61. data[1].x:size(1),
  62. data[1].x:size(2),
  63. data[1].x:size(3)):zero()
  64. local targets_tmp = torch.Tensor(batch_size,
  65. data[1].y:size(1),
  66. data[1].y:size(2),
  67. data[1].y:size(3)):zero()
  68. local inputs = inputs_tmp:clone():cuda()
  69. local targets = targets_tmp:clone():cuda()
  70. for t = 1, #data, batch_size do
  71. if t + batch_size -1 > #data then
  72. break
  73. end
  74. for i = 1, batch_size do
  75. inputs_tmp[i]:copy(data[t + i - 1].x)
  76. targets_tmp[i]:copy(data[t + i - 1].y)
  77. end
  78. inputs:copy(inputs_tmp)
  79. targets:copy(targets_tmp)
  80. local z = model:forward(inputs)
  81. loss = loss + criterion:forward(z, targets)
  82. loss_count = loss_count + 1
  83. if loss_count % 10 == 0 then
  84. xlua.progress(t, #data)
  85. collectgarbage()
  86. end
  87. end
  88. xlua.progress(#data, #data)
  89. return loss / loss_count
  90. end
  91. local function create_criterion(model)
  92. if reconstruct.is_rgb(model) then
  93. local offset = reconstruct.offset_size(model)
  94. local output_w = settings.crop_size - offset * 2
  95. local weight = torch.Tensor(3, output_w * output_w)
  96. weight[1]:fill(0.29891 * 3) -- R
  97. weight[2]:fill(0.58661 * 3) -- G
  98. weight[3]:fill(0.11448 * 3) -- B
  99. return w2nn.ClippedWeightedHuberCriterion(weight, 0.1, {0.0, 1.0}):cuda()
  100. else
  101. local offset = reconstruct.offset_size(model)
  102. local output_w = settings.crop_size - offset * 2
  103. local weight = torch.Tensor(1, output_w * output_w)
  104. weight[1]:fill(1.0)
  105. return w2nn.ClippedWeightedHuberCriterion(weight, 0.1, {0.0, 1.0}):cuda()
  106. end
  107. end
  108. local function transformer(x, is_validation, n, offset)
  109. x = compression.decompress(x)
  110. n = n or settings.patches
  111. if is_validation == nil then is_validation = false end
  112. local random_color_noise_rate = nil
  113. local random_overlay_rate = nil
  114. local active_cropping_rate = nil
  115. local active_cropping_tries = nil
  116. if is_validation then
  117. active_cropping_rate = settings.active_cropping_rate
  118. active_cropping_tries = settings.active_cropping_tries
  119. random_color_noise_rate = 0.0
  120. random_overlay_rate = 0.0
  121. else
  122. active_cropping_rate = settings.active_cropping_rate
  123. active_cropping_tries = settings.active_cropping_tries
  124. random_color_noise_rate = settings.random_color_noise_rate
  125. random_overlay_rate = settings.random_overlay_rate
  126. end
  127. if settings.method == "scale" then
  128. return pairwise_transform.scale(x,
  129. settings.scale,
  130. settings.crop_size, offset,
  131. n,
  132. {
  133. downsampling_filters = settings.downsampling_filters,
  134. upsampling_filter = settings.upsampling_filter,
  135. random_half_rate = settings.random_half_rate,
  136. random_color_noise_rate = random_color_noise_rate,
  137. random_overlay_rate = random_overlay_rate,
  138. random_unsharp_mask_rate = settings.random_unsharp_mask_rate,
  139. max_size = settings.max_size,
  140. active_cropping_rate = active_cropping_rate,
  141. active_cropping_tries = active_cropping_tries,
  142. rgb = (settings.color == "rgb"),
  143. gamma_correction = settings.gamma_correction
  144. })
  145. elseif settings.method == "noise" then
  146. return pairwise_transform.jpeg(x,
  147. settings.style,
  148. settings.noise_level,
  149. settings.crop_size, offset,
  150. n,
  151. {
  152. random_half_rate = settings.random_half_rate,
  153. random_color_noise_rate = random_color_noise_rate,
  154. random_overlay_rate = random_overlay_rate,
  155. random_unsharp_mask_rate = settings.random_unsharp_mask_rate,
  156. max_size = settings.max_size,
  157. jpeg_chroma_subsampling_rate = settings.jpeg_chroma_subsampling_rate,
  158. active_cropping_rate = active_cropping_rate,
  159. active_cropping_tries = active_cropping_tries,
  160. nr_rate = settings.nr_rate,
  161. rgb = (settings.color == "rgb")
  162. })
  163. end
  164. end
  165. local function resampling(x, y, train_x, transformer, input_size, target_size)
  166. print("## resampling")
  167. for t = 1, #train_x do
  168. xlua.progress(t, #train_x)
  169. local xy = transformer(train_x[t], false, settings.patches)
  170. for i = 1, #xy do
  171. local index = (t - 1) * settings.patches + i
  172. x[index]:copy(xy[i][1])
  173. y[index]:copy(xy[i][2])
  174. end
  175. if t % 50 == 0 then
  176. collectgarbage()
  177. end
  178. end
  179. end
  180. local function plot(train, valid)
  181. gnuplot.plot({
  182. {'training', torch.Tensor(train), '-'},
  183. {'validation', torch.Tensor(valid), '-'}})
  184. end
  185. local function train()
  186. local hist_train = {}
  187. local hist_valid = {}
  188. local model = srcnn.create(settings.model, settings.backend, settings.color)
  189. local offset = reconstruct.offset_size(model)
  190. local pairwise_func = function(x, is_validation, n)
  191. return transformer(x, is_validation, n, offset)
  192. end
  193. local criterion = create_criterion(model)
  194. local eval_metric = nn.MSECriterion():cuda()
  195. local x = torch.load(settings.images)
  196. local train_x, valid_x = split_data(x, math.max(math.floor(settings.validation_rate * #x), 1))
  197. local adam_config = {
  198. learningRate = settings.learning_rate,
  199. xBatchSize = settings.batch_size,
  200. }
  201. local lrd_count = 0
  202. local ch = nil
  203. if settings.color == "y" then
  204. ch = 1
  205. elseif settings.color == "rgb" then
  206. ch = 3
  207. end
  208. local best_score = 1000.0
  209. print("# make validation-set")
  210. local valid_xy = make_validation_set(valid_x, pairwise_func,
  211. settings.validation_crops,
  212. settings.patches)
  213. valid_x = nil
  214. collectgarbage()
  215. model:cuda()
  216. print("load .. " .. #train_x)
  217. local x = torch.Tensor(settings.patches * #train_x,
  218. ch, settings.crop_size, settings.crop_size)
  219. local y = torch.Tensor(settings.patches * #train_x,
  220. ch * (settings.crop_size - offset * 2) * (settings.crop_size - offset * 2)):zero()
  221. for epoch = 1, settings.epoch do
  222. model:training()
  223. print("# " .. epoch)
  224. resampling(x, y, train_x, pairwise_func)
  225. for i = 1, settings.inner_epoch do
  226. local train_score = minibatch_adam(model, criterion, eval_metric, x, y, adam_config)
  227. print(train_score)
  228. model:evaluate()
  229. print("# validation")
  230. local score = validate(model, eval_metric, valid_xy, adam_config.xBatchSize)
  231. table.insert(hist_train, train_score.MSE)
  232. table.insert(hist_valid, score)
  233. if settings.plot then
  234. plot(hist_train, hist_valid)
  235. end
  236. if score < best_score then
  237. local test_image = image_loader.load_float(settings.test) -- reload
  238. lrd_count = 0
  239. best_score = score
  240. print("* update best model")
  241. if settings.save_history then
  242. torch.save(string.format(settings.model_file, epoch, i), model:clearState(), "ascii")
  243. if settings.method == "noise" then
  244. local log = path.join(settings.model_dir,
  245. ("noise%d_best.%d-%d.png"):format(settings.noise_level,
  246. epoch, i))
  247. save_test_jpeg(model, test_image, log)
  248. elseif settings.method == "scale" then
  249. local log = path.join(settings.model_dir,
  250. ("scale%.1f_best.%d-%d.png"):format(settings.scale,
  251. epoch, i))
  252. save_test_scale(model, test_image, log)
  253. end
  254. else
  255. torch.save(settings.model_file, model:clearState(), "ascii")
  256. if settings.method == "noise" then
  257. local log = path.join(settings.model_dir,
  258. ("noise%d_best.png"):format(settings.noise_level))
  259. save_test_jpeg(model, test_image, log)
  260. elseif settings.method == "scale" then
  261. local log = path.join(settings.model_dir,
  262. ("scale%.1f_best.png"):format(settings.scale))
  263. save_test_scale(model, test_image, log)
  264. end
  265. end
  266. else
  267. lrd_count = lrd_count + 1
  268. if lrd_count > 2 then
  269. adam_config.learningRate = adam_config.learningRate * 0.8
  270. print("* learning rate decay: " .. adam_config.learningRate)
  271. lrd_count = 0
  272. end
  273. end
  274. print("PSNR: " .. 10 * math.log10(1 / score) .. ", MSE: " .. score .. ", Best MSE: " .. best_score)
  275. collectgarbage()
  276. end
  277. end
  278. end
  279. if settings.gpu > 0 then
  280. cutorch.setDevice(settings.gpu)
  281. end
  282. torch.manualSeed(settings.seed)
  283. cutorch.manualSeed(settings.seed)
  284. print(settings)
  285. train()