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

Prevent folder name conflicts in label() call #93

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

capjamesg
Copy link
Member

This PR appends a timestamp of when label() was called to the output folder label name if the provided folder name already exists and contains images.

This PR prevents the scenario where label() labels a dataset (which could be hundreds or thousands of images) then returns an error after labeling because the existing folder already contains an image with the same name as one in the newly-labeled dataset.

@capjamesg capjamesg self-assigned this Nov 21, 2023
@capjamesg
Copy link
Member Author

NB: This PR fixes #53.

Copy link
Contributor

@yeldarby yeldarby left a comment

Choose a reason for hiding this comment

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

Note: same comments made on detection_base_model likely also apply to classification_base_model

if output_folder is None:
output_folder = input_folder + "_labeled"

os.makedirs(output_folder, exist_ok=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

Logic should be:

  • If output_folder already exists
  • Check whether it was created with the same config (same base model, same ontology)
  • If the config is the same, continue
  • If the config is different, move the old folder to a backup location ({output_folder}-{old_timestamp})

To do this we need to store a hash of the ontology in the config file.

We should also rename this config.json to be .autodistill.json so it's not conflicting with other stuff & is clear where it came from/what it's used for.

Copy link
Contributor

Choose a reason for hiding this comment

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

To do the hashing we'll add a .hash() method to the Ontology base class that's just an md5 of the JSON of the ontology. Subclasses can override this to create their own definition of what it means for an ontology to be "different" from another.


if not os.path.exists(annotation_path):
detections = self.predict(f_path)
detections_map[f_path_short] = detections
Copy link
Contributor

Choose a reason for hiding this comment

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

Save these to disk as we go vs to memory so that if you cancel or crash in the middle you don't lose all your progress


images_map = {}
detections_map = {}

# if output_folder/autodistill.json exists
if os.path.exists(output_folder + "/data.yaml"):
dataset = sv.DetectionDataset.from_yolo(
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't load all the images into memory here in case the dataset is huge; we just need the list of filenames

files = glob.glob(input_folder + "/*" + extension)
progress_bar = tqdm(files, desc="Labeling images")
# iterate through images in input_folder

Copy link
Contributor

Choose a reason for hiding this comment

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

Save the data.yaml file first instead of at the end so it's there next run

config["roboflow_tags"] = roboflow_tags
config["task"] = "detection"

with open(os.path.join(output_folder, "config.json"), "w+") as f:
Copy link
Contributor

Choose a reason for hiding this comment

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

Change filename to match

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

Successfully merging this pull request may close these issues.

None yet

2 participants