Update app.py
Browse files
app.py
CHANGED
@@ -1,146 +1,106 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import random
|
4 |
-
|
5 |
-
import torch
|
6 |
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
else:
|
15 |
-
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
|
16 |
-
pipe = pipe.to(device)
|
17 |
|
18 |
-
|
19 |
-
|
20 |
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
#col-container {
|
48 |
-
margin: 0 auto;
|
49 |
-
max-width: 520px;
|
50 |
-
}
|
51 |
-
"""
|
52 |
-
|
53 |
-
if torch.cuda.is_available():
|
54 |
-
power_device = "GPU"
|
55 |
-
else:
|
56 |
-
power_device = "CPU"
|
57 |
-
|
58 |
-
with gr.Blocks(css=css) as demo:
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
# Text-to-Image Gradio Template
|
63 |
-
Currently running on {power_device}.
|
64 |
-
""")
|
65 |
-
|
66 |
-
with gr.Row():
|
67 |
-
|
68 |
-
prompt = gr.Text(
|
69 |
-
label="Prompt",
|
70 |
-
show_label=False,
|
71 |
-
max_lines=1,
|
72 |
-
placeholder="Enter your prompt",
|
73 |
-
container=False,
|
74 |
-
)
|
75 |
-
|
76 |
-
run_button = gr.Button("Run", scale=0)
|
77 |
-
|
78 |
-
result = gr.Image(label="Result", show_label=False)
|
79 |
-
|
80 |
-
with gr.Accordion("Advanced Settings", open=False):
|
81 |
-
|
82 |
-
negative_prompt = gr.Text(
|
83 |
-
label="Negative prompt",
|
84 |
-
max_lines=1,
|
85 |
-
placeholder="Enter a negative prompt",
|
86 |
-
visible=False,
|
87 |
-
)
|
88 |
-
|
89 |
-
seed = gr.Slider(
|
90 |
-
label="Seed",
|
91 |
-
minimum=0,
|
92 |
-
maximum=MAX_SEED,
|
93 |
-
step=1,
|
94 |
-
value=0,
|
95 |
-
)
|
96 |
-
|
97 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
98 |
-
|
99 |
-
with gr.Row():
|
100 |
-
|
101 |
-
width = gr.Slider(
|
102 |
-
label="Width",
|
103 |
-
minimum=256,
|
104 |
-
maximum=MAX_IMAGE_SIZE,
|
105 |
-
step=32,
|
106 |
-
value=512,
|
107 |
-
)
|
108 |
-
|
109 |
-
height = gr.Slider(
|
110 |
-
label="Height",
|
111 |
-
minimum=256,
|
112 |
-
maximum=MAX_IMAGE_SIZE,
|
113 |
-
step=32,
|
114 |
-
value=512,
|
115 |
-
)
|
116 |
-
|
117 |
-
with gr.Row():
|
118 |
-
|
119 |
-
guidance_scale = gr.Slider(
|
120 |
-
label="Guidance scale",
|
121 |
-
minimum=0.0,
|
122 |
-
maximum=10.0,
|
123 |
-
step=0.1,
|
124 |
-
value=0.0,
|
125 |
-
)
|
126 |
-
|
127 |
-
num_inference_steps = gr.Slider(
|
128 |
-
label="Number of inference steps",
|
129 |
-
minimum=1,
|
130 |
-
maximum=12,
|
131 |
-
step=1,
|
132 |
-
value=2,
|
133 |
-
)
|
134 |
-
|
135 |
-
gr.Examples(
|
136 |
-
examples = examples,
|
137 |
-
inputs = [prompt]
|
138 |
-
)
|
139 |
-
|
140 |
-
run_button.click(
|
141 |
-
fn = infer,
|
142 |
-
inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
143 |
-
outputs = [result]
|
144 |
-
)
|
145 |
-
|
146 |
-
demo.queue().launch()
|
|
|
1 |
+
from huggingface_hub import from_pretrained_fastai
|
2 |
import gradio as gr
|
3 |
+
|
4 |
+
from fastai.vision.all import *
|
5 |
+
|
6 |
+
import torchvision.transforms as transforms
|
7 |
+
import torchvision.transforms as transforms
|
8 |
+
|
9 |
+
from fastai.basics import *
|
10 |
+
from fastai.vision import models
|
11 |
+
from fastai.vision.all import *
|
12 |
+
from fastai.metrics import *
|
13 |
+
from fastai.data.all import *
|
14 |
+
from fastai.callback import *
|
15 |
+
from pathlib import Path
|
16 |
+
|
17 |
import random
|
18 |
+
import PIL
|
|
|
19 |
|
20 |
+
#Definimos las funciones de transformacion que hemos creado en la practica para poder tratar los datos de entrada y que funcione bien
|
21 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
22 |
+
def transform_image(image):
|
23 |
+
my_transforms = transforms.Compose([transforms.ToTensor(),
|
24 |
+
transforms.Normalize(
|
25 |
+
[0.485, 0.456, 0.406],
|
26 |
+
[0.229, 0.224, 0.225])])
|
27 |
+
image_aux = image
|
28 |
+
return my_transforms(image_aux).unsqueeze(0).to(device)
|
29 |
|
30 |
+
class TargetMaskConvertTransform(ItemTransform):
|
31 |
+
def __init__(self):
|
32 |
+
pass
|
33 |
+
def encodes(self, x):
|
34 |
+
img,mask = x
|
|
|
|
|
|
|
35 |
|
36 |
+
#Convertimos a array
|
37 |
+
mask = np.array(mask)
|
38 |
|
39 |
+
mask[(mask!=255) & (mask!=150) & (mask!=76) & (mask!=74) & (mask!=29) & (mask!=25)]=0
|
40 |
+
mask[mask==255]=1
|
41 |
+
mask[mask==150]=2
|
42 |
+
mask[mask==76]=4
|
43 |
+
mask[mask==74]=4
|
44 |
+
mask[mask==29]=3
|
45 |
+
mask[mask==25]=3
|
46 |
|
47 |
+
# Back to PILMask
|
48 |
+
mask = PILMask.create(mask)
|
49 |
+
return img, mask
|
50 |
+
|
51 |
+
from albumentations import (
|
52 |
+
Compose,
|
53 |
+
OneOf,
|
54 |
+
ElasticTransform,
|
55 |
+
GridDistortion,
|
56 |
+
OpticalDistortion,
|
57 |
+
HorizontalFlip,
|
58 |
+
Rotate,
|
59 |
+
Transpose,
|
60 |
+
CLAHE,
|
61 |
+
ShiftScaleRotate
|
62 |
+
)
|
63 |
+
|
64 |
+
def get_y_fn (x):
|
65 |
+
return Path(str(x).replace("Images","Labels").replace("color","gt").replace(".jpg",".png"))
|
66 |
+
|
67 |
+
class SegmentationAlbumentationsTransform(ItemTransform):
|
68 |
+
split_idx = 0
|
69 |
+
|
70 |
+
def __init__(self, aug):
|
71 |
+
self.aug = aug
|
72 |
+
|
73 |
+
def encodes(self, x):
|
74 |
+
img,mask = x
|
75 |
+
aug = self.aug(image=np.array(img), mask=np.array(mask))
|
76 |
+
return PILImage.create(aug["image"]), PILMask.create(aug["mask"])
|
77 |
+
|
78 |
+
#Cargamos el modelo
|
79 |
+
|
80 |
+
repo_id = "jegilj/Practica3"
|
81 |
+
learn = from_pretrained_fastai(repo_id)
|
82 |
+
model = learn.model
|
83 |
+
model = model.cpu()
|
84 |
+
|
85 |
+
|
86 |
+
# Funcion de predicción
|
87 |
+
def predict(img_ruta):
|
88 |
+
img = PIL.Image.fromarray(img_ruta)
|
89 |
+
image = transforms.Resize((480,640))(img)
|
90 |
+
tensor = transform_image(image=image)
|
91 |
+
model.to(device)
|
92 |
+
with torch.no_grad():
|
93 |
+
outputs = model(tensor)
|
94 |
|
95 |
+
outputs = torch.argmax(outputs,1)
|
96 |
+
mask = np.array(outputs.cpu())
|
97 |
+
mask[mask==1]=255
|
98 |
+
mask[mask==2]=150
|
99 |
+
mask[mask==3]=29
|
100 |
+
mask[mask==4]=74
|
101 |
+
mask = np.reshape(mask,(480,640))
|
102 |
+
return Image.fromarray(mask.astype('uint8'))
|
103 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
+
# Creamos la interfaz y la lanzamos.
|
106 |
+
gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(480, 640)), outputs=gr.inputs.Image(shape=(480, 640)), examples=['color_184.jpg','color_189.jpg']).launch(share=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|