|
25 | 25 | from __future__ import absolute_import, division, print_function, unicode_literals |
26 | 26 |
|
27 | 27 | import logging |
28 | | -from typing import Optional, Tuple, TYPE_CHECKING |
| 28 | +from typing import Tuple, Optional, Union, TYPE_CHECKING |
29 | 29 |
|
30 | 30 | import numpy as np |
31 | 31 | import scipy |
@@ -105,7 +105,7 @@ def __init__( |
105 | 105 | batch_size: int = 32, |
106 | 106 | use_amp: bool = False, |
107 | 107 | opt_level: str = "O1", |
108 | | - loss_scale: int = 1, |
| 108 | + loss_scale: Optional[Union[float, str]] = 1.0, |
109 | 109 | ): |
110 | 110 | """ |
111 | 111 | Create a :class:`.ImperceptibleASRPytorch` instance. |
@@ -144,9 +144,10 @@ def __init__( |
144 | 144 | only triggered if there are GPUs available. |
145 | 145 | :param opt_level: Specify a pure or mixed precision optimization level. Used when use_amp is True. Accepted |
146 | 146 | values are `O0`, `O1`, `O2`, and `O3`. |
147 | | - :param loss_scale: Loss scaling. Used when use_amp is True. Default is 1 due to warp-ctc not supporting |
| 147 | + :param loss_scale: Loss scaling. Used when use_amp is True. Default is 1.0 due to warp-ctc not supporting |
148 | 148 | scaling of gradients. |
149 | 149 | """ |
| 150 | + import torch # lgtm [py/repeated-import] |
150 | 151 | from torch.autograd import Variable |
151 | 152 |
|
152 | 153 | if ( |
@@ -237,6 +238,8 @@ def generate(self, x: np.ndarray, y: np.ndarray, **kwargs) -> np.ndarray: |
237 | 238 | class only supports targeted attack. |
238 | 239 | :return: An array holding the adversarial examples. |
239 | 240 | """ |
| 241 | + import torch # lgtm [py/repeated-import] |
| 242 | + |
240 | 243 | # Start to compute adversarial examples |
241 | 244 | adv_x = x.copy() |
242 | 245 |
|
@@ -276,6 +279,8 @@ def _generate_batch(self, x: np.ndarray, y: np.ndarray) -> np.ndarray: |
276 | 279 | class only supports targeted attack. |
277 | 280 | :return: A batch of adversarial examples. |
278 | 281 | """ |
| 282 | + import torch # lgtm [py/repeated-import] |
| 283 | + |
279 | 284 | # First stage of attack |
280 | 285 | successful_adv_input_1st_stage, original_input = self._attack_1st_stage(x=x, y=y) |
281 | 286 | successful_perturbation_1st_stage = successful_adv_input_1st_stage - torch.tensor(original_input).to( |
@@ -325,6 +330,8 @@ class only supports targeted attack. |
325 | 330 | - A tensor holding the candidate adversarial examples. |
326 | 331 | - An array holding the original inputs. |
327 | 332 | """ |
| 333 | + import torch # lgtm [py/repeated-import] |
| 334 | + |
328 | 335 | # Compute local shape |
329 | 336 | local_batch_size = len(x) |
330 | 337 | real_lengths = np.array([x_.shape[0] for x_ in x]) |
@@ -493,6 +500,8 @@ class only supports targeted attack. |
493 | 500 | :param original_max_psd_batch: Original maximum psd. |
494 | 501 | :return: An array holding the candidate adversarial examples. |
495 | 502 | """ |
| 503 | + import torch # lgtm [py/repeated-import] |
| 504 | + |
496 | 505 | # Compute local shape |
497 | 506 | local_batch_size = len(x) |
498 | 507 | real_lengths = np.array([x_.shape[0] for x_ in x]) |
@@ -596,6 +605,8 @@ def _forward_2nd_stage( |
596 | 605 | :param original_max_psd_batch: Original maximum psd. |
597 | 606 | :return: The loss tensor of the second stage of the attack. |
598 | 607 | """ |
| 608 | + import torch # lgtm [py/repeated-import] |
| 609 | + |
599 | 610 | # Compute loss for masking threshold |
600 | 611 | losses = [] |
601 | 612 | relu = torch.nn.ReLU() |
@@ -744,6 +755,7 @@ def _psd_transform(self, delta: "torch.Tensor", original_max_psd: "torch.Tensor" |
744 | 755 | :param original_max_psd: The maximum psd of the original audio. |
745 | 756 | :return: The psd matrix. |
746 | 757 | """ |
| 758 | + import torch # lgtm [py/repeated-import] |
747 | 759 | import torchaudio |
748 | 760 |
|
749 | 761 | # These parameters are needed for the transformation |
|
0 commit comments