Spaces:
Runtime error
Runtime error
import pandas as pd | |
import numpy as np | |
import streamlit as st | |
from models import Generator, Discriminrator | |
from utils import image_to_base64 | |
import torch | |
import torchvision.transforms as T | |
from torchvision.utils import make_grid | |
from PIL import Image | |
device = 'cuda' if torch.cuda.is_available() else 'cpu' | |
model_name = { | |
"aurora": 'huggan/fastgan-few-shot-aurora-bs8', | |
"painting": 'huggan/fastgan-few-shot-painting-bs8', | |
"shell": 'huggan/fastgan-few-shot-shells', | |
"fauvism": 'huggan/fastgan-few-shot-fauvism-still-life', | |
} | |
#@st.cache(allow_output_mutation=True) | |
def load_generator(model_name_or_path): | |
generator = Generator(in_channels=256, out_channels=3) | |
generator = generator.from_pretrained(model_name_or_path, in_channels=256, out_channels=3) | |
_ = generator.to('cuda') | |
_ = generator.eval() | |
return generator | |
def _denormalize(input: torch.Tensor) -> torch.Tensor: | |
return (input * 127.5) + 127.5 | |
def generate_images(generator, number_imgs): | |
noise = torch.zeros(number_imgs, 256, 1, 1, device='cuda').normal_(0.0, 1.0) | |
with torch.no_grad(): | |
gan_images, _ = generator(noise) | |
gan_images = _denormalize(gan_images.detach()).cpu() | |
gan_images = make_grid(gan_images, nrow=number_imgs, normalize=True) | |
gan_images = gan_images.mul(255).add_(0.5).clamp_(0, 255).permute(1, 2, 0).to("cpu", torch.uint8).numpy() | |
gan_images = Image.fromarray(gan_images) | |
return gan_images | |
def main(): | |
st.set_page_config( | |
page_title="FastGAN Generator", | |
page_icon="🖥️", | |
layout="wide", | |
initial_sidebar_state="expanded" | |
) | |
# st.sidebar.markdown( | |
# """ | |
# <style> | |
# .aligncenter { | |
# text-align: center; | |
# } | |
# </style> | |
# <p class="aligncenter"> | |
# <img src="https://e7.pngegg.com/pngimages/510/121/png-clipart-machine-learning-deep-learning-artificial-intelligence-algorithm-machine-learning-angle-text.png"/> | |
# </p> | |
# """, | |
# unsafe_allow_html=True, | |
# ) | |
st.sidebar.markdown( | |
""" | |
___ | |
<p style='text-align: center'> | |
FastGAN is an few-shot GAN model that generates images of several types! | |
</p> | |
<p style='text-align: center'> | |
Model training and Space creation by | |
<br/> | |
<a href="https://huggingface.co/vumichien" target="_blank">Chien Vu</a> | <a href="https://huggingface.co/geninhu" target="_blank">Nhu Hoang</a> | |
<br/> | |
</p> | |
<p style='text-align: center'> | |
<a href="https://github.com/silentz/Towards-Faster-And-Stabilized-GAN-Training-For-High-Fidelity-Few-Shot-Image-Synthesis" target="_blank">based on FastGAN model</a> | <a href="https://arxiv.org/abs/2101.04775" target="_blank">Article</a> | |
</p> | |
""", | |
unsafe_allow_html=True, | |
) | |
st.header("Welcome to FastGAN") | |
col1, col2, col3, col4 = st.columns([3,3,3,3]) | |
with col1: | |
st.markdown('Fauvism GAN [model](https://huggingface.co/huggan/fastgan-few-shot-fauvism-still-life)', unsafe_allow_html=True) | |
st.image('fauvism.png', width=300) | |
with col2: | |
st.markdown('Aurora GAN [model](https://huggingface.co/huggan/fastgan-few-shot-aurora-bs8)', unsafe_allow_html=True) | |
st.image('aurora.png', width=300) | |
with col3: | |
st.markdown('Painting GAN [model](https://huggingface.co/huggan/fastgan-few-shot-painting-bs8)', unsafe_allow_html=True) | |
st.image('painting.png', width=300) | |
with col4: | |
st.markdown('Shell GAN [model](https://huggingface.co/huggan/fastgan-few-shot-shells)', unsafe_allow_html=True) | |
st.image('shell.png', width=300) | |
# Choose generator | |
col11, col12, col13 = st.columns([4,4,2]) | |
with col11: | |
st.markdown('Choose type of image to generate', unsafe_allow_html=True) | |
img_type = st.selectbox("", index=0, options=["shell", "aurora", "painting", "fauvism"]) | |
with col12: | |
number_imgs = st.number_input('How many images you want to generate ?', min_value=1, max_value=5) | |
if number_imgs is None: | |
st.write('Invalid number ! Please insert number of images to generate !') | |
raise ValueError('Invalid number ! Please insert number of images to generate !') | |
with col13: | |
generate_button = st.button('Get Image!') | |
# row2 = st.columns([10]) | |
# with row2: | |
if generate_button: | |
st.markdown(""" | |
<small><i>Predictions may take up to 1mn under high load. Please stand by.</i></small> | |
""", | |
unsafe_allow_html=True,) | |
generator = load_generator(model_name[img_type]) | |
gan_images = generate_images(generator, number_imgs) | |
# margin = 0.1 # for better position of zoom in arrow | |
# n_columns = 2 | |
# cols = st.columns([1] + [margin, 1] * (n_columns - 1)) | |
# for i, img in enumerate(gan_images): | |
# cols[(i % n_columns) * 2].image(img) | |
st.image(gan_images, width=200*number_imgs) | |
if __name__ == '__main__': | |
main() | |