示例#1
0
 def initialize_from(self, old_model_path, ctx, *args):
     if os.path.exists(os.path.join(old_model_path, 'ModelAchi.pkl')):
         with open(os.path.join(old_model_path, 'ModelAchi.pkl'),
                   'rb') as f:
             archi_dict = pickle.load(f)
     else:
         archi_dict = self.default_params
     old = models.SphereNet20(archi_dict=archi_dict, use_custom_relu=True)
     old.load_params(os.path.join(old_model_path, 'model'))
     for name, block in self.features._children.items():
         old_block = old.features._children[name]
         if isinstance(block, qDense):
             block.weight.initialize(init=mx.init.Constant(
                 old_block.weight._data[0]),
                                     force_reinit=True,
                                     ctx=ctx)
             block.bias.initialize(init=mx.init.Constant(
                 old_block.bias._data[0]),
                                   force_reinit=True,
                                   ctx=ctx)
         else:
             if not old_block.same_shape:
                 block.conv0.weight.initialize(init=mx.init.Constant(
                     old_block.conv0.conv.weight._data[0]),
                                               force_reinit=True,
                                               ctx=ctx)
                 block.conv0.bias.initialize(init=mx.init.Constant(
                     old_block.conv0.conv.bias._data[0]),
                                             force_reinit=True,
                                             ctx=ctx)
                 block.a0.initialize(init=mx.init.Constant(
                     old_block.a0.alpha._data[0]),
                                     force_reinit=True,
                                     ctx=ctx)
             block.conv1.weight.initialize(init=mx.init.Constant(
                 old_block.conv1.conv.weight._data[0]),
                                           force_reinit=True,
                                           ctx=ctx)
             block.conv1.bias.initialize(init=mx.init.Constant(
                 old_block.conv1.conv.bias._data[0]),
                                         force_reinit=True,
                                         ctx=ctx)
             block.a1.initialize(init=mx.init.Constant(
                 old_block.a1.alpha._data[0]),
                                 force_reinit=True,
                                 ctx=ctx)
             block.conv2.weight.initialize(init=mx.init.Constant(
                 old_block.conv2.conv.weight._data[0]),
                                           force_reinit=True,
                                           ctx=ctx)
             block.conv2.bias.initialize(init=mx.init.Constant(
                 old_block.conv2.conv.bias._data[0]),
                                         force_reinit=True,
                                         ctx=ctx)
             block.a2.initialize(init=mx.init.Constant(
                 old_block.a2.alpha._data[0]),
                                 force_reinit=True,
                                 ctx=ctx)
示例#2
0
def cal_my_acc(test_files, target_files):
    '''
    this method is deprecated
    :param test_files:
    :param target_files:
    :return:
    '''
    mTransform = MTransform()
    normalize = transforms.Normalize(mean=0.5, std=0.5)
    transform = transforms.Compose([
        transforms.Resize((96, 112)),
        transforms.ToTensor(),
        normalize,
        # mTransform,
    ])
    model = models.SphereNet20()
    model.load_params("spherenet_model", ctx=mx.gpu())
    correct = 0
    total = 0
    target_emb = {}
    for target_file in target_files:
        target_image = transform(nd.array(Image.open(target_file))).as_in_context(mx.gpu())
        target_image = nd.expand_dims(target_image, axis=0)
        target_label = ''.join(target_file.split('/')[-1].split('.')[:-1])
        target_out = model(target_image)
        target_emb[target_label] = target_out
    test_emb = {}
    for test_file in test_files:
        test_image = Image.open(test_file)
        test_image = nd.expand_dims(transform(nd.array(test_image)), axis=0).as_in_context(mx.gpu())
        test_label = ''.join(test_file.split('/')[-1].split('.')[:-1])
        test_out = model(test_image)
        max_s = mx.nd.zeros(1, ctx=mx.gpu())
        max_label = ''
        sims = {}
        for target_label, target_out in target_emb.items():
            similarity = nd.sum(test_out * target_out) / \
                         (nd.norm(test_out) * nd.norm(target_out))
            sims[target_label] = similarity.asscalar()
            if max_s < similarity:
                max_s = similarity
                max_label = target_label
        if ''.join(max_label.split('_')[:-1]) == ''.join(test_label.split('_')[:-1]):
            correct += 1
        else:
            print test_label, max_s.asscalar(), max_label
        total += 1
        test_emb[test_label] = test_out
        # print correct, total, float(correct)/total

    return float(correct) / total, test_emb, target_emb
示例#3
0
def export_sym(model_path, ctx=mx.gpu()):
    if os.path.exists(os.path.join(model_path, 'ModelAchi.pkl')):
        with open(os.path.join(model_path, 'ModelAchi.pkl'), 'rb') as f:
            archi_dict = pickle.load(f)
    else:
        archi_dict = None
    model = models.SphereNet20(archi_dict=archi_dict)
    model.load_params(os.path.join(model_path, 'model'), ctx=ctx)

    train_data_loader, _, _ = dataset.train_valid_test_loader(
        '/home1/CASIA-WebFace/aligned_Webface-112X96', (0.9, 0.05),
        batch_size=1)
    # run forward once
    model.hybridize()
    model.get_feature = False
    for batch, label in train_data_loader:
        batch = batch.as_in_context(ctx)
        model(batch)
        break
    model.export(os.path.join(model_path, 'model_symbol'))
示例#4
0
                os.system("mkdir " + self.log_dir)
                os.system("touch " + log_path)
            with open(os.path.join(self.log_dir, "log.txt"), "a") as f:
                f.write(str(mstr) + "\n")


if __name__ == '__main__':
    args = get_args()

    # gpus = [0,1]
    # ctx = [mx.gpu(ii) for ii in gpus]
    ctx = mx.gpu(args.device_id)
    time_info = time.strftime('%Y-%m-%d_%H%M%S', time.localtime(time.time()))
    log_dir = os.path.abspath("./log/")
    if not args.prune:
        model = models.SphereNet20(use_custom_relu=False)
        model.load_params('spherenet_model2', ctx=ctx)
        if args.log:
            log_dir = os.path.abspath("./log/train-" + time_info + "/")
    else:
        model = models.SphereNet20()
        model.load_params(args.model_path, ctx=ctx)
        if args.log:
            log_dir = os.path.abspath("./log/prune-" + time_info + "/")
    p = Printer(log_dir, args.log)
    p.log(ctx)
    msg = "doing fine tuning(train)" if not args.prune else "doing pruning, using model " + \
        args.model_path
    p.log(msg)
    p.log(model)
示例#5
0
if __name__ == "__main__":
    # gpus = [0,1]
    # ctx = [mx.gpu(ii) for ii in gpus]
    model_path1 = "./log/prune-2018-07-13_121936"
    model_path2 = "./log/prune-2018-07-14_003715"
    ctx = mx.gpu()
    archi_dict = {
        0:[32,40],
        1:[73,74],
        2:[75,74],
        3:[108,115],
        4:[97,115],
        5:[106,115],
        6:[110,115],
        7:[143,205]
    }
    with open(os.path.join(model_path1,"ModelAchi.pkl"),"rb") as f:
        archi_dict1 = pickle.load(f)
    # with open(os.path.join(model_path1,"ModelAchi.pkl"),"wb") as f:
    #     pickle.dump(archi_dict,f)
    with open(os.path.join(model_path2,"ModelAchi.pkl"),"rb") as f:
        archi_dict2 = pickle.load(f)
    model1 = models.SphereNet20(archi_dict=archi_dict1,use_custom_relu=True)
    model1.load_params(os.path.join(model_path1,"model"), ctx=ctx)
    model2 = models.SphereNet20(archi_dict=archi_dict2,use_custom_relu=True)
    model2.load_params(os.path.join(model_path2,"model"), ctx=ctx)
    print(test_on_LFW(model1))
    print(test_on_LFW(model2))
    test_speed(model1)
    test_speed(model2)
    print 'over'