-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
76 lines (64 loc) · 2.55 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from tensorflow import keras
from pathlib import Path
from tqdm import tqdm
import numpy as np
from utils import Config
class Data:
states = {i: state.name for i, state in enumerate(Path(f'{Config.PATH}data').glob('*/'))}
def __init__(self,
validation_split: float = 0.15,
image_size: tuple = (256, 256),
batch_size: int = 32,
seed: int = 123,
label_mode: str = 'categorical'):
self.val_split = validation_split
self.image_size = image_size
self.batch_size = batch_size
self.seed = seed
self.label_mode = label_mode
def load_train(self,
name: str,
path: str = Config.PATH):
return keras.preprocessing.image_dataset_from_directory(
f'{path}{name}',
validation_split=self.val_split,
subset="training",
seed=self.seed,
label_mode=self.label_mode,
image_size=self.image_size,
batch_size=self.batch_size)
def load_val(self,
name: str,
path: str = Config.PATH):
return keras.preprocessing.image_dataset_from_directory(
f'{path}{name}',
validation_split=self.val_split,
subset="validation",
seed=self.seed,
label_mode=self.label_mode,
image_size=self.image_size,
batch_size=self.batch_size)
def load_test(self,
path: str = Config.PATH) -> tuple:
x_test, y_test = [], []
test_ds = keras.preprocessing.image_dataset_from_directory(
f'{path}test_data',
label_mode=self.label_mode,
image_size=self.image_size,
batch_size=self.batch_size)
for images, labels in tqdm(test_ds.unbatch()):
x_test.append(images.numpy())
y_test.append(labels.numpy())
return np.array(x_test), np.array(y_test)
def preprocess_data_sep_by_heading():
path = 'C:/Users/TuriB/Documents/5.felev/bevadat/geo_project/geolocator'
headings = [0, 90, 180, 270]
data = Path(f'{path}/data')
for heading in headings:
outdir = Path(f'{path}/pictures_{str(heading)}')
outdir.mkdir(exist_ok=True, parents=True)
for directory in tqdm(data.glob("*")):
outdir2 = Path(f'pictures_{str(heading)}/{directory.name}')
outdir2.mkdir(exist_ok=True, parents=True)
for file in directory.glob(f"*_{str(heading)}.jpg"):
file.rename(outdir2 / file.name)