How to use the torchvision.utils.save_image function in torchvision

To help you get started, we’ve selected a few torchvision examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github elvisyjlin / AttGAN-PyTorch / test_multi.py View on Github external
att_a = att_a.type(torch.float)
    att_b = att_a.clone()
    
    for a in args.test_atts:
        i = args.attrs.index(a)
        att_b[:, i] = 1 - att_b[:, i]
        att_b = check_attribute_conflict(att_b, args.attrs[i], args.attrs)

    with torch.no_grad():
        samples = [img_a]
        att_b_ = (att_b * 2 - 1) * args.thres_int
        for a, i in zip(args.test_atts, args.test_ints):
            att_b_[..., args.attrs.index(a)] = att_b_[..., args.attrs.index(a)] * i / args.thres_int
        samples.append(attgan.G(img_a, att_b_))
        samples = torch.cat(samples, dim=3)
        vutils.save_image(
            samples, output_path + '/{:06d}.jpg'.format(idx + 182638), 
            nrow=1, normalize=True, range=(-1., 1.)
        )
        print('{:06d}.jpg done!'.format(idx + 182638))
github rahulbhalley / cyclegan-qp / main.py View on Github external
# WARNING: Please do not change this code snippet with a closed mind. 🤪👻
    iteration = int(iteration / 1000)
    only_img_name = img_name.split('.')[0]
    img_type = img_name.split('.')[1]

    # Set up names
    out_sty_name = f"sty_{only_img_name}_{style}_{iteration}k.{img_type}"
    out_rec_name = f"rec_{only_img_name}_{style}_{iteration}k.{img_type}"
    
    # Set up paths
    sty_path = os.path.join(SAMPLE_DIR, style, out_sty_dir, out_sty_name)
    rec_path = os.path.join(SAMPLE_DIR, style, out_rec_dir, out_rec_name)
    
    # Save image grids
    vutils.save_image(sty_img, sty_path, normalize=True)
    vutils.save_image(rec_img, rec_path, normalize=True)
    
    # Status
    print(f"Saved {rec_path}")
    print(f"Saved {sty_path}")
github edgarriba / ali-pytorch / main.py View on Github external
def test(dataloader, epoch):
    real_cpu_first, _ = iter(dataloader).next()

    if opt.cuda:
        real_cpu_first = real_cpu_first.cuda()

    netGx.eval(), netGz.eval()  # switch to test mode
    latent = netGz(Variable(real_cpu_first, volatile=True))

    # removes last sigmoid activation to visualize reconstruction correctly
    mu, sigma = latent[:, :opt.nz], latent[:, opt.nz:].exp()
    # recon = nn.Sequential(*list(netGx.main.children())[:-1])(mu + sigma)
    recon = netGx(mu + sigma)

    vutils.save_image(recon.data, '{0}/reconstruction.png'.format(opt.experiment))
    vutils.save_image(real_cpu_first, '{0}/real_samples.png'.format(opt.experiment))
github vithursant / SPL-ADVisE / optimizers / spld.py View on Github external
correct += (pred == labels.data).sum()
            accuracy = correct / total

            start = batch_idx * images.size(0)
            stop = start + images.size(0)
            #pdb.set_trace()
            progress_bar.set_postfix(
                xentropy='%.3f' % (xentropy_loss_avg / (batch_idx + 1)),
                acc='%.3f' % accuracy)

            batch_builder.update_losses(batch_train_inds[start:stop],
                                    xentropy_loss_vector.squeeze(), 'spld')

            updates += 1

        torchvision.utils.save_image(images.data, 'spld_results/' + args.folder + '/' + args.dataset + '_spld_log_' + test_id + '.jpg', normalize=True)

        test_acc, test_loss = test(args, model, test_loader)
        tqdm.write('test_acc: %.3f' % (test_acc))

        row = {'epoch': str(updates), 'train_acc': str(accuracy), 'train_loss': str(xentropy_loss_avg / (updates)), 'test_acc': str(test_acc), 'test_loss': str(test_loss)}
        spld_logger.writerow(row)

        if args.en_scheduler:
            scheduler.step(updates)

        batch_train_inds = batch_builder.gen_batch_spl(spld_params[0], spld_params[1], args.batch_size)
        train_loader.sampler.batch_indices = batch_train_inds.astype(np.int32)

        # Increase the learning pace
        spld_params[0] *= (1+spld_params[2])
        spld_params[0] = int(round(spld_params[0]))
github Linardos / SalEMA / main.py View on Github external
optimizer.step()

                    accumulated_losses.append(loss.data)

            # Visualize some of the data
            if j == 5:
                print(saliency_map.max())
                print(saliency_map.min())
                print(gtruths[idx].max())
                print(gtruths[idx].min())

                #writer.add_image('Frame', clip[idx], n_iter)
                #writer.add_image('Gtruth', gtruths[idx], n_iter)


                utils.save_image(saliency_map, "./log/smap{}_epoch{}.png".format(i, epoch))

                if epoch == 1:
                    utils.save_image(gtruths[idx], "./log/gt{}.png".format(i))
                #writer.add_image('Prediction', prediction, n_iter)


        end = datetime.datetime.now().replace(microsecond=0)
        print('Epoch: {}\tVideo: {}\t Training Loss: {}\t Time elapsed: {}\t'.format(epoch, i, mean(accumulated_losses), end-start))
        video_losses.append(mean(accumulated_losses))

    return (mean(video_losses), n_iter, optimizer)
github sg-nm / Evolutionary-Autoencoders / Inpainitng / cnn_train.py View on Github external
else:
                    data_noise = self.half_mask(input_, gpuID)
                optimizer.zero_grad()
                try:
                    output = model(data_noise)
                except:
                    import traceback
                    traceback.print_exc()
                    return 0.
                loss = criterion(output, input_)
                train_loss += loss.data[0]
                loss.backward()
                optimizer.step()
                if ite == 100:
                    vutils.save_image(data_noise.data, './noise_samples%d.png' % gpuID, normalize=False)
                    vutils.save_image(input_.data, './org_samples%d.png' % gpuID, normalize=False)
                    vutils.save_image(output.data, './output%d.png' % gpuID, normalize=False)
                ite += 1
                if ite == 1000:
                    break
            print('Train set : Average loss: {:.4f}'.format(train_loss))
            print('time ', time.time()-start_time)
            if self.validation: # for evolution
                if epoch == epoch_num:
                    for module in model.children():
                        module.train(False)
                    t_loss = self.__test_per_std(model, criterion, gpuID, input, mask_type, mask)
            else: # for retrain the best model
                if epoch % 10 == 0:
                    for module in model.children():
                        module.train(False)
                    t_loss = self.__test_per_std(model, criterion, gpuID, input, mask_type, mask)
github AlexiaJM / RelativisticGAN / code / GAN_losses_iter.py View on Github external
if os.path.exists('%s/%01d/' % (param.extra_folder, current_set_images)):
			for root, dirs, files in os.walk('%s/%01d/' % (param.extra_folder, current_set_images)):
				for f in files:
					os.unlink(os.path.join(root, f))
		else:
			os.mkdir('%s/%01d/' % (param.extra_folder, current_set_images))

		# Generate 50k images for FID/Inception to be calculated later (not on this script, since running both tensorflow and pytorch at the same time cause issues)
		ext_curr = 0
		z_extra = torch.FloatTensor(100, param.z_size, 1, 1)
		if param.cuda:
			z_extra = z_extra.cuda()
		for ext in range(int(param.gen_extra_images/100)):
			fake_test = G(Variable(z_extra.normal_(0, 1)))
			for ext_i in range(100):
				vutils.save_image((fake_test[ext_i].data*.50)+.50, '%s/%01d/fake_samples_%05d.png' % (param.extra_folder, current_set_images,ext_curr), normalize=False, padding=0)
				ext_curr += 1
		del z_extra
		del fake_test
		# Later use this command to get FID of first set:
github samet-akcay / ganomaly / lib / model.py View on Github external
self.an_scores[i*self.opt.batchsize : i*self.opt.batchsize+error.size(0)] = error.reshape(error.size(0))
                self.gt_labels[i*self.opt.batchsize : i*self.opt.batchsize+error.size(0)] = self.gt.reshape(error.size(0))
                self.latent_i [i*self.opt.batchsize : i*self.opt.batchsize+error.size(0), :] = latent_i.reshape(error.size(0), self.opt.nz)
                self.latent_o [i*self.opt.batchsize : i*self.opt.batchsize+error.size(0), :] = latent_o.reshape(error.size(0), self.opt.nz)

                self.times.append(time_o - time_i)

                # Save test images.
                if self.opt.save_test_images:
                    dst = os.path.join(self.opt.outf, self.opt.name, 'test', 'images')
                    if not os.path.isdir(dst):
                        os.makedirs(dst)
                    real, fake, _ = self.get_current_images()
                    vutils.save_image(real, '%s/real_%03d.eps' % (dst, i+1), normalize=True)
                    vutils.save_image(fake, '%s/fake_%03d.eps' % (dst, i+1), normalize=True)

            # Measure inference time.
            self.times = np.array(self.times)
            self.times = np.mean(self.times[:100] * 1000)

            # Scale error vector between [0, 1]
            self.an_scores = (self.an_scores - torch.min(self.an_scores)) / (torch.max(self.an_scores) - torch.min(self.an_scores))
            # auc, eer = roc(self.gt_labels, self.an_scores)
            auc = evaluate(self.gt_labels, self.an_scores, metric=self.opt.metric)
            performance = OrderedDict([('Avg Run Time (ms/batch)', self.times), ('AUC', auc)])

            if self.opt.display_id > 0 and self.opt.phase == 'test':
                counter_ratio = float(epoch_iter) / len(self.dataloader['test'].dataset)
                self.visualizer.plot_performance(self.epoch, counter_ratio, performance)
            return performance
github BCV-Uniandes / SMIT / solver.py View on Github external
def show_img_single(self, img):  
    img_ = self.denorm(img.data.cpu())
    save_image(img_.cpu(), 'show/tmp0.jpg',nrow=int(math.sqrt(img_.size(0))), padding=0)
    os.system('eog show/tmp0.jpg')    
    os.remove('show/tmp0.jpg')
github HongguangZhang / DMPHN-cvpr19-master / SDNet3.py View on Github external
def save_deblur_images(images, iteration, epoch):
    filename = './checkpoints/' + METHOD + "/epoch" + str(epoch) + "/" + "Iter_" + str(iteration) + "_deblur.png"
    torchvision.utils.save_image(images, filename)