-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
354 lines (297 loc) · 12 KB
/
utils.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
import glob
import pandas as pd
import random
import os
import torchvision.transforms as transforms
import torch
import time
import matplotlib.pyplot as plt
import pprint
import yaml
import itertools
import re
import ast
from collections import defaultdict
from typing import List, Dict
from torch import Tensor
from torch.utils.data.dataloader import DataLoader
from tqdm import tqdm
from typing import Tuple
from dataset import BreastCancer
from sklearn.model_selection import train_test_split
def create_dataset_csv(root_dir: str, seed: int = 2023):
"""
create the csv table with columns:
- img_path: file path to each image
- label: main type of image (benign/malignant)
- subtype: subtypes of benign and malignant tumors (A/F/PT/TA/MC/DC/LC/PC)
- magnification: maginification of each image (40X/100X/200X/400X)
- split: split the dataset into train:eval:test = 60:20:20
"""
img_paths = find_images(root_dir)
df = pd.DataFrame(img_paths, columns=["img_path"])
df["label"] = df["img_path"].str.split("/", expand=True).iloc[:, 8]
df["subtype"] = (
df["img_path"]
.str.split("/", expand=True)
.iloc[:, 10]
.str.split("_", expand=True)
.iloc[:, 2]
)
df["magnification"] = df["img_path"].str.split("/", expand=True).iloc[:, 11]
#### create a dataframe consists of 4 cols: img_path, label, magnification, subtype and split (train/eval/test) ## e.g.,
## img_path label subtype magnification split
## '/.../SOB_M_MC-14-13418DE-100-009.png' benign MC 100X train
## '/.../SOB_M_MC-14-13418DE-100-008.png' benign MC 100X train
## '/.../SOB_M_MC-14-13418DE-100-003.png' benign MC 100X train
## split the dataset into train:eval:test = 60:20:20, stratified by magnification
train, valid_test = train_test_split(
df, test_size=0.4, stratify=df["magnification"], random_state=seed
)
valid, test = train_test_split(
valid_test, test_size=0.5, stratify=valid_test["magnification"], random_state=seed
)
train["split"] = "train"
valid["split"] = "valid"
test["split"] = "test"
df = pd.concat([train, valid, test])
return df
def find_images(directory, image_extensions=["*.png", "*.jpg", "*.jpeg", "*.gif", "*.bmp"]):
"""
Recursively finds all images in the directory with specified extensions.
:param directory: The directory to search in.
:param image_extensions: List of image file extensions to search for.
:return: List of paths to the images found.
"""
image_paths = []
for extension in image_extensions:
image_paths.extend(glob.glob(os.path.join(directory, "**", extension), recursive=True))
return image_paths
def compute_mean_std(loader):
mean = 0.0
for images, _, _ in tqdm(loader):
# print(images.shape) : 10, 3, 238, 374
batch_samples = images.size(0)
images = images.view(batch_samples, images.size(1), -1)
# print(images.shape): 10, 3, 89012
mean += images.mean(2).sum(0)
mean = mean / len(loader.dataset)
var = 0.0
pixel_count = 0
for images, _, _ in tqdm(loader):
batch_samples = images.size(0)
images = images.view(batch_samples, images.size(1), -1)
var += ((images - mean.unsqueeze(1)) ** 2).sum([0, 2])
pixel_count += images.nelement() / images.size(1)
std = torch.sqrt(var / pixel_count)
return list(mean.numpy()), list(std.numpy())
def get_dataloaders(batch_size: int, img_size: Tuple):
## prepare dataset
transform = transforms.Compose(
[
transforms.ToTensor(),
transforms.Resize(img_size, antialias=True),
transforms.Normalize((0.7844108, 0.6242002, 0.76210874), (0.122441396, 0.17505719, 0.10627644)),
]
)
trainset = BreastCancer(
csv_path="breast_cancer_meta_data.csv",
split="train",
transform=transform,
)
validset = BreastCancer(
csv_path="breast_cancer_meta_data.csv",
split="valid",
transform=transform,
)
testset = BreastCancer(
csv_path="breast_cancer_meta_data.csv",
split="test",
transform=transform,
)
trainloader = DataLoader(
trainset, batch_size=batch_size, shuffle=True, num_workers=2, drop_last=True
)
validloader = DataLoader(
validset, batch_size=batch_size, shuffle=True, num_workers=2, drop_last=False
)
testloader = DataLoader(
testset, batch_size=batch_size, shuffle=False, num_workers=2, drop_last=False
)
return trainloader, validloader, testloader
def print_out(print_str, log):
print(print_str)
datetime_now = time.strftime("%Y-%m-%d_%H-%M-%S")
## append different models to log file:
log.write(datetime_now + ": " + print_str + "\n")
log.flush()
def plot_train_eval_summary(df_train_summary, df_eval_summary):
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))
ax1.plot(df_train_summary["epoch"], df_train_summary["40X"], label="40X")
ax1.plot(df_train_summary["epoch"], df_train_summary["100X"], label="100X")
ax1.plot(df_train_summary["epoch"], df_train_summary["200X"], label="200X")
ax1.plot(df_train_summary["epoch"], df_train_summary["400X"], label="400X")
ax1.plot(
df_train_summary["epoch"],
df_train_summary["avg_acc"],
label="Average Accuracy",
color="black",
linewidth=2,
linestyle="--",
)
ax1.set_title("Training accuracy over Epochs")
ax1.set_xlabel("Epoch")
ax1.set_ylabel("Accuracy (%)")
ax1.legend()
ax2.plot(df_eval_summary["epoch"], df_eval_summary["40X"], label="40X")
ax2.plot(df_eval_summary["epoch"], df_eval_summary["100X"], label="100X")
ax2.plot(df_eval_summary["epoch"], df_eval_summary["200X"], label="200X")
ax2.plot(df_eval_summary["epoch"], df_eval_summary["400X"], label="400X")
ax2.plot(
df_eval_summary["epoch"],
df_eval_summary["avg_acc"],
label="Average Accuracy",
color="black",
linewidth=2,
linestyle="--",
)
ax2.set_title("Training accuracy over Epochs")
ax2.set_title("Evaluation accuracy over Epochs")
ax2.set_xlabel("Epoch")
ax2.set_ylabel("Accuracy (%)")
ax2.legend()
plt.show()
def resnet_get_grid_search(config_path: str,
optimizers: list,
learning_rates: list,
num_blocks_list:list,
is_batchnorm:list) -> List[Dict]:
"""
Read the config file and return a list of all possible combinations of hyperparameters
"""
## read the config file
with open(config_path, 'r') as f:
config = yaml.safe_load(f)
combinations = list(
itertools.product(optimizers, learning_rates, num_blocks_list, is_batchnorm)
)
resnet_grid_search = []
for opt, lr, num_blocks, bn in combinations:
new_config = config.copy()
new_config["optimizer"] = opt
new_config["lr"] = lr
new_config["num_blocks_list"] = num_blocks
new_config["is_batchnorm"] = bn
resnet_grid_search.append(new_config)
return resnet_grid_search
def mlp_get_grid_search(config_path: str,
optimizers: list,
learning_rates: list,
weight_decay:list) -> List[Dict]:
"""
Read the config file and return a list of all possible combinations of hyperparameters
"""
## read the config file
with open(config_path, 'r') as f:
config = yaml.safe_load(f)
combinations = list(
itertools.product(optimizers, learning_rates, weight_decay)
)
mlp_grid_search = []
for opt, lr, reg in combinations:
new_config = config.copy()
new_config["optimizer"] = opt
new_config["lr"]=lr
new_config["weight_decay"] = reg
mlp_grid_search.append(new_config)
return mlp_grid_search
def cnn_get_grid_search(config_path: str,
optimizers: list,
learning_rates: list,
num_kernel_conv_list:list,
use_pooling:list)-> List[Dict]:
"""
Read the config file and return a list of all possible combinations of hyperparameters
"""
## read the config file
with open(config_path, 'r') as f:
config = yaml.safe_load(f)
combinations = list(
itertools.product(optimizers, learning_rates, num_kernel_conv_list, use_pooling)
)
cnn_grid_search = []
for opt, lr, num_kernel_conv, pooling in combinations:
new_config = config.copy()
new_config["optimizer"] = opt
new_config["lr"] = lr
new_config["num_kernel_conv_list"] = num_kernel_conv
new_config["use_pooling"] = pooling
cnn_grid_search.append(new_config)
return cnn_grid_search
def create_result_summary_dictionary(log_path: str) -> Dict:
with open(log_path, 'r') as f:
# Read the file
log = f.read()
# Extract the config dictionary string
config_dict_str = re.search(r"config: ({.*})", log).group(1)
# Convert the string to a config dictionary
config = ast.literal_eval(config_dict_str)
# Extract the total parameters
try:
total_parameters_match = re.search(r"Total parameters: ([\d,]+);", log)
if total_parameters_match is not None:
total_parameters = total_parameters_match.group(1)
else:
total_parameters_match = re.search(r"Total parameters: ([\d,]+)", log)
if total_parameters_match is not None:
total_parameters = total_parameters_match.group(1)
else:
total_parameters = "0" # Default value if no match is found
except AttributeError:
total_parameters = "0" # Default value if an error occurs
# Remove commas and convert to integer
total_parameters = int(total_parameters.replace(',', ''))
config["total_parameters"] = total_parameters
#Extract the final test accuracy dictionary string
test_accuracy_dict_str = re.search(r"Final test accuracy: ({.*})", log)
if test_accuracy_dict_str is not None:
test_accuracy_dict_str = test_accuracy_dict_str.group(1)
final_test_accuracy = ast.literal_eval(test_accuracy_dict_str)
# Merge config and final_test_accuracy dictionaries
config.update(final_test_accuracy)
config["log_name"] = log_path.split("/")[-1]
if config["log_name"].startswith("resnet"):
config["is_batchnorm"] = True
return config
def get_all_log_results():
log_paths = glob.glob("logs/*.log")
config_list = []
for log_path in log_paths:
config = create_result_summary_dictionary(log_path)
if config is not None:
config_list.append(config)
df = pd.DataFrame(config_list)
# df.drop_duplicates(inplace=True)
df.sort_values(by=["avg_acc"], ascending=False, inplace=True)
# df = df.loc[df.astype(str).drop_duplicates(subset=["lr","optimizer","num_blocks_list","is_batchnorm"], keep="first").index]
df.reset_index(drop=True, inplace=True)
df.to_csv("result_summary.csv")
return df
if __name__ == "__main__": ### put all test code in this block
# log_path = "logs/v2_resnet_epoch30_lr0.0001_2023-12-03_18-15-17.log"
# res = create_result_summary_dictionary(log_path = log_path)
# print(res)
# get all log files in logs folder
get_all_log_results()
# Open the file
# path = "logs/resnet_epoch10_lr0.001_2023-12-02_14-27-14.log"
# folder_path = "data_model/"
# df = create_dataset_csv(folder_path)
# print(df.sample(30))
# df.to_csv("breast_cancer_meta_data.csv")
# Calculate mean and std of trainloader
# trainloader = get_dataloaders(batch_size=32, img_size=(224, 224))[0]
# mean, std = compute_mean_std(trainloader) # get_mean_std(trainloader)
# print(mean)
# print(std)
# pass