-
Notifications
You must be signed in to change notification settings - Fork 0
/
face_mask_DataLoader.py
74 lines (69 loc) · 3.08 KB
/
face_mask_DataLoader.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
import torch
import numpy as np
from torch.utils.data import Dataset, DataLoader
import os
from torchvision.io import read_image
from torchvision import transforms
class faceMaskDataSet(Dataset):
pathMask = 'Face Mask Dataset/Train/WithMask/'
pathNoMask = 'Face Mask Dataset/Train/WithoutMask/'
def __init__(self):
self.myTransforms = transforms.Resize(256)
self.maskData = os.listdir(faceMaskDataSet.pathMask)
self.noMaskData = os.listdir(faceMaskDataSet.pathNoMask)
self.maskLabel = np.ones(len(self.maskData))
self.noMaskLabel = np.zeros(len(self.noMaskData))
self.X = self.maskData + self.noMaskData
self.Y = np.concatenate((self.maskLabel, self.noMaskLabel))
def __len__(self):
return len(self.X)
def __getitem__(self,index):
self.y = self.Y[index]
self.filePath = "Face Mask Dataset/Train/WithMask/" if self.y==1.\
else "Face Mask Dataset/Train/WithoutMask/"
self.path = self.X[index]
self.image = read_image(self.filePath + self.path)
self.x = self.myTransforms(self.image)/255
return self.x, self.y
class faceMaskTestSet(Dataset):
pathMask = 'Face Mask Dataset/Test/WithMask/'
pathNoMask = 'Face Mask Dataset/Test/WithoutMask/'
def __init__(self):
self.myTransforms = transforms.Resize(256)
self.maskData = os.listdir(faceMaskTestSet.pathMask)
self.noMaskData = os.listdir(faceMaskTestSet.pathNoMask)
self.maskLabel = np.ones(len(self.maskData))
self.noMaskLabel = np.zeros(len(self.noMaskData))
self.X = self.maskData + self.noMaskData
self.Y = np.concatenate((self.maskLabel, self.noMaskLabel))
def __len__(self):
return len(self.X)
def __getitem__(self,index):
self.y = self.Y[index]
self.filePath = "Face Mask Dataset/Test/WithMask/" if self.y==1\
else "Face Mask Dataset/Test/WithoutMask/"
self.path = self.X[index]
self.image = read_image(self.filePath + self.path)
self.x = self.myTransforms(self.image)/255
return self.x, self.y
class faceMaskAccSet(Dataset):
pathMask = 'Face Mask Dataset/Validation/WithMask/'
pathNoMask = 'Face Mask Dataset/Validation/WithoutMask/'
def __init__(self):
self.myTransforms = transforms.Resize(256)
self.maskData = os.listdir(faceMaskAccSet.pathMask)
self.noMaskData = os.listdir(faceMaskAccSet.pathNoMask)
self.maskLabel = np.ones(len(self.maskData))
self.noMaskLabel = np.zeros(len(self.noMaskData))
self.X = self.maskData + self.noMaskData
self.Y = np.concatenate((self.maskLabel, self.noMaskLabel))
def __len__(self):
return len(self.X)
def __getitem__(self,index):
self.y = self.Y[index]
self.filePath = "Face Mask Dataset/Validation/WithMask/" if self.y==1\
else "Face Mask Dataset/Validation/WithoutMask/"
self.path = self.X[index]
self.image = read_image(self.filePath + self.path)
self.x = self.myTransforms(self.image)/255
return self.x, self.y