train.lua 21 KB

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