Skip to content

Using the clip_min / clip_max argument causes TypeError #1248

Description

@asim29

https://github.com/cleverhans-lab/cleverhans/blob/574efc1d2f5c7e102c78cf0e937654e847267522/cleverhans/torch/attacks/projected_gradient_descent.py#L152C1-L153C1

I'm using the following code:

        for x, y in self.test_loader:
            x, y = x.to(self.device), y.to(self.device)
            x_pgd = projected_gradient_descent(
                self.model,
                x,
                self.epsilon,
                self.step_size,
                self.iterations,
                np.inf,
                clip_min=self.clip_min,
                clip_max=self.clip_max,
            )

            adv_input.append(x_pgd.detach().cpu().numpy())
            labels.append(y.detach().cpu().numpy())

Where:

  • self.model is a standard VGG model
  • self.test_loader is a DataLoader created from torchvision.datasets.CIFAR10
  • self.clip_min is 0.0
  • self.clip_max is 1.0
  • self.epsilon is 0.01
  • self.step_size is 0.01
  • self.iterations is 40

I get the following error:

File ".../.venv/lib/python3.11/site-packages/cleverhans/torch/attacks/projected_gradient_descent.py", line 152, in projected_gradient_descent
    assert np.all(asserts)
           ^^^^^^^^^^^^^^^
  File ".../.venv/lib/python3.11/site-packages/numpy/core/fromnumeric.py", line 2504, in all
    return _wrapreduction(a, np.logical_and, 'all', axis, None, out,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../.venv/lib/python3.11/site-packages/numpy/core/fromnumeric.py", line 88, in _wrapreduction
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../.venv/lib/python3.11/site-packages/torch/_tensor.py", line 1087, in __array__
    return self.numpy()
           ^^^^^^^^^^^^
TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

The error goes away when I do not use the clip_min and clip_max arguments.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions