-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutil.py
66 lines (53 loc) · 2.22 KB
/
util.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
import os
from pathlib import Path
def read_image(dataset_tag, extension=".png", onehot=False, reload=False, cache=True):
from matplotlib import pyplot as plt
import tensorflow.keras as keras
import numpy as np
# For sake of simplicity, switch dir and do things inside
pwd = Path(".").absolute()
image_dir = f"{dataset_tag}_images"
label_file = f"{dataset_tag}_labels.csv"
cache_npz = f"{dataset_tag}_cache.npz"
if not reload and Path(cache_npz).exists():
print(f"Reuse cached array {cache_npz}")
cache = np.load(cache_npz)
x, y = cache['x'], cache['y']
else:
# pushd
os.chdir(image_dir)
im_files = sorted(os.listdir("."))
images = [plt.imread(f)[:, :, :3] for f in im_files if f.endswith(extension)]
print(f'Number of {dataset_tag} images:', len(images))
x = np.array(images)
print(f'x_{dataset_tag} shape:', x.shape)
# popd
os.chdir(pwd)
# Read training labels
y = np.genfromtxt(label_file, delimiter=',')
print(f'Number of training labels equals number of {dataset_tag} images:',
len(y) == x.shape[0])
if cache:
np.savez_compressed(cache_npz, x=x, y=y)
print(f"Cached saved as {cache_npz}")
# Do we want to use one-hot encoding for labels
if onehot:
y = keras.utils.to_categorical(y, num_classes=2)
return x, y
def inject_config():
import inspect
stack = inspect.stack()
caller_globals = stack[1][0].f_globals
# Means we need switch to project directory
if '_src_root' in caller_globals:
os.chdir(caller_globals['_src_root'])
# Set variables
HOME_DIR = Path(".").absolute()
caller_globals['HOME_DIR'] = HOME_DIR
caller_globals['TRAIN_DIR'] = str(HOME_DIR / 'train_images')
caller_globals['VALIDATION_DIR'] = str(HOME_DIR / 'validation_images')
caller_globals['TEST_DIR'] = str(HOME_DIR / 'test_images')
caller_globals['TRAIN_LABELS'] = str(HOME_DIR / 'train_labels.csv')
caller_globals['VALIDATION_LABELS'] = str(HOME_DIR / 'validation_labels.csv')
caller_globals['TEST_LABELS'] = str(HOME_DIR / 'test_labels.csv')
caller_globals['EXTENSION'] = '.png'