From 3258271c9e251b31e39e9869a0155377a4e8dbc9 Mon Sep 17 00:00:00 2001 From: "Minkyu Choi (mc76728)" Date: Wed, 7 Feb 2024 17:23:19 -0600 Subject: [PATCH] clean codes and change variable names --- README.md | 4 +- .ruff.toml => ruff.toml | 0 run_scripts/run_synthetic_tlv_coco.py | 4 +- ...n_synthetic_tlv_coco_until_long_horizon.py | 4 +- run_scripts/run_synthetic_tlv_imagenet.py | 4 +- ...nthetic_tlv_imagenet_until_long_horizon.py | 4 +- tlv_dataset/data/tlv_dataset.py | 31 ++++++++++---- .../generator/synthetic_tlv_generator.py | 42 ++++++++++++------- 8 files changed, 61 insertions(+), 32 deletions(-) rename .ruff.toml => ruff.toml (100%) diff --git a/README.md b/README.md index 01a23e2..0237062 100644 --- a/README.md +++ b/README.md @@ -122,9 +122,9 @@ Please find argument details from run scripts. * `number_video_per_set_of_frame`: Number of videos to generate per set of frames. * `increase_rate`: Rate at which the number of frames increases. * `ltl_logic`: Temporal logic to apply. Options include various logical expressions like "F prop1", "G prop1", etc. -* `save_frames`: Boolean to decide whether to save individual frames (True or False). -In each run script, make sure +* `save_images`: Boolean to decide whether to save individual frame images (True or False). +In each run script, make sure 1. **coco synthetic data generator**
COCO synthetic data generator can generate & compositions since it has multiple labels. ``` diff --git a/.ruff.toml b/ruff.toml similarity index 100% rename from .ruff.toml rename to ruff.toml diff --git a/run_scripts/run_synthetic_tlv_coco.py b/run_scripts/run_synthetic_tlv_coco.py index 559899e..0d010c0 100644 --- a/run_scripts/run_synthetic_tlv_coco.py +++ b/run_scripts/run_synthetic_tlv_coco.py @@ -65,7 +65,7 @@ ], ) parser.add_argument( - "--save_frames", + "--save_images", type=bool, default=False, ) @@ -97,5 +97,5 @@ number_video_per_set_of_frame=args.number_video_per_set_of_frame, increase_rate=args.increase_rate, ltl_logic=tl, - save_frames=args.save_frames, + save_images=args.save_frames, ) diff --git a/run_scripts/run_synthetic_tlv_coco_until_long_horizon.py b/run_scripts/run_synthetic_tlv_coco_until_long_horizon.py index ffdca51..16b1f80 100644 --- a/run_scripts/run_synthetic_tlv_coco_until_long_horizon.py +++ b/run_scripts/run_synthetic_tlv_coco_until_long_horizon.py @@ -60,7 +60,7 @@ ], ) parser.add_argument( - "--save_frames", + "--save_images", type=bool, default=False, ) @@ -81,5 +81,5 @@ number_video_per_set_of_frame=args.number_video_per_set_of_frame, increase_rate=args.increase_rate, present_prop1_till_prop2=args.present_prop1_till_prop2, - save_frames=args.save_frames, + save_images=args.save_frames, ) diff --git a/run_scripts/run_synthetic_tlv_imagenet.py b/run_scripts/run_synthetic_tlv_imagenet.py index 8f8d604..d134ce2 100644 --- a/run_scripts/run_synthetic_tlv_imagenet.py +++ b/run_scripts/run_synthetic_tlv_imagenet.py @@ -63,7 +63,7 @@ ], ) parser.add_argument( - "--save_frames", + "--save_images", type=bool, default=False, ) @@ -94,5 +94,5 @@ number_video_per_set_of_frame=args.number_video_per_set_of_frame, increase_rate=args.increase_rate, ltl_logic=tl, - save_frames=args.save_frames, + save_images=args.save_frames, ) diff --git a/run_scripts/run_synthetic_tlv_imagenet_until_long_horizon.py b/run_scripts/run_synthetic_tlv_imagenet_until_long_horizon.py index aec1ff6..135ba35 100644 --- a/run_scripts/run_synthetic_tlv_imagenet_until_long_horizon.py +++ b/run_scripts/run_synthetic_tlv_imagenet_until_long_horizon.py @@ -60,7 +60,7 @@ ], ) parser.add_argument( - "--save_frames", + "--save_images", type=bool, default=False, ) @@ -82,5 +82,5 @@ number_video_per_set_of_frame=args.number_video_per_set_of_frame, increase_rate=args.increase_rate, present_prop1_till_prop2=args.present_prop1_till_prop2, - save_frames=args.save_frames, + save_images=args.save_frames, ) diff --git a/tlv_dataset/data/tlv_dataset.py b/tlv_dataset/data/tlv_dataset.py index 79234f8..1dfc38b 100644 --- a/tlv_dataset/data/tlv_dataset.py +++ b/tlv_dataset/data/tlv_dataset.py @@ -1,15 +1,10 @@ import dataclasses # noqa: D100 -import random -from pathlib import Path -from typing import Dict, List, Optional +from typing import List, Optional -import cv2 import numpy as np -import torch from PIL import Image from tlv_dataset.common.frame_grouping import combine_consecutive_lists -from tlv_dataset.common.utility import get_file_or_dir_with_datetime @dataclasses.dataclass @@ -48,12 +43,11 @@ def save_frames( Args: path (str, optional): Path to save image. """ - from PIL import Image for idx, img in enumerate(self.images_of_frames): Image.fromarray(img).save(f"{path}/{idx}.png") - def save( + def save_as_class( self, save_path: str = "/opt/Neuro-Symbolic-Video-Frame-Search/artifacts", ) -> None: @@ -63,3 +57,24 @@ def save( """Save the current instance to a pickle file.""" with open(save_path, "wb") as f: pickle.dump(self, f) + + def save_as_dict( + self, + save_path: str = "/opt/Neuro-Symbolic-Video-Frame-Search/artifacts", + ) -> None: + """Save the current instance to a pickle file.""" + import pickle + + dict_format = dict( + ground_truth=self.ground_truth, + ltl_formula=self.ltl_formula, + proposition=self.proposition, + number_of_frame=self.number_of_frame, + frames_of_interest=self.frames_of_interest, + labels_of_frames=self.labels_of_frames, + images_of_frames=self.images_of_frames, + ) + + """Save the current instance to a pickle file.""" + with open(save_path, "wb") as f: + pickle.dump(dict_format, f) diff --git a/tlv_dataset/generator/synthetic_tlv_generator.py b/tlv_dataset/generator/synthetic_tlv_generator.py index 294986f..24c0da2 100644 --- a/tlv_dataset/generator/synthetic_tlv_generator.py +++ b/tlv_dataset/generator/synthetic_tlv_generator.py @@ -23,8 +23,8 @@ def __init__( Args: dataloader (TLVImageLoader): Image data loader. """ - self._data_loader = dataloader - self.data = self._data_loader.data + self.data_loader = dataloader + self.data = self.data_loader.data self._save_dir = Path(save_dir) self._save_dir.mkdir(parents=True, exist_ok=True) @@ -78,7 +78,8 @@ def generate( ltl_logic: str = "F prop1", temporal_property: str = "", conditional_property: str = "", - save_frames: bool = False, + save_images: bool = False, + save_as: str = "dict", # class ) -> any: """Generate data.""" number_frame = initial_number_of_frame @@ -122,13 +123,19 @@ def generate( proposition=ltl_frame.proposition, ) self.extract_properties(ltl_frame.ltl_formula) - ltl_frame.save( - save_path=self._save_dir - / f"benchmark_{self._data_loader.name}_ltl_{ltl_frame.ltl_formula}_{number_frame}_{video_idx}.pkl" - ) + if save_as == "dict": + ltl_frame.save_as_dict( + save_path=self._save_dir + / f"benchmark_{self.data_loader.name}_ltl_{ltl_frame.ltl_formula}_{number_frame}_{video_idx}.pkl" + ) + else: + ltl_frame.save_as_class( + save_path=self._save_dir + / f"benchmark_{self.data_loader.name}_ltl_{ltl_frame.ltl_formula}_{number_frame}_{video_idx}.pkl" + ) number_frame += increase_rate - if save_frames: + if save_images: ltl_frame.save_frames() def generate_until_time_delta( @@ -139,7 +146,8 @@ def generate_until_time_delta( increase_rate: int = 1, ltl_logic: str = "prop1 U prop2", present_prop1_till_prop2: bool = False, - save_frames: bool = False, + save_images: bool = False, + save_as: str = "dict", # class ) -> any: """Generate data.""" number_frame = initial_number_of_frame @@ -184,13 +192,19 @@ def generate_until_time_delta( proposition=ltl_frame.proposition, ) self.extract_properties(ltl_frame.ltl_formula) - ltl_frame.save( - save_path=self._save_dir - / f"timedelta_{number_frame - (initial_number_of_frame - 1)}_benchmark_{self._data_loader.name}_ltl_{ltl_frame.ltl_formula}_{number_frame}_{video_idx}.pkl" - ) + if save_as == "dict": + ltl_frame.save_as_dict( + save_path=self._save_dir + / f"benchmark_{self.data_loader.name}_ltl_{ltl_frame.ltl_formula}_{number_frame}_{video_idx}.pkl" + ) + else: + ltl_frame.save_as_class( + save_path=self._save_dir + / f"benchmark_{self.data_loader.name}_ltl_{ltl_frame.ltl_formula}_{number_frame}_{video_idx}.pkl" + ) number_frame += increase_rate - if save_frames: + if save_images: ltl_frame.save_frames() def generate_unique_random_indices_within_range(self, start, end):