コード例 #1
0
ファイル: attribute.py プロジェクト: ttumiel/interpret
    def __mul__(self, other):
        if isinstance(other, Attribute):
            self_data, other_data = self.data, other.data

            # Repeat along missing colour dimensions if necessary
            if self.data.ndim < other.data.ndim:
                self_data = self.data.unsqueeze(0).repeat(
                    other.data.shape[0], 1, 1)
            elif self.data.ndim > other.data.ndim:
                other_data = other.data.unsqueeze(0).repeat(
                    self.data.shape[0], 1, 1)

            # Compare shape of data tensors
            if self_data.shape != other_data.shape:
                self_data = denorm(self_data)
                other_data = denorm(other_data)
                if self_data.size > other_data.size:
                    other_data = other_data.resize(self_data.size, resample=2)
                else:
                    self_data = self_data.resize(other_data.size, resample=2)

                self_data = norm(self_data, unsqueeze=False, grad=False)
                other_data = norm(other_data, unsqueeze=False, grad=False)

            return Attribute(self_data * other_data, self.input_data)
        elif isinstance(other, (int, float)):
            return Attribute(self.data * other, self.input_data)
        else:
            raise ValueError(f"Can't multiply by type {type(other)}")
コード例 #2
0
def test_norm_denorm():
    img = Image.fromarray(np.random.random((32, 32, 3)), 'RGB')

    data = norm(img)
    denorm_data = denorm(data, image=False)

    assert np.isclose(np.array(img), denorm_data).all()
コード例 #3
0
def test_norm():
    data = (np.random.random((32, 32, 3)) * 255).astype('uint8')
    img = Image.fromarray(data)
    data = norm(img,
                mean=(data / 255).mean((0, 1)),
                std=(data / 255).std((0, 1)))

    assert data.mean().item() == pytest.approx(0., abs=1e-6)
    assert data.std().item() == pytest.approx(1., 1e-3)
コード例 #4
0
ファイル: param.py プロジェクト: ttumiel/interpret
    def forward(self):
        im = self.cppn(self.inp)

        if self.decorrelate:
            im = _linear_decorrelate_color(im)

        if self.sigmoid:
            im = torch.sigmoid(im)
            im = norm(im, input_range=(0,1), unsqueeze=False, grad=False)

        return im
コード例 #5
0
ファイル: param.py プロジェクト: ttumiel/interpret
    def forward(self):
        im = self.get_image(self.noise)

        if self.decorrelate:
            im = _linear_decorrelate_color(im)

        if self.sigmoid:
            # if we sigmoid, we should also normalize
            im = torch.sigmoid(im)
            im = norm(im, input_range=(0,1), unsqueeze=False, grad=False, mean=self.mean, std=self.std)

        return im
コード例 #6
0
ファイル: param.py プロジェクト: ttumiel/interpret
def random_im(size, sd=0.5, device='cuda' if torch.cuda.is_available() else 'cpu'):
    "Create a random 'image' from a normal distribution"
    # im = torch.randn(1, 3, size, size, device=device)*sd
    im = norm(torch.tensor(np.random.uniform(150, 180, (size, size, 3)), device=device).permute(2,0,1).float())
    return im, lambda x: x