generated from rochacbruno/python-project-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(train,compile_jsons): seperated training from json generation
- Loading branch information
Showing
2 changed files
with
73 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
""" | ||
Trains LLAVA model on the cumulative dataset. | ||
""" | ||
|
||
import json | ||
import os | ||
import random | ||
from typing import List | ||
|
||
from drivellava.constants import ENCODED_JSON, VAL_ENCODED_JSON | ||
from drivellava.sparse_llava_dataset import get_drivellava_prompt | ||
from drivellava.trajectory_encoder import TrajectoryEncoder | ||
|
||
|
||
def load_json_dataset( | ||
json_list: List[str], | ||
trajectory_encoder: TrajectoryEncoder, | ||
): | ||
|
||
data = [] | ||
for json_path in json_list: | ||
with open(json_path, "r", encoding="utf-8") as f: | ||
loaded = json.load(f) | ||
for index in range(len(loaded)): | ||
assert len(loaded[index]["conversations"][1]["value"]) == 1 | ||
|
||
loaded[index]["conversations"][1]["value"] = ( | ||
"Selected Trajectory: " | ||
+ loaded[index]["conversations"][1]["value"] | ||
) | ||
loaded[index]["conversations"][0]["value"] = ( | ||
get_drivellava_prompt(trajectory_encoder) | ||
) | ||
data.extend(loaded) | ||
|
||
return data | ||
|
||
|
||
def main(): | ||
|
||
trajectory_encoder = TrajectoryEncoder() | ||
|
||
train = load_json_dataset( | ||
ENCODED_JSON, | ||
trajectory_encoder, | ||
) | ||
val = load_json_dataset( | ||
VAL_ENCODED_JSON, | ||
trajectory_encoder, | ||
) | ||
|
||
print(f"Train: {len(train)}") | ||
print(f"Val: {len(val)}") | ||
|
||
# Shuffle train and val | ||
random.shuffle(train) | ||
random.shuffle(val) | ||
|
||
new_train_json_path = os.path.abspath("checkpoints/train.json") | ||
new_val_json_path = os.path.abspath("checkpoints/val.json") | ||
|
||
# Save train to a temp file | ||
with open(new_train_json_path, "w", encoding="utf-8") as f: | ||
json_data = json.dumps(train, ensure_ascii=False, indent=4) | ||
f.write(json_data) | ||
|
||
with open(new_val_json_path, "w", encoding="utf-8") as f: | ||
json_data = json.dumps(val, ensure_ascii=False, indent=4) | ||
f.write(json_data) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters