-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
60 lines (42 loc) · 2.09 KB
/
data.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
from pathlib import Path
import requests
import zipfile
import os
def downlaod_data(source: str, destination: str, remove_source: bool=True) -> Path:
data_path = Path('data')
image_path = data_path / destination
if image_path.is_dir():
print(f"The {destination} path is already exict, skipping downlaod")
else:
image_path.mkdir(parents=True, exist_ok=True)
target_file = Path(source).name
with open(data_path / target_file, 'wb') as f:
request = requests.get(source)
print("downloading data from source")
f.write(request.content)
with zipfile.ZipFile(data_path / target_file, 'r') as zipref:
print("Unziping data")
zipref.extractall(image_path)
if remove_source:
os.remove(data_path / target_file)
return image_path.absolute()
from torch.utils.data import DataLoader
from torchvision import transforms, datasets
NUM_WORKER = os.cpu_count()
def data_setup(train_dir: str, test_dir: str, batch_size: int, transform: transforms.Compose=None, num_worker=NUM_WORKER):
if transform:
img_transform = transform
elif not transform:
img_transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
# Loading data with the label. This dataloader will use folder name as label
train_data = datasets.ImageFolder(train_dir, transform=img_transform)
test_data = datasets.ImageFolder(test_dir, transform=img_transform)
class_names = train_data.classes
train_dataloader = DataLoader(dataset=train_data, batch_size=batch_size, shuffle=True, num_workers=NUM_WORKER)
test_dataloader = DataLoader(dataset= test_data, batch_size=batch_size, num_workers=NUM_WORKER)
return train_dataloader, test_dataloader, class_names