-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutils.py
59 lines (53 loc) · 1.96 KB
/
utils.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
import torch
from glob import glob
from PIL import Image
import os
from torchvision import transforms as T
from torchvision.datasets import CIFAR10, CIFAR100
class UnlabeledImageFolder(torch.utils.data.Dataset):
def __init__(self, root, transform=None, exts=["*.jpg", "*.png", "*.jpeg", "*.webp"]):
self.root = root
self.files = []
self.transform = transform
for ext in exts:
self.files.extend(glob(os.path.join(root, '**/*.{}'.format(ext)), recursive=True))
def __len__(self):
return len(self.files)
def __getitem__(self, idx):
path = self.files[idx]
img = Image.open(path).convert("RGB")
if self.transform is not None:
img = self.transform(img)
return img
def set_dropout(model, p):
for m in model.modules():
if isinstance(m, torch.nn.Dropout):
m.p = p
def get_dataset(name_or_path, transform=None):
if name_or_path.lower()=='cifar10':
if transform is None:
transform = T.Compose([
T.RandomHorizontalFlip(),
T.ToTensor(),
T.Normalize(mean=0.5, std=0.5),
])
dataset = CIFAR10(root='./data', train=True, download=True, transform=transform)
elif name_or_path.lower()=='cifar100':
if transform is None:
transform = T.Compose([
T.RandomHorizontalFlip(),
T.ToTensor(),
T.Normalize(mean=0.5, std=0.5),
])
dataset = CIFAR100(root='./data', train=True, download=True, transform=transform)
elif os.path.isdir(name_or_path):
if transform is None:
transform = T.Compose([
T.Resize(256),
T.RandomCrop(256),
T.RandomHorizontalFlip(),
T.ToTensor(),
T.Normalize(mean=0.5, std=0.5),
])
dataset = UnlabeledImageFolder(name_or_path, transform=transform)
return dataset