signal-grad-cam 0.1.1__tar.gz → 0.1.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of signal-grad-cam might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: signal_grad_cam
3
- Version: 0.1.1
3
+ Version: 0.1.2
4
4
  Summary: SignalGrad-CAM aims at generalising Grad-CAM to one-dimensional applications, while enhancing usability and efficiency.
5
5
  Home-page: https://github.com/samuelepe11/signal_grad_cam
6
6
  Author: Samuele Pe
@@ -5,7 +5,7 @@ with open("README.md", "r") as f:
5
5
 
6
6
  setup(
7
7
  name="signal_grad_cam",
8
- version="0.1.1",
8
+ version="0.1.2",
9
9
  description="SignalGrad-CAM aims at generalising Grad-CAM to one-dimensional applications, while enhancing usability"
10
10
  " and efficiency.",
11
11
  keywords="XAI, class activation maps, CNN, time series",
@@ -183,9 +183,26 @@ class TorchCamBuilder(CamBuilder):
183
183
  # during derivation
184
184
  target_scores = torch.log(outputs)
185
185
  target_probs = outputs
186
+
187
+ # Adjust results for binary network
188
+ if len(outputs.shape) == 1:
189
+ target_scores = torch.stack([-target_scores, target_scores], dim=1)
190
+ target_probs = torch.stack([1 - target_probs, target_probs], dim=1)
191
+ elif len(outputs.shape) == 2 and outputs.shape[1] == 1:
192
+ target_scores = torch.cat([-target_scores, target_scores], dim=1)
193
+ target_probs = torch.cat([1 - target_probs, target_probs], dim=1)
186
194
  else:
195
+ if len(outputs.shape) == 1:
196
+ outputs = torch.stack([-outputs, outputs], dim=1)
197
+ elif len(outputs.shape) == 2 and outputs.shape[1] == 1:
198
+ outputs = torch.cat([-outputs, outputs], dim=1)
187
199
  target_scores = outputs
188
- target_probs = torch.softmax(target_scores, dim=1)
200
+
201
+ if len(outputs.shape) == 2 and outputs.shape[1] > 1:
202
+ target_probs = torch.softmax(target_scores, dim=1)
203
+ else:
204
+ tmp = torch.sigmoid(target_scores[:, 1])
205
+ target_probs = torch.stack([1 - tmp, tmp], dim=1)
189
206
 
190
207
  target_probs = target_probs[:, target_class].cpu().detach().numpy()
191
208
 
@@ -189,7 +189,7 @@ class TfCamBuilder(CamBuilder):
189
189
 
190
190
  grad_model = keras.models.Model(self.model.inputs, [target_layer.output, self.model.output])
191
191
  extra_inputs_list = extra_inputs_list or []
192
- with tf.GradientTape() as tape:
192
+ with (tf.GradientTape() as tape):
193
193
  self.activations, outputs = grad_model([data_batch] + extra_inputs_list)
194
194
 
195
195
  if softmax_final:
@@ -197,9 +197,26 @@ class TfCamBuilder(CamBuilder):
197
197
  # during derivation
198
198
  target_scores = tf.math.log(outputs)
199
199
  target_probs = outputs
200
+
201
+ # Adjust results for binary network
202
+ if len(outputs.shape) == 1:
203
+ target_scores = tf.stack([-target_scores, target_scores], axis=1)
204
+ target_probs = tf.stack([1 - target_probs, target_probs], axis=1)
205
+ elif len(outputs.shape) == 2 and outputs.shape[1] == 1:
206
+ target_scores = tf.concat([-target_scores, target_scores], axis=1)
207
+ target_probs = tf.concat([1 - target_probs, target_probs], axis=1)
200
208
  else:
209
+ if len(outputs.shape) == 1:
210
+ outputs = tf.stack([-outputs, outputs], axis=1)
211
+ elif len(outputs.shape) == 2 and outputs.shape[1] == 1:
212
+ outputs = tf.concat([-outputs, outputs], axis=1)
201
213
  target_scores = outputs
202
- target_probs = tf.nn.softmax(target_scores, axis=1)
214
+
215
+ if len(outputs.shape) == 2 and outputs.shape[1] > 1:
216
+ target_probs = tf.nn.softmax(target_scores, axis=1)
217
+ else:
218
+ tmp = tf.math.sigmoid(target_scores[:, 1])
219
+ target_probs = tf.stack([1 - tmp, tmp], axis=1)
203
220
 
204
221
  target_scores = target_scores[:, target_class]
205
222
  target_probs = target_probs[:, target_class]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: signal-grad-cam
3
- Version: 0.1.1
3
+ Version: 0.1.2
4
4
  Summary: SignalGrad-CAM aims at generalising Grad-CAM to one-dimensional applications, while enhancing usability and efficiency.
5
5
  Home-page: https://github.com/samuelepe11/signal_grad_cam
6
6
  Author: Samuele Pe
File without changes