Ejemplo n.º 1
0
    def simulate(self, batch_size, dt, seq_len, verbose=False):
        """
           conducts simulation of the process with model parameters

           input:
                  batch_size - int, number of sequences to generate
                  dt - Tensor like, size = (batch_size), delta time during the generation (Poisson = Poisson(lambda*dt))
                  seq_len - int, sequence length
                  verbose - bool, if True, print the info during generation

           output:
                  sequences - torch.Tensor, size = (batch_size, seq_len, num_classes), simulated data
        """
        with torch.no_grad():
            self.eval()

            # result template
            res = torch.zeros(batch_size, seq_len, 1 + self.num_classes)

            # initial hidden and cell state preprocessing
            hidden0 = self.hidden0[:, None, :]
            cell0 = self.cell0[:, None, :]

            # iterations over batch
            for b in range(batch_size):
                if verbose:
                    print('Generating batch {}/{}'.format(b + 1, batch_size))
                # iteration over sequence
                for s in range(seq_len):
                    if verbose and s % 100 == 0:
                        print('>>> Generating sequence step {}/{}'.format(
                            s + 1, seq_len))

                    # first iteration doesn't depend on the history and should be processed independently
                    if s == 0:
                        # computing lambda
                        if self.bidir:
                            h0 = hidden0[-3:, :, :].reshape(-1)
                        else:
                            h0 = hidden0[-1, :, :].reshape(-1)
                        lambdas = self.f(h0 @ self.W)
                        # simulation
                        res[b, s, 1:] = torch.poisson(lambdas * dt[b])
                        res[b, s, 0] = dt[b]
                        hidden = hidden0.clone()
                        cell = cell0.clone()

                    else:
                        # computing lambda
                        o, (hidden,
                            cell) = self.lstm(res[b, s - 1][None, None, :],
                                              (hidden, cell))
                        lambdas = self.f(o[0, -1, :] @ self.W)
                        # simulation
                        res[b, s, 1:] = torch.poisson(lambdas * dt[b])
                        res[b, s, 0] = dt[b]

            return res
Ejemplo n.º 2
0
def add_poisson_gaussian_noise(x, a, b):
    if a == 0:
        z = x + math.sqrt(b) * torch.randn(*x.shape)
    else:
        chi = 1 / a
        z = torch.poisson(chi * x) / chi + math.sqrt(b) * torch.randn(*x.shape)
    return torch.max(torch.zeros_like(z), torch.min(torch.ones_like(z), z))
Ejemplo n.º 3
0
    def infill_and_mask_tokens(self, input_ids):
        """
        Prepares masked tokens input and label pairs for text-infilled MLM.

        :param (torch.Tensor) input_ids: tensor with shape (B, S), contains sequences
        :return: (torch.Tensor, torch.Tensor) input_ids, labels
        """
        # In masked language modeling, the labels are the input itself (i.e. self-supervision)
        labels = input_ids.clone()
        # Initialize the masked indices with all zeros
        masked_indices = torch.full(labels.shape, fill_value=0.0, dtype=torch.bool)

        # Do for each example in batch
        for i in range(labels.shape[0]):
            # Start from the beginning of the sequence; j is the num. of tokens processed
            j = 0
            while j < labels.shape[1]:
                # Sample a span_length (i.e. length of span of tokens to be masked)
                span_length = torch.poisson(torch.tensor(self.mean_span_length, dtype=torch.float)).long().item()
                if span_length >= labels.shape[1]:
                    span_length = self.mean_span_length
                # Reserve a context of length to surround the span to be masked
                context_length = int(span_length / self.mask_probability)
                # Sample a starting point and mask tokens in the computed span
                start_index = j + torch.randint(context_length-span_length + 1, (1,)).item()
                if start_index >= labels.shape[1]:
                    break
                # Simply replace all tokens in the span with [MASK] tokens if 'spanbert'
                if self.mask_strategy == 'spanbert':
                    masked_indices[i, start_index: start_index+span_length] = 1
                # Replace all tokens in the span with a single [MASK] token and add n-1 [PAD] tokens
                elif self.mask_strategy == 'bart':
                    masked_indices[i, start_index] = 1
                    input_ids[i, start_index+1:] = torch.cat(
                        [input_ids[i, start_index+span_length:],
                         torch.full(size=(span_length-1, ), fill_value=self.tokenizer.pad_token_id, dtype=torch.long)]
                    )
                # Update num. tokens processed
                j += context_length

        # Store all of the special indices, those set by self.special_token_ids, in a mask
        all_special_indices = torch.zeros(labels.shape).bool()
        # Check if certain other token (e.g. [PAD], [UNK], etc.) exists in the tokenizer
        for special_token_id in self.special_token_ids:
            if special_token_id:
                # Get the related indices in the input IDs
                special_indices = labels.eq(special_token_id)
                all_special_indices |= special_indices
                # Fill in special indices with 0.0 - we don't want them masked
                masked_indices.masked_fill_(mask=special_indices, value=0.0)

        # Mask the selected indices in input IDs and and set everything else to a large, negative
        # number in labels to ensure that loss is not computed on them
        input_ids[masked_indices] = self.tokenizer.mask_token_id
        if self.mask_strategy == 'spanbert':
            labels[~masked_indices] = self.nomask_id
        elif self.mask_strategy == 'bart':
            labels[all_special_indices] = self.nomask_id

        return input_ids, labels
Ejemplo n.º 4
0
def test_abp_global_search(cfg, clean, noisy_img=None):

    # -- create vars for search --
    N, B, C, H, W = clean.shape
    PS = cfg.patchsize
    NH = cfg.nh_size

    # -- setup for best_indices --
    patches = tile_burst_patches(clean, PS, NH)

    # -- add noise for testing --
    if noisy_img is None:
        alpha = 4.
        noisy = torch.poisson(alpha * (patches + 0.5)) / alpha
    else:
        noisy = tile_burst_patches(noisy_img, PS, NH)

    # -- run search --
    scores, indices = test_run_abp_patch_search_exhaustive_global_dynamics(
        noisy, patches, PS, NH)
    indices.cuda(non_blocking=True)

    # -- recover aligned burst images --
    aligned = aligned_burst_image_from_indices_global_dynamics(
        patches, np.arange(N), indices)

    ave = torch.mean(aligned, dim=0)

    return indices, ave, aligned
Ejemplo n.º 5
0
 def sample(self, sample_shape=torch.Size()):
     shape = self._extended_shape(sample_shape)
     with torch.no_grad():
         mask = torch.bernoulli(self.gate.expand(shape)).byte()
         samples = torch.poisson(self.rate.expand(shape))
         samples.masked_fill_(mask, 0.)
     return samples
Ejemplo n.º 6
0
Archivo: util.py Proyecto: ryul99/mmsr
def add_noise(image, noise_mode=''):
    """
    ref: https://github.com/scikit-image/scikit-image/blob/master/skimage/util/noise.py#L8
    :param image:
    :return noisy image:
    """
    if torch.min(image) < 0:
        low_clip = -1.
    else:
        low_clip = 0.

    noise_mode = noise_mode.lower()
    if noise_mode == 'gaussian':
        mean = 0.0
        var = 0.01
        noise_map = image.clone().normal_(mean, var**0.5)
        out = image + noise_map
    elif noise_mode == 'poisson':
        # Determine unique values in image & calculate the next power of two
        vals = torch.tensor(len(torch.unique(image)) * 1.)
        vals = 2**torch.ceil(torch.log2(vals))
        # Ensure image is exclusively positive
        if low_clip == -1.:
            old_max = torch.max(image)
            image = (image + 1.) / (old_max + 1.)
        # Generating noise for each unique value in image.
        out = torch.poisson(image * vals) / float(vals)
        # Return image to original range if input was signed
        if low_clip == -1.:
            out = out * (old_max + 1.) - 1.
    else:
        raise NotImplementedError
    out = torch.clamp(out, low_clip, 1.0)
    return out
Ejemplo n.º 7
0
 def __call__(self,pic):
     pic_bw = tvF.rgb_to_grayscale(pic,1)
     poisson_pic = torch.poisson(self.alpha*pic_bw,generator=self.seed)/self.alpha
     if pic.shape[-3] == 3:
         repeat = [1 for i in pic.shape]
         repeat[-3] = 3
         poisson_pic = poisson_pic.repeat(*(repeat))
     return poisson_pic
def shot_noise(x: Tensor, severity: int = 1) -> Tensor:
    """Applys shot noise."""
    imagenet_rate_mult = [60, 25, 12, 5, 3]
    cifar_rate_mult = [500, 250, 100, 75, 50]
    rate_mult = interpolate_severity(x, cifar_rate_mult, imagenet_rate_mult,
                                     severity)
    # very likely the clamp is not needed, but numerical stability...
    return (torch.poisson(x * rate_mult) / rate_mult).clamp(min=0.0, max=1.0)
Ejemplo n.º 9
0
def generate_poisson_noise_pt(img, scale=1.0, gray_noise=0):
    """Generate a batch of poisson noise (PyTorch version)

    Args:
        img (Tensor): Input image, shape (b, c, h, w), range [0, 1], float32.
        scale (float | Tensor): Noise scale. Number or Tensor with shape (b).
            Default: 1.0.
        gray_noise (float | Tensor): 0-1 number or Tensor with shape (b).
            0 for False, 1 for True. Default: 0.

    Returns:
        (Tensor): Returned noisy image, shape (b, c, h, w), range[0, 1],
            float32.
    """
    b, _, h, w = img.size()
    if isinstance(gray_noise, (float, int)):
        cal_gray_noise = gray_noise > 0
    else:
        gray_noise = gray_noise.view(b, 1, 1, 1)
        cal_gray_noise = torch.sum(gray_noise) > 0
    if cal_gray_noise:
        img_gray = rgb_to_grayscale(img, num_output_channels=1)
        # round and clip image for counting vals correctly
        img_gray = torch.clamp((img_gray * 255.0).round(), 0, 255) / 255.
        # use for-loop to get the unique values for each sample
        vals_list = [len(torch.unique(img_gray[i, :, :, :])) for i in range(b)]
        vals_list = [2**np.ceil(np.log2(vals)) for vals in vals_list]
        vals = img_gray.new_tensor(vals_list).view(b, 1, 1, 1)
        out = torch.poisson(img_gray * vals) / vals
        noise_gray = out - img_gray
        noise_gray = noise_gray.expand(b, 3, h, w)

    # always calculate color noise
    # round and clip image for counting vals correctly
    img = torch.clamp((img * 255.0).round(), 0, 255) / 255.
    # use for-loop to get the unique values for each sample
    vals_list = [len(torch.unique(img[i, :, :, :])) for i in range(b)]
    vals_list = [2**np.ceil(np.log2(vals)) for vals in vals_list]
    vals = img.new_tensor(vals_list).view(b, 1, 1, 1)
    out = torch.poisson(img * vals) / vals
    noise = out - img
    if cal_gray_noise:
        noise = noise * (1 - gray_noise) + noise_gray * gray_noise
    if not isinstance(scale, (float, int)):
        scale = scale.view(b, 1, 1, 1)
    return noise * scale
Ejemplo n.º 10
0
 def poisson_noise_loader(self, size, numpyData=False, seed=-1):
     if seed != -1:
         if numpyData:
             np.random.seed(self.data_seed)
         else:
             torch.random.manual_seed(self.data_seed)
     if numpyData:
         # more data means all data after #0 are signal cases
         data = []
         if len(self.mean_data) > 2 and self.signal_no_signal:
             labels = np.random.randint(2, size=size)
             for l in labels:
                 if l == 0:
                     data.append(np.random.poisson(self.mean_data[l]))
                 else:
                     selector = np.random.randint(1, len(self.mean_data))
                     data.append(np.random.poisson(
                         self.mean_data[selector]))
         else:
             labels = np.random.randint(len(self.mean_data), size=size)
             for l in labels:
                 data.append(np.random.poisson(self.mean_data[l]))
         data = np.stack(data)
         return data, labels
     else:
         data = []
         if len(self.mean_data) > 2 and self.signal_no_signal:
             labels = torch.randint(2, (size, )).type(torch.long)
             for l in labels:
                 if l == 0:
                     data.append(torch.poisson(self.mean_data[l]))
                 else:
                     selector = np.random.randint(1, len(self.mean_data))
                     data.append(torch.poisson(self.mean_data[selector]))
         else:
             labels = torch.randint(len(self.mean_data),
                                    (size, )).type(torch.long)
             for l in labels:
                 data.append(torch.poisson(self.mean_data[l]))
         data = torch.stack(data)
         return data, labels
Ejemplo n.º 11
0
def span_dropout(x, dropout=0.15):
    if len(x) == 1:
        return x
    span_size = torch.poisson(torch.ones(
        1, 1)).long().item()  #random.choice(range(min(4, len(x))))
    if span_size == 0 or span_size > (len(x) - 1):
        return x
    mask_ix = random.choice(range(len(x)))
    return torch.cat([
        x[:mask_ix],
        torch.zeros((1, x.shape[-1])),
        x[mask_ix + span_size:],
    ]), (mask_ix, span_size)
Ejemplo n.º 12
0
def test_PoissonNLLLoss_direct(reduction, log_input, full):
    torch.manual_seed(42)

    model = torch.nn.PoissonNLLLoss(log_input, full, reduction=reduction)
    poptorch_model = poptorch.inferenceModel(model)

    target = torch.poisson(torch.rand(10) * 5)
    input = torch.empty(10).uniform_()

    native_out = model(input, target)
    poptorch_out = poptorch_model(input, target)

    torch.testing.assert_allclose(native_out, poptorch_out)
Ejemplo n.º 13
0
def add_noise(image, mode='poisson', psnr=25, noisy_per_clean=2, clip=False):
    """This function is called to create noisy images, after getting minibatch
    of clean images from dataloader.
    Works well for non-saturating images, uint8.

    Different implementation of scaling of pixel values for the mean of
    Poisson noise.

    References:
        https://github.com/scikit-image/scikit-image/blob/master/skimage/util/noise.py
        https://www.mathworks.com/help/images/ref/imnoise.html#mw_226e1fb2-f53a-4e49-9bb1-6b167fc2eac1
        http://reference.wolfram.com/language/ref/ImageEffect.html
        https://imagej.nih.gov/ij/plugins/poisson-noise.html

    Args:
        image (torch.Tensor): image after `torchvision.transforms.ToTensor`,
            range [0.0, 1.0], (B, C, H, W)
        mode (str): Default `Poisson`, other kinds of noise on the way...
        psnr (float): Peak-SNR in DB. If it is list of size 2, then uniformly
            select one psnr from this range
        noisy_per_clean (int): return number of noisy images per clean image
        clip (bool): clip the noisy output or not
    """

    if mode == 'poisson':
        if image.dtype == torch.uint8:
            max_val = 255
        elif image.dtype == torch.int16:
            max_val = 32767 if image.max() > 4095 else 4095
        else:
            raise TypeError('image data type is expected to be either uint8 ' \
                            'or int16, but got {}'.format(image.dtype))
        if noisy_per_clean > 1:
            image = image.repeat(noisy_per_clean, 1, 1, 1)
        image = image.float()

        if isinstance(psnr, (list, tuple)):
            assert len(psnr) == 2, 'please specify the range of PSNR using ' \
                                   'only two numbers'
            # randomly select noise level for each channel
            psnr = torch.randn(image.shape[0]).uniform_(psnr[0], psnr[1]).to(
                image.device)
        scale = 10**(psnr / 10) * image.view(image.size(0),
                                             -1).mean(1) / max_val**2
        scale = scale.view(image.size(0), 1, 1, 1)
        noisy = torch.poisson(image * scale) / scale
        return torch.clamp(noisy, 0., max_val) if clip else noisy

    else:
        raise NotImplementedError('Other noise mode to be implemented')
Ejemplo n.º 14
0
    def _transform(
            self, x: "torch.Tensor", y: Optional["torch.Tensor"],
            **kwargs) -> Tuple["torch.Tensor", Optional["torch.Tensor"]]:
        """
        Transformation of an image with randomly sampled shot (Poisson) noise.

        :param x: Input samples.
        :param y: Label of the samples `x`.
        :return: Transformed samples and labels.
        """
        import torch  # lgtm [py/repeated-import]

        lam_i = np.random.uniform(low=self.lam_range[0],
                                  high=self.lam_range[1])
        delta_i = torch.poisson(input=torch.ones_like(x) *
                                lam_i) / lam_i * self.clip_values[1]
        return torch.clamp(x + delta_i,
                           min=self.clip_values[0],
                           max=self.clip_values[1]), y
Ejemplo n.º 15
0
 def __call__(self,pic):
     """
     :params pic: input image shaped [...,C,H,W]
     
     we assume C = 3 and then we convert it to BW. 
     """
     # if pic.max() <= 1: pic *= 255.
     # print("noise",torch.get_rng_state())
     device = pic.device
     pix_max = 2**self.nbits-1
     pic_bw = tvF.rgb_to_grayscale(pic,1)
     ll_pic = torch.poisson(self.alpha*pic_bw,generator=self.seed)
     ll_pic += self.read_noise*torch.randn(ll_pic.shape,device=device)
     if pic.shape[-3] == 3: ll_pic = self._add_color_channel(ll_pic)
     if self.use_adc:
         ll_pic = torch.round(ll_pic)
         ll_pic = torch.clamp(ll_pic, 0, pix_max)
     ll_pic /= self.alpha
     return ll_pic
Ejemplo n.º 16
0
    def forward(self, input_irrad_gray):
        # This function converts the gray values to electrons, applies noise and converts back to gray values
        if self.generator.device != input_irrad_gray.device:
            self.generator = torch.Generator(device=input_irrad_gray.device)
        # todo: store normalization value
        input_irrad_gray *= self.full_well

        # Convert gray to photons
        input_irrad_electrons = input_irrad_gray - self.baseline
        input_irrad_electrons = input_irrad_electrons / self.sensitivity  #* self.full_well / (2**self.bit_depth-1)
        input_irrad_electrons[input_irrad_electrons < 0] = 0.0

        input_irrad_photons = input_irrad_electrons / self.quantum_eff

        # Add shot noise
        noisy_image = input_irrad_photons.detach() - torch.poisson(
            input_irrad_photons.detach(), generator=self.generator)
        photons = input_irrad_photons + noisy_image

        # Convert to electrons
        electrons = self.quantum_eff * photons

        # Add dark noise
        electrons_out = torch.normal(mean=0.0,
                                     std=self.dark_noise,
                                     generator=self.generator) + electrons

        # Convert to ADU and add baseline
        max_adu = torch.tensor([2**self.bit_depth - 1],
                               dtype=torch.float32,
                               device=input_irrad_gray.device)
        adu = (electrons_out *
               self.sensitivity).float()  # Convert to discrete numbers
        adu += self.baseline
        adu[adu > max_adu] = max_adu  # models pixel saturation

        return adu / self.full_well
Ejemplo n.º 17
0
 def sample(self, sample_shape=torch.Size()):
     shape = self._extended_shape(sample_shape)
     with torch.no_grad():
         return torch.poisson(self.rate.expand(shape))
Ejemplo n.º 18
0
# parser.add_argument('-visdom', '--visdom_flag', action="store_true", help='Whether plotting in visdom is desired')
# parser.add_argument('-i-tsne', '--tsne_iter', default=100, type=int, help='epoch when tsne visualization runs')
args = parser.parse_args()

expn_pth = '/n/data_02/Basset/data/expn/roadmap/57epigenomes.RPKM.pc'
print("Reading gene expression data from:\n{}".format(expn_pth))
# Gene expression dataset
expn = pd.read_table(expn_pth,header=0)
col_names = expn.columns.values[1:]
expn = expn.drop(col_names[-1],axis=1) # 19795*57 right now # TODO: is this all right?
expn.columns = col_names
pinned_lookup = torch.nn.Embedding.from_pretrained(torch.FloatTensor(expn.as_matrix().T[1:]),freeze=True) # [1:] is new!
pinned_lookup.cuda()

torch.manual_seed(3435)
imgs = torch.poisson(pinned_lookup.weight) # discretize data
# imgs = pinned_lookup.weight.round()
# imgs = pinned_lookup.weight
dat = torch.utils.data.TensorDataset(imgs, torch.zeros(56,1)) # placeholder arg required pytorch <0.4.0...
loader = torch.utils.data.DataLoader(dat, batch_size=args.batch_size, shuffle=False)
print(next(iter(loader))[0].size())

# setup the VAE
vae = PyroVAE(latent_dim=args.latent_dim)
adam_args = {"lr": args.learning_rate}
optimizer = Adam(adam_args)
svi = SVI(vae.model, vae.guide, optimizer, loss=Trace_ELBO())

# setup visdom for visualization
if args.visdom_flag:
    vis = visdom.Visdom()