Exemple #1
0
def get_container(th, flatten=False):
    assert isinstance(th, Config)
    model = Classifier(mark=th.mark)
    model.add(Input(sample_shape=th.input_shape))
    if th.centralize_data: model.add(Normalize(mu=th.data_mean, sigma=255.))
    if flatten: model.add(Flatten())
    return model
Exemple #2
0
def multinput(th):
    assert isinstance(th, Config)
    model = Classifier(mark=th.mark)

    # Add hidden layers
    assert isinstance(th.fc_dims, list)
    subnet = model.add(inter_type=model.CONCAT)
    for dims in th.fc_dims:
        subsubnet = subnet.add()
        # Add input layer
        subsubnet.add(Input(sample_shape=th.input_shape))
        subsubnet.add(Flatten())
        assert isinstance(dims, list)

        for dim in dims:
            subsubnet.add(Linear(output_dim=dim))
            # if cf10_core.use_bn: subsubnet.add(BatchNormalization())
            subsubnet.add(Activation(th.actype1))

    # Add output layer
    model.add(Linear(output_dim=th.num_classes))

    # Build model
    model.build(metric=['accuracy', 'loss'],
                batch_metric='accuracy',
                eval_metric='accuracy')

    return model
Exemple #3
0
def multinput(th):
    assert isinstance(th, Config)
    model = Classifier(mark=th.mark)

    # Add hidden layers
    assert isinstance(th.fc_dims, list)
    subnet = model.add(inter_type=model.CONCAT)
    for dims in th.fc_dims:
        subsubnet = subnet.add()
        # Add input layer
        subsubnet.add(Input(sample_shape=th.input_shape))
        subsubnet.add(Flatten())
        assert isinstance(dims, list)

        for dim in dims:
            subsubnet.add(Linear(output_dim=dim))
            if core.use_bn: subsubnet.add(BatchNormalization())
            subsubnet.add(Activation(th.actype1))

    # Add output layer
    model.add(Linear(output_dim=th.num_classes))

    # Build model
    optimizer = tf.train.AdamOptimizer(learning_rate=th.learning_rate)
    model.build(optimizer=optimizer)

    return model
Exemple #4
0
def conv_2d_test(th):
    assert isinstance(th, Config)
    # Initiate model
    th.mark = 'cnn_2d' + th.mark

    def data_dim(sample_rate=44100, duration=2, n_mfcc=40):
        audio_length = sample_rate * duration
        dim = (n_mfcc, 1 + int(np.floor(audio_length / 512)), 1)
        return dim

    dim = data_dim()

    model = Classifier(mark=th.mark)

    # Add input layer
    model.add(Input(sample_shape=[dim[0], dim[1], 1]))
    # Add hidden layers
    model.add(Conv2D(32, (4, 10), padding='same'))
    model.add(BatchNorm())
    model.add(Activation('relu'))
    model.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))
    # model.add(Dropout(0.7))

    model.add(Conv2D(32, (4, 10), padding='same'))
    model.add(BatchNorm())
    model.add(Activation('relu'))
    model.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))
    # model.add(Dropout(0.7))

    model.add(Conv2D(32, (4, 10), padding='same'))
    model.add(BatchNorm())
    model.add(Activation('relu'))
    model.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))
    # model.add(Dropout(0.7))

    model.add(Conv2D(32, (4, 10), padding='same'))
    model.add(BatchNorm())
    model.add(Activation('relu'))
    model.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))
    # model.add(Dropout(0.7))

    model.add(Flatten())
    model.add(Linear(output_dim=64))
    model.add(BatchNorm())
    model.add(Activation('relu'))

    # Add output layer
    model.add(Linear(output_dim=41))
    model.add(Activation('softmax'))

    # Build model
    optimizer = tf.train.AdamOptimizer(learning_rate=th.learning_rate)
    model.build(optimizer=optimizer)

    return model
Exemple #5
0
def get_container(th, flatten=False):
    assert isinstance(th, Config)
    model = Classifier(mark=th.mark)
    model.add(Input(sample_shape=th.input_shape))
    model.add(Normalize(sigma=255.))
    if th.centralize_data: model.add(Normalize(mu=th.data_mean))
    if flatten:
        model.add(Flatten())
        # Register extractor and researcher
        model.register_extractor(mn_du.MNIST.connection_heat_map_extractor)
        monitor.register_grad_researcher(mn_du.MNIST.flatten_researcher)
    return model
Exemple #6
0
def deep_conv(mark):
    # Initiate predictor
    model = Classifier(mark=mark)
    model.add(Input(sample_shape=config.sample_shape))

    def ConvBNReLU(filters, strength=1.0, bn=True):
        model.add(
            Conv2D(filters=filters,
                   kernel_size=5,
                   padding='same',
                   kernel_regularizer=regularizers.L2(strength=strength)))

        if bn:
            model.add(BatchNorm())

        model.add(Activation('relu'))

    # Conv layers
    reg = 1e-5
    ConvBNReLU(32, reg)
    model.add(Dropout(0.5))
    ConvBNReLU(32, reg)
    model.add(MaxPool2D(2, 2, 'same'))

    ConvBNReLU(64, reg)
    model.add(Dropout(0.5))
    ConvBNReLU(64, reg)
    model.add(MaxPool2D(2, 2, 'same'))

    ConvBNReLU(128, reg)

    # FC layers
    model.add(Flatten())
    model.add(Linear(256))
    # model.add(BatchNorm())
    model.add(Activation('relu'))

    model.add(Linear(256))
    # model.add(BatchNorm())
    model.add(Activation('relu'))

    model.add(Linear(config.y_dim))

    # Build model
    model.build(optimizer=tf.train.AdamOptimizer(learning_rate=1e-4))

    return model
Exemple #7
0
def mlp00(mark):
    # Define model
    model = TDPlayer(mark=mark)

    model.add(Input(sample_shape=[15, 15]))
    model.add(Flatten())

    model.add(Linear(225))
    model.add(Activation.ReLU())

    model.add(Linear(225))
    model.add(Activation.ReLU())

    model.add(Linear(1))
    model.add(Activation('sigmoid'))

    # Build model
    model.build()

    return model
Exemple #8
0
def mlp(th):
    assert isinstance(th, Config)
    # Initiate a model
    model = Classifier(mark=th.mark)

    # Add input layer
    model.add(Input(sample_shape=th.input_shape))
    model.add(Flatten())
    # Add hidden layers
    for _ in range(th.num_blocks):
        model.add(Linear(output_dim=th.hidden_dim))
        model.add(BatchNormalization())
        model.add(Activation(th.actype1))
    # Add output layer
    model.add(Linear(output_dim=th.num_classes))
    model.add(Activation('softmax'))

    # Build model
    optimizer = tf.train.AdamOptimizer(learning_rate=th.learning_rate)
    model.build(optimizer=optimizer)

    return model
Exemple #9
0
def ka_convnet(mark):
    model = Classifier(mark=mark)
    model.add(Input(sample_shape=config.sample_shape))

    strength = 1e-5

    def ConvLayer(filters, bn=False):
        model.add(
            Conv2D(filters=filters,
                   kernel_size=5,
                   padding='same',
                   kernel_regularizer=regularizers.L2(strength=strength)))
        if bn:
            model.add(BatchNorm())
        model.add(Activation.ReLU())

    # Define structure
    ConvLayer(32)
    model.add(Dropout(0.5))
    ConvLayer(32, False)
    model.add(Dropout(0.5))
    model.add(MaxPool2D(2, 2, 'same'))
    ConvLayer(64, True)
    model.add(Dropout(0.5))
    model.add(MaxPool2D(2, 2, 'same'))

    model.add(Flatten())
    model.add(Linear(128))
    model.add(Activation.ReLU())
    # model.add(Dropout(0.5))
    model.add(Linear(10))

    # Build model
    model.build(optimizer=tf.train.AdamOptimizer(learning_rate=1e-4))

    return model
Exemple #10
0
def dcgan(mark):
    model = GAN(z_dim=100, sample_shape=[32, 32, 3], mark=mark, classes=10)

    nch = 256
    h = 5
    reg = regularizers.L2(strength=1e-7)

    # Define generator
    model.G.add(Linear(output_dim=nch * 4 * 4, weight_regularizer=reg))
    model.G.add(BatchNorm())
    model.G.add(Reshape(shape=(4, 4, nch)))

    model.G.add(
        Deconv2D(filters=int(nch / 2),
                 kernel_size=5,
                 padding='same',
                 kernel_regularizer=reg))
    model.G.add(BatchNorm())
    model.G.add(Activation.LeakyReLU())

    model.G.add(
        Deconv2D(filters=int(nch / 2),
                 kernel_size=5,
                 strides=2,
                 padding='same',
                 kernel_regularizer=reg))
    model.G.add(BatchNorm())
    model.G.add(Activation.LeakyReLU())

    model.G.add(
        Deconv2D(filters=int(nch / 4),
                 kernel_size=5,
                 strides=2,
                 padding='same',
                 kernel_regularizer=reg))
    model.G.add(BatchNorm())
    model.G.add(Activation.LeakyReLU())

    model.G.add(
        Deconv2D(filters=3,
                 kernel_size=5,
                 strides=2,
                 padding='same',
                 kernel_regularizer=reg))
    model.G.add(Activation('sigmoid'))

    # ===========================================================================

    # Define discriminator
    model.D.add(
        Conv2D(filters=int(nch / 4),
               kernel_size=h,
               strides=2,
               padding='same',
               kernel_regularizer=reg))
    model.D.add(Activation.LeakyReLU())

    model.D.add(
        Conv2D(filters=int(nch / 2),
               kernel_size=h,
               strides=2,
               padding='same',
               kernel_regularizer=reg))
    model.D.add(Activation.LeakyReLU())

    model.D.add(
        Conv2D(filters=nch,
               kernel_size=h,
               strides=2,
               padding='same',
               kernel_regularizer=reg))
    model.D.add(Activation.LeakyReLU())

    model.D.add(Flatten())
    model.D.add(Linear(output_dim=1, weight_regularizer=reg))
    model.D.add(Activation('sigmoid'))

    # Build model
    optimizer = tf.train.AdamOptimizer(learning_rate=0.0002, beta1=0.5)
    model.build(loss=pedia.cross_entropy,
                G_optimizer=optimizer,
                D_optimizer=optimizer)

    return model
Exemple #11
0
def multinput_ver_only(th):
    assert isinstance(th, Config)
    # model = Classifier(mark=th.mark)
    model = Classifier_Gpat(mark=th.mark)

    def data_dim(sample_rate=16000, duration=2, n_mfcc=50):
        audio_length = sample_rate * duration
        dim = (n_mfcc, 1 + int(np.floor(audio_length / 512)), 1)
        return dim

    dim = data_dim()

    # Add hidden layers
    subnet = model.add(inter_type=model.CONCAT)
    subsubnet = subnet.add()
    # the net to process raw data
    subsubnet.add(Input(sample_shape=[32000, 1]))

    def conv_bn_relu(filters, twod=True, bn=True):
        if twod:
            subsubnet.add(
                Conv2D(filters=filters, kernel_size=(4, 10), padding='same'))
        else:
            subsubnet.add(
                Conv1D(filters=filters, kernel_size=9, padding='valid'))
        if bn: subsubnet.add(BatchNorm())
        subsubnet.add(Activation('relu'))

    def maxpool_drop(pool_size, strides, twod=True, drop=True):
        if twod:
            subsubnet.add(MaxPool2D(pool_size=pool_size, strides=strides))
        else:
            subsubnet.add(MaxPool1D(pool_size=pool_size, strides=strides))
        if drop: subsubnet.add(Dropout(th.raw_keep_prob))

    for _ in range(th.raw_std_blocks):
        conv_bn_relu(32, twod=False, bn=True)
        maxpool_drop(pool_size=16, strides=16, drop=True, twod=False)
    conv_bn_relu(filters=32, twod=False, bn=True)
    subsubnet.add(Dropout(th.raw_keep_prob))
    subsubnet.add(GlobalMaxPooling1D())

    # the net to process mfcc features
    subsubnet = subnet.add()
    subsubnet.add(Input(sample_shape=[dim[0], dim[1], 1], name='mfcc'))
    for _ in range(th.mfcc_std_blocks):
        conv_bn_relu(filters=th.mfcc_cnn_filters, bn=True)
        maxpool_drop(pool_size=(2, 2), strides=(2, 2))
    subsubnet.add(Flatten())

    model.add(Dropout(th.concat_keep_prob))

    def linear_bn_relu(units, bn=True):
        model.add(Linear(output_dim=units))
        if bn: model.add(BatchNorm())
        model.add(Activation('relu'))

    for _ in range(th.concat_std_blocks):
        linear_bn_relu(th.concat_part_units)

    # Add output layer
    model.add(Linear(output_dim=41))
    model.add(Activation('softmax'))

    # Build model
    optimizer = tf.train.AdamOptimizer(learning_rate=th.learning_rate)
    model.build(optimizer=optimizer)

    return model
Exemple #12
0
def res_00(th):
    assert isinstance(th, Config)
    model = Classifier(mark=th.mark)

    def data_dim(sample_rate=16000, duration=2, n_mfcc=50):
        audio_length = sample_rate * duration
        dim = (n_mfcc, 1 + int(np.floor(audio_length / 512)), 1)
        return dim

    dim = data_dim()

    # Add hidden layers
    subnet = model.add(inter_type=model.CONCAT)
    # the net to process raw data
    subsubnet = subnet.add()
    # subsubnet.add(Input(sample_shape=[32000, 1], name='raw_data'))
    subsubnet.add(Input(sample_shape=[32000, 1]))
    subsubnet.add(Conv1D(filters=16, kernel_size=9, padding='valid'))
    subsubnet.add(BatchNorm())
    subsubnet.add(Activation('relu'))
    subsubnet.add(Conv1D(filters=16, kernel_size=9, padding='valid'))
    subsubnet.add(BatchNorm())
    subsubnet.add(Activation('relu'))
    subsubnet.add(MaxPool1D(pool_size=16, strides=16))
    subsubnet.add(Dropout(th.raw_keep_prob))

    subsubnet.add(Conv1D(filters=32, kernel_size=3, padding='valid'))
    subsubnet.add(Activation('relu'))
    subsubnet.add(Conv1D(filters=32, kernel_size=3, padding='valid'))
    subsubnet.add(Activation('relu'))
    subsubnet.add(MaxPool1D(pool_size=4, strides=4))
    subsubnet.add(Dropout(th.raw_keep_prob))

    subsubnet.add(Conv1D(filters=32, kernel_size=3, padding='valid'))
    subsubnet.add(Activation('relu'))
    subsubnet.add(Conv1D(filters=32, kernel_size=3, padding='valid'))
    subsubnet.add(Activation('relu'))
    subsubnet.add(MaxPool1D(pool_size=4, strides=4))

    subsubnet.add(Conv1D(filters=256, kernel_size=3, padding='valid'))
    subsubnet.add(BatchNorm())
    subsubnet.add(Activation('relu'))
    subsubnet.add(Conv1D(filters=256, kernel_size=3, padding='valid'))
    subsubnet.add(BatchNorm())
    subsubnet.add(Activation('relu'))
    subsubnet.add(GlobalMaxPooling1D())

    # the net to process mfcc features
    subsubnet = subnet.add()
    subsubnet.add(Input(sample_shape=[dim[0], dim[1], 1], name='mfcc'))
    subsubnet.add(Conv2D(32, (4, 10), padding='same'))
    subsubnet.add(BatchNorm())
    subsubnet.add(Activation('relu'))
    subsubnet.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))
    subsubnet.add(Dropout(th.mfcc_keep_prob))

    net = subsubnet.add(ResidualNet())
    net.add(Conv2D(32, (4, 10), padding='same'))
    net.add(BatchNorm())
    net.add_shortcut()
    subsubnet.add(Activation('relu'))
    subsubnet.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))
    subsubnet.add(Dropout(th.mfcc_keep_prob))
    #
    net = subsubnet.add(ResidualNet())
    net.add(Conv2D(32, (4, 10), padding='same'))
    net.add(BatchNorm())
    net.add_shortcut()
    subsubnet.add(Activation('relu'))
    subsubnet.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))
    subsubnet.add(Dropout(th.mfcc_keep_prob))

    net = subsubnet.add(ResidualNet())
    net.add(Conv2D(32, (4, 10), padding='same'))
    net.add(BatchNorm())
    net.add_shortcut()
    subsubnet.add(Activation('relu'))
    subsubnet.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))
    subsubnet.add(Dropout(th.mfcc_keep_prob))

    net = subsubnet.add(ResidualNet())
    net.add(Conv2D(32, (4, 10), padding='same'))
    net.add(BatchNorm())
    net.add_shortcut()
    subsubnet.add(Activation('relu'))
    subsubnet.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))
    subsubnet.add(Dropout(th.mfcc_keep_prob))
    subsubnet.add(Flatten())

    subsubnet.add(Dropout(th.concat_keep_prob))
    model.add(Linear(output_dim=128))
    model.add(BatchNorm())
    model.add(Activation('relu'))
    #
    model.add(Linear(output_dim=64))
    model.add(BatchNorm())
    model.add(Activation('relu'))

    # Add output layer
    model.add(Linear(output_dim=41))
    model.add(Activation('softmax'))

    # Build model
    optimizer = tf.train.AdamOptimizer(learning_rate=th.learning_rate)
    model.build(optimizer=optimizer)

    return model
Exemple #13
0
def multinput_mlp(th):
    assert isinstance(th, Config)
    model = Classifier(mark=th.mark)

    def data_dim(sample_rate=16000, duration=2, n_mfcc=50):
        audio_length = sample_rate * duration
        dim = (n_mfcc, 1 + int(np.floor(audio_length / 512)), 1)
        return dim

    dim = data_dim()

    # Add hidden layers
    subnet = model.add(inter_type=model.CONCAT)

    subsubnet = subnet.add()
    subsubnet.add(Input(sample_shape=[32000, 1]))
    subsubnet.add(Linear(output_dim=512))
    subsubnet.add(Activation('relu'))
    subsubnet.add(Linear(output_dim=256))
    subsubnet.add(Activation('relu'))

    subsubnet = subnet.add()
    subsubnet.add(Input(sample_shape=[dim[0], dim[1], 1], name='mfcc'))
    subsubnet.add(Conv2D(32, (4, 10), padding='same'))
    subsubnet.add(BatchNorm())
    subsubnet.add(Activation('relu'))
    subsubnet.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))
    subsubnet.add(Dropout(0.8))

    # subsubnet.add(Conv2D(32, (4, 10), padding='same'))
    # subsubnet.add(BatchNorm())
    # subsubnet.add(Activation('relu'))
    # subsubnet.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))
    # subsubnet.add(Dropout(0.8))

    subsubnet.add(Conv2D(32, (4, 10), padding='same'))
    subsubnet.add(BatchNorm())
    subsubnet.add(Activation('relu'))
    subsubnet.add(MaxPool2D(pool_size=(2, 2), strides=(2, 2)))
    subsubnet.add(Dropout(0.7))

    subsubnet.add(Flatten())

    model.add(Linear(output_dim=128))
    model.add(BatchNorm())
    model.add(Activation('relu'))

    model.add(Linear(output_dim=64))
    model.add(BatchNorm())
    model.add(Activation('relu'))

    model.add(Linear(output_dim=64))
    model.add(BatchNorm())
    model.add(Activation('relu'))

    # Add output layer
    model.add(Linear(output_dim=41))
    model.add(Activation('softmax'))

    # Build model
    optimizer = tf.train.AdamOptimizer(learning_rate=th.learning_rate)
    model.build(optimizer=optimizer)

    return model