def _spherical_upsampling(self, x): """Apply spherical paddings to the feature maps before bilinear upsampling. Args: x: [BATCH, HEIGHT, WIDTH, CHANNELS] input tensor. Returns: a tensor [BATCH, 2*(HEIGHT+2), 2*(WIDTH+2), CHANNELS]. """ return UpSampling2D(interpolation='bilinear')( geometry.equirectangular_padding(x, [[1, 1], [1, 1]]))
def __init__(self, filters, trans_down=True, **kwargs): super(TransitionBlock, self).__init__(**kwargs) self.filters = filters self.trans_down = trans_down self.bn = BatchNormalization() self.relu = ReLU() self.conv2d = Conv2D(filters=filters, kernel_size=(1, 1), padding="same") if trans_down: self.pool2d = AvgPool2D(pool_size=2, strides=2) else: self.pool2d = UpSampling2D(size=(2, 2))
''' Create the network. ''' factor = 1 print('FACTOR:', factor) ''' Create the network. ''' latent_size = 200 loc = Sequential([ Dense(int(factor * 128) * 7 * 7, input_dim=latent_size), Reshape((7, 7, int(factor * 128))), Conv2D(int(factor * 64), (5, 5), padding='same', kernel_initializer='he_uniform'), LeakyReLU(), BatchNormalization(), UpSampling2D(size=(2, 2), interpolation='bilinear'), ZeroPadding2D((2, 2)), # LocallyConnected2D(int(factor*6), (5, 5), kernel_initializer='he_uniform'), Conv2D(int(factor * 6), (5, 5), kernel_initializer='he_uniform'), LeakyReLU(), BatchNormalization(), UpSampling2D(size=(2, 2), interpolation='bilinear'), # LocallyConnected2D(int(factor*6), (3, 3), kernel_initializer='he_uniform'), Conv2D(int(factor * 6), (3, 3), kernel_initializer='he_uniform'), LeakyReLU(), # LocallyConnected2D(1, (2, 2), use_bias=False, kernel_initializer='glorot_normal'), Conv2D(1, (2, 2), use_bias=False, kernel_initializer='glorot_normal'), Activation('relu') ])
def create_yolov3_model(nb_class, anchors, max_box_per_image, max_grid, batch_size, warmup_batches, ignore_thresh, grid_scales, obj_scale, noobj_scale, xywh_scale, class_scale): input_image = Input(shape=(None, None, 3)) # net_h, net_w, 3 true_boxes = Input(shape=(1, 1, 1, max_box_per_image, 4)) true_yolo_1 = Input( shape=(None, None, len(anchors) // 6, 4 + 1 + nb_class)) # grid_h, grid_w, nb_anchor, 5+nb_class true_yolo_2 = Input( shape=(None, None, len(anchors) // 6, 4 + 1 + nb_class)) # grid_h, grid_w, nb_anchor, 5+nb_class true_yolo_3 = Input( shape=(None, None, len(anchors) // 6, 4 + 1 + nb_class)) # grid_h, grid_w, nb_anchor, 5+nb_class # Layer 0 => 4 x = _conv_block(input_image, [{ 'filter': 32, 'kernel': 3, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 0 }, { 'filter': 64, 'kernel': 3, 'stride': 2, 'bnorm': True, 'leaky': True, 'layer_idx': 1 }, { 'filter': 32, 'kernel': 1, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 2 }, { 'filter': 64, 'kernel': 3, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 3 }]) # Layer 5 => 8 x = _conv_block(x, [{ 'filter': 128, 'kernel': 3, 'stride': 2, 'bnorm': True, 'leaky': True, 'layer_idx': 5 }, { 'filter': 64, 'kernel': 1, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 6 }, { 'filter': 128, 'kernel': 3, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 7 }]) # Layer 9 => 11 x = _conv_block(x, [{ 'filter': 64, 'kernel': 1, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 9 }, { 'filter': 128, 'kernel': 3, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 10 }]) # Layer 12 => 15 x = _conv_block(x, [{ 'filter': 256, 'kernel': 3, 'stride': 2, 'bnorm': True, 'leaky': True, 'layer_idx': 12 }, { 'filter': 128, 'kernel': 1, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 13 }, { 'filter': 256, 'kernel': 3, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 14 }]) # Layer 16 => 36 for i in range(7): x = _conv_block(x, [{ 'filter': 128, 'kernel': 1, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 16 + i * 3 }, { 'filter': 256, 'kernel': 3, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 17 + i * 3 }]) skip_36 = x # Layer 37 => 40 x = _conv_block(x, [{ 'filter': 512, 'kernel': 3, 'stride': 2, 'bnorm': True, 'leaky': True, 'layer_idx': 37 }, { 'filter': 256, 'kernel': 1, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 38 }, { 'filter': 512, 'kernel': 3, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 39 }]) # Layer 41 => 61 for i in range(7): x = _conv_block(x, [{ 'filter': 256, 'kernel': 1, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 41 + i * 3 }, { 'filter': 512, 'kernel': 3, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 42 + i * 3 }]) skip_61 = x # Layer 62 => 65 x = _conv_block(x, [{ 'filter': 1024, 'kernel': 3, 'stride': 2, 'bnorm': True, 'leaky': True, 'layer_idx': 62 }, { 'filter': 512, 'kernel': 1, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 63 }, { 'filter': 1024, 'kernel': 3, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 64 }]) # Layer 66 => 74 for i in range(3): x = _conv_block(x, [{ 'filter': 512, 'kernel': 1, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 66 + i * 3 }, { 'filter': 1024, 'kernel': 3, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 67 + i * 3 }]) # Layer 75 => 79 x = _conv_block(x, [{ 'filter': 512, 'kernel': 1, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 75 }, { 'filter': 1024, 'kernel': 3, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 76 }, { 'filter': 512, 'kernel': 1, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 77 }, { 'filter': 1024, 'kernel': 3, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 78 }, { 'filter': 512, 'kernel': 1, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 79 }], do_skip=False) # Layer 80 => 82 pred_yolo_1 = _conv_block(x, [{ 'filter': 1024, 'kernel': 3, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 80 }, { 'filter': (3 * (5 + nb_class)), 'kernel': 1, 'stride': 1, 'bnorm': False, 'leaky': False, 'layer_idx': 81 }], do_skip=False) loss_yolo_1 = YoloLayer( anchors[12:], [1 * num for num in max_grid], batch_size, warmup_batches, ignore_thresh, grid_scales[0], obj_scale, noobj_scale, xywh_scale, class_scale)([input_image, pred_yolo_1, true_yolo_1, true_boxes]) # Layer 83 => 86 x = _conv_block(x, [{ 'filter': 256, 'kernel': 1, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 84 }], do_skip=False) x = UpSampling2D(2)(x) x = concatenate([x, skip_61]) # Layer 87 => 91 x = _conv_block(x, [{ 'filter': 256, 'kernel': 1, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 87 }, { 'filter': 512, 'kernel': 3, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 88 }, { 'filter': 256, 'kernel': 1, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 89 }, { 'filter': 512, 'kernel': 3, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 90 }, { 'filter': 256, 'kernel': 1, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 91 }], do_skip=False) # Layer 92 => 94 pred_yolo_2 = _conv_block(x, [{ 'filter': 512, 'kernel': 3, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 92 }, { 'filter': (3 * (5 + nb_class)), 'kernel': 1, 'stride': 1, 'bnorm': False, 'leaky': False, 'layer_idx': 93 }], do_skip=False) loss_yolo_2 = YoloLayer( anchors[6:12], [2 * num for num in max_grid], batch_size, warmup_batches, ignore_thresh, grid_scales[1], obj_scale, noobj_scale, xywh_scale, class_scale)([input_image, pred_yolo_2, true_yolo_2, true_boxes]) # Layer 95 => 98 x = _conv_block(x, [{ 'filter': 128, 'kernel': 1, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 96 }], do_skip=False) x = UpSampling2D(2)(x) x = concatenate([x, skip_36]) # Layer 99 => 106 pred_yolo_3 = _conv_block(x, [{ 'filter': 128, 'kernel': 1, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 99 }, { 'filter': 256, 'kernel': 3, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 100 }, { 'filter': 128, 'kernel': 1, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 101 }, { 'filter': 256, 'kernel': 3, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 102 }, { 'filter': 128, 'kernel': 1, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 103 }, { 'filter': 256, 'kernel': 3, 'stride': 1, 'bnorm': True, 'leaky': True, 'layer_idx': 104 }, { 'filter': (3 * (5 + nb_class)), 'kernel': 1, 'stride': 1, 'bnorm': False, 'leaky': False, 'layer_idx': 105 }], do_skip=False) loss_yolo_3 = YoloLayer( anchors[:6], [4 * num for num in max_grid], batch_size, warmup_batches, ignore_thresh, grid_scales[2], obj_scale, noobj_scale, xywh_scale, class_scale)([input_image, pred_yolo_3, true_yolo_3, true_boxes]) train_model = Model( [input_image, true_boxes, true_yolo_1, true_yolo_2, true_yolo_3], [loss_yolo_1, loss_yolo_2, loss_yolo_3]) infer_model = Model(input_image, [pred_yolo_1, pred_yolo_2, pred_yolo_3]) return [train_model, infer_model]
""" Create the network. """ factor = 1 print("FACTOR:", factor) """ Create the network. """ latent_size = 200 loc = Sequential([ Dense(int(factor * 128) * 7 * 7, input_dim=latent_size), Reshape((7, 7, int(factor * 128))), Conv2D(int(factor * 64), (5, 5), padding="same", kernel_initializer="he_uniform"), LeakyReLU(), BatchNormalization(), UpSampling2D(size=(2, 2), interpolation="bilinear"), ZeroPadding2D((2, 2)), # LocallyConnected2D(int(factor*6), (5, 5), kernel_initializer='he_uniform'), Conv2D(int(factor * 6), (5, 5), kernel_initializer="he_uniform"), LeakyReLU(), BatchNormalization(), UpSampling2D(size=(2, 2), interpolation="bilinear"), # LocallyConnected2D(int(factor*6), (3, 3), kernel_initializer='he_uniform'), Conv2D(int(factor * 6), (3, 3), kernel_initializer="he_uniform"), LeakyReLU(), # LocallyConnected2D(1, (2, 2), use_bias=False, kernel_initializer='glorot_normal'), Conv2D(1, (2, 2), use_bias=False, kernel_initializer="glorot_normal"), Activation("relu"), ]) latent = Input(shape=(latent_size, ))