-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathnet.py
More file actions
1370 lines (1121 loc) · 54.1 KB
/
net.py
File metadata and controls
1370 lines (1121 loc) · 54.1 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
"""
This is the main implementation of UrsonNet.
Disclaimer:
Part of this code was adapted from
https://github.com/matterport/Mask_RCNN
Copyright (c) 2017 Matterport, INC.
Licenced under the MIT Licence
TODO:
- layer_regex replace 'fpn' and 'pose_'
"""
import os
import random
import datetime
import re
import math
import logging
from collections import OrderedDict
import multiprocessing
import numpy as np
import skimage.transform
import tensorflow as tf
import keras
import keras.backend as K
import keras.layers as KL
import keras.engine as KE
import keras.models as KM
import imgaug as ia
from imgaug import augmenters as iaa
import utils
# Requires TensorFlow 1.3+ and Keras 2.0.8+.
from distutils.version import LooseVersion
assert LooseVersion(tf.__version__) >= LooseVersion("1.3")
assert LooseVersion(keras.__version__) >= LooseVersion('2.0.8')
############################################################
# Utility Functions
############################################################
def log(text, array=None):
"""Prints a text message. And, optionally, if a Numpy array is provided it
prints it's shape, min, and max values.
"""
if array is not None:
text = text.ljust(25)
text += ("shape: {:20} min: {:10.5f} max: {:10.5f} {}".format(
str(array.shape),
array.min() if array.size else "",
array.max() if array.size else "",
array.dtype))
print(text)
class BatchNorm(KL.BatchNormalization):
"""Extends the Keras BatchNormalization class to allow a central place
to make changes if needed.
Batch normalization has a negative effect on training if batches are small
so this layer is often frozen (via setting in Config class) and functions
as linear layer.
"""
def call(self, inputs, training=None):
"""
Note about training values:
None: Train BN layers. This is the normal mode
False: Freeze BN layers. Good when batch size is small
True: (don't use). Set layer in training mode even when inferencing
"""
return super(self.__class__, self).call(inputs, training=training)
############################################################
# Resnet Graph
############################################################
# Code adopted from:
# https://github.com/fchollet/deep-learning-models/blob/master/resnet50.py
def identity_block(input_tensor, kernel_size, filters, stage, block,
use_bias=True, train_bn=True):
"""The identity_block is the block that has no conv layer at shortcut
# Arguments
input_tensor: input tensor
kernel_size: defualt 3, the kernel size of middle conv layer at main path
filters: list of integers, the nb_filters of 3 conv layer at main path
stage: integer, current stage label, used for generating layer names
block: 'a','b'..., current block label, used for generating layer names
use_bias: Boolean. To use or not use a bias in conv layers.
train_bn: Boolean. Train or freeze Batch Norm layres
"""
nb_filter1, nb_filter2, nb_filter3 = filters
conv_name_base = 'res' + str(stage) + block + '_branch'
bn_name_base = 'bn' + str(stage) + block + '_branch'
x = KL.Conv2D(nb_filter1, (1, 1), name=conv_name_base + '2a',
use_bias=use_bias)(input_tensor)
x = BatchNorm(name=bn_name_base + '2a')(x, training=train_bn)
x = KL.Activation('relu')(x)
x = KL.Conv2D(nb_filter2, (kernel_size, kernel_size), padding='same',
name=conv_name_base + '2b', use_bias=use_bias)(x)
x = BatchNorm(name=bn_name_base + '2b')(x, training=train_bn)
x = KL.Activation('relu')(x)
x = KL.Conv2D(nb_filter3, (1, 1), name=conv_name_base + '2c',
use_bias=use_bias)(x)
x = BatchNorm(name=bn_name_base + '2c')(x, training=train_bn)
x = KL.Add()([x, input_tensor])
x = KL.Activation('relu', name='res' + str(stage) + block + '_out')(x)
return x
def conv_block(input_tensor, kernel_size, filters, stage, block,
strides=(2, 2), use_bias=True, train_bn=True):
"""conv_block is the block that has a conv layer at shortcut
# Arguments
input_tensor: input tensor
kernel_size: default 3, the kernel size of middle conv layer at main path
filters: list of integers, the nb_filters of 3 conv layer at main path
stage: integer, current stage label, used for generating layer names
block: 'a','b'..., current block label, used for generating layer names
use_bias: Boolean. To use or not use a bias in conv layers.
train_bn: Boolean. Train or freeze Batch Norm layres
Note that from stage 3, the first conv layer at main path is with subsample=(2,2)
And the shortcut should have subsample=(2,2) as well
"""
nb_filter1, nb_filter2, nb_filter3 = filters
conv_name_base = 'res' + str(stage) + block + '_branch'
bn_name_base = 'bn' + str(stage) + block + '_branch'
x = KL.Conv2D(nb_filter1, (1, 1), strides=strides,
name=conv_name_base + '2a', use_bias=use_bias)(input_tensor)
x = BatchNorm(name=bn_name_base + '2a')(x, training=train_bn)
x = KL.Activation('relu')(x)
x = KL.Conv2D(nb_filter2, (kernel_size, kernel_size), padding='same',
name=conv_name_base + '2b', use_bias=use_bias)(x)
x = BatchNorm(name=bn_name_base + '2b')(x, training=train_bn)
x = KL.Activation('relu')(x)
x = KL.Conv2D(nb_filter3, (1, 1), name=conv_name_base +
'2c', use_bias=use_bias)(x)
x = BatchNorm(name=bn_name_base + '2c')(x, training=train_bn)
shortcut = KL.Conv2D(nb_filter3, (1, 1), strides=strides,
name=conv_name_base + '1', use_bias=use_bias)(input_tensor)
shortcut = BatchNorm(name=bn_name_base + '1')(shortcut, training=train_bn)
x = KL.Add()([x, shortcut])
x = KL.Activation('relu', name='res' + str(stage) + block + '_out')(x)
return x
def resnet_graph(input_image, architecture, stage5=False, train_bn=True):
"""Build a ResNet graph.
architecture: Can be resnet50 or resnet101
stage5: Boolean. If False, stage5 of the network is not created
train_bn: Boolean. Train or freeze Batch Norm layres
"""
assert architecture in ["resnet50", "resnet101"]
# Stage 1
x = KL.ZeroPadding2D((3, 3))(input_image)
x = KL.Conv2D(64, (7, 7), strides=(2, 2), name='conv1', use_bias=True)(x)
x = BatchNorm(name='bn_conv1')(x, training=train_bn)
x = KL.Activation('relu')(x)
# tmp
#C1 = x = KL.Conv2D(64, (3, 3), strides=(2, 2), name='conv2')(x)
C1 = x = KL.MaxPooling2D((3, 3), strides=(2, 2), padding="same")(x)
# Stage 2
x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1), train_bn=train_bn)
x = identity_block(x, 3, [64, 64, 256], stage=2, block='b', train_bn=train_bn)
C2 = x = identity_block(x, 3, [64, 64, 256], stage=2, block='c', train_bn=train_bn)
# Stage 3
x = conv_block(x, 3, [128, 128, 512], stage=3, block='a', train_bn=train_bn)
x = identity_block(x, 3, [128, 128, 512], stage=3, block='b', train_bn=train_bn)
x = identity_block(x, 3, [128, 128, 512], stage=3, block='c', train_bn=train_bn)
C3 = x = identity_block(x, 3, [128, 128, 512], stage=3, block='d', train_bn=train_bn)
# Stage 4
x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a', train_bn=train_bn)
block_count = {"resnet50": 5, "resnet101": 22}[architecture]
for i in range(block_count):
x = identity_block(x, 3, [256, 256, 1024], stage=4, block=chr(98 + i), train_bn=train_bn)
C4 = x
# Stage 5
if stage5:
x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a', train_bn=train_bn)
x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b', train_bn=train_bn)
C5 = x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c', train_bn=train_bn)
else:
C5 = None
return [C1, C2, C3, C4, C5]
############################################################
# Shallow Resnet
############################################################
# Code adopted from:
# https://github.com/qubvel/classification_models/blob/master/classification_models/resnet/builder.py
def handle_block_names(stage, block):
name_base = 'stage{}_unit{}_'.format(stage + 1, block + 1)
conv_name = name_base + 'conv'
bn_name = name_base + 'bn'
relu_name = name_base + 'relu'
sc_name = name_base + 'sc'
return conv_name, bn_name, relu_name, sc_name
def residual_basic_block(input_tensor, filters, stage, block, strides=(1, 1), cut='pre', use_bias=False, train_bn=True):
# get names of layers
conv_name, bn_name, relu_name, sc_name = handle_block_names(stage, block)
# defining shortcut connection
if cut == 'pre':
shortcut = input_tensor
elif cut == 'post':
shortcut = KL.Conv2D(filters, (1, 1), name=sc_name, strides=strides, use_bias=use_bias)(input_tensor)
else:
raise ValueError('Cut type not in ["pre", "post"]')
# Two 3x3 convolution layers
x = KL.ZeroPadding2D(padding=(1, 1))(input_tensor)
x = KL.Conv2D(filters, (3, 3), strides=strides, name=conv_name + '1', use_bias=use_bias)(x)
x = BatchNorm(name=bn_name + '2')(x, training=train_bn)
x = KL.Activation('relu', name=relu_name + '1')(x)
x = KL.ZeroPadding2D(padding=(1, 1))(x)
x = KL.Conv2D(filters, (3, 3), name=conv_name + '2', use_bias=use_bias)(x)
# add residual connection
x = KL.Add()([x, shortcut])
x = KL.Activation('relu', name=relu_name + '2')(x)
return x
def resnet_shallow_graph(input_image, architecture, train_bn=True):
'''
N.b: Currently convolutions do not use the bias term (unlike the 'deeper' resnet_graph)
to keep compatibility with pre-trained weights
'''
assert architecture in ["resnet18", "resnet34"]
nr_init_filters = 64
# Resnet bottom
x = KL.ZeroPadding2D(padding=(3, 3))(input_image)
x = KL.Conv2D(nr_init_filters, (7, 7), strides=(2, 2), name='conv0', use_bias=False)(x)
x = BatchNorm(name='bn_conv0')(x, training=train_bn)
x = KL.Activation('relu')(x)
C1 = x = KL.MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)
# TODO: Allow more architectures
if architecture == 'resnet18':
repetitions = [2, 2, 2, 2]
else:
# This is fo 34 layers
repetitions = (3, 4, 6, 3)
for stage, rep in enumerate(repetitions):
for block in range(rep):
nr_filters = nr_init_filters * (2 ** stage)
# first block of first stage without strides because we have maxpooling before
if block == 0 and stage == 0:
x = residual_basic_block(x, nr_filters, stage, block, strides=(1, 1), cut='post', train_bn=train_bn)
elif block == 0:
x = residual_basic_block(x, nr_filters, stage, block, strides=(2, 2),cut='post',train_bn=train_bn)
else:
x = residual_basic_block(x, nr_filters, stage, block, strides=(1, 1), cut='pre', train_bn=train_bn)
return x
############################################################
# Network Heads
############################################################
def build_loc_graph(feature_map, config, nr_features):
"""Builds the computation graph for location estimation on top of the Feature Network.
Options: (1) XYZ regression (default), (2) 3D Keypoint regression and (3) classification (experimental)
Returns: Location [batch, N]
"""
nr_fc_layers = config.NR_DENSE_LAYERS
assert nr_fc_layers in range(3)
# TODO: Move this outside the function (redundancy)
x = KL.Reshape((nr_features,))(feature_map)
for i in range(nr_fc_layers):
intermediate_fc_layer_name = 'loc_dense_' + str(i)
x = KL.Dense(config.BRANCH_SIZE, name =intermediate_fc_layer_name)(x)
if config.TRAIN_BN:
bn_name = 'loc_bn_' + str(i)
x = BatchNorm(name =bn_name)(x)
x = KL.Activation('relu')(x)
if config.REGRESS_KEYPOINTS:
k1 = KL.Dense(3, activation='linear', name="k1_final")(x)
k2 = KL.Dense(3, activation='linear', name="k2_final")(x)
k3 = KL.Dense(3, activation='linear', name="k3_final")(x)
loc = [k1,k2,k3]
else:
if config.REGRESS_LOC:
loc = KL.Dense(3, activation='linear', name="loc_final")(x)
else:
loc = KL.Dense(config.LOC_BINS_PER_DIM**3, activation='relu', name="loc_final")(x)
return loc
def build_ori_graph(feature_map, config, nr_features):
"""Builds the computation graph for orientation estimation on top of the Feature Network.
Options: (1) 4D -vector regression (e.g. quaternion), (2) 3D-vector regression (e.g. angle-axis) and (3) classification
Returns: Orientation [batch, N]
"""
nr_fc_layers = config.NR_DENSE_LAYERS
assert nr_fc_layers in range(3)
# TODO: Move this outside the function (redundancy)
x = KL.Reshape((nr_features,))(feature_map)
for i in range(nr_fc_layers):
intermediate_fc_layer_name = 'ori_dense_' + str(i)
x = KL.Dense(config.BRANCH_SIZE, name =intermediate_fc_layer_name)(x)
if config.TRAIN_BN:
bn_name = 'ori_bn_' + str(i)
x = BatchNorm(name =bn_name)(x)
x = KL.Activation('relu')(x)
if config.REGRESS_ORI:
if config.ORIENTATION_PARAM == 'quaternion':
q = KL.Dense(4, activation='linear', name="ori_q")(x)
q = KL.Lambda(lambda x: K.l2_normalize(q, axis=-1))(q)
else:
q = KL.Dense(3, activation='linear', name="ori_final")(x)
else:
q = KL.Dense(config.ORI_BINS_PER_DIM**3, activation='relu', name="ori_final")(x)
return q
############################################################
# Data Generator
############################################################
def load_image_gt(dataset, config, image_id):
"""Load an image + object pose and apply augmentation pipeline (if necessary)
Returns:
image: [height, width, n]
shape: the original shape of the image before resizing and cropping.
loc: [x,y,z]
ori: orientation representation
"""
# Load and resize image
image = dataset.load_image(image_id)
if config.REGRESS_LOC:
loc = dataset.load_location(image_id)
else:
loc = dataset.load_location_encoded(image_id)
if config.REGRESS_KEYPOINTS:
keypoints = dataset.load_keypoints(image_id)
k1 = keypoints[0]
k2 = keypoints[1]
if config.REGRESS_KEYPOINTS or config.REGRESS_ORI:
if config.ORIENTATION_PARAM == 'quaternion':
ori = dataset.load_quaternion(image_id)
elif config.ORIENTATION_PARAM == 'euler_angles':
ori = dataset.load_euler_angles(image_id)
elif config.ORIENTATION_PARAM == 'angle_axis':
ori = dataset.load_angle_axis(image_id)
else:
ori = dataset.load_orientation_encoded(image_id)
if config.SIM2REAL_AUG:
image_gray = 0.2126*image[:,:,0]+0.7152*image[:,:,1]+0.0722*image[:,:,2]
image[:, :, 0] = image_gray
image[:, :, 1] = image_gray
image[:, :, 2] = image_gray
if np.random.rand(1) > 0.5:
# Image Augmentation Pipeline
aug_pipeline = iaa.Sequential([
iaa.AdditiveGaussianNoise(scale=0.01 * 255),
iaa.GaussianBlur(sigma=(0.0,1.5)),
iaa.Add((-20, 20)),
iaa.Multiply((0.5,2.0)),
iaa.CoarseDropout([0.0, 0.03], size_percent=(0.02,0.1))
], random_order=True)
det = aug_pipeline.to_deterministic()
image = det.augment_image(image)
if config.ROT_AUG or config.ROT_IMAGE_AUG:
assert config.REGRESS_LOC
assert config.ORIENTATION_PARAM == 'quaternion'
# TODO: The 2 rotation augmentation operations are so far applied with mutual exclusion. Arbitrary may lead to more variation.
dice = np.random.rand(1)
# Camera orientation perturbation half the time
if config.ROT_AUG and dice > 0.5:
if config.REGRESS_KEYPOINTS or config.REGRESS_ORI:
image, loc, ori = utils.rotate_cam(image, loc, ori, dataset.camera.K, 20)
k1, k2 = utils.encode_as_keypoints(ori, loc)
else:
ori = dataset.load_quaternion(image_id)
image, loc, ori = utils.rotate_cam(image, loc, ori, dataset.camera.K, 20)
# Update encoded orientation
ori = utils.encode_ori_fast(ori, config.BETA, dataset.ori_histogram_map, dataset.ori_output_mask)
elif config.ROT_IMAGE_AUG and dice <= 0.5:
if config.REGRESS_KEYPOINTS or config.REGRESS_ORI:
image, loc, ori = utils.rotate_image(image, loc, ori, dataset.camera.K)
k1, k2 = utils.encode_as_keypoints(ori, loc)
else:
ori = dataset.load_quaternion(image_id)
image, loc, ori = utils.rotate_image(image, loc, ori, dataset.camera.K)
# Update encoded orientation
ori = utils.encode_ori_fast(ori, config.BETA, dataset.ori_histogram_map, dataset.ori_output_mask)
original_shape = image.shape
image, window, scale, padding, crop = utils.resize_image(
image,
min_dim=config.IMAGE_MIN_DIM,
min_scale=config.IMAGE_MIN_SCALE,
max_dim=config.IMAGE_MAX_DIM,
mode=config.IMAGE_RESIZE_MODE)
# Image meta data
image_meta = compose_image_meta(image_id, original_shape, image.shape,
window, scale)
if config.REGRESS_KEYPOINTS:
return image, image_meta, loc, k1.T, k2.T
else:
return image, image_meta, loc, ori
def data_generator(dataset, config, shuffle=True, batch_size=1):
"""A generator that returns images and corresponding groundtruth.
dataset: The Dataset object to pick data from
config: The model config object
shuffle: If True, shuffles the samples before every epoch
batch_size: How many images to return in each call
Returns a Python generator. Upon calling next() on it, the
generator returns two lists, inputs and outputs. The containtes
of the lists differs depending on the received arguments:
inputs list:
- images: [batch, H, W, C]
- image_meta: [batch, (meta data)] Image details. See compose_image_meta()
- gt_locs: [batch, N]
- gt_oris: [batch, N]
"""
b = 0 # batch item index
image_index = -1
image_ids = np.copy(dataset.image_ids)
error_count = 0
tensor_dtype = np.float32
# For modern GPUs
if config.F16:
tensor_dtype = np.float16
# Keras requires a generator to run indefinately.
while True:
try:
# Increment index to pick next image. Shuffle if at the start of an epoch.
image_index = (image_index + 1) % len(image_ids)
if shuffle and image_index == 0:
np.random.shuffle(image_ids)
# Get GT for image.
image_id = image_ids[image_index]
if config.REGRESS_KEYPOINTS:
image, image_meta, gt_loc, gt_k1, gt_k2 = load_image_gt(dataset, config, image_id)
else:
image, image_meta, gt_loc, gt_ori = load_image_gt(dataset, config, image_id)
# Init batch arrays
if b == 0:
batch_image_meta = np.zeros(
(batch_size,) + image_meta.shape, dtype=image_meta.dtype)
batch_images = np.zeros(
(batch_size,) + image.shape, dtype=tensor_dtype)
if config.REGRESS_LOC:
batch_gt_locs = np.zeros((batch_size, 3), dtype=tensor_dtype)
else:
batch_gt_locs = np.zeros((batch_size, config.LOC_BINS_PER_DIM ** 3), dtype=tensor_dtype)
if config.REGRESS_KEYPOINTS:
batch_gt_k1 = np.zeros((batch_size, 3), dtype=tensor_dtype)
batch_gt_k2 = np.zeros((batch_size, 3), dtype=tensor_dtype)
else:
if config.REGRESS_ORI:
if config.ORIENTATION_PARAM == 'quaternion':
batch_gt_oris = np.zeros((batch_size, 4), dtype=tensor_dtype)
else:
batch_gt_oris = np.zeros((batch_size, 3), dtype=tensor_dtype)
else:
batch_gt_oris = np.zeros((batch_size, config.ORI_BINS_PER_DIM ** 3), dtype=tensor_dtype)
# Add to batch
batch_image_meta[b] = image_meta
batch_images[b] = mold_image(image.astype(tensor_dtype), config)
batch_gt_locs[b] = gt_loc
if config.REGRESS_KEYPOINTS:
batch_gt_k1[b] = gt_k1
batch_gt_k2[b] = gt_k2
else:
batch_gt_oris[b] = gt_ori
b += 1
# Batch full?
if b >= batch_size:
if config.REGRESS_KEYPOINTS:
inputs = [batch_images, batch_image_meta, batch_gt_locs, batch_gt_k1, batch_gt_k2]
else:
inputs = [batch_images, batch_image_meta, batch_gt_locs, batch_gt_oris]
outputs = []
yield inputs, outputs
# start a new batch
b = 0
except (GeneratorExit, KeyboardInterrupt):
raise
except:
# Log it and skip the image
logging.exception("Error processing image {}".format(
dataset.image_info[image_id]))
error_count += 1
if error_count > 5:
raise
############################################################
# NNetwork Class and Graph Initialization
############################################################
class UrsoNet():
def __init__(self, mode, config, model_dir):
"""
mode: Either "training" or "inference"
config: A Sub-class of the Config class
model_dir: Directory to save training logs and trained weights
"""
assert mode in ['training', 'inference']
self.mode = mode
self.config = config
self.model_dir = model_dir
self.set_log_dir()
self.keras_model = self.build(mode=mode, config=config)
def build(self, mode, config):
"""Build UrsoNet architecture.
input_shape: The shape of the input image.
mode: Either "training" or "inference". The inputs and
outputs of the model differ accordingly.
"""
assert mode in ['training', 'inference']
# Change Keras backend to use f16 precision
if config.F16:
K.set_floatx('float16')
# default is 1e-7 which is too small for float16. Without adjusting the epsilon, we will get NaN predictions because of divide by zero problems
K.set_epsilon(1e-4)
# Image size must be dividable by 2 multiple times
h, w = config.IMAGE_SHAPE[:2]
if h / 2 ** 6 != int(h / 2 ** 6) or w / 2 ** 6 != int(w / 2 ** 6):
raise Exception("Image size must be dividable by 2 at least 6 times "
"to avoid fractions when downscaling and upscaling."
"For example, use 256, 320, 384, 448, 512, ... etc. ")
# Inputs
input_image = KL.Input(shape=[None, None, config.NR_IMAGE_CHANNELS], name="input_image")
input_image_meta = KL.Input(shape=[config.IMAGE_META_SIZE], name="input_image_meta")
tensor_dtype = tf.float32
if config.F16:
tensor_dtype = tf.float16
if mode == "training":
if config.REGRESS_LOC:
input_gt_loc = KL.Input(shape=[3], name="input_gt_loc", dtype=tensor_dtype)
else:
input_gt_loc = KL.Input(shape=[config.LOC_BINS_PER_DIM**3], name="input_gt_loc", dtype=tensor_dtype)
if config.REGRESS_KEYPOINTS:
input_gt_k2 = KL.Input(shape=[3], name="input_gt_k2", dtype=tensor_dtype)
input_gt_k3 = KL.Input(shape=[3], name="input_gt_k3", dtype=tensor_dtype)
else:
if config.REGRESS_ORI:
if config.ORIENTATION_PARAM == 'quaternion':
input_gt_ori = KL.Input(shape=[4], name="input_gt_ori", dtype=tensor_dtype)
else:
input_gt_ori = KL.Input(shape=[3], name="input_gt_ori", dtype=tensor_dtype)
else:
input_gt_ori = KL.Input(shape=[config.ORI_BINS_PER_DIM ** 3], name="input_gt_ori", dtype=tensor_dtype)
# Backbone architecture
if config.BACKBONE in ['resnet50', 'resnet101']:
_, C2, C3, C4, C5 = resnet_graph(input_image, config.BACKBONE, stage5=True, train_bn=config.TRAIN_BN)
else:
C5 = resnet_shallow_graph(input_image, config.BACKBONE, train_bn=config.TRAIN_BN)
# Original Resnet uses a 7x7 average pooling:
# C6 = KL.GlobalAveragePooling2D()(C5)
# but because we care about resolution, instead we perform here a convolution
C6 = KL.Conv2D(config.BOTTLENECK_WIDTH, (3, 3), padding='SAME', strides=(2, 2), name='bottleneck_layer')(C5)
nr_features = int(config.BOTTLENECK_WIDTH * config.IMAGE_SHAPE[0] * config.IMAGE_SHAPE[1] / (64 ** 2))
loc_pred = build_loc_graph(C6, config, nr_features)
ori_pred = build_ori_graph(C6, config, nr_features)
if mode == "training":
# Experimental feature
if config.LEARNABLE_LOSS_WEIGHTS:
self.ori_weight = K.variable(-2.3, name= 'ori_weight')
self.loc_weight = K.variable(0.0, name= 'loc_weight')
else:
# Default
self.ori_weight = K.variable(0.0, name= 'ori_weight')
self.loc_weight = K.variable(0.0)
if config.REGRESS_KEYPOINTS:
loc_loss = KL.Lambda(lambda x: self.mse_loss_graph(*x), name="loc_loss")([input_gt_loc, loc_pred[0]])
k2_loss = KL.Lambda(lambda x: self.mse_loss_graph(*x), name="k2_loss")([input_gt_k2, loc_pred[1]])
k3_loss = KL.Lambda(lambda x: self.mse_loss_graph(*x), name="k3_loss")([input_gt_k3, loc_pred[2]])
else:
if config.REGRESS_LOC:
loc_loss = KL.Lambda(lambda x: self.rel_loss_graph(*x), name="loc_loss")([input_gt_loc, loc_pred])
else:
loc_loss = KL.Lambda(lambda x: self.softmax_loss_graph(*x), name="loc_loss")([input_gt_loc, loc_pred])
if config.REGRESS_ORI:
ori_loss = KL.Lambda(lambda x: self.one_minus_dot_prod_graph(*x), name="ori_loss")([input_gt_ori, ori_pred])
else:
ori_loss = KL.Lambda(lambda x: self.softmax_loss_graph(*x), name="ori_loss")([input_gt_ori, ori_pred])
# Model
if config.REGRESS_KEYPOINTS:
inputs = [input_image, input_image_meta, input_gt_loc, input_gt_k2, input_gt_k3]
else:
inputs = [input_image, input_image_meta, input_gt_loc, input_gt_ori]
if config.REGRESS_KEYPOINTS:
outputs = [loc_pred[0], loc_pred[1], loc_pred[2], loc_loss, k2_loss, k3_loss]
else:
outputs = [loc_pred, ori_pred, loc_loss, ori_loss]
model = KM.Model(inputs, outputs, name='urso_net')
# Workaround to make weights trainable
if config.LEARNABLE_LOSS_WEIGHTS:
model.layers[-1].trainable_weights.extend([self.ori_weight, self.loc_weight])
else:
if config.REGRESS_KEYPOINTS:
model = KM.Model(input_image, [loc_pred[0], loc_pred[1], loc_pred[2]], name='urso_net')
else:
model = KM.Model(input_image, [loc_pred, ori_pred], name='urso_net')
# Add multi-GPU support.
# if config.GPU_COUNT > 1:
# from mrcnn.parallel_model import ParallelModel
# model = ParallelModel(model, config.GPU_COUNT)
return model
############################################################
# Loss Functions
############################################################
def softmax_loss_graph(self, y_gt, y_pred):
"""Loss for classification prediction.
"""
# Experimental: Adaptive weighting based on Laplace likelihood (Kendall & Cipolla)
# loss = tf.losses.softmax_cross_entropy(y_gt, y_pred)/tf.exp(self.ori_weight) + self.ori_weight
loss = tf.losses.softmax_cross_entropy(y_gt, y_pred)
return loss
def arcos_graph(self, y_true, y_pred):
"""Implements rotation error
y_true and y_pred are typicallly: [N, 4], but could be any shape.
"""
loss = tf.acos(K.abs(K.sum(y_true * y_pred, axis=-1, keepdims=True)))
# Experimental: Adaptive weighting based on Laplace likelihood (Kendall & Cipolla)
# loss = loss/tf.exp(self.ori_weight) + self.ori_weight
loss_mean = K.mean(loss)
return loss_mean
def one_minus_dot_prod_graph(self, y_true, y_pred):
"""Implements 1-dot-product.
y_true and y_pred are typicallly: [N, 4], but could be any shape.
"""
loss = 1 - K.abs(K.sum(y_true * y_pred, axis=-1, keepdims=True))
# Experimental: Adaptive weighting based on Laplace likelihood (Kendall & Cipolla)
# loss = loss / tf.exp(self.ori_weight) + self.ori_weight
loss_mean = K.mean(loss)
return loss_mean
def mse_loss_graph(self, y_gt, y_pred):
"""Loss for regression prediction.
e.g.
pose_gt: [batch, (x,y,z)]
pose_pred: [batch, (x,y,z)]
"""
loss = K.square(y_gt - y_pred)
# Experimental: Adaptive weighting based on Laplace likelihood (Kendall & Cipolla)
# loss_mse = K.square(y_gt - y_pred)
# loss = loss_mse/tf.exp(self.loc_weight) + self.loc_weight
loss_mean = K.mean(loss)
return loss_mean
def rel_loss_graph(self, y_gt, y_pred):
"""Loss for regression prediction.
e.g.
pose_gt: [batch, (x,y,z)]
pose_pred: [batch, (x,y,z)]
"""
loss = tf.norm((y_gt - y_pred) / tf.norm(y_gt))
# Experimental: Adaptive weighting based on Laplace likelihood (Kendall & Cipolla)
# loss = loss/tf.exp(self.loc_weight) + self.loc_weight
loss_mean = K.mean(loss)
return loss_mean
############################################################
# Weights Loading Functions
############################################################
def get_last_checkpoint(self,model_name):
"""Finds the last checkpoint file of a selected trained model in the
model directory.
Returns:
log_dir: The directory where events and weights are saved
checkpoint_path: the path to the last checkpoint file
"""
dir_names = next(os.walk(self.model_dir))[1]
assert model_name in dir_names
model_path = os.path.join(self.model_dir, model_name)
checkpoints = next(os.walk(model_path))[2]
checkpoints = filter(lambda f: f.startswith("weights"), checkpoints)
checkpoints = sorted(checkpoints)
if not checkpoints:
return model_path, None
checkpoint = os.path.join(model_path, checkpoints[-1])
return model_path, checkpoint
def find_last(self):
"""Finds the last checkpoint file of the last trained model in the
model directory.
Returns:
log_dir: The directory where events and weights are saved
checkpoint_path: the path to the last checkpoint file
"""
# Get directory names. Each directory corresponds to a model
dir_names = next(os.walk(self.model_dir))[1]
key = self.config.NAME.lower()
dir_names = filter(lambda f: f.startswith(key), dir_names)
dir_names = sorted(dir_names)
if not dir_names:
return None, None
# Pick last directory
dir_name = os.path.join(self.model_dir, dir_names[-1])
# Find the last checkpoint
checkpoints = next(os.walk(dir_name))[2]
checkpoints = filter(lambda f: f.startswith("weights"), checkpoints)
checkpoints = sorted(checkpoints)
if not checkpoints:
return dir_name, None
checkpoint = os.path.join(dir_name, checkpoints[-1])
return dir_name, checkpoint
def load_weights(self, weights_in_path, weights_out_path, by_name=False, exclude=None):
"""Modified version of the correspoding Keras function with
the addition of multi-GPU support and the ability to exclude
some layers from loading.
exlude: list of layer names to exclude
"""
import h5py
from keras.engine import topology
if exclude:
by_name = True
if h5py is None:
raise ImportError('`load_weights` requires h5py.')
f = h5py.File(weights_in_path, mode='r')
if 'layer_names' not in f.attrs and 'model_weights' in f:
f = f['model_weights']
# In multi-GPU training, we wrap the model. Get layers
# of the inner model because they have the weights.
keras_model = self.keras_model
layers = keras_model.inner_model.layers if hasattr(keras_model, "inner_model") \
else keras_model.layers
# Exclude some layers
if exclude:
layers = filter(lambda l: l.name not in exclude, layers)
if by_name:
topology.load_weights_from_hdf5_group_by_name(f, layers)
else:
topology.load_weights_from_hdf5_group(f, layers)
if hasattr(f, 'close'):
f.close()
# Update the log directory
self.set_log_dir(weights_out_path)
def get_imagenet_weights(self, architecture):
"""Downloads ImageNet trained weights from Keras.
Returns path to weights file.
"""
from keras.utils.data_utils import get_file
if architecture in ['resnet50', 'resnet101']:
TF_WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/' \
'releases/download/v0.2/' \
'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
weights_path = get_file('resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5',
TF_WEIGHTS_PATH_NO_TOP,
cache_subdir='models',
md5_hash='a268eb855778b3df3c7506639542a6af')
elif architecture == 'resnet18':
TF_WEIGHTS_PATH_NO_TOP = 'https://github.com/qubvel/classification_models/'\
'releases/download/0.0.1/resnet18_imagenet_1000_no_top.h5'
weights_path = get_file('resnet18_imagenet_1000_no_top.h5',
TF_WEIGHTS_PATH_NO_TOP,
cache_subdir='models',
md5_hash='318e3ac0cd98d51e917526c9f62f0b50')
elif architecture == 'resnet34':
TF_WEIGHTS_PATH_NO_TOP = 'https://github.com/qubvel/classification_models/'\
'releases/download/0.0.1/resnet34_imagenet_1000_no_top.h5'
weights_path = get_file('resnet34_imagenet_1000_no_top.h5',
TF_WEIGHTS_PATH_NO_TOP,
cache_subdir='models',
md5_hash='8caaa0ad39d927cb8ba5385bf945d582')
return weights_path
def get_urso_weights(self, dataset_name):
"""Downloads URSO trained weights from Keras.
Returns path to weights file.
"""
assert dataset_name in ['soyuz_hard', 'dragon_hard', 'speed']
from keras.utils.data_utils import get_file
if dataset_name == "soyuz_hard":
assert self.config.BACKBONE == 'resnet50'
assert self.config.BOTTLENECK_WIDTH == 128
assert self.config.ORI_BINS_PER_DIM == 24
weights_name = 'resnet50_soyuz_hard_128_24.h5'
TF_WEIGHTS_PATH = 'https://github.com/pedropro/UrsoNet/releases/download/v1.0/' + weights_name
elif dataset_name == "dragon_hard":
assert self.config.BACKBONE == 'resnet50'
assert self.config.BOTTLENECK_WIDTH == 128
assert self.config.ORI_BINS_PER_DIM == 24
weights_name = 'resnet50_dragon_hard_128_24.h5'
TF_WEIGHTS_PATH = 'https://github.com/pedropro/UrsoNet/releases/download/v1.0\/' + weights_name
elif dataset_name == "speed":
assert self.config.BACKBONE == 'resnet101'
if self.config.ORI_BINS_PER_DIM == 32:
assert self.config.BOTTLENECK_WIDTH == 528
weights_name = 'resnet101_speed_528_32.h5'
TF_WEIGHTS_PATH = 'https://github.com/pedropro/UrsoNet/releases/download/v1.0/' + weights_name
elif self.config.ORI_BINS_PER_DIM == 64:
assert self.config.BOTTLENECK_WIDTH == 800
weights_name = 'resnet101_speed_800_64.h5'
TF_WEIGHTS_PATH = 'https://github.com/pedropro/UrsoNet/releases/download/v1.0/' + weights_name
weights_path = get_file(weights_name,TF_WEIGHTS_PATH,cache_subdir='models')
return weights_path
def set_log_dir(self, model_path=None):
"""Sets the model log directory and epoch counter.
model_path: If None, or a format different from what this code uses
then set a new log directory and start epochs from 0. Otherwise,
extract the log directory and the epoch counter from the file
name.
"""
if model_path:
# Directory for training logs
self.log_dir = os.path.dirname(model_path)
self.epoch = int(model_path[-6:-3])
else:
self.epoch = 0
now = datetime.datetime.now()
self.log_dir = os.path.join(self.model_dir, "{}{:%Y%m%dT%H%M}".format(
self.config.NAME.lower(), now))
# Path to save after each epoch. Include placeholders that get filled by Keras.
self.checkpoint_path = os.path.join(self.log_dir, "weights_{}_*epoch*.h5".format(
self.config.NAME.lower()))
self.checkpoint_path = self.checkpoint_path.replace(
"*epoch*", "{epoch:04d}")
############################################################
# Weights Loading Functions
############################################################
def compile(self, learning_rate, momentum):
"""Gets the model ready for training. Adds losses, regularization, and
metrics. Then calls the Keras compile() function.
"""
# Optimizer object
if self.config.OPTIMIZER == 'SGD':
optimizer = keras.optimizers.SGD(lr=learning_rate, momentum=momentum,
clipnorm=self.config.GRADIENT_CLIP_NORM)
else:
optimizer = keras.optimizers.Adam(learning_rate, amsgrad=True, clipnorm=self.config.GRADIENT_CLIP_NORM)
# Add Losses
# First, clear previously set losses to avoid duplication
self.keras_model._losses = []
self.keras_model._per_input_losses = {}
if self.config.REGRESS_KEYPOINTS:
loss_names = ["loc_loss", "k2_loss", "k3_loss"]
else:
loss_names = ["loc_loss", "ori_loss"]
for name in loss_names:
layer = self.keras_model.get_layer(name)
if layer.output in self.keras_model.losses:
continue
loss = (
tf.reduce_mean(layer.output, keepdims=True)
* self.config.LOSS_WEIGHTS.get(name, 1.))
self.keras_model.add_loss(loss)