def inference_kitti(self, data):
        #crop kitti images into 3 parts
        with torch.no_grad():
            self.a_l_real = data['A_l'].cuda()
            _, b_l_classes = self.depth_model(self.a_l_real)
            self.b_l_fake = bins_to_depth(b_l_classes)

            self.a_m_real = data['A_m'].cuda()
            _, b_m_classes = self.depth_model(self.a_m_real)
            self.b_m_fake = bins_to_depth(b_m_classes)

            self.a_r_real = data['A_r'].cuda()
            _, b_r_classes = self.depth_model(self.a_r_real)
            self.b_r_fake = bins_to_depth(b_r_classes)

            out = kitti_merge_imgs(self.b_l_fake, self.b_m_fake, self.b_r_fake,
                                   torch.squeeze(data['B_raw']).shape,
                                   data['crop_lmr'])
            return {'b_fake': out}
    def criterion(self, pred_softmax, pred_logit, data, epoch):
        pred_depth = bins_to_depth(pred_softmax)
        loss_metric = self.weight_cross_entropy_loss(pred_logit,
                                                     data['B_bins'],
                                                     data['B'].cuda())
        loss_normal = self.virtual_normal_loss(data['B'].cuda(), pred_depth)

        loss = {}
        loss['metric_loss'] = loss_metric
        loss['virtual_normal_loss'] = cfg.MODEL.DIFF_LOSS_WEIGHT * loss_normal
        loss['total_loss'] = loss['metric_loss'] + loss['virtual_normal_loss']
        return loss
    def inference(self, data):
        with torch.no_grad():
            out = self.forward(data)

            if cfg.MODEL.PREDICTION_METHOD == 'classification':
                pred_depth = bins_to_depth(out['b_fake_softmax'])
            elif cfg.MODEL.PREDICTION_METHOD == 'regression':
                # for regression methods
                pred_depth = torch.nn.functional.sigmoid(out['b_fake_logit'])
            else:
                raise ValueError("Unknown prediction methods")

            out = pred_depth
            return {'b_fake': out}
Example #4
0
    def run(self, imgfilelist, basedatasetpath):
        pred_depths = []

        num_lines = sum(1 for line in open(imgfilelist, 'r'))
        with open(imgfilelist, 'r') as f:
            for _, line in enumerate(tqdm(f, total=num_lines)):
                imgname = line.split()[0]
                fullpath = os.path.join(basedatasetpath, imgname)

                with torch.no_grad():
                    # Load image
                    img = cv2.imread(fullpath)
                    # Scale image
                    img_resize = cv2.resize(
                        img, (int(img.shape[1]), int(img.shape[0])),
                        interpolation=cv2.INTER_LINEAR)
                    img_torch = scale_torch(img_resize, 255)
                    img_torch = img_torch[None, :, :, :].cuda()
                    # Run model
                    _, pred_depth_softmax = self.model.module.depth_model(
                        img_torch)
                    pred_depth = bins_to_depth(pred_depth_softmax)
                    pred_depth = pred_depth.cpu().numpy().squeeze()
                    # Store depth prediction and image path
                    if self.dataset == "kitti":
                        partial_image_path = os.sep.join(
                            os.path.normpath(fullpath).split(os.sep)[-5:])
                    else:
                        partial_image_path = os.sep.join(
                            os.path.normpath(fullpath).split(os.sep)[-2:])
                    outsample = {
                        'pred_depth': pred_depth,
                        'image_path': partial_image_path
                    }

                    pred_depths.append(outsample)
        return pred_depths
                out_dir, file.replace('leftImg8bit', 'VNL_Monocular'))

            if not os.path.exists(out_dir):
                os.makedirs(out_dir)

            if not os.path.exists(out_path):
                with torch.no_grad():
                    try:
                        img = Image.open(img_path)
                        img_resize = img.copy()
                        img_torch = scale_torch(img_resize, 255)
                        img_torch = img_torch[None, :, :, :].cuda()

                        _, pred_depth_softmax = model.module.depth_model(
                            img_torch)
                        pred_depth = bins_to_depth(pred_depth_softmax)

                        # Un-normalize using factor from vnl/data/kitti_dataset.py
                        pred_depth = 80 * pred_depth.cpu().numpy().squeeze()

                        # Convert to uint16 for storage
                        pred_depth_scaled = (pred_depth * 256).astype('uint16')

                        # Write out as uint16 png
                        with open(out_path, 'wb') as f:
                            writer = png.Writer(width=pred_depth.shape[1],
                                                height=pred_depth.shape[0],
                                                bitdepth=16,
                                                greyscale=True)
                            z = pred_depth_scaled.tolist()
                            writer.write(f, z)
 def inference(self, data):
     with torch.no_grad():
         out = self.forward(data)
         pred_depth = bins_to_depth(out['b_fake_softmax'])
         b_fake = pred_depth
         return {'b_fake': b_fake}