GithubHelp home page GithubHelp logo

custom dataset about deeplab-pytorch HOT 2 CLOSED

soans1994 avatar soans1994 commented on June 3, 2024
custom dataset

from deeplab-pytorch.

Comments (2)

kazuto1011 avatar kazuto1011 commented on June 3, 2024 3

The following explains how to create the custom dataset class, inheriting libs.datasets.base._BaseDataset. The class has no content in _set_files() and _load_data(), where you need to instantiate them for your case.

def _set_files(self):
"""
Create a file path/image id list.
"""
raise NotImplementedError()
def _load_data(self, image_id):
"""
Load the image and label in numpy.ndarray
"""
raise NotImplementedError()

  1. Assuming a pair of image and label files is named with a unique ID, we then store all the IDs to a list self.files in _set_files(). For instance, CocoStuff164k extracts the IDs from image paths as follows.

def _set_files(self):
# Create data list by parsing the "images" folder
if self.split in ["train2017", "val2017"]:
file_list = sorted(glob(osp.join(self.root, "images", self.split, "*.jpg")))
assert len(file_list) > 0, "{} has no image".format(
osp.join(self.root, "images", self.split)
)
file_list = [f.split("/")[-1].replace(".jpg", "") for f in file_list]
self.files = file_list
else:
raise ValueError("Invalid split name: {}".format(self.split))

  1. Next, we implement _load_data(), which reads and returns a pair of image and label data from the given index of the IDs. Again, CocoStuff164k may help you.

def _load_data(self, index):
# Set paths
image_id = self.files[index]
image_path = osp.join(self.root, "images", self.split, image_id + ".jpg")
label_path = osp.join(self.root, "annotations", self.split, image_id + ".png")
# Load an image and label
image = cv2.imread(image_path, cv2.IMREAD_COLOR).astype(np.float32)
label = cv2.imread(label_path, cv2.IMREAD_GRAYSCALE)
return image_id, image, label

That's all. The custom dataset class can fetch and preprocess the data, as implemented in the super class.

def __getitem__(self, index):
image_id, image, label = self._load_data(index)
if self.augment:
image, label = self._augmentation(image, label)
# Mean subtraction
image -= self.mean_bgr
# HWC -> CHW
image = image.transpose(2, 0, 1)
return image_id, image.astype(np.float32), label.astype(np.int64)

from deeplab-pytorch.

soans1994 avatar soans1994 commented on June 3, 2024

@kazuto1011

thank you very much for the detailed explaination.

from deeplab-pytorch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.