P-a-l commited on
Commit
d455542
1 Parent(s): 964c6f5

Iiniciado el proyecto de mariposas

Browse files
Files changed (6) hide show
  1. .gitignore +1 -0
  2. app.py +63 -0
  3. assets/app.py +70 -0
  4. packages.txt +1 -0
  5. requirements.txt +5 -0
  6. utils.py +22 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ venv/
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ from utils import carga_modelo, genera
4
+
5
+ ## Página principal
6
+ st.title("Butterfly GAN (GAN de mariposas)")
7
+ st.write(
8
+ "Modelo Light-GAN entrenado con 1000 imágenes de mariposas tomadas de la colección del Museo Smithsonian."
9
+ )
10
+
11
+ ## Barra lateral
12
+ st.sidebar.subheader("¡Esta mariposa no existe! Ni en América Latina 🤯.")
13
+ st.sidebar.image("assets/logo.png", width=200)
14
+ st.sidebar.caption(
15
+ f"[Modelo](https://huggingface.co/ceyda/butterfly_cropped_uniq1K_512) y [Dataset](https://huggingface.co/datasets/huggan/smithsonian_butterflies_subset) usados."
16
+ )
17
+ st.sidebar.caption(f"*Disclaimers:*")
18
+ st.sidebar.caption(
19
+ "* Este demo es una versión simplificada del creado por [Ceyda Cinarel](https://github.com/cceyda) y [Jonathan Whitaker](https://datasciencecastnet.home.blog/) ([link](https://huggingface.co/spaces/huggan/butterfly-gan)) durante el hackathon [HugGan](https://github.com/huggingface/community-events). Cualquier error se atribuye a [Omar Espejel](https://twitter.com/espejelomar)."
20
+ )
21
+ st.sidebar.caption(
22
+ "* Modelo basado en el [paper](https://openreview.net/forum?id=1Fqg133qRaI) *Towards Faster and Stabilized GAN Training for High-fidelity Few-shot Image Synthesis*."
23
+ )
24
+
25
+ ## Cargamos modelo
26
+ repo_id = "ceyda/butterfly_cropped_uniq1K_512"
27
+ version_modelo = "57d36a15546909557d9f967f47713236c8288838"
28
+ modelo_gan = carga_modelo(repo_id, version_modelo)
29
+
30
+ ## Generamos 4 mariposas
31
+ n_mariposas = 4
32
+
33
+ ## Función que genera mariposas y lo guarda como un estado de la sesión
34
+ def corre():
35
+ #bolita e espera que gira indcándote que se está generando
36
+ with st.spinner("Generando, espera un poco..."):
37
+ #mientras generamos nuestras mariposas con el modelo gan
38
+ ims = genera(modelo_gan, n_mariposas)
39
+ #guardamos las imágenes en un state session llamado ims
40
+ st.session_state["ims"] = ims
41
+
42
+
43
+ ## Si no hay una imagen generada entonces generala
44
+ if "ims" not in st.session_state:
45
+ st.session_state["ims"] = None
46
+ corre()
47
+
48
+ ## ims contiene las imágenes generadas
49
+ ims = st.session_state["ims"]
50
+
51
+ ## Si la usuaria da click en el botón entonces corremos la función genera()
52
+ #Creamos un botón
53
+ corre_boton = st.button(
54
+ "Genera mariposas, porfa.",
55
+ on_click=corre,
56
+ help="Estamos en pleno vuelo, puede tardar.",
57
+ )
58
+ #Si no está vacía creamos columnas con n mariposas
59
+ if ims is not None:
60
+ cols = st.columns(n_mariposas)
61
+ for j, im in enumerate(ims):
62
+ i = j % n_mariposas
63
+ cols[i].image(im, use_column_width=True)
assets/app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import numpy as np
4
+ import cv2
5
+ from huggingface_hub import from_pretrained_keras
6
+
7
+ st.header("Segmentación de dientes con rayos X")
8
+
9
+ st.markdown('''
10
+
11
+ Hola estudiantes de Platzi 🚀. Este modelo usan UNet para segmentar imágenes
12
+ de dientos en rayos X. Se utila un modelo de Keras importado con la función
13
+ `huggingface_hub.from_pretrained_keras`. Recuerda que el Hub de Hugging Face está integrado
14
+ con muchas librerías como Keras, scikit-learn, fastai y otras.
15
+
16
+ El modelo fue creado por [SerdarHelli](https://huggingface.co/SerdarHelli/Segmentation-of-Teeth-in-Panoramic-X-ray-Image-Using-U-Net).
17
+
18
+ ''')
19
+
20
+ model_id = "SerdarHelli/Segmentation-of-Teeth-in-Panoramic-X-ray-Image-Using-U-Net"
21
+ model=from_pretrained_keras(model_id)
22
+
23
+ ## Si una imagen tiene más de un canal entonces se convierte a escala de grises (1 canal)
24
+ def convertir_one_channel(img):
25
+ if len(img.shape)>2:
26
+ img= cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
27
+ return img
28
+ else:
29
+ return img
30
+
31
+ def convertir_rgb(img):
32
+ if len(img.shape)==2:
33
+ img= cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)
34
+ return img
35
+ else:
36
+ return img
37
+
38
+
39
+ image_file = st.file_uploader("Sube aquí tu imagen.", type=["png","jpg","jpeg"])
40
+
41
+
42
+ if image_file is not None:
43
+
44
+ img= Image.open(image_file)
45
+
46
+ st.image(img,width=850)
47
+
48
+ img=np.asarray(img)
49
+
50
+ img_cv=convertir_one_channel(img)
51
+ img_cv=cv2.resize(img_cv,(512,512), interpolation=cv2.INTER_LANCZOS4)
52
+ img_cv=np.float32(img_cv/255)
53
+
54
+ img_cv=np.reshape(img_cv,(1,512,512,1))
55
+ prediction=model.predict(img_cv)
56
+ predicted=prediction[0]
57
+ predicted = cv2.resize(predicted, (img.shape[1],img.shape[0]), interpolation=cv2.INTER_LANCZOS4)
58
+ mask=np.uint8(predicted*255)#
59
+ _, mask = cv2.threshold(mask, thresh=0, maxval=255, type=cv2.THRESH_BINARY+cv2.THRESH_OTSU)
60
+ kernel =( np.ones((5,5), dtype=np.float32))
61
+ mask=cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel,iterations=1 )
62
+ mask=cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel,iterations=1 )
63
+ cnts,hieararch=cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
64
+ output = cv2.drawContours(convertir_one_channel(img), cnts, -1, (255, 0, 0) , 3)
65
+
66
+
67
+ if output is not None :
68
+ st.subheader("Segmentación:")
69
+ st.write(output.shape)
70
+ st.image(output,width=850)
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ libgl1
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,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Funciones que ayudarñán en el resto de la app
2
+
3
+ import numpy as mp
4
+ import torch
5
+ from huggan.pytorch.lightweigth_gan.lightweigth_gan import lightweightGAN #par generar imágenes
6
+
7
+ ## Cargamos el modelo desde el Hub de Hugging Face
8
+ def carga_modelo(model_name="ceyda/butterfly_cropped_uniq1K_512", model_version=None):
9
+ #creamos nuestra gan y descargamos el modelo
10
+ gan = LightweightGAN.from_pretrained(model_name, version=model_version)
11
+ #ponemos nuestra gan en forma de evaluacion
12
+ gan.eval()
13
+ return gan
14
+
15
+
16
+ ## Usamos el modelo GAN para generar imágenes, y le decimos que el tamaño será 1
17
+ def genera(gan, batch_size=1):
18
+ #utilizando pytorch vamos a llamar a nuestra gan
19
+ with torch.no_grad():
20
+ ims = gan.G(torch.randn(batch_size, gan.latent_dim)).clamp_(0.0, 1.0) * 255
21
+ ims = ims.permute(0, 2, 3, 1).detach().cpu().numpy().astype(np.uint8)
22
+ return ims