def f(input_x):
     x = self.first_layer(input_x, scope='first_layer')
     features = []
     filters = self.init_filters
     self.filter_list = [filters]
     # First Layer consumed one stage
     for i in range(repetitions):
         print('\nBuilding ... %d/%d' % (i, repetitions))
         # Get Downsample
         scope = 'stage_%d' % (i + 1)
         if i == 0:
             down_x = self.residual_layer(filters, scope=scope, first_layer_stride=(2, 2), res_block=self.blocks)(x)
         else:
             down_x = self.residual_layer(filters, scope=scope, first_layer_stride=(2, 2), res_block=self.blocks)(features[-1])
         features.append(down_x)
         # Get concatenated feature maps
         out_maps = self.multi_resolution_concat(features, self.filter_list)
         features = []
         print('Identity Mapping:')
         # Residual connection with 3x3 kernel, 1x1 stride with same number of filters
         for idx, (fm, num_filter) in enumerate(zip(out_maps, self.filter_list)):
             x = Lambda(lambda x: x, output_shape=x.get_shape().as_list())(fm)
             print(idx, x)
             features.append(x)
         filters *= 2
         self.filter_list.append(filters)
     return features
Пример #2
0
def SP_ResNet(num_classes,
              input_shape,
              depths=[2, 2, 2, 2],
              filters=[64, 128, 256, 512],
              pool_at=[0, 1, 2, 3],
              squeeze_ratio=16,
              use_residuals=True,
              dense_layers=[],
              dropout_rate=None):
    # ...

    input_img = Input(shape=input_shape, name='input')

    # entry conv + pool
    x = Conv2D(filters[0], (7, 7),
               strides=(2, 2),
               padding='same',
               activation=None,
               name='entry_conv')(input_img)
    x = BatchNormalization(name='entry_bn')(x)
    x = Activation('relu', name='entry_relu')(x)
    x = MaxPool2D((3, 3), strides=(2, 2), padding='same')(x)

    pooling_outputs = []
    for i, (f, d) in enumerate(zip(filters, depths)):
        # n_blocks = depth
        for n in range(d):
            downsample = True if n == 0 else False
            x, z = SP_block(x,
                            f,
                            str(i) + '_' + str(n),
                            ratio=squeeze_ratio,
                            residual=use_residuals,
                            downsample=downsample)

        # only pool at last block in depth
        if i in pool_at and z is not None:
            z = Lambda(bilinear_pooling,
                       name='bilinear_pooling' + str(i))([z, z])
            pooling_outputs.append(z)
            print(z.get_shape().as_list())

    x = GlobalAveragePooling2D(name='global_pooling_top')(x)
    pooling_outputs.append(x)
    x = Concatenate(name='feature_concat')(pooling_outputs)

    x = make_dense_layers(dense_layers, dropout=dropout_rate)(x)

    pred = Dense(num_classes, activation='softmax')(x)

    model = KerasModel(inputs=input_img, outputs=pred)

    return model
Пример #3
0
    def calculate_voxel_coordinates(self, points):
        # points - (B,N,3) batched point coordinates
        #
        # returns - (B,N,V,3) batched V voxel center coordinates placed in ball around each of N points

        sample_radius = self.net_config['voxel_sampling_radius']
        voxel_size = self.net_config['voxel_size']

        def fun(xyz, s_radius, v_size):

            n = s_radius // v_size
            steps = list(np.arange(-n * v_size, (n + 1) * v_size, v_size))
            translations = []

            for i in steps:
                for j in steps:
                    for k in steps:
                        p = np.array([i * v_size, j * v_size, k * v_size])
                        dist = np.sqrt(np.sum(p**2))
                        if dist <= s_radius:
                            translations.append(p.tolist())

            offsets = tf.constant(np.array(translations))

            voxel_count = tf.shape(offsets)[0]
            b_size = tf.shape(xyz)[0]
            xyz_count = tf.shape(xyz)[1]
            xyz_expanded = tf.expand_dims(xyz, axis=2)
            xyz_repeated = tf.repeat(xyz_expanded, voxel_count, axis=2)
            offsets_expanded = tf.expand_dims(tf.expand_dims(offsets, axis=0),
                                              axis=0)
            offsets_repeated = tf.repeat(tf.repeat(offsets_expanded,
                                                   b_size,
                                                   axis=0),
                                         xyz_count,
                                         axis=1)

            return tf.cast(offsets_repeated, tf.float32) + xyz_repeated

        voxels = Lambda(lambda x: fun(x, sample_radius, voxel_size),
                        name="calculate_voxel_centers")(points)
        self.voxel_dim_size = voxels.get_shape()[2]
        return voxels
Пример #4
0
def LaneCapsNet(input_shape,
                n_class,
                routings,
                num_lanes=4,
                lanesize=1,
                lanedepth=1,
                lanetype=1,
                gpus=1):
    x = layers.Input(shape=input_shape, batch_size=args.batch_size)

    lanes = []
    for i in range(0, num_lanes):
        if (gpus != 0):
            with tf.device("/gpu:%d" % (i % gpus)):
                lanes = lanes + [
                    Lane(i,
                         n_class,
                         lanesize,
                         lanetype,
                         x,
                         routings,
                         stacked=lanedepth)
                ]
        else:
            lanes = lanes + [
                Lane(i,
                     n_class,
                     lanesize,
                     lanetype,
                     x,
                     routings,
                     stacked=lanedepth)
            ]

    digitcaps1 = Lambda(lambda ls: K.permute_dimensions(
        concatenate(ls, axis=1), [0, 2, 1]))(lanes)

    digitcaps = layers.Dropout(args.dropout,
                               (1, digitcaps1.get_shape()[2]))(digitcaps1)

    out_caps = Length(name='capsnet')(digitcaps)

    # Decoder network.
    y = layers.Input(shape=(n_class, ))
    masked_by_y = Mask()(
        [digitcaps1, y]
    )  # The true label is used to mask the output of capsule layer. For training
    masked = Mask(
    )(digitcaps1)  # Mask using the capsule with maximal length. For prediction

    # Shared Decoder model in training and prediction
    decoder = models.Sequential(name='decoder')
    decoder.add(
        layers.Dense(512, activation='relu', input_dim=num_lanes * n_class))
    decoder.add(layers.Dense(1024, activation='relu'))
    decoder.add(layers.Dense(np.prod(input_shape), activation='sigmoid'))
    decoder.add(layers.Reshape(target_shape=input_shape, name='out_recon'))

    # Models for training and evaluation (prediction)
    train_model = models.Model([x, y], [out_caps, decoder(masked_by_y)])
    eval_model = models.Model(x, [out_caps, decoder(masked)])

    # manipulate model
    noise = layers.Input(shape=(n_class, num_lanes))
    noised_digitcaps = layers.Add()([digitcaps1, noise])
    masked_noised_y = Mask()([noised_digitcaps, y])
    manipulate_model = models.Model([x, y, noise], decoder(masked_noised_y))
    return train_model, eval_model, manipulate_model