Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RobustDpatch doesn't work #2507

Open
YXU300 opened this issue Oct 13, 2024 · 3 comments
Open

RobustDpatch doesn't work #2507

YXU300 opened this issue Oct 13, 2024 · 3 comments
Assignees
Labels
question Further information is requested

Comments

@YXU300
Copy link

YXU300 commented Oct 13, 2024

`attack = RobustDPatch(
estimator=detector,
patch_shape=(3, 100, 100),
patch_location=(0,0),
crop_range=[0, 0],
brightness_range=[1.0, 1.0],
rotation_weights=[1, 0, 0, 0],
sample_size=1,
learning_rate=1.99,
max_iter=200,
batch_size=1,
verbose=True,
targeted=True
)

loss_history = []

for images, image_names in get_images(data_dir):
dets = detector.predict(images)
y = []
for j in range(len(dets)):

    highest_box = filter_highest_score_box([dets[j]])
    y.append(highest_box)

patch = attack.generate(x=images,y=y)
patched_images = attack.apply_patch(images)

loss_components, _ = detector._get_losses(patched_images, dets)
losses = {}
for loss in loss_components.keys():
    if 'loss' in loss:
        losses[loss] = loss_components[loss].item()
loss_history.append(losses)
print(f"Current Losses: {losses}")

for i in range(len(patched_images)):
    adversarial_image_pgd_bgr = cv2.cvtColor(
        patched_images[i].transpose((1, 2, 0)).astype(np.uint8),
        cv2.COLOR_RGB2BGR
    )
    cv2.imwrite(os.path.join(save_dir, image_names[i]), adversarial_image_pgd_bgr)

print(f"Loss history over all batches: {loss_history}")
`
I tried to attack yolov5s, but it didn't work. The dataset is coco2017.
13321728791086_ pic

@beat-buesser
Copy link
Collaborator

Hi @YXU300 Thank you very much for your interest in ART! How did you define detector in your script above?

@beat-buesser beat-buesser self-assigned this Oct 21, 2024
@beat-buesser beat-buesser added the question Further information is requested label Oct 21, 2024
@YXU300
Copy link
Author

YXU300 commented Oct 21, 2024

class Yolo(torch.nn.Module):
def init(self, model):
super().init()
self.model = model
self.model.hyp = {'box': 0.1, 'obj': 0.1, 'cls': 0.1, 'anchor_t': 1.0, 'cls_pw': 1.0, 'obj_pw': 1.0, 'fl_gamma': 0.0}
self.compute_loss = ComputeLoss(self.model.model.model)

def forward(self, x, targets=None):
    if self.training:
        outputs = self.model.model.model(x)
        loss, loss_items = self.compute_loss(outputs, targets)
        loss_components_dict = {"loss_total": loss, 'loss_box': loss_items[0], 'loss_obj': loss_items[1], 'loss_cls': loss_items[2]}
        return loss_components_dict
    else:
        return self.model(x)

model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
model = Yolo(model)
detector = PyTorchYolo(model=model,
device_type='cuda',
input_shape=(3, 256, 256),
clip_values=(0, 255),
attack_losses=("loss_total", "loss_cls", "loss_box", "loss_obj"))

attack = RobustDPatch(
estimator=detector,
patch_shape=(3, 100, 100),
patch_location=(0,0),
crop_range=[0, 0],
brightness_range=[1.0, 1.0],
rotation_weights=[1, 0, 0, 0],
sample_size=1,
learning_rate=1.99,
max_iter=200,
batch_size=5,
verbose=True,
targeted=False
)

loss_history = []

for images, image_names in get_images(data_dir):
dets = detector.predict(images)
y = []
for j in range(len(dets)):

    highest_box = filter_highest_score_box([dets[j]])
    y.append(highest_box)

patch = attack.generate(x=images)
patched_images = attack.apply_patch(images)

loss_components, _ = detector._get_losses(patched_images, dets)
losses = {}
for loss in loss_components.keys():
    if 'loss' in loss:
        losses[loss] = loss_components[loss].item()
loss_history.append(losses)
print(f"Current Losses: {losses}")

for i in range(len(patched_images)):
    adversarial_image_pgd_bgr = cv2.cvtColor(
        patched_images[i].transpose((1, 2, 0)).astype(np.uint8),
        cv2.COLOR_RGB2BGR
    )
    cv2.imwrite(os.path.join(save_dir, image_names[i]), adversarial_image_pgd_bgr)

print(f"Loss history over all batches: {loss_history}")

Thanks for your reply, here is my code. This code is modified from adversarial patch example.

@YXU300
Copy link
Author

YXU300 commented Oct 21, 2024

I've also tried to run the example code and it also has no work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants