RobustDpatch doesn't work
Closed this issue · 4 comments
`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.
Hi @YXU300 Thank you very much for your interest in ART! How did you define detector
in your script above?
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.
I've also tried to run the example code and it also has no work.
Closing because of inactivity, please reopen if still current.