def _validate_(self): with torch.no_grad(): print ("val ,"*8,"warm start...",len(self.dt_val_warm)) iter_start_time = time.time() ssim = [] batch_loss = [] psnr = [] cub_ssim = [] cub_batch_loss = [] cub_psnr = [] save_image_list_1 = [] for ii,data in tqdm.tqdm( enumerate(self.dt_val_warm) ): if len(data)>3: input_lr ,input_hr , cubic_hr,_,_ =data else : input_lr ,input_hr , cubic_hr =data self. input_lr = input_lr .to(self.device) self. input_hr = input_hr .to(self.device) self. input_cubic_hr = cubic_hr .to(self.device) self.forward() save_image_list_1.append(torch.cat( [self.input_cubic_hr ,\ self.output_hr ,\ self.input_hr ],dim=3) ) loss = self.critic_pixel(self.output_hr, self.input_hr) batch_loss.append(loss.item()) ssim.append(image_quality.msssim(self.output_hr, self.input_hr).item()) psnr.append(image_quality.psnr( self.output_hr, self.input_hr ).item()) cub_loss = self.critic_pixel( self.input_cubic_hr , self.input_hr) cub_batch_loss.append(cub_loss.item()) cub_ssim.append(image_quality.msssim(self.input_cubic_hr, self.input_hr).item()) cub_psnr.append(image_quality.psnr( self.input_cubic_hr, self.input_hr ).item()) np.random.shuffle(save_image_list_1) save_image_list= save_image_list_1[:8] save_image_list = util.tensor2im( torch.cat(save_image_list,dim=2) ) save_image_list = OrderedDict([ ("cub_out_gt", save_image_list )] ) self.visualizer.display_current_results(save_image_list,self.epoch, save_result=True, offset=20,title="val_imag") val_info = ( np.mean(batch_loss),np.mean(ssim),np.mean(psnr) ,np.mean(cub_batch_loss) , np.mean(cub_ssim) ,np.mean(cub_psnr)) errors = dict(zip ( ("loss","ssim","psnr","cub_loss","cub_ssim","cub_psnr") , val_info ) ) t = (time.time() - iter_start_time) self.visualizer.print_current_errors(self.epoch , self.epoch, errors, t,log_name="loss_log_val.txt") self.visualizer.plot_current_errors(self.epoch , self.epoch,opt=None,errors=errors,display_id_offset=3,loss_name="val") return val_info
DCSCN_12 = "model_check_points/DCSCN/DCSCN_model_387epos_L12_noise_1.pt" model_dcscn = torch.load(DCSCN_12) model_upconv7 = UpConv_7() model_upconv7.load_pre_train_weights("model_check_points/Upconv_7/anime/noise0_scale2.0x_model.json") img_dataset = ImageData(img_folder='demo/demo_imgs/', max_patch_per_img=1000, patch_size=96, shrink_size=2, noise_level=1, down_sample_method=Image.BICUBIC, color_mod='RGB') img_data = ImageLoader(img_dataset, up_sample=None, batch_size=1, shuffle=True, pad_img=model_upconv7.offset) # DCSCN must set pad_img = 0 ssim_score = [] psnr_score = [] for img in tqdm(img_data, ascii=True): lr, hr = img out = model_upconv7.forward_checkpoint(lr) psnr_score.append(image_quality.calc_psnr(out, hr)) ssim_score.append(image_quality.msssim(out, hr)) print("Averge PSNR score: {:.4f}".format(np.mean(psnr_score))) print("Average MS-SSIM score: {:.4f}".format(np.mean(ssim_score)))
batch_loss = [] insample_ssim = 0 for index, batch in enumerate(img_data): lr_img, hr_img = batch model.zero_grad() outputs = model.forward(lr_img) loss = criteria(outputs, hr_img) loss.backward() nn.utils.clip_grad_norm_(model.parameters(), 5) optimizer.step() counter += 1 all_loss.append(loss.item()) batch_loss.append(loss.item()) ssim = image_quality.msssim(outputs, hr_img) ibar.set_postfix(ratio=index / total_batch, loss=loss.item(), ssim=ssim, batch=index) insample_ssim += ssim one_ite_loss = np.mean(batch_loss) print("One iteration loss {:.5f}".format(one_ite_loss)) # eval ssim = 0 for test_batch in test_data: lr_img, hr_img lr_img_up = model.forward_checkpoint(lr_img) ssim += image_quality.msssim(lr_img_up, hr_img)
hr_img = hr_img.cuda() # model.zero_grad() optimizer.zero_grad() outputs = model.forward(lr_img) outputs = outputs.float() loss = criteria(outputs, hr_img) # loss.backward() optimizer.backward(loss) # nn.utils.clip_grad_norm_(model.parameters(), 5) optimizer.step() counter += 1 # train_loss.append(loss.item()) ssim = image_quality.msssim(outputs, hr_img).item() psnr = image_quality.psnr(outputs, hr_img).item() ibar.set_postfix(ratio=index / total_batch, loss=loss.item(), ssim=ssim, batch=index, psnr=psnr, lr=scheduler.current_lr) train_loss.append(loss.item()) train_ssim.append(ssim) train_psnr.append(psnr) # +++++++++++++++++++++++++++++++++++++ # save checkpoints by iterations # -------------------------------------