-
Notifications
You must be signed in to change notification settings - Fork 0
/
ucf101_oudefend_old.py
444 lines (363 loc) · 16.4 KB
/
ucf101_oudefend_old.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import os
import sys
import json
import subprocess
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
from torch.autograd import Variable
from PIL import Image
from spatial_transforms import Compose, Scale, CenterCrop
from model import generate_model
from utils import *
from typing import Any, Dict, List, Optional, Tuple, Union, TYPE_CHECKING
from armory.data.utils import maybe_download_weights_from_s3
from art.classifiers import PyTorchClassifier
from art.utils import Deprecated
def detector_and_model(detector, model, inputs, spatial_transform):
#temp_root = 'image.jpg'
#frame_num = len(inputs[0,0,:,0,0])
#batch_num = len(inputs[:,0,0,0,0])
#for bb in range(batch_num):
# for kk in range(frame_num):
# inputs_frame = torchvision.transforms.ToPILImage()(inputs[bb,:,kk,:,:].cpu())
# inputs_frame.save(temp_root)
# inputs_frame = Image.open(temp_root).convert('RGB')
# inputs_frame = spatial_transform(inputs_frame)
# inputs[bb,:,kk,:,:] = inputs_frame
#
#outputs = detector(inputs)
#
#_, type_pred = outputs.topk(1, 1, True)
#type_pred = type_pred.t().cpu().numpy()
#
#outputs = model(inputs, type_pred[:,0])
outputs = model(inputs)
return outputs
class MyPytorchClassifier(PyTorchClassifier):
def __init__(self, my_detector, my_model, spatial_transform,
model=nn.Conv2d(1, 3, 1, bias=False),
loss=nn.CrossEntropyLoss(),
input_shape=(3, 40, 112, 112),
nb_classes=101,
optimizer: Optional["torch.optim.Optimizer"] = None, # type: ignore
channel_index=Deprecated,
channels_first: bool = True,
clip_values=(0, 1,),
preprocessing_defences: Union["Preprocessor", List["Preprocessor"], None] = None,
postprocessing_defences: Union["Postprocessor", List["Postprocessor"], None] = None,
preprocessing = (0, 1),
device_type: str = "gpu",
):
# Remove in 1.5.0
if channel_index == 3:
channels_first = False
elif channel_index == 1:
channels_first = True
elif channel_index is not Deprecated:
raise ValueError("Not a proper channel_index. Use channels_first.")
super(MyPytorchClassifier, self).__init__(
model=model,
loss=loss,
input_shape=input_shape,
nb_classes=nb_classes,
optimizer=optimizer,
channel_index=channel_index,
channels_first=channels_first,
clip_values=clip_values,
preprocessing_defences=preprocessing_defences,
postprocessing_defences=postprocessing_defences,
preprocessing=preprocessing,
device_type=device_type,
)
self.my_detector = my_detector
self.my_model = my_model
self.spatial_transform = spatial_transform
cuda_idx = torch.cuda.current_device()
self._device = torch.device("cuda:{}".format(cuda_idx))
self.my_detector.to(self._device)
self.my_model.to(self._device)
def predict(self, x: np.ndarray, batch_size: int = 128, **kwargs) -> np.ndarray:
"""
Perform prediction for a batch of inputs.
:param x: Test set.
:param batch_size: Size of batches.
:return: Array of predictions of shape `(nb_inputs, nb_classes)`.
"""
self.my_detector.eval()
self.my_model.eval()
# Apply preprocessing
x_preprocessed = preprocessing_fn_torch(x)
# Run prediction with batch processing
results = np.zeros((x_preprocessed.shape[0], self.nb_classes), dtype=np.float32)
num_batch = int(np.ceil(len(x_preprocessed) / float(batch_size)))
for m in range(num_batch):
# Batch indexes
begin, end = (m * batch_size, min((m + 1) * batch_size, x_preprocessed.shape[0]),)
#my_inputs = torch.from_numpy(x_preprocessed[begin:end]).to(self._device)
my_inputs = x_preprocessed[begin:end].to(self._device)
#my_inputs = x_preprocessed.to(self._device)
#print('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA')
#print(my_inputs.shape)
with torch.no_grad():
model_outputs = detector_and_model(self.my_detector, self.my_model, my_inputs, self.spatial_transform)
output = model_outputs[-1]
results[begin:end] = output.detach().cpu().numpy()
predictions = results
return predictions
def class_gradient(self, x: np.ndarray, label: Union[int, List[int], None] = None, **kwargs) -> np.ndarray:
"""
Compute per-class derivatives w.r.t. `x`.
:param x: Sample input with shape as expected by the model.
:param label: Index of a specific per-class derivative. If an integer is provided, the gradient of that class
output is computed for all samples. If multiple values as provided, the first dimension should
match the batch size of `x`, and each value will be used as target for its corresponding sample in
`x`. If `None`, then gradients for all classes will be computed for each sample.
:return: Array of gradients of input features w.r.t. each class in the form
`(batch_size, nb_classes, input_shape)` when computing for all classes, otherwise shape becomes
`(batch_size, 1, input_shape)` when `label` parameter is specified.
"""
if not (
(label is None)
or (isinstance(label, (int, np.integer)) and label in range(self._nb_classes))
or (
isinstance(label, np.ndarray)
and len(label.shape) == 1
and (label < self._nb_classes).all()
and label.shape[0] == x.shape[0]
)
):
raise ValueError("Label %s is out of range." % label)
# Apply preprocessing
x_preprocessed = preprocessing_fn_torch(x)
x_preprocessed = torch.from_numpy(x_preprocessed).to(self._device)
# Compute gradients
if self._layer_idx_gradients < 0:
x_preprocessed.requires_grad = True
# Run prediction
model_outputs = detector_and_model(self.my_detector, self.my_model, x_preprocessed, self.spatial_transform)
# Set where to get gradient
if self._layer_idx_gradients >= 0:
input_grad = model_outputs[self._layer_idx_gradients]
else:
input_grad = x_preprocessed
# Set where to get gradient from
preds = model_outputs
# Compute the gradient
grads = []
def save_grad():
def hook(grad):
grads.append(grad.cpu().numpy().copy())
grad.data.zero_()
return hook
input_grad.register_hook(save_grad())
self.my_model.zero_grad()
if label is None:
for i in range(self.nb_classes):
torch.autograd.backward(
preds[:, i], torch.tensor([1.0] * len(preds[:, 0])).to(self._device), retain_graph=True,
)
elif isinstance(label, (int, np.integer)):
torch.autograd.backward(
preds[:, label], torch.tensor([1.0] * len(preds[:, 0])).to(self._device), retain_graph=True,
)
else:
unique_label = list(np.unique(label))
for i in unique_label:
torch.autograd.backward(
preds[:, i], torch.tensor([1.0] * len(preds[:, 0])).to(self._device), retain_graph=True,
)
grads = np.swapaxes(np.array(grads), 0, 1)
lst = [unique_label.index(i) for i in label]
grads = grads[np.arange(len(grads)), lst]
grads = grads[None, ...]
grads = np.swapaxes(np.array(grads), 0, 1)
grads = preprocessing_fn_inverse_torch(x, grads)
print('BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB')
print(x.shape)
print(grads.shape)
return grads
def loss_gradient(self, x: np.ndarray, y: np.ndarray, **kwargs) -> np.ndarray:
"""
Compute the gradient of the loss function w.r.t. `x`.
:param x: Sample input with shape as expected by the model.
:param y: Target values (class labels) one-hot-encoded of shape `(nb_samples, nb_classes)` or indices of shape
`(nb_samples,)`.
:return: Array of gradients of the same shape as `x`.
"""
# Apply preprocessing
x_preprocessed = preprocessing_fn_torch(x)
_, y_preprocessed = self._apply_preprocessing(x, y, fit=False)
# Check label shape
if self._reduce_labels:
y_preprocessed = np.argmax(y_preprocessed, axis=1)
# Convert the inputs to Tensors
#inputs_t = torch.from_numpy(x_preprocessed).to(self._device)
inputs_t = x_preprocessed.to(self._device)
inputs_t.requires_grad_()
# Convert the labels to Tensors
labels_t = torch.from_numpy(y_preprocessed).to(self._device)
# Compute the gradient and return
inputs_zero = torch.zeros_like(inputs_t)
model_outputs = detector_and_model(self.my_detector, self.my_model, inputs_zero + inputs_t, self.spatial_transform)
loss = self._loss(model_outputs, labels_t)
# Clean gradients
self.my_model.zero_grad()
# Compute gradients
loss.backward()
grads = inputs_t.grad.cpu().numpy().copy() # type: ignore
grads = preprocessing_fn_inverse_torch(x, grads)
print('BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB')
print(x.shape)
print(grads.shape)
assert grads.shape == x.shape
return grads
def loss_gradient_framework(self, x: "torch.Tensor", y: "torch.Tensor", **kwargs) -> "torch.Tensor":
"""
Compute the gradient of the loss function w.r.t. `x`.
:param x: Input with shape as expected by the model.
:param y: Target values (class labels) one-hot-encoded of shape (nb_samples, nb_classes) or indices of shape
(nb_samples,).
:return: Gradients of the same shape as `x`.
"""
# Check label shape
if self._reduce_labels:
y = torch.argmax(y, dim=1)
# Convert the inputs to Variable
x = preprocessing_fn_torch(x)
x = Variable(x)
x.requires_grad_()
# Compute the gradient and return
inputs_zero = torch.zeros_like(x)
model_outputs = detector_and_model(self.my_detector, self.my_model, inputs_zero + x, self.spatial_transform)
loss = self._loss(model_outputs, y)
# Clean gradients
self.my_model.zero_grad()
# Compute gradients
loss.backward()
grads = x.grad
assert grads.shape == x.shape # type: ignore
print('BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB')
print(x.shape)
print(grads.shape)
return grads # type: ignore
def preprocessing_fn_torch(
batch: Union[torch.Tensor, np.ndarray],
consecutive_frames: int = 40,
align_corners: bool = False,
):
"""
inputs - batch of videos each with shape (frames, height, width, channel)
outputs - batch of videos each with shape (n_stack, channel, stack_frames, new_height, new_width)
frames = n_stack * stack_frames (after padding)
new_height = new_width = 112
consecutive_frames - number of consecutive frames (stack_frames)
After resizing, a center crop is performed to make the image square
This is a differentiable alternative to MARS' PIL-based preprocessing.
There are some
"""
if not isinstance(batch, torch.Tensor):
#logger.warning(f"batch {type(batch)} is not a torch.Tensor. Casting")
batch = torch.from_numpy(batch).cuda()
# raise ValueError(f"batch {type(batch)} is not a torch.Tensor")
if batch.dtype != torch.float32:
raise ValueError(f"batch {batch.dtype} should be torch.float32")
if batch.shape[0] != 1:
raise ValueError(f"Batch size {batch.shape[0]} != 1")
video = batch[0]
if video.ndim != 4:
raise ValueError(
f"video dims {video.ndim} != 4 (frames, height, width, channel)"
)
if video.shape[0] < 1:
raise ValueError("video must have at least one frame")
if tuple(video.shape[1:]) == (240, 320, 3):
standard_shape = True
elif tuple(video.shape[1:]) == (226, 400, 3):
#logger.warning("Expected odd example shape (226, 400, 3)")
standard_shape = False
else:
raise ValueError(f"frame shape {tuple(video.shape[1:])} not recognized")
if video.max() > 1.0 or video.min() < 0.0:
raise ValueError("input should be float32 in [0, 1] range")
if not isinstance(consecutive_frames, int):
raise ValueError(f"consecutive_frames {consecutive_frames} must be an int")
if consecutive_frames < 1:
raise ValueError(f"consecutive_frames {consecutive_frames} must be positive")
# Select a integer multiple of consecutive frames
while len(video) < consecutive_frames:
# cyclic pad if insufficient for a single stack
video = torch.cat([video, video[: consecutive_frames - len(video)]])
if len(video) > consecutive_frames:
# cut trailing frames
#video = video[: len(video) - (len(video) % consecutive_frames)]
video = video[: consecutive_frames]
# Attempts to directly follow MARS approach
# (frames, height, width, channel) to (frames, channel, height, width)
video = video.permute(0, 3, 1, 2)
if standard_shape: # 240 x 320
sample_height, sample_width = 112, 149
else: # 226 x 400
video = video[:, :, 1:-1, :] # crop top/bottom pixels, reduce by 2
sample_height, sample_width = 112, 200
video = F.interpolate(video, size=(sample_height, sample_width), mode="bilinear", align_corners=align_corners)
if standard_shape:
crop_left = 18 # round((149 - 112)/2.0)
else:
crop_left = 40
video = video[:, :, :, crop_left : crop_left + sample_height]
if video.max() > 1.0:
raise ValueError("Video exceeded max after interpolation")
if video.min() < 0.0:
raise ValueError("Video under min after interpolation")
# reshape into stacks of frames
#video = torch.reshape(video, (-1, consecutive_frames) + video.shape[1:])
video = torch.unsqueeze(video, 0)
# transpose to (stacks, channel, stack_frames, height, width)
video = video.permute(0, 2, 1, 3, 4)
# video = torch.transpose(video, axes=(0, 4, 1, 2, 3))
# normalize before changing channel position?
#video = torch.transpose(video, 1, 4)
#video = torch.transpose(video, 4, 1)
return video
def preprocessing_fn_inverse_torch(x, grads):
'''
x.shape = [1, 118, 240, 320, 3]
grads.shape = [1, 3, 40, 112, 112]
'''
consecutive_frames = 40
# (batch, channel, frames, height, width) to (channel, frames, height, width)
grads = torch.squeeze(grads, 0)
# resize
grads = F.interpolate(grads, size=(x.shape[2], x.shape[2]), mode="bilinear", align_corners=align_corners)
# pad
p1d = (int((x.shape[3]-x.shape[2])/2), int((x.shape[3]-x.shape[2])/2))
grads = F.pad(grads, p1d, 'constant', 0)
# temporal
if x.shape[1] <= consecutive_frames:
grads = grads[:, :x.shape[1], :, :]
else:
grads = torch.cat([grads, torch.zeros(x.shape[0], x.shape[1]-consecutive_frames ,x.shape[2], x.shape[3]).to(self._device)], dim=1)
# (channel, frames, height, width) to (frames, height, width, channel)
grads = grads.permute(1, 2, 3, 0)
# (batch, frames, height, width, channel) to (frames, height, width, channel)
grads = torch.unsqueeze(grads, 0)
return grads
def get_my_model(model_kwargs, wrapper_kwargs, weights_file):
if weights_file:
pretrain_path = maybe_download_weights_from_s3('JHUM_oudefend.pth')
my_model = generate_model('resnext_oun')
model_data = torch.load(pretrain_path)
my_model.load_state_dict(model_data['state_dict'])
spatial_transform = Compose([Scale(112), CenterCrop(112), torchvision.transforms.ToTensor()])
wrapped_model = PyTorchClassifier(
my_model,
loss=torch.nn.CrossEntropyLoss(),
optimizer=my_model.optimizer,
input_shape=(None, 240, 320, 3),
nb_classes=101,
clip_values=(0.0, 1.0),
)
return wrapped_model