forked from clemsgrs/hipt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract_features_patch.py
189 lines (165 loc) · 5.69 KB
/
extract_features_patch.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import os
import tqdm
import time
import wandb
import torch
import hydra
import shutil
import datetime
import subprocess
import pandas as pd
import multiprocessing as mp
from pathlib import Path
from omegaconf import DictConfig
from torchvision import transforms
from source.dataset import ImageFolderWithNameDataset
from source.models import PatchEmbedder
from source.utils import (
initialize_wandb,
is_main_process,
)
from pretrain.utils import make_classification_eval_transform
@hydra.main(
version_base="1.2.0", config_path="config/feature_extraction", config_name="patch"
)
def main(cfg: DictConfig):
distributed = torch.cuda.device_count() > 1
if distributed:
torch.distributed.init_process_group(backend="nccl")
gpu_id = int(os.environ["LOCAL_RANK"])
if gpu_id == 0:
print(f"Distributed session successfully initialized")
else:
gpu_id = -1
if is_main_process():
print(f"torch.cuda.device_count(): {torch.cuda.device_count()}")
run_id = datetime.datetime.now().strftime("%Y-%m-%d_%H_%M")
# set up wandb
if cfg.wandb.enable:
key = os.environ.get("WANDB_API_KEY")
wandb_run = initialize_wandb(cfg, key=key)
wandb_run.define_metric("processed", summary="max")
run_id = wandb_run.id
else:
run_id = ""
if distributed:
obj = [run_id]
torch.distributed.broadcast_object_list(
obj, 0, device=torch.device(f"cuda:{gpu_id}")
)
run_id = obj[0]
output_dir = Path(cfg.output_dir, cfg.experiment_name, run_id)
features_dir = Path(output_dir, "features")
if not cfg.resume and is_main_process():
if output_dir.exists():
print(f"{output_dir} already exists! deleting it...")
shutil.rmtree(output_dir)
print("done")
output_dir.mkdir(parents=False)
features_dir.mkdir()
else:
output_dir.mkdir(parents=True, exist_ok=True)
features_dir.mkdir(exist_ok=True)
model = PatchEmbedder(
img_size=cfg.patch_size,
mini_patch_size=cfg.mini_patch_size,
pretrain_vit_patch=cfg.pretrain_vit_patch,
verbose=(gpu_id in [-1, 0]),
img_size_pretrained=cfg.img_size_pretrained,
)
transform = make_classification_eval_transform(img_size=cfg.patch_size)
dataset = ImageFolderWithNameDataset(cfg.data_dir, transform)
if distributed and is_main_process() and cfg.wandb.enable:
command_line = [
"python3",
"log_nproc.py",
"--output_dir",
f"{features_dir}",
"--fmt",
"pt",
"--total",
f"{len(dataset)}",
"--log_to_wandb",
"--id",
f"{run_id}",
]
subprocess.Popen(command_line)
time.sleep(5)
if distributed:
sampler = torch.utils.data.DistributedSampler(dataset, shuffle=True)
else:
sampler = torch.utils.data.RandomSampler(dataset)
num_workers = min(mp.cpu_count(), cfg.num_workers)
if "SLURM_JOB_CPUS_PER_NODE" in os.environ:
num_workers = min(num_workers, int(os.environ['SLURM_JOB_CPUS_PER_NODE']))
loader = torch.utils.data.DataLoader(
dataset,
sampler=sampler,
batch_size=cfg.batch_size,
num_workers=num_workers,
shuffle=False,
drop_last=False,
)
if gpu_id == -1:
device = torch.device("cuda")
else:
device = torch.device(f"cuda:{gpu_id}")
model = model.to(device, non_blocking=True)
if is_main_process():
print()
filenames, feature_paths = [], []
with tqdm.tqdm(
loader,
desc="Feature Extraction",
unit=" img",
ncols=80,
unit_scale=cfg.batch_size,
position=0,
leave=True,
disable=not (gpu_id in [-1, 0]),
) as t1:
with torch.no_grad():
for i, batch in enumerate(t1):
imgs, fnames = batch
imgs = imgs.to(device, non_blocking=True)
features = model(imgs)
for k, f in enumerate(features):
fname = fnames[k]
feature_path = Path(
features_dir, f"{fname}.pt"
)
torch.save(f, feature_path)
filenames.append(fname)
feature_paths.append(feature_path)
if cfg.wandb.enable and not distributed:
wandb.log({"processed": i + imgs.shape[0]})
features_df = pd.DataFrame.from_dict(
{
"filename": filenames,
"feature_path": feature_paths,
}
)
if distributed:
features_csv_path = Path(output_dir, f"features_{gpu_id}.csv")
else:
features_csv_path = Path(output_dir, f"features.csv")
features_df.to_csv(features_csv_path, index=False)
if distributed:
torch.distributed.barrier()
if is_main_process():
dfs = []
for gpu_id in range(torch.cuda.device_count()):
fp = Path(output_dir, f"features_{gpu_id}.csv")
df = pd.read_csv(fp)
dfs.append(df)
os.remove(fp)
features_df = pd.concat(dfs, ignore_index=True)
features_df = features_df.drop_duplicates()
features_df.to_csv(
Path(output_dir, f"features.csv"), index=False
)
if cfg.wandb.enable and is_main_process() and distributed:
wandb.log({"processed": len(features_df)})
if __name__ == "__main__":
# python3 -m torch.distributed.run --standalone --nproc_per_node=gpu extract_features_patch.py --config-name 'patch'
main()