|
import matplotlib.pyplot as plt |
|
import requests, validators |
|
import torch |
|
import pathlib |
|
import numpy as np |
|
from PIL import Image |
|
|
|
from transformers import DetrFeatureExtractor, DetrForSegmentation, MaskFormerImageProcessor, MaskFormerForInstanceSegmentation |
|
|
|
from transformers.image_transforms import rgb_to_id |
|
|
|
TEST_IMAGE = Image.open(r"images/9999999_00783_d_0000358.jpg") |
|
MODEL_NAME_DETR = "facebook/detr-resnet-50-panoptic" |
|
MODEL_NAME_MASKFORMER = "facebook/maskformer-swin-large-coco" |
|
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
|
|
|
|
|
|
|
|
image = TEST_IMAGE |
|
model_name = MODEL_NAME_MASKFORMER |
|
|
|
|
|
|
|
processor = MaskFormerImageProcessor.from_pretrained(model_name) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model = MaskFormerForInstanceSegmentation.from_pretrained(model_name) |
|
|
|
model.to(DEVICE) |
|
|
|
|
|
|
|
inputs = processor(images=image, return_tensors="pt") |
|
|
|
|
|
|
|
|
|
|
|
inputs.to(DEVICE) |
|
|
|
|
|
outputs = model(**inputs) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
results = processor.post_process_panoptic_segmentation(outputs, target_sizes=[image.size[::-1]])[0] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" |
|
>>> model.config.id2label |
|
{0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus', 6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant', 11: 'stop sign', 12: 'parking meter', |
|
13: 'bench', 14: 'bird', 15: 'cat', 16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear', 22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag', |
|
27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard', 32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove', 36: 'skateboard', 37: 'surfboard', 38: 'tennis racket', |
|
39: 'bottle', 40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl', 46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli', 51: 'carrot', 52: 'hot dog', 53: 'pizza', |
|
54: 'donut', 55: 'cake', 56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table', 61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard', 67: 'cell phone', |
|
68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink', 72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors', 77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush', 80: 'banner', 81: 'blanket', |
|
82: 'bridge', 83: 'cardboard', 84: 'counter', 85: 'curtain', 86: 'door-stuff', 87: 'floor-wood', 88: 'flower', 89: 'fruit', 90: 'gravel', 91: 'house', 92: 'light', 93: 'mirror-stuff', 94: 'net', 95: 'pillow', |
|
96: 'platform', 97: 'playingfield', 98: 'railroad', 99: 'river', 100: 'road', 101: 'roof', 102: 'sand', 103: 'sea', 104: 'shelf', 105: 'snow', 106: 'stairs', 107: 'tent', 108: 'towel', 109: 'wall-brick', |
|
110: 'wall-stone', 111: 'wall-tile', 112: 'wall-wood', 113: 'water-other', 114: 'window-blind', 115: 'window-other', 116: 'tree-merged', 117: 'fence-merged', 118: 'ceiling-merged', 119: 'sky-other-merged', |
|
120: 'cabinet-merged', 121: 'table-merged', 122: 'floor-other-merged', 123: 'pavement-merged', 124: 'mountain-merged', 125: 'grass-merged', 126: 'dirt-merged', 127: 'paper-merged', 128: 'food-other-merged', |
|
129: 'building-other-merged', 130: 'rock-merged', 131: 'wall-other-merged', 132: 'rug-merged'} |
|
>>> model.config.id2label[123] |
|
'pavement-merged' |
|
>>> results["segments_info"][1] |
|
{'id': 2, 'label_id': 123, 'was_fused': False, 'score': 0.995813} |
|
""" |
|
|
|
|
|
|
|
""" |
|
>>> Image.fromarray((mask * 255).cpu().numpy().astype(np.uint8)) |
|
<PIL.Image.Image image mode=L size=2000x1500 at 0x7F07773691C0> |
|
>>> temp = Image.fromarray((mask * 255).cpu().numpy().astype(np.uint8)) |
|
""" |
|
|
|
""" |
|
>>> mask = (results["segmentation"].cpu().numpy == 4) |
|
>>> mask = (results["segmentation"].cpu().numpy() == 4) |
|
>>> mask |
|
array([[False, False, False, ..., False, False, False], |
|
[False, False, False, ..., False, False, False], |
|
[False, False, False, ..., False, False, False], |
|
..., |
|
[False, False, False, ..., False, False, False], |
|
[False, False, False, ..., False, False, False], |
|
[False, False, False, ..., False, False, False]]) |
|
>>> visual_mask = (mask * 255).astype(np.uint8) |
|
>>> visual_mask = Image.fromarray(visual_mask) |
|
>>> plt.imshow(visual_mask) |
|
<matplotlib.image.AxesImage object at 0x7f0761e78040> |
|
>>> plt.show() |
|
""" |
|
|
|
""" |
|
>>> mask = (results["segmentation"].cpu().numpy() == 1) |
|
>>> visual_mask = (mask*255).astype(np.uint8) |
|
>>> visual_mask = Image.fromarray(visual_mask) |
|
>>> plt.imshow(visual_mask) |
|
<matplotlib.image.AxesImage object at 0x7f0760298550> |
|
>>> plt.show() |
|
>>> results["segments_info"][0] |
|
{'id': 1, 'label_id': 25, 'was_fused': False, 'score': 0.998022} |
|
>>> |
|
""" |