-
Notifications
You must be signed in to change notification settings - Fork 0
/
data2.py
289 lines (247 loc) · 7.84 KB
/
data2.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from typing import Dict, Union
import os
import pandas as pd
import numpy as np
import tensorflow as tf
def augmentations(
x, crop_size=22, brightness=0.2, contrast=0.2, saturation=0.2, hue=0.2
):
x = tf.cast(x, tf.float32)
x = tf.image.random_crop(x, (tf.shape(x)[0], 100, 100, 3))
x = tf.image.random_brightness(x, max_delta=brightness)
x = tf.image.random_contrast(x, lower=1.0 - contrast, upper=1 + contrast)
x = tf.image.random_saturation(x, lower=1.0 - saturation, upper=1.0 + saturation)
x = tf.image.random_hue(x, max_delta=hue)
x = tf.image.resize(x, (128, 128))
x = tf.clip_by_value(x, 0.0, 255.0)
x = tf.keras.applications.resnet_v2.preprocess_input(x)
return x
def symmetric_batch(batch_x):
return tf.concat([augmentations(batch_x), augmentations(batch_x)], axis=0)
def resize_images(batch_x, width=224, height=224):
return tf.image.resize(batch_x, (width, height))
def data_config(username="irodri15_oscar"):
if username == "irodri15_oscar":
local_datasets_dir = "/users/irodri15/data/irodri15/Fossils/Experiments/softmax_triplet/datasets/"
pretrained_weights_dir = "/users/irodri15/data/irodri15/Fossils/Experiments/softmax_triplet/pretrained/"
training_models_dir = "e"
caffe_iter_size = 1
logging_threshold = 100
batch_size = 32
else:
print("not implemented")
return local_datasets_dir, pretrained_weights_dir, training_models_dir
def load_train_test_val(train_csv_file, test_csv_file, val_csv_file):
train_df, test_df, val_df = (
pd.read_csv(train_csv_file),
pd.read_csv(test_csv_file),
pd.read_csv(val_csv_file),
)
# import pdb;pdb.set_trace()
# make domains to have same amount of data.
try:
labels, counts = np.unique(train_df.domain.tolist(), return_counts=True)
ratio = np.max(counts) // np.min(counts)
under_rep_domain = labels[np.argmin(counts)]
train_df = pd.concat(
[train_df] + [train_df[train_df.domain == under_rep_domain]] * ratio
)
except:
print("domain not present")
return {"train": train_df, "val": val_df, "test": test_df}
def load_dataset_from_file(
username="irodri15_oscar",
dataname="leaves_fossils_fewshot_v1.0",
split=None,
thresh=None,
label_out=None,
shot=None,
):
csv_path = "./csv/fossils.csv"
test_df = pd.read_csv(csv_path)
return {"test": test_df}
def load_data_from_tensor_slices(
data: pd.DataFrame,
training=False,
seed=42,
x_col="path",
y_col="label",
d_col="domain",
dtype=tf.float32,
number_classes=19,
wsize=600,
hsize=600,
gray=False,
):
dtype = dtype or tf.uint8
num_samples = data.shape[0]
def load_img(image_path, gray=False):
img = tf.io.read_file(image_path)
img = tf.image.decode_jpeg(img, channels=3)
img = tf.image.convert_image_dtype(img, tf.float32)
if gray:
img = tf.image.rgb_to_grayscale(img)
img = tf.image.grayscale_to_rgb(img)
img = tf.image.resize(img, (wsize, hsize))
return img
# print("\n\n\n\n columns are : ", list(data[d_col].columns), "\n\n\n")
x_data = tf.data.Dataset.from_tensor_slices(data[x_col].values.tolist())
y_data = tf.data.Dataset.from_tensor_slices(
data[y_col].astype("int").values.tolist()
)
d_data = tf.data.Dataset.from_tensor_slices(
data[d_col].astype("int").values.tolist()
)
data = tf.data.Dataset.zip((x_data, y_data, d_data))
data = data.map(lambda x, y, d: {"x": x, "y": y, "d": d})
data = data.take(num_samples).cache()
# TODO TEST performance and randomness of the order of shuffle and cache when shuffling full dataset each iteration, but only filepaths and not full images.
if training:
data = data.shuffle(num_samples, seed=seed, reshuffle_each_iteration=True)
# import pdb;pdb.set_trace()
data = data.map(
lambda example: {
"x": tf.image.convert_image_dtype(
load_img(example["x"], gray=gray), dtype=dtype
),
"y": tf.one_hot(example["y"], number_classes),
"d": example["d"],
"p": example["x"],
},
num_parallel_calls=-1,
)
return data
def extract_data(
data,
number_classes=19,
hsize=600,
wsize=600,
shuffle_first=True,
seed=None,
gray=False,
):
subset_keys = list(data.keys())
extracted_data = {}
for subset in subset_keys:
if shuffle_first:
data[subset] = data[subset].sample(frac=1)
paths = data[subset]["file_name"]
labels = data[subset]["label"]
if "domain" not in data[subset].columns:
data[subset]["domain"] = [1] * len(data[subset])
domains = data[subset]["domain"]
extracted_data[subset] = pd.DataFrame.from_records(
[
{"path": path, "label": label, "domain": text_label}
for path, label, text_label in zip(paths, labels, domains)
]
)
training = subset == "train"
extracted_data[subset] = load_data_from_tensor_slices(
data=extracted_data[subset],
number_classes=number_classes,
training=training,
seed=seed,
x_col="path",
y_col="label",
wsize=wsize,
hsize=hsize,
dtype=tf.float32,
gray=gray,
)
return extracted_data
def load_and_extract_data(
number_classes=19,
username="irodri15_oscar",
dataname="leaves_fossils_fewshot_v1.0",
hsize=600,
wsize=600,
label_out=None,
split=None,
shot=None,
shuffle_first=True,
seed=None,
batch_size=32,
gray=False,
thresh=None,
computing_viz=False,
):
data = load_dataset_from_file(
username="irodri15_oscar",
dataname=dataname,
label_out=label_out,
shot=shot,
split=split,
thresh=thresh,
)
data = extract_data(
data,
shuffle_first=shuffle_first,
hsize=hsize,
wsize=wsize,
seed=seed,
number_classes=number_classes,
gray=gray,
)
test_dataset = data["test"].batch(batch_size)
return test_dataset
def leaves_fewshot_ds(
label_out,
shot,
split=None,
wsize=600,
hsize=600,
batch_size=32,
dataname="leaves_fossils_fewshot_v1.0",
number_classes=19,
gray=False,
thresh=None,
computing_viz=False,
):
test = load_and_extract_data(
label_out=label_out,
wsize=wsize,
hsize=hsize,
shot=shot,
split=split,
number_classes=number_classes,
batch_size=batch_size,
dataname=dataname,
gray=gray,
thresh=thresh,
computing_viz=computing_viz,
)
if computing_viz:
test = test.map(
lambda sample: (
resize_images(sample["x"], wsize, hsize),
sample["y"],
sample["d"],
sample["p"],
)
)
else:
test = test.map(
lambda sample: (
resize_images(sample["x"], wsize, hsize),
sample["y"],
sample["d"],
)
)
return test
def mocking_ds():
"""What I need"""
nb_samples = 200
size = 600
inputs = tf.random.normal((nb_samples, size, size, 3)) # (N, 600, 600, 3)
labels = tf.one_hot(
tf.argmax(tf.random.normal((nb_samples, 19)), -1), 19
) # (N, 19) one hot encoded classes
domains = tf.cast(
tf.random.normal((nb_samples,)) > 0.5, tf.int32
) # (N,) int32 1 or zero
train_ds = (
tf.data.Dataset.from_tensor_slices((inputs, labels, domains)).batch(32).repeat()
)
test_ds = tf.data.Dataset.from_tensor_slices((inputs, labels, domains)).batch(32)
return train_ds, test_ds