<jupyter_start><jupyter_text>Based on: https://arxiv.org/abs/1508.06576<jupyter_code>!ln -s "/content/drive/MyDrive/meu/imgs" ./data
# from __future__ import print_function

import torch
import torch.nn as nn
import torch.optim as optim
from PIL import Image
import torchvision.transforms as transforms
import torchvision.models as models
from torchvision.utils import save_image

# import torch.nn.functional as F
# import matplotlib.pyplot as plt
# import copy
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
img_size =  356
loader = transforms.Compose(
    [transforms.Resize((img_size,img_size)),
     transforms.ToTensor(),
     #transforms.Normalize(mean=[],std=[])
     ]
)
# cnn = models.vgg19(pretrained=True).to(device).eval()# get all NET
# cnn = models.vgg19(pretrained=True).features.to(device).eval()# get only the CNN
"""
import torch.nn.functional as F
def read_img(path,device):
    img = cv2.cvtColor(cv2.imread(path),cv2.COLOR_BGR2RGB) # inportando imagem
    img = torch.Tensor(img) # convertendo em tensor
    img = img.permute(2,0,1).unsqueeze(0) # reordenando
    img = F.interpolate(img, size=224) # resize
    # img = img.to('cpu')
    return img.to(device)
"""
def load_image(path,device,loader):
    img = Image.open(path)
    img = loader(img).unsqueeze(0)
    return img.to(device)
class VGG(nn.Module):
    def __init__(self,device):
        super(VGG, self).__init__()
        self.features_layers = [0,5,10,19,28]
        self.model = models.vgg19(pretrained=True).features[:29].to(device).eval()
    def forward(self, x):
        features = []
        for num, layer in enumerate(self.model):
            x=layer(x)

            if num in self.features_layers :
                features.append(x)
        return features<jupyter_output><empty_output><jupyter_text>Import images<jupyter_code>original_img = load_image('/content/data/foto.jpeg',device=device,loader=loader)
style_img = load_image('/content/data/AP_Biblioteca_R01-960x540.jpg',device=device,loader=loader)
generated_img = original_img.clone().requires_grad_(True)
model = VGG(device)<jupyter_output><empty_output><jupyter_text>Hyperparametrs<jupyter_code>steps = 600
learn_rate = 0.01
alpha = 0.56 # content_weight
beta = 1000000 #0.56 # style_weight
optimizer = optim.Adam([generated_img],lr=learn_rate)
for step in range(steps):
    generated_img_features = model(generated_img)
    original_img_features = model(original_img)
    style_img_features = model(style_img)

    style_loss = original_loss = 0

    for gen_feature, orig_feature, style_feature in zip(
        generated_img_features, original_img_features, style_img_features):

        bash_size, channel, h,w =  gen_feature.shape

        original_loss += torch.mean((gen_feature-orig_feature)**2) 

        #compute gram matrix
        G = gen_feature.view(channel,h*w).mm(
            gen_feature.view(channel,h*w).t())
        
        A = style_feature.view(channel,h*w).mm(
            style_feature.view(channel,h*w).t())

        style_loss += torch.mean((G-A)**2) 

    total_loss = alpha * original_loss + beta * style_loss
    optimizer.zero_grad()
    total_loss.backward()
    optimizer.step()

    if step % 50 == 0:
        print(f"{step}: {total_loss.item()}")
        save_image(generated_img,f"hoovertowernight_{step}.png")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12760882528324388, 'fraction_numerical': 0.021765056648777578, 'mean_word_length': 4.014947683109118, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '73021', 'license': 'Apache-2.0', 'n_tokens_mistral': 1196, 'n_tokens_neox': 1131, 'n_words': 205, 'path': 'StyleTransfer.ipynb', 'repo_name': 'pattrickx/StyleTransfer', 'size': '3354'}