Home Backend Development Python Tutorial OxfordIIITPet in PyTorch

OxfordIIITPet in PyTorch

Dec 22, 2024 pm 06:42 PM

Buy Me a Coffee☕

*My post explains Oxford-IIIT Pet.

OxfordIIITPet() can use Oxford-IIIT Pet dataset as shown below:

*Memos:

  • The 1st argument is root(Required-Type:str or pathlib.Path). *An absolute or relative path is possible.
  • The 2nd argument is split(Optional-Default:"train"-Type:str). *"trainval"(3,680 images) or "test"(3,669 images) can be set to it.
  • The 3rd argument is target_types(Optional-Default:"attr"-Type:str or list of str): *Memos:
    • "category", "binary-category" and/or "segmentation" can be set to it: *Memos:
    • "category" is for the label from 37 categories(classes).
    • "binary-category" is for the label of cat(0) or dog(1).
    • "segmentation" is for a segmentation trimap image.
    • An empty tuple or list can also be set to it.
    • The multiple same values can be set to it.
    • If the order of values is different, the order of their elements is also different.
  • The 4th argument is transform(Optional-Default:None-Type:callable).
  • The 5th argument is target_transform(Optional-Default:None-Type:callable).
  • The 6th argument is download(Optional-Default:False-Type:bool): *Memos:
    • If it's True, the dataset is downloaded from the internet and extracted(unzipped) to root.
    • If it's True and the dataset is already downloaded, it's extracted.
    • If it's True and the dataset is already downloaded and extracted, nothing happens.
    • It should be False if the dataset is already downloaded and extracted because it's faster.
    • You can manually download and extract the dataset(images.tar.gz and annotations.tar.gz) from here to data/oxford-iiit-pet/.
  • About the label from the categories(classes) for the train image indices, Abyssinian(0) is 0~49, American Bulldog(1) is 50~99, American Pit Bull Terrier(2) is 100~149, Basset Hound(3) is 150~199, Beagle(4) is 200~249, Bengal(5) is 250~299, Birman(6) is 300~349, Bombay(7) is 350~398, Boxer(8) is 399~448, British Shorthair(9) is 449~498, etc.
  • About the label from the categories(classes) for the test image indices, Abyssinian(0) is 0~97, American Bulldog(1) is 98~197, American Pit Bull Terrier(2) is 198~297, Basset Hound(3) is 298~397, Beagle(4) is 398~497, Bengal(5) is 498~597, Birman(6) is 598~697, Bombay(7) is 698~785, Boxer(8) is 786~884, British Shorthair(9) is 885~984, etc.
from torchvision.datasets import OxfordIIITPet

trainval_cate_data = OxfordIIITPet(
    root="data"
)

trainval_cate_data = OxfordIIITPet(
    root="data",
    split="trainval",
    target_types="category",
    transform=None,
    target_transform=None,
    download=False
)

trainval_bincate_data = OxfordIIITPet(
    root="data",
    split="trainval",
    target_types="binary-category"
)

test_seg_data = OxfordIIITPet(
    root="data",
    split="test",
    target_types="segmentation"
)

test_empty_data = OxfordIIITPet(
    root="data",
    split="test",
    target_types=[]
)

test_all_data = OxfordIIITPet(
    root="data",
    split="test",
    target_types=["category", "binary-category", "segmentation"]
)

len(trainval_cate_data), len(trainval_bincate_data)
# (3680, 3680)

len(test_seg_data), len(test_empty_data), len(test_all_data)
# (3669, 3669, 3669)

trainval_cate_data
# Dataset OxfordIIITPet
#     Number of datapoints: 3680
#     Root location: data

trainval_cate_data.root
# 'data'

trainval_cate_data._split
# 'trainval'

trainval_cate_data._target_types
# ['category']

print(trainval_cate_data.transform)
# None

print(trainval_cate_data.target_transform)
# None

trainval_cate_data._download
# <bound method OxfordIIITPet._download of Dataset OxfordIIITPet
#     Number of datapoints: 3680
#     Root location: data>

len(trainval_cate_data.classes), trainval_cate_data.classes
# (37,
#  ['Abyssinian', 'American Bulldog', 'American Pit Bull Terrier',
#   'Basset Hound', 'Beagle', 'Bengal', 'Birman', 'Bombay', 'Boxer',
#   'British Shorthair', ..., 'Wheaten Terrier', 'Yorkshire Terrier'])

trainval_cate_data[0]
# (<PIL.Image.Image image mode=RGB size=394x500>, 0)

trainval_cate_data[1]
# (<PIL.Image.Image image mode=RGB size=450x313>, 0)

trainval_cate_data[2]
# (<PIL.Image.Image image mode=RGB size=500x465>, 0)

trainval_bincate_data[0]
# (<PIL.Image.Image image mode=RGB size=394x500>, 0)

trainval_bincate_data[1]
# (<PIL.Image.Image image mode=RGB size=450x313>, 0)

trainval_bincate_data[2]
# (<PIL.Image.Image image mode=RGB size=500x465>, 0)

test_seg_data[0]
# (<PIL.Image.Image image mode=RGB size=300x225>,
#  <PIL.PngImagePlugin.PngImageFile image mode=L size=300x225>)

test_seg_data[1]
# (<PIL.Image.Image image mode=RGB size=300x225>,
#  <PIL.PngImagePlugin.PngImageFile image mode=L size=300x225>)

test_seg_data[2]
# (<PIL.Image.Image image mode=RGB size=229x300>,
#  <PIL.PngImagePlugin.PngImageFile image mode=L size=229x300>)

test_empty_data[0]
# (<PIL.Image.Image image mode=RGB size=300x225>, None)

test_empty_data[1]
# (<PIL.Image.Image image mode=RGB size=300x225>, None)

test_empty_data[2]
# (<PIL.Image.Image image mode=RGB size=229x300>, None)

test_all_data[0]
# (<PIL.Image.Image image mode=RGB size=300x225>,
#  (0, 0, <PIL.PngImagePlugin.PngImageFile image mode=L size=300x225>))

test_all_data[1]
# (<PIL.Image.Image image mode=RGB size=300x225>,
#  (0, 0, <PIL.PngImagePlugin.PngImageFile image mode=L size=300x225>))

test_all_data[2]
# (<PIL.Image.Image image mode=RGB size=229x300>,
#  (0, 0, <PIL.PngImagePlugin.PngImageFile image mode=L size=229x300>))

import matplotlib.pyplot as plt

def show_images(data, ims, main_title=None):
    if len(data._target_types) == 0:      
        plt.figure(figsize=(12, 6))
        plt.suptitle(t=main_title, y=1.0, fontsize=14)
        for i, j in enumerate(ims, start=1):
            plt.subplot(2, 5, i)
            im, _ = data[j]
            plt.imshow(X=im)
    elif len(data._target_types) == 1:
        if data._target_types[0] == "category":
            plt.figure(figsize=(12, 6))
            plt.suptitle(t=main_title, y=1.0, fontsize=14)
            for i, j in enumerate(ims, start=1):
                plt.subplot(2, 5, i)
                im, cate = data[j]
                plt.title(label=cate)
                plt.imshow(X=im)
        elif data._target_types[0] == "binary-category":
            plt.figure(figsize=(12, 6))
            plt.suptitle(t=main_title, y=1.0, fontsize=14)
            for i, j in enumerate(ims, start=1):
                plt.subplot(2, 5, i)
                im, bincate = data[j]
                plt.title(label=bincate)
                plt.imshow(X=im)
        elif data._target_types[0] == "segmentation":
            plt.figure(figsize=(12, 12))
            plt.suptitle(t=main_title, y=1.0, fontsize=14)
            for i, j in enumerate(ims, start=1):
                im, seg = data[j]
                if 1 <= i and i <= 5:
                    plt.subplot(4, 5, i)
                    plt.imshow(X=im)
                    plt.subplot(4, 5, i+5)
                    plt.imshow(X=seg)
                if 6 <= i and i <= 10:
                    plt.subplot(4, 5, i+5)
                    plt.imshow(X=im)
                    plt.subplot(4, 5, i+10)
                    plt.imshow(X=seg)
    elif len(data._target_types) == 3:
        plt.figure(figsize=(12, 12))
        plt.suptitle(t=main_title, y=1.0, fontsize=14)
        for i, j in enumerate(ims, start=1):
            im, (cate, bincate, seg) = data[j]
            if 1 <= i and i <= 5:
                plt.subplot(4, 5, i)
                plt.title(label=f"{cate}, {bincate}")
                plt.imshow(X=im)
                plt.subplot(4, 5, i+5)
                plt.imshow(X=seg)
            if 6 <= i and i <= 10:
                plt.subplot(4, 5, i+5)
                plt.title(label=f"{cate}, {bincate}")
                plt.imshow(X=im)
                plt.subplot(4, 5, i+10)
                plt.imshow(X=seg)
    plt.tight_layout(h_pad=3.0)
    plt.show()

train_ims = (0, 1, 2, 50, 100, 150, 200, 250, 300, 350)
test_ims = (0, 1, 2, 98, 198, 298, 398, 498, 598, 698)

show_images(data=trainval_cate_data, ims=train_ims,
            main_title="trainval_cate_data")
show_images(data=trainval_bincate_data, ims=train_ims, 
            main_title="trainval_bincate_data")
show_images(data=test_seg_data, ims=test_ims,
            main_title="test_seg_data")
show_images(data=test_empty_data, ims=test_ims,
            main_title="test_empty_data")
show_images(data=test_all_data, ims=test_ims,
            main_title="test_all_data")
Copy after login

OxfordIIITPet in PyTorch

OxfordIIITPet in PyTorch

OxfordIIITPet in PyTorch

OxfordIIITPet in PyTorch

OxfordIIITPet in PyTorch

The above is the detailed content of OxfordIIITPet in PyTorch. 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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to get news data bypassing Investing.com's anti-crawler mechanism? How to get news data bypassing Investing.com's anti-crawler mechanism? Apr 02, 2025 am 07:03 AM

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...

See all articles