Exemplo n.º 1
0
def dfield_to_torch_position(dfield, spacing=[1., 1., 1.]):
    """
    Input displacement must be in physical coordinates
    specified by the given spacing parameter.

    input dfield is shape [nz ny nx 3]
    dfield[:,:,:,0] holds z displacements
    dfield[:,:,:,1] holds y displacements
    dfield[:,:,:,2] holds x displacements

    spacing is size [3] holding [z y x] spacings

    :param dfield:
    :param spacing:
    :return:
    """

    # the last dimension must hold the displacement vector

    np.flip(dfield)

    # copies may be necessary to avoid negative stride
    dfield_copy = dfield.astype('=f4')

    # IMPORTANT: The flipping below is because
    # the torch call:
    #   F.grid_sample( im, grid )
    #
    # for an image of size [1, 1, z, y, x]
    # grid must be of size [1, z, y, x, 3]
    # where
    #   grid[:,:,:,:0] holds x displacements
    #   grid[:,:,:,:1] holds y displacements
    #   grid[:,:,:,:2] holds z displacements

    dfield_t = torch.flip(torch.from_numpy(dfield_copy), [3])

    spacing_t = torch.as_tensor(
        spacing.copy())  # spacing may be a numpy or python array
    spacing_t = torch.flipud(spacing_t)

    mtmp = 2.0 / (torch.as_tensor(dfield_t.size()[:3]).float() - 1.0)

    # mtmp is in z y x order, but it needs to be x y z so:
    mtmp = torch.flipud(mtmp)

    mul = mtmp / spacing_t

    # displacements to pixel coordinates
    dfield_t *= mul

    # the grid torch needs for grid_sample
    identity_grid = torch.meshgrid(torch.linspace(-1, 1, dfield_t.size(2)),
                                   torch.linspace(-1, 1, dfield_t.size(1)),
                                   torch.linspace(-1, 1, dfield_t.size(0)))

    id_grid_stack = torch.stack(identity_grid).permute(3, 2, 1, 0)
    position_grid = (id_grid_stack + dfield_t)

    return position_grid
Exemplo n.º 2
0
def augment_data(x, y, n=None):
    """
    Generate an augmented training dataset with random reflections
    and 90 degree rotations
    x, y : Image sets of shape (Samples, Width, Height, Channels)
        training images and next images
    n : number of training examples
    """
    n_data = x.shape[0]

    if not n:
        n = n_data
    x_out, y_out = list(), list()

    for i in range(n):
        r = random.randint(0, n_data)
        x_r, y_r = x[r], y[r]

        if random.random() < 0.5:
            x_r = torch.fliplr(x_r)
            y_r = torch.fliplr(y_r)
        if random.random() < 0.5:
            x_r = torch.flipud(x_r)
            y_r = torch.flipud(y_r)

        num_rots = random.randint(0, 4)
        x_r = torch.rot90(x_r, k=num_rots)
        y_r = torch.rot90(y_r, k=num_rots)

        x_out.append(x_r), y_out.append(y_r)
    return torch.stack(x_out), torch.stack(y_out)
    def __call__(self, sample):
        train, truth = sample['train'], sample['truth']
        if np.random.rand() >= self.probability:
            train = torch.flipud(train)
            truth = torch.flipud(truth)

        return {'train': train, 'truth': truth}
Exemplo n.º 4
0
    def data_augmentation(image, mask):
        image = torch.Tensor(image)
        mask = torch.Tensor(mask)
        mask = mask.unsqueeze(0)

        if random.random() < 0.5:
            # flip left right
            image = torch.fliplr(image)
            mask = torch.fliplr(mask)

        rot = np.random.choice([0, 1, 2, 3])
        image = torch.rot90(image, rot, [1, 2])
        mask = torch.rot90(mask, rot, [1, 2])

        if random.random() < 0.5:
            # flip up-down
            image = torch.flipud(image)
            mask = torch.flipud(mask)

        if intensity >= 1:

            # random crop
            cropsize = image.shape[2] // 2
            image, mask = random_crop(image, mask, cropsize=cropsize)

            std_noise = 1 * image.std()
            if random.random() < 0.5:
                # add noise per pixel and per channel
                pixel_noise = torch.rand(image.shape[1], image.shape[2])
                pixel_noise = torch.repeat_interleave(pixel_noise.unsqueeze(0),
                                                      image.size(0),
                                                      dim=0)
                image = image + pixel_noise * std_noise

            if random.random() < 0.5:
                channel_noise = torch.rand(
                    image.shape[0]).unsqueeze(1).unsqueeze(2)
                channel_noise = torch.repeat_interleave(
                    torch.repeat_interleave(channel_noise, image.shape[1], 1),
                    image.shape[2], 2)
                image = image + channel_noise * std_noise

            if random.random() < 0.5:
                # add noise
                noise = torch.rand(image.shape[0], image.shape[1],
                                   image.shape[2]) * std_noise
                image = image + noise

        if intensity >= 2:
            # channel shuffle
            if random.random() < 0.5:
                idxs = np.arange(image.shape[0])
                np.random.shuffle(idxs)  # random band indixes
                image = image[idxs]

        mask = mask.squeeze(0)
        return image, mask
Exemplo n.º 5
0
 def __call__(self, img, mask):
     # def __call__(self, img, mask):
     #     if random.random() < self.p:
     #         return (
     #             (np.flipud(img)).copy(), (np.flipud(mask)).copy()
     #         )
     #     return img, mask
     if random.random() < self.p:
         return ((torch.flipud(img)).copy(), (torch.flipud(mask)).copy())
     return img, mask
Exemplo n.º 6
0
 def __call__(self, img, mask):
     if random.random() < self.p:
         # img = cv2.flip(img, 1)
         # mask = cv2.flip(mask, 1)
         img = torch.flipud(img)
         mask = torch.flipud(mask)
         return (
             # img.transpose(Image.FLIP_TOP_BOTTOM),
             # mask.transpose(Image.FLIP_TOP_BOTTOM),
             img,
             mask,
         )
     return img, mask
Exemplo n.º 7
0
def cosine2by2(representation1, representation2,device = 'cpu'):
    loss_func = nn.CosineEmbeddingLoss()
    x1 = torch.cat([representation1,representation1])
    x2 = torch.cat([representation2,torch.flipud(representation2)])
    y = torch.tensor([1,1,-1,-1],)
    idx = np.random.choice(len(y),len(y),replace = False)
    return loss_func(x1[idx].to(device),x2[idx].to(device),y[idx].to(device))
Exemplo n.º 8
0
def prepare_first_frame(curr_video,
                        save_prediction,
                        annotation,
                        sigma1=8,
                        sigma2=21,
                        inference_strategy='single',
                        probability_propagation=False,
                        scale=None):
    first_annotation = Image.open(annotation)
    (H, W) = np.asarray(first_annotation).shape
    H_d = int(np.ceil(H * Config.SCALE))
    W_d = int(np.ceil(W * Config.SCALE))
    palette = first_annotation.getpalette()
    label = np.asarray(first_annotation)
    d = np.max(label) + 1
    label = torch.Tensor(label).long().to(Config.DEVICE)  # (1, H, W)
    label_1hot = get_labels(label, d, H, W, H_d, W_d)

    weight_dense = get_spatial_weight((H_d, W_d), sigma1) if not probability_propagation else None
    weight_sparse = get_spatial_weight((H_d, W_d), sigma2) if not probability_propagation else None

    if save_prediction is not None:
        if not os.path.exists(save_prediction):
            os.makedirs(save_prediction)
        save_path = os.path.join(save_prediction, curr_video)
        if not os.path.exists(save_path):
            os.makedirs(save_path)
        first_annotation.save(os.path.join(save_path, '00000.png'))

    if inference_strategy == 'single':
        return label_1hot, d, palette, weight_dense, weight_sparse
    elif inference_strategy == 'hor-flip':
        label_1hot_flipped = get_labels(torch.fliplr(label), d, H, W, H_d, W_d)
        return label_1hot, label_1hot_flipped, d, palette, weight_dense, weight_sparse
    elif inference_strategy == 'ver-flip':
        label_1hot_flipped = get_labels(torch.flipud(label), d, H, W, H_d, W_d)
        return label_1hot, label_1hot_flipped, d, palette, weight_dense, weight_sparse
    elif inference_strategy == '2-scale' or inference_strategy == 'hor-2-scale':
        H_d_2 = int(np.ceil(H * Config.SCALE * scale))
        W_d_2 = int(np.ceil(W * Config.SCALE * scale))
        weight_dense_2 = get_spatial_weight((H_d_2, W_d_2), sigma1) if not probability_propagation else None
        weight_sparse_2 = get_spatial_weight((H_d_2, W_d_2), sigma2) if not probability_propagation else None
        label_1hot_2 = get_labels(label, d, H, W, H_d_2, W_d_2)
        return (label_1hot, label_1hot_2), d, palette, (weight_dense, weight_dense_2), (weight_sparse, weight_sparse_2)
    elif inference_strategy == 'multimodel':
        # that's right, do nothing
        pass
    elif inference_strategy == '3-scale':
        del weight_dense, weight_sparse
        H_d = int(np.ceil(H * Config.SCALE * scale))
        W_d = int(np.ceil(W * Config.SCALE * scale))
        weight_dense = get_spatial_weight((H_d, W_d), sigma1) if not probability_propagation else None
        weight_sparse = get_spatial_weight((H_d, W_d), sigma2) if not probability_propagation else None
        label_1hot = get_labels(label, d, H, W, H_d, W_d)
        return label_1hot, d, palette, weight_dense, weight_sparse

    return label_1hot, d, palette, weight_dense, weight_sparse
Exemplo n.º 9
0
 def test_flipud_invalid(self, device, dtype):
     with self.assertRaisesRegex(RuntimeError, "Input must be >= 1-d."):
         torch.flipud(torch.tensor(42, device=device, dtype=dtype))
Exemplo n.º 10
0
 def other_ops(self):
     a = torch.randn(4)
     b = torch.randn(4)
     c = torch.randint(0, 8, (5, ), dtype=torch.int64)
     e = torch.randn(4, 3)
     f = torch.randn(4, 4, 4)
     size = [0, 1]
     dims = [0, 1]
     return (
         torch.atleast_1d(a),
         torch.atleast_2d(a),
         torch.atleast_3d(a),
         torch.bincount(c),
         torch.block_diag(a),
         torch.broadcast_tensors(a),
         torch.broadcast_to(a, (4)),
         # torch.broadcast_shapes(a),
         torch.bucketize(a, b),
         torch.cartesian_prod(a),
         torch.cdist(e, e),
         torch.clone(a),
         torch.combinations(a),
         torch.corrcoef(a),
         # torch.cov(a),
         torch.cross(e, e),
         torch.cummax(a, 0),
         torch.cummin(a, 0),
         torch.cumprod(a, 0),
         torch.cumsum(a, 0),
         torch.diag(a),
         torch.diag_embed(a),
         torch.diagflat(a),
         torch.diagonal(e),
         torch.diff(a),
         torch.einsum("iii", f),
         torch.flatten(a),
         torch.flip(e, dims),
         torch.fliplr(e),
         torch.flipud(e),
         torch.kron(a, b),
         torch.rot90(e),
         torch.gcd(c, c),
         torch.histc(a),
         torch.histogram(a),
         torch.meshgrid(a),
         torch.lcm(c, c),
         torch.logcumsumexp(a, 0),
         torch.ravel(a),
         torch.renorm(e, 1, 0, 5),
         torch.repeat_interleave(c),
         torch.roll(a, 1, 0),
         torch.searchsorted(a, b),
         torch.tensordot(e, e),
         torch.trace(e),
         torch.tril(e),
         torch.tril_indices(3, 3),
         torch.triu(e),
         torch.triu_indices(3, 3),
         torch.vander(a),
         torch.view_as_real(torch.randn(4, dtype=torch.cfloat)),
         torch.view_as_complex(torch.randn(4, 2)),
         torch.resolve_conj(a),
         torch.resolve_neg(a),
     )
Exemplo n.º 11
0
        def camera_rgb(obs_cache):

            # Switch to correct camera
            if self is not None and self.camera_name != cam_name:
                self._switch_camera(cam_name)

            rendered_imgs = self.renderer.render(modes=self.modes)
            rendered_mapping = {
                k: val
                for k, val in zip(self.modes, rendered_imgs)
            }

            # in np array image received is of correct orientation
            # in torch tensor image is flipped upside down
            # adjusting the np image in a way so that return statement stays same
            # flipping torch tensor when opencv coordinate system is required.
            # using torch.flipud because negative strides do not work in torch.

            if 'rgb' in self.modes:
                img = rendered_mapping['rgb'][:, :, :3]
                if isinstance(img, np.ndarray):
                    # np array is in range 0-1
                    img = (img * 255).astype(np.uint8)
                    img = adjust_convention(img, convention)
                elif convention == -1:
                    img = torch.flipud(img)
            else:
                img = np.zeros((cam_h, cam_w), np.uint8)

            if 'seg' in self.modes:
                # 0th channel contains segmentation
                seg_map = rendered_mapping['seg'][:, :, 0]
                if isinstance(seg_map, np.ndarray):
                    # np array is in range 0-1
                    # round function is important here otherwise trippy looking segmaps
                    seg_map = (seg_map * MAX_CLASS_COUNT).round().astype(
                        np.int64)
                    # flip the image upside down if required
                    seg_map = adjust_convention(seg_map, convention)
                elif convention == -1:
                    # flip in Y direction if torch tensor
                    seg_map = torch.flipud(seg_map)
                obs_cache[seg_sensor_name] = seg_map

            if '3d' in self.modes:
                # 2nd channel contains correct depth map
                depth_map = rendered_mapping['3d'][:, :, 2]
                if isinstance(depth_map, np.ndarray):
                    depth_map = adjust_convention(depth_map, convention)
                elif convention == -1:
                    # flip in Y direction if torch tensor
                    depth_map = torch.flipud(depth_map)

                obs_cache[depth_sensor_name] = depth_map

            if 'normal' in self.modes:
                normal_map = rendered_mapping['normal'][:, :, :3]
                if isinstance(normal_map, np.ndarray):
                    # np array is in range 0-1
                    normal_map = (normal_map * 255).astype(np.uint8)
                    normal_map = adjust_convention(normal_map, convention)
                elif convention == -1:
                    normal_map = torch.flipud(normal_map)
                obs_cache[normal_sensor_name] = normal_map

            return img
Exemplo n.º 12
0
    def predict(self, path, predpath):
        with rasterio.open(path, "r") as src:
            meta = src.meta
        self.model.eval()

        # for prediction
        predimage = os.path.join(predpath, os.path.basename(path))
        os.makedirs(predpath, exist_ok=True)
        meta["count"] = 1
        meta["dtype"] = "uint8" # storing as uint8 saves a lot of storage space

        #Window(col_off, row_off, width, height)
        H, W = self.image_size

        rows = np.arange(0, meta["height"], H)
        cols = np.arange(0, meta["width"], W)

        image_window = Window(0, 0, meta["width"], meta["height"])

        with rasterio.open(predimage, "w+", **meta) as dst:

            for r, c in tqdm(product(rows, cols), total=len(rows) * len(cols), leave=False):

                window = image_window.intersection(
                    Window(c-self.offset, r-self.offset, W+self.offset, H+self.offset))

                with rasterio.open(path) as src:
                    image = src.read(window=window)

                # if L1C image (13 bands). read only the 12 bands compatible with L2A data
                if (image.shape[0] == 13):
                    image = image[[l1cbands.index(b) for b in l2abands]]

                # to torch + normalize
                image = self.transform(torch.from_numpy(image.astype(np.float32)), [])[0].to(self.device)

                # predict
                with torch.no_grad():
                    x = image.unsqueeze(0)
                    #import pdb; pdb.set_trace()
                    y_logits = torch.sigmoid(self.model(x).squeeze(0))
                    if self.use_test_aug > 0:
                        y_logits += torch.sigmoid(torch.fliplr(self.model(torch.fliplr(x)))).squeeze(0) # fliplr)
                        y_logits += torch.sigmoid(torch.flipud(self.model(torch.flipud(x)))).squeeze(0) # flipud
                        if self.use_test_aug > 1:
                            for rot in [1, 2, 3]: # 90, 180, 270 degrees
                                y_logits += torch.sigmoid(torch.rot90(self.model(torch.rot90(x, rot, [2, 3])),-rot,[2,3]).squeeze(0))
                            y_logits /= 6
                        else:
                            y_logits /= 3

                    y_score = y_logits.cpu().detach().numpy()[0]
                    #y_score = y_score[:,self.offset:-self.offset, self.offset:-self.offset]

                data = dst.read(window=window)[0] / 255
                overlap = data > 0

                if overlap.any():
                    # smooth transition in overlapping regions
                    dx, dy = np.gradient(overlap.astype(float)) # get border
                    g = np.abs(dx) + np.abs(dy)
                    transition = gaussian_filter(g, sigma=self.offset / 2)
                    transition /= transition.max()
                    transition[~overlap] = 1.# normalize to 1

                    y_score = transition * y_score + (1-transition) * data

                # write
                writedata = (np.expand_dims(y_score, 0).astype(np.float32) * 255).astype(np.uint8)
                dst.write(writedata, window=window)
Exemplo n.º 13
0
    d = torch.tensor([1.7, 1.2, 3.1, 2])
    print(torch.maximum(a, b))
    print(torch.minimum(a, b))
    print(torch.fmod(a, 2))
    print(torch.dist(c, d, 1))  # p-norm
    print(torch.norm(c))
    print(torch.div(c, d))
    print(torch.true_divide(c, d))  # rounding_mode=None
    print(torch.sub(c, d, alpha=2.))
    print(c.add(d))
    print(torch.dot(c, d))
    print(torch.sigmoid(c))
    # print(torch.inner(c, d))
    """flip"""
    x = torch.arange(4).view(2, 2)
    print(torch.flipud(x))
    print(torch.fliplr(x))

    # logical
    print("logical function:")
    print(torch.eq(c, d))
    print(torch.ne(c, d))
    print(torch.gt(c, d))
    print(torch.logical_and(c, d))
    print(torch.logical_or(c, d))
    print(torch.logical_xor(c, d))
    print(torch.logical_not(c))
    print(torch.equal(c, d))  # if all equal
    a = torch.rand(2, 2).bool()
    print(a)
    print(torch.all(a))
Exemplo n.º 14
0
        masked_random_actions[i] = sample_action(rand_policy * legal_mask, 0)

    print("RANDOM policy selected the following illegal actions:")
    print(set(random_actions) - set(legal_moves))
    print("RANDOM policy selected the following legal actions:")
    print(set(random_actions) & set(legal_moves))
    print("MASKED RANDOM policy selected the following illegal actions:")
    print(set(masked_random_actions) - set(legal_moves))
    print("MASKED RANDOM policy selected the following legal actions:")
    print(set(masked_random_actions) & set(legal_moves))

    q.exec_move('a4')
    q.exec_move('h5')
    q.exec_move('a1v')
    q.exec_move('d4h')
    q.exec_move('h3v')
    q.exec_move('h8v')
    planes0 = encode_state_to_planes(q)
    q.current_player = 1
    planes1 = encode_state_to_planes(q)

    print(planes0)
    print(planes1)

    # Test that plane 0 is 'current' player and flipped direction from perspective of other player
    assert torch.all(planes0[0] == torch.flipud(planes1[2]))
    assert torch.all(planes0[2] == torch.flipud(planes1[0]))
    # Test that walls are vertically flipped between two players' perspectives
    assert torch.all(planes0[4] == torch.flipud(planes1[4]))
    assert torch.all(planes0[5] == torch.flipud(planes1[5]))
def main():
    parser = argparse.ArgumentParser(
        description='Feature (log-spec) Extraction')
    parser.add_argument('--data-dir',
                        required=True,
                        help='data directory contains wave files')
    parser.add_argument('--label-file',
                        required=True,
                        help='protocol file that contains utt2label mapping')
    parser.add_argument('--feat-dir',
                        required=True,
                        help='feature saving directory')
    parser.add_argument('--no-cuda',
                        action='store_true',
                        default=False,
                        help='disables CUDA for feature extraction')
    parser.add_argument('--logging-dir',
                        required=True,
                        help='log save directory')
    parser.add_argument('--segment',
                        action='store_true',
                        default=False,
                        help='whether to segment the logsepc by energy')
    parser.add_argument(
        '--seg-win',
        type=int,
        help='the window size to be used for segment: 64, 128..')
    parser.add_argument('--seg-method',
                        help='the method to be used for segment: h, l, hl, lh')
    args = parser.parse_args()

    os.makedirs(args.logging_dir, exist_ok=True)
    os.makedirs(args.feat_dir, exist_ok=True)

    use_cuda = not args.no_cuda and torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")

    if args.segment and (args.seg_method is None or args.seg_win is None):
        raise ValueError("segment method or win_size is missing")

    # Setup logs
    basename = DATA_NAME + os.path.basename(args.data_dir)
    run_name = "logspec-" + basename + time.strftime("-%Y-%m-%d")
    if os.path.exists(run_name + ".log"):
        os.remove(run_name + ".log")
    logger = setup_logs(args.logging_dir, run_name)

    logger.info("===> Start logspec extraction for dataset: " + args.data_dir)

    # global timer starts
    global_start = timer()

    utt2label_path = os.path.join(
        os.path.dirname(args.label_file),
        os.path.basename(args.label_file) + '.utt2label')

    f_utt2label = open(utt2label_path, 'w')
    f_label = open(args.label_file, 'r')

    for line in f_label:
        item = line.strip().split(' ')
        if item[1] == 'genuine':
            label = 1
        elif item[1] == 'spoof':
            label = 0
        else:
            raise ValueError("Invalid label: " + item[1])
        f_utt2label.write(item[0][:-4] + ' ' + str(label) + '\n')

        audio_path = os.path.join(args.data_dir, item[0])

        t_start = timer()
        logspec = get_logspec(audio_path, device)
        if args.segment:
            if args.seg_method == DEFAULT_SEG:
                feat = expand_logspec(logspec, M=args.seg_win)
            elif args.seg_method == TAIL_SEG:
                feat = torch.flipud(
                    expand_logspec(torch.flipud(logspec), M=args.seg_win))
            else:
                feat = segment_logspec(logspec, args.seg_win, args.seg_method)
        else:
            feat = expand_logspec(logspec)
        t_end = timer()
        logger.info(item[0] + "\tfeature extraction time: %s" %
                    (t_end - t_start))

        f_feat_path = os.path.join(args.feat_dir, item[0][:-4] + '.pt')
        torch.save(feat, f_feat_path)

    f_label.close()
    f_utt2label.close()

    global_end = timer()
    logger.info("#### Done logspec extraction for dataset: " + args.data_dir +
                "####")
    logger.info("Total elapsed time: %s" % (global_end - global_start))
Exemplo n.º 16
0
    def compute(self, s: torch.Tensor) -> torch.Tensor:
        if s.shape != self.pre.shape:
            raise Exception('Spikes shape is diffrent from pre shape!')

        return convolve(s, torch.flipud(torch.fliplr(self.kernel)))