train.lua 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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 'image'
  7. require 'w2nn'
  8. local threads = require 'threads'
  9. local settings = require 'settings'
  10. local srcnn = require 'srcnn'
  11. local minibatch_adam = require 'minibatch_adam'
  12. local iproc = require 'iproc'
  13. local reconstruct = require 'reconstruct'
  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 save_test_user(model, rgb, file)
  24. if settings.scale == 1 then
  25. save_test_jpeg(model, rgb, file)
  26. else
  27. save_test_scale(model, rgb, file)
  28. end
  29. end
  30. local function split_data(x, test_size)
  31. local index = torch.randperm(#x)
  32. local train_size = #x - test_size
  33. local train_x = {}
  34. local valid_x = {}
  35. for i = 1, train_size do
  36. train_x[i] = x[index[i]]
  37. end
  38. for i = 1, test_size do
  39. valid_x[i] = x[index[train_size + i]]
  40. end
  41. return train_x, valid_x
  42. end
  43. local g_transform_pool = nil
  44. local g_mutex = nil
  45. local g_mutex_id = nil
  46. local function transform_pool_init(has_resize, offset)
  47. local nthread = torch.getnumthreads()
  48. if (settings.thread > 0) then
  49. nthread = settings.thread
  50. end
  51. g_mutex = threads.Mutex()
  52. g_mutex_id = g_mutex:id()
  53. g_transform_pool = threads.Threads(
  54. nthread,
  55. threads.safe(
  56. function(threadid)
  57. require 'pl'
  58. local __FILE__ = (function() return string.gsub(debug.getinfo(2, 'S').source, "^@", "") end)()
  59. package.path = path.join(path.dirname(__FILE__), "lib", "?.lua;") .. package.path
  60. require 'torch'
  61. require 'nn'
  62. require 'cunn'
  63. torch.setnumthreads(1)
  64. torch.setdefaulttensortype("torch.FloatTensor")
  65. local threads = require 'threads'
  66. local compression = require 'compression'
  67. local pairwise_transform = require 'pairwise_transform'
  68. function transformer(x, is_validation, n)
  69. local mutex = threads.Mutex(g_mutex_id)
  70. local meta = {data = {}}
  71. local y = nil
  72. if type(x) == "table" and type(x[2]) == "table" then
  73. meta = x[2]
  74. if x[1].x and x[1].y then
  75. y = compression.decompress(x[1].y)
  76. x = compression.decompress(x[1].x)
  77. else
  78. x = compression.decompress(x[1])
  79. end
  80. else
  81. x = compression.decompress(x)
  82. end
  83. n = n or settings.patches
  84. if is_validation == nil then is_validation = false end
  85. local random_color_noise_rate = nil
  86. local random_overlay_rate = nil
  87. local active_cropping_rate = nil
  88. local active_cropping_tries = nil
  89. if is_validation then
  90. active_cropping_rate = settings.active_cropping_rate
  91. active_cropping_tries = settings.active_cropping_tries
  92. random_color_noise_rate = 0.0
  93. random_overlay_rate = 0.0
  94. else
  95. active_cropping_rate = settings.active_cropping_rate
  96. active_cropping_tries = settings.active_cropping_tries
  97. random_color_noise_rate = settings.random_color_noise_rate
  98. random_overlay_rate = settings.random_overlay_rate
  99. end
  100. if settings.method == "scale" then
  101. local conf = tablex.update({
  102. mutex = mutex,
  103. downsampling_filters = settings.downsampling_filters,
  104. random_half_rate = settings.random_half_rate,
  105. random_color_noise_rate = random_color_noise_rate,
  106. random_overlay_rate = random_overlay_rate,
  107. random_unsharp_mask_rate = settings.random_unsharp_mask_rate,
  108. random_blur_rate = settings.random_blur_rate,
  109. random_blur_size = settings.random_blur_size,
  110. random_blur_sigma_min = settings.random_blur_sigma_min,
  111. random_blur_sigma_max = settings.random_blur_sigma_max,
  112. max_size = settings.max_size,
  113. active_cropping_rate = active_cropping_rate,
  114. active_cropping_tries = active_cropping_tries,
  115. rgb = (settings.color == "rgb"),
  116. x_upsampling = not has_resize,
  117. resize_blur_min = settings.resize_blur_min,
  118. resize_blur_max = settings.resize_blur_max}, meta)
  119. return pairwise_transform.scale(x,
  120. settings.scale,
  121. settings.crop_size, offset,
  122. n, conf)
  123. elseif settings.method == "noise" then
  124. local conf = tablex.update({
  125. mutex = mutex,
  126. random_half_rate = settings.random_half_rate,
  127. random_color_noise_rate = random_color_noise_rate,
  128. random_overlay_rate = random_overlay_rate,
  129. random_unsharp_mask_rate = settings.random_unsharp_mask_rate,
  130. random_blur_rate = settings.random_blur_rate,
  131. random_blur_size = settings.random_blur_size,
  132. random_blur_sigma_min = settings.random_blur_sigma_min,
  133. random_blur_sigma_max = settings.random_blur_sigma_max,
  134. max_size = settings.max_size,
  135. jpeg_chroma_subsampling_rate = settings.jpeg_chroma_subsampling_rate,
  136. active_cropping_rate = active_cropping_rate,
  137. active_cropping_tries = active_cropping_tries,
  138. nr_rate = settings.nr_rate,
  139. rgb = (settings.color == "rgb")}, meta)
  140. return pairwise_transform.jpeg(x,
  141. settings.style,
  142. settings.noise_level,
  143. settings.crop_size, offset,
  144. n, conf)
  145. elseif settings.method == "noise_scale" then
  146. local conf = tablex.update({
  147. mutex = mutex,
  148. downsampling_filters = settings.downsampling_filters,
  149. random_half_rate = settings.random_half_rate,
  150. random_color_noise_rate = random_color_noise_rate,
  151. random_overlay_rate = random_overlay_rate,
  152. random_unsharp_mask_rate = settings.random_unsharp_mask_rate,
  153. random_blur_rate = settings.random_blur_rate,
  154. random_blur_size = settings.random_blur_size,
  155. random_blur_sigma_min = settings.random_blur_sigma_min,
  156. random_blur_sigma_max = settings.random_blur_sigma_max,
  157. max_size = settings.max_size,
  158. jpeg_chroma_subsampling_rate = settings.jpeg_chroma_subsampling_rate,
  159. nr_rate = settings.nr_rate,
  160. active_cropping_rate = active_cropping_rate,
  161. active_cropping_tries = active_cropping_tries,
  162. rgb = (settings.color == "rgb"),
  163. x_upsampling = not has_resize,
  164. resize_blur_min = settings.resize_blur_min,
  165. resize_blur_max = settings.resize_blur_max}, meta)
  166. return pairwise_transform.jpeg_scale(x,
  167. settings.scale,
  168. settings.style,
  169. settings.noise_level,
  170. settings.crop_size, offset,
  171. n, conf)
  172. elseif settings.method == "user" then
  173. if is_validation == nil then is_validation = false end
  174. local rotate_rate = nil
  175. local scale_rate = nil
  176. local negate_rate = nil
  177. local negate_x_rate = nil
  178. if is_validation then
  179. rotate_rate = 0
  180. scale_rate = 0
  181. negate_rate = 0
  182. negate_x_rate = 0
  183. else
  184. rotate_rate = settings.random_pairwise_rotate_rate
  185. scale_rate = settings.random_pairwise_scale_rate
  186. negate_rate = settings.random_pairwise_negate_rate
  187. negate_x_rate = settings.random_pairwise_negate_x_rate
  188. end
  189. local conf = tablex.update({
  190. gcn = settings.gcn,
  191. max_size = settings.max_size,
  192. active_cropping_rate = active_cropping_rate,
  193. active_cropping_tries = active_cropping_tries,
  194. random_pairwise_rotate_rate = rotate_rate,
  195. random_pairwise_rotate_min = settings.random_pairwise_rotate_min,
  196. random_pairwise_rotate_max = settings.random_pairwise_rotate_max,
  197. random_pairwise_scale_rate = scale_rate,
  198. random_pairwise_scale_min = settings.random_pairwise_scale_min,
  199. random_pairwise_scale_max = settings.random_pairwise_scale_max,
  200. random_pairwise_negate_rate = negate_rate,
  201. random_pairwise_negate_x_rate = negate_x_rate,
  202. pairwise_y_binary = settings.pairwise_y_binary,
  203. pairwise_flip = settings.pairwise_flip,
  204. rgb = (settings.color == "rgb")}, meta)
  205. return pairwise_transform.user(x, y,
  206. settings.crop_size, offset,
  207. n, conf)
  208. end
  209. end
  210. end)
  211. )
  212. g_transform_pool:synchronize()
  213. end
  214. local function make_validation_set(x, n, patches)
  215. local nthread = torch.getnumthreads()
  216. if (settings.thread > 0) then
  217. nthread = settings.thread
  218. end
  219. n = n or 4
  220. local validation_patches = math.min(16, patches or 16)
  221. local data = {}
  222. g_transform_pool:synchronize()
  223. torch.setnumthreads(1) -- 1
  224. for i = 1, #x do
  225. for k = 1, math.max(n / validation_patches, 1) do
  226. local input = x[i]
  227. g_transform_pool:addjob(
  228. function()
  229. local xy = transformer(input, true, validation_patches)
  230. return xy
  231. end,
  232. function(xy)
  233. for j = 1, #xy do
  234. table.insert(data, {x = xy[j][1], y = xy[j][2]})
  235. end
  236. end
  237. )
  238. end
  239. if i % 20 == 0 then
  240. collectgarbage()
  241. g_transform_pool:synchronize()
  242. xlua.progress(i, #x)
  243. end
  244. end
  245. g_transform_pool:synchronize()
  246. torch.setnumthreads(nthread) -- revert
  247. local new_data = {}
  248. local perm = torch.randperm(#data)
  249. for i = 1, perm:size(1) do
  250. new_data[i] = data[perm[i]]
  251. end
  252. data = new_data
  253. return data
  254. end
  255. local function validate(model, criterion, eval_metric, data, batch_size)
  256. local loss = 0
  257. local mse = 0
  258. local loss_count = 0
  259. local inputs_tmp = torch.Tensor(batch_size,
  260. data[1].x:size(1),
  261. data[1].x:size(2),
  262. data[1].x:size(3)):zero()
  263. local targets_tmp = torch.Tensor(batch_size,
  264. data[1].y:size(1),
  265. data[1].y:size(2),
  266. data[1].y:size(3)):zero()
  267. local inputs = inputs_tmp:clone():cuda()
  268. local targets = targets_tmp:clone():cuda()
  269. for t = 1, #data, batch_size do
  270. if t + batch_size -1 > #data then
  271. break
  272. end
  273. for i = 1, batch_size do
  274. inputs_tmp[i]:copy(data[t + i - 1].x)
  275. targets_tmp[i]:copy(data[t + i - 1].y)
  276. end
  277. inputs:copy(inputs_tmp)
  278. targets:copy(targets_tmp)
  279. local z = model:forward(inputs)
  280. loss = loss + criterion:forward(z, targets)
  281. mse = mse + eval_metric:forward(z, targets)
  282. loss_count = loss_count + 1
  283. if loss_count % 10 == 0 then
  284. xlua.progress(t, #data)
  285. collectgarbage()
  286. end
  287. end
  288. xlua.progress(#data, #data)
  289. return {loss = loss / loss_count, MSE = mse / loss_count, PSNR = 10 * math.log10(1 / (mse / loss_count))}
  290. end
  291. local function create_criterion(model)
  292. if settings.loss == "huber" then
  293. if reconstruct.is_rgb(model) then
  294. local offset = reconstruct.offset_size(model)
  295. local output_w = settings.crop_size - offset * 2
  296. local weight = torch.Tensor(3, output_w * output_w)
  297. weight[1]:fill(0.29891 * 3) -- R
  298. weight[2]:fill(0.58661 * 3) -- G
  299. weight[3]:fill(0.11448 * 3) -- B
  300. return w2nn.ClippedWeightedHuberCriterion(weight, 0.1, {0.0, 1.0}):cuda()
  301. else
  302. local offset = reconstruct.offset_size(model)
  303. local output_w = settings.crop_size - offset * 2
  304. local weight = torch.Tensor(1, output_w * output_w)
  305. weight[1]:fill(1.0)
  306. return w2nn.ClippedWeightedHuberCriterion(weight, 0.1, {0.0, 1.0}):cuda()
  307. end
  308. elseif settings.loss == "l1" then
  309. return w2nn.L1Criterion():cuda()
  310. else
  311. error("unsupported loss .." .. settings.loss)
  312. end
  313. end
  314. local function resampling(x, y, train_x)
  315. local c = 1
  316. local shuffle = torch.randperm(#train_x)
  317. local nthread = torch.getnumthreads()
  318. if (settings.thread > 0) then
  319. nthread = settings.thread
  320. end
  321. torch.setnumthreads(1) -- 1
  322. for t = 1, #train_x do
  323. local input = train_x[shuffle[t]]
  324. g_transform_pool:addjob(
  325. function()
  326. local xy = transformer(input, false, settings.patches)
  327. return xy
  328. end,
  329. function(xy)
  330. for i = 1, #xy do
  331. if c <= x:size(1) then
  332. x[c]:copy(xy[i][1])
  333. y[c]:copy(xy[i][2])
  334. c = c + 1
  335. else
  336. break
  337. end
  338. end
  339. end
  340. )
  341. if t % 50 == 0 then
  342. collectgarbage()
  343. g_transform_pool:synchronize()
  344. xlua.progress(t, #train_x)
  345. end
  346. if c > x:size(1) then
  347. break
  348. end
  349. end
  350. g_transform_pool:synchronize()
  351. xlua.progress(#train_x, #train_x)
  352. torch.setnumthreads(nthread) -- revert
  353. end
  354. local function get_oracle_data(x, y, instance_loss, k, samples)
  355. local index = torch.LongTensor(instance_loss:size(1))
  356. local dummy = torch.Tensor(instance_loss:size(1))
  357. torch.topk(dummy, index, instance_loss, k, 1, true)
  358. print("MSE of all data: " ..instance_loss:mean() .. ", MSE of oracle data: " .. dummy:mean())
  359. local shuffle = torch.randperm(k)
  360. local x_s = x:size()
  361. local y_s = y:size()
  362. x_s[1] = samples
  363. y_s[1] = samples
  364. local oracle_x = torch.Tensor(table.unpack(torch.totable(x_s)))
  365. local oracle_y = torch.Tensor(table.unpack(torch.totable(y_s)))
  366. for i = 1, samples do
  367. oracle_x[i]:copy(x[index[shuffle[i]]])
  368. oracle_y[i]:copy(y[index[shuffle[i]]])
  369. end
  370. return oracle_x, oracle_y
  371. end
  372. local function remove_small_image(x)
  373. local compression = require 'compression'
  374. local new_x = {}
  375. for i = 1, #x do
  376. local xe, meta, x_s
  377. xe = x[i]
  378. if type(x) == "table" and type(x[2]) == "table" then
  379. if xe[1].x and xe[1].y then
  380. x_s = compression.size(xe[1].y) -- y size
  381. else
  382. x_s = compression.size(xe[1])
  383. end
  384. else
  385. x_s = compression.size(xe)
  386. end
  387. if x_s[2] / settings.scale > settings.crop_size + 32 and
  388. x_s[3] / settings.scale > settings.crop_size + 32 then
  389. table.insert(new_x, x[i])
  390. end
  391. if i % 100 == 0 then
  392. collectgarbage()
  393. end
  394. end
  395. print(string.format("%d small images are removed", #x - #new_x))
  396. return new_x
  397. end
  398. local function plot(train, valid)
  399. gnuplot.plot({
  400. {'training', torch.Tensor(train), '-'},
  401. {'validation', torch.Tensor(valid), '-'}})
  402. end
  403. local function train()
  404. local x = remove_small_image(torch.load(settings.images))
  405. local train_x, valid_x = split_data(x, math.max(math.floor(settings.validation_rate * #x), 1))
  406. local hist_train = {}
  407. local hist_valid = {}
  408. local model
  409. if settings.resume:len() > 0 then
  410. model = torch.load(settings.resume, "ascii")
  411. else
  412. model = srcnn.create(settings.model, settings.backend, settings.color)
  413. end
  414. if model.w2nn_input_size then
  415. if settings.crop_size ~= model.w2nn_input_size then
  416. io.stderr:write(string.format("warning: crop_size is replaced with %d\n",
  417. model.w2nn_input_size))
  418. settings.crop_size = model.w2nn_input_size
  419. end
  420. end
  421. if model.w2nn_gcn then
  422. settings.gcn = true
  423. else
  424. settings.gcn = false
  425. end
  426. dir.makepath(settings.model_dir)
  427. local offset = reconstruct.offset_size(model)
  428. transform_pool_init(reconstruct.has_resize(model), offset)
  429. local criterion = create_criterion(model)
  430. local eval_metric = w2nn.ClippedMSECriterion(0, 1):cuda()
  431. local adam_config = {
  432. xLearningRate = settings.learning_rate,
  433. xBatchSize = settings.batch_size,
  434. xLearningRateDecay = settings.learning_rate_decay,
  435. xInstanceLoss = (settings.oracle_rate > 0)
  436. }
  437. local ch = nil
  438. if settings.color == "y" then
  439. ch = 1
  440. elseif settings.color == "rgb" then
  441. ch = 3
  442. end
  443. local best_score = 1000.0
  444. print("# make validation-set")
  445. local valid_xy = make_validation_set(valid_x,
  446. settings.validation_crops,
  447. settings.patches)
  448. valid_x = nil
  449. collectgarbage()
  450. model:cuda()
  451. print("load .. " .. #train_x)
  452. local x = nil
  453. local y = torch.Tensor(settings.patches * #train_x,
  454. ch * (settings.crop_size - offset * 2) * (settings.crop_size - offset * 2)):zero()
  455. if reconstruct.has_resize(model) then
  456. x = torch.Tensor(settings.patches * #train_x,
  457. ch, settings.crop_size / settings.scale, settings.crop_size / settings.scale)
  458. else
  459. x = torch.Tensor(settings.patches * #train_x,
  460. ch, settings.crop_size, settings.crop_size)
  461. end
  462. local instance_loss = nil
  463. for epoch = 1, settings.epoch do
  464. model:training()
  465. print("# " .. epoch)
  466. if adam_config.learningRate then
  467. print("learning rate: " .. adam_config.learningRate)
  468. end
  469. print("## resampling")
  470. if instance_loss then
  471. -- active learning
  472. local oracle_k = math.min(x:size(1) * (settings.oracle_rate * (1 / (1 - settings.oracle_drop_rate))), x:size(1))
  473. local oracle_n = math.min(x:size(1) * settings.oracle_rate, x:size(1))
  474. if oracle_n > 0 then
  475. local oracle_x, oracle_y = get_oracle_data(x, y, instance_loss, oracle_k, oracle_n)
  476. resampling(x:narrow(1, oracle_x:size(1) + 1, x:size(1)-oracle_x:size(1)),
  477. y:narrow(1, oracle_x:size(1) + 1, x:size(1) - oracle_x:size(1)), train_x)
  478. x:narrow(1, 1, oracle_x:size(1)):copy(oracle_x)
  479. y:narrow(1, 1, oracle_y:size(1)):copy(oracle_y)
  480. local draw_n = math.floor(math.sqrt(oracle_x:size(1), 0.5))
  481. if draw_n > 100 then
  482. draw_n = 100
  483. end
  484. image.save(path.join(settings.model_dir, "oracle_x.png"),
  485. image.toDisplayTensor({
  486. input = oracle_x:narrow(1, 1, draw_n * draw_n),
  487. padding = 2,
  488. nrow = draw_n,
  489. min = 0,
  490. max = 1}))
  491. else
  492. resampling(x, y, train_x)
  493. end
  494. else
  495. resampling(x, y, train_x, pairwise_func)
  496. end
  497. collectgarbage()
  498. instance_loss = torch.Tensor(x:size(1)):zero()
  499. for i = 1, settings.inner_epoch do
  500. model:training()
  501. local train_score, il = minibatch_adam(model, criterion, eval_metric, x, y, adam_config)
  502. instance_loss:copy(il)
  503. print(train_score)
  504. model:evaluate()
  505. print("# validation")
  506. local score = validate(model, criterion, eval_metric, valid_xy, adam_config.xBatchSize)
  507. table.insert(hist_train, train_score.loss)
  508. table.insert(hist_valid, score.loss)
  509. if settings.plot then
  510. plot(hist_train, hist_valid)
  511. end
  512. if score.loss < best_score then
  513. local test_image = image_loader.load_float(settings.test) -- reload
  514. best_score = score.loss
  515. print("* model has updated")
  516. if settings.save_history then
  517. torch.save(settings.model_file_best, model:clearState(), "ascii")
  518. torch.save(string.format(settings.model_file, epoch, i), model:clearState(), "ascii")
  519. if settings.method == "noise" then
  520. local log = path.join(settings.model_dir,
  521. ("noise%d_best.%d-%d.png"):format(settings.noise_level,
  522. epoch, i))
  523. save_test_jpeg(model, test_image, log)
  524. elseif settings.method == "scale" then
  525. local log = path.join(settings.model_dir,
  526. ("scale%.1f_best.%d-%d.png"):format(settings.scale,
  527. epoch, i))
  528. save_test_scale(model, test_image, log)
  529. elseif settings.method == "noise_scale" then
  530. local log = path.join(settings.model_dir,
  531. ("noise%d_scale%.1f_best.%d-%d.png"):format(settings.noise_level,
  532. settings.scale,
  533. epoch, i))
  534. save_test_scale(model, test_image, log)
  535. elseif settings.method == "user" then
  536. local log = path.join(settings.model_dir,
  537. ("%s_best.%d-%d.png"):format(settings.name,
  538. epoch, i))
  539. save_test_user(model, test_image, log)
  540. end
  541. else
  542. torch.save(settings.model_file, model:clearState(), "ascii")
  543. if settings.method == "noise" then
  544. local log = path.join(settings.model_dir,
  545. ("noise%d_best.png"):format(settings.noise_level))
  546. save_test_jpeg(model, test_image, log)
  547. elseif settings.method == "scale" then
  548. local log = path.join(settings.model_dir,
  549. ("scale%.1f_best.png"):format(settings.scale))
  550. save_test_scale(model, test_image, log)
  551. elseif settings.method == "noise_scale" then
  552. local log = path.join(settings.model_dir,
  553. ("noise%d_scale%.1f_best.png"):format(settings.noise_level,
  554. settings.scale))
  555. save_test_scale(model, test_image, log)
  556. elseif settings.method == "user" then
  557. local log = path.join(settings.model_dir,
  558. ("%s_best.png"):format(settings.name))
  559. save_test_user(model, test_image, log)
  560. end
  561. end
  562. end
  563. print("Batch-wise PSNR: " .. score.PSNR .. ", loss: " .. score.loss .. ", Minimum loss: " .. best_score .. ", MSE: " .. score.MSE)
  564. collectgarbage()
  565. end
  566. end
  567. end
  568. if settings.gpu > 0 then
  569. cutorch.setDevice(settings.gpu)
  570. end
  571. torch.manualSeed(settings.seed)
  572. cutorch.manualSeed(settings.seed)
  573. print(settings)
  574. train()