Skip to content

Commit

Permalink
clean codes and change variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
minkyu-choi07 committed Feb 7, 2024
1 parent dbfd604 commit 3258271
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 32 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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** <br>
COCO synthetic data generator can generate & compositions since it has multiple labels.
```
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions run_scripts/run_synthetic_tlv_coco.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
],
)
parser.add_argument(
"--save_frames",
"--save_images",
type=bool,
default=False,
)
Expand Down Expand Up @@ -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,
)
4 changes: 2 additions & 2 deletions run_scripts/run_synthetic_tlv_coco_until_long_horizon.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
],
)
parser.add_argument(
"--save_frames",
"--save_images",
type=bool,
default=False,
)
Expand All @@ -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,
)
4 changes: 2 additions & 2 deletions run_scripts/run_synthetic_tlv_imagenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
],
)
parser.add_argument(
"--save_frames",
"--save_images",
type=bool,
default=False,
)
Expand Down Expand Up @@ -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,
)
4 changes: 2 additions & 2 deletions run_scripts/run_synthetic_tlv_imagenet_until_long_horizon.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
],
)
parser.add_argument(
"--save_frames",
"--save_images",
type=bool,
default=False,
)
Expand All @@ -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,
)
31 changes: 23 additions & 8 deletions tlv_dataset/data/tlv_dataset.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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)
42 changes: 28 additions & 14 deletions tlv_dataset/generator/synthetic_tlv_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 3258271

Please sign in to comment.