bonifaciohansel commited on
Commit
9e9024d
·
1 Parent(s): 69de5c3

Implementacion de gan para generar mariposas

Browse files
Files changed (2) hide show
  1. app.py +40 -4
  2. utils.py +14 -0
app.py CHANGED
@@ -1,6 +1,42 @@
1
  import streamlit as st
 
2
 
3
- st.title("Este es mi demo")
4
- st.markdown("Esta es mi descripcion")
5
- x = st.slider("Selecciona un valor")
6
- st.write(x, "el cuadrado es ",x*x)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from utils import carga_modelo, genera
3
 
4
+ ##Pagina principal
5
+ st.title("Generador de mariposas")
6
+ st.write("Este es un modelo Light GAN entrenado y utilizado con platzi")
7
+
8
+ ##barra lateral
9
+ st.sidebar.subheader("Esta mariposa no existe, puedes creerlo ?")
10
+ st.sidebar.image("assets/logo.png", width=200)
11
+ st.sidebar.caption("Demo creado en vivo.")
12
+
13
+ ##Cargamos el modelo
14
+ repo_id = "ceyda/butterfly_cropped_uniq1K_512"
15
+ modelo = carga_modelo(repo_id)
16
+
17
+ #Generamos 4 mariposas
18
+ n_mariposas = 4
19
+
20
+ def corre():
21
+ with st.spinner("Generando, espero un poco..."):
22
+ ims = genera(modelo_gan,n_mariposas)
23
+ st.session_state["ims"] = ims
24
+
25
+ if "ims" not in st.session_state:
26
+ st.session_state["ims"] = None
27
+ corre()
28
+
29
+ ims = st.session_state["ims"]
30
+ corre_boton =st.button(
31
+ "Genera mariposas",
32
+ on_click=corre,
33
+ help = "Estamos en vuelo, abrocha tu cinturon."
34
+ )
35
+
36
+ if ims is not None:
37
+ cols = st.colums(n_mariposas)
38
+ for j, im in enumerate(ims):
39
+ i= j % n_mariposas
40
+ cols[i].image(im,use_colums_width=True)
41
+
42
+
utils.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import torch
3
+ from huggan.pytorch.lightweight_gan.lightweight_gan import LightweightGAN
4
+
5
+ def carga_modelo(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
+ def genera(gan, batch_size = 1):
11
+ with torch.no_grad():
12
+ ims = gan.G(torch.randn(batch_size, gan.latent_dim)).clamp_(0.0,1.0)*255
13
+ ims =ims.permute(0,2,3,1).deatch().cpu().numpy().astype(np.uint8)
14
+ return ims