Instructions to use AndreCosta/resnet-unet-segmentation with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use AndreCosta/resnet-unet-segmentation with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-segmentation", model="AndreCosta/resnet-unet-segmentation")# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("AndreCosta/resnet-unet-segmentation", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download scripts/Segmentation/app.py from AndreCosta/resnet-unet-segmentation: direct link, hf CLI and curl.
- Browser
- Download file 1.86 kB
-
https://huggingface.co/AndreCosta/resnet-unet-segmentation/resolve/main/scripts/Segmentation/app.py
- Command line
-
hf download hf://AndreCosta/resnet-unet-segmentation/scripts/Segmentation/app.py
-
curl -L -o app.py https://huggingface.co/AndreCosta/resnet-unet-segmentation/resolve/main/scripts/Segmentation/app.py
1.86 kB
| import os | |
| import sys | |
| sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))) | |
| import torch | |
| import numpy as np | |
| import gradio as gr | |
| from PIL import Image | |
| import scripts.config as config | |
| import torchvision.transforms as transforms | |
| import scripts.Segmentation.augment as augment | |
| from scripts.Segmentation.models import ResNetUNet | |
| config.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') | |
| modelo = '/best_model.pt' | |
| model = ResNetUNet(num_classes=2).to(config.device) | |
| model.load_state_dict(torch.load(config.checkpoints + modelo, map_location=config.device)) | |
| model.eval() | |
| transform = transforms.Compose([ | |
| transforms.Grayscale(num_output_channels=1), | |
| transforms.Resize((config.height, config.width)), | |
| transforms.ToTensor(), | |
| transforms.Normalize(mean=[0.5], std=[0.5]) | |
| ]) | |
| def segment_image(input_img): | |
| input_img = input_img.convert('L') | |
| img_tensor = transform(input_img).unsqueeze(0).to(config.device) # (1, 1, H, W) | |
| output = None | |
| if config.USE_TTA: | |
| output = augment.predict_with_tta(model, img_tensor) | |
| else: | |
| output = model(img_tensor) | |
| probs = torch.softmax(output, dim=1) # (1, 2, H, W) | |
| mask = torch.argmax(probs, dim=1).squeeze(0) | |
| if config.USE_REFINEMENT: | |
| mask = augment.refine_mask(mask) | |
| mask = mask.cpu().numpy() | |
| mask_img = Image.fromarray((mask * 255).astype(np.uint8)) | |
| return input_img, mask_img | |
| demo = gr.Interface( | |
| fn=segment_image, | |
| inputs=gr.Image(type="pil", label="Input image"), | |
| outputs=[ | |
| gr.Image(type="pil", label="Original image"), | |
| gr.Image(type="pil", label="Segmented mask"), | |
| ], | |
| title="ResNet-UNet Image Segmentator", | |
| description="Send an image and see the segmentation result generated by the trained model." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(share=True) | |