Views
No views yet
1import torch
2from PIL import Image
3from huggan.pytorch.cyclegan.modeling_cyclegan import GeneratorResNet
4from torchvision import transforms as T
5from torchvision.transforms import Compose, Resize, ToTensor, Normalize
6from torchvision.utils import make_grid
7from huggingface_hub import hf_hub_download, file_download
8from accelerate import Accelerator
9import json
10
11def load_lightweight_model(model_name):
12 file_path = file_download.hf_hub_download(
13 repo_id=model_name,
14 filename="config.json"
15 )
16 config = json.loads(open(file_path).read())
17 organization_name, name = model_name.split("/")
18 model = Trainer(**config, organization_name=organization_name, name=name)
19 model.load(use_cpu=True)
20 model.accelerator = Accelerator()
21 return model
22def get_concat_h(im1, im2):
23 dst = Image.new('RGB', (im1.width + im2.width, im1.height))
24 dst.paste(im1, (0, 0))
25 dst.paste(im2, (im1.width, 0))
26 return dst
27
28
29n_channels = 3
30image_size = 256
31input_shape = (image_size, image_size)
32
33transform = Compose([
34 T.ToPILImage(),
35 T.Resize(input_shape),
36 ToTensor(),
37 Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
38])
39
40# load the translation model from source to target images: source will be generated by a separate Lightweight GAN, w
41# while the target images are the result of the translation applied by the GeneratorResnet to the generated source images.
42# Hence, given the source domain A and target domain B,
43# B = Translator(GAN(A))
44translator = GeneratorResNet.from_pretrained(f'huggingnft/{model_name}',
45 input_shape=(n_channels, image_size, image_size),
46 num_residual_blocks=9)
47
48# sample noise that is used to generate source images by the
49z = torch.randn(nrows, 100, 1, 1)
50# load the GAN generator of source images that will be translated by the translation model
51model = load_lightweight_model(f"huggingnft/{model_name.split('__2__')[0]}")
52collectionA = model.generate_app(
53 num=timestamped_filename(),
54 nrow=nrows,
55 checkpoint=-1,
56 types="default"
57 )[1]
58# resize to translator model input shape
59resize = T.Resize((256, 256))
60input = resize(collectionA)
61
62# translate the resized collectionA to collectionB
63collectionB = translator(input)
64
65out_transform = T.ToPILImage()
66results = []
67for collA_image, collB_image in zip(input, collectionB):
68 results.append(
69 get_concat_h(out_transform(make_grid(collA_image, nrow=1, normalize=True)), out_transform(make_grid(collB_image, nrow=1, normalize=True)))
70 )1from datasets import load_dataset
2
3collectionA = load_dataset("huggingnft/COLLECTION_A")
4collectionB = load_dataset("huggingnft/COLLECTION_B")1n_channels = 3
2image_size = 256
3input_shape = (image_size, image_size)
4
5transform = Compose([
6 T.ToPILImage(),
7 T.Resize(input_shape),
8 ToTensor(),
9 Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
10])1@misc{https://doi.org/10.48550/arxiv.1703.10593,
2 doi = {10.48550/ARXIV.1703.10593},
3
4 url = {https://arxiv.org/abs/1703.10593},
5
6 author = {Zhu, Jun-Yan and Park, Taesung and Isola, Phillip and Efros, Alexei A.},
7
8 keywords = {Computer Vision and Pattern Recognition (cs.CV), FOS: Computer and information sciences, FOS: Computer and information sciences},
9
10 title = {Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks},
11
12 publisher = {arXiv},
13
14 year = {2017},
15
16 copyright = {arXiv.org perpetual, non-exclusive license}
17}1@InProceedings{huggingnft,
2 author={Aleksey Korshuk, Christian Cancedda}
3 year=2022
4}