-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmd_fast.py
33 lines (24 loc) · 1.41 KB
/
fmd_fast.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
def load_data(data_dir='/content/image', seed=42, train_rate=0.5):
transform_train = transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
transform_test = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
dataset_train = datasets.ImageFolder(data_dir, transform=transform_train)
dataset_test = datasets.ImageFolder(data_dir, transform=transform_test)
num_train = int(len(dataset_train) * train_rate)
num_test = len(dataset_train) - num_train
trainset = torch.utils.data.random_split(dataset_train, [num_train,num_test], generator=torch.Generator().manual_seed(seed))[0]
testset = torch.utils.data.random_split(dataset_test, [num_train,num_test], generator=torch.Generator().manual_seed(seed))[1]
return trainset, testset
# Example usage
trainset, testset = load_data(data_dir='/content/image', seed=42, train_rate=0.5)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, shuffle=True, num_workers=2)
testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size, shuffle=False, num_workers=2)