|
from huggingface_hub import from_pretrained_fastai |
|
import gradio as gr |
|
from fastai.basics import * |
|
from fastai.vision import models |
|
from fastai.vision.all import * |
|
from fastai.metrics import * |
|
from fastai.data.all import * |
|
from fastai.callback import * |
|
|
|
|
|
from pathlib import Path |
|
import random |
|
import PIL |
|
import torchvision.transforms as transformss |
|
import torch |
|
from albumentations import * |
|
|
|
def get_y_fn (x): |
|
return Path(str(x).replace("Images","Labels").replace("color","gt").replace(".jpg",".png")) |
|
|
|
def ParentSplitter(x): |
|
return Path(x).parent.name==test_name |
|
|
|
class SegmentationAlbumentationsTransform(ItemTransform): |
|
split_idx = 0 |
|
|
|
def __init__(self, aug): |
|
self.aug = aug |
|
|
|
def encodes(self, x): |
|
img,mask = x |
|
aug = self.aug(image=np.array(img), mask=np.array(mask)) |
|
return PILImage.create(aug["image"]), PILMask.create(aug["mask"]) |
|
|
|
transforms=Compose([HorizontalFlip(p=0.5), |
|
Rotate(p=0.40,limit=10),GridDistortion() |
|
],p=1) |
|
transformPipeline=SegmentationAlbumentationsTransform(transforms) |
|
|
|
class TargetMaskConvertTransform(ItemTransform): |
|
def __init__(self): |
|
pass |
|
def encodes(self, x): |
|
img,mask = x |
|
|
|
|
|
mask = np.array(mask) |
|
|
|
|
|
|
|
mask[mask==255]=1 |
|
|
|
mask[mask==150]=2 |
|
|
|
mask[mask==76]=3 |
|
mask[mask==74]=3 |
|
|
|
mask[mask==29]=4 |
|
mask[mask==25]=4 |
|
|
|
|
|
mask = PILMask.create(mask) |
|
return img, mask |
|
|
|
repo_id = "paascorb/practica3_Segmentation" |
|
|
|
learner = from_pretrained_fastai(repo_id) |
|
|
|
def transform_image(image): |
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
my_transforms = transformss.Compose([transformss.ToTensor(), |
|
transformss.Normalize( |
|
[0.485, 0.456, 0.406], |
|
[0.229, 0.224, 0.225])]) |
|
image_aux = image |
|
return my_transforms(image_aux).unsqueeze(0).to(device) |
|
|
|
def predict(img): |
|
img = PIL.Image.fromarray(img, "RGB") |
|
image = transformss.Resize((480,640))(img) |
|
tensor = transform_image(image=image) |
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
learner.to(device) |
|
with torch.no_grad(): |
|
outputs = learner(tensor) |
|
|
|
outputs = torch.argmax(outputs,1) |
|
mask = np.array(outputs.cpu()) |
|
mask[mask==1]=255 |
|
mask[mask==2]=150 |
|
mask[mask==3]=76 |
|
mask[mask==4]=29 |
|
mask=np.reshape(mask,(480,640)) |
|
return Image.fromarray(mask.astype('uint8')) |
|
|
|
gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(128, 128)), outputs=[gr.outputs.Image(type="pil", label="Predicci贸n")], examples=['color_155.jpg','color_154.jpg']).launch(share=False) |