Views
No views yet
1from huggingface_hub import hf_hub_download
2import torch
3import torch.nn as nn
4
5# Define the model architecture
6class DuckNet(nn.Module):
7 def __init__(self, img_size=(256, 256), num_classes=3):
8 super(DuckNet, self).__init__()
9 # ... (model definition)
10
11 def forward(self, x):
12 # ... (forward pass)
13 return torch.sigmoid(output)
14
15# Download and load model
16model_path = hf_hub_download(
17 repo_id="ibrahim313/ducknet-polyp-segmentation",
18 filename="pytorch_model.bin"
19)
20
21model = DuckNet(img_size=(256, 256), num_classes=3)
22model.load_state_dict(torch.load(model_path, map_location='cpu'))
23model.eval()
24
25# Inference
26import albumentations as A
27from albumentations.pytorch import ToTensorV2
28
29transform = A.Compose([
30 A.Resize(256, 256),
31 A.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
32 ToTensorV2()
33])
34
35# Process image
36transformed = transform(image=your_image)
37input_tensor = transformed['image'].unsqueeze(0)
38
39with torch.no_grad():
40 prediction = model(input_tensor)
41 binary_mask = (prediction > 0.5).float()1import cv2
2import numpy as np
3from PIL import Image
4
5def predict_polyp(image_path, model, threshold=0.5):
6 # Load image
7 image = cv2.imread(image_path)
8 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
9
10 # Preprocess
11 transformed = transform(image=image)
12 input_tensor = transformed['image'].unsqueeze(0)
13
14 # Predict
15 model.eval()
16 with torch.no_grad():
17 prediction = model(input_tensor)
18 binary_mask = (prediction > threshold).float()
19
20 return binary_mask.squeeze().numpy()1@misc{ducknet_polyp_2024,
2 title={Duck-Net for Polyp Segmentation in Colonoscopy Images},
3 author={Ibrahim313},
4 year={2024},
5 howpublished={Hugging Face Model Hub},
6 url={https://huggingface.co/ibrahim313/ducknet-polyp-segmentation}
7}| Specification | Value |
|---|---|
| Input Resolution | 256×256 |
| Input Channels | 3 (RGB) |
| Output Channels | 3 (Multi-class) |
| Model Size | ~29.6 MB |
| Parameters | 7,766,051 |
| Inference Time | <1 second (CPU) |
| Memory Usage | ~2GB (inference) |