├── style.jpg ├── content.jpg ├── outputs ├── Elon │ ├── 500.png │ ├── style.jpg │ └── content.jpg ├── ex3 │ ├── 4000.png │ ├── content.jpg │ └── style.jpg ├── ex4 │ ├── 300.png │ ├── 1-style.jpg │ └── 1-content.png ├── ex5 │ ├── 100.png │ ├── 200.png │ ├── 300.png │ ├── 1-style.jpg │ └── 1-content.jpg ├── ex6 │ ├── 100.png │ ├── 200.png │ ├── 300.png │ ├── 1-style.jpg │ └── 1-content.jpg ├── picaso │ ├── 3900.png │ ├── style.jpg │ └── content.jpg ├── MonaLisa │ ├── monaLisa.jpg │ ├── MonaLisaOutput.png │ └── MonaLisaStyle.jpg └── dark-knight │ ├── output.png │ ├── style.jpg │ └── content.jpg ├── README.md ├── LICENSE └── styleTransfer.py /style.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/style.jpg -------------------------------------------------------------------------------- /content.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/content.jpg -------------------------------------------------------------------------------- /outputs/Elon/500.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/Elon/500.png -------------------------------------------------------------------------------- /outputs/ex3/4000.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/ex3/4000.png -------------------------------------------------------------------------------- /outputs/ex4/300.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/ex4/300.png -------------------------------------------------------------------------------- /outputs/ex5/100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/ex5/100.png -------------------------------------------------------------------------------- /outputs/ex5/200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/ex5/200.png -------------------------------------------------------------------------------- /outputs/ex5/300.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/ex5/300.png -------------------------------------------------------------------------------- /outputs/ex6/100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/ex6/100.png -------------------------------------------------------------------------------- /outputs/ex6/200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/ex6/200.png -------------------------------------------------------------------------------- /outputs/ex6/300.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/ex6/300.png -------------------------------------------------------------------------------- /outputs/Elon/style.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/Elon/style.jpg -------------------------------------------------------------------------------- /outputs/ex3/content.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/ex3/content.jpg -------------------------------------------------------------------------------- /outputs/ex3/style.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/ex3/style.jpg -------------------------------------------------------------------------------- /outputs/ex4/1-style.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/ex4/1-style.jpg -------------------------------------------------------------------------------- /outputs/ex5/1-style.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/ex5/1-style.jpg -------------------------------------------------------------------------------- /outputs/ex6/1-style.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/ex6/1-style.jpg -------------------------------------------------------------------------------- /outputs/picaso/3900.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/picaso/3900.png -------------------------------------------------------------------------------- /outputs/Elon/content.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/Elon/content.jpg -------------------------------------------------------------------------------- /outputs/ex4/1-content.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/ex4/1-content.png -------------------------------------------------------------------------------- /outputs/ex5/1-content.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/ex5/1-content.jpg -------------------------------------------------------------------------------- /outputs/ex6/1-content.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/ex6/1-content.jpg -------------------------------------------------------------------------------- /outputs/picaso/style.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/picaso/style.jpg -------------------------------------------------------------------------------- /outputs/picaso/content.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/picaso/content.jpg -------------------------------------------------------------------------------- /outputs/MonaLisa/monaLisa.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/MonaLisa/monaLisa.jpg -------------------------------------------------------------------------------- /outputs/dark-knight/output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/dark-knight/output.png -------------------------------------------------------------------------------- /outputs/dark-knight/style.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/dark-knight/style.jpg -------------------------------------------------------------------------------- /outputs/dark-knight/content.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/dark-knight/content.jpg -------------------------------------------------------------------------------- /outputs/MonaLisa/MonaLisaOutput.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/MonaLisa/MonaLisaOutput.png -------------------------------------------------------------------------------- /outputs/MonaLisa/MonaLisaStyle.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AyushExel/Neural-Style-Transfer/HEAD/outputs/MonaLisa/MonaLisaStyle.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Neural-Style-Transfer 2 | This repo contains the implementation of original neural style transfer algorithm 3 | 4 | # Testing 5 | To run this program on your sample content-style pair, just replace the content and style image of the repo with the image of your choice having the same name. the program will run 4000 iterations and output the resultant ater every 500th execution. You can edit these details in the file containing the code. 6 | 7 | To perform style transfer run: `python styleTransfer.py` 8 | 9 | # Some Outputs: 10 | ![](https://github.com/AyushExel/Neural-Style-Transfer/blob/master/outputs/Elon/500.png) 11 | ![](https://github.com/AyushExel/Neural-Style-Transfer/blob/master/outputs/ex4/300.png) 12 | ![](https://github.com/AyushExel/Neural-Style-Transfer/blob/master/outputs/ex5/100.png) 13 | ![](https://github.com/AyushExel/Neural-Style-Transfer/blob/master/outputs/ex5/1-style.jpg) 14 | ![](https://github.com/AyushExel/Neural-Style-Transfer/blob/master/outputs/ex6/100.png) 15 | ![](https://github.com/AyushExel/Neural-Style-Transfer/blob/master/outputs/dark-knight/output.png) 16 | 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Ayush Chaurasia 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /styleTransfer.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torchvision import transforms , models 3 | from PIL import Image 4 | import matplotlib.pyplot as plt 5 | import numpy as np 6 | 7 | device = ("cuda" if torch.cuda.is_available() else "cpu") 8 | 9 | 10 | model = models.vgg19(pretrained=True).features 11 | for p in model.parameters(): 12 | p.requires_grad = False 13 | model.to(device) 14 | 15 | def model_activations(input,model): 16 | layers = { 17 | '0' : 'conv1_1', 18 | '5' : 'conv2_1', 19 | '10': 'conv3_1', 20 | '19': 'conv4_1', 21 | '21': 'conv4_2', 22 | '28': 'conv5_1' 23 | } 24 | features = {} 25 | x = input 26 | x = x.unsqueeze(0) 27 | for name,layer in model._modules.items(): 28 | x = layer(x) 29 | if name in layers: 30 | features[layers[name]] = x 31 | 32 | return features 33 | 34 | 35 | 36 | 37 | 38 | transform = transforms.Compose([transforms.Resize(300), 39 | transforms.ToTensor(), 40 | transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))]) 41 | 42 | 43 | content = Image.open("content.jpg").convert("RGB") 44 | content = transform(content).to(device) 45 | print("COntent shape => ", content.shape) 46 | style = Image.open("style.jpg").convert("RGB") 47 | style = transform(style).to(device) 48 | 49 | def imcnvt(image): 50 | x = image.to("cpu").clone().detach().numpy().squeeze() 51 | x = x.transpose(1,2,0) 52 | x = x*np.array((0.5,0.5,0.5)) + np.array((0.5,0.5,0.5)) 53 | return np.clip(x,0,1) 54 | 55 | fig, (ax1,ax2) = plt.subplots(1,2) 56 | 57 | ax1.imshow(imcnvt(content),label = "Content") 58 | ax2.imshow(imcnvt(style),label = "Style") 59 | plt.show() 60 | 61 | def gram_matrix(imgfeature): 62 | _,d,h,w = imgfeature.size() 63 | imgfeature = imgfeature.view(d,h*w) 64 | gram_mat = torch.mm(imgfeature,imgfeature.t()) 65 | 66 | return gram_mat 67 | 68 | 69 | target = content.clone().requires_grad_(True).to(device) 70 | 71 | #set device to cuda if available 72 | print("device = ",device) 73 | 74 | 75 | style_features = model_activations(style,model) 76 | content_features = model_activations(content,model) 77 | 78 | style_wt_meas = {"conv1_1" : 1.0, 79 | "conv2_1" : 0.8, 80 | "conv3_1" : 0.4, 81 | "conv4_1" : 0.2, 82 | "conv5_1" : 0.1} 83 | 84 | style_grams = {layer:gram_matrix(style_features[layer]) for layer in style_features} 85 | 86 | content_wt = 100 87 | style_wt = 1e8 88 | 89 | print_after = 500 90 | epochs = 4000 91 | optimizer = torch.optim.Adam([target],lr=0.007) 92 | 93 | for i in range(1,epochs+1): 94 | target_features = model_activations(target,model) 95 | content_loss = torch.mean((content_features['conv4_2']-target_features['conv4_2'])**2) 96 | 97 | style_loss = 0 98 | for layer in style_wt_meas: 99 | style_gram = style_grams[layer] 100 | target_gram = target_features[layer] 101 | _,d,w,h = target_gram.shape 102 | target_gram = gram_matrix(target_gram) 103 | 104 | style_loss += (style_wt_meas[layer]*torch.mean((target_gram-style_gram)**2))/d*w*h 105 | 106 | total_loss = content_wt*content_loss + style_wt*style_loss 107 | 108 | if i%10==0: 109 | print("epoch ",i," ", total_loss) 110 | 111 | optimizer.zero_grad() 112 | total_loss.backward() 113 | optimizer.step() 114 | 115 | if i%print_after == 0: 116 | plt.imshow(imcnvt(target),label="Epoch "+str(i)) 117 | plt.show() 118 | plt.imsave(str(i)+'.png',imcnvt(target),format='png') 119 | 120 | 121 | 122 | 123 | 124 | 125 | --------------------------------------------------------------------------------