def CapsNet(input_shape, n_class, routings):
    x = layers.Input(shape=input_shape)
    conv1 = layers.Conv2D(filters=64, kernel_size=(1,12), strides=(1,1), padding='same',dilation_rate = 5)(x)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.Conv2D(filters=64, kernel_size=(1,12), strides=(1,2), padding='same',dilation_rate = 1)(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.MaxPooling2D((1, 2), strides=(1, 2))(conv1)
    
    conv1 = layers.Conv2D(filters=96, kernel_size=(1,9), strides=1, padding='same',dilation_rate = 4)(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.Conv2D(filters=96, kernel_size=(1,9), strides=1, padding='same',dilation_rate = 4)(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.MaxPooling2D((1, 2), strides=(1, 2))(conv1)
    
    conv1 = layers.Conv2D(filters=128, kernel_size=(1,6), strides=1, padding='same',dilation_rate = 3)(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.Conv2D(filters=128, kernel_size=(1,6), strides=1, padding='same',dilation_rate = 3)(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.MaxPooling2D((1, 2), strides=(1, 2))(conv1)

    conv1 = layers.Conv2D(filters=192, kernel_size=(1,3), strides=1, padding='same',dilation_rate = 2)(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.Conv2D(filters=192, kernel_size=(1,3), strides=1, padding='same',dilation_rate = 2)(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.Conv2D(filters=192, kernel_size=(1,3), strides=1, padding='same',dilation_rate = 2)(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.MaxPooling2D((1, 2), strides=(1, 2))(conv1)

    conv1 = layers.Conv2D(filters=256, kernel_size=(1,3), strides=1, padding='same',dilation_rate = 2)(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.Conv2D(filters=256, kernel_size=(1,3), strides=1, padding='same',dilation_rate = 2)(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.Conv2D(filters=256, kernel_size=(1,3), strides=1, padding='same',dilation_rate = 2)(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    
    primarycaps = PrimaryCap(conv1, dim_capsule=8, n_channels=32, kernel_size=(1,3),
                             strides=1, padding='same')
    digitcaps = CapsuleLayer(num_capsule=n_class, dim_capsule=args.dim_capsule, routings=routings,
                             name='digitcaps')(primarycaps)
    out_caps = Length(name='capsnet')(digitcaps)
    
    

    model = models.Model(x, out_caps)
    return model
def CapsNet(input_shape, n_class, routings):
    x = layers.Input(shape=input_shape)
    conv1 = layers.Conv2D(filters=256,
                          kernel_size=9,
                          strides=1,
                          padding='valid',
                          activation='relu',
                          name='conv1')(x)
    primarycaps = PrimaryCap(conv1,
                             dim_capsule=8,
                             n_channels=32,
                             kernel_size=9,
                             strides=2,
                             padding='valid')
    digitcaps = CapsuleLayer(num_capsule=n_class,
                             dim_capsule=16,
                             routings=routings,
                             name='digitcaps')(primarycaps)
    out_caps = Length(name='capsnet')(digitcaps)

    y = layers.Input(shape=(2, ))

    masked_by_y = Mask()([digitcaps, y])
    masked_by_y0 = Lambda(lambda x: x[:, 0, :])(masked_by_y)
    masked_by_y1 = Lambda(lambda x: x[:, 1, :])(masked_by_y)

    masked = Mask()(digitcaps)
    masked0 = Lambda(lambda x: x[:, 0, :])(masked)
    masked1 = Lambda(lambda x: x[:, 1, :])(masked)

    decoder = models.Sequential(name='decoder')
    decoder.add(layers.Reshape(target_shape=(4, 4, 1), input_shape=(16, )))
    decoder.add(layers.ZeroPadding2D(((3, 0), (3, 0))))

    decoder.add(layers.UpSampling2D((2, 2)))
    decoder.add(layers.Conv2D(256, 3, activation='relu', padding='same'))
    decoder.add(layers.Conv2D(256, 3, activation='relu', padding='same'))

    decoder.add(layers.UpSampling2D((2, 2)))
    decoder.add(layers.Conv2D(128, 3, activation='relu', padding='same'))
    decoder.add(layers.Conv2D(128, 3, activation='relu', padding='same'))
    decoder.add(layers.Conv2D(1, 1))

    train_model = models.Model(
        [x, y],
        [out_caps, decoder(masked_by_y0),
         decoder(masked_by_y1)])
    eval_model = models.Model(
        x, [out_caps, decoder(masked0),
            decoder(masked1)])
    return train_model, eval_model
Beispiel #3
0
def CapsNet(input_shape, n_class, routings):
    x = layers.Input(shape=input_shape)
    conv1 = layers.Conv2D(filters=64, kernel_size=(1,3), strides=(1,1), padding='same')(x)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.Conv2D(filters=64, kernel_size=(1,3), strides=(1,1), padding='same')(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.MaxPooling2D((1, 2), strides=(1, 2))(conv1)
    
    conv1 = layers.Conv2D(filters=96, kernel_size=(1,3), strides=1, padding='same')(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.Conv2D(filters=96, kernel_size=(1,3), strides=1, padding='same')(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.MaxPooling2D((1, 2), strides=(1, 2))(conv1)
    
    conv1 = layers.Conv2D(filters=128, kernel_size=(1,3), strides=1, padding='same')(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.Conv2D(filters=128, kernel_size=(1,3), strides=1, padding='same')(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.MaxPooling2D((1, 2), strides=(1, 2))(conv1)
    
    conv1 = layers.Conv2D(filters=192, kernel_size=(1,3), strides=1, padding='same')(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.Conv2D(filters=192, kernel_size=(1,3), strides=1, padding='same')(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.MaxPooling2D((1, 2), strides=(1, 2))(conv1)
    
    conv1 = layers.Conv2D(filters=256, kernel_size=(1,3), strides=1, padding='same')(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    conv1 = layers.Conv2D(filters=256, kernel_size=(1,3), strides=1, padding='same')(conv1)
    conv1 = ELU(alpha=0.5)(conv1)
    conv1 = BN()(conv1)
    
    
    
    primarycaps = PrimaryCap(conv1, dim_capsule=8, n_channels=32, kernel_size=(1,3),
                             strides=1, padding='same')
    digitcaps = CapsuleLayer(num_capsule=n_class, dim_capsule=args.dim_capsule, routings=routings,
                             name='digitcaps')(primarycaps)
    out_caps = Length(name='capsnet')(digitcaps)

    y = layers.Input(shape=(2,))

    masked_by_y = Mask()([digitcaps, y])
    masked_by_y0 =  Lambda(lambda x: x[:,0,:])(masked_by_y)
    masked_by_y1 =  Lambda(lambda x: x[:,1,:])(masked_by_y)

    masked = Mask()(digitcaps)
    masked0 =  Lambda(lambda x: x[:,0,:])(masked)
    masked1 =  Lambda(lambda x: x[:,1,:])(masked)
    
    decoder = models.Sequential(name='decoder')
    decoder.add(layers.Dense(375, input_dim=args.dim_capsule))
    decoder.add(ELU(alpha=0.5))
    decoder.add(layers.Reshape(target_shape=(1,375,1), name='out_recon'))
    
    decoder.add(UpSampling2D((1,2)))
    decoder.add(layers.Conv2D(filters=256, kernel_size=(1,3), strides=1, padding='same'))
    decoder.add(ELU(alpha=0.5))
    decoder.add(BN())
    decoder.add(layers.Conv2D(filters=256, kernel_size=(1,3), strides=1, padding='same'))
    decoder.add(ELU(alpha=0.5))
    decoder.add(BN())
    
    decoder.add(UpSampling2D((1,2)))
    decoder.add(layers.Conv2D(filters=128, kernel_size=(1,3), strides=1, padding='same'))
    decoder.add(ELU(alpha=0.5))
    decoder.add(BN())
    decoder.add(layers.Conv2D(filters=128, kernel_size=(1,3), strides=1, padding='same'))
    decoder.add(ELU(alpha=0.5))
    decoder.add(BN())
    
    decoder.add(UpSampling2D((1,2)))
    decoder.add(layers.Conv2D(filters=64, kernel_size=(1,3), strides=1, padding='same'))
    decoder.add(ELU(alpha=0.5))
    decoder.add(BN())
    decoder.add(layers.Conv2D(filters=64, kernel_size=(1,3), strides=1, padding='same'))
    decoder.add(ELU(alpha=0.5))
    decoder.add(BN())
    
    decoder.add(layers.Conv2D(filters=1, kernel_size=(1,1), strides=1, padding='same'))
    
    

    train_model = models.Model([x, y], [out_caps, decoder(masked_by_y0), decoder(masked_by_y1)])
    eval_model = models.Model(x, [out_caps, decoder(masked0), decoder(masked1)])
    return train_model, eval_model
def CapsNet(input_shape, n_class, num_routing):
    from keras import layers, models
    from capsulelayers2 import CapsuleLayer, PrimaryCap, Length, Mask
    from keras.preprocessing import sequence
    """
    A Capsule Network on MNIST.
    :param input_shape: data shape, 4d, [None, width, height, channels]
    :param n_class: number of classes
    :param num_routing: number of routing iterations
    :return: A Keras Model with 2 inputs and 2 outputs
    """
    x = layers.Input(shape=input_shape)

    #    conv1 = layers.Conv1D(filters=256, kernel_size=9, strides=1, padding='valid', activation='relu', name='conv1')(embed)
    conv1 = layers.Conv2D(filters=args.filters,
                          kernel_size=(4, args.kernel_size),
                          strides=(1, 1),
                          padding='valid',
                          activation='relu',
                          name='conv1')(x)
    # conv1 = layers.Dropout(0.1)(conv1)

    # Layer 2: Conv2D layer with `squash` activation, then reshape to [None, num_capsule, dim_vector]
    #    primarycaps = PrimaryCap(conv1, dim_vector=8, n_channels=32, kernel_size=9, strides=2, padding='valid')
    primarycaps = PrimaryCap(conv1,
                             dim_vector=8,
                             n_channels=32,
                             kernel_size=(1, 9),
                             strides=2,
                             padding='valid')

    # Layer 3: Capsule layer. Routing algorithm works here.
    #    digitcaps = CapsuleLayer(num_capsule=n_class, dim_vector=16, num_routing=num_routing, name='digitcaps')(primarycaps)
    digitcaps = CapsuleLayer(num_capsule=n_class,
                             dim_vector=16,
                             num_routing=args.rout,
                             name='digitcaps')(primarycaps)

    # Layer 4: This is an auxiliary layer to replace each capsule with its length. Just to match the true label's shape.
    # If using tensorflow, this will not be necessary. :)
    out_caps = Length(name='out_caps')(digitcaps)

    # Decoder network.
    y = layers.Input(shape=(n_class, ))
    masked_by_y = Mask()(
        [digitcaps, y]
    )  # The true label is used to mask the output of capsule layer. For training
    masked = Mask(
    )(digitcaps)  # 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=16 * 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, 16))
    noised_digitcaps = layers.Add()([digitcaps, 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