Spaces:
Sleeping
Sleeping
#app.py: | |
# from huggingface_hub import from_pretrained_fastai | |
import gradio as gr | |
from fastai.data.block import DataBlock | |
from fastai.data.transforms import get_image_files, FuncSplitter, Normalize | |
from fastai.layers import Mish | |
from fastai.losses import BaseLoss | |
from fastai.optimizer import ranger | |
from fastai.torch_core import tensor | |
from fastai.vision.augment import aug_transforms | |
from fastai.vision.core import PILImage, PILMask | |
from fastai.vision.data import ImageBlock, MaskBlock, imagenet_stats | |
from fastai.vision.learner import unet_learner | |
from PIL import Image | |
import numpy as np | |
from torch import nn | |
import torch | |
import torch.nn.functional as F | |
# # repo_id = "YOUR_USERNAME/YOUR_LEARNER_NAME" | |
repo_id = "islasher/segm-grapes" | |
repo_id='segm-grapes' | |
# # Definimos una funci贸n que se encarga de llevar a cabo las predicciones | |
from fastai.learner import load_learner | |
# # Cargar el modelo y el tokenizador | |
learn = load_learner(repo_id) | |
#learner = from_pretrained_fastai(repo_id) | |
import torchvision.transforms as transforms | |
def transform_image(image): | |
my_transforms = transforms.Compose([transforms.ToTensor(), | |
transforms.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) | |
# Definimos una funci贸n que se encarga de llevar a cabo las predicciones | |
def predict(img): | |
image = transforms.Resize((480,640))(img) | |
tensor = transform_image(image=image) | |
with torch.no_grad(): | |
outputs = learn.model(tensor) | |
outputs = torch.argmax(outputs,1) | |
mask = np.array(outputs) | |
mask[mask==1]=150 | |
mask[mask==3]=76 #pole # y no 74 | |
# mask[mask==5]=74 #pole | |
mask[mask==2]=29 #wood # y no 25 | |
# mask[mask==6]=25 #wood | |
mask[mask==4]=255 #grape | |
mask=np.reshape(mask,(480,640)) #en modo matriz | |
return Image.fromarray(mask.astype('uint8')) | |
# Creamos la interfaz y la lanzamos. | |
gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(128, 128)), outputs=gr.outputs.Image(shape=(480,640)),examples=['color_154.jpg','color_155.jpg']).launch(share=False) |