예제 #1
0
    config = args.parse_args()

    N_CLASSES = 11

    # 1. Loading a saved model
    x = tf.keras.layers.Input(shape=(257, None, 4))
    model = getattr(model, config.model)(
        weights=None,
        input_tensor=x,
        classes=N_CLASSES,
        backend=tf.keras.backend,
        layers=tf.keras.layers,
        models=tf.keras.models,
        utils=tf.keras.utils,
    )
    model.load_weights(config.name)

    if config.verbose:
        model.summary()

    PATH = '/media/data1/datasets/ai_challenge/icassp/'

    # 2. loading evaluation dataset
    if config.dataset == 'challenge':
        eval_x = np.load(os.path.join(PATH, 'final_x.npy'))
        eval_y = np.load(os.path.join(PATH, 'final_y.npy'))
    else:
        eval_x = np.concatenate([
            np.load(os.path.join(PATH, 'test_x.npy')),
            np.load(os.path.join(PATH, 'noise_test_x.npy'))
        ],
예제 #2
0
            out = tf.keras.layers.Flatten()(out)
            out = tf.keras.layers.ReLU()(out)
        else:
            for i in range(config.n_layers):
                # out = tf.keras.layers.Dropout(0.1)(out)
                out = tf.keras.layers.Dense(config.n_dim)(out)
                out = tf.keras.layers.Activation('sigmoid')(out) * out

    out = tf.keras.layers.Dense(config.n_classes, activation='relu')(out)
    model = tf.keras.models.Model(inputs=model.input, outputs=out)

    specs = None
    for name in config.name.split(','):
        NAME = name if name.endswith('.h5') else name + '.h5'
        model.load_weights(NAME)

        if specs is None:
            specs = model.predict(wavs, verbose=True)
        else:
            specs += model.predict(wavs, verbose=True)
    specs /= len(config.name.split(','))

    specs = specs / config.multiplier
    specs = tf.reshape(specs, [*specs.shape[:2], 3, 10])

    angles = tf.cast(tf.round(tf.reduce_sum(specs, axis=(1, 2))), tf.int32)
    classes = tf.cast(tf.round(tf.reduce_sum(specs, axis=(1, 3))), tf.int32)

    d_dir = D_direction(tf.cast(gt_angle, tf.float32),
                        tf.cast(angles, tf.float32))
예제 #3
0
def main(config):
    wavs = load_wav(config)

    import tensorflow as tf
    import efficientnet.model as model
    x = tf.keras.layers.Input(shape=(config.n_mels, None, 2))
    model = getattr(model, config.model)(
        include_top=False,
        weights=None,
        input_tensor=x,
        backend=tf.keras.backend,
        layers=tf.keras.layers,
        models=tf.keras.models,
        utils=tf.keras.utils,
    )
    out = tf.transpose(model.output, perm=[0, 2, 1, 3])
    out = tf.keras.layers.Reshape([-1, out.shape[-1] * out.shape[-2]])(out)

    if config.n_layers > 0:
        if config.mode == 'GRU':
            out = tf.keras.layers.Dense(config.n_dim)(out)
            for i in range(config.n_layers):
                # out = transformer_layer(config.n_dim, config.n_heads)(out)
                out = tf.keras.layers.Bidirectional(
                    tf.keras.layers.GRU(config.n_dim, return_sequences=True),
                    backward_layer=tf.keras.layers.GRU(config.n_dim,
                                                       return_sequences=True,
                                                       go_backwards=True))(out)
        elif config.mode == 'transformer':
            out = tf.keras.layers.Dense(config.n_dim)(out)
            out = encoder(config.n_layers, config.n_dim, config.n_heads)(out)

            out = tf.keras.layers.Flatten()(out)
            out = tf.keras.layers.ReLU()(out)

    out = tf.keras.layers.Dense(config.n_classes, activation='relu')(out)
    model = tf.keras.models.Model(inputs=model.input, outputs=out)

    model.load_weights('model.h5')

    wavs = model.predict(wavs)
    wavs = wavs / config.multiplier
    wavs = tf.reshape(wavs, [*wavs.shape[:2], 3, 10])

    angles = tf.cast(tf.round(tf.reduce_sum(wavs, axis=(1, 2))), tf.int8)
    classes = tf.cast(tf.round(tf.reduce_sum(wavs, axis=(1, 3))), tf.int8)

    # import numpy as np
    # from glob import glob
    # num = len(sorted(glob('dataset/3rd_track3/*.wav')))
    # angles = tf.convert_to_tensor(np.zeros((num, 10), dtype=np.int))
    # classes = tf.convert_to_tensor(np.ones((num, 3), dtype=np.int) * 2)

    data = {'track3_results': list()}
    for idx, (ag, cl) in enumerate(zip(angles, classes)):

        _data = {
            'id': idx,
            'angle': ag.numpy().tolist(),
            'class': cl.numpy().tolist()
        }
        data['track3_results'].append(_data)
    tojson(data)