Beispiel #1
0
def val(args, model, val_img_path, val_label_path, csv_path):
    print('start val!')
    dataset_val = ADE(val_img_path, val_label_path, scale=(args.crop_height, args.crop_width), mode='val')
    dataloader_val = DataLoader(
        dataset_val,
        # this has to be 1
        batch_size=1,
        shuffle=True,
        num_workers=args.num_workers
    )
    label_info = get_label_info(csv_path)
    with torch.no_grad():
        model.eval()
        precision_record = []
        for i, (data, label) in enumerate(dataloader_val):
            if torch.cuda.is_available() and args.use_gpu:
                data = data.cuda()
                label = label.cuda()
            # get RGB predict image
            predict = model(data).squeeze()
            predict = reverse_one_hot(predict)
            predict = colour_code_segmentation(np.array(predict), label_info)  # predict info
            # get RGB label image
            label = label.squeeze()
            label = reverse_one_hot(label)
            label = colour_code_segmentation(np.array(label), label_info)
            # compute per pixel accuracy
            precision = compute_global_accuracy(predict, label)
            precision_record.append(precision)
        dice = np.mean(precision_record)
        print('precision per pixel for validation: %.3f' % dice)
        return dice
Beispiel #2
0
def val(args, model, dataloader, csv_path):
    print('start val!')
    label_info = get_label_info(csv_path)
    with torch.no_grad():
        model.eval()
        precision_record = []
        for i, (data, label) in enumerate(dataloader):
            if torch.cuda.is_available() and args.use_gpu:
                data = data.cuda()
                label = label.cuda()

            # get RGB predict image
            predict = model(data).squeeze()
            predict = reverse_one_hot(predict)
            predict = colour_code_segmentation(np.array(predict), label_info)

            # get RGB label image
            label = label.squeeze()
            label = reverse_one_hot(label)
            label = colour_code_segmentation(np.array(label), label_info)
            # compute per pixel accuracy
            precision = compute_global_accuracy(predict, label)
            precision_record.append(precision)
        dice = np.mean(precision_record)
        print('precision per pixel for validation: %.3f' % dice)
        return dice
Beispiel #3
0
def eval(model, dataloader, args, label_info):
    print('start test!')
    with torch.no_grad():
        model.eval()
        precision_record = []
        tq = tqdm.tqdm(total=len(dataloader) * args.batch_size)
        tq.set_description('test')
        for i, (data, label) in enumerate(dataloader):
            tq.update(args.batch_size)
            if torch.cuda.is_available() and args.use_gpu:
                data = data.cuda()
                label = label.cuda()
            predict = model(data).squeeze()
            predict = reverse_one_hot(predict)
            predict = colour_code_segmentation(np.array(predict), label_info)

            label = label.squeeze()
            label = reverse_one_hot(label)
            label = colour_code_segmentation(np.array(label), label_info)

            precision = compute_global_accuracy(predict, label)
            precision_record.append(precision)
        precision = np.mean(precision_record)
        tq.close()
        print('precision for test: %.3f' % precision)
        return precision
Beispiel #4
0
def eval(model, dataset, args, label_info):
    dataloader = DataLoader(
        dataset,
        batch_size=1,
        shuffle=False,
        num_workers=4,
    )
    print('start test!')
    with torch.no_grad():
        model.eval()
        precision_record = []
        tq = tqdm.tqdm(total=len(dataloader) * args.batch_size)
        tq.set_description('test')
        for i, (data, label) in enumerate(dataloader):
            tq.update(args.batch_size)
            if torch.cuda.is_available() and args.use_gpu:
                data = data.cuda()
                label = label.cuda()
            predict = model(data).squeeze()
            '''
            from PIL import Image
            import numpy as np
            
            temp = np.reshape(predict.detach().cpu().numpy(), (32, 640, 640))
            print(type(temp))
            temp = np.transpose(temp, [1, 2, 0])
            temp = np.asarray(temp[:, :])
            print(type(temp))
            for i in range(temp):
                for j in range(temp[0]):
                    k=max(j)
                    t=k.index()


            temp = np.asarray(temp < 0.05)
            new_im = Image.fromarray(temp)
            new_im.save('l.gif')
            print(predict)
            '''
            predict = reverse_one_hot(predict)
            predict = colour_code_segmentation(np.array(predict.cpu()),
                                               label_info)
            #print(predict)
            #cv2.imwrite("./result/"+dataset.image_name[i]+"_R.png",predict)
            label = label.squeeze()
            label = reverse_one_hot(label)
            label = colour_code_segmentation(np.array(label.cpu()), label_info)

            precision = compute_global_accuracy(predict, label)
            precision_record.append(precision)
        precision = np.mean(precision_record)
        tq.close()
        print('precision for test: %.3f' % precision)
        return precision
def predict_on_image(model, args, data, label_file, img_info):
    # read csv label path
    label_info = get_label_info(args.csv_path)

    # pre-processing on image
    label = Image.open(label_file)
    label = np.array(label)
    label = one_hot_it_v11_dice(label, label_info).astype(np.uint8)
    label = np.transpose(label, [2, 0, 1]).astype(np.float32)
    label = label.squeeze()
    label = np.argmax(label, axis=0)

    image = cv2.imread(data, -1)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    resize = iaa.Scale({'height': args.crop_height, 'width': args.crop_width})
    resize_det = resize.to_deterministic()
    image = resize_det.augment_image(image)
    image = Image.fromarray(image).convert('RGB')
    image = transforms.ToTensor()(image)
    image = transforms.Normalize((0.485, 0.456, 0.406),
                                 (0.229, 0.224, 0.225))(image).unsqueeze(0)
    # predict
    model.eval()
    predict = model(image).squeeze()
    # 512 * 512
    predict = reverse_one_hot(predict)

    predict_ = colour_code_segmentation(np.array(predict), label_info)
    predict_ = cv2.resize(np.uint8(predict_), (512, 512))
    cv2.imwrite('res/pred_' + 'img_info' + '.png',
                cv2.cvtColor(np.uint8(predict_), cv2.COLOR_RGB2BGR))
    diff = plot_diff(np.array(predict), label)
    cv2.imwrite('res/diff_' + 'img_info' + '.png',
                cv2.cvtColor(np.uint8(diff), cv2.COLOR_RGB2BGR))
Beispiel #6
0
def predict_on_RGB(image):  # nd convenient both for img and video
    # pre-processing on image
    image = resize_img(image)
    image = transforms.ToTensor()(image).float().unsqueeze(0)

    # predict
    model.eval()
    predict = model(image).squeeze()
    predict = reverse_one_hot(predict)
    predict = colour_code_segmentation(np.array(predict), label_info)  # RGB
    predict = predict.astype(np.uint8)

    return predict
Beispiel #7
0
    def predict_on_image(self, image):
        # transform image to tensor
        image = self.transform(image[:, :, ::-1]).to(self.device)

        # prediction map
        predict = self.model(image.unsqueeze(0)).squeeze()

        # encode to class index
        predict = reverse_one_hot(predict).cpu().numpy()

        # encode to color code
        predict = colour_code_segmentation(predict,
                                           self.label_info).astype(np.uint8)

        # get bbox output
        predict, bboxes, num_people = self.bbox_output(predict)
        return predict, bboxes, num_people
def predict_on_image(model, args):
    # pre-processing on image
    image = cv2.imread(args.data, -1)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    resize = iaa.Scale({'height': args.crop_height, 'width': args.crop_width})
    resize_det = resize.to_deterministic()
    image = resize_det.augment_image(image)
    image = Image.fromarray(image).convert('RGB')
    image = transforms.ToTensor()(image).unsqueeze(0)

    # read csv label path
    label_info = get_label_info(args.csv_path)
    # predict
    model.eval()
    predict = model(image).squeeze()
    predict = reverse_one_hot(predict)
    predict = colour_code_segmentation(np.array(predict), label_info)
    predict = cv2.resize(np.uint8(predict), (960, 720))
    cv2.imwrite(args.save_path,
                cv2.cvtColor(np.uint8(predict), cv2.COLOR_RGB2BGR))
Beispiel #9
0
def predict_on_RGBD(image, depth):  # nd convenient both for img and video
    # pre-processing on image
    image = resize_img(image).convert('RGB')
    image = transforms.ToTensor()(image).float().unsqueeze(0)

    depth = resize_depth(depth)
    depth = np.array(depth)
    depth = depth[:, :, np.newaxis]
    depth = depth / 255
    depth = transforms.ToTensor()(depth).float().unsqueeze(0)

    rgbd = torch.cat((image, depth), 1)

    # predict
    model.eval()
    predict = model(rgbd).squeeze()
    predict = reverse_one_hot(predict)
    predict = colour_code_segmentation(np.array(predict), label_info)
    predict = np.uint8(predict)

    return cv2.cvtColor(np.uint8(predict), cv2.COLOR_RGB2BGR)
Beispiel #10
0
def predict_on_image(model, args, image):
    '''
        run inference and return the resultant image
    '''
    # pre-processing on image
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    resize = iaa.Scale({'height': args.crop_height, 'width': args.crop_width})
    resize_det = resize.to_deterministic()
    image = resize_det.augment_image(image)
    image = Image.fromarray(image).convert('RGB')
    image = transforms.ToTensor()(image)
    image = transforms.Normalize((0.485, 0.456, 0.406),
                                 (0.229, 0.224, 0.225))(image).unsqueeze(0)
    # read csv label path
    label_info = get_label_info(args.csv_path)
    # predict
    model.eval()
    predict = model(image).squeeze()
    predict = reverse_one_hot(predict)
    # predict = colour_code_segmentation(np.array(predict), label_info)
    predict = colour_code_segmentation(np.array(predict.cpu()), label_info)
    predict = cv2.resize(np.uint8(predict), (960, 720))
    # cv2.imwrite(args.save_path, cv2.cvtColor(np.uint8(predict), cv2.COLOR_RGB2BGR))
    return predict
    def val_out(self,
                sess,
                val_init,
                threshold=0.5,
                output_dir="val",
                epoch=0):
        print("validation starts.")
        save_dir = output_dir + "/%d" % (epoch)
        if not os.path.exists(save_dir):
            os.mkdir(save_dir)
        target = open(save_dir + "/val_scores.csv", 'w')
        target.write(
            "val_name, avg_accuracy, precision, recall, f1 score, mean iou, %s\n"
            % (self.name_string))

        sess.run(val_init)
        #for ind in range(self.num_val):
        scores_list = []
        class_scores_list = []
        precision_list = []
        recall_list = []
        f1_list = []
        iou_list = []
        try:
            while True:
                img, ann, output_image = sess.run(
                    [self.img, self.mask, self.logits])
                img = img[0, :, :, :] * 255

                ann = np.array(ann[0, :, :, :])
                ann = reverse_one_hot(ann)

                path, size = sess.run([self.path, self.size])
                size = (size[0][0], size[0][1])

                output_single_image = np.array(output_image)
                output_single_image = np.array(output_single_image[0, :, :, :])
                output_image = reverse_one_hot(output_single_image)
                out_vis_image = colour_code_segmentation(
                    output_image, self.label_values)

                accuracy, class_accuracies, prec, rec, f1, iou = evaluate_segmentation(
                    pred=output_image, label=ann, num_classes=self.num_classes)

                dir = path[0].decode('ascii')
                file_name = filepath_to_name(dir)

                target.write("%s, %f, %f, %f, %f, %f" %
                             (file_name, accuracy, prec, rec, f1, iou))
                for item in class_accuracies:
                    target.write(", %f" % (item))
                target.write("\n")

                mask = colour_code_segmentation(ann, self.label_values)

                mask = cv2.cvtColor(np.uint8(mask), cv2.COLOR_RGB2BGR)
                out_vis_image = cv2.cvtColor(np.uint8(out_vis_image),
                                             cv2.COLOR_RGB2BGR)
                mask, ori_out_vis = resizeImage(mask, out_vis_image, size)

                out_vis_image = cv2.resize(ori_out_vis[:, :, 1],
                                           size,
                                           interpolation=cv2.INTER_NEAREST)
                out_vis_image[out_vis_image < threshold * 255] = 0
                out_vis_image[out_vis_image >= threshold * 255] = 255

                save_ori_img = cv2.cvtColor(np.uint8(img), cv2.COLOR_RGB2BGR)
                save_ori_img = cv2.resize(save_ori_img,
                                          size,
                                          interpolation=cv2.INTER_NEAREST)
                transparent_image = np.append(np.array(save_ori_img)[:, :,
                                                                     0:3],
                                              out_vis_image[:, :, None],
                                              axis=-1)
                # transparent_image = Image.fromarray(transparent_image)

                cv2.imwrite(save_dir + "/%s_img.jpg" % (file_name),
                            save_ori_img)
                cv2.imwrite(save_dir + "/%s_ann.png" % (file_name), mask)
                cv2.imwrite(save_dir + "/%s_ori_pred.png" % (file_name),
                            ori_out_vis)
                cv2.imwrite(save_dir + "/%s_filter_pred.png" % (file_name),
                            out_vis_image)
                cv2.imwrite(save_dir + "/%s_mat.png" % (file_name),
                            transparent_image)

                scores_list.append(accuracy)
                class_scores_list.append(class_accuracies)
                precision_list.append(prec)
                recall_list.append(rec)
                f1_list.append(f1)
                iou_list.append(iou)
        except tf.errors.OutOfRangeError:
            avg_score = np.mean(scores_list)
            class_avg_scores = np.mean(class_scores_list, axis=0)
            avg_precision = np.mean(precision_list)
            avg_recall = np.mean(recall_list)
            avg_f1 = np.mean(f1_list)
            avg_iou = np.mean(iou_list)

            print("\nAverage validation accuracy for epoch # %04d = %f" %
                  (epoch, avg_score))
            print("Average per class validation accuracies for epoch # %04d:" %
                  (epoch))
            for index, item in enumerate(class_avg_scores):
                print("%s = %f" % (self.name_list[index], item))
            print("Validation precision = ", avg_precision)
            print("Validation recall = ", avg_recall)
            print("Validation F1 score = ", avg_f1)
            print("Validation IoU score = ", avg_iou)