def fmix(data, targets, alpha, decay_power, shape, max_soft=0.0):
    lam, mask = sample_mask(alpha, decay_power, shape, max_soft)
    indices = torch.randperm(data.size(0))
    shuffled_data = data[indices]
#     shuffled_targets = targets[indices]
    x1 = torch.from_numpy(mask)*data
    x2 = torch.from_numpy(1-mask)*shuffled_data
#     targets=(targets, shuffled_targets, lam)

    return (x1+x2), targets
Example #2
0
    def __call__(self, x):
        shape = [int(s) for s in x.shape][1:-1]
        lam, mask = sample_mask(self.alpha, self.decay_power, shape, self.max_soft, self.reformulate)
        index = np.random.permutation(int(x.shape[0]))
        index = tf.constant(index)
        mask = np.expand_dims(mask, -1)

        x1 = x * mask
        x2 = tf.gather(x, index) * (1 - mask)
        self.index = index
        self.lam = lam

        return x1 + x2
Example #3
0
    def __call__(self, x):
        # Sample mask and generate random permutation
        lam, mask = sample_mask(self.alpha, self.decay_power, self.size,
                                self.max_soft, self.reformulate)
        index = torch.randperm(x.size(0)).to(x.device)
        mask = torch.from_numpy(mask).float().to(x.device)

        # Mix the images
        x1 = mask * x
        x2 = (1 - mask) * x[index]
        self.index = index
        self.lam = lam
        return x1 + x2
Example #4
0
def fmix(data, target, alpha, decay_power, shape, max_soft=0.0, reformulate=False):
    lam, mask = sample_mask(alpha, decay_power, shape, max_soft, reformulate)

    indices = torch.randperm(data.size(0))
    shuffled_data = data[indices]
    shuffled_target = target[indices]

    x1 = torch.from_numpy(mask).to(data.device) * data
    x2 = torch.from_numpy(1 - mask).to(data.device) * shuffled_data
    targets = {
        'target': target,
        'shuffled_target': shuffled_target,
        'lam': lam
    }

    return x1 + x2, targets
Example #5
0
    def __call__(self, x):
        size = []
        for i, s in enumerate(self.size):
            if s != -1:
                size.append(s)
            else:
                size.append(x.shape[i + 1])

        lam, mask = sample_mask(self.alpha, self.decay_power, size,
                                self.max_soft, self.reformulate)
        index = torch.randperm(x.size(0)).to(x.device)
        mask = torch.from_numpy(mask).float().to(x.device)

        # Mix the images
        x1 = mask * x
        x2 = (1 - mask) * x[index]
        self.index = index
        self.lam = lam
        return x1 + x2
Example #6
0
def fmix(data,
         targets,
         alpha,
         decay_power,
         shape,
         device,
         max_soft=0.0,
         reformulate=False):
    data = data.detach().cpu()
    lam, mask = sample_mask(alpha, decay_power, shape, max_soft, reformulate)
    indices = torch.randperm(data.size(0))
    shuffled_data = data[indices]
    shuffled_targets = targets[indices]
    x1 = torch.from_numpy(mask) * data
    x2 = torch.from_numpy(1 - mask) * shuffled_data
    # x1 = x1.to(device)
    # x2 = x2.to(device)
    targets = (targets, shuffled_targets, lam)

    return (x1 + x2), targets
Example #7
0
    def __call__(self, x):
        # Sample mask and generate random permutation
        lam, mask = sample_mask(self.alpha, self.decay_power, self.size,
                                self.max_soft, self.reformulate)
        index = torch.randperm(x.size(0)).to(x.device)
        mask = torch.from_numpy(mask).float().to(x.device)

        # Mix the images
        x1 = mask * x
        x2 = (1 - mask) * x[index]
        self.index = index
        self.lam = lam

        if lam >= 0.5:
            x_cut = x1
        else:
            x_cut = x2

        masky = mask.cpu()
        #print(masky.numpy())
        #np.save("mask.txt",masky.numpy())
        return x1 + x2, x1, x2