-
Notifications
You must be signed in to change notification settings - Fork 3
/
data_processing.py
86 lines (74 loc) · 3 KB
/
data_processing.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
import torch
import os
import numpy as np
from PIL import Image
from torch.utils.data.dataset import Dataset
class DatasetProcessingUCMD_21(Dataset):
def __init__(self, data_path, img_filename, label_filename, transform=None):
self.img_path = data_path
self.transform = transform
# reading img file from file
img_filepath = os.path.join(data_path, img_filename)
fp = open(img_filepath, 'r')
self.img_filename = [x.strip() for x in fp]
fp.close()
label_filepath = os.path.join(data_path, label_filename)
fp_label = open(label_filepath, 'r')
labels = [int(x.strip()) for x in fp_label]
fp_label.close()
self.label = labels
def __getitem__(self, index):
img = Image.open(os.path.join(self.img_path, self.img_filename[index]))
img = img.convert('RGB')
if self.transform is not None:
img = self.transform(img)
label = torch.LongTensor([self.label[index]])
return img, label, index
def __len__(self):
return len(self.img_filename)
class DatasetProcessingWHURS_19(Dataset):
def __init__(self, data_path, img_filename, label_filename, transform=None):
self.img_path = data_path
self.transform = transform
# reading img file from file
img_filepath = os.path.join(data_path, img_filename)
fp = open(img_filepath, 'r')
self.img_filename = [x.strip() for x in fp]
fp.close()
label_filepath = os.path.join(data_path, label_filename)
fp_label = open(label_filepath, 'r')
labels = [int(x.strip()) for x in fp_label]
fp_label.close()
self.label = labels
def __getitem__(self, index):
img = Image.open(os.path.join(self.img_path, self.img_filename[index]))
img = img.convert('RGB')
if self.transform is not None:
img = self.transform(img)
label = torch.LongTensor([self.label[index]])
return img, label, index
def __len__(self):
return len(self.img_filename)
class DatasetProcessingAID_30(Dataset):
def __init__(self, data_path, img_filename, label_filename, transform=None):
self.img_path = data_path
self.transform = transform
# reading img file from file
img_filepath = os.path.join(data_path, img_filename)
fp = open(img_filepath, 'r')
self.img_filename = [x.strip() for x in fp]
fp.close()
label_filepath = os.path.join(data_path, label_filename)
fp_label = open(label_filepath, 'r')
labels = [int(x.strip()) for x in fp_label]
fp_label.close()
self.label = labels
def __getitem__(self, index):
img = Image.open(os.path.join(self.img_path, self.img_filename[index]))
img = img.convert('RGB')
if self.transform is not None:
img = self.transform(img)
label = torch.LongTensor([self.label[index]])
return img, label, index
def __len__(self):
return len(self.img_filename)