def __getitem__(self,idx): #if index % 2 == 0: img0 = cv2.imread(self._image_paths[idx][0], 0) img1 = cv2.imread(self._image_paths[idx][1], 0) img0 = Image.fromarray(img0) img1 = Image.fromarray(img1) if self._mode == 'train': img0 = GetTransforms(img0, type=self.cfg.use_transforms_type) img1 = GetTransforms(img1, type=self.cfg.use_transforms_type) img0 = np.array(img0) img1 = np.array(img1) img0 = transform(img0, self.cfg) img1 = transform(img1, self.cfg) labels = np.array(self._labels[idx]).astype(np.float32) #print(self._image_paths[index][0],self._image_paths[index][1],labels) img0 = torch.from_numpy(img0).float() img1 = torch.from_numpy(img1).float() labels = torch.from_numpy(labels).float() if self._mode == 'train' or self._mode == 'dev': return (img0, img1, labels) else: raise Exception('Unknown mode : {}'.format(self._mode)) #return img0, img1 , torch.from_numpy(np.array([int(self.training_df.iat[index,2])],dtype=np.float32)) return img0, img1 , labels
def predict_from_file(self, image_file): """Run prediction from image path Args: image_file (str): image path Returns: numpy array: model prediction in numpy array type """ image_gray = cv2.imread(image_file, 0) image = transform(image_gray, self.cfg) image = torch.from_numpy(image) image = image.unsqueeze(0) return tensor2numpy(nn.Sigmoid()(self.predict(image)))
def image_reader(self, image_file): """ Args: image_file: str to an image file path Returns: image: (1, C, H, W) tensor of image image_color: (H, W, C) numpy array of RGB image """ image_gray = cv2.imread(image_file, 0) assert image_gray is not None, "invalid image read in: {}"\ .format(image_file) image_color = cv2.cvtColor(image_gray, cv2.COLOR_GRAY2RGB) image = transform(image_gray, self.cfg) image = torch.from_numpy(image) image = image.unsqueeze(0) return image, image_color
def __getitem__(self, idx): image = cv2.imread(self._image_paths[idx], 0) image = Image.fromarray(image) if self._mode == 'train': image = GetTransforms(image, type=self.cfg.use_transforms_type) image = np.array(image) image = transform(image, self.cfg) labels = np.array(self._labels[idx]).astype(np.float32) path = self._image_paths[idx] if self._mode == 'train' or self._mode == 'dev': return (image, labels) elif self._mode == 'test': return (image, path) elif self._mode == 'heatmap': return (image, path, labels) else: raise Exception('Unknown mode : {}'.format(self._mode))
def __getitem__(self, idx): image = cv2.imread(self._image_paths[idx], 0) # reading images in grayscale image = Image.fromarray(image) # converting it to pil image if self._mode == 'train': # No TTA used image = GetTransforms(image, type=self.cfg.use_transforms_type) image = np.array(image) image = transform(image, self.cfg) # this is happening for test time as well labels = np.array(self._labels[idx]).astype(np.float32) path = self._image_paths[idx] if self._mode == 'train' or self._mode == 'dev': return (image, labels) elif self._mode == 'test': return (image, path) elif self._mode == 'heatmap': return (image, path, labels) else: raise Exception('Unknown mode : {}'.format(self._mode))
def __getitem__(self, index): #if index % 2 == 0: #print(index) if (index % 4 == 0): while True: line0 = random.choice(self.lines) fields0 = line0.strip('\n').split(',') if self.dict[0].get(fields0[15]) == '0': break while True: line1 = random.choice(self.lines) fields1 = line1.strip('\n').split(',') if self.dict[0].get(fields1[15]) == '1': break self._labels.append(1) elif (index % 4 == 1): while True: line0 = random.choice(self.lines) fields0 = line0.strip('\n').split(',') if self.dict[0].get(fields0[15]) == '1': break while True: line1 = random.choice(self.lines) fields1 = line1.strip('\n').split(',') if self.dict[0].get(fields1[15]) == '0': break self._labels.append(1) elif (index % 4 == 2 or index % 4 == 3): while True: line0 = random.choice(self.lines) fields0 = line0.strip('\n').split(',') if self.dict[0].get(fields0[15]) == '1': break while True: line1 = random.choice(self.lines) fields1 = line1.strip('\n').split(',') if self.dict[0].get(fields1[15]) == '1': break self._labels.append(0) ''' if should_get_same_class: while True: line1 = random.choice(self.lines) fields1 = line1.strip('\n').split(',') if self.dict[0].get(fields0[7]) == self.dict[0].get(fields1[7]): break else: line1 = random.choice(self.lines) fields1 = line1.strip('\n').split(',') ''' image_path = fields0[0] image_path = "/kaggle/input/chexpert/" + image_path[21:] img0 = cv2.imread(image_path, 0) image_path = fields1[0] image_path = "/kaggle/input/chexpert/" + image_path[21:] img1 = cv2.imread(image_path, 0) img0 = Image.fromarray(img0) img1 = Image.fromarray(img1) if self._mode == 'train': img0 = GetTransforms(img0, type=self.cfg.use_transforms_type) img1 = GetTransforms(img1, type=self.cfg.use_transforms_type) img0 = np.array(img0) img1 = np.array(img1) img0 = transform(img0, self.cfg) img1 = transform(img1, self.cfg) labels = np.array(self._labels[0]).astype(np.float32) #print(self._image_paths[index][0],self._image_paths[index][1],labels) img0 = torch.from_numpy(img0).float() img1 = torch.from_numpy(img1).float() labels = torch.from_numpy(labels).float() if self._mode == 'train' or self._mode == 'dev': return (img0, img1, labels) else: raise Exception('Unknown mode : {}'.format(self._mode)) #return img0, img1 , torch.from_numpy(np.array([int(self.training_df.iat[index,2])],dtype=np.float32)) return img0, img1, labels