亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home Backend Development Python Tutorial CocoDetection in PyTorch (3)

CocoDetection in PyTorch (3)

Jan 08, 2025 pm 02:13 PM

Buy Me a Coffee?

*Memos:

  • My post explains CocoDetection() using train2014 with captions_train2014.json, instances_train2014.json and person_keypoints_train2014.json, val2014 with captions_val2014.json, instances_val2014.json and person_keypoints_val2014.json and test2017 with image_info_test2014.json, image_info_test2015.json and image_info_test-dev2015.json.
  • My post explains CocoDetection() using train2017 with captions_train2017.json, instances_train2017.json and person_keypoints_train2017.json, val2017 with captions_val2017.json, instances_val2017.json and person_keypoints_val2017.json and test2017 with image_info_test2017.json and image_info_test-dev2017.json.
  • My post explains MS COCO.

CocoDetection() can use MS COCO dataset as shown below. *This is for train2017 with stuff_train2017.json, val2017 with stuff_val2017.json, stuff_train2017_pixelmaps with stuff_train2017.json, stuff_val2017_pixelmaps with stuff_val2017.json, panoptic_train2017 with panoptic_train2017.json, panoptic_val2017 with panoptic_val2017.json and unlabeled2017 with image_info_unlabeled2017.json:

from torchvision.datasets import CocoDetection

stf_train2017_data = CocoDetection(
    root="data/coco/imgs/train2017",
    annFile="data/coco/anns/stuff_trainval2017/stuff_train2017.json"
)

stf_val2017_data = CocoDetection(
    root="data/coco/imgs/val2017",
    annFile="data/coco/anns/stuff_trainval2017/stuff_val2017.json"
)

len(stf_train2017_data), len(stf_val2017_data)
# (118287, 5000)

# pms_stf_train2017_data = CocoDetection(
#     root="data/coco/anns/stuff_trainval2017/stuff_train2017_pixelmaps",
#     annFile="data/coco/anns/stuff_trainval2017/stuff_train2017.json"
# ) # Error

# pms_stf_val2017_data = CocoDetection(
#     root="data/coco/anns/stuff_trainval2017/stuff_val2017_pixelmaps",
#     annFile="data/coco/anns/stuff_trainval2017/stuff_val2017.json"
# ) # Error

# pan_train2017_data = CocoDetection(
#     root="data/coco/anns/panoptic_trainval2017/panoptic_train2017",
#     annFile="data/coco/anns/panoptic_trainval2017/panoptic_train2017.json"
# ) # Error

# pan_val2017_data = CocoDetection(
#     root="data/coco/anns/panoptic_trainval2017/panoptic_val2017",
#     annFile="data/coco/anns/panoptic_trainval2017/panoptic_val2017.json"
# ) # Error

unlabeled2017_data = CocoDetection(
    root="data/coco/imgs/unlabeled2017",
    annFile="data/coco/anns/unlabeled2017/image_info_unlabeled2017.json"
)

len(unlabeled2017_data)
# 123403

stf_train2017_data[2]
# (<PIL.Image.Image image mode=RGB size=640x428>,
#  [{'segmentation': {'counts': 'W2a0S2Q1T7mNmHS1R7mN...0100000000',
#    'size': [428, 640]}, 'area': 112666.0, 'iscrowd': 0, 'image_id': 30, 
#    'bbox': [0.0, 0.0, 640.0, 321.0], 'category_id': 119, 'id': 10000010},
#   {'segmentation': ..., 'category_id': 124, 'id': 10000011},
#   ...
#   {'segmentation': ..., 'category_id': 183, 'id': 10000014}])

stf_train2017_data[47]
# (<PIL.Image.Image image mode=RGB size=640x427>,
#  [{'segmentation': {'counts': '\\j1h0[<a0G2N001O0...00001O0000',
#    'size': [427, 640]}, 'area': 65213.0, 'iscrowd': 0, 'image_id': 294,
#    'bbox': [140.0, 0.0, 500.0, 326.0], 'category_id': 98, 'id': 10000284}, 
#   {'segmentation': ..., 'category_id': 123, 'id': 10000285},
#   ...
#   {'segmentation': ..., 'category_id': 183, 'id': 10000291}])

stf_train2017_data[64]
# (<PIL.Image.Image image mode=RGB size=480x640>,
#  [{'segmentation': {'counts': '0[9e:1O000000O100000...O5mc0F^Zj7',
#    'size': [640, 480]}, 'area': 20503.0, 'iscrowd': 0, 'image_id': 370,
#    'bbox': [0.0, 0.0, 79.0, 316.0], 'category_id': 102, 'id': 10000383},
#   {'segmentation': ..., 'category_id': 105, 'id': 10000384},
#   ...
#   {'segmentation': ..., 'category_id': 183, 'id': 10000389}])

stf_val2017_data[2]
# (<PIL.Image.Image image mode=RGB size=640x483>,
#  [{'segmentation': {'counts': '\9g5]9O1O1O;EU1kNU1...VMKQ?NY`d3',
#    'size': [483, 640]}, 'area': 5104.0, 'iscrowd': 0, 'image_id': 632,
#    'bbox': [0.0, 300.0, 392.0, 183.0], 'category_id': 93, 'id': 20000017},
#   {'segmentation': ..., 'category_id': 128, 'id': 20000018},
#   ...
#   {'segmentation': ..., 'category_id': 183, 'id': 20000020}])

stf_val2017_data[47]
# (<PIL.Image.Image image mode=RGB size=640x480>,
#  [{'segmentation': {'counts': '[da7T1X>D3M2J5M4M4LoQg1',
#    'size': [480, 640]}, 'area': 122.0, 'iscrowd': 0, 'image_id': 5001,
#    'bbox': [515.0, 235.0, 7.0, 36.0], 'category_id': 104, 'id': 20000247},
#   {'segmentation': ..., 'category_id': 105, 'id': 20000248},
#   ...
#   {'segmentation': ..., 'category_id': 183, 'id': 20000256}])

stf_val2017_data[64]
# (<PIL.Image.Image image mode=RGB size=640x483>,
#  [{'segmentation': {'counts': 'U<^1W>N020mN]B2e>N1O...Mb@N^?2hd2',
#    'size': [500, 375]}, 'area': 2404.0, 'iscrowd': 0, 'image_id': 6763,
#    'bbox': [0.0, 235.0, 369.0, 237.0], 'category_id': 105, 'id': 20000356},
#   {'segmentation': ..., 'category_id': 123, 'id': 20000357},
#   ...
#   {'segmentation': ..., 'category_id': 183, 'id': 20000362}])

unlabeled2017_data[2]
# (<PIL.Image.Image image mode=RGB size=640x427>, [])

unlabeled2017_data[47]
# (<PIL.Image.Image image mode=RGB size=428x640>, [])

unlabeled2017_data[64]
# (<PIL.Image.Image image mode=RGB size=640x480>, [])

import matplotlib.pyplot as plt
from matplotlib.patches import Polygon, Rectangle
import numpy as np
from pycocotools import mask

# `show_images1()` doesn't work very well for the images with
# segmentations so for it, use `show_images2()` which
# more uses the original coco functions. 
def show_images1(data, ims, main_title=None):
    file = data.root.split('/')[-1]
    fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(14, 8))
    fig.suptitle(t=main_title, y=0.9, fontsize=14)
    for i, axis in zip(ims, axes.ravel()):
        if data[i][1] and "segmentation" in data[i][1][0]:
            im, anns = data[i]
            axis.imshow(X=im)
            axis.set_title(label=anns[0]["image_id"])
            ec = ['g', 'r', 'c', 'm', 'y', 'w']
            ec_index = 0
            for ann in anns:
                seg = ann['segmentation']
                compressed_rld = mask.decode(rleObjs=seg)
                y_plts, x_plts = np.nonzero(a=np.squeeze(a=compressed_rld))
                axis.plot(x_plts, y_plts, alpha=0.4)
                x, y, w, h = ann['bbox']
                rect = Rectangle(xy=(x, y), width=w, height=h,
                                 linewidth=3, edgecolor=ec[ec_index],
                                 facecolor='none', zorder=2)
                ec_index += 1
                if ec_index == len(ec)-1:
                    ec_index = 0
                axis.add_patch(p=rect)
        elif not data[i][1]:
            im, _ = data[i]
            axis.imshow(X=im)
    fig.tight_layout()
    plt.show()

ims = (2, 47, 64)

show_images1(data=stf_train2017_data, ims=ims,
             main_title="stf_train2017_data")
show_images1(data=stf_val2017_data, ims=ims, 
             main_title="stf_val2017_data")
show_images1(data=unlabeled2017_data, ims=ims,
             main_title="unlabeled2017_data")

def show_images2(data, index, main_title=None):
    img_set = data[index]
    img, img_anns = img_set
    if img_anns and "segmentation" in img_anns[0]:
        img_id = img_anns[0]['image_id']
        coco = data.coco
        def show_image(imgIds, areaRng=[],
                       iscrowd=None, draw_bbox=False):
            plt.figure(figsize=(11, 8))
            plt.imshow(X=img)
            plt.suptitle(t=main_title, y=1, fontsize=14)
            plt.title(label=img_id, fontsize=14)
            anns_ids = coco.getAnnIds(imgIds=img_id,
                                      areaRng=areaRng, iscrowd=iscrowd)
            anns = coco.loadAnns(ids=anns_ids)
            coco.showAnns(anns=anns, draw_bbox=draw_bbox)
            plt.show()
        show_image(imgIds=img_id, draw_bbox=True)
        show_image(imgIds=img_id, draw_bbox=False)
        show_image(imgIds=img_id, iscrowd=False, draw_bbox=True)
        show_image(imgIds=img_id, areaRng=[0, 5000], draw_bbox=True)
    elif not img_anns:
        plt.figure(figsize=(11, 8))
        plt.imshow(X=img)
        plt.suptitle(t=main_title, y=1, fontsize=14)
        plt.show()

show_images2(data=stf_val2017_data, index=47, 
             main_title="stf_train2017_data")

show_images1():

Image description

Image description

Image description

show_images2():

Image description

Image description

Image description

Image description

The above is the detailed content of CocoDetection in PyTorch (3). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
Polymorphism in python classes Polymorphism in python classes Jul 05, 2025 am 02:58 AM

Polymorphism is a core concept in Python object-oriented programming, referring to "one interface, multiple implementations", allowing for unified processing of different types of objects. 1. Polymorphism is implemented through method rewriting. Subclasses can redefine parent class methods. For example, the spoke() method of Animal class has different implementations in Dog and Cat subclasses. 2. The practical uses of polymorphism include simplifying the code structure and enhancing scalability, such as calling the draw() method uniformly in the graphical drawing program, or handling the common behavior of different characters in game development. 3. Python implementation polymorphism needs to satisfy: the parent class defines a method, and the child class overrides the method, but does not require inheritance of the same parent class. As long as the object implements the same method, this is called the "duck type". 4. Things to note include the maintenance

Explain Python generators and iterators. Explain Python generators and iterators. Jul 05, 2025 am 02:55 AM

Iterators are objects that implement __iter__() and __next__() methods. The generator is a simplified version of iterators, which automatically implement these methods through the yield keyword. 1. The iterator returns an element every time he calls next() and throws a StopIteration exception when there are no more elements. 2. The generator uses function definition to generate data on demand, saving memory and supporting infinite sequences. 3. Use iterators when processing existing sets, use a generator when dynamically generating big data or lazy evaluation, such as loading line by line when reading large files. Note: Iterable objects such as lists are not iterators. They need to be recreated after the iterator reaches its end, and the generator can only traverse it once.

How to handle API authentication in Python How to handle API authentication in Python Jul 13, 2025 am 02:22 AM

The key to dealing with API authentication is to understand and use the authentication method correctly. 1. APIKey is the simplest authentication method, usually placed in the request header or URL parameters; 2. BasicAuth uses username and password for Base64 encoding transmission, which is suitable for internal systems; 3. OAuth2 needs to obtain the token first through client_id and client_secret, and then bring the BearerToken in the request header; 4. In order to deal with the token expiration, the token management class can be encapsulated and automatically refreshed the token; in short, selecting the appropriate method according to the document and safely storing the key information is the key.

Explain Python assertions. Explain Python assertions. Jul 07, 2025 am 12:14 AM

Assert is an assertion tool used in Python for debugging, and throws an AssertionError when the condition is not met. Its syntax is assert condition plus optional error information, which is suitable for internal logic verification such as parameter checking, status confirmation, etc., but cannot be used for security or user input checking, and should be used in conjunction with clear prompt information. It is only available for auxiliary debugging in the development stage rather than substituting exception handling.

How to iterate over two lists at once Python How to iterate over two lists at once Python Jul 09, 2025 am 01:13 AM

A common method to traverse two lists simultaneously in Python is to use the zip() function, which will pair multiple lists in order and be the shortest; if the list length is inconsistent, you can use itertools.zip_longest() to be the longest and fill in the missing values; combined with enumerate(), you can get the index at the same time. 1.zip() is concise and practical, suitable for paired data iteration; 2.zip_longest() can fill in the default value when dealing with inconsistent lengths; 3.enumerate(zip()) can obtain indexes during traversal, meeting the needs of a variety of complex scenarios.

What are Python type hints? What are Python type hints? Jul 07, 2025 am 02:55 AM

TypehintsinPythonsolvetheproblemofambiguityandpotentialbugsindynamicallytypedcodebyallowingdeveloperstospecifyexpectedtypes.Theyenhancereadability,enableearlybugdetection,andimprovetoolingsupport.Typehintsareaddedusingacolon(:)forvariablesandparamete

What are python iterators? What are python iterators? Jul 08, 2025 am 02:56 AM

InPython,iteratorsareobjectsthatallowloopingthroughcollectionsbyimplementing__iter__()and__next__().1)Iteratorsworkviatheiteratorprotocol,using__iter__()toreturntheiteratorand__next__()toretrievethenextitemuntilStopIterationisraised.2)Aniterable(like

Python FastAPI tutorial Python FastAPI tutorial Jul 12, 2025 am 02:42 AM

To create modern and efficient APIs using Python, FastAPI is recommended; it is based on standard Python type prompts and can automatically generate documents, with excellent performance. After installing FastAPI and ASGI server uvicorn, you can write interface code. By defining routes, writing processing functions, and returning data, APIs can be quickly built. FastAPI supports a variety of HTTP methods and provides automatically generated SwaggerUI and ReDoc documentation systems. URL parameters can be captured through path definition, while query parameters can be implemented by setting default values ??for function parameters. The rational use of Pydantic models can help improve development efficiency and accuracy.

See all articles