-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset_2.py
More file actions
59 lines (51 loc) · 1.86 KB
/
Copy pathdataset_2.py
File metadata and controls
59 lines (51 loc) · 1.86 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
53
54
55
56
57
58
59
import os
import torch
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
from PIL import Image
import pandas as pd
import numpy as np
import pydicom
class MammoDataset(Dataset):
def __init__(self, df, transform, image_dir, target='pathology'):
self.df = df
self.transform = transform
self.target = target
self.image_dir = image_dir
self.label_map = {'benign': 0, 'malignant': 1}
def __len__(self):
return len(self.df)
def __getitem__(self, index):
image_name = self.df.iloc[index]['image file path']
image_path = os.path.join(self.image_dir, image_name)
if not os.path.exists(image_path):
base,ext=os.path.splitext(image_path)
alternatives=['.jpeg','.jpg','']
found=False
for alternatite_ext in alternatives:
alt_paths=base+alternatite_ext
if os.path.exists(alt_paths):
image_path=alt_paths
found=True
break
if not found:
raise FileNotFoundError(f"File not found with expected extensions: {image_path}")
image = Image.open(image_path).convert('RGB')
label = self.label_map[self.df.iloc[index][self.target]]
if self.transform is not None:
image = self.transform(image)
return image, label
def compute_mean_std(dataset):
temp_loader = DataLoader(dataset, batch_size=16, shuffle=False)
mean = 0.
std = 0.
total = 0
for images, _ in temp_loader:
batch_samples = images.size(0)
images = images.view(batch_samples, images.size(1), -1)
mean += images.mean(2).sum(0)
std += images.std(2).sum(0)
total += batch_samples
mean /= total
std /= total
return mean.tolist(), std.tolist()