-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
52 lines (46 loc) · 1.69 KB
/
Copy pathutils.py
File metadata and controls
52 lines (46 loc) · 1.69 KB
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
import os
import csv
import pandas as pd
from torchvision.io import read_image
import numpy as np
def register_images(csv_name, output_path="./", data_path="./") -> None:
files: list[str] = os.listdir(data_path)
with open(csv_name, "w") as f:
writer = csv.writer(f)
writer.writerow(["img1", "img2", "mask"])
prefixs: str = [f.split("_")[0] for f in files]
prefixs = list(set([f for f in prefixs if "README" not in f]))
print(prefixs)
for i in range(len(prefixs)):
files_prefixed = [f for f in files if prefixs[i] in f]
mask = [f for f in files_prefixed if "cm" in f]
img1 = [f for f in files_prefixed if "1" in f]
img2 = [f for f in files_prefixed if "2" in f]
writer.writerow([img1[0], img2[0], mask[0]])
print("Image registered in CSV file")
return None
def split_dataset(percentage=80, dataset="data.csv"):
data = pd.read_csv(dataset)
#Shuffle the data
train = data.sample(frac=percentage/100)
val = data.drop(train.index)
train.to_csv("train.csv", index=False)
val.to_csv("val.csv", index=False)
def class_weights(data_file):
# Compute the class weights
data = pd.read_csv(data_file)
mask = data["mask"]
w0 = 0
w1 = 0
for i in range(len(mask)):
file = mask[i]
img = read_image("data/"+file)
total_pixels = img.shape[1]*img.shape[2]
w1 += 5*img[0].sum()/(255*total_pixels)
w0 += (total_pixels - (img[0].sum())/255)/total_pixels
return [w0, w1]
def main():
register_images("data.csv", data_path="./data")
split_dataset(percentage=80, dataset="data.csv")
if __name__ == "__main__":
main()