-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
1608 lines (1348 loc) · 50.9 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import datetime
import errno
import logging
import os
import pickle
import time
from collections import defaultdict, deque
from glob import glob
from subprocess import PIPE, Popen
import fastai
import numpy as np
import pandas as pd
import pydicom
import tensorflow as tf
import tensorflow.keras.backend as K
import torch
import torch.distributed as dist
import torch.utils.data
import torchvision
from fastai.callbacks import csv_logger
from IPython.core.debugger import set_trace
from PIL import Image, ImageFile
from tensorflow.keras import constraints, initializers, regularizers
from tensorflow.keras.layers import Layer
from tensorflow.python.ops import math_ops
from torchvision import transforms
from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
from torchvision.models.detection.mask_rcnn import MaskRCNNPredictor
from tqdm import tqdm
trace_flag = True
def my_trace():
if trace_flag:
set_trace()
ImageFile.LOAD_TRUNCATED_IMAGES = True
class AMQPURL:
class AMQPURL_DEV:
host = "termite.rmq.cloudamqp.com" # (Load balanced)
passwd = "QrBHPPxbsd8IuIxKrCnX3-RGoLKaFhYI"
username = "drdsfaew"
Vhost = "drdsfaew"
# host = "127.0.0.1" # (Load balanced)
# passwd = "guest"
# username = "guest"
# Vhost = "/"
def __init__(
self,
host=AMQPURL_DEV.host,
passwd=AMQPURL_DEV.passwd,
Vhost=AMQPURL_DEV.Vhost,
username=AMQPURL_DEV.username,
):
self.host = host
self.passwd = passwd
self.Vhost = Vhost
self.username = username
def string(self):
Vhost = self.Vhost
if self.Vhost == "/":
Vhost = ""
return f"amqp://{self.username}:{self.passwd}@{self.host}/{Vhost}"
BIN_FOLDER = (
"/content/gdrivedata/My Drive/" if os.path.isdir(
"/content/gdrivedata") else "./"
)
def get_logger(name="utils", level=logging.DEBUG):
FORMAT = "[%(levelname)s]%(asctime)s:%(name)s:%(message)s"
logging.basicConfig(format=FORMAT)
logger = logging.getLogger(name)
logger.setLevel(level)
return logger
logger = get_logger()
def dump_obj(obj, filename, fullpath=False, force=False):
if not fullpath:
path = BIN_FOLDER + filename
else:
path = filename
if not force and os.path.isfile(path):
logger.debug(f"{path} already existed, not dumping")
else:
logger.debug(f"Overwrite {path}!")
with open(path, "wb") as f:
pickle.dump(obj, f)
def get_obj_or_dump(filename, fullpath=False, default=None):
if not fullpath:
path = BIN_FOLDER + filename
else:
path = filename
if os.path.isfile(path):
logger.debug("load " + filename)
with open(path, "rb") as f:
return pickle.load(f)
else:
if default is not None:
logger.debug("dump :" + filename)
dump_obj(default, filename)
return default
def file_exist(filename, fullpath=False):
if not fullpath:
path = BIN_FOLDER + filename
else:
path = filename
return os.path.isfile(path)
# 0.5 means no rebalance
def binary_crossentropy_with_focal_seasoned(
y_true, logit_pred, beta=0.0, gamma=1.0, alpha=0.5, custom_weights_in_Y_true=True
):
"""
:param alpha:weight for positive classes **loss**. default to 1- true
positive cnts / all cnts, alpha range [0,1] for class 1 and 1-alpha
for calss -1. In practiceαmay be set by inverse class freqency or
hyperparameter.
:param custom_weights_in_Y_true:
:return:
"""
balanced = gamma * logit_pred + beta
y_pred = math_ops.sigmoid(balanced)
# only use gamma in this layer, easier to split out factor
return binary_crossentropy_with_focal(
y_true,
y_pred,
gamma=0,
alpha=alpha,
custom_weights_in_Y_true=custom_weights_in_Y_true,
)
# 0.5 means no rebalance
def binary_crossentropy_with_focal(
y_true, y_pred, gamma=1.0, alpha=0.5, custom_weights_in_Y_true=True
):
"""
https://arxiv.org/pdf/1708.02002.pdf
$$ FL(p_t) = -(1-p_t)^{\gamma}log(p_t) $$
$$ p_t=p\: if\: y=1$$
$$ p_t=1-p\: otherwise$$
:param y_true:
:param y_pred:
:param gamma: make easier ones weights down
:param alpha: weight for positive classes. default to 1 - (true
positive cnts / all cnts), alpha range [0,1] for class 1 and 1-alpha
for calss -1. In practice α may be set by inverse class freqency or
hyperparameter.
:return: bce
"""
# assert 0 <= alpha <= 1 and gamma >= 0
# hyper parameters, just use the one for binary?
# alpha = 1. # maybe smaller one can help, as multi-class will make the
# error larger
# gamma = 1.5 # for our problem, try different gamma
# for binary_crossentropy, the implementation is in tensorflow/tensorflow/python/keras/backend.py
# bce = target * alpha* (1-output+epsilon())**gamma * math_ops.log(output + epsilon())
# bce += (1 - target) *(1-alpha)* (output+epsilon())**gamma * math_ops.log(1 - output + epsilon())
# return -bce # binary cross entropy
eps = tf.keras.backend.epsilon()
if custom_weights_in_Y_true:
custom_weights = y_true[:, 1:2]
y_true = y_true[:, :1]
if 1.0 - eps <= gamma <= 1.0 + eps:
bce = alpha * math_ops.multiply(
1.0 - y_pred, math_ops.multiply(y_true, math_ops.log(y_pred + eps))
)
bce += (1 - alpha) * math_ops.multiply(
y_pred, math_ops.multiply(
(1.0 - y_true), math_ops.log(1.0 - y_pred + eps))
)
elif 0.0 - eps <= gamma <= 0.0 + eps:
bce = alpha * math_ops.multiply(y_true, math_ops.log(y_pred + eps))
bce += (1 - alpha) * math_ops.multiply(
(1.0 - y_true), math_ops.log(1.0 - y_pred + eps)
)
else:
gamma_tensor = tf.broadcast_to(
tf.constant(gamma), tf.shape(input=y_pred))
bce = alpha * math_ops.multiply(
math_ops.pow(1.0 - y_pred, gamma_tensor),
math_ops.multiply(y_true, math_ops.log(y_pred + eps)),
)
bce += (1 - alpha) * math_ops.multiply(
math_ops.pow(y_pred, gamma_tensor),
math_ops.multiply(
(1.0 - y_true), math_ops.log(1.0 - y_pred + eps)),
)
if custom_weights_in_Y_true:
return math_ops.multiply(-bce, custom_weights)
else:
return -bce
def reinitLayers(model):
session = K.get_session()
for layer in model.layers:
# if isinstance(layer, keras.engine.topology.Container):
if isinstance(layer, tf.keras.Model):
reinitLayers(layer)
continue
print("LAYER::", layer.name)
if layer.trainable is False:
continue
for v in layer.__dict__:
v_arg = getattr(layer, v)
if hasattr(v_arg, "initializer"):
# not work for layer wrapper, like Bidirectional
initializer_method = getattr(v_arg, "initializer")
initializer_method.run(session=session)
print("reinitializing layer {}.{}".format(layer.name, v))
class AttentionRaffel(Layer):
def __init__(
self,
step_dim,
W_regularizer=None,
b_regularizer=None,
W_constraint=None,
b_constraint=None,
bias=True,
**kwargs,
):
"""
Follows the work of Raffel et al. [https://arxiv.org/abs/1512.08756]
:param step_dim: feature vector length
:param W_regularizer:
:param b_regularizer:
:param W_constraint:
:param b_constraint:
:param bias:
:param kwargs:
"""
super(AttentionRaffel, self).__init__(**kwargs)
self.supports_masking = True
self.init = "glorot_uniform"
self.W_regularizer = regularizers.get(W_regularizer)
self.b_regularizer = regularizers.get(b_regularizer)
self.W_constraint = constraints.get(W_constraint)
self.b_constraint = constraints.get(b_constraint)
self.bias = bias
self.step_dim = step_dim
self.features_dim = 0
def get_config(self):
config = {
"step_dim": self.step_dim,
"bias": self.bias,
"W_regularizer": regularizers.serialize(self.W_regularizer),
"b_regularizer": regularizers.serialize(self.b_regularizer),
"W_constraint": constraints.serialize(self.W_constraint),
"b_constraint": constraints.serialize(self.b_constraint),
}
base_config = super(AttentionRaffel, self).get_config()
if "cell" in base_config:
del base_config["cell"]
return dict(list(base_config.items()) + list(config.items()))
def build(self, input_shape):
# Input shape 3D tensor with shape: `(samples, steps, features)`.
# one step is means one bidirection?
assert len(input_shape) == 3
self.W = self.add_weight(
"{}_W".format(self.name),
(int(input_shape[-1]),),
initializer=self.init,
regularizer=self.W_regularizer,
constraint=self.W_constraint,
)
self.features_dim = input_shape[-1] # features dimention of input
if self.bias:
self.b = self.add_weight(
"{}_b".format(self.name),
(int(input_shape[1]),),
initializer="zero",
regularizer=self.b_regularizer,
constraint=self.b_constraint,
)
else:
self.b = None
self.built = True
def compute_mask(self, inputs, mask=None):
# do not pass the mask to the next layers
return None
def call(self, x, mask=None):
# more like the alignment model, which scores how the inputs around
# position j and the output at position i match
features_dim = self.features_dim
step_dim = self.step_dim
eij = K.reshape(
K.dot(
K.reshape(x, (-1, features_dim)
), K.reshape(self.W, (features_dim, 1))
),
(-1, step_dim),
)
if self.bias:
eij += self.b
eij = K.tanh(eij) # activation
# softmax
a = K.exp(eij)
# apply mask after the exp. will be re-normalized next
if mask is not None:
# Cast the mask to floatX to avoid float64 upcasting in theano
a *= K.cast(mask, K.floatx())
# in some cases especially in the early stages of training the sum may
# be almost zero
a /= K.cast(K.sum(a, axis=1, keepdims=True) + K.epsilon(), K.floatx())
a = K.expand_dims(a)
# context vector c_i (or for this, only one c_i)
weighted_input = x * a
# print weigthted_input.shape
return K.sum(weighted_input, axis=1)
def compute_output_shape(self, input_shape):
# return input_shape[0], input_shape[-1]
return input_shape[0], self.features_dim
class NBatchProgBarLogger(tf.keras.callbacks.ProgbarLogger):
def __init__(
self,
count_mode="samples",
stateful_metrics=None,
display_per_batches=1000,
verbose=1,
early_stop=False,
patience_displays=0,
epsilon=1e-7,
batch_size=1024,
):
super(NBatchProgBarLogger, self).__init__(count_mode, stateful_metrics)
self.display_per_batches = 1 if display_per_batches < 1 else display_per_batches
self.step_idx = 0 # across epochs
self.display_idx = 0 # across epochs
self.verbose = verbose
# better way is subclass EearlyStopping callback.
self.early_stop = early_stop
self.patience_displays = patience_displays
self.losses = np.empty(patience_displays, dtype=np.float32)
self.losses_sum_display = 0
self.epsilon = epsilon
self.stopped_step = 0
self.batch_size = batch_size
def on_train_begin(self, logs=None):
self.epochs = self.params["epochs"]
def on_batch_end(self, batch, logs=None):
logs = logs or {}
batch_size = logs.get("size", 0)
# In case of distribution strategy we can potentially run multiple
# steps at the same time, we should account for that in the `seen`
# calculation.
num_steps = logs.get("num_steps", 1)
if self.use_steps:
self.seen += num_steps
else:
self.seen += batch_size * num_steps
for k in self.params["metrics"]:
if k in logs:
self.log_values.append((k, logs[k]))
self.step_idx += 1
# Skip progbar update for the last batch;
# will be handled by on_epoch_end.
if self.early_stop:
# only record for this batch, not the display. Should work
loss = logs.get("loss")
self.losses_sum_display += loss
if self.step_idx % self.display_per_batches == 0:
if self.verbose and self.seen < self.target:
self.progbar.update(self.seen, self.log_values)
if self.early_stop:
avg_loss_per_display = (
self.losses_sum_display / self.display_per_batches
)
self.losses_sum_display = 0 # clear mannually...
self.losses[
self.display_idx % self.patience_displays
] = avg_loss_per_display
# but it still SGD, variance still, it just smaller by factor of
# display_per_batches
display_info_start_step = self.step_idx - self.display_per_batches + 1
print(
f"\nmean: {avg_loss_per_display}, Step {display_info_start_step }({display_info_start_step*self.batch_size}) to {self.step_idx}({self.step_idx*self.batch_size}) for {self.display_idx}th display step"
)
self.display_idx += 1 # used in index, so +1 later
if self.display_idx >= self.patience_displays:
std = np.std(
self.losses
) # as SGD, always variance, so not a good way, need to learn from early stopping
std_start_step = (
self.step_idx
- self.display_per_batches * self.patience_displays
+ 1
)
print(
f"mean: {np.mean(self.losses)}, std:{std} for Step {std_start_step}({std_start_step*self.batch_size}) to {self.step_idx}({self.step_idx*self.batch_size}) for {self.display_idx}th display steps"
)
if std < self.epsilon:
self.stopped_step = self.step_idx
self.model.stop_training = True
print(
f"Early Stop criterion met: std is {std} at Step {self.step_idx} for {self.display_idx}th display steps"
)
def on_train_end(self, logs=None):
if self.stopped_step > 0 and self.verbose > 0:
print("Step %05d: early stopping" % (self.stopped_step + 1))
class PS_TF_DataHandler:
def __init__(self):
self.fns = None
def to_tf_from_disk(self, fns, df, TARGET_COLUMN, im_height, im_width, im_chan):
self.df = df
self.TARGET_COLUMN = TARGET_COLUMN
self.im_height = im_height
self.im_width = im_width
self.im_chan = im_chan
fns_ds = tf.data.Dataset.from_tensor_slices(fns)
image_ds = fns_ds.map(
self.load_and_preprocess_image(imgPreprocessFlag=False),
num_parallel_calls=2,
)
return image_ds
def load_and_preprocess_image(self, imgPreprocessFlag=True):
def _preprocess_image(img):
raise NotImplementedError()
# hard to do, as read_file, _id.split needs complicate op of tensor,
# easier to first read numpy then save to tfrecord
def _load_and_preprocess_image(path):
X_train = np.zeros(
(self.im_height, self.im_width, self.im_chan), dtype=np.uint8
)
Y_train = np.zeros(
(self.im_height, self.im_width, 1), dtype=np.uint8)
print("Getting train images and masks ... ")
_id = path
# sys.stdout.flush()
# FIXME it cannot be put to autograph!!!
raise RuntimeError("Pydicom read cannot be put to autograph!!!")
dataset = pydicom.read_file(_id)
_id_keystr = _id.split("/")[-1][:-4]
X_train = np.expand_dims(dataset.pixel_array, axis=2)
try:
mask_data = self.df.loc[_id_keystr, self.TARGET_COLUMN]
if "-1" in mask_data:
Y_train = np.zeros((1024, 1024, 1))
else:
if type(mask_data) == str:
Y_train = np.expand_dims(
rle2mask(
self.df.loc[_id_keystr,
self.TARGET_COLUMN], 1024, 1024
),
axis=2,
)
else:
Y_train = np.zeros((1024, 1024, 1))
for x in mask_data:
Y_train = Y_train + np.expand_dims(
rle2mask(x, 1024, 1024), axis=2
)
except KeyError:
print(
f"Key {_id.split('/')[-1][:-4]} without mask, assuming healthy patient."
)
# Assume missing masks are empty masks.
Y_train = np.zeros((1024, 1024, 1))
if imgPreprocessFlag:
return _preprocess_image(X_train), _preprocess_image(Y_train)
return (X_train, Y_train)
return _load_and_preprocess_image
@staticmethod
def maybe_download():
# By default the file at the url origin is downloaded to the cache_dir
# ~/.keras, placed in the cache_subdir datasets, and given the filename
# fname
train_path = tf.keras.utils.get_file(
TRAIN_URL.split("/")[-1], TRAIN_URL)
test_path = tf.keras.utils.get_file(TEST_URL.split("/")[-1], TEST_URL)
return train_path, test_path
@staticmethod
def get_train_dataset(train_X_np, train_Y_np): # join(dataset_dir,'labels.csv')
image_ds = tf.data.Dataset.from_tensor_slices(train_X_np)
image_mask_ds = tf.data.Dataset.from_tensor_slices(train_Y_np)
return tf.data.Dataset.zip((image_ds, image_mask_ds))
@staticmethod
def load_data(train_path, test_path):
"""Returns the iris dataset as (train_x, train_y), (test_x, test_y)."""
# train_path, test_path = maybe_download()
# here the test is really no lable we need to do CV in train part
train_X = pickle.load(open(train_path, "rb")) # (None, 2048)
# (None, 2048) 2048 features from xception net
to_predict_X = pickle.load(open(test_path, "rb"))
try:
labels = pd.read_csv(os.path.join(DATASET_DIR, "labels.csv"))
except FileNotFoundError:
labels = pd.read_csv(os.path.join(DATASET_DIR2, "labels.csv"))
labels = labels["breed"].values.tolist() # for all training data
global SPECIES
SPECIES = sorted(list(set(labels)))
_label_id_map = dict((name, index)
for index, name in enumerate(SPECIES))
train_y = [_label_id_map[label] for label in labels]
return (train_X, train_y), to_predict_X
@staticmethod
def train_input_fn_bt(
features,
labels,
batch_size,
cv,
cv_train=True,
split_id=None,
n_splits=None,
ds=None,
ds_len=-1,
):
# for boost tree, need to prepare feature columns
# 2048? columns, all float
if cv:
return PS_TF_DataHandler._input_fn_bt(
features,
labels,
batch_size,
shuffle=True,
split_id=split_id,
n_splits=n_splits,
cv_train=cv_train,
ds=ds,
ds_len=ds_len,
)
else:
return PS_TF_DataHandler._input_fn_bt(
features, labels, batch_size, shuffle=True, cv=False, ds=ds
)
@staticmethod
def eval_input_fn_bt(
features, labels, batch_size, cv, split_id=None, n_splits=None
):
if cv:
return PS_TF_DataHandler._input_fn_bt(
features,
labels,
batch_size,
with_y=True,
repeat=False,
shuffle=False,
split_id=split_id,
n_splits=n_splits,
cv_train=False,
)
else:
return PS_TF_DataHandler._input_fn_bt(
features,
labels,
batch_size,
with_y=True,
repeat=False,
shuffle=False,
cv=False,
)
@staticmethod
def pred_input_fn_bt(features, batch_size):
return PS_TF_DataHandler._input_fn_bt(
features,
None,
batch_size,
with_y=False,
repeat=False,
shuffle=False,
cv=False,
)
@staticmethod
# for these, we will need to extract all the points before:
def _input_fn_bt(
features,
labels,
batch_size,
with_y=True,
repeat=True,
shuffle=True,
split_id=-1,
n_splits=10,
cv=True,
cv_train=True,
ds=None,
ds_len=-1,
):
if ds is not None:
if shuffle and ds_len <= 0:
raise ValueError("shuffle need to now data length")
data_len = ds_len
else:
data_len = len(labels)
def _to_dict(f):
# first to pandas data frame
df = pd.DataFrame(
f, columns=[str(i) for i in range(features.shape[-1])]
)
return dict(df)
features = _to_dict(features)
if with_y:
ds = tf.data.Dataset.from_tensor_slices((features, labels))
else:
ds = tf.data.Dataset.from_tensor_slices(features)
if cv:
assert split_id >= 0 and n_splits > 1 and split_id < n_splits
if cv_train:
ds = [ds.shard(n_splits, i) for i in range(n_splits)]
shards_cross = [
ds[val_id] for val_id in range(n_splits) if val_id != split_id
]
ds = shards_cross[0]
for t in shards_cross[1:]:
ds = ds.concatenate(t)
if shuffle:
# just memory is not enough ...
ds = ds.shuffle(
buffer_size=int(data_len * (n_splits - 1) / n_splits)
)
else: # cv part for evaluation, no need to shuffle
ds = ds.shard(n_splits, split_id)
else:
if shuffle:
ds = ds.shuffle(buffer_size=data_len)
# after shuffle, we do cross validtation split
# taken from Dan, https://stackoverflow.com/questions/39748660/how-to-perform-k-fold-cross-validation-with-tensorflow
# will need to append id, then remove the id?
# -> no need, we just split to 5 shards, then rearrange these shards
if repeat and cv_train:
ds = ds.repeat()
# Batch the examples
assert batch_size is not None, "batch_size must not be None"
# Return the dataset.
return ds.batch(batch_size).prefetch(1)
@staticmethod
# for these, we will need to extract all the points before:
def train_input_fn(features, labels, batch_size, split_id=-1, n_splits=10, cv=True):
"""An input function for training"""
# read from the tfrecord file (save the extracted ones)(read the data)
ds = tf.data.Dataset.from_tensor_slices((features, labels))
if cv:
assert split_id >= 0 and n_splits > 1 and split_id < n_splits
ds = [ds.shard(n_splits, i) for i in range(n_splits)]
shards_cross = [
ds[val_id] for val_id in range(n_splits) if val_id != split_id
]
s = shards_cross[0]
for t in shards_cross[1:]:
s = s.concatenate(t)
# just memory is not enough ...
ds = s.shuffle(buffer_size=int(
len(labels) * (n_splits - 1) / n_splits))
else:
ds = ds.shuffle(buffer_size=len(labels))
# after shuffle, we do cross validtation split
# taken from Dan, https://stackoverflow.com/questions/39748660/how-to-perform-k-fold-cross-validation-with-tensorflow
# will need to append id, then remove the id?
# -> no need, we just split to 5 shards, then rearrange these shards
ds = ds.repeat()
# Batch the examples
assert batch_size is not None, "batch_size must not be None"
# Return the dataset.
return ds.batch(batch_size).prefetch(1)
@staticmethod
def eval_input_fn(features, labels, batch_size, split_id, n_splits=10):
"""An input function for evaluation or prediction"""
assert split_id >= 0 and n_splits > 1 and split_id < n_splits
if labels is None:
# No labels, use only features.
inputs = features
else:
inputs = (features, labels)
# Convert the inputs to a Dataset.
ds = tf.data.Dataset.from_tensor_slices(inputs)
ds = ds.shard(n_splits, split_id)
# Batch the examples
assert batch_size is not None, "batch_size must not be None"
ds = ds.batch(batch_size)
# Return the dataset.
return ds
@staticmethod
def predict_input_fn(features, batch_size):
"""An input function for evaluation or prediction"""
inputs = features
# Convert the inputs to a Dataset.
ds = tf.data.Dataset.from_tensor_slices(inputs)
# Batch the examples
assert batch_size is not None, "batch_size must not be None"
ds = ds.batch(batch_size)
# Return the dataset.
return ds
@staticmethod
# specific to data structure, need to split out later
def to_tfrecord(ds, file_name="train_dev.tfrec"):
ds = ds.map(lambda a, b: (tf.io.encode_jpeg(a), tf.io.encode_jpeg(b)))
writer = tf.data.experimental.TFRecordWriter(file_name)
writer.write(ds.map(lambda a, b: a))
target_writer = tf.data.experimental.TFRecordWriter(
f"target_{file_name}")
target_writer.write(ds.map(lambda a, b: b))
return
@staticmethod
def from_tfrecord():
def _tf_read_jpeg(wc):
pathes = sorted(glob(wc))
logger.debug(f"recover data from {pathes}")
ds = tf.data.TFRecordDataset(pathes)
ds = ds.map(tf.io.decode_jpeg)
return ds
image_data_wildcard = "train_dev.*.tfrec"
mask_data_wildcard = "target_train_dev.*.tfrec"
return tf.data.Dataset.zip(
(_tf_read_jpeg(image_data_wildcard), _tf_read_jpeg(mask_data_wildcard))
)
@staticmethod
def serialize_PS_example(feature0, feature1):
"""
NOT WORKING... don't know why
Creates a tf.Example message ready to be written to a file.
"""
# Create a dictionary mapping the feature name to the
# tf.Example-compatible data type.
assert feature0.shape[0] == 1 and feature0.shape[1] == 128
assert (
feature0.shape[0] == feature1.shape[0]
and feature0.shape[1] == feature1.shape[1]
)
f0 = tf.reshape(feature0, [-1])
f1 = tf.reshape(feature1, [-1])
feature = {
"image": _int64_feature_from_list(f0),
"mask": _int64_feature_from_list(f1),
}
# Create a Features message using tf.train.Example.
logger.debug("in transforming to tf example proto")
example_proto = tf.train.Example(
features=tf.train.Features(feature=feature))
logger.debug("after transforming one feature to tf example proto")
return example_proto.SerializeToString()
@staticmethod
def tf_serialize_example(f0, f1):
print(PS_TF_DataHandler.serialize_PS_example(f0, f1))
# the return type is
# <a href="..../../versions/r2.0/api_docs/python/tf#string">
# <code>tf.string</code></a>.
tf_string = tf.py_function(
PS_TF_DataHandler.serialize_PS_example,
(f0, f1), # pass these args to the above function.
tf.string,
)
return tf.reshape(tf_string, ()) # The result is a scalar
@staticmethod
def get_generator_with_features(features_dataset):
def generator():
for features in features_dataset:
yield PS_TF_DataHandler.serialize_PS_example(*features)
return generator
# The following functions can be used to convert a value to a type compatible
# with tf.Example.
def _bytes_feature(value):
"""Returns a bytes_list from a string / byte."""
if isinstance(value, type(tf.constant(0))):
value = value.numpy()
# BytesList won't unpack a string from an EagerTensor.
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def _float_feature(value):
"""Returns a float_list from a float / double."""
return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))
def _int64_feature(value):
"""Returns an int64_list from a bool / enum / int / uint."""
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def _int64_feature_from_list(value):
"""Returns an int64_list from a bool / enum / int / uint."""
return tf.train.Feature(int64_list=tf.train.Int64List(value=value))
def set_metrics(self):
"""
set_metrics for model training
:return: None
"""
pass
def set_result_analyzer(self):
pass
def pre_prepare_data_hook(self):
pass
def after_prepare_data_hook(self):
pass
def prepare_train_dev_data(self):
pass
def prepare_test_data(self):
pass
def predict_on_test(self):
pass
def dump_state(self, exec_flag=False, force=True):
logger.debug(f"state {self._stage}")
if exec_flag:
logger.debug(f"dumping state {self._stage}")
dump_obj(self, f"run_state_{self._stage}.pkl", force=force)
# dump_obj(self, 'run_state.pkl', force=True) # too large
def run(
self,
start_stage=None,
end_stage=KernelRunningState.SAVE_SUBMISSION_DONE,
dump_flag=False,
force_dump=True,
):
"""
:param start_stage: if set, will overwrite the stage
:param end_stage:
:param dump_flag:
:return:
"""
self.continue_run(
start_stage=start_stage,
end_stage=end_stage,
dump_flag=dump_flag,
force_dump=force_dump,
)
def continue_run(
self,
start_stage=None,
end_stage=KernelRunningState.SAVE_SUBMISSION_DONE,
dump_flag=False,
force_dump=True,
):
if start_stage is not None:
assert start_stage.value < end_stage.value
self._stage = start_stage
if self._stage.value < KernelRunningState.PREPARE_DATA_DONE.value:
self.pre_prepare_data_hook()
self.prepare_train_dev_data()
self.after_prepare_data_hook()
self._stage = KernelRunningState.PREPARE_DATA_DONE
self.dump_state(exec_flag=dump_flag, force=force_dump)
if self._stage.value >= end_stage.value:
return
if self._stage.value < KernelRunningState.TRAINING_DONE.value:
self.pre_train()
self.build_and_set_model()
self.train_model()
self.after_train()
self.save_model()
self._stage = KernelRunningState.TRAINING_DONE
self.dump_state(exec_flag=dump_flag, force=force_dump)
if self._stage.value >= end_stage.value:
return
if self._stage.value < KernelRunningState.EVL_DEV_DONE.value:
self.set_result_analyzer()
self._stage = KernelRunningState.EVL_DEV_DONE
self.dump_state(exec_flag=False, force=force_dump)
if self._stage.value >= end_stage.value:
return
if self._stage.value < KernelRunningState.SAVE_SUBMISSION_DONE.value:
self.pre_test()
self.prepare_test_data()
self.predict_on_test()
self.after_test()
self._stage = KernelRunningState.SAVE_SUBMISSION_DONE
self.dump_state(exec_flag=False, force=force_dump)
if self._stage.value >= end_stage.value:
return
@classmethod
def _load_state(cls, stage=None, file_name="run_state.pkl"):
"""
:param file_name:
:return: the kernel object, need to continue
"""