def eval(save_dir): mAP = [] Acc = [] features = [] for idx, config in enumerate(configs): model = models.create( config.model_name, num_features=config.num_features, dropout=config.dropout, num_classes=config.num_classes) model = torch.nn.DataParallel(model).cuda() model_name = MODEL[idx] feature = [] for epoch in range(5): save_pth = os.path.join(save_dir, '%s.epoch%s' % (model_name, epoch)) if os.path.exists(save_pth) is not True: raise ValueError('wrong model pth %s' % save_pth) checkpoint = load_checkpoint(save_pth) state_dict = { k: v for k, v in checkpoint['state_dict'].items() if k in model.state_dict().keys() } model.load_state_dict(state_dict) result = checkpoint['performance'] mAP += [str(result[0])] Acc += [str(result[1])] feature += [mu.get_feature(model, query_gallery, data.images_dir, config)] features += [feature] for idx in range(5): feas = [features[j][idx] for j in range(3)] result = mu.combine_evaluate(feas, data) mAP += [str(result[0])] Acc += [str(result[1])] return mAP, Acc
def cotrain(configs, data, iter_steps=1, train_ratio=0.2, device='cuda:0'): """ cotrain model: params: model_names: model configs data: dataset include train and untrain data save_paths: paths for storing models iter_steps: maximum iteration steps train_ratio: labeled data ratio """ add_ratio = 0.5 assert iter_steps >= 1 train_data, untrain_data = dp.split_dataset(data.trainval, train_ratio, args.seed) query_gallery = list(set(data.query) | set(data.gallery)) data_dir = data.images_dir new_train_data = deepcopy(train_data) features = [] for step in range(iter_steps): pred_probs = [] add_ids = [] for view in range(2): config = configs[view] config.set_training(True) net = models.create(config.model_name, num_features=config.num_features, dropout=config.dropout, num_classes=config.num_classes).to(device) mu.train(net, new_train_data, data_dir, configs[view], device) save_checkpoint( { 'state_dict': net.state_dict(), 'epoch': step + 1, 'train_data': new_train_data }, False, fpath=os.path.join('logs/cotrain/seed_%d/%s.epoch%d' % (args.seed, config.model_name, step))) if len(untrain_data) == 0: continue pred_probs.append( mu.predict_prob(net, untrain_data, data_dir, configs[view], device)) add_ids.append(dp.sel_idx(pred_probs[view], train_data, add_ratio)) # calculate predict probility on all data p_b = mu.predict_prob(net, data.trainval, data_dir, configs[view], device) p_y = np.argmax(p_b, axis=1) t_y = [c for (_, c, _, _) in data.trainval] print(np.mean(t_y == p_y)) ### final evaluation if step + 1 == iter_steps: features += [ mu.get_feature(net, query_gallery, data.images_dir, configs[view], device) ] # update training data pred_y = np.argmax(sum(pred_probs), axis=1) add_id = sum(add_ids) if args.tricks: add_ratio += 1.2 new_train_data, _ = dp.update_train_untrain( add_id, train_data, untrain_data, pred_y) else: if len(untrain_data) == 0: break new_train_data, untrain_data = dp.update_train_untrain( add_id, new_train_data, untrain_data, pred_y) acc = mu.combine_evaluate(features, data) print(acc)
def spaco(configs, data, iter_steps=1, gamma=0, train_ratio=0.2, regularizer='hard', device='cuda:0'): """ self-paced co-training model implementation based on Pytroch params: model_names: model names for spaco, such as ['resnet50','densenet121'] data: dataset for spaco model save_pathts: save paths for two models iter_step: iteration round for spaco gamma: spaco hyperparameter train_ratio: initiate training dataset ratio """ num_view = len(configs) train_data, untrain_data = dp.split_dataset(data.trainval, train_ratio, args.seed) data_dir = data.images_dir query_gallery = list(set(data.query) | set(data.gallery)) save_dir = os.path.join('logs', 'parallel_spaco', regularizer, 'seed_%d' % args.seed) ########### # initiate classifier to get preidctions ########### add_ratio = 0.5 pred_probs = [] sel_ids = [] weights = [] features = [] start_step = 0 for view in range(num_view): net = models.create(configs[view].model_name, num_features=configs[view].num_features, dropout=configs[view].dropout, num_classes=configs[view].num_classes).to(device) mu.train(net, train_data, data_dir, configs[view], device) pred_probs.append(mu.predict_prob(net, untrain_data, data_dir, configs[view], device)) predictions = mu.predict_prob(net, data.trainval, data_dir, configs[view], device) mAP = mu.evaluate(net, data, configs[view], device) save_checkpoint( { 'state_dict': net.state_dict(), 'epoch': 0, 'train_data': train_data, 'trainval': data.trainval, 'predictions': predictions, 'performance': mAP }, False, fpath=os.path.join(save_dir, '%s.epoch0' % (configs[view].model_name))) pred_y = np.argmax(sum(pred_probs), axis=1) # initiate weights for unlabled examples for view in range(num_view): sel_id, weight = dp.get_ids_weights(pred_probs[view], pred_y, train_data, add_ratio, gamma, regularizer, num_view) sel_ids.append(sel_id) weights.append(weight) # start iterative training for step in range(start_step, iter_steps): for view in range(num_view): # update v_view sel_ids[view], weights[view] = update_ids_weights(view, pred_probs, sel_ids, weights, pred_y, train_data, add_ratio, gamma, regularizer) # update w_view new_train_data, _ = dp.update_train_untrain(sel_ids[view], train_data, untrain_data, pred_y, weights[view]) configs[view].set_training(True) net = models.create(configs[view].model_name, num_features=configs[view].num_features, dropout=configs[view].dropout, num_classes=configs[view].num_classes).to(device) mu.train(net, new_train_data, data_dir, configs[view], device) # update y pred_probs[view] = mu.predict_prob(net, untrain_data, data_dir, configs[view], device) # calculate predict probility on all data test_acc(net, data.trainval, data_dir, configs[view], device) # evaluation current model and save it mAP = mu.evaluate(net, data, configs[view], device) predictions = mu.predict_prob(net, data.trainval, data_dir, configs[view], device) save_checkpoint( { 'state_dict': net.state_dict(), 'epoch': step + 1, 'train_data': new_train_data, 'trainval': data.trainval, 'predictions': predictions, 'performance': mAP }, False, fpath=os.path.join( save_dir, '%s.epoch%d' % (configs[view].model_name, step + 1))) if step + 1 == iter_steps: features += [ mu.get_feature(net, query_gallery, data.images_dir, configs[view], device) ] add_ratio += 1.2 # pred_y = np.argmax(sum(pred_probs), axis=1) acc = mu.combine_evaluate(features, data) print(acc)
configs = [config3, config2] else: raise ValueError('wrong combination') features = [] for config in configs: model = models.create(config.model_name, num_features=config.num_features, dropout=config.dropout, num_classes=config.num_classes) model = torch.nn.DataParallel(model).cuda() save_pth = os.path.join(config.logs_dir, config.model_name, '%s' % (args.checkpoint)) if os.path.exists(save_pth) is not True: raise ValueError('wrong model pth') checkpoint = load_checkpoint(save_pth) state_dict = { k: v for k, v in checkpoint['state_dict'].items() if k in model.state_dict().keys() } model.load_state_dict(state_dict) if args.single_eval: mu.evaluate(model, data, config) features.append( mu.get_feature(model, query_gallery, data.images_dir, config)) mu.combine_evaluate(features, data)