Eloi Campeny commited on
Commit
d717c03
1 Parent(s): 5c7b333

firts try gan

Browse files
Files changed (4) hide show
  1. app.py +57 -0
  2. assets/logo.png +0 -0
  3. requirements.txt +5 -0
  4. utils.py +15 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ from utils import load_model, generate
4
+
5
+ ## Title and write
6
+ st.title("Butterfly GAN")
7
+ st.write(
8
+ "Light-GAN model trained with 1000 butterfly images taken from the Smithsonian Museum collection."
9
+ )
10
+
11
+ ## Sidebar
12
+ st.sidebar.subheader("This butterfly does not exist!.")
13
+ st.sidebar.image("assets/logo.png", width=200)
14
+ st.sidebar.caption(
15
+ f"[Model](https://huggingface.co/ceyda/butterfly_cropped_uniq1K_512) and [Dataset](https://huggingface.co/datasets/huggan/smithsonian_butterflies_subset) used."
16
+ )
17
+ st.sidebar.caption(f"*Disclaimers:*")
18
+ st.sidebar.caption(
19
+ "* This demo is a simplified version of the one created by [Ceyda Cinarel](https://github.com/cceyda) and [Jonathan Whitaker](https://datasciencecastnet.home.blog/) ([link](https://huggingface.co/spaces/huggan/butterfly-gan)) during the hackathon [HugGan](https://github.com/huggingface/community-events)."
20
+ )
21
+ st.sidebar.caption(
22
+ "* Model based on [paper](https://openreview.net/forum?id=1Fqg133qRaI) *Towards Faster and Stabilized GAN Training for High-fidelity Few-shot Image Synthesis*."
23
+ )
24
+
25
+ ## Load model
26
+ repo_id = "ceyda/butterfly_cropped_uniq1K_512"
27
+ version_model = "57d36a15546909557d9f967f47713236c8288838"
28
+ model_gan = load_model(repo_id, version_model)
29
+
30
+
31
+ n_mariposas = 4
32
+
33
+ ## Function that generates butterflies and saves it as a state of the session
34
+ def run():
35
+ with st.spinner("Generating, wait a little..."):
36
+ ims = generate(model_gan, n_mariposas)
37
+ st.session_state["ims"] = ims
38
+
39
+
40
+ if "ims" not in st.session_state:
41
+ st.session_state["ims"] = None
42
+ run()
43
+
44
+ ims = st.session_state["ims"]
45
+
46
+
47
+ run_boton = st.button(
48
+ "Spawn butterflies.",
49
+ on_click=run,
50
+ help="Generate images.",
51
+ )
52
+
53
+ if ims is not None:
54
+ cols = st.columns(n_mariposas)
55
+ for j, im in enumerate(ims):
56
+ i = j % n_mariposas
57
+ cols[i].image(im, use_column_width=True)
assets/logo.png ADDED
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ git+https://github.com/huggingface/community-events.git@3fea10c5d5a50c69f509e34cd580fe9139905d04#egg=huggan
2
+ transformers
3
+ faiss-cpu
4
+ paddlehub
5
+ paddlepaddle
utils.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import torch
3
+ from huggan.pytorch.lightweight_gan.lightweight_gan import LightweightGAN
4
+
5
+ def load_model(model_name="ceyda/butterfly_cropped_uniq1K_512", model_version=None):
6
+ gan = LightweightGAN.from_pretrained(model_name, version=model_version)
7
+ gan.eval()
8
+ return gan
9
+
10
+
11
+ def generate(gan, batch_size=1):
12
+ with torch.no_grad():
13
+ ims = gan.G(torch.randn(batch_size, gan.latent_dim)).clamp_(0.0, 1.0) * 255
14
+ ims = ims.permute(0, 2, 3, 1).detach().cpu().numpy().astype(np.uint8)
15
+ return ims