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 * import PIL import torchvision.transforms as transforms from huggingface_hub import hf_hub_download hf_hub_download(repo_id="Alesteba/deep_model_03", filename="unet.pth") # load model device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = torch.jit.load("unet.pth") model = model.cpu() def transform_image(image): device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 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) def predict(img): img = PIL.Image.fromarray(img, "RGB") image = transforms.Resize((480,640))(img) tensor = transform_image(image=image) model.to(device) with torch.no_grad(): outputs = model(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')) # repo_id = "YOUR_USERNAME/YOUR_LEARNER_NAME" # repo_id = "Alesteba/deep_model_03" # learner = from_pretrained_fastai(repo_id) # labels = learner.dls.vocab # # Definimos una funciĆ³n que se encarga de llevar a cabo las predicciones # def predict(img): # #img = PILImage.create(img) # pred,pred_idx,probs = learner.predict(img) # return {labels[i]: float(probs[i]) for i in range(len(labels))} # Creamos la interfaz y la lanzamos. gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(128, 128)), outputs=gr.outputs.Label(num_top_classes=3),examples=['color_154.jpg','color_155.jpg']).launch(share=False)