forked from rmaphoh/RETFound_MAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
368 lines (297 loc) · 10.4 KB
/
train.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import torch
import models_vit
from util.pos_embed import interpolate_pos_embed
from timm.models.layers import trunc_normal_
import os
from util.data_handler import split_dataset, check_images
import subprocess
import datetime
import json
import toml
torch.cuda.empty_cache()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f'Using device: {device}')
try:
# Load configurations from toml file
with open("train_state.toml", "r") as toml_file:
config = toml.load(toml_file)
except:
print('Error loading toml file')
if not os.path.exists('train_state.toml'):
print('File does not exist')
os.makedirs('train_state.toml')
else:
print('File exists but could not be loaded')
# Now access your variables like this
cfpweightpath = config["training"]["cfpweightpath"]
octweightpath = config["training"]["octweightpath"]
parent_folder = config["training"]["parent_folder"]
print(f'Parent folder: {parent_folder}')
output_folder = config["training"]["output_folder"]
batch_size = config["training"]["batch_size"]
world_size = config["training"]["world_size"]
epochs = config["training"]["epochs"]
base_model = config["training"]["base_model"]
ft_weightpath = config["training"]["ft_weightpath"]
blr = config["training"]["blr"]
layer_decay = config["training"]["layer_decay"]
weight_decay = config["training"]["weight_decay"]
drop_path = config["training"]["drop_path"]
num_classes = config["training"]["num_classes"]
task = config["training"]["task"]
rmbg = config["training"]["rmbg"]
input_size = config["training"]["input_size"]
use_cases = config["training"]["use_cases"]
limitations = config["training"]["limitations"]
ethics = config["training"]["ethics"]
authors = config["training"]["authors"]
references = config["training"]["references"]
intended_use = config["training"]["intended_use"]
# we'll use the time in the output folder name to avoid overwriting previous results - so clean it up
time = datetime.datetime.now().strftime("%m-%d-%Y-%H%M%S").replace(' ', '_').replace(':', '')
# create output folder if it doesn't exist
if output_folder == '':
output_folder = os.path.join(parent_folder, 'outputs', f'_artifacts_{time}')
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# if parent folder is not split (i.e does not contain train, val, test folders), split it
dirs = os.listdir(parent_folder)
print(f'Parent folder contains: {dirs}')
if 'train' not in dirs or 'val' not in dirs or 'test' not in dirs:
print(f'Parent folder {parent_folder} is not split. Splitting now.')
data_folder = split_dataset(parent_folder, remove_background=rmbg)
else:
data_folder = parent_folder
model_name = f'{task}-{time}'
if task == '':
model_folder_name = f'task-{time}'
task = os.path.join(output_folder, f'task-{time}')
else:
model_folder_name = f'{task}-{time}'
task = f'./models/{task}-{time}/'
try:
if not os.path.exists(task):
os.makedirs(task)
except:
print(f'Error creating task folder: {task}')
check_images(data_folder)
num_classes = len(
[d for d in os.listdir(os.path.join(data_folder, 'train')) if os.path.isdir(os.path.join(data_folder, 'train', d))])
# number of training images is the total number of files in all subfolders of train
num_training_images = sum([len(files) for r, d, files in os.walk(os.path.join(data_folder, 'train'))])
# classes are the subfolders of train only (i.e. the class names)
classes = [d for d in os.listdir(os.path.join(data_folder, 'train')) if
os.path.isdir(os.path.join(data_folder, 'train', d))]
print(f'Number of classes: {num_classes}')
print(f'Classes: {classes}')
print(f'Number of training images: {num_training_images}')
# call the model
model = models_vit.__dict__[base_model](
num_classes=num_classes,
drop_path_rate=drop_path,
global_pool=True,
img_size=input_size,
)
checkpoint = torch.load(ft_weightpath, map_location=device)
checkpoint_model = checkpoint['model']
state_dict = model.state_dict()
for k in ['head.weight', 'head.bias']:
if k in checkpoint_model and checkpoint_model[k].shape != state_dict[k].shape:
print(f"Removing key {k} from pretrained checkpoint")
del checkpoint_model[k]
# interpolate position embedding
interpolate_pos_embed(model, checkpoint_model)
# load pre-trained model
msg = model.load_state_dict(checkpoint_model, strict=False)
assert set(msg.missing_keys) == {'head.weight', 'head.bias', 'fc_norm.weight', 'fc_norm.bias'}
# manually initialize fc layer
trunc_normal_(model.head.weight, std=2e-5)
print("Model = %s" % str(model))
# Save dataset information
dataset_info = {
'num_classes': num_classes,
'classes': classes,
'num_training_images': num_training_images
}
dataset_info_path = os.path.join(task, 'dataset_info.json')
with open(dataset_info_path, 'w') as f:
json.dump(dataset_info, f)
# Save training configuration
training_config = {
'batch_size': batch_size,
'epochs': epochs,
'model': 'vit_large_patch16',
'base_learning_rate': blr,
'layer_decay': layer_decay,
'weight_decay': weight_decay,
'drop_path_rate': drop_path,
'input_size': input_size,
'num_classes': num_classes,
'task': task,
'output_dir': output_folder,
'world_size': world_size,
'finetune': ft_weightpath,
'rmbg': rmbg,
# Add any other relevant configuration parameters here
}
training_config_path = os.path.join(task, 'training_config.json')
with open(training_config_path, 'w') as f:
json.dump(training_config, f)
# Prepare command for fine-tuning
command = [
'python', 'main_finetune.py',
'--data_path', data_folder, # Use the output folder from split_dataset
'--batch_size', str(batch_size),
'--world_size', str(world_size),
'--epochs', str(epochs),
'--model', base_model,
'--finetune', ft_weightpath,
'--blr', str(blr),
'--layer_decay', str(layer_decay),
'--weight_decay', str(weight_decay),
'--drop_path', str(drop_path),
'--nb_classes', str(num_classes),
'--task', f'./{task}/',
'--output_dir', output_folder,
'--input_size', str(input_size),
# Add other necessary arguments here
]
# Run the fine-tuning command
subprocess.run(command)
# Config for the model
config = {
"model_type": "vit",
"architecture": base_model,
"input_size": input_size,
"num_classes": num_classes,
"drop_path_rate": drop_path,
"layer_decay": layer_decay,
"weight_decay": weight_decay,
"base_learning_rate": blr,
"batch_size": batch_size,
"epochs": epochs,
"remove_background": rmbg,
}
# Content for the model card
model_card = {
"model_name": model_name,
"model_type": "vit",
"architecture": base_model,
"description": f'Fine-tuned {base_model} model for {model_name}',
"use_cases": use_cases,
"limitations": limitations,
"ethics": ethics,
"training_data": f'{num_training_images} images from {num_classes} classes',
"training_procedure": f'Fine-tuned for {epochs} epochs with batch size {batch_size} and base learning rate {blr}',
"intended_use": intended_use,
"authors": authors,
"references": references,
}
# Save config.json
config_path = os.path.join(task, 'config.json')
with open(config_path, 'w') as f:
json.dump(config, f)
# Save model_card.json
model_card_path = os.path.join(task, 'model_card.json')
with open(model_card_path, 'w') as f:
json.dump(model_card, f)
# Convert model_card to Markdown format and save as README.md
model_card_md = f"""# {model_card["model_name"]}
## Description
{model_card["description"]}
## Use Cases
- {model_card["use_cases"][0]}
- {model_card["use_cases"][1]}
## Limitations
- {model_card["limitations"][0]}
- {model_card["limitations"][1]}
## Ethics
- {model_card["ethics"][0]}
- {model_card["ethics"][1]}
## Training Data
{model_card["training_data"]}
## Training Procedure
{model_card["training_procedure"]}
## Intended Use
{model_card["intended_use"]}
## Authors
- {model_card["authors"][0]}
- {model_card["authors"][1]}
## References
- {model_card["references"][0]}
- {model_card["references"][1]}
"""
readme_path = os.path.join(task, 'README.md')
with open(readme_path, 'w') as f:
f.write(model_card_md)
# Create a requirements.txt file for huggingface
requirements = [
"torch==1.8.1+cu111",
"timm==0.3.2",
"torchvision==0.9.1+cu111",
"torchaudio==0.8.1",
"opencv-python>=4.5.3.56",
"pandas>=0.25.3",
"Pillow>=8.3.1",
"protobuf>=3.17.3",
"pycm>=3.2",
"pydicom>=2.3.0",
"scikit-image>=0.17.2",
"scikit-learn>=0.24.2",
"scipy>=1.5.4",
"tensorboard>=2.6.0",
"tensorboard-data-server>=0.6.1",
"tensorboard-plugin-wit>=1.8.0",
"tqdm>=4.62.1",
"einops>=0.3.0",
"h5py>=2.8.0",
"imageio>=2.9.0",
"matplotlib>=3.3.2",
"tqdm>=4.51.0",
"transformers>=3.5.1",
"utils>=1.0.1",
"Pygments>=2.7.4",
"pytorch-msssim>=1.0.0",
"toml",
]
requirements_path = os.path.join(task, 'requirements.txt')
with open(requirements_path, 'w') as f:
f.writelines(f"{req}\n" for req in requirements)
test_toml = toml.load('test_state.toml')
# update model_folder to the new model folder
test_toml["test"]["model_folder"] = model_folder_name
test_toml["test"]["input_size"] = input_size
with open('test_state.toml', 'w') as toml_file:
toml.dump(test_toml, toml_file)
from transformers import ViTConfig
# Create a ViTConfig object with relevant parameters
vit_config = ViTConfig(
image_size=input_size,
num_labels=num_classes,
# Add other parameters specific to your model architecture here
)
# Instantiate your custom model
model_wrapper = models_vit.VisionTransformerForImageClassification(config=vit_config)
# Load your trained model's state dict
model_path = os.path.join(task, 'checkpoint-best.pth')
checkpoint = torch.load(model_path, map_location='cpu')
model.load_state_dict(checkpoint['model'])
# Save the model and configuration
model_wrapper.save_pretrained(save_directory=task)
# Save the configuration separately if needed
config_path = os.path.join(task, 'config_vit.json')
vit_config.to_json_file(config_path)
# create and save a preprocessor_config.json file
size = [input_size, input_size]
preprocessor_config = {
"preprocess": {
"resize": size,
"normalize": {
"mean": [0.485, 0.456, 0.406],
"std": [0.229, 0.224, 0.225]
}
}
}
preprocessor_config_path = os.path.join(task, 'preprocessor_config.json')
with open(preprocessor_config_path, 'w') as f:
json.dump(preprocessor_config, f)