Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -49,13 +49,13 @@ def compute_loss(generated_features, content_features, style_features, alpha, be
|
|
| 49 |
for generated_feature, content_feature, style_feature in zip(generated_features, content_features, style_features):
|
| 50 |
batch_size, n_feature_maps, height, width = generated_feature.size()
|
| 51 |
|
| 52 |
-
content_loss +=
|
| 53 |
|
| 54 |
G = torch.mm((generated_feature.view(batch_size * n_feature_maps, height * width)), (generated_feature.view(batch_size * n_feature_maps, height * width)).t())
|
| 55 |
A = torch.mm((style_feature.view(batch_size * n_feature_maps, height * width)), (style_feature.view(batch_size * n_feature_maps, height * width)).t())
|
| 56 |
|
| 57 |
E_l = ((G - A) ** 2)
|
| 58 |
-
w_l = 1/5
|
| 59 |
style_loss += torch.mean(w_l * E_l)
|
| 60 |
|
| 61 |
return alpha * content_loss + beta * style_loss
|
|
@@ -86,8 +86,8 @@ def inference(content_image, style_name, style_strength, output_quality, progres
|
|
| 86 |
|
| 87 |
with torch.no_grad():
|
| 88 |
content_features = model(content_img)
|
| 89 |
-
|
| 90 |
-
|
| 91 |
|
| 92 |
for _ in tqdm(range(iters), desc='The magic is happening ✨'):
|
| 93 |
optimizer.zero_grad()
|
|
|
|
| 49 |
for generated_feature, content_feature, style_feature in zip(generated_features, content_features, style_features):
|
| 50 |
batch_size, n_feature_maps, height, width = generated_feature.size()
|
| 51 |
|
| 52 |
+
content_loss += torch.mean((generated_feature - content_feature) ** 2)
|
| 53 |
|
| 54 |
G = torch.mm((generated_feature.view(batch_size * n_feature_maps, height * width)), (generated_feature.view(batch_size * n_feature_maps, height * width)).t())
|
| 55 |
A = torch.mm((style_feature.view(batch_size * n_feature_maps, height * width)), (style_feature.view(batch_size * n_feature_maps, height * width)).t())
|
| 56 |
|
| 57 |
E_l = ((G - A) ** 2)
|
| 58 |
+
w_l = 1 / 5
|
| 59 |
style_loss += torch.mean(w_l * E_l)
|
| 60 |
|
| 61 |
return alpha * content_loss + beta * style_loss
|
|
|
|
| 86 |
|
| 87 |
with torch.no_grad():
|
| 88 |
content_features = model(content_img)
|
| 89 |
+
|
| 90 |
+
style_features = cached_style_features[style_name][0 if img_size == 512 else 1]
|
| 91 |
|
| 92 |
for _ in tqdm(range(iters), desc='The magic is happening ✨'):
|
| 93 |
optimizer.zero_grad()
|
utils.py
CHANGED
|
@@ -3,18 +3,11 @@ from PIL import Image
|
|
| 3 |
import torch
|
| 4 |
import torchvision.transforms as transforms
|
| 5 |
|
| 6 |
-
def preprocess_img(img: Image, img_size):
|
| 7 |
-
original_size = img.size
|
| 8 |
-
|
| 9 |
-
transform = transforms.Compose([
|
| 10 |
-
transforms.Resize((img_size, img_size)),
|
| 11 |
-
transforms.ToTensor()
|
| 12 |
-
])
|
| 13 |
-
img = transform(img).unsqueeze(0)
|
| 14 |
-
return img, original_size
|
| 15 |
-
|
| 16 |
def preprocess_img_from_path(path_to_image, img_size):
|
| 17 |
img = Image.open(path_to_image)
|
|
|
|
|
|
|
|
|
|
| 18 |
original_size = img.size
|
| 19 |
|
| 20 |
transform = transforms.Compose([
|
|
@@ -25,8 +18,7 @@ def preprocess_img_from_path(path_to_image, img_size):
|
|
| 25 |
return img, original_size
|
| 26 |
|
| 27 |
def postprocess_img(img, original_size):
|
| 28 |
-
img = img.cpu().
|
| 29 |
-
img = img.squeeze(0)
|
| 30 |
|
| 31 |
# address tensor value scaling and quantization
|
| 32 |
img = torch.clamp(img, 0, 1)
|
|
|
|
| 3 |
import torch
|
| 4 |
import torchvision.transforms as transforms
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def preprocess_img_from_path(path_to_image, img_size):
|
| 7 |
img = Image.open(path_to_image)
|
| 8 |
+
return preprocess_img(img, img_size)
|
| 9 |
+
|
| 10 |
+
def preprocess_img(img: Image, img_size):
|
| 11 |
original_size = img.size
|
| 12 |
|
| 13 |
transform = transforms.Compose([
|
|
|
|
| 18 |
return img, original_size
|
| 19 |
|
| 20 |
def postprocess_img(img, original_size):
|
| 21 |
+
img = img.detach().cpu().squeeze(0)
|
|
|
|
| 22 |
|
| 23 |
# address tensor value scaling and quantization
|
| 24 |
img = torch.clamp(img, 0, 1)
|