Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
148 commits
Select commit Hold shift + click to select a range
9c872a5
fix
BearingBall Jan 25, 2022
0e2e2bf
test
BearingBall Jan 25, 2022
b0f8358
fix
BearingBall Jan 25, 2022
774977b
fix
BearingBall Jan 25, 2022
4ee92d3
fix
BearingBall Jan 25, 2022
cea194b
fix
BearingBall Jan 25, 2022
98e9c04
fix
BearingBall Jan 25, 2022
ba3f590
fix
BearingBall Jan 25, 2022
f044888
test
BearingBall Jan 28, 2022
c4314f0
test
BearingBall Jan 28, 2022
1205260
test
BearingBall Jan 28, 2022
8503d9d
test
BearingBall Jan 28, 2022
74d62d8
test
BearingBall Jan 28, 2022
2c9f4e6
test
BearingBall Jan 28, 2022
36d2dc9
test
BearingBall Jan 28, 2022
b299779
test
BearingBall Jan 28, 2022
a0a888b
test
BearingBall Jan 28, 2022
8eb2f45
fix
BearingBall Jan 29, 2022
029d62c
new feature
BearingBall Feb 1, 2022
07f86f4
cdev
BearingBall Feb 1, 2022
438dd8e
fix
BearingBall Feb 1, 2022
a03f082
fix
BearingBall Feb 9, 2022
24a2338
fix
BearingBall Feb 9, 2022
d9f7ea9
fix
BearingBall Feb 10, 2022
9bbf610
Update .gitignore
BearingBall Feb 23, 2022
bc642d1
fix
BearingBall Feb 23, 2022
aba5521
fix
BearingBall Feb 24, 2022
1436bae
fix
BearingBall Feb 24, 2022
ff64337
fix
BearingBall Feb 24, 2022
e78a6ae
fix
BearingBall Feb 26, 2022
f5f80d2
fix
BearingBall Feb 26, 2022
90521d6
changes
BearingBall Mar 12, 2022
74dfdde
fix
BearingBall Mar 12, 2022
afc378d
fix
BearingBall Mar 12, 2022
9a1d136
fix
BearingBall Mar 13, 2022
6112e02
fix
BearingBall Mar 17, 2022
6b2bb54
fix
BearingBall Mar 18, 2022
9e35ce4
fix
BearingBall Mar 18, 2022
8a3bc03
fix
BearingBall Mar 18, 2022
12ac849
Merge branch 'optimizer' into multymodel
BearingBall Mar 25, 2022
eaf2d5d
fix
BearingBall Mar 25, 2022
0679256
fix
BearingBall Mar 25, 2022
6ae9288
fix
BearingBall Mar 25, 2022
d748c9b
fix
BearingBall Mar 25, 2022
47c2fbd
fix
BearingBall Mar 25, 2022
27644cf
fix
BearingBall Mar 25, 2022
e407493
fix
BearingBall Mar 25, 2022
7bca5f8
fix
BearingBall Mar 25, 2022
9049e4a
fix
BearingBall Mar 25, 2022
949c8bd
fix
BearingBall Mar 25, 2022
8a9eddf
fix
BearingBall Mar 25, 2022
ba2a91b
fix
BearingBall Mar 25, 2022
cb9ee44
fix
BearingBall Mar 25, 2022
41f5606
fix
BearingBall Mar 25, 2022
ba173d4
fix
BearingBall Apr 2, 2022
086fdf4
fix
BearingBall Apr 2, 2022
c514980
fix
BearingBall Apr 2, 2022
70e65e4
fix
BearingBall Apr 2, 2022
f585bd9
fix
BearingBall Apr 2, 2022
b5d1474
fix
BearingBall Apr 2, 2022
0faaf4d
fix
BearingBall Apr 2, 2022
f055905
fix
BearingBall Apr 2, 2022
c665b7f
fix
BearingBall Apr 2, 2022
5a4aca5
fix
BearingBall Apr 3, 2022
cceb04b
fix
BearingBall Apr 3, 2022
8847475
fix
BearingBall Apr 3, 2022
4ea0cb6
fix
BearingBall Apr 3, 2022
7347fae
fix
BearingBall Apr 3, 2022
fe7e0ba
fix
BearingBall Apr 12, 2022
6b73e49
fix
BearingBall Apr 12, 2022
54719fe
fix
BearingBall Apr 12, 2022
45686de
fix
BearingBall Apr 12, 2022
1ed8a7f
fix
BearingBall Apr 12, 2022
2cc1577
fix
BearingBall Apr 12, 2022
aad1c1c
fix
BearingBall Apr 12, 2022
751b9e8
fix
BearingBall Apr 12, 2022
0893f8d
fix
BearingBall Apr 12, 2022
3b00961
fix
BearingBall Apr 12, 2022
b1bc415
fix
BearingBall Apr 12, 2022
832154a
fix
BearingBall Apr 12, 2022
67b5ecf
fix
BearingBall Apr 21, 2022
eea8874
fix
BearingBall May 10, 2022
6c6c697
fix
BearingBall May 11, 2022
ccd7e89
fix
BearingBall May 12, 2022
736cd4c
fix
BearingBall May 12, 2022
13c16a0
fix
BearingBall May 12, 2022
47b7d55
fix
BearingBall May 12, 2022
4f5f953
fix
BearingBall May 12, 2022
79b18c4
fix
BearingBall May 12, 2022
09419b8
fix
BearingBall May 12, 2022
0a60ab5
fix
BearingBall May 12, 2022
360ee48
fix
BearingBall May 12, 2022
cfaf1bb
fix
BearingBall May 12, 2022
ed561d1
fix
BearingBall May 12, 2022
95216b0
fix
BearingBall May 12, 2022
c2f357b
fix
BearingBall May 12, 2022
7987b74
fix
BearingBall May 12, 2022
c957862
fix
BearingBall May 12, 2022
e5c3cea
fix
BearingBall May 12, 2022
e47a766
fix
BearingBall May 12, 2022
2ee2c7f
fix
BearingBall May 12, 2022
6ebd6f1
fix
BearingBall May 12, 2022
432c316
fix
BearingBall May 12, 2022
8481435
fix
BearingBall May 12, 2022
ac1da01
fix
BearingBall May 12, 2022
3ee0a86
fix
BearingBall May 12, 2022
f63487c
fix
BearingBall May 12, 2022
10f667d
fix
BearingBall May 12, 2022
a86c4eb
fix
BearingBall May 12, 2022
b8211da
fix
BearingBall May 12, 2022
4e203ec
fix
BearingBall May 12, 2022
4a0bfb5
fix
BearingBall May 12, 2022
95af80c
fix
BearingBall May 12, 2022
587d8c9
fix
BearingBall May 12, 2022
908729e
fix
BearingBall May 12, 2022
92459e3
fix
BearingBall May 12, 2022
bb78432
fix
BearingBall May 12, 2022
bf7464b
fix
BearingBall May 12, 2022
bb3f54f
fix
BearingBall May 12, 2022
00634bb
fix
BearingBall May 12, 2022
9e537e1
fix
BearingBall May 13, 2022
24a96a1
fix
BearingBall May 15, 2022
71065c2
fix
BearingBall May 15, 2022
e5044da
fix
BearingBall May 15, 2022
fc258b9
fix
BearingBall May 15, 2022
73b6dd0
fix
BearingBall May 15, 2022
5a4f8b8
fix
BearingBall May 15, 2022
4f6b7c6
fix
BearingBall May 15, 2022
d5eff0c
fix
BearingBall May 15, 2022
c5e095c
fix
BearingBall May 15, 2022
0c9536c
fix
BearingBall May 15, 2022
1b57480
fix
BearingBall May 16, 2022
283ab95
fix
BearingBall May 16, 2022
d957d9f
fix
BearingBall May 16, 2022
cf28cd0
fix
BearingBall May 16, 2022
081535c
fix
BearingBall May 16, 2022
f79cb16
fix
BearingBall May 16, 2022
b2655ae
fix
BearingBall May 16, 2022
52149f5
fix
BearingBall May 17, 2022
4957472
fix
BearingBall May 17, 2022
7d45737
fix
BearingBall May 17, 2022
23d7c53
fix
BearingBall May 20, 2022
e4b95ed
fix
BearingBall May 20, 2022
2506080
git
BearingBall May 20, 2022
408cc1f
fix
BearingBall May 20, 2022
18093fb
fix
BearingBall May 20, 2022
e7be5ea
fix
BearingBall May 20, 2022
c7a1e6f
fix
BearingBall May 20, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ build/
develop-eggs/
dist/
downloads/
runs/
eggs/
.eggs/
lib64/
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "RAdam"]
path = RAdam
url = https://github.com/LiyuanLucasLiu/RAdam.git
1 change: 1 addition & 0 deletions RAdam
Submodule RAdam added at d9fd30
2 changes: 1 addition & 1 deletion argument_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def parse_command_line_args_train():
help='percentage of validation data',
required=False,
type=float,
default=0.05,
default=1,
)
parser.add_argument(
'--batch_size',
Expand Down
Binary file modified attack_construction/__pycache__/attack_methods.cpython-38.pyc
Binary file not shown.
Binary file modified attack_construction/__pycache__/metrics.cpython-38.pyc
Binary file not shown.
Binary file modified attack_construction/__pycache__/utils.cpython-38.pyc
Binary file not shown.
211 changes: 211 additions & 0 deletions attack_construction/attack_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
#from turtle import forward
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
import attack_construction.attack_methods as attack
from torch.utils.tensorboard import SummaryWriter
from attack_construction.utils import save_patch_tensor
from progress.bar import IncrementalBar
import json
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval


def train(my_complex_model, train_dataloader, augmentations, optimizer, writer, loss, epoch):
bar = IncrementalBar(f'Epoch {epoch} progress', max = len(train_dataloader))

for step_num, (images, labels, _, _) in enumerate(train_dataloader):
for model_index in range(len(my_complex_model.module.models)):
prediction = my_complex_model(images, labels, model_index, augmentations)
costs = loss(prediction, my_complex_model.module.patch)
cost = sum(costs)
cost.backward()

optimizer.step()
optimizer.zero_grad()

with torch.no_grad():
my_complex_model.module.patch.data.clamp_(0,1)

bar.next()

bar.finish()


#в разработке
def validate(my_complex_model, val_dataloader, augmentations, annotation_file, local_rank):
if (local_rank != 0):
return

mAPs = []

for model_index in range(len(my_complex_model.module.models)):
annotation_after = []

bar = IncrementalBar(f'Validate {model_index} model in progress progress', max = len(val_dataloader))

for val_idx, (images, labels, img_ids, scale_factor) in enumerate(val_dataloader):
with torch.no_grad():
prediction = my_complex_model(images, labels, model_index, augmentations)

for i in range(len(prediction)):
for j in range(len(prediction[i]["labels"])):
annotation_after.append({
'image_id': img_ids[i].item(),
'category_id': prediction[i]["labels"][j].item(),
'bbox': [
prediction[i]["boxes"][j][0].item() / scale_factor[i][0].item(),
prediction[i]["boxes"][j][1].item() / scale_factor[i][1].item(),
(prediction[i]["boxes"][j][2].item() - prediction[i]["boxes"][j][0].item()) / scale_factor[i][0].item(),
(prediction[i]["boxes"][j][3].item() - prediction[i]["boxes"][j][1].item()) / scale_factor[i][1].item()
],
"score": prediction[i]['scores'][j].item()
})

bar.next()

bar.finish()

with open("tmp.json", 'w') as f_after:
json.dump(annotation_after, f_after)

cocoGt = COCO(annotation_file)
cocoDt = cocoGt.loadRes("./tmp.json")

cocoEval = COCOeval(cocoGt, cocoDt, 'bbox')
cocoEval.params.imgIds = cocoGt.getImgIds()
cocoEval.evaluate()
cocoEval.accumulate()
cocoEval.summarize()

print("mAP =", np.mean(cocoEval.stats))
mAPs.append(np.mean(cocoEval.stats))


return mAPs




class Attack_class(nn.Module):
def __init__(self, models, patch, local_rank):
super().__init__()
self.models = models
self.patch = nn.Parameter(data=patch)
self.local_rank = local_rank


def forward(self, images, labels, model_index, augmentations):
if (model_index >= len(self.models)):
return None

augmented_patch = self.patch if augmentations is None else augmentations(self.patch)

attacked_images = []

for i, image in enumerate(images):
attacked_image = image.to(f'cuda:{self.local_rank}')

if labels[i][0][2] * labels[i][0][3] != 0:
for label in labels[i]:
attacked_image = attack.insert_patch(attacked_image, augmented_patch, label, 0.4, self.local_rank, True)

attacked_images.append(attacked_image)

if (len(attacked_images) == 0):
return None

return self.models[model_index](attacked_images)









#ниже устаревшее
'''
def train(self, epochs, train_loader, batch_size, augmentations, loss_function, optimizer, experiment_dir, step_save_frequency, val_loader, small_val_loader, val_labels):
writer = SummaryWriter(log_dir=experiment_dir.as_posix())
for epoch in range(epochs):
image_counter = 0
prev_steps = epoch * len(train_loader)

for step_num, (images, labels, _, _) in enumerate(train_loader):
image_counter += batch_size

losses = []
for model in self.models:
loss, patch = attack.training_step(
model=model,
patch=self.patch,
augmentations=augmentations,
images=images,
labels=labels,
loss=loss_function,
device=self.device,
optimizer = optimizer,
)
losses.append(loss)
self.patch = patch
loss = np.mean(losses)

# TODO: apply tqdm library for progress logging
print(f"ep:{epoch}, epoch_progress:{step_num/len(train_loader)}, batch_loss:{loss}")
writer.add_scalar('Loss/train', loss, step_num + prev_steps)

if step_num % step_save_frequency == 0:
self.log_results(writer, experiment_dir, epoch, step_num, prev_steps, small_val_loader, val_labels, augmentations)

# at least one time in epoch you need full validation
self.log_results(writer, experiment_dir, epoch, step_num, prev_steps, val_loader, val_labels, augmentations)

writer.close()


def validate(self, validate_dir, val_loader, val_labels, augmentations):
objs = 0
tvs = 0
maps = 0
for model in self.models:
obj, tv, mAP = attack.validate(
model,
self.patch,
augmentations,
val_loader,
self.device,
val_labels,
validate_dir)
objs += obj
tvs += tv
maps += mAP

obj = objs / len(self.models)
tv = tvs / len(self.models)
mAP = maps / len(self.models)

print(f'Patch validated. VAL: objectness:{obj}, tv:{tv}, mAP:{mAP}')
return obj, tv, mAP


def log_results(self, writer, experiment_dir, epoch, step_num, prev_steps, small_val_loader, val_labels, augmentations):
save_patch_tensor(self.patch, experiment_dir, epoch=epoch, step=step_num, save_mode='both')
validate_dir = experiment_dir / ('validate_epoch_' + str(epoch) + '_step_' + str(step_num))
validate_dir.mkdir(parents=True, exist_ok=True)
obj, tv, mAP = self.validate(
validate_dir,
small_val_loader,
val_labels,
augmentations)
print(f'patch saved. VAL: objectness:{obj}, attacked:{tv}, mAP:{mAP}')
writer.add_scalar('Loss/val_obj', obj, step_num + prev_steps)
writer.add_scalar('Loss/val_tv', tv, step_num + prev_steps)
writer.add_scalar('mAP/val', mAP, step_num + prev_steps)
writer.flush()


'''

79 changes: 58 additions & 21 deletions attack_construction/attack_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,27 @@
import attack_construction.metrics as metrics


def adversarial_loss_function_batch(predicts, patch, device, tv_scale):
return [adversarial_loss_function(predict, patch, device, tv_scale) for predict in predicts]
def adversarial_loss_function_batch(predicts, patch, tv_scale, local_rank):
return [adversarial_loss_function(predict, patch, tv_scale, local_rank) for predict in predicts]


def adversarial_loss_function(predict, patch, device, tv_scale):
return metrics.general_objectness(predict, device) + tv_scale * metrics.total_variation(patch)
def adversarial_loss_function(predict, patch,tv_scale, local_rank):
return metrics.general_objectness(predict, local_rank) + tv_scale * metrics.total_variation(patch)


def generate_random_patch(resolution=(200, 200)):
def generate_random_patch(resolution=(90, 90)):
return torch.rand(3, resolution[0], resolution[1])


def insert_patch(image, patch, box, ratio, device, random_place=False):
def insert_patch(image, patch, box, ratio, local_rank ,random_place=False):
patch_size = (int(box[3] * ratio), int(box[2] * ratio))

# cant insert patch with this box parameters
if patch_size[0] == 0 or patch_size[1] == 0:
return image

resized_patch = T.Resize(size=patch_size, interpolation=T.InterpolationMode.BICUBIC)(patch)
resized_patch = torch.clamp(resized_patch, 0, 1)

patch_x_offset = box[2] * (random.uniform(0, 1 - ratio) if random_place else 0.5 - ratio / 2)
patch_y_offset = box[3] * (random.uniform(0.3, 1 - ratio) if random_place else 0.5 - ratio / 2)
Expand All @@ -43,16 +44,13 @@ def insert_patch(image, patch, box, ratio, device, random_place=False):

padding = (x_shift, y_shift, image.shape[2] - x_shift - patch_size[1], image.shape[1] - y_shift - patch_size[0])
padded_patch = T.Pad(padding=padding)(resized_patch)
patch_mask = T.Pad(padding=padding)(torch.ones(size=(3, patch_size[0], patch_size[1]))).to(device)
patch_mask = T.Pad(padding=padding)(torch.ones(size=(3, patch_size[0], patch_size[1]))).to(f'cuda:{local_rank}')
result = padded_patch + (torch.ones_like(image) - patch_mask) * image
return result


def training_step(model, patch, augmentations, images, labels, loss, device, grad_rate):
torch.cuda.empty_cache()

patch.requires_grad = True

def training_step(model, patch, augmentations, images, labels, loss, device, optimizer):

attacked_images = [] #torch.tensor(image.to(device), requires_grad = True) for image in images

augmented_patch = patch if augmentations is None else augmentations(patch)
Expand All @@ -67,25 +65,64 @@ def training_step(model, patch, augmentations, images, labels, loss, device, gra
attacked_images.append(attacked_image)

costMean = 0

if len(attacked_images) != 0:

predict = model(attacked_images)

costs = loss(predict, patch, device)
grad = torch.autograd.grad(outputs=sum(costs), inputs=patch, retain_graph=True, create_graph=True, allow_unused=True)[0]

if grad is not None:
patch = torch.clamp(patch - grad_rate * grad.sign(), 0, 1)

cost = sum(costs)

cost.backward()

costMean = np.mean(np.asarray([cost.detach().cpu().numpy() for cost in costs]))
optimizer.step()
optimizer.zero_grad()

with torch.no_grad():
patch.data.clamp_(0,1)

costMean = np.mean(np.asarray([cost.detach().cpu().numpy() for cost in costs]))

patch = patch.detach()
return costMean, patch


def training_step_multymodels(models, patch, augmentations, images, labels, loss, device, optimizer):
attacked_images = [] #torch.tensor(image.to(device), requires_grad = True) for image in images

augmented_patch = patch if augmentations is None else augmentations(patch)

for i, image in enumerate(images):
if labels[i][0][2] * labels[i][0][3] != 0:
attacked_image = image.to(device)

for label in labels[i]:
attacked_image = insert_patch(attacked_image, augmented_patch, label, 0.4, device, model.model.local_rank, True)

attacked_images.append(attacked_image)

costMean = 0

if len(attacked_images) != 0:
costMean = []

for model in models:
predict = model(attacked_images)
costs = loss(predict, patch, device)
cost = sum(costs)
cost.backward()
optimizer.step()

with torch.no_grad():
patch.data.clamp_(0,1)

costMean.append(np.mean(np.asarray([cost.detach().cpu().numpy() for cost in costs])))


optimizer.zero_grad()
return np.mean(costMean), patch


def validate(
model,
patch,
Expand All @@ -110,7 +147,7 @@ def validate(
if augmented_patch is not None:
for i, _ in enumerate(images):
for label in labels[i]:
attacked_images[i] = insert_patch(attacked_images[i], augmented_patch, label, 0.4, device, True)
attacked_images[i] = insert_patch(attacked_images[i], augmented_patch, label, 0.4, device, model.model.local_rank , True)

with torch.no_grad():
predict = model(attacked_images)
Expand Down Expand Up @@ -156,4 +193,4 @@ def validate(
if patch is not None:
total_variation = attack_metric.total_variation(patch).detach().cpu()

return np.mean(np.asarray(objectness)), total_variation, np.mean(cocoEval.stats)
return np.mean(np.asarray(objectness)), total_variation, np.mean(cocoEval.stats)
Loading