Spaces:
Sleeping
Sleeping
Download app.py from vasvadim/minecraft2real: direct link, hf CLI and curl.
- Browser
- Download file 5.2 kB
-
https://huggingface.co/spaces/vasvadim/minecraft2real/resolve/main/app.py
- Command line
-
hf download hf://spaces/vasvadim/minecraft2real/app.py
-
curl -L -o app.py https://huggingface.co/spaces/vasvadim/minecraft2real/resolve/main/app.py
5.2 kB
| import gradio as gr | |
| import torch | |
| import torch.nn as nn | |
| import torchvision.transforms as transforms | |
| from PIL import Image | |
| class ResidualBlock(nn.Module): | |
| def __init__(self, in_features): | |
| super(ResidualBlock, self).__init__() | |
| self.block = nn.Sequential( | |
| nn.ReflectionPad2d(1), | |
| nn.Conv2d(in_features, in_features, 3), | |
| nn.InstanceNorm2d(in_features), | |
| nn.ReLU(inplace=True), | |
| nn.ReflectionPad2d(1), | |
| nn.Conv2d(in_features, in_features, 3), | |
| nn.InstanceNorm2d(in_features), | |
| ) | |
| def forward(self, x): | |
| return x + self.block(x) | |
| class Generator(nn.Module): | |
| def __init__(self, input_nc=3, output_nc=3, n_residual_blocks=9): | |
| super(Generator, self).__init__() | |
| model = [ | |
| nn.ReflectionPad2d(3), | |
| nn.Conv2d(input_nc, 64, 7), | |
| nn.InstanceNorm2d(64), | |
| nn.ReLU(inplace=True) | |
| ] | |
| in_features = 64 | |
| out_features = in_features * 2 | |
| for _ in range(2): | |
| model += [ | |
| nn.Conv2d(in_features, out_features, 3, stride=2, padding=1), | |
| nn.InstanceNorm2d(out_features), | |
| nn.ReLU(inplace=True) | |
| ] | |
| in_features = out_features | |
| out_features = in_features * 2 | |
| for _ in range(n_residual_blocks): | |
| model += [ResidualBlock(in_features)] | |
| out_features = in_features // 2 | |
| for _ in range(2): | |
| model += [ | |
| nn.ConvTranspose2d(in_features, out_features, 3, stride=2, padding=1, output_padding=1), | |
| nn.InstanceNorm2d(out_features), | |
| nn.ReLU(inplace=True) | |
| ] | |
| in_features = out_features | |
| out_features = in_features // 2 | |
| model += [nn.ReflectionPad2d(3), nn.Conv2d(64, output_nc, 7), nn.Tanh()] | |
| self.model = nn.Sequential(*model) | |
| def forward(self, x): | |
| return self.model(x) | |
| class CycleGAN_Inference(nn.Module): | |
| def __init__(self): | |
| super(CycleGAN_Inference, self).__init__() | |
| self.generators = nn.ModuleDict({ | |
| "a_to_b": Generator(n_residual_blocks=9), | |
| "b_to_a": Generator(n_residual_blocks=9) | |
| }) | |
| MEAN_A = [0.390726238489151, 0.445621520280838, 0.43530991673469543] | |
| STD_A = [0.18484675884246826, 0.20507246255874634, 0.31139838695526123] | |
| MEAN_B = [0.4437699615955353, 0.47194185853004456, 0.4608269929885864] | |
| STD_B = [0.23820169270038605, 0.226677805185318, 0.26058071851730347] | |
| model = CycleGAN_Inference() | |
| state_dict = torch.load("generators_weights.pt", map_location=torch.device('cpu'), weights_only=True) | |
| model.load_state_dict(state_dict) | |
| model.eval() | |
| def preprocess_image(image, is_a_to_b): | |
| mean = MEAN_A if is_a_to_b else MEAN_B | |
| std = STD_A if is_a_to_b else STD_B | |
| transform = transforms.Compose([ | |
| transforms.Resize((256, 256)), | |
| transforms.ToTensor(), | |
| transforms.Normalize(mean, std) | |
| ]) | |
| if image.mode != 'RGB': | |
| image = image.convert('RGB') | |
| return transform(image).unsqueeze(0) | |
| def postprocess_image(tensor, is_a_to_b): | |
| mean = MEAN_B if is_a_to_b else MEAN_A | |
| std = STD_B if is_a_to_b else STD_A | |
| mean_t = torch.tensor(mean).view(3, 1, 1) | |
| std_t = torch.tensor(std).view(3, 1, 1) | |
| tensor = tensor.squeeze(0).cpu().detach() | |
| tensor = tensor * std_t + mean_t | |
| tensor = tensor.clamp(0, 1) | |
| img = transforms.ToPILImage()(tensor) | |
| return img | |
| def translate_image(input_img, direction): | |
| if input_img is None: | |
| return None | |
| is_a_to_b = (direction == "Minecraft -> Реальные пейзажи (A to B)") | |
| input_tensor = preprocess_image(input_img, is_a_to_b) | |
| with torch.no_grad(): | |
| if is_a_to_b: | |
| output_tensor = model.generators["a_to_b"](input_tensor) | |
| else: | |
| output_tensor = model.generators["b_to_a"](input_tensor) | |
| result_img = postprocess_image(output_tensor, is_a_to_b) | |
| return result_img | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("# 🌍 CycleGAN: Minecraft ↔ Real Life") | |
| gr.Markdown("Загрузи картинку, выбери направление перевода и нажми кнопку **Генерировать**!") | |
| with gr.Row(): | |
| with gr.Column(): | |
| input_image = gr.Image(type="pil", label="Оригинальное изображение") | |
| direction = gr.Radio( | |
| choices=["Minecraft -> Реальные пейзажи (A to B)", "Реальные пейзажи -> Minecraft (B to A)"], | |
| value="Minecraft -> Реальные пейзажи (A to B)", | |
| label="Направление перевода" | |
| ) | |
| submit_btn = gr.Button("Генерировать 🚀", variant="primary") | |
| with gr.Column(): | |
| output_image = gr.Image(type="pil", label="Результат") | |
| submit_btn.click( | |
| fn=translate_image, | |
| inputs=[input_image, direction], | |
| outputs=output_image | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |