MCPcopy
hub / github.com/WongKinYiu/PyTorch_YOLOv4 / __getitem__

Method __getitem__

utils/datasets.py:827–911  ·  view source on GitHub ↗
(self, index)

Source from the content-addressed store, hash-verified

825 # return self
826
827 def __getitem__(self, index):
828 if self.image_weights:
829 index = self.indices[index]
830
831 hyp = self.hyp
832 mosaic = self.mosaic and random.random() < hyp['mosaic']
833 if mosaic:
834 # Load mosaic
835 #img, labels = load_mosaic(self, index)
836 img, labels = load_mosaic9(self, index)
837 shapes = None
838
839 # MixUp https://arxiv.org/pdf/1710.09412.pdf
840 if random.random() < hyp['mixup']:
841 #img2, labels2 = load_mosaic(self, random.randint(0, len(self.labels) - 1))
842 img2, labels2 = load_mosaic9(self, random.randint(0, len(self.labels) - 1))
843 r = np.random.beta(8.0, 8.0) # mixup ratio, alpha=beta=8.0
844 img = (img * r + img2 * (1 - r)).astype(np.uint8)
845 labels = np.concatenate((labels, labels2), 0)
846
847 else:
848 # Load image
849 img, (h0, w0), (h, w) = load_image(self, index)
850
851 # Letterbox
852 shape = self.batch_shapes[self.batch[index]] if self.rect else self.img_size # final letterboxed shape
853 img, ratio, pad = letterbox(img, shape, auto=False, scaleup=self.augment)
854 shapes = (h0, w0), ((h / h0, w / w0), pad) # for COCO mAP rescaling
855
856 # Load labels
857 labels = []
858 x = self.labels[index]
859 if x.size > 0:
860 # Normalized xywh to pixel xyxy format
861 labels = x.copy()
862 labels[:, 1] = ratio[0] * w * (x[:, 1] - x[:, 3] / 2) + pad[0] # pad width
863 labels[:, 2] = ratio[1] * h * (x[:, 2] - x[:, 4] / 2) + pad[1] # pad height
864 labels[:, 3] = ratio[0] * w * (x[:, 1] + x[:, 3] / 2) + pad[0]
865 labels[:, 4] = ratio[1] * h * (x[:, 2] + x[:, 4] / 2) + pad[1]
866
867 if self.augment:
868 # Augment imagespace
869 if not mosaic:
870 img, labels = random_perspective(img, labels,
871 degrees=hyp['degrees'],
872 translate=hyp['translate'],
873 scale=hyp['scale'],
874 shear=hyp['shear'],
875 perspective=hyp['perspective'])
876
877 # Augment colorspace
878 augment_hsv(img, hgain=hyp['hsv_h'], sgain=hyp['hsv_s'], vgain=hyp['hsv_v'])
879
880 # Apply cutouts
881 # if random.random() < 0.9:
882 # labels = cutout(img, labels)
883
884 nL = len(labels) # number of labels

Callers

nothing calls this directly

Calls 6

xyxy2xywhFunction · 0.90
load_mosaic9Function · 0.85
load_imageFunction · 0.85
letterboxFunction · 0.85
random_perspectiveFunction · 0.85
augment_hsvFunction · 0.85

Tested by

no test coverage detected