|
| 1 | +--- |
| 2 | +layout: hub_detail |
| 3 | +background-class: hub-background |
| 4 | +body-class: hub |
| 5 | +category: researchers |
| 6 | +title: cyclegan |
| 7 | +summary: generate pictures as famous artis from photos |
| 8 | +image: cyclegan_cezanne.png |
| 9 | +author: Nicola Landro |
| 10 | +tags: [vision, generative] |
| 11 | +github-link: https://github.com/nicolalandro/cyclegan-pretrained |
| 12 | +github-id: nicolalandro/cyclegan-pretrained |
| 13 | +featured_image_1: cyclegan_cezanne.png |
| 14 | +featured_image_2: no-image |
| 15 | +accelerator: "cuda-optional" |
| 16 | +--- |
| 17 | + |
| 18 | +```python |
| 19 | +import torch |
| 20 | +model = torch.hub.load('nicolalandro/cyclegan-pretrained', 'cyclegan', pretrained='img2cezanne', device='cpu') |
| 21 | +print(model.trained_models_list) |
| 22 | +``` |
| 23 | + |
| 24 | +### Example Usage |
| 25 | + |
| 26 | +```python |
| 27 | +from torchvision import transforms |
| 28 | +import torch |
| 29 | +import urllib |
| 30 | +from PIL import Image |
| 31 | +from torchvision.utils import save_image |
| 32 | + |
| 33 | + |
| 34 | +model = torch.hub.load('nicolalandro/cyclegan-pretrained', 'cyclegan', pretrained='img2cezanne', device='cpu') |
| 35 | +model.eval() |
| 36 | + |
| 37 | +url = 'https://raw.githubusercontent.com/nicolalandro/cyclegan-pretrained/main/images/scala_madonnina_del_mare.jpeg' |
| 38 | +img = Image.open(urllib.request.urlopen(url)) |
| 39 | +scale_factor = 0.8 |
| 40 | +shape = [int(x * scale_factor) for x in img.size] |
| 41 | + |
| 42 | +transform = transforms.Compose([ |
| 43 | + transforms.Resize(size=(shape[0], shape[1])), |
| 44 | + transforms.ToTensor(), |
| 45 | + transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) |
| 46 | +]) |
| 47 | + |
| 48 | +scaled_img = transform(img) |
| 49 | +torch_images = scaled_img.unsqueeze(0) |
| 50 | + |
| 51 | +with torch.no_grad(): |
| 52 | + fake_A = 0.5 * (model(torch_images).data + 1.0) |
| 53 | + save_image(fake_A, 'cezanne_generated.png') |
| 54 | +``` |
| 55 | + |
| 56 | +### Model Description |
| 57 | +This is a cyclegan, a generative model that is trained on an image style and can trasnform the generic images into images with that particular style. |
| 58 | + |
| 59 | + |
0 commit comments