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

iterate through sys.path instead of just checking cwd #1985

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions armory/art_experimental/attacks/carla_obj_det_adversarial_patch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
from typing import Optional

from art.attacks.evasion.adversarial_patch.adversarial_patch_pytorch import (
Expand Down Expand Up @@ -57,11 +58,18 @@ def create_initial_image(self, size, hsv_lower_bound, hsv_upper_bound):
patch_base_image_path = os.path.abspath(
os.path.join(module_folder, self.patch_base_image)
)
# if the image does not exist, check cwd
# if the image does not exist iterate through path
if not os.path.exists(patch_base_image_path):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to combine this conditional (line 62) with the similar one on line 74?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can certainly be cleaned up to avoid fallthroughs like this but the second check on 74 is needed to determine if it was in the path. Another option is to use for-break iterating through the path and checking for url only if the loop finishes.

Regardless, I am cleaning this code path up in #1904:
bff3f02#diff-43528b7021f0d40c4760148beb2bdbb8e81013288e0d00e42ff57940600b7d12R124-R167

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created this PR more as a hotfix so people can use images anywhere off their armory git directory by including it in the local_repo_path since that gets added to your pythonpath. Happy to move the fetch_file_or_url function into this PR to prevent additional changes with #1904

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Fine either way

patch_base_image_path = os.path.abspath(
os.path.join(paths.runtime_paths().cwd, self.patch_base_image)
)
_path = sys.path.copy()
if (_cwd := paths.runtime_paths().cwd) not in _path:
_path.insert(0, _cwd)
for path in _path:
patch_base_image_path = os.path.abspath(
os.path.join(path, self.patch_base_image)
)
if os.path.exists(patch_base_image_path):
break
del _path, _cwd
# image not in cwd or module, check if it is a url to an image
if not os.path.exists(patch_base_image_path):
# Send a HEAD request
Expand Down