def __getitem__(self, idx): self.get_lane_image(idx) self.img, self.label_img = crop_resize_data(self.img, self.label_img) self.img = CLAHE_nomalization(self.img) self.img = contrast(self.img) self.img, self.label_img = random_filp(self.img, self.label_img) self.label_img = self.label_img * (self.label_img < 9) * (self.label_img >= 0) self.get_ins_img() # cv2.imshow('img', self.img) # cv2.imshow('label', self.label_img) # cv2.imshow('ins', self.ins_img[2]*100) # cv2.waitKey(0) # cv2.destroyAllWindows() if self.flags['train']: # self.img = np.array(np.transpose(self.img, (2,0,1)), dtype=np.float32) self.img = self.transforms(self.img) self.label_img = np.array(self.label_img, dtype=np.float32) self.ins_img = np.array(self.ins_img, dtype=np.float32) return torch.Tensor(self.img), torch.LongTensor( self.label_img), torch.Tensor(self.ins_img) else: self.img = np.array(np.transpose(self.img, (2, 0, 1)), dtype=np.float32) return torch.Tensor(self.img)
def train_image_gen(train_list, batch_size=4, image_size=[1024, 384], crop_offset=690): # Arrange all indexes all_batches_index = np.arange(0, len(train_list)) out_images = [] out_masks = [] image_dir = np.array(train_list['image']) label_dir = np.array(train_list['label']) 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]): ori_image = cv2.imread(image_dir[index]) ori_mask = cv2.imread(label_dir[index], cv2.IMREAD_GRAYSCALE) # Crop the top part of the image # Resize to train size train_img, train_mask = crop_resize_data(ori_image, ori_mask, image_size, crop_offset) # Encode train_mask = encode_labels(train_mask) # verify_labels(train_mask) out_images.append(train_img) out_masks.append(train_mask) if len(out_images) >= batch_size: out_images = np.array(out_images) out_masks = np.array(out_masks) out_images = out_images[:, :, :, ::-1].transpose(0, 3, 1, 2).astype(np.float32) / (255.0 / 2) - 1 out_masks = out_masks.astype(np.int64) yield out_images, out_masks out_images, out_masks = [], [] else: print(image_dir, 'does not exist.')
def img_transform(img): img = crop_resize_data(img) img = np.transpose(img, (2, 0, 1)) img = img[np.newaxis, ...].astype(np.float32) img = torch.from_numpy(img.copy()) if torch.cuda.is_available(): img = img.cuda(device=device_id) return img
def img_transform(img): img = crop_resize_data(img) # 轴 img = np.transpose(img, (2, 0, 1)) # batch的封装,添加了一个新的轴,在前面扩充一维,变成了 1*C*H*W,...不管图片后面哪一维,全都要 img = img[np.newaxis, ...].astype(np.float32) img = torch.from_numpy(img.copy()) if torch.cuda.is_available(): img = img.cuda(device=device_id) return img
def main(): IMG_SIZE =[1536, 512] SUBMISSION_SIZE = [3384, 1710] save_test_logits = False num_classes = 8 batch_size = 4 log_iters = 100 network = 'unet_simple' # Define paths for each model if network == 'deeplabv3p': model_path = "./model_weights/paddle_deeplabv3p_8_end_060223" npy_dir = '/npy_save/deeplabv3p/' elif network == 'unet_base': model_path = "./model_weights/paddle_unet_base_10_end_059909" npy_dir = '/npy_save/unet_base/' elif network == 'unet_simple': model_path = "./model_weights/paddle_unet_simple_12_end_060577" npy_dir = '/npy_save/unet_simple/' program_choice = 2 # 1 - Validtion; 2 - Test show_label = False crop_offset = 690 data_dir = './data_list/val.csv' test_dir = '/root/private/LaneDataSet/TestSet/Image_Data/ColorImage/' sub_dir = './test_submission/' # Get data list and split it into train and validation set. val_list = pd.read_csv(data_dir) iter_id = 0 total_loss = 0.0 total_miou = 0.0 prev_time = time.time() # Validation if program_choice == 1: val_reader = val_image_gen(val_list, batch_size=batch_size, image_size=IMG_SIZE, crop_offset=crop_offset) model = create_network(network=network) model.load_weights(model_path) print("loaded model from: %s" % model_path) print('Start Validation!') for iteration in range(int(len(val_list) / batch_size)): val_data,val_label = next(val_reader) results = model.evaluate(val_data,val_label) if iter_id % log_iters == 0: print('Finished Processing %d Images.' %(iter_id * batch_size)) iter_id += 1 total_loss += np.mean(results[0]) total_miou += np.mean(results[1]) # label to mask if show_label == True: label_image = val_label[0] color_label_mask = decode_color_labels(label_image) color_label_mask = np.transpose(color_label_mask, (1, 2, 0)) cv2.imshow('gt_label', cv2.resize(color_label_mask, (IMG_SIZE[0], IMG_SIZE[1]))) prediction = np.argmax(results[2][0], axis=0) color_pred_mask = decode_color_labels(prediction) color_pred_mask = np.transpose(color_pred_mask, (1, 2, 0)) cv2.imshow('pred_label', cv2.resize(color_pred_mask, (IMG_SIZE[0], IMG_SIZE[1]))) cv2.waitKey(0) end_time = time.time() print("validation loss: %.3f, mean iou: %.3f, time cost: %.3f s" % (total_loss / iter_id, total_miou / iter_id, end_time - prev_time)) # Test elif program_choice == 2: model = create_network(network=network) model.load_weights(model_path) print("loaded model from: %s" % model_path) print('Start Making Submissions!') test_list = os.listdir(test_dir) for test_name in test_list: test_ori_image = cv2.imread(os.path.join(test_dir, test_name)) test_image = crop_resize_data(test_ori_image, label=None, image_size=IMG_SIZE, offset=crop_offset) out_image = np.expand_dims(np.array(test_image), axis=0) out_image = out_image[:, :, :, ::-1].transpose(0, 3, 1, 2).astype(np.float32) / (255.0 / 2) - 1 results_1 = model.evaluate(out_image,None) if iter_id % 20 == 0: print('Finished Processing %d Images.' %(iter_id)) iter_id += 1 prediction = np.argmax(results_1[0][0], axis=0) # Save npy files if save_test_logits == True: np.save(npy_dir + test_name.replace('.jpg', '.npy'), results_1[0][0]) # Save Submission PNG submission_mask = expand_resize_data(prediction, SUBMISSION_SIZE, crop_offset) cv2.imwrite(os.path.join(sub_dir, test_name.replace('.jpg', '.png')), submission_mask) # Show Label if show_label == True: cv2.imshow('test_image', cv2.resize(test_ori_image,(IMG_SIZE[0], IMG_SIZE[1]))) cv2.imshow('pred_label', cv2.resize(submission_mask,(IMG_SIZE[0], IMG_SIZE[1]))) cv2.waitKey(0) sys.stdout.flush()
import os import cv2 import numpy as np import pandas as pd import imgaug as ia from imgaug import augmenters as iaa import albumentations as albu import matplotlib.pyplot as plt from utils.image_process import crop_resize_data data = pd.read_csv(os.path.join(os.getcwd(), "data_list", "train.csv"), header=None, names=["image", "label"]) images = data["image"].values[1:] labels = data["label"].values[1:] ori_image = cv2.imread(images[0]) ori_mask = cv2.imread(labels[0], cv2.IMREAD_GRAYSCALE) ori_image, ori_mask = crop_resize_data(ori_image, ori_mask) seq = iaa.Sequential([iaa.CropAndPad( percent=(-0.05, 0.1))]) seg_to = seq.to_deterministic() for i in range(8): seg_to = seq.to_deterministic() ori_image = seg_to.augment_image(ori_image) # 将方法应用在原图像上 ori_mask = seg_to.augment_image(ori_mask) print(ori_mask.dtype) print(ori_mask.shape) plt.imshow(ori_mask) plt.show()
def main(): IMG_SIZE =[1536, 512] SUBMISSION_SIZE = [3384, 1710] save_test_logits = False num_classes = 8 batch_size = 4 log_iters = 100 network = 'unet_simple' # Define paths for each model if network == 'deeplabv3p': model_path = "./model_weights/paddle_deeplabv3p_8_end_060223" npy_dir = '/npy_save/deeplabv3p/' elif network == 'unet_base': model_path = "./model_weights/paddle_unet_base_10_end_059909" npy_dir = '/npy_save/unet_base/' elif network == 'unet_simple': model_path = "./model_weights/paddle_unet_simple_12_end_060577" npy_dir = '/npy_save/unet_simple/' program_choice = 2 # 1 - Validtion; 2 - Test show_label = False crop_offset = 690 data_dir = './data_list/val.csv' test_dir = '../PaddlePaddle/TestSet_Final/ColorImage/' sub_dir = './test_submission/' # Get data list and split it into train and validation set. val_list = pd.read_csv(data_dir) #Initialization images = fluid.layers.data(name='image', shape=[3, IMG_SIZE[1], IMG_SIZE[0]], dtype='float32') labels = fluid.layers.data(name='label', shape=[1, IMG_SIZE[1], IMG_SIZE[0]], dtype='float32') iter_id = 0 total_loss = 0.0 total_miou = 0.0 prev_time = time.time() # Validation if program_choice == 1: val_reader = val_image_gen(val_list, batch_size=batch_size, image_size=IMG_SIZE, crop_offset=crop_offset) reduced_loss, miou, pred = create_network(images, labels, num_classes, network=network, image_size=(IMG_SIZE[1], IMG_SIZE[0]), for_test=False) place = fluid.CUDAPlace(0) exe = fluid.Executor(place) exe.run(fluid.default_startup_program()) fluid.io.load_params(exe, model_path) print("loaded model from: %s" % model_path) # Parallel Executor to use multi-GPUs exec_strategy = fluid.ExecutionStrategy() exec_strategy.allow_op_delay = True build_strategy = fluid.BuildStrategy() build_strategy.reduce_strategy = fluid.BuildStrategy.ReduceStrategy.Reduce train_exe = fluid.ParallelExecutor(use_cuda=True, build_strategy=build_strategy, exec_strategy=exec_strategy) print('Start Validation!') for iteration in range(int(len(val_list) / batch_size)): val_data = next(val_reader) results = train_exe.run( feed=get_feeder_data(val_data, place), fetch_list=[reduced_loss.name, miou.name, pred.name]) if iter_id % log_iters == 0: print('Finished Processing %d Images.' %(iter_id * batch_size)) iter_id += 1 total_loss += np.mean(results[0]) total_miou += np.mean(results[1]) # label to mask if show_label == True: label_image = val_data[1][0] color_label_mask = decode_color_labels(label_image) color_label_mask = np.transpose(color_label_mask, (1, 2, 0)) cv2.imshow('gt_label', cv2.resize(color_label_mask, (IMG_SIZE[0], IMG_SIZE[1]))) prediction = np.argmax(results[2][0], axis=0) color_pred_mask = decode_color_labels(prediction) color_pred_mask = np.transpose(color_pred_mask, (1, 2, 0)) cv2.imshow('pred_label', cv2.resize(color_pred_mask, (IMG_SIZE[0], IMG_SIZE[1]))) cv2.waitKey(0) end_time = time.time() print("validation loss: %.3f, mean iou: %.3f, time cost: %.3f s" % (total_loss / iter_id, total_miou / iter_id, end_time - prev_time)) # Test elif program_choice == 2: predictions = create_network(images, labels, num_classes, network=network, image_size=(IMG_SIZE[1], IMG_SIZE[0]), for_test=True) place = fluid.CUDAPlace(0) # place = fluid.CPUPlace() exe = fluid.Executor(place) exe.run(fluid.default_startup_program()) fluid.io.load_params(exe, model_path) print("loaded model from: %s" % model_path) print('Start Making Submissions!') test_list = os.listdir(test_dir) for test_name in test_list: test_ori_image = cv2.imread(os.path.join(test_dir, test_name)) test_image = crop_resize_data(test_ori_image, label=None, image_size=IMG_SIZE, offset=crop_offset) out_image = np.expand_dims(np.array(test_image), axis=0) out_image = out_image[:, :, :, ::-1].transpose(0, 3, 1, 2).astype(np.float32) / (255.0 / 2) - 1 feed_dict = {} feed_dict["image"] = out_image results_1 = exe.run( feed=feed_dict, fetch_list=[predictions]) if iter_id % 20 == 0: print('Finished Processing %d Images.' %(iter_id)) iter_id += 1 prediction = np.argmax(results_1[0][0], axis=0) # Save npy files if save_test_logits == True: np.save(npy_dir + test_name.replace('.jpg', '.npy'), results_1[0][0]) # Save Submission PNG submission_mask = expand_resize_data(prediction, SUBMISSION_SIZE, crop_offset) cv2.imwrite(os.path.join(sub_dir, test_name.replace('.jpg', '.png')), submission_mask) # Show Label if show_label == True: cv2.imshow('test_image', cv2.resize(test_ori_image,(IMG_SIZE[0], IMG_SIZE[1]))) cv2.imshow('pred_label', cv2.resize(submission_mask,(IMG_SIZE[0], IMG_SIZE[1]))) cv2.waitKey(0) sys.stdout.flush()