def __getitem__(self, idx): print('image path:', self.images[idx]) ori_image = cv2.imread(self.images[idx]) ori_mask = cv2.imread(self.labels[idx], cv2.IMREAD_GRAYSCALE) train_img, train_mask = crop_resize_data(ori_image, ori_mask) # Encode train_mask = encode_labels(train_mask) sample = [train_img.copy(), train_mask.copy()] if self.transform: sample = self.transform(sample) return sample
def __getitem__(self, item): # read image data img_data = cv2.imread(self.image[item]) gray_data = cv2.imread(self.label[item], cv2.IMREAD_GRAYSCALE) train_img, train_mask = crop_data(img_data, gray_data) # Encode train_mask = encode_labels(train_mask) sample = [train_img, train_mask] if self.transform: sample = self.transform(sample) return sample
def preprocess(self, roi_image, roi_label): roi_image = roi_image[self.offset:, :] roi_label = roi_label[self.offset:, :] train_image = cv2.resize(roi_image, (self.image_size[0], self.image_size[1]), interpolation=cv2.INTER_LINEAR) train_label = cv2.resize(roi_label, (self.image_size[0], self.image_size[1]), interpolation=cv2.INTER_NEAREST) train_label = encode_labels(train_label) train_image = train_image / (255.0 / 2) - 1 return train_image, train_label
def __getitem__(self, item): # 在该函数内部对图像做处理,返回一个单个样本 image = cv2.imread(self.images[item], cv2.IMREAD_COLOR) # print(self.images[item]) label = cv2.imread(self.labels[item], cv2.IMREAD_GRAYSCALE) # 读取灰度图 train_image, train_label = crop_resize_data(image, label) # 标签encode train_label = encode_labels(train_label) sample = [np.copy(train_image), np.copy(train_label)] # verify_labels(train_label) if self.transform is not None: # 需进行数据增强 sample = self.transform(sample) return sample
def __getitem__(self, idx): # print('image path:', self.images[idx]) # print('labels path:', self.labels[idx]) try: ori_image = cv2.imread(self.images[idx]) ori_mask = cv2.imread(self.labels[idx], cv2.IMREAD_GRAYSCALE) # print ('image shape:', ori_image.shape) # print('label shape:', ori_mask.shape) train_img, train_mask = crop_resize_data(ori_image, ori_mask) # Encode train_mask = encode_labels(train_mask) sample = [train_img.copy(), train_mask.copy()] if self.transform: sample = self.transform(sample) return sample except Exception as e: print('rechoice id:', idx) idx = random.choice(range(len(self.images))) return self.__getitem__(idx)
def train_image_gen(train_list, batch_size=128, image_size=(1024, 384), crop_offset=690): # 这里要注意的是opencv读取进来的数据格式是BGR格式,因此需要注意的是如何训练的,最后如何预测 # Arrange all indexes all_batches_index = np.arange(0, len(train_list)) out_images = [] out_masks = [] image_dir = np.array(train_list['Image_Path']) label_dir = np.array(train_list['Label_Path']) while True: # Random shuffle indexes every epoch np.random.shuffle(all_batches_index) for index in all_batches_index: if os.path.exists(image_dir[index]): image_src = cv2.imread(image_dir[index], cv2.IMREAD_COLOR) label_src = cv2.imread(label_dir[index], cv2.IMREAD_GRAYSCALE) roi_image, roi_label = crop_resize_data( image_src, label_src, image_size, crop_offset) # Encode roi_label_encode = encode_labels(roi_label) out_images.append(roi_image) out_masks.append(roi_label_encode) if len(out_images) >= batch_size: # 表示已经满足batch_size的数据量 out_images = np.array(out_images) out_masks = np.array(out_masks) # print(out_images.shape) out_images = out_images[:, :, :, ::-1].transpose( 0, 3, 1, 2).astype(np.float32) / ( 255.0 / 2) - 1 # 首先将BGR模式转换为RGB,再进行维度变换,再将数据缩放至[-1,1] out_masks = out_masks.astype(np.int64) # print(out_images.shape, out_masks.shape) yield out_images, out_masks out_images, out_masks = [], [] else: print(image_dir, 'does not exist.') break
def __getitem__(self, item): label = cv2.imread(self.labels[item], cv2.IMREAD_GRAYSCALE) train_label = encode_labels(label) return train_label