-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataloader.py
73 lines (56 loc) · 2.01 KB
/
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
import os
import numpy as np
from dataset import ImageFolder
from paddle.vision.transforms import Compose, Resize
from paddle.io import DataLoader
def load_data(args, config):
normal_class = config['normal_class']
batch_size = config['batch_size']
data_path = os.path.join(args.dataset_root, normal_class, 'train')
mvtec_img_size = config['mvtec_img_size']
orig_transform = Compose([
Resize([mvtec_img_size, mvtec_img_size]),
])
train_dataset = ImageFolder(root=data_path, transform=orig_transform)
test_data_path = os.path.join(args.dataset_root, normal_class,'test')
test_dataset = ImageFolder(root=test_data_path, transform=orig_transform)
train_dataloader = DataLoader(
train_dataset,
batch_size=batch_size,
shuffle=True,
num_workers=8,
)
test_dataloader = DataLoader(
test_dataset,
batch_size=batch_size,
shuffle=False,
num_workers=8,
)
return train_dataloader, test_dataloader
def load_localization_data(args, config):
normal_class = config['normal_class']
mvtec_img_size = config['mvtec_img_size']
orig_transform = Compose([
Resize([mvtec_img_size, mvtec_img_size]),
])
test_data_path = os.path.join(args.dataset_root, normal_class, 'test')
test_set = ImageFolder(root=test_data_path, transform=orig_transform, localization_test=True)
test_dataloader = DataLoader(
test_set,
batch_size=512,
shuffle=False,
num_workers=8,
)
ground_data_path = os.path.join(args.dataset_root, normal_class, 'ground_truth')
ground_dataset = ImageFolder(root=ground_data_path, transform=orig_transform)
ground_dataloader = DataLoader(
ground_dataset,
batch_size=512,
shuffle=False,
num_workers=8,
)
x_ground = next(iter(ground_dataloader))[0].numpy()
ground_temp = x_ground
std_groud_temp = np.transpose(ground_temp, (0, 2, 3, 1))
x_ground = std_groud_temp
return test_dataloader, x_ground