예제 #1
0
    def test_training_model_bka_to_step_12_c(self):
        from keras import Model
        from dlnn.tests.ml.cnn_func_test import inputs
        from dlnn.util import MoorePenrose
        #
        # Feed Beta
        #
        feed = Model(inputs=inputs, outputs=step_11_c_activation)
        output = feed.predict(normalized)
        w_10_b = feed.get_layer(index=10).get_weights()
        w_12_b = [K.eval(K.dot(MoorePenrose.pinv3(output), K.variable(categorical_label_init)))]
        for i in range(12):
            layer = feed.get_layer(index=i).get_weights()
            self.assertIsNotNone(layer)
            # print("step_%d" % (i + 1), layer)

        #
        # Training Model
        #
        network = Model(inputs=inputs, outputs=step_12_c_dense)
        network.compile(optimizer=keras.optimizers.RMSprop(lr=0.0, rho=0.0, epsilon=None, decay=0.0),
                        loss=keras.losses.categorical_crossentropy,
                        metrics=[keras.metrics.categorical_accuracy, keras.metrics.mape])
        network.get_layer(index=10).set_weights(w_10_b)
        network.get_layer(index=12).set_weights(w_12_b)
        network.fit(normalized, categorical_label_init, batch_size=normalized.shape[0])
        self.assertTrue(numpy.allclose(w_10_b[0], network.get_layer(index=10).get_weights()[0], rtol=0))
        self.assertTrue(numpy.allclose(w_12_b[0], network.get_layer(index=12).get_weights()[0], rtol=0))
        result = network.predict(normalized, batch_size=normalized.shape[0])
        self.assertIsNotNone(result)
예제 #2
0
def resnet_50(input_shape):
    img_input = Input(input_shape)
    x = Conv2D(64, (7, 7), strides=(2, 2), padding='same',
               name='conv1')(img_input)
    if input_shape[-1] > 3:
        x = Conv2D(64, (7, 7),
                   strides=(2, 2),
                   padding='same',
                   name='conv1_changed')(img_input)
    x = BatchNormalization(name='bn_conv1')(x)
    x = Activation('relu')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), padding="same")(x)

    x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')

    x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')

    x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')

    x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')

    print("Loading pretrained weights for Resnet50...")
    weights_path = get_file(
        'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5',
        resnet50_padding.WEIGHTS_PATH_NO_TOP,
        cache_subdir='models',
        md5_hash='a268eb855778b3df3c7506639542a6af')
    model = Model(img_input, x)
    model.load_weights(weights_path, by_name=True)
    if input_shape[-1] > 3:
        print(
            "Loading weights for conv1 layer separately for the first 3 channels"
        )
        conv1_weights = np.zeros((7, 7, input_shape[-1], 64), dtype="float32")
        resnet_ori = ResNet50(include_top=False, input_shape=(224, 224, 3))
        conv1_weights[:, :, :3, :] = resnet_ori.get_layer(
            "conv1").get_weights()[0][:, :, :, :]
        # random init
        conv1_weights[:, :, 3:, :] = model.get_layer(
            'conv1_changed').get_weights()[0][:, :, 3:, :]
        bias = resnet_ori.get_layer("conv1").get_weights()[1]
        model.get_layer('conv1_changed').set_weights((conv1_weights, bias))
        model.get_layer('conv1_changed').name = 'conv1'

    return model
예제 #3
0
def get_model_cut_at(model: Model, cut_layer_name):
    cut_layer_output = model.get_layer(cut_layer_name).output
    cut_model = Model(inputs=[
        model.get_layer("forward_image_input").input,
        model.get_layer("info_input").input,
        model.get_layer("hlc_input").input
    ],
                      outputs=cut_layer_output)
    return cut_model
예제 #4
0
 def test_check_weights_layer(self):
     from keras import Model
     from dlnn.tests.ml.cnn_func_test import inputs
     network = Model(inputs=inputs, outputs=step_11_a_activation)
     network.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
     for i in range(12):
         layer = network.get_layer(index=i).get_weights()
         self.assertIsNotNone(layer)
         # print("step_%d" % (i + 1), layer)
     network.predict(normalized)
     for i in range(12):
         layer = network.get_layer(index=i).get_weights()
         self.assertIsNotNone(layer)
예제 #5
0
def test_predict(net, probe_path, gallery_path, pid_path, score_path):
    net = Model(inputs=[net.get_layer('resnet50').get_input_at(0)],
                outputs=[net.get_layer('resnet50').get_output_at(0)])
    # net = Model(inputs=[net.input], outputs=[net.get_layer('avg_pool').output])
    test_f, test_info = extract_feature(gallery_path, net)
    query_f, query_info = extract_feature(probe_path, net)
    result, result_argsort = sort_similarity(query_f, test_f)
    for i in range(len(result)):
        result[i] = result[i][result_argsort[i]]
    result = np.array(result)
    safe_remove(pid_path)
    safe_remove(score_path)
    np.savetxt(pid_path, result_argsort, fmt='%d')
    np.savetxt(score_path, result, fmt='%.4f')
예제 #6
0
def resnet_50(input_shape):
    img_input = Input(input_shape)
    x = Conv2D(64, (7, 7), strides=(2, 2), padding='same', name='conv1')(img_input)
    if input_shape[-1] > 3:
        x = Conv2D(64, (7, 7), strides=(2, 2), padding='same', name='conv1_changed')(img_input)
    x = BatchNormalization(name='bn_conv1')(x)
    x = Activation('relu')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), padding="same")(x)

    x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')

    x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')

    x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')

    x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')

    print("Loading pretrained weights for Resnet50...")
    weights_path = get_file('resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5',
                            resnet50_padding.WEIGHTS_PATH_NO_TOP,
                            cache_subdir='models',
                            md5_hash='a268eb855778b3df3c7506639542a6af')
    model = Model(img_input, x)
    model.load_weights(weights_path, by_name=True)
    if input_shape[-1] > 3:
        print("Loading weights for conv1 layer separately for the first 3 channels")
        conv1_weights = np.zeros((7, 7, input_shape[-1], 64), dtype="float32")
        resnet_ori = ResNet50(include_top=False, input_shape=(224, 224, 3))
        conv1_weights[:, :, :3, :] = resnet_ori.get_layer("conv1").get_weights()[0][:, :, :, :]
        # random init
        conv1_weights[:, :, 3:, :] = model.get_layer('conv1_changed').get_weights()[0][:, :, 3:, :]
        bias = resnet_ori.get_layer("conv1").get_weights()[1]
        model.get_layer('conv1_changed').set_weights((conv1_weights, bias))
        model.get_layer('conv1_changed').name = 'conv1'

    return model
예제 #7
0
def build_cnn():

    vgg_inst = vgg19.VGG19(include_top=True,
                           weights='imagenet',
                           input_tensor=None,
                           input_shape=(224, 224, 3),
                           pooling=None,
                           classes=1000)

    x = vgg_inst.output
    x = Dense(64, activation="relu")(x)
    x = Dense(2, activation="softmax")(x)

    model = Model(inputs=vgg_inst.inputs, outputs=x)

    for i in model.layers:
        i.trainable = False

    trainable_layers = [
        "block3_pool", "block4_conv1", "block4_conv2", "block4_conv3",
        "block4_conv4", "block4_pool", "block5_conv1", "block5_conv2",
        "block5_conv3", "block5_conv4", "block5_pool", "flatten", "fc1", "fc2",
        "predictions", "dense_1", "dense_2"
    ]

    for i in trainable_layers:
        model.get_layer(i).trainable = True

    sgd = SGD(lr=1e-6, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(optimizer=sgd,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    return model
예제 #8
0
def build_combined_conv_model(word_indices, word_vectors, output_dim):
    learning_rate = args.learning_rate

    pos_model, _ = build_pos_sequence_conv_model(output_dim=64)
    word_model, _ = build_word_sequence_conv_model(word_indices,
                                                   word_vectors,
                                                   output_dim=64)
    char_model, _ = build_char_sequence_conv_model(output_dim=64)

    mrg = Concatenate(name='final_layer_mrg')(
        [pos_model.output, word_model.output, char_model.output])
    dense = Dense(output_dim, activation='softmax')(mrg)

    model = Model(inputs=[pos_model.input, word_model.input, char_model.input],
                  outputs=dense)
    print("\tLearning rate:", learning_rate)
    rmsprop = RMSprop(lr=learning_rate)

    model.compile(optimizer=rmsprop,
                  loss='categorical_crossentropy',
                  metrics=['categorical_accuracy'])
    final_layer = Model(inputs=model.input,
                        outputs=model.get_layer('final_layer_mrg').output)

    return model, final_layer
예제 #9
0
def build_char_sequence_conv_model(output_dim):
    char_doc_input = Input(shape=(max_num_of_sentences, max_num_of_chars), dtype='int64')
    char_sent_input = Input(shape=(max_num_of_chars,), dtype='int64')

    embedded = Lambda(binarize_char, output_shape=binarize_char_outshape)(char_sent_input)

    block2 = char_block(embedded, (128, 256), filter_length=(5, 5), subsample=(1, 1), pool_length=(2, 2))
    block3 = char_block(embedded, (192, 320), filter_length=(7, 5), subsample=(1, 1), pool_length=(2, 2))

    sent_encode = concatenate([block2, block3], axis=-1)
    # sent_encode = Dropout(0.2)(sent_encode)

    encoder = Model(inputs=char_sent_input, outputs=sent_encode)
    encoder.summary()

    encoded = TimeDistributed(encoder)(char_doc_input)

    lstm_h = 92

    lstm_layer = LSTM(lstm_h, return_sequences=True, dropout=0.1, recurrent_dropout=0.1, implementation=0)(encoded)
    lstm_layer2 = LSTM(lstm_h, return_sequences=False, dropout=0.1, recurrent_dropout=0.1, implementation=0,
                       name='final_layer_char')(lstm_layer)

    # output = Dropout(0.2)(bi_lstm)
    output = Dense(output_dim, activation='softmax')(lstm_layer2)

    model = Model(outputs=output, inputs=char_doc_input)
    optimizer = 'rmsprop'
    model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['categorical_accuracy'])
    final_layer = Model(inputs=model.input, outputs=model.get_layer('final_layer_char').output)

    return model, final_layer
예제 #10
0
class Autoencoder:
    def __init__(self, n_items, n_users, k=80, verbose=True):
        self.n_users = n_users
        self.n_items = n_items
        self.k = k
        self.verbose = verbose
        self._compile()

    def _compile(self):
        layer_item_id = Input(shape=(1,), name='item_id')

        layer_item_vector_compressed = Embedding(self.n_items, self.k, name='items_vectors')

        layer_item_ratings = Dense(self.n_users)(layer_item_vector_compressed(layer_item_id))

        self.model = Model(layer_item_id, layer_item_ratings)

        self.model.compile('adam', 'mse')

    def train(self, rating_matrix, epochs=80):
        x_train = np.arange(self.n_items)
        y_train = rating_matrix.T.reshape(self.n_items, 1, self.n_users)

        self.model.fit(x=x_train, y=y_train, epochs=epochs, verbose=(1 if self.verbose else 0), validation_split=0)

        self.items_vectors = self.model.get_layer(name='items_vectors').get_weights()[0]

    def recommend(self, index_item, n):
        return KNN(index_item, self.items_vectors, n, cosine=False)
예제 #11
0
    def create_prediction_model(self,
                                model: Model,
                                pyramids: List[str] = ('P3', 'P4', 'P5', 'P6',
                                                       'P7'),
                                use_nms=True,
                                name='retinanet-prediction'):
        # Get Pyramid Features
        pyramids = list(map(lambda p: p.lower(), pyramids))
        pyramid_features = [
            model.get_layer(p_name).output for p_name in pyramids
        ]

        anchors = self.generate_anchors(pyramid_features)

        clf_output = model.outputs[0]  # (1, points 360360, n_labels)
        reg_output = model.outputs[1]  # (1, points 360360, 4)

        boxes = RegressBoxes(name='boxes')([anchors, reg_output])
        boxes = ClipBoxes(name='clipped_boxes')([model.inputs[0], boxes])

        # Apply NMS / Score threshold / Select top-k
        outputs = FilterDetections(nms=use_nms,
                                   parallel_iterations=128,
                                   name='nms_filter')([boxes, clf_output])

        # construct the model
        pred_model = keras.models.Model(inputs=model.inputs,
                                        outputs=outputs,
                                        name=name)

        return pred_model
예제 #12
0
def create_train_visual_feature_main():
    from config import VISUAL_SIZE, last_layer
    from create_pickle_file import TRAIN_PICKLE, IMAGE_SIZE, NUM_CHANNELS
    from dataset_utils import FILE_NAME_CID_PATH
    all_train_fine_labels = read_pickle_file(
        FILE_NAME_CID_PATH)['all_train_fine_labels']
    print('LOADING CLASSIFIER AT {} ...'.format(BEST_CLASSIFY_CKPT_FILE))
    model = load_model(BEST_CLASSIFY_CKPT_FILE)
    model = Model(inputs=model.input,
                  outputs=model.get_layer(last_layer).output)
    print('LOADING TRAIN DATA....')
    X = read_pickle_file(TRAIN_PICKLE)['data']
    X = X.reshape(-1, IMAGE_SIZE, IMAGE_SIZE, NUM_CHANNELS)
    print('Creating training visual feature .... ')
    features = model.predict(X, verbose=1).reshape(-1, VISUAL_SIZE)
    features_f = open(TRAIN_FEATURE_PATH, 'wb')
    pickle.dump(
        {
            'features': features,
            'fine_label_names': all_train_fine_labels
        },
        features_f,
        protocol=4)
    features_f.close()
    print("Done.")
예제 #13
0
def mobilenet_3d(input_shape: tuple, model_2d: Model):
    input_image = Input(input_shape)
    x = ZeroPadding3D()(input_image)
    x = conv2d3d(model_2d.get_layer('conv1'))(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = ZeroPadding3D()(x)
    x = DepthwiseConv2D()(x)
예제 #14
0
def build_word_sequence_lstm_model(word_indices, word_vectors, output_dim):
    lstm_unit_size = args.lstm_unit_size
    learning_rate = args.learning_rate
    dropout = 0.6
    # from word_indices of all word vocabulary to word_embedded for one sentence of text
    word_symbols = len(word_indices) + 1
    word_embedding_weights = np.zeros((word_symbols, word_embeddings_size))
    for word, index in word_indices.items():
        try:
            word_embedding_weights[index, :] = word_vectors[word]
        except KeyError:
            word_embedding_weights[
                index, :] = np.ones(word_embeddings_size) * -1

    doc_word_input = Input(shape=(max_num_of_sentences,
                                  max_num_of_tokens_per_sentence),
                           dtype='int64',
                           name="doc_word_input")
    sent_word_input = Input(shape=(max_num_of_tokens_per_sentence, ),
                            dtype='int64',
                            name="sent_word_input")

    word_embedding_layer = Embedding(output_dim=word_embeddings_size,
                                     input_dim=word_symbols,
                                     mask_zero=True)
    word_embedding_layer.build(
        (None, ))  # if you don't do this, the next step won't work
    word_embedding_layer.set_weights([word_embedding_weights])
    word_embedded = word_embedding_layer(sent_word_input)

    bi_lstm_word_sent = Bidirectional(
        GRU(lstm_unit_size, return_sequences=rueT))(word_embedded)

    #attention_word = AttentionWithContext()(bi_lstm_word_sent)

    word_sent_encode = Dropout(dropout)(bi_lstm_word_sent)  #(attention_word)
    word_encoder = Model(inputs=sent_word_input, outputs=word_sent_encode)
    word_encoded = TimeDistributed(word_encoder)(doc_word_input)

    b_lstm_word_doc = Bidirectional(GRU(lstm_unit_size,
                                        return_sequences=False))(word_encoded)

    word_output = Dropout(dropout, name='final_layer_word')(
        b_lstm_word_doc)  #(attention_doc)
    word_output = Dense(output_dim,
                        activation='softmax')(word_output)  #(word_output)

    model = Model(inputs=doc_word_input, outputs=word_output)
    rmsprop = RMSprop(lr=learning_rate)
    model.compile(optimizer=rmsprop,
                  loss='categorical_crossentropy',
                  metrics=['categorical_accuracy'])

    final_layer = Model(inputs=model.input,
                        outputs=model.get_layer('final_layer_word').output)
    # final_layer = Model(inputs=model.input, outputs=model.get_layer('final_layer_word').output)

    return model, final_layer
예제 #15
0
def _get_outputs(model: keras.Model,
                 input_specs: Optional[Sequence[NodeSpec]]):
    if input_specs is None:
        return model.outputs

    return [
        tensor for input_spec in input_specs for tensor in _iterate_tensors(
            model.get_layer(input_spec.layer_name).get_output_at(
                input_spec.node_index))
    ]
예제 #16
0
def deepspeech_custom(is_gpu: bool,
                      layers: List[dict],
                      input_dim: int,
                      to_freeze: List[dict] = [],
                      random_state=1) -> Model:
    np.random.seed(random_state)
    set_random_seed(random_state)

    constructors = {
        'BatchNormalization':
        lambda params: BatchNormalization(**params),
        'Conv2D':
        lambda params: Conv2D(**params, name=name),
        'Dense':
        lambda params: TimeDistributed(Dense(**params), name=name),
        'Dropout':
        lambda params: Dropout(**params),
        'LSTM':
        lambda params: Bidirectional(CuDNNLSTM(**params) if is_gpu else LSTM(
            activation='tanh', recurrent_activation='sigmoid', **params),
                                     merge_mode='sum',
                                     name=name),
        'ReLU':
        lambda params: ReLU(**params),
        'ZeroPadding2D':
        lambda params: ZeroPadding2D(**params),
        'expand_dims':
        lambda params: Lambda(expand_dims, arguments=params),
        'squeeze':
        lambda params: Lambda(squeeze, arguments=params),
        'squeeze_last_dims':
        lambda params: Reshape([-1, params['units']])
    }
    with tf.device('/cpu:0'):
        input_tensor = Input([None, input_dim], name='X')

        x = input_tensor
        for params in layers:
            constructor_name = params.pop('constructor')
            name = params.pop(
                'name'
            ) if 'name' in params else None  # `name` is implicit passed to constructors
            constructor = constructors[
                constructor_name]  # Conv2D, TimeDistributed and Bidirectional.
            layer = constructor(params)
            x = layer(x)
        output_tensor = x

        model = Model(input_tensor, output_tensor, name='DeepSpeech')
        for params in to_freeze:
            name = params.pop('name')
            layer = model.get_layer(name)
            layer.trainable = False
    return model
예제 #17
0
 def get_embedding(encoder, image_data):
     # get the encoder layer
     layer_name = 'encoded'
     encoder = Model(inputs=encoder.input,
                     outputs=encoder.get_layer(layer_name).output)
     image_data = np.expand_dims(
         image_data,
         0) if not len(image_data.shape) == 4 else image_data
     image_data = image_data.astype('float32') / 255.
     encoded_img = encoder.predict(image_data)
     return encoded_img
예제 #18
0
def transfer_weights(model_2d: Model, model_3d: Model):
    for l2 in model_2d.layers:
        if type(l2).__name__ == 'Conv2D':
            n2 = l2.name
            try:
                l3 = model_3d.get_layer(n2)
                s = l3.kernel.shape[0]
                w = l2.get_weights()
                w[0] = np.array(w[0] * s)
                l3.set_weights(w)
            except ValueError:
                pass
예제 #19
0
def _get_inputs(model: keras.Model, input_specs: Optional[Sequence[NodeSpec]]):
    if input_specs is None:
        return [
            Input(tensor=tensor, data_format=_get_data_format(tensor))
            for tensor in model.inputs
        ]

    return [
        Input(tensor=tensor, data_format=_get_data_format(tensor))
        for input_spec in input_specs for tensor in _iterate_tensors(
            model.get_layer(input_spec.layer_name).get_input_at(
                input_spec.node_index))
    ]
예제 #20
0
def build_char_sequence_lstm_model(char_indices, char_vectors, output_dim):
    lstm_unit_size = args.lstm_unit_size
    learning_rate = args.learning_rate
    lstm_dropout = 0.15
    dropout = 0.3

    doc_char_input = Input(shape=(max_num_of_sentences, max_num_of_chars), dtype='int64', name="doc_char_input")
    sent_char_input = Input(shape=(max_num_of_chars,), dtype='int64', name="sent_char_input")

    char_symbols = len(char_indices) + 1
    char_embedding_weights = np.zeros((char_symbols, char_embeddings_size))
    for char, index in char_indices.items():
        char_embedding_weights[index, :] = char_vectors[char]
    char_embedding_layer = Embedding(output_dim=char_embeddings_size, input_dim=char_symbols, mask_zero=True)
    char_embedding_layer.build((None,))  # if you don't do this, the next step won't work
    char_embedding_layer.set_weights([char_embedding_weights])
    char_embedded = char_embedding_layer(sent_char_input)

    # filter_length = [5, 3, 3]
    # nb_filter = [196, 196, 256]
    # pool_length = 2
    # char_embedded = Lambda(binarize_char, output_shape=binarize_char_outshape)(sent_char_input)
    # for i in range(len(nb_filter)):
    #     char_embedded = Conv1D(filters=nb_filter[i], kernel_size=filter_length[i], padding='valid', activation='relu',
    #                            kernel_initializer='glorot_normal', strides=1)(char_embedded)
    #
    #     char_embedded = Dropout(0.1)(char_embedded)
    #     char_embedded = MaxPooling1D(pool_size=pool_length)(char_embedded)

    bi_lstm_char_sent = Bidirectional(
        LSTM(lstm_unit_size, return_sequences=False, dropout=lstm_dropout, recurrent_dropout=lstm_dropout))(
        char_embedded)

    char_sent_encode = Dropout(dropout)(bi_lstm_char_sent)
    char_encoder = Model(inputs=sent_char_input, outputs=char_sent_encode)
    char_encoded = TimeDistributed(char_encoder)(doc_char_input)

    b_lstm_char_doc = Bidirectional(
        LSTM(lstm_unit_size, return_sequences=False, dropout=lstm_dropout, recurrent_dropout=lstm_dropout))(
        char_encoded)

    char_output = Dropout(dropout, name='final_layer_char')(b_lstm_char_doc)
    char_output = Dense(output_dim, activation='softmax')(char_output)

    model = Model(inputs=doc_char_input, outputs=char_output)
    rmsprop = RMSprop(lr=learning_rate)
    model.compile(optimizer=rmsprop, loss='categorical_crossentropy', metrics=['categorical_accuracy'])

    final_layer = Model(inputs=model.input, outputs=model.get_layer('final_layer_char').output)

    return model, final_layer
예제 #21
0
    def test_manual_merge_categorical_value(self):
        from keras import Model
        from dlnn.tests.ml.cnn_func_test import inputs
        from dlnn.util import MoorePenrose
        import tensorflow as tf
        #
        # Feed Beta
        #
        feed = Model(inputs=inputs, outputs=step_11_a_activation)
        output = feed.predict(normalized)
        w_10_a = feed.get_layer(index=10).get_weights()
        w_12_a = [K.eval(K.dot(MoorePenrose.pinv3(output), K.variable(categorical_label_init)))]
        feed = Model(inputs=inputs, outputs=step_11_b_activation)
        output = feed.predict(normalized)
        w_10_b = feed.get_layer(index=10).get_weights()
        w_12_b = [K.eval(K.dot(MoorePenrose.pinv3(output), K.variable(categorical_label_init)))]
        feed = Model(inputs=inputs, outputs=step_11_c_activation)
        output = feed.predict(normalized)
        w_10_c = feed.get_layer(index=10).get_weights()
        w_12_c = [K.eval(K.dot(MoorePenrose.pinv3(output), K.variable(categorical_label_init)))]

        #
        # Training Model
        #
        network = Model(inputs=inputs, outputs=step_14_reshape)
        network.compile(optimizer=keras.optimizers.RMSprop(lr=0.0, rho=0.0, epsilon=None, decay=0.0),
                        loss=keras.losses.categorical_crossentropy,
                        metrics=[keras.metrics.categorical_accuracy, keras.metrics.mape])
        network.get_layer(index=10).set_weights(w_10_a)
        network.get_layer(index=11).set_weights(w_10_b)
        network.get_layer(index=12).set_weights(w_10_c)
        network.get_layer(index=16).set_weights(w_12_a)
        network.get_layer(index=17).set_weights(w_12_b)
        network.get_layer(index=18).set_weights(w_12_c)
        result = network.predict(normalized, batch_size=normalized.shape[0])
        # print(result)
        result = K.cast(K.argmax(result), dtype=tf.int32)
        # print(K.eval(result))
        result = tf.map_fn(lambda x: tf.bincount(x, minlength=3), result)
        # print(K.eval(result))
        result = K.argmax(result)
        # print(K.eval(result))
        self.assertIsNotNone(result)
예제 #22
0
 def __train(self, x, y):
     assert self.layer is not None
     yc = to_categorical(y, self.category_num)
     elm_1_beta_net = Model(inputs=self.layer['input'],
                            outputs=self.layer['elm_1_activation_1'])
     elm_1_activation_1_o = elm_1_beta_net.predict(x)
     elm_1_dense_1_w = elm_1_beta_net.get_layer(
         name='elm_1_dense_1').get_weights()
     elm_1_dense_2_w = [
         K.eval(K.dot(MoorePenrose.pinv3(elm_1_activation_1_o), yc))
     ]
     elm_2_beta_net = Model(inputs=self.layer['input'],
                            outputs=self.layer['elm_2_activation_1'])
     elm_2_activation_1_o = elm_2_beta_net.predict(x)
     elm_2_dense_1_w = elm_2_beta_net.get_layer(
         name='elm_2_dense_1').get_weights()
     elm_2_dense_2_w = [
         K.eval(K.dot(MoorePenrose.pinv3(elm_2_activation_1_o), yc))
     ]
     elm_3_beta_net = Model(inputs=self.layer['input'],
                            outputs=self.layer['elm_3_activation_1'])
     elm_3_activation_1_o = elm_3_beta_net.predict(x)
     elm_3_dense_1_w = elm_3_beta_net.get_layer(
         name='elm_3_dense_1').get_weights()
     elm_3_dense_2_w = [
         K.eval(K.dot(MoorePenrose.pinv3(elm_3_activation_1_o), yc))
     ]
     network = Model(inputs=self.layer['input'],
                     outputs=self.layer['fully_connected_merge'])
     network.compile(optimizer=RMSprop(lr=0.0,
                                       rho=0.0,
                                       epsilon=None,
                                       decay=0.0),
                     loss=categorical_crossentropy,
                     metrics=[categorical_accuracy, mape])
     network.get_layer(name='elm_1_dense_1').set_weights(elm_1_dense_1_w)
     network.get_layer(name='elm_1_dense_2').set_weights(elm_1_dense_2_w)
     network.get_layer(name='elm_2_dense_1').set_weights(elm_2_dense_1_w)
     network.get_layer(name='elm_2_dense_2').set_weights(elm_2_dense_2_w)
     network.get_layer(name='elm_3_dense_1').set_weights(elm_3_dense_1_w)
     network.get_layer(name='elm_3_dense_2').set_weights(elm_3_dense_2_w)
     network.fit(x, yc)
     network.save(self.network_path)
예제 #23
0
def vae_classifier(vae_model: keras.Model):
    for layer in vae_model.layers:
        layer.trainable = False
    encoded = vae_model.get_layer(name=VAE_ENCODER_LAYER).output
    x = Flatten()(encoded)
    x = Dense(512)(x)
    x = Activation("relu")(x)
    x = Dense(len(DAN_LIST))(x)
    x = Activation("softmax")(x)
    model = Model(vae_model.input, x)
    model.compile("adam", loss="mean_squared_error", metrics=["accuracy"])
    model.summary()

    return model
예제 #24
0
    def std_net(self):
        out = self.g(self.feature_layers_num)
        center_cls, scale_regr, offset, angle = HEAD(1)(out)

        m = Model(inputs=self.inp, outputs=[center_cls, scale_regr, offset])

        conv2d_6 = m.get_layer('conv2d_6').output
        x = self.up_2x(conv2d_6, 32, self.mode)
        stage1 = m.get_layer('block1_pool').output
        x = Concatenate(axis=-1)([x, stage1])
        bn1 = BatchNormalization()(x)
        conv_1 = Conv2D(32, 1, activation='relu', padding='same')(bn1)
        bn2 = BatchNormalization()(conv_1)
        conv_2 = Conv2D(32, 3, activation='relu', padding='same')(bn2)
        x = self.up_2x(conv_2, 32, self.mode)
        x = Conv2D(16, (3, 3), padding='same', activation='relu')(x)

        center_line = Conv2D(1,
                             kernel_size=1,
                             strides=1,
                             activation='sigmoid',
                             name='cl')(x)
        outs = m.outputs
        return Model(m.input, [center_line] + outs)
예제 #25
0
def createAndSaveVGGVectors(trainData, testData, classSize):
    vggModel: Model = keras.applications.vgg16.VGG16(include_top=False,
                                                     weights='imagenet',
                                                     input_shape=(200, 200, 3),
                                                     pooling='max')
    vggModel.summary()
    vggModel = Model(inputs=vggModel.input,
                     outputs=vggModel.get_layer("fc1").output)
    vggModel.summary()
    intermediate_prediction = vggModel.predict(trainData)
    print(intermediate_prediction.shape)
    np.save("predictionVGG.npy", intermediate_prediction)
    intermediate_prediction = vggModel.predict(testData)
    print(intermediate_prediction.shape)
    np.save("predictionTestVGG.npy", intermediate_prediction)
    print("Kaydedildi.")
예제 #26
0
def create_test_visual_feature_main():
    from config import VISUAL_SIZE, last_layer
    from create_pickle_file import TEST_PICKLE, IMAGE_SIZE, NUM_CHANNELS
    from read_zjl import read_pickle_file
    print('LOADING TEST DATA....')
    X = read_pickle_file(TEST_PICKLE).reshape(-1, IMAGE_SIZE, IMAGE_SIZE,
                                              NUM_CHANNELS)
    print('LOADING CLASSIFIER AT {} ...'.format(BEST_CLASSIFY_CKPT_FILE))
    model = load_model(BEST_CLASSIFY_CKPT_FILE)
    model = Model(inputs=model.input,
                  outputs=model.get_layer(last_layer).output)
    print('Creating Test Feature...')
    features = model.predict(X, verbose=1).reshape(-1, VISUAL_SIZE)

    with open(TEST_FEATURE_PATH, 'wb') as features_f:
        pickle.dump({'features': features}, features_f, protocol=4)
    print("Done.")
예제 #27
0
def layer_learned(model: Model, layer_name, img_path):
    img = np.load(img_path)
    img = img[np.newaxis, :, :, :, np.newaxis]
    img = (img - np.min(img)) / (np.max(img) - np.min(img))

    layer_output = model.get_layer(layer_name).output
    activation_mode = models.Model(inputs=model.input, outputs=layer_output)
    activations = activation_mode.predict(img)
    activations = np.mean(activations, -1)
    activations = np.squeeze(activations)
    plt.figure()

    for i in range(9):
        plt.subplot(3, 3, 1 + i)
        plt.imshow(activations[:, :, 20 + i], cmap='viridis')
    plt.savefig(os.path.join(pic_path, 'second_conv_learned.png'))
    plt.show()
예제 #28
0
def proper_scoring_method(X_train, X_val, X_test, y_train):

    # Transform data for neural network
    X_train = StandardScaler().fit_transform(X_train)
    X_val = StandardScaler().fit_transform(X_val)
    X_test = StandardScaler().fit_transform(X_test)

    # Build training model, one hidden layer with 1000 neurons
    inputs = Input(shape=(X_train.shape[1], ))
    x = Dense(1000, activation='tanh')(inputs)
    mu, sigma = ProperScoringLayer(1, name='main_output')(x)
    model = Model(inputs, mu)
    model.compile(loss=custom_loss(sigma), optimizer=Adam(lr=1e-3))

    # Implements early stopping
    callback = [
        tf.keras.callbacks.EarlyStopping(monitor='loss',
                                         patience=3,
                                         min_delta=0.04)
    ]

    # Train model
    model.fit(X_train, y_train, epochs=500, callbacks=callback, batch_size=150)

    # Allows us to make predictions with our model
    get_intermediate = K.function(
        inputs=[model.input], outputs=model.get_layer('main_output').output)

    # Make predictions for mean and variance
    test_predictions, sigmas = [], []
    for record in X_test:
        mu, sigma = get_intermediate(np.array([record]))
        test_predictions.append(mu.reshape(1, )[0])
        sigmas.append(sigma.reshape(1, )[0])

    variance = np.abs(np.array(sigmas))

    # Validation set predictions for kNN ICP
    val_predictions, _ = get_intermediate(X_val)

    print_log('Done proper scoring method training and predictions')

    return np.array(test_predictions), val_predictions.ravel(), variance
예제 #29
0
def getFeature(modelpath, imglist):
    x = fe.img2array(imglist)
    model = load_model(modelpath)
    oriimgclass = model.predict(x)
    imgclass = []
    for i in range(0, len(imglist)):
        max = 0
        maxindex = 0
        for j in range(0, 10):
            if (oriimgclass[i][j] > max):
                max = oriimgclass[i][j]
                maxindex = j
        print((max, maxindex + 1))
        imgclass.append(maxindex + 1)
    imgclass = np.array(imgclass)
    model = Model(inputs=model.input,
                  outputs=model.get_layer('fc-final').output)
    imgfeature = model.predict(x)
    return imgclass, imgfeature
예제 #30
0
def build_cnn():

    nasnet_inst = resnet50.ResNet50(include_top=True, weights='imagenet', input_tensor=None, input_shape=(HEIGHT,WIDTH,3), pooling=None, classes=1000)

    x = nasnet_inst.get_layer("flatten_1").output
    x = Dense(1000, activation="relu")(x)
    x = Dense(64, activation="relu")(x)
    x = Dense(2, activation="softmax")(x)
  
    model = Model(inputs=nasnet_inst.inputs, outputs=x)

    for i in model.layers:
        i.trainable = True

    non_trainable = ["input_1","conv1_pad","conv1","bn_conv1","activation_1","max_pooling2d_1",
                    "res2a_branch2a","bn2a_branch2a","activation_2",
                    "res2a_branch2b","bn2a_branch2b","activation_3","res2a_branch2c","res2a_branch1","bn2a_branch2c","bn2a_branch1",
                    "add_1","bn2a_branch1","activation_4","res2b_branch2a","bn2b_branch2a","activation_5","res2b_branch2b","bn2b_branch2b","activation_6","res2b_branch2c","bn2b_branch2c",
                    "add_2","activation_4","activation_7","res2c_branch2a","bn2c_branch2a","activation_8","res2c_branch2b","bn2c_branch2b","activation_9","res2c_branch2c","bn2c_branch2c",
                    "add_3","activation_7","activation_10","res3a_branch2a","bn3a_branch2a","activation_11","res3a_branch2b","bn3a_branch2b","activation_12","res3a_branch2c","res3a_branch1","bn3a_branch2c","bn3a_branch1"]
#                    "add_4","bn3a_branch1","activation_13","res3b_branch2a","bn3b_branch2a","activation_14","res3b_branch2b","bn3b_branch2b","activation_15","res3b_branch2c","bn3b_branch2c",
#                    "add_5","activation_13","activation_16","res3c_branch2a","bn3c_branch2a","activation_17","res3c_branch2b","bn3c_branch2b","activation_18","res3c_branch2c","bn3c_branch2c",
#                    "add_6","activation_16","activation_19","res3d_branch2a","bn3d_branch2a","activation_20","res3d_branch2b","bn3d_branch2b","activation_21","res3d_branch2c","bn3d_branch2c",
#                    "add_7","activation_19","activation_22","res4a_branch2a","bn4a_branch2a","activation_23","res4a_branch2b","bn4a_branch2b","activation_24","res4a_branch2c","res4a_branch1","bn4a_branch2c","bn4a_branch1",
#                    "add_8","bn4a_branch1[0][0]","activation_25","res4b_branch2a","bn4b_branch2a","activation_26","res4b_branch2b","bn4b_branch2b","activation_27","res4b_branch2c","bn4b_branch2c",
#                    "add_9","activation_25[0][0]","activation_28","res4c_branch2a","bn4c_branch2a","activation_29","res4c_branch2b","bn4c_branch2b","activation_30","res4c_branch2c","bn4c_branch2c",
#                    "add_10","activation_28[0][0]","activation_31","res4d_branch2a","bn4d_branch2a","activation_32","res4d_branch2b","bn4d_branch2b","activation_33","res4d_branch2c","bn4d_branch2c",
#                    "add_11","activation_31[0][0]","activation_34","res4e_branch2a","bn4e_branch2a","activation_35","res4e_branch2b","bn4e_branch2b","activation_36","res4e_branch2c","bn4e_branch2c",
#                    "add_12","activation_34","activation_37","res4f_branch2a","bn4f_branch2a","activation_38","res4f_branch2b","bn4f_branch2b","activation_39","res4f_branch2c","bn4f_branch2c",
#                    "add_13","activation_37","activation_40","res5a_branch2a","bn5a_branch2a","activation_41","res5a_branch2b","bn5a_branch2b","activation_42","res5a_branch2c","res5a_branch1","bn5a_branch2c","bn5a_branch1",
#                    "add_14","bn5a_branch1","activation_43","res5b_branch2a","bn5b_branch2a","activation_44","res5b_branch2b","bn5b_branch2b","activation_45","res5b_branch2c","bn5b_branch2c",
#                    "add_15","activation_43","activation_46","res5c_branch2a","bn5c_branch2a","activation_47","res5c_branch2b","bn5c_branch2b","activation_48","res5c_branch2c","bn5c_branch2c",
#                    "add_16","activation_46","activation_49","avg_pool","flatten_1","fc1000"]
    for i in non_trainable:
        try:
            model.get_layer(i).trainable = False
        except ValueError:
            print("Layer does not exist: ",i)

    sgd = SGD(lr=1e-6, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])

    return model
예제 #31
0
def build_word_sequence_conv_model(word_indices, word_vectors, output_dim):
    word_symbols = len(word_indices) + 1
    word_embedding_weights = np.zeros((word_symbols, word_embeddings_size))
    for word, index in word_indices.items():
        word_embedding_weights[index, :] = word_vectors[word]

    word_doc_input = Input(shape=(max_num_of_sentences, max_num_of_tokens_per_sentence), dtype='int64')
    word_sent_input = Input(shape=(max_num_of_tokens_per_sentence,), dtype='int64')

    word_embedding_layer = Embedding(output_dim=word_embeddings_size, input_dim=word_symbols, mask_zero=False)
    word_embedding_layer.build((None,))  # if you don't do this, the next step won't work
    word_embedding_layer.set_weights([word_embedding_weights])
    word_embedded = word_embedding_layer(word_sent_input)

    block2 = char_block(word_embedded, (128, 256), filter_length=(5, 5), subsample=(1, 1), pool_length=(2, 2))
    block3 = char_block(word_embedded, (192, 320), filter_length=(7, 5), subsample=(1, 1), pool_length=(2, 2))

    sent_encode = concatenate([block2, block3], axis=-1)
    # sent_encode = Dropout(0.2)(sent_encode)

    encoder = Model(inputs=word_sent_input, outputs=sent_encode)
    encoder.summary()

    encoded = TimeDistributed(encoder)(word_doc_input)

    lstm_h = 92

    lstm_layer = LSTM(lstm_h, return_sequences=True, dropout=0.1, recurrent_dropout=0.1, implementation=0)(encoded)
    lstm_layer2 = LSTM(lstm_h, return_sequences=False, dropout=0.1, recurrent_dropout=0.1, implementation=0,
                       name='final_layer_word')(lstm_layer)

    # output = Dropout(0.2)(bi_lstm)
    output = Dense(output_dim, activation='softmax')(lstm_layer2)

    model = Model(outputs=output, inputs=word_doc_input)
    optimizer = 'rmsprop'
    model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['categorical_accuracy'])
    final_layer = Model(inputs=model.input, outputs=model.get_layer('final_layer_word').output)

    return model, final_layer