import pickle import numpy as np from sklearn.decomposition import PCA from sklearn.manifold import TSNE from libs.Matrix import MulLayer import torchvision.utils as vutils import torch.backends.cudnn as cudnn from libs.models import encoder3, encoder4 from libs.models import decoder3, decoder4 os.environ[ "CUDA_VISIBLE_DEVICES"] = "0" # USED ONLY IF OTHER GPUS ARE BEING USED if True: style_dataset = Dataset('Database/WikiArt/train/', 256, 256, test=True) style_loader_ = torch.utils.data.DataLoader(dataset=style_dataset, batch_size=64, shuffle=False, num_workers=4, drop_last=True) style_loader = iter(style_loader_) # styleV = torch.Tensor(64,3,224,224).cuda() matrix = MulLayer('r41') vgg = encoder4() vgg.load_state_dict(torch.load('models/vgg_r41.pth')) matrix.load_state_dict(torch.load('models/r41.pth')) vgg.cuda() matrix.cuda() features = []
# help='which features to transfer, either r31 or r41') parser.add_argument("--layer", default="r41", help='which features to transfer, either r31 or r41') ################# PREPARATIONS ################# opt = parser.parse_args() opt.cuda = torch.cuda.is_available() print_options(opt) os.makedirs(opt.outf, exist_ok=True) cudnn.benchmark = True ################# DATA ################# content_dataset = Dataset(opt.contentPath, opt.loadSize, opt.fineSize, test=True) content_loader = torch.utils.data.DataLoader(dataset=content_dataset, batch_size=opt.batchSize, shuffle=False, num_workers=1) style_dataset = Dataset(opt.stylePath, opt.loadSize, opt.fineSize, test=True) style_loader = torch.utils.data.DataLoader(dataset=style_dataset, batch_size=opt.batchSize, shuffle=False, num_workers=1) ################# MODEL #################
torch.cuda.FloatTensor(self.features[idx]).shape, sF.shape, Fm.shape) else: idx = np.random.randint(0, self.size, b) sF = torch.cuda.FloatTensor(self.features[idx]) Fm = torch.cuda.FloatTensor(self.means[idx]) cF = self.vgg(x) feature, transmatrix = self.matrix(cF, sF, Fm) transfer = self.dec(feature) return transfer.clamp(0, 1) if __name__ == "__main__": batch_size = 8 content_dataset = Dataset('Database/COCO/2017/train2017/', 256, 256, test=True) content_loader = torch.utils.data.DataLoader(dataset=content_dataset, batch_size=batch_size, shuffle=False, num_workers=1, drop_last=True) Stylenet = StyleAugmentation().cuda() for it, (content, _) in enumerate(content_loader): styled = Stylenet(content.cuda()) # vutils.save_image(styled[0].data,'Style.png',normalize=True,scale_each=True,nrow=1) for n in range(styled.shape[0]): Image = np.uint8( content.permute(0, 2, 3, 1)[n].cpu().detach().numpy() * 255)