-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_pre_processing.py
165 lines (145 loc) · 4.89 KB
/
main_pre_processing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import argparse
import os
import numpy as np
import pandas as pd
import torch
from torch.utils.data import DataLoader
from tqdm import tqdm
from datasets import ImageSequenceDataset
from lightning_modules import DeepImageHomographyEstimationModuleBackbone
from utils.io import load_yaml, natural_keys, scan2df
from utils.processing import LoFTRHomographyEstimation, frame_pairs
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument(
"--server", type=str, default="local", help="Server to be used."
)
parser.add_argument(
"--servers_file",
type=str,
default="config/servers.yml",
help="Server configuration file.",
)
parser.add_argument(
"--backbone_path",
type=str,
default="ae_cai/resnet/48/25/34/version_0",
help="Path to log folders, relative to server logging location.",
)
parser.add_argument(
"--data_prefix",
type=str,
default="cholec80_single_video_frames_cropped",
help="Relative path to data from database location.",
)
parser.add_argument(
"--in_pkl",
type=str,
default="log.pkl",
help="Pickle file with database information.",
)
parser.add_argument(
"--out_pkl",
type=str,
default="pre_processed_log.pkl",
help="Pickle file with preprocessed information.",
)
parser.add_argument(
"--num_workers", type=int, default=8, help="Number of workers for data loading."
)
parser.add_argument(
"--batch_size", type=int, default=16, help="Batch size for data loading."
)
parser.add_argument(
"--frame_increment", type=int, default=1, help="Frame increment in a clip."
)
parser.add_argument(
"--frames_between_clips",
type=int,
default=1,
help="Number of frames between clips.",
)
parser.add_argument(
"--loftr", action="store_true", help="If specified, runs a loftr predictor."
)
args = parser.parse_args()
servers = load_yaml(args.servers_file)
server = servers[args.server]
# Load model
device = "cpu"
if torch.cuda.is_available():
device = "cuda"
if args.loftr:
module = LoFTRHomographyEstimation()
module = module.eval().to(device)
else:
backbone_path = os.path.join(server["logging"]["location"], args.backbone_path)
backbone_configs = load_yaml(
os.path.join(
server["logging"]["location"], args.backbone_path, "config.yml"
)
)
backbone_configs["model"][
"pretrained"
] = False # set to false, as loaded anyways
df = scan2df(
os.path.join(
server["logging"]["location"], args.backbone_path, "checkpoints"
),
".ckpt",
)
ckpts = sorted(list(df["file"]), key=natural_keys)
module = DeepImageHomographyEstimationModuleBackbone.load_from_checkpoint(
checkpoint_path=os.path.join(
backbone_path, "checkpoints/{}".format(ckpts[-1])
),
**backbone_configs["model"]
)
module = module.eval().to(device)
module.freeze()
# Prepare data
data_prefix = os.path.join(server["database"]["location"], args.data_prefix)
df = pd.read_pickle(os.path.join(data_prefix, args.in_pkl))
ds = ImageSequenceDataset(
df,
data_prefix,
seq_len=2,
frame_increment=args.frame_increment,
frames_between_clips=args.frames_between_clips,
)
ds._df = ds._df.astype(object)
dl = DataLoader(
ds,
num_workers=args.num_workers,
batch_size=args.batch_size,
drop_last=False,
shuffle=False,
)
# Prepase logging data frame
duv_df = pd.DataFrame({"duv": np.full(len(df), np.nan)})
duv_df = duv_df.astype(object)
for vid, tf_vid, idcs, vid_idcs in tqdm(dl):
vid = (
vid.to(device=device, dtype=torch.float) / 255.0
) # without shuffling, average circle detection over whole video
frames_i, frames_ips = frame_pairs(vid, 1) # re-sort images
frames_i = frames_i.reshape(
(-1,) + frames_i.shape[-3:]
) # reshape BxNxCxHxW -> B*NxCxHxW
frames_ips = frames_ips.reshape(
(-1,) + frames_ips.shape[-3:]
) # reshape BxNxCxHxW -> B*NxCxHxW
duvs = module(frames_i, frames_ips)
idcs = idcs[:, 0].view(-1).numpy() # take starting index
duvs = duvs.cpu().numpy()
for cnt, idx in enumerate(idcs):
duv_df.loc[idx].duv = duvs[cnt].tolist()
df["duv"] = duv_df.duv
# Compute mean motion
df["duv_mpd"] = df.duv.apply(
lambda x: x if np.isnan(x).any() else np.linalg.norm(x, axis=1).mean()
)
# Safe data
df.to_pickle(os.path.join(data_prefix, args.out_pkl))
if __name__ == "__main__":
main()