-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_segmentor.py
151 lines (138 loc) · 6.91 KB
/
train_segmentor.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
import torch
import numpy as np
import os
import timm
from models.segmentation.segmentor import SegmentorTrainer
from models.segmentation.dataloader import SegmentDataset,SegmentDatasetTest, Padding2Resize
from models.segmentation.patch_histogram import test_patch_histogram
from torch.utils.data import DataLoader
import yaml
import sys
import random
sys.path.append(".")
def seed_everything(seed=42):
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
def read_config(config_path):
with open(config_path, "r") as f:
config = yaml.load(f, Loader=yaml.SafeLoader)
return config
def inference(category):
# get scores without training
project_root = os.path.dirname(os.path.abspath(__file__))
image_size = 256
config = read_config(f"{project_root}/configs/segmentor/{category}.yaml")
config['image_size'] = image_size
config['train_image_path'] = config['train_image_path'].replace("category",category).replace("PROJECT_ROOT",project_root)
config['test_image_path'] = config['test_image_path'].replace("category",category).replace("PROJECT_ROOT",project_root)
config['val_image_path'] = config['val_image_path'].replace("category",category).replace("PROJECT_ROOT",project_root)
config['mask_root'] = config['mask_root'].replace("category",category).replace("PROJECT_ROOT",project_root)
config['model_path'] = config['model_path'].replace("category",category).replace("PROJECT_ROOT",project_root)
config['model_path'] = config['model_path'].replace(".pth",f"_{config['image_size']}.pth")
config['category'] = category
test_dataset = SegmentDatasetTest(
image_path=config['test_image_path'],
image_size=config['image_size']
)
test_dataloader = DataLoader(test_dataset,batch_size=1,shuffle=False)
full_train_dataset = SegmentDatasetTest(
image_path=config['train_image_path'],
image_size=config['image_size']
)
full_train_dataloader = DataLoader(full_train_dataset,batch_size=1,shuffle=False)
val_dataset = SegmentDatasetTest(
image_path=config['val_image_path'],
image_size=config['image_size']
)
val_dataloader = DataLoader(val_dataset,batch_size=1,shuffle=False)
encoder = timm.create_model('hf_hub:timm/wide_resnet50_2.tv2_in1k'
,pretrained=True,
features_only=True,
out_indices=[1,2,3]).cuda().eval()
for name,param in encoder.named_parameters():
param.requires_grad = False
segmentor = torch.load(f"./ckpt/segmentor_{category}_256.pth").cuda().eval()
test_patch_histogram(
train_loader=full_train_dataloader,
val_loader=val_dataloader,
test_loader=test_dataloader,
encoder=encoder,
segmentor=segmentor,
category=category,
patch_size=[256,128],
overlap_ratio=[0,0],
save_score=True
)
if __name__ == "__main__":
# categories = ["breakfast_box","juice_bottle","pushpins","screw_bag","splicing_connectors",]#
# for category in categories:
# inference(category)
# exit()
project_root = os.path.dirname(os.path.abspath(__file__))
for image_size in [256]:
categories = ["pushpins","breakfast_box","juice_bottle","screw_bag","splicing_connectors",]#
for category in categories:
seed_everything(42)
config = read_config(f"{project_root}/configs/segmentor/{category}.yaml")
config['image_size'] = image_size
config['train_image_path'] = config['train_image_path'].replace("category",category).replace("PROJECT_ROOT",project_root)
config['test_image_path'] = config['test_image_path'].replace("category",category).replace("PROJECT_ROOT",project_root)
config['val_image_path'] = config['val_image_path'].replace("category",category).replace("PROJECT_ROOT",project_root)
config['mask_root'] = config['mask_root'].replace("category",category).replace("PROJECT_ROOT",project_root)
config['model_path'] = config['model_path'].replace("category",category).replace("PROJECT_ROOT",project_root)
config['model_path'] = config['model_path'].replace(".pth",f"_{config['image_size']}.pth")
config['category'] = category
sup_dataset = SegmentDataset(
image_path=config['train_image_path'],
sup=True,
mask_root=config['mask_root'],
image_size=config['image_size'],
config=config
)
config['pad2resize'] = sup_dataset.pad2resize
sup_dataloader = DataLoader(sup_dataset,
batch_size=4,
shuffle=True,
drop_last=True,
pin_memory=False,
num_workers=0)
unsup_dataset = SegmentDataset(
image_path=config['train_image_path'],
sup=False,
mask_root=config['mask_root'],
image_size=config['image_size'],
config=config
)
unsup_dataloader = DataLoader(unsup_dataset,
batch_size=4,
shuffle=True,
drop_last=True,
pin_memory=False,
num_workers=0)
test_dataset = SegmentDatasetTest(
image_path=config['test_image_path'],
image_size=config['image_size']
)
test_dataloader = DataLoader(test_dataset,batch_size=1,shuffle=False)
full_train_dataset = SegmentDatasetTest(
image_path=config['train_image_path'],
image_size=config['image_size']
)
full_train_dataloader = DataLoader(full_train_dataset,batch_size=1,shuffle=False)
val_dataset = SegmentDatasetTest(
image_path=config['val_image_path'],
image_size=config['image_size']
)
val_dataloader = DataLoader(val_dataset,batch_size=1,shuffle=False)
encoder = timm.create_model('hf_hub:timm/wide_resnet50_2.tv2_in1k'
,pretrained=True,
features_only=True,
out_indices=[1,2,3])
for name,param in encoder.named_parameters():
param.requires_grad = False
segmentor = SegmentorTrainer(encoder,config)
segmentor.fit(sup_dataloader,unsup_dataloader,val_dataloader,test_dataloader,full_train_dataloader)
print("Done!")