-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdataset.py
More file actions
38 lines (30 loc) · 1.23 KB
/
dataset.py
File metadata and controls
38 lines (30 loc) · 1.23 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
from torch.utils.data import Dataset
from PIL import Image
from torchvision import transforms
import torch
import pandas as pd
import os
imageSize = 32
class AntennaDataset(Dataset):
def __init__(self, annotations_file, img_dir, transform=None, target_transform=None):
self.img_labels = pd.read_csv(annotations_file)
self.img_dir = img_dir
self.transform = transform
self.target_transform = target_transform
def __len__(self):
return len(self.img_labels)
def __getitem__(self, idx):
img_path = os.path.join(self.img_dir, self.img_labels.iloc[idx, 0]+'.png')
image = Image.open(img_path).convert("RGB")
label = torch.tensor(self.img_labels.iloc[idx, 1:].tolist(), dtype=float)
label = label
title = self.img_labels.iloc[idx, 0]
if self.transform:
image = self.transform(image)
return image.float(), label.float(), title
transform = transforms.Compose([
transforms.Grayscale(num_output_channels=1),
transforms.Resize(imageSize),
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,)),
])