This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def __getitem__(self, idx): | |
img_name = self.annotations.iloc[idx, 0] | |
tg_height = self.annotations.iloc[idx, 1] if self.rect_training else 640 | |
tg_width = self.annotations.iloc[idx, 2] if self.rect_training else 640 | |
label_path = os.path.join(self.root_directory, "labels", self.annot_folder, img_name[:-4] + ".txt") | |
# to avoid an annoying "UserWarning: loadtxt: Empty input file" | |
with warnings.catch_warnings(): | |
warnings.simplefilter("ignore") | |
labels = np.loadtxt(fname=label_path, delimiter=" ", ndmin=2) | |
# removing annotations with negative values |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if self.bboxes_format == "coco": | |
labels[:, -1] -= 1 # 0-indexing the classes of coco labels (1-80 --> 0-79) | |
labels = np.roll(labels, axis=1, shift=1) | |
# normalized coordinates are scale invariant, hence after resizing the img we don't resize labels | |
labels[:, 1:] = coco_to_yolo_tensors(labels[:, 1:], w0=img.shape[1], h0=img.shape[0]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
img_name = self.annotations.iloc[idx, 0] | |
tg_height = self.annotations.iloc[idx, 1] if self.rect_training else 640 | |
tg_width = self.annotations.iloc[idx, 2] if self.rect_training else 640 | |
# img_name[:-4] to remove the .jpg or .png which are coco img formats | |
label_path = os.path.join(self.root_directory, "labels", self.annot_folder, img_name[:-4] + ".txt") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if self.transform: | |
batch_n = idx // self.bs | |
if batch_n % 2 == 0: | |
self.transform[1].p = 1 | |
else: | |
self.transform[1].p = 0 | |
# albumentations requires bboxes to be (x,y,w,h,class_idx) | |
augmentations = self.transform(image=img, | |
bboxes=np.roll(labels, axis=1, shift=4) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if self.ultralytics_loss: | |
labels = torch.from_numpy(labels) | |
out_bboxes = torch.zeros((labels.shape[0], 6)) | |
if len(labels): | |
out_bboxes[..., 1:] = labels | |
img = img.transpose((2, 0, 1)) | |
img = np.ascontiguousarray(img) | |
return torch.from_numpy(img), out_bboxes if self.ultralytics_loss else labels |
OlderNewer