tf.data

        if split == 'train':
            assert(self.mode in ['train_noval', 'train_with_val'])
            tf_ds = tf.data.Dataset.from_tensor_slices(self._trn_idx) ##self._trn_idx是数组,对应训练样本序号的索引
            tf_ds = tf_ds.apply(tf.contrib.data.shuffle_and_repeat(buffer_size=len(self._trn_idx), count=-1)) #count=-1或者none表示无限repeat
            
            aa=self._train_stub(self._trn_idx)  ##下一个函数[idx需要修改]
            tf_ds = tf_ds.apply(tf.contrib.data.map_and_batch(
                map_func=lambda idx: tf.py_func(self._train_stub, [idx], [tf.uint8, tf.float32, tf.string]),
                batch_size=batch_size * num_gpus, num_parallel_batches=threads))
    def _train_stub(self, idx):
        """tf.py_func stub for _get_train_samples()
        Args:
            idx: Index of unique sample to return
        Returns:
            x: Image pair in [2, H, W, 3] format
            y: Groundtruth flow in [H, W, 2] format
            ID: string, sample ID
        """
        x, y, ID = self.get_samples(idx=idx, split='train', as_list=False, simple_IDs=True) ##正确:idx=[idx],做测试的时候idx=idx
        return np.squeeze(x), np.squeeze(y), ID[0]
def get_samples(
            self,
            num_samples=0,
            idx=None,
            split='val',
            as_list=True,
            deterministic=False,
            as_tuple=False,
            simple_IDs=False):
        """Get a few (or all) random (or ordered) samples from the dataset.
        Used for debugging purposes (testing how the model is improving over time, for instance).
        If sampling from the training/validation set, there is a label; otherwise, there isn't.
        Note that this doesn't return a valid np array if the images don't have the same size.
        Args:
            num_samples: Number of samples to return (used only if idx is set to None)
            idx: Specific list of indices to pull from the dataset split (no need to set num_samples in this case)
            split: 'train','val','val_with_preds','val_with_pred_paths','test','test_with_preds','test_with_pred_paths'
            as_list: Return as list or np array?
            return_IDs: If True, also return ID of the sample
            deterministic: If True, return first num_samples samples, otherwise, sample randomly
            as_tuple: If True, return image pairs as tuples; otherwise, return them as np arrays in [2, H, W, 3] format
            simple_IDs: If True, return concatenated IDs; otherwise, return individual image and label IDs
        In training and validation mode, returns:
            images: Batch of image pairs in format [num_samples, 2, H, W, 3] or list([2, H, W, 3])
            labels: Batch of optical flows in format [num_samples, H, W, 2] or list([H, W, 2])
            IDs: List of ID strings in list or np.ndarray format
        In testing mode, returns:
            images: Batch of image pairs in format [num_samples, 2, H, W, 3]
            output_files: List of output file names that match the input file names
            IDs: List of ID strings in list or np.ndarray format
        """
        assert(idx is not None or num_samples > 0)

        if split == 'train':
            assert(self.mode in ['train_noval', 'train_with_val'])
            if idx is None:
                if deterministic:
                    idx = self._trn_idx[0:num_samples]
                else:
                    idx = np.random.choice(self._trn_idx, size=num_samples, replace=False)

            images, labels, IDs = self._get_train_samples(idx, as_tuple=as_tuple, simple_IDs=simple_IDs)

            if as_list:
                return images, labels, IDs
            else:
                return map(np.asarray, (images, labels, IDs))

        elif split == 'val':
            assert(self.mode in ['val', 'val_notrain', 'train_with_val'])
            if idx is None:
                if deterministic:
                    idx = self._val_idx[0:num_samples]
                else:
                    idx = np.random.choice(self._val_idx, size=num_samples, replace=False)

            images, gt_labels, IDs = self._get_val_samples(idx, as_tuple=as_tuple, simple_IDs=simple_IDs)

            if as_list:
                return images, gt_labels, IDs
            else:
                return map(np.asarray, (images, gt_labels, IDs))

        elif split == 'val_with_preds':
            assert(self.mode in ['val', 'val_notrain', 'train_with_val'])
            if idx is None:
                if deterministic:
                    idx = self._val_idx[0:num_samples]
                else:
                    idx = np.random.choice(self._val_idx, size=num_samples, replace=False)

            images, gt_labels, pred_labels, IDs = self._get_val_samples_with_preds(
                idx, as_tuple=as_tuple, simple_IDs=simple_IDs)

            if as_list:
                return images, gt_labels, pred_labels, IDs
            else:
                return map(np.asarray, (images, gt_labels, pred_labels, IDs))

        elif split == 'val_with_pred_paths':
            assert(self.mode in ['val', 'val_notrain', 'train_with_val'])
            if idx is None:
                if deterministic:
                    idx = self._val_idx[0:num_samples]
                else:
                    idx = np.random.choice(self._val_idx, size=num_samples, replace=False)

            images, gt_labels, pred_label_paths, IDs = self._get_val_samples_with_pred_paths(
                idx, as_tuple=as_tuple, simple_IDs=simple_IDs)

            if as_list:
                return images, gt_labels, pred_label_paths, IDs
            else:
                return map(np.asarray, (images, gt_labels, pred_label_paths, IDs))

        elif split == 'test':
            if idx is None:
                if deterministic:
                    idx = self._tst_idx[0:num_samples]
                else:
                    idx = np.random.choice(self._tst_idx, size=num_samples, replace=False)

            images, IDs = [], []
            for l in idx:
                if self.opts['in_memory']:
                    image = self._images_test[l]
                else:
                    image = self._load_sample(self._img_tst_path[l], preprocess=False, as_tuple=as_tuple)
                images.append(image)
                if simple_IDs is True:
                    IDs.append(self._tst_IDs_simpl[l])
                else:
                    IDs.append(self._tst_IDs[l])

            if as_list:
                return images, IDs
            else:
                return map(np.asarray, (images, IDs))

        elif split == 'test_with_preds':
            if idx is None:
                if deterministic:
                    idx = self._tst_idx[0:num_samples]
                else:
                    idx = np.random.choice(self._tst_idx, size=num_samples, replace=False)

            images, pred_labels, IDs = self._get_test_samples_with_preds(idx, as_tuple=as_tuple, simple_IDs=simple_IDs)

            if as_list:
                return images, pred_labels, IDs
            else:
                return map(np.asarray, (images, pred_labels, IDs))

        elif split == 'test_with_pred_paths':
            if idx is None:
                if deterministic:
                    idx = self._tst_idx[0:num_samples]
                else:
                    idx = np.random.choice(self._tst_idx, size=num_samples, replace=False)

            images, pred_label_paths, IDs = self._get_test_samples_with_pred_paths(
                idx, as_tuple=as_tuple, simple_IDs=simple_IDs)

            if as_list:
                return images, pred_label_paths, IDs
            else:
                return map(np.asarray, (images, pred_label_paths, IDs))

        else:
            return None, None

 

def _get_train_samples(self, idx, as_tuple=False, simple_IDs=False):
        """Get training images with associated labels
        Args:
            idx: List of sample indices to return
            as_tuple: If True, return image pairs as tuples; otherwise, return them as np arrays in [2, H, W, 3] format
            simple_IDs: If True, return concatenated IDs; otherwise, return individual image and label IDs
        Returns:
            images: List of RGB images in format list(([H, W, 3],[H, W, 3])) or list([2, H, W, 3])
            labels: List of labels in format list([H, W, 2])
            IDs: List of IDs in format list(str) or list([str,str,str])
        """
        images, labels, IDs = [], [], []
        for l in idx:
            if self.opts['in_memory']:
                image = self._images_train[l]
                label = self._labels_train[l]
            else:
                image, label = self._load_sample(self._img_trn_path[l], self._lbl_trn_path[l],
                                                 is_training=True)

            # Crop images and/or labels to a fixed size, if requested
            if self.opts['crop_preproc'] is not None:
                h, w = image[0].shape[:2]  #384,512
                h_max, w_max = self.opts['crop_preproc'] # 256,448
                assert (h >= h_max and w >= w_max)
                max_y_offset, max_x_offset = h - h_max, w - w_max ##max_y_offset128,64
                if max_y_offset > 0 or max_x_offset > 0:
                    y_offset = np.random.randint(max_y_offset + 1)##117
                    x_offset = np.random.randint(max_x_offset + 1)##42
                    # The following assumes the image pair is in [2,H,W,3] format
                    image = image[:, y_offset:y_offset + h_max, x_offset:x_offset + w_max, :]##(2,256,448,3)
                    label = label[y_offset:y_offset + h_max, x_offset:x_offset + w_max, :]  #(256,448,3)

            # Scale images and/or labels to a fixed size, if requested
            if self.opts['scale_preproc'] is not None:
                scale_shape = (int(self.opts['scale_preproc'][0]), int(self.opts['scale_preproc'][1]))
                image[0] = cv2.resize(image[0], scale_shape)
                image[1] = cv2.resize(image[1], scale_shape)
                label = cv2.resize(label, scale_shape) * scale_shape[0] / image[0].shape[0]

            # Augment the samples, if requested
            if self.opts['aug_type'] is not None:
                image, label = self._augment_sample((image[0], image[1]), label, as_tuple)

            # Don't move augmentation to _load_sample() otherwise, if the samples are in-memory they will have been
            # augmented once and that's it, the first time they were loaded. The augmentation code needs to be
            # here so that, no matter the memory mode, every sample is augmented differently with every batch

            images.append(image)
            labels.append(label)
            if simple_IDs is True:
                IDs.append(self._trn_IDs_simpl[l])
            else:
                IDs.append(self._trn_IDs[l])

        return images, labels, IDs
def _load_sample(self, image_path=None, label_path=None, preprocess=True, as_tuple=False, is_training=False):
        """Load a properly formatted sample (image pair + optical flow)
        Args:
            image_path: Pair of image paths, if any
            label_path: Path to optical flow, if any
            preprocess: If True, apply preprocessing steps (cropping + scaling); otherwise, don't
            as_tuple: If true, return image pair as a tuple; otherwise, return a np array in [2, H, W, 3] format
            is_training: If true, sample is a training sample subject to data augmentation
        Returns:
            image: Image pair in format [2, H, W, 3] or ([H, W, 3],[H, W, 3]), if any
            label: Optical flow in format [W, H, 2], if any
        """
        # Read in RGB image, if any
        if image_path:
            image1, image2 = imread(image_path[0]), imread(image_path[1])
            assert(len(image1.shape) == 3 and image1.shape[2] == 3 and len(image2.shape) == 3 and image2.shape[2] == 3)

        # Read in label, if any
        if label_path:
            label = flow_read(label_path)
            assert (len(label.shape) == 3 and label.shape[2] == 2)
        else:
            label = None

        # Return image and/or label
        if label_path:
            if image_path:
                if as_tuple:
                    return (image1, image2), label
                else:
                    return np.array([image1, image2]), label
            else:
                return label
        else:
            if image_path:
                if as_tuple:
                    return (image1, image2)
                else:
                    return np.array([image1, image2])

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值