Example #1
0
def rank_transfer_model(pair_model_path):
    pair_model = load_model(pair_model_path)
    base_model = pair_model.layers[2]
    base_model = Model(inputs=base_model.get_input_at(0),
                       outputs=[base_model.get_output_at(0)],
                       name='resnet50')
    img0 = Input(shape=(224, 224, 3), name='img_0')
    img1 = Input(shape=(224, 224, 3), name='img_1')
    img2 = Input(shape=(224, 224, 3), name='img_2')
    feature0 = Flatten()(base_model(img0))
    feature1 = Flatten()(base_model(img1))
    feature2 = Flatten()(base_model(img2))
    dis1 = Lambda(eucl_dist, name='square1')([feature0, feature1])
    dis2 = Lambda(eucl_dist, name='square2')([feature0, feature2])
    score1 = Dense(1, activation='sigmoid', name='score1')(dis1)
    score2 = Dense(1, activation='sigmoid', name='score2')(dis2)
    sub_score = Lambda(sub, name='sub_score')([score1, score2])

    model = Model(inputs=[img0, img1, img2], outputs=[sub_score])
    model.get_layer('score1').set_weights(
        pair_model.get_layer('bin_out').get_weights())
    model.get_layer('score2').set_weights(
        pair_model.get_layer('bin_out').get_weights())
    plot_model(model, to_file='rank_model.png')

    for layer in base_model.layers:
        layer.trainable = False
    return model
def rank_transfer_model(pair_model_path):
    pair_model = load_model(pair_model_path)
    base_model = pair_model.layers[2]
    base_model = Model(inputs=base_model.get_input_at(0), outputs=[base_model.get_output_at(0)], name='resnet50')
    # base_model = ResNet50(weights='imagenet', include_top=False, input_tensor=Input(shape=(224, 224, 3)))
    # base_model = Model(inputs=[base_model.input], outputs=[base_model.output], name='resnet50')
    # for layer in base_model.layers[:len(base_model.layers)/2]:
    #     layer.trainable = False
    for layer in base_model.layers:
        if isinstance(layer, BatchNormalization):
            layer.trainable = False
    print 'to layer: %d' % (len(base_model.layers)/3*2)

    img0 = Input(shape=(224, 224, 3), name='img_0')
    img1 = Input(shape=(224, 224, 3), name='img_1')
    img2 = Input(shape=(224, 224, 3), name='img_2')
    feature0 = Flatten()(base_model(img0))
    feature1 = Flatten()(base_model(img1))
    feature2 = Flatten()(base_model(img2))
    dis1 = Lambda(eucl_dist, name='square1')([feature0, feature1])
    dis2 = Lambda(eucl_dist, name='square2')([feature0, feature2])
    score1 = Dense(1, activation='sigmoid', name='score1')(dis1)
    score2 = Dense(1, activation='sigmoid', name='score2')(dis2)
    sub_score = Lambda(sub, name='sub_score')([score1, score2])

    model = Model(inputs=[img0, img1, img2], outputs=[sub_score])
    # model = Model(inputs=[img0, img1, img2], outputs=[sub_score])
    model.get_layer('score1').set_weights(pair_model.get_layer('bin_out').get_weights())
    model.get_layer('score2').set_weights(pair_model.get_layer('bin_out').get_weights())
    plot_model(model, to_file='rank_model.png')


    print(model.summary())
    return model
Example #3
0
def pair_model(source_model_path, num_classes):
    pair_model = load_model(source_model_path)
    # base_model = ResNet50(weights='imagenet', include_top=False, input_tensor=Input(shape=(224, 224, 3)))
    base_model = pair_model.layers[2]
    base_model = Model(inputs=base_model.get_input_at(0), outputs=[base_model.get_output_at(0)], name='resnet50')
    img1 = Input(shape=(224, 224, 3), name='img_1')
    img2 = Input(shape=(224, 224, 3), name='img_2')
    feature1 = Lambda(lambda  x: K.l2_normalize(x,axis=1))(Flatten()(base_model(img1)))
    feature2 = Lambda(lambda  x: K.l2_normalize(x,axis=1))(Flatten()(base_model(img2)))
    dis = Lambda(eucl_dist, name='square')([feature1, feature2])
    # judge = Lambda(dis_sigmoid, name='bin_out')(dis)
    judge = Dense(1, activation='sigmoid', name='bin_out')(Dropout(0.8)(dis))

    model = Model(inputs=[img1, img2], outputs=[judge])
    model.get_layer('bin_out').set_weights(pair_model.get_layer('bin_out').get_weights())
    plot_model(model, to_file='model_combined.png')
    # for layer in base_model.layers[:-10]:
    #     layer.trainable = False
    # for layer in base_model.layers:
    #     layer.trainable = True
    return model
def two_stage_rank_transfer_2dataset(source_pair_model_path, target_train_list, target_model_path, target_train_path,
                           rank_pid_path, rank_score_path):
    train_images = reid_img_prepare(target_train_list, target_train_path)
    batch_size = 16
    similar_persons = np.genfromtxt(rank_pid_path, delimiter=' ')
    # if 'cross' in rank_pid_path:
    #     similar_persons = similar_persons - 1
    similar_matrix = np.genfromtxt(rank_score_path, delimiter=' ')
    rank_transfer(
        triplet_generator_by_rank_list(train_images, batch_size, similar_persons, similar_matrix, train=True),
        triplet_generator_by_rank_list(train_images, batch_size, similar_persons, similar_matrix, train=False),
        source_pair_model_path,
        target_model_path,
        batch_size=batch_size
    )


if __name__ == '__main__':
    pair_model = load_model('../pretrain/cuhk_pair_pretrain.h5')
    base_model = pair_model.layers[2]
    base_model = Model(inputs=base_model.get_input_at(0), outputs=[base_model.get_output_at(0)], name='resnet50')
    print isinstance(base_model.layers[-20], Conv2D)

    rank_transfer_2dataset('../pretrain/cuhk_pair_pretrain.h5', '../dataset/market_train.list',
                          'rank_transfer_test.h5',
                          '/content/Market-1501/train',
                          '/content/TFusion/TrackViz/data/cuhk_market-train/cross_filter_pid.log',
                          '/content/TFusion/TrackViz/data/cuhk_market-train/cross_filter_score.log')

Example #5
0
    similar_matrix = np.genfromtxt(rank_score_path, delimiter=' ')
    rank_transfer(triplet_generator_by_rank_list(train_images,
                                                 batch_size,
                                                 similar_persons,
                                                 similar_matrix,
                                                 train=True),
                  triplet_generator_by_rank_list(train_images,
                                                 batch_size,
                                                 similar_persons,
                                                 similar_matrix,
                                                 train=False),
                  source_pair_model_path,
                  target_model_path,
                  batch_size=batch_size)


if __name__ == '__main__':
    pair_model = load_model('../pretrain/cuhk_pair_pretrain.h5')
    base_model = pair_model.layers[2]
    base_model = Model(inputs=base_model.get_input_at(0),
                       outputs=[base_model.get_output_at(0)],
                       name='resnet50')
    print isinstance(base_model.layers[-20], Conv2D)

    rank_transfer_2dataset(
        '../pretrain/cuhk_pair_pretrain.h5', '../dataset/market_train.list',
        'rank_transfer_test.h5', '/home/cwh/coding/Market-1501/train',
        '/home/cwh/coding/TrackViz/data/cuhk_market-train/cross_filter_pid.log',
        '/home/cwh/coding/TrackViz/data/cuhk_market-train/cross_filter_score.log'
    )