-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbreakhis_v2.py
More file actions
2450 lines (1922 loc) · 92.9 KB
/
breakhis_v2.py
File metadata and controls
2450 lines (1922 loc) · 92.9 KB
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
# -*- coding: utf-8 -*-
"""Breakhis v2
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/17dqHsfx5kp81I9rVdmts3T1HUOjr9oZ2
"""
from google.colab import drive
drive.mount('/content/drive')
# Commented out IPython magic to ensure Python compatibility.
# %tensorflow_version 2.x
import tensorflow as tf
import tensorflow_addons as tfa
import numpy as np
import os
import matplotlib.pyplot as plt
from pickle import load, dump
import sys
import functools
"""# Load Data"""
# patient ids/class e.g. ids[0] : patients with Adenosis
path = 'drive/My Drive/TFRecord_ds/'
ids = load(open(path+'ids.pkl','rb') )
zoom_ids = load(open(path+'zoom_ids.pkl','rb'))
raw_image_dataset = tf.data.TFRecordDataset(tf.data.Dataset.list_files("drive/My\ Drive/TFRecord_ds/img_*.tfrecord",shuffle=False))
image_feature_description = {
'id' : tf.io.FixedLenFeature([], tf.string),
'zoom' : tf.io.FixedLenFeature([], tf.int64),
'label': tf.io.FixedLenFeature([], tf.int64),
'image_raw': tf.io.FixedLenFeature([], tf.string),
}
def _parse_image_function(example_proto):
# Parse the input tf.Example proto using the dictionary above.
return tf.io.parse_single_example(example_proto, image_feature_description)
parsed_image_dataset = raw_image_dataset.map(_parse_image_function)
"""# Split Dataset"""
from random import shuffle, seed
from collections import defaultdict
seed(10)
other_paper = True
set_indices = []
paper_indices = [(),(),(),(),(21,6,11),(),(2,1,2),()]
def filter_patient_fn(img_dict,id_set,i):
return tf.math.logical_and(
tf.math.equal(img_dict['label'], tf.constant(i, dtype='int64')),
tf.reduce_any(tf.convert_to_tensor(list(map(lambda id: tf.math.equal(img_dict['id'], id),id_set)))))
def split_dataset(ds, train_ratio, val_ratio, ids):
splitted_ids = {k:[] for k in ['Val', 'Train', 'Test']}
num_of_patients = defaultdict(int)
# split every class patientwise
for i,id_set in enumerate(ids):
id_set = list(sorted(id_set))
shuffle(id_set)
ids_size = len(id_set)
if (i!=4 and i!=6) or other_paper:
test_idx = round(train_ratio*ids_size)
# keep at least 1 patient for testing
test_idx = test_idx-1 if test_idx==ids_size else test_idx
val_idx = round(val_ratio*test_idx)
# keep at least 1 patient for validation
val_idx = 1 if not val_idx else val_idx
else :
test_idx = ids_size - paper_indices[i][2]
val_idx = paper_indices[i][1]
splitted_ids['Val'].append(id_set[:val_idx])
splitted_ids['Train'].append(id_set[val_idx:test_idx])
splitted_ids['Test'].append(id_set[test_idx:])
num_of_patients['Val']+=val_idx
num_of_patients['Train']+=test_idx-val_idx
num_of_patients['Test']+=ids_size-test_idx
set_indices.append(( test_idx-val_idx,val_idx,ids_size-test_idx))
dss = {k : tuple(ds.filter(lambda im: filter_patient_fn(im, set_ids, i))
for i,set_ids in enumerate(ids_lists)) for k,ids_lists in splitted_ids.items() }
ret = {k: functools.reduce(lambda a, b: a.concatenate(b), d).map(lambda img_dict : (img_dict['image_raw'], img_dict['label'])) for k, d in dss.items()}
return ret,num_of_patients
"""## Split per zoom"""
train_zoom, val_zoom, test_zoom = {}, {}, {}
train_zoom_sz, val_zoom_sz, test_zoom_sz = {}, {}, {}
for k in zoom_ids.keys():
ds_at_zoom = parsed_image_dataset.filter(lambda img : tf.math.equal(img['zoom'], tf.constant(k, dtype='int64')))
splitted = split_dataset(ds_at_zoom, 0.9, 1/9, ids)
dss = splitted[0]
num_of_patients = splitted[1]
train_zoom[k], val_zoom[k], test_zoom[k] = dss['Train'], dss['Val'], dss['Test']
train_zoom_sz[k],val_zoom_sz[k],test_zoom_sz[k] = num_of_patients['Train'], num_of_patients['Val'], num_of_patients['Test']
zoom = 40
train_parsed, val_parsed , test_parsed = train_zoom[zoom], val_zoom[zoom], test_zoom[zoom]
train_patients, val_patients, test_patients = train_zoom_sz[zoom], val_zoom_sz[zoom], test_zoom_sz[zoom]
print(train_patients, val_patients, test_patients)
"""## Whole Dataset Split"""
splitted = split_dataset(parsed_image_dataset, 0.8, 0.1, ids)
train_parsed, val_parsed , test_parsed = splitted[0]['Train'], splitted[0]['Val'], splitted[0]['Test']
train_patients, val_patients , test_patients = splitted[1]['Train'], splitted[1]['Val'], splitted[1]['Test']
"""# Count"""
# Save time ^^
distribution = defaultdict(list)
#0.9, 1/9, zoom 40
distribution['Train'] = [70, 204, 58, 121, 674, 161, 83, 91]
distribution['Val'] = [15, 23, 13, 16, 92, 29, 51, 19]
distribution['Test'] = [29, 26, 38, 12, 98, 15, 22, 35]
# 0.8, .15 whole ds
# distribution['Train'] = [262, 665, 235, 452, 2491, 554, 351, 353]
# distribution['Val'] = [61, 81, 60, 64, 234, 119, 201, 65]
# distribution['Test'] = [121, 268, 158, 53, 726, 119, 74, 142]
# attention by selection distr
# distribution['Train'] = [70, 123, 58, 105, 459, 73, 63, 61]
# distribution['Val'] = [15, 37, 13, 16, 167, 75, 51, 19]
# distribution['Test'] = [29, 93, 38, 28, 238, 57, 42, 65]
# my distrib
# distribution['Train'] = [70, 123, 58, 105, 436, 73, 83, 61]
# distribution['Val'] = [15, 37, 13, 16, 190, 75, 51, 19]
# distribution['Test'] = [29, 93, 38, 28, 238, 57, 22, 65]
distribution = defaultdict(list)
for distr, parsed in zip(['Train', 'Val', 'Test'], [train_parsed, val_parsed, test_parsed]):
distribution[distr] = [0 for i in range(8)]
for _,l in parsed.as_numpy_iterator():
distribution[distr][l]+=1
print(f"{distr}: {distribution[distr]}")
train_size = sum(distribution['Train'])
val_size = sum(distribution['Val'])
test_size = sum(distribution['Test'])
# size of train, validation and test set respectively
train_size, val_size, test_size
max_num_of_instances = max(distribution['Train'])
number_of_transformations = [ max_num_of_instances//v - 1 for v in distribution['Train']]
# number of transformations needed per class to improve balance
number_of_transformations
"""# Preprocess"""
# run for no augmentation
number_of_transformations = [2 for i in range(8)]
AUTOTUNE = tf.data.experimental.AUTOTUNE # https://www.tensorflow.org/guide/data_performance
batch_size = 32
image_size = 224
from random import randint, random, choice
# def rotate_image(image, deg):
# image = tfa.image.rotate(image,tf.constant(deg*1.))
# return image
# def random_rotate(x):
# image = tfa.image.rotate(x,tf.constant(randint(0, 359)*1.))
# return image
def apply_rnd_transformation(x, transformations):
f = choice(transformations)
return f[0](*((x,)+f[1:])) if random()<.5 else x
def augment_data(ds,transformations):
augmented = [ds.map(lambda x,l: ( f[0](*((x,)+f[1:])), l) ) for f in transformations ]
ret = functools.reduce(lambda a, b: a.concatenate(b), [ds]+augmented)
return ret
# for a set of transformation functions f1,f2,..,fn return fn(...(f2(f1(x))))
def fold_transformations(ds, transformations):
return ds.map(lambda x,l: ((functools.reduce(lambda i,f: f[0](*((i,)+f[1:])), transformations, x)), l))
def apply_rnd_transformations(ds, transformations):
return ds.map(lambda x,l: ( apply_rnd_transformation(x,transformations), l))
def mean_stdev(ds, size):
means = np.zeros((3))
sum_tmp = np.zeros((3))
for im,l in ds.as_numpy_iterator():
for i in range(3):
means[i]+=tf.reduce_mean(im[:,:,i])
means = (batch_size * means)/(size)
for im,l in ds.as_numpy_iterator():
for i in range(3):
sum_tmp[i]+=(tf.reduce_sum((im[:,:,i] - means[i])**2))
stdevs = np.sqrt(sum_tmp / (size * 224 *224 ))
means = np.repeat([np.repeat([means], 224, axis=0)], 224,axis=0)
stdevs = np.repeat([np.repeat([stdevs], 224, axis=0)], 224,axis=0)
return means, stdevs
from operator import truediv
def _input_fn(ds):
return ds.repeat().batch(batch_size).prefetch(buffer_size=AUTOTUNE)
preprocess = [(tf.io.decode_jpeg, 3 ), (tf.image.resize, [image_size, image_size]),
# (tf.image.resize_with_crop_or_pad, 460, 700), # not all images have exactly the same dimensions
( tf.cast, tf.float32),
(tf.image.per_image_standardization, )
# (truediv, 255.)
]
val ,test = [ fold_transformations(ds, preprocess).cache()
for ds in [val_parsed , test_parsed ]
]
train_per_class = [ train_parsed.filter(lambda _,l : tf.math.equal(l, tf.constant(i, 'int64'))) for i in range(8)]
train_per_class = [ fold_transformations(ds, preprocess).cache() for ds in train_per_class]
aug_size=train_size
if any(number_of_transformations):
transformations = [
(tf.image.flip_up_down, ), (tf.image.flip_left_right,) , (tf.image.rot90, 1) , (tf.image.rot90, 3),
(tf.image.rot90, 2) ]#,(tf.image.random_brightness, 0.3), (tf.roll, [round(image_size*.2) for _ in range(3)], [0,1,2]) ,
# (tf.image.random_contrast,.2, .5), (tf.image.adjust_saturation, .5) ]
rnd_transformations = [(tf.image.random_flip_up_down, 42 ), (tf.image.random_flip_left_right, 42) ] #,(tf.image.random_brightness, 0.3, 42), (tf.image.random_contrast,.2, .5, 42)]
ds_size = min( d*(min(t, len(transformations))+1) for d,t in zip(distribution['Train'], number_of_transformations))
aug_train_per_class = [ augment_data(ds, transformations[:min(n, len(transformations))]) for ds,n in zip(train_per_class, number_of_transformations)]
# aug_train_per_class[4] = apply_rnd_transformations(aug_train_per_class[4], transformations) # apply random transformations to majority class
aug_distribution = [d*(min(t, len(transformations))+1) for d,t in zip(distribution['Train'], number_of_transformations)]
aug_size = sum(aug_distribution)
# train set too large to fit in shuffle buffer, use sampling to randomly shuffle
train_concatenated = tf.data.experimental.sample_from_datasets(train_per_class, [.125]*8, seed=10)
# train_concatenated = tf.data.experimental.sample_from_datasets(train_per_class, \
# [(train_size - d)/(train_size*(train_size-1)) for d in distribution['Train']],
# seed=10)
# GCN
# means_stdevs = [ mean_stdev(functools.reduce(lambda a, b: a.concatenate(b), [train_concatenated, val, test]), sum([aug_size, val_size, test_size]))]*3
# sets = [ st.map(lambda im,l: ((im-mu)/sg,l)) for st,(mu,sg) in zip([train_concatenated, val, test], means_stdevs)]
# train_concatenated, val, test = sets
train_ds = _input_fn(train_concatenated.shuffle(aug_size, seed=10))
val_ds = _input_fn(val.shuffle(val_size, seed=10))
test_ds = _input_fn(test.shuffle(test_size, seed=10))
steps = aug_size//batch_size
# class_weight = {k : (1/distr)*(aug_size/8.) for k,distr in enumerate(aug_distribution) } # argument to pass as class weight in fit()
# class_weight = {k : (1/ (distr*7))*(aug_size)/8. for k,(distr, tr) in enumerate(zip(distribution['Train'], number_of_transformations)) }
print(aug_size)
print(steps)
val_ds = _input_fn(val.concatenate(test).shuffle(val_size+test_size, seed=10))
"""# Model
## Resnet18
"""
!pip install torch torchvision
!pip install onnx onnxruntime
!pip install fire
!git clone https://github.com/AxisCommunications/onnx-to-keras.git
import torchvision.models as models
import torch
resnet18 = models.resnet18(pretrained=True)
dummy_input = torch.randn(32, 3, 224, 224)
torch.onnx.export(resnet18, dummy_input, "resnet18.onnx")
!python3 onnx-to-keras/onnx2keras.py resnet18.onnx resnet18.h5
import re
from keras.models import Model
def insert_layer_nonseq(model, layer_regex, insert_layer_factory,
insert_layer_name=None, position='after'):
# Auxiliary dictionary to describe the network graph
network_dict = {'input_layers_of': {}, 'new_output_tensor_of': {}}
# Set the input layers of each layer
for layer in model.layers:
for node in layer._outbound_nodes:
layer_name = node.outbound_layer.name
if layer_name not in network_dict['input_layers_of']:
network_dict['input_layers_of'].update(
{layer_name: [layer.name]})
else:
# network_dict['input_layers_of'][layer_name].append(layer.name)
if (layer.name not in network_dict['input_layers_of'][layer_name]):
network_dict['input_layers_of'][layer_name].append(layer.name)
# Set the output tensor of the input layer
network_dict['new_output_tensor_of'].update(
{model.layers[0]._name: model.input})
# Iterate over all layers after the input
model_outputs = []
for layer in model.layers[1:]:
# Determine input tensors
layer_input = [network_dict['new_output_tensor_of'][layer_aux]
for layer_aux in network_dict['input_layers_of'][layer.name]]
if len(layer_input) == 1:
layer_input = layer_input[0]
# Insert layer if name matches the regular expression
if re.match(layer_regex, layer.name):
if position == 'replace':
x = layer_input
elif position == 'after':
x = layer(layer_input)
elif position == 'before':
pass
else:
raise ValueError('position must be: before, after or replace')
new_layer = insert_layer_factory()
if insert_layer_name:
new_layer.name = insert_layer_name
else:
new_layer._name = '{}_{}'.format(layer.name, new_layer.name)
x = new_layer(x)
print('New layer: {} Old layer: {} Type: {}'.format(new_layer.name,
layer.name, position))
if position == 'before':
x = layer(x)
else:
x = layer(layer_input)
# Set new output tensor (the original one, or the one of the inserted
# layer)
network_dict['new_output_tensor_of'].update({layer._name: x})
# Save tensor in output list if it is output in initial model
if layer_name in model.output_names:
model_outputs.append(x)
return Model(inputs=model.inputs, outputs=model_outputs)
from tensorflow.keras.layers import *
tf.keras.backend.clear_session()
mod = tf.keras.models.load_model('resnet18.h5')
t_model = tf.keras.Model(inputs=mod.input, outputs=mod.get_layer(index=-4).output)
t_model.trainable=False
for layer in t_model.layers[56:]:
layer.trainable = True
# model.summary()
x = t_model.output
# x = Dropout(.5)(x)
x = GlobalAveragePooling2D()(x)
x = Dropout(.5)(x)
x = Dense(128)(x)
# x = Dropout(.5)(x)
x = Dense(8)(x)
out = Softmax()(x)
hope_model = tf.keras.Model(inputs=t_model.input, outputs=out)
conf = defaultdict()
for layer in hope_model.layers[56:]:
conf = layer.get_config()
if 'kernel_regularizer' in conf:
layer.kernel_regularizer = tf.keras.regularizers.l2(0.001)
# hope_model.summary()
def dropout_layer_factory():
return Dropout(rate=0.5)
drop = insert_layer_nonseq(hope_model, '.*conv2d.*', dropout_layer_factory)
tmp = Model(inputs=drop.input, outputs=drop.output[-1])
tmp.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.001, momentum=0.9), loss='sparse_categorical_crossentropy', metrics='acc')
history = tmp.fit(train_ds, epochs=5, shuffle=False, steps_per_epoch=steps,validation_data=val_ds, validation_steps=(val_size+test_size)//batch_size )
history = hope_model.fit(train_ds, epochs=20, initial_epoch=5, steps_per_epoch=steps,validation_data=val_ds, validation_steps=(val_size+test_size)//batch_size )
history = hope_model.fit(train_ds, epochs=40, initial_epoch=20,steps_per_epoch=steps,validation_data=val_ds, validation_steps=(val_size+test_size)//batch_size )
"""## Any pretrained"""
def model(pretrained, size, no_fine_tune=True, freeze=1 ):
PRETRAINED_MODEL=pretrained(input_shape=(size, size,3), include_top=False, weights='imagenet')
PRETRAINED_MODEL.trainable = True
if no_fine_tune:
for layer in PRETRAINED_MODEL.layers[:round(len(PRETRAINED_MODEL.layers)*freeze)]:
layer.trainable = False
dropout_layer1 = tf.keras.layers.Dropout(0.5)
dense_layer2 = tf.keras.layers.Dense(512,activation='relu')
dropout_layer3 = tf.keras.layers.Dropout(0.5)
dense_layer1 = tf.keras.layers.Dense(256,kernel_regularizer=tf.keras.regularizers.l1(0.0001),activation='relu')
dropout_layer2 = tf.keras.layers.Dropout(0.5)
dense_layer3 = tf.keras.layers.Dense(128,activation='relu')
dropout_layer4 = tf.keras.layers.Dropout(0.5)
dense_layer4 = tf.keras.layers.Dense(16,kernel_regularizer=tf.keras.regularizers.l2(),activation='relu')
global_max_layer = tf.keras.layers.GlobalMaxPooling2D()
global_avg_layer = tf.keras.layers.GlobalAveragePooling2D()
batch_norm = tf.keras.layers.BatchNormalization()
batch_norm1 = tf.keras.layers.BatchNormalization()
flatten = tf.keras.layers.Flatten()
# add top layer classification
prediction_layer = tf.keras.layers.Dense(8,activation='softmax')
# prediction_layer = tf.keras.layers.Dense(4, activation='linear', kernel_regularizer=tf.keras.regularizers.l2())
dropout_layer2 = tf.keras.layers.Dropout(0.5)
model = tf.keras.Sequential([PRETRAINED_MODEL,global_avg_layer, dropout_layer1, batch_norm, dense_layer1, batch_norm1,dropout_layer2,dense_layer3 ,prediction_layer ])
# model = tf.keras.Sequential([PRETRAINED_MODEL,global_avg_layer, dropout_layer1,batch_norm,dense_layer1,dropout_layer2,batch_norm1,dense_layer3,dropout_layer4,prediction_layer ])
lr = tf.keras.optimizers.schedules.ExponentialDecay(initial_learning_rate=0.0002, decay_rate=0.7, decay_steps=2)
model.compile(optimizer=tf.optimizers.RMSprop(learning_rate=0.00001, decay=.000001), loss=tf.keras.losses.sparse_categorical_crossentropy , metrics=["accuracy"])
model.summary()
return model
the_model = model(tf.keras.applications.VGG16,image_size, freeze=.5)
learn_control = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_accuracy', patience=5,
verbose=1,factor=0.2, min_lr=1e-7)
history = the_model.fit(train_ds, epochs=10,steps_per_epoch=steps, validation_data=val_ds, validation_steps=val_size// batch_size, class_weight=class_weight)
history = the_model.fit(train_ds, epochs=20,initial_epoch=10,steps_per_epoch=steps, validation_data=val_ds, validation_steps=val_size// batch_size, class_weight=class_weight)
"""# Attention Model"""
from tensorflow.keras import Model
from tensorflow.keras.layers import *
import tensorflow as tf
import numpy as np
"""## Metrics"""
class ImageLevelAccuracy(tf.keras.metrics.Metric):
def __init__(self, name='image_level_accuracy', **kwargs):
super(ImageLevelAccuracy, self).__init__(name=name, **kwargs)
self.n_rec= self.add_weight(name='n_rec', initializer='zeros')
self.cnt = self.add_weight(name='count', initializer='zeros')
self.majority = 3*tf.ones(4, dtype='int64')
def update_state(self, y_true, y_pred, sample_weight=None):
y_pred = tf.reshape(tf.argmax(y_pred, axis=1), [4,5])
y_true = tf.reshape(y_true,[4,5])
v = tf.equal(y_true, y_pred)
# count correctly classified patches
v = tf.math.reduce_sum(tf.cast(v, dtype='int64'), axis=1)
# count correctly classified images TODO: if no majority in training should we select more patches?
v = tf.reduce_sum(tf.cast(tf.greater_equal(v,self.majority), 'int64'))
self.n_rec.assign_add(tf.cast(v,'float32'))
self.cnt.assign_add(tf.cast(4, dtype='float32'))
def result(self):
return self.n_rec/self.cnt
def reset_states(self):
self.n_rec.assign(0)
self.cnt.assign(0)
my_acc = ImageLevelAccuracy()
"""## Soft Attention"""
def image_cross_entropy_loss(y_true, y_pred):
class_prob = tf.reduce_sum(tf.one_hot(tf.squeeze(y_true), depth=8)*y_pred, axis=1)
y_image = tf.reduce_mean(tf.reshape(class_prob, (4,5)), axis=1)
return -tf.reduce_mean(tf.math.log(y_image))
def residual_unit(inp, patch_size=112, is_first=False):
# A
a = Conv2D(input_shape=(patch_size, patch_size, 3),filters=64, kernel_size=1)(inp) if is_first else Conv2D(filters=64, kernel_size=1)(inp)
a = BatchNormalization( momentum=.1, epsilon=1e-5)(a) # PyTorch different default values
# B
# b = Conv2D(input_shape=(patch_size+2, patch_size+2, 3),filters=64, kernel_size=1)(inp) if is_first else Conv2D(filters=64, kernel_size=1)(inp) # when zero padding at beginning
b = Conv2D(filters=64, kernel_size=1)(inp)
b = BatchNormalization(momentum=.1, epsilon=1e-5)(b) # PyTorch different default values
b = PReLU(shared_axes=[1,2], alpha_initializer=tf.keras.initializers.constant(.25))(b)
b = Conv2D(filters=64, kernel_size=3)(b)
b = BatchNormalization(momentum=.1, epsilon=1e-5)(b) # PyTorch different default values
b = PReLU(shared_axes=[1,2], alpha_initializer=tf.keras.initializers.constant(.25))(b)
b = Conv2D(filters=64, kernel_size=1)(b)
b = BatchNormalization(momentum=.1, epsilon=1e-5)(b)
# a = patch_size X patch_size, b = (patch_size-2) X (patch_size-2)
b = ZeroPadding2D()(b)
output = Add()([a,b])
return output
patch = Input(shape=( 112,112,3),name='input' )
# SaNet
# let patch be the patch
ru = residual_unit(patch, True)
# Trunk
t = residual_unit(ru)
t = residual_unit(t)
# Mask
m = MaxPool2D(pool_size=3, strides=2)(ru)
m = residual_unit(m)
m = residual_unit(m)
m = MaxPool2D(pool_size=3, strides=2)(m)
m = residual_unit(m)
m = residual_unit(m)
m = UpSampling2D(interpolation='bilinear')(m)
m = BatchNormalization(momentum=.1, epsilon=1e-5)(m)
m = PReLU(shared_axes=[1,2], alpha_initializer=tf.keras.initializers.constant(.25))(m)
m = residual_unit(m)
m = ZeroPadding2D()(m)
m = UpSampling2D(interpolation='bilinear')(m)
m = BatchNormalization(momentum=.1, epsilon=1e-5)(m)
m = PReLU(shared_axes=[1,2], alpha_initializer=tf.keras.initializers.constant(.25))(m)
m = residual_unit(m)
m = Conv2D(filters=64, kernel_size=1, activation='sigmoid')(m)
x = Multiply()([m+1, t])
# x = AveragePooling2D(pool_size=5, strides=1)(x)
x = GlobalAveragePooling2D()(x)
# x = Flatten()(x)
deep = Dense(128, activation='relu', name='feature_layer')(x)
output = Dense(8)(deep)
output = Softmax()(output)
lr = tf.keras.optimizers.schedules.ExponentialDecay(initial_learning_rate=.01, decay_steps=steps*200, decay_rate=.95)
SA_Net = Model(inputs=patch, outputs=output)
SA_Net.compile(optimizer=tf.keras.optimizers.Adam(), loss=image_cross_entropy_loss, metrics=[my_acc])
# SA_Net.summary()
SA_Net_features = Model(inputs=SA_Net.input, outputs=SA_Net.get_layer('feature_layer').output)
np.random.seed(12)
train_iter = train_ds.as_numpy_iterator()
val_iter = val_ds.as_numpy_iterator()
"""### Testing area"""
batch = val_iter.next()
# positions = np.hstack([(np.random.randint(66,394, size=(4,5))/460.) , (np.random.randint(66,634, size=(4,5))/700.)]).reshape(4,5,2)
# shallow = np.zeros((4,5,7))
# shallows=[]
# deeps=[]
# avg_loss = 0.
# avg_acc = 0.
# guard=0
# cropped = [0]*4
# l = []
# b = []
# show_batch(batch, positions)
# exctract 5 patches for each image in mini batch
glmps = [tf.image.extract_glimpse(batch[0], size=(112,112), offsets=positions[:, i, :], centered=True, normalized=True) for i in range(5)]
glmps = tf.concat(glmps, 0)
labels = np.vstack([batch[1]]*5 ).T
# 6 loc_x, 7 loc_y
# shallow[:,:,5] = positions[:,:,0]
# shallow[:,:,6] = positions[:,:,1]
# # 3 pred, 4 true pred
# shallow[:,:,3] = np.argmax(SA_Net.predict_on_batch(glmps), axis=1).reshape(4,5)
# shallow[:,:,4] = labels
# shallow_t = tf.convert_to_tensor(shallow, dtype=tf.float64)
# deep = tf.reshape(SA_Net_features(glmps), (4,5,128))
# if not val:
# deeps.append(deep)
# shallows.append(shallow_t)
# out = [o.numpy() for o in De_Net([deep,shallow_t ]) ]
# patches = glmps.numpy().reshape(4,5, 112,112,3)
# # print(positions[0])
# # iterate over DeNet actions, select patches and update centers for next iter
# for i,im in enumerate(out[1]):
# for j,o in enumerate(im):
# if (out[0][i][j][0]>=out[0][i][j][1] and cropped[i] <5) or (guard>15 and cropped[i]<5):
# b.append(patches[i][j])
# l.append(labels[i][j])
# cropped[i]+=1
# # avoid croping the edges
# y = np.clip(o[0]+np.random.normal(scale=20/460), 0,1) *164/230
# x = np.clip(o[1]+np.random.normal(scale=20/700), 0, 1)*294/350
# positions[i,j,:] = [y , x ]
# # out = SA_Net.train_on_batch(glmps.numpy, labels)
step = 0
historical_loss = 0
len(l)
loss, acc = SA_Net.train_on_batch(np.array(b[:20]), np.array(l[:20]))
avg_loss = ( avg_loss*step + loss) / (step + 1) # running average
avg_acc = (avg_acc*step + acc) / (step + 1)
# (historical?) training loss, state feauture 0
historical_loss = ( historical_loss*(step + shallow[0][0][2] ) + loss) / (step + shallow[0][0][2] + 1)
shallow[:,:,0] = np.array([historical_loss]*20).reshape(4,5)
shallow[:,:,2] = np.array([shallow[0][0][2]+1]*20).reshape(4,5) # num of iterations (epochs?)
l , acc = SA_Net.train_on_batch(glmps.numpy(), labels.reshape(20), )
l, acc
features = SA_Net.evaluate(glmps.numpy(), labels.reshape(20), batch_size=20, steps=1)
features
tmp = tf.reduce_sum(tf.one_hot(tf.convert_to_tensor(labels.reshape(20), dtype='int64'),8)*pred, axis=-1) #, tf.math.reduce_max(pred, 1)
tf.reshape(tmp, [4,5])
# -tf.reduce_mean(tf.math.log(tmp))
outputs[0]
SA_Net.get_layer(index=4).get_weights()
tf.reduce_max(SA_Net_features(glmps), axis=-1)
my_acc.update_state(tf.convert_to_tensor(labels.reshape(20), dtype='int64'), tf.convert_to_tensor(pred, 'float64'))
my_acc.result()
pred = SA_Net.predict(glmps.numpy() )
tf.argmax(pred, axis=1).numpy().reshape(4,5), labels
tf.reduce_mean(tf.keras.backend.sparse_categorical_crossentropy(labels.reshape(20),pred))
positions
def show_batch(batch, positions):
bboxes = np.apply_along_axis( lambda y : [(y[0]+1)/2-66/460, (y[1]+1)/2-66/700, (y[0]+1)/2+66/460, (y[1]+1)/2+66/700], axis=-1, arr=positions )
# bboxes = np.apply_along_axis( lambda y : [y[0]-66/460, y[1]-66/700, y[0]+66/460, y[1]+66/700], axis=-1, arr=positions )
colors = np.array([[0.2, 0.0, 1.0]])
to_show = tf.image.draw_bounding_boxes(batch[0], bboxes, colors)
f, axarr = plt.subplots(2,2, figsize=(20,10))
for i, im in enumerate(tf.unstack(to_show)):
axarr[i//2,i % 2].imshow(im)
# show_batch(batch, positions)
"""## Hard Attention"""
# DeNet
deep = Input(shape=(5,128), batch_size=4, name='deep_input')
other_state = Input(shape=(5, 7), batch_size=4,name='other_input' )
norm_other_state = Lambda(lambda x:tf.keras.backend.l2_normalize(x,axis=2))(other_state)
norm_deep = Lambda(lambda x: tf.keras.backend.l2_normalize(x, axis=2))(deep)
fc_relu = Dense(32, activation='relu',kernel_initializer=tf.keras.initializers.RandomUniform(minval=-0.01, maxval=0.01))(deep)
fc_tanh = Dense(10, activation='tanh',kernel_initializer=tf.keras.initializers.RandomUniform(minval=-0.01, maxval=0.01))(other_state)
fc = Concatenate()([fc_tanh, fc_relu])
lstm_input = Dense(24,activation='relu',kernel_initializer=tf.keras.initializers.RandomUniform(minval=-0.01, maxval=0.01))(fc)
lstm_output = LSTM(24, return_sequences=True)(lstm_input)
action = Dense(2, activation='sigmoid',kernel_initializer=tf.keras.initializers.RandomUniform(minval=-0.01, maxval=0.01), \
bias_initializer=tf.keras.initializers.Constant(2))(lstm_output)
loc_y = Dense(2, activation='sigmoid', kernel_initializer=tf.keras.initializers.RandomUniform(minval=-0.01, maxval=0.01))(lstm_output)
loc_x = Dense(2, activation='sigmoid', kernel_initializer=tf.keras.initializers.RandomUniform(minval=-0.01, maxval=0.01))(lstm_output)
# crop = LSTM(2)(lstm_input)
# loc_y = LSTM(236)(lstm_input)
# loc_x = LSTM(476)(lstm_input)
De_Net = Model(inputs=[deep, other_state], outputs = [action, loc_y, loc_x] )
# De_Net = Model(inputs=[deep, other_state], outputs = [action, loc] )
# De_Net = Model(inputs=[deep, other_state], outputs = [lstm_output] )
De_Net.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001) )
# De_Net.summary()
"""## Experiments
### B2B 1 val 1 train
"""
def step_fn(batch, step, avg_loss=0., avg_acc=0., val=False):
global positions, shallow
cropped = 0
guard = 0 # prevent infinite loop while cropping
b = np.empty((4,5,112,112,3)) # batch for SaNet
l = np.zeros((4,5), dtype='int')-1 # labels of batch for SaNet
ds_per_ep = []
shs_per_ep = []
actions_per_ep = []
actions_ts = np.zeros((3,4,5))
while cropped<20:
guard+=1
# exctract 5 patches for each image in mini batch
glmps = [tf.image.extract_glimpse(batch[0], size=(112,112), offsets=positions[:, i, :], centered=True, normalized=True) for i in range(5)]
glmps = tf.concat(glmps, 0)
labels = np.vstack([batch[1] for _ in range(5)]).T
# 6 loc_x, 7 loc_y
shallow[:,:,5] = positions[:,:,0]
shallow[:,:,6] = positions[:,:,1]
# 3 pred, 4 true pred
shallow[:,:,3] = np.argmax(SA_Net.predict_on_batch(glmps), axis=1).reshape(4,5)
shallow[:,:,4] = labels
shallow_t = tf.convert_to_tensor(shallow, dtype=tf.float64)
deep = tf.reshape(SA_Net_features(glmps), (4,5,128))
if not val:
ds_per_ep.append(deep)
shs_per_ep.append(shallow_t)
out = [o.numpy() for o in De_Net([deep,shallow_t ]) ]
patches = glmps.numpy().reshape(4,5, 112,112,3)
# iterate over DeNet actions, select patches and update centers for next iter
for i,im in enumerate(out[1]):
for j,o in enumerate(im):
actions_ts[0][i][j] = 0
if l[i][j]==-1 and (out[0][i][j][1]-out[0][i][j][0]>=0. or guard>10 ):
b[i][j] = patches[i][j]
l[i][j] = labels[i][j]
cropped+=1
actions_ts[0][i][j] = 1
# avoid croping the edges
y = np.clip(tf.random.normal([1], mean=o[0], stddev=o[1], dtype=tf.float32), 66/230 -1, 394/230-1) #*164/230
x = np.clip(tf.random.normal([1], mean=out[2][i][j][0], stddev=out[2][i][j][1], dtype=tf.float32), 66/350-1,634/350-1) #*294/350
positions[i,j,:] = [y , x ]
actions_ts[1][i][j] = y
actions_ts[2][i][j] = x
if not val:
actions_per_ep.append(actions_ts)
loss, acc = SA_Net.train_on_batch(b.reshape((20,112,112,3)), l.reshape((20,1)) ) if not val else SA_Net.test_on_batch(b.reshape((20,112,112,3)), l.reshape((20,1)) )
avg_loss = ( avg_loss*step + loss) / (step + 1) # running average
avg_acc = (avg_acc*step + acc) / (step + 1)
if not val:
return avg_loss, avg_acc, actions_per_ep, ds_per_ep, shs_per_ep, loss
else:
return avg_loss, avg_acc, guard
def train_epoch( historical_loss=0.):
global positions, shallow
avg_loss = 0.
avg_acc = 0.
deeps = []
shallows = []
actions = []
rews = []
for step in range(train_size//4):
batch = train_iter.next()
avg_loss, avg_acc, actions_per_ep, ds_per_ep, shs_per_ep, loss = step_fn(batch, step, avg_loss, avg_acc)
sys.stdout.write(f"\r {len(actions_per_ep)} Step {step+1}: Loss {avg_loss} , Accuracy {avg_acc}")
sys.stdout.flush()
deeps.append(ds_per_ep)
shallows.append(shs_per_ep)
actions.append(actions_per_ep)
# (historical?) training loss, state feauture 0
historical_loss = ( historical_loss*(step + shallow[0][0][2] ) + loss) / (step + shallow[0][0][2] + 1)
shallow[:,:,0] = np.array([historical_loss]*20).reshape(4,5)
shallow[:,:,2] = np.array([shallow[0][0][2]+1]*20).reshape(4,5) # num of iterations (epochs?)
rp=0.
val_loss = 0.
for val_step in range(5):
val_batch = val_iter.next()
val_loss,rp, _ = step_fn(val_batch,val_step,val_loss, rp, val=True)
rc = -np.log(step/253 + 1e-15) if val_loss <= .25 else 0.
if rc : print('Found it!')
rews.append(rp+rc)
print()
val_acc=0.
val_loss = 0.
for step in range(val_size//4):
val_batch = val_iter.next()
val_loss,val_acc, guard = step_fn(val_batch,step, val_loss, val_acc,True)
sys.stdout.write(f"\r {guard} Step {step+1}: Val Loss {val_loss} , Val Accuracy {val_acc}")
sys.stdout.flush()
# update at the end of validation or during?
shallow[:,:,1] = np.array([max(val_acc, shallow[0][0][1])]*20).reshape(4,5) # best val acc
return deeps, shallows,actions, rews,historical_loss
# Commented out IPython magic to ensure Python compatibility.
# %%time
# positions = np.hstack([(np.random.randint(66,394, size=(4,5)))/230. - 1, np.random.randint(66,634, size=(4,5))/350. -1]).reshape(4,5,2)
# # positions = np.hstack([(np.random.randint(66,394, size=(4,5)))/460. , (np.random.randint(66,634, size=(4,5)))/700. ]).reshape(4,5,2)
# shallow = np.zeros((4,5,7)) # all other but deep feautures of DeNet input
# hist_rew = 0.
#
# for epoch in range(5):
# print(f"Epoch {epoch+1}")
# deeps,shallows, actions, rews , hist_loss= train_epoch(historical_loss=0. if not epoch else hist_loss)
#
# print()
# with tf.GradientTape() as policy_tape:
# sums = []
# for ep, (ds, os) in enumerate(zip(deeps,shallows)):
# outputs = [De_Net([d,o]) for d,o in zip(ds,os)]
# for ts,o in enumerate(outputs):
# select = tf.reduce_sum(tf.one_hot(actions[ep][ts][0],depth=2)*o[0], axis=-1)
# pdf_y = tf.exp(-0.5 *((actions[ep][ts][1] - o[1][:,:,0]) / (o[1][:,:,1]))**2) * 1/(o[1][:,:,1]*tf.sqrt(2 *np.pi))
# pdf_x = tf.exp(-0.5 *((actions[ep][ts][2] - o[2][:,:,0]) / (o[2][:,:,1]))**2) * 1/(o[2][:,:,1]*tf.sqrt(2 *np.pi))
# outputs[ts] = tf.stack([select, pdf_y,pdf_x], axis=-1 )
#
# log_probs = tf.keras.backend.log(outputs)
# sums.append(tf.reduce_sum(log_probs, axis=[0]))
# sums = tf.stack(sums, axis=0)
#
# policy_loss = -tf.reduce_mean(sums*(np.array([[[[r]*3]*5]*4 for r in rews])-hist_rew), axis=0)
# policy_variables = De_Net.trainable_variables
# grads = policy_tape.gradient(policy_loss, policy_variables)
# De_Net.optimizer.apply_gradients(zip(grads, policy_variables))
#
# hist_rew = (hist_rew*epoch+np.mean(rews))/(epoch+1)
#
# show_batch(batch, positions)
for epoch in range(5,10):
print(f"Epoch {epoch+1}")
deeps,shallows, actions, rews , hist_loss= train_epoch(historical_loss=0. if not epoch else hist_loss)
print()
with tf.GradientTape() as policy_tape:
sums = []
for ep, (ds, os) in enumerate(zip(deeps,shallows)):
outputs = [De_Net([d,o]) for d,o in zip(ds,os)]
for ts,o in enumerate(outputs):
select = tf.reduce_sum(tf.one_hot(actions[ep][ts][0],depth=2)*o[0], axis=-1)
pdf_y = tf.exp(-0.5 *((actions[ep][ts][1] - o[1][:,:,0]) / (o[1][:,:,1]))**2) * 1/(o[1][:,:,1]*tf.sqrt(2 *np.pi))
pdf_x = tf.exp(-0.5 *((actions[ep][ts][2] - o[2][:,:,0]) / (o[2][:,:,1]))**2) * 1/(o[2][:,:,1]*tf.sqrt(2 *np.pi))
outputs[ts] = tf.stack([select, pdf_y,pdf_x], axis=-1 )
log_probs = tf.keras.backend.log(outputs)
sums.append(tf.reduce_sum(log_probs, axis=[0,1,2]))
sums = tf.stack(sums, axis=0)
policy_loss = -tf.reduce_mean(sums*(np.array([[r] for r in rews])-hist_rew), axis=0)
policy_variables = De_Net.trainable_variables
grads = policy_tape.gradient(policy_loss, policy_variables)
De_Net.optimizer.apply_gradients(zip(grads, policy_variables))
hist_rew = (hist_rew*epoch+np.mean(rews))/(epoch+1)
show_batch(batch, positions)
for epoch in range(10,20):
print(f"Epoch {epoch+1}")
deeps,shallows, actions, rews , hist_loss= train_epoch(historical_loss=0. if not epoch else hist_loss)
print()
with tf.GradientTape() as policy_tape:
sums = []
for ep, (ds, os) in enumerate(zip(deeps,shallows)):
outputs = [De_Net([d,o]) for d,o in zip(ds,os)]
for ts,o in enumerate(outputs):
select = tf.reduce_sum(tf.one_hot(actions[ep][ts][0],depth=2)*o[0], axis=-1)
pdf_y = tf.exp(-0.5 *((actions[ep][ts][1] - o[1][:,:,0]) / (o[1][:,:,1]))**2) * 1/(o[1][:,:,1]*tf.sqrt(2 *np.pi))
pdf_x = tf.exp(-0.5 *((actions[ep][ts][2] - o[2][:,:,0]) / (o[2][:,:,1]))**2) * 1/(o[2][:,:,1]*tf.sqrt(2 *np.pi))
outputs[ts] = tf.stack([select, pdf_y,pdf_x], axis=-1 )
log_probs = tf.keras.backend.log(outputs)
sums.append(tf.reduce_sum(log_probs, axis=[0,1,2]))
sums = tf.stack(sums, axis=0)
policy_loss = -tf.reduce_mean(sums*(np.array([[r] for r in rews])-hist_rew), axis=0)
policy_variables = De_Net.trainable_variables
grads = policy_tape.gradient(policy_loss, policy_variables)
De_Net.optimizer.apply_gradients(zip(grads, policy_variables))
hist_rew = (hist_rew*epoch+np.mean(rews))/(epoch+1)
show_batch(batch, positions)
"""### Experiment 6/10
* 1 train 5 val
* Zeorpadding before multiply in SaNet
* normalized, .125
* Default adam
* loc= continuous actions
"""
# Commented out IPython magic to ensure Python compatibility.
# %%time
# positions = np.hstack([(np.random.randint(66,394, size=(4,5)))/230. - 1, np.random.randint(66,634, size=(4,5))/350. -1]).reshape(4,5,2)
# # positions = np.hstack([(np.random.randint(66,394, size=(4,5)))/460. , (np.random.randint(66,634, size=(4,5)))/700. ]).reshape(4,5,2)
# shallow = np.zeros((4,5,7)) # all other but deep feautures of DeNet input
# hist_rew = 0.
#
# for epoch in range(5):
# print(f"Epoch {epoch+1}")
# deeps,shallows, actions, rews , hist_loss= train_epoch(historical_loss=0. if not epoch else hist_loss)
#
# print()
# with tf.GradientTape() as policy_tape:
# sums = []
# for ep, (ds, os) in enumerate(zip(deeps,shallows)):
# outputs = [De_Net([d,o]) for d,o in zip(ds,os)]
# for ts,o in enumerate(outputs):
# select = tf.reduce_sum(tf.one_hot(actions[ep][ts][0],depth=2)*o[0], axis=-1)
# pdf_y = tf.exp(-0.5 *((actions[ep][ts][1] - o[1][:,:,0]) / (o[1][:,:,1]))**2) * 1/(o[1][:,:,1]*tf.sqrt(2 *np.pi))
# pdf_x = tf.exp(-0.5 *((actions[ep][ts][2] - o[2][:,:,0]) / (o[2][:,:,1]))**2) * 1/(o[2][:,:,1]*tf.sqrt(2 *np.pi))
# outputs[ts] = tf.stack([select, pdf_y,pdf_x], axis=-1 )
#
# log_probs = tf.keras.backend.log(outputs)
# sums.append(tf.reduce_sum(log_probs, axis=[0,1,2]))
# sums = tf.stack(sums, axis=0)
#
# policy_loss = -tf.reduce_mean(sums*(np.array([[r] for r in rews])-hist_rew), axis=0)
# policy_variables = De_Net.trainable_variables
# grads = policy_tape.gradient(policy_loss, policy_variables)
# De_Net.optimizer.apply_gradients(zip(grads, policy_variables))
#
# hist_rew = (hist_rew*epoch+np.mean(rews))/(epoch+1)
#
# show_batch(batch, positions)
# Commented out IPython magic to ensure Python compatibility.
# %%time
# for epoch in range(5,15):
# print(f"Epoch {epoch+1}")
# deeps,shallows, actions, rews , hist_loss= train_epoch(historical_loss=0. if not epoch else hist_loss)
#
# print()
# with tf.GradientTape() as policy_tape:
# sums = []
# for ep, (ds, os) in enumerate(zip(deeps,shallows)):
# outputs = [De_Net([d,o]) for d,o in zip(ds,os)]
# for ts,o in enumerate(outputs):
# select = tf.reduce_sum(tf.one_hot(actions[ep][ts][0],depth=2)*o[0], axis=-1)
# pdf_y = tf.exp(-0.5 *((actions[ep][ts][1] - o[1][:,:,0]) / (o[1][:,:,1]))**2) * 1/(o[1][:,:,1]*tf.sqrt(2 *np.pi))
# pdf_x = tf.exp(-0.5 *((actions[ep][ts][2] - o[2][:,:,0]) / (o[2][:,:,1]))**2) * 1/(o[2][:,:,1]*tf.sqrt(2 *np.pi))
# outputs[ts] = tf.stack([select, pdf_y,pdf_x], axis=-1 )
#
# log_probs = tf.keras.backend.log(outputs)
# sums.append(tf.reduce_sum(log_probs, axis=[0,1,2]))
# sums = tf.stack(sums, axis=0)
#
# policy_loss = -tf.reduce_mean(sums*(np.array([[r] for r in rews])-hist_rew), axis=0)
# policy_variables = De_Net.trainable_variables
# grads = policy_tape.gradient(policy_loss, policy_variables)
# De_Net.optimizer.apply_gradients(zip(grads, policy_variables))