Skip to content

Commit

Permalink
Merge pull request #40 from ttngu207/main_external-storage
Browse files Browse the repository at this point in the history
Use external storage: store result files as filepath@store
  • Loading branch information
kushalbakshi authored Aug 23, 2024
2 parents 5465016 + 709fb47 commit a491f42
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 38 deletions.
77 changes: 39 additions & 38 deletions element_facemap/facemap_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,19 @@ class BodyPartPosition(dj.Part):
likelihood : longblob # model evaluated likelihood
"""

class File(dj.Part):
definition = """
-> master
file_name: varchar(255)
---
file: filepath@facemap-processed
"""

def make(self, key):
""".populate() method will launch training for each FacemapInferenceTask"""
"""
Calls facemap.pose.Pose to run pose estimation on the video files using the specified model.
Video files and model are specified in the FacemapInferenceTask table.
"""
# ID model and directories
task_mode, output_dir = (FacemapInferenceTask & key).fetch1(
"task_mode", "facemap_inference_output_dir"
Expand Down Expand Up @@ -353,27 +364,9 @@ def make(self, key):
full_metadata_path = output_dir / f"{vid_name}_FacemapPose_metadata.pkl"

# Load or Trigger Facemap Pose Estimation Inference
if (
if task_mode == "trigger" and not (
facemap_result_path.exists() & full_metadata_path.exists()
) or task_mode == "load": # Load results and do not rerun processing
(
body_part_position_entry,
inference_duration,
total_frame_count,
creation_time,
) = _load_facemap_results(key, facemap_result_path, full_metadata_path)
self.insert1(
{
**key,
"inference_completion_time": creation_time,
"inference_run_duration": inference_duration,
"total_frame_count": total_frame_count,
}
)
self.BodyPartPosition.insert(body_part_position_entry)
return

elif task_mode == "trigger":
):
from facemap.pose import pose as facemap_pose, model_loader

bbox = (FacemapInferenceTask & key).fetch1("bbox") or []
Expand All @@ -382,9 +375,10 @@ def make(self, key):
facemap_model_name = (
FacemapModel.File & f'model_id="{key["model_id"]}"'
).fetch1("model_file")

facemap_model_path = Path.cwd() / facemap_model_name
# copy this model file to the facemap model root directory (~/.facemap/models/)
models_root_dir = model_loader.get_models_dir()
shutil.copy(facemap_model_path, models_root_dir)

# Create Symbolic Links to raw video data files from outbox directory
video_symlinks = []
Expand All @@ -395,9 +389,6 @@ def make(self, key):
video_symlink.symlink_to(video_file)
video_symlinks.append(video_symlink.as_posix())

# copy this model file to the facemap model root directory (~/.facemap/models/)
shutil.copy(facemap_model_path, models_root_dir)

# Instantiate Pose object, with filenames specified as video files, and bounding specified in params
# Assumes GUI to be none as we are running CLI implementation
pose = facemap_pose.Pose(
Expand All @@ -408,21 +399,32 @@ def make(self, key):
)
pose.run()

(
body_part_position_entry,
inference_duration,
total_frame_count,
creation_time,
) = _load_facemap_results(key, facemap_result_path, full_metadata_path)
self.insert1(
(
body_part_position_entry,
inference_duration,
total_frame_count,
creation_time,
) = _load_facemap_results(key, facemap_result_path, full_metadata_path)
self.insert1(
{
**key,
"inference_completion_time": creation_time,
"inference_run_duration": inference_duration,
"total_frame_count": total_frame_count,
}
)
self.BodyPartPosition.insert(body_part_position_entry)
# Insert result files
self.File.insert(
[
{
**key,
"inference_completion_time": creation_time,
"inference_run_duration": inference_duration,
"total_frame_count": total_frame_count,
"file_name": f.relative_to(output_dir).as_posix(),
"file": f,
}
)
self.BodyPartPosition.insert(body_part_position_entry)
for f in (facemap_result_path, full_metadata_path)
]
)

@classmethod
def get_trajectory(cls, key: dict, body_parts: list = "all") -> pd.DataFrame:
Expand Down Expand Up @@ -468,7 +470,6 @@ def get_trajectory(cls, key: dict, body_parts: list = "all") -> pd.DataFrame:

def _load_facemap_results(key, facemap_result_path, full_metadata_path):
"""Load facemap results from h5 and metadata files."""

from facemap import utils

with open(full_metadata_path, "rb") as f:
Expand Down
19 changes: 19 additions & 0 deletions element_facemap/facial_behavior_estimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,14 @@ class FacemapProcessing(dj.Computed):
package_version='' : varchar(16)
"""

class File(dj.Part):
definition = """
-> master
file_name: varchar(255)
---
file: filepath@facemap-processed
"""

# Process only the VideoRecordings that have their Info inserted.
@property
def key_source(self):
Expand Down Expand Up @@ -331,6 +339,17 @@ def _run_facemap_process():
creation_time = datetime.fromtimestamp(results_proc_fp.stat().st_ctime)

self.insert1({**key, "processing_time": creation_time})
# Insert result files
self.File.insert(
[
{
**key,
"file_name": f.relative_to(output_dir).as_posix(),
"file": f,
}
for f in (results_proc_fp,)
]
)


@schema
Expand Down

0 comments on commit a491f42

Please sign in to comment.