|
|
from torchvision.io.image import read_image
|
|
|
from torchvision.models.segmentation import fcn_resnet50, FCN_ResNet50_Weights
|
|
|
from torchvision.transforms.functional import to_pil_image
|
|
|
|
|
|
img = read_image("gallery/assets/dog1.jpg")
|
|
|
|
|
|
|
|
|
weights = FCN_ResNet50_Weights.DEFAULT
|
|
|
model = fcn_resnet50(weights=weights)
|
|
|
model.eval()
|
|
|
|
|
|
|
|
|
preprocess = weights.transforms()
|
|
|
|
|
|
|
|
|
batch = preprocess(img).unsqueeze(0)
|
|
|
|
|
|
|
|
|
prediction = model(batch)["out"]
|
|
|
normalized_masks = prediction.softmax(dim=1)
|
|
|
class_to_idx = {cls: idx for (idx, cls) in enumerate(weights.meta["categories"])}
|
|
|
mask = normalized_masks[0, class_to_idx["dog"]]
|
|
|
to_pil_image(mask).show() |