Spaces:
Runtime error
Runtime error
File size: 6,140 Bytes
9ac69b4 34bfe14 fcb49fd 20549c6 9ac69b4 756e2bd 9ac69b4 f76cde4 9ac69b4 756e2bd fcb49fd 20549c6 fcb49fd 9ac69b4 756e2bd 9ac69b4 9107020 756e2bd 20549c6 756e2bd 9ac69b4 756e2bd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
from huggingface_hub import hf_hub_download
Rain_Princess = hf_hub_download(repo_id="maze/FastStyleTransfer", filename="Rain_Princess_512.pth")
The_Scream = hf_hub_download(repo_id="maze/FastStyleTransfer", filename="Scream_512.pth")
The_Mosaic = hf_hub_download(repo_id="maze/FastStyleTransfer", filename="Mosaic_512.pth")
Starry_Night = hf_hub_download(repo_id="maze/FastStyleTransfer", filename="Starry_Night_512.pth")
import numpy as np
from PIL import Image
import gradio as gr
import torch
import torch.nn as nn
import torchvision.transforms as transforms
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class TransformerNetwork(nn.Module):
def __init__(self, tanh_multiplier=None):
super(TransformerNetwork, self).__init__()
self.ConvBlock = nn.Sequential(
ConvLayer(3, 32, 9, 1),
nn.ReLU(),
ConvLayer(32, 64, 3, 2),
nn.ReLU(),
ConvLayer(64, 128, 3, 2),
nn.ReLU()
)
self.ResidualBlock = nn.Sequential(
ResidualLayer(128, 3),
ResidualLayer(128, 3),
ResidualLayer(128, 3),
ResidualLayer(128, 3),
ResidualLayer(128, 3)
)
self.DeconvBlock = nn.Sequential(
DeconvLayer(128, 64, 3, 2, 1),
nn.ReLU(),
DeconvLayer(64, 32, 3, 2, 1),
nn.ReLU(),
ConvLayer(32, 3, 9, 1, norm="None")
)
self.tanh_multiplier = tanh_multiplier
def forward(self, x):
x = self.ConvBlock(x)
x = self.ResidualBlock(x)
x = self.DeconvBlock(x)
if isinstance(self.tanh_multiplier, int):
x = self.tanh_multiplier * F.tanh(x)
return x
class ConvLayer(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride, norm="instance"):
super(ConvLayer, self).__init__()
padding_size = kernel_size // 2
self.pad = nn.ReflectionPad2d(padding_size)
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride)
if norm == "instance":
self.norm = nn.InstanceNorm2d(out_channels, affine=True)
elif norm == "batch":
self.norm = nn.BatchNorm2d(out_channels, affine=True)
else:
self.norm = nn.Identity()
def forward(self, x):
x = self.pad(x)
x = self.conv(x)
x = self.norm(x)
return x
class ResidualLayer(nn.Module):
def __init__(self, channels=128, kernel_size=3):
super(ResidualLayer, self).__init__()
self.conv1 = ConvLayer(channels, channels, kernel_size, stride=1)
self.relu = nn.ReLU()
self.conv2 = ConvLayer(channels, channels, kernel_size, stride=1)
def forward(self, x):
identity = x
out = self.relu(self.conv1(x))
out = self.conv2(out)
out = out + identity
return out
class DeconvLayer(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride, output_padding, norm="instance"):
super(DeconvLayer, self).__init__()
padding_size = kernel_size // 2
self.conv_transpose = nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride, padding_size, output_padding)
if norm == "instance":
self.norm = nn.InstanceNorm2d(out_channels, affine=True)
elif norm == "batch":
self.norm = nn.BatchNorm2d(out_channels, affine=True)
else:
self.norm = nn.Identity()
def forward(self, x):
x = self.conv_transpose(x)
out = self.norm(x)
return out
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
transformer = TransformerNetwork().to(device)
transformer.eval()
transform = transforms.Compose([
# transforms.Resize(512),
transforms.ToTensor(),
transforms.Normalize(mean, std),
])
denormalize = transforms.Normalize(
mean= [-m/s for m, s in zip(mean, std)],
std= [1/s for s in std]
)
tensor2Image = transforms.ToPILImage()
@torch.no_grad()
def process(image, model):
image = transform(image).to(device)
image = image.unsqueeze(dim=0)
image = denormalize(model(image)).cpu()
image = torch.clamp(image.squeeze(dim=0), 0, 1)
image = tensor2Image(image)
return image
def main(image, backbone, style):
if style == "The Scream":
transformer.load_state_dict(torch.load(The_Scream, map_location=torch.device('cpu')))
elif style == "Rain Princess":
transformer.load_state_dict(torch.load(Rain_Princess, map_location=torch.device('cpu')))
elif style == "The Mosaic":
transformer.load_state_dict(torch.load(The_Mosaic, map_location=torch.device('cpu')))
elif style == "Starry Night":
transformer.load_state_dict(torch.load(Starry_Night, map_location=torch.device('cpu')))
else:
transformer.load_state_dict(torch.load(Rain_Princess, map_location=torch.device('cpu')))
image = Image.fromarray(image)
isize = image.size
image = process(image, transformer)
s = f"The output image {str(image.size)} is processed by {backbone} based on input image {str(isize)}. <br> Please <b>rate</b> the generated image through the <b>Flag</b> button below!"
return image, s
gr.Interface(
title = "Stylize",
description = "Image generated based on Fast Style Transfer",
fn = main,
inputs = [
gr.inputs.Image(),
gr.inputs.Radio(["VGG19", "Robust ResNet50", "Standard ResNet50"], label="Backbone"),
gr.inputs.Dropdown(["The Scream", "Rain Princess", "Starry Night", "The Mosaic"], type="value", default="Rain Princess", label="style")
],
outputs = [gr.outputs.Image(label="Stylized"), gr.outputs.HTML(label="Comment")],
# examples = [
# []
# ],
# live = True, # the interface will recalculate as soon as the user input changes.
allow_flagging = "manual",
flagging_options = ["Excellect", "Moderate", "Bad"],
flagging_dir = "flagged",
allow_screenshot = False,
).launch()
# iface.launch(enable_queue=True, cache_examples=True, debug=True) |