def train_predict(net, train_data, untrain_data, config, device, iteration, pred_probs): config = adjust_config(config, len(train_data[0]), iteration) mu.train(net, train_data, config, device) pred_probs[device] = mu.predict_prob(net, untrain_data, config, device) save_checkpoint({ 'state_dict': net.state_dict(), 'epoch': iteration, }, False, fpath=os.path.join('logs/spaco/%s.epoch%d' % (config.model_name, iteration)))
def test_net(net, test_data, config, device, probs): probs[device] = mu.predict_prob(net, test_data, config, device)
def spaco(configs, data, iter_steps=1, gamma=0, train_ratio=0.2, regularizer='soft'): """ 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['train'], seed=args.seed, num_per_class=args.num_per_class) add_num = 4000 pred_probs = [] test_preds = [] sel_ids = [] weights = [] start_step = 0 ########### # initiate classifier to get preidctions ########### for view in range(num_view): configs[view] = adjust_config(configs[view], len(train_data[0]), 0) net = models.create(configs[view].model_name).to(view) mu.train(net, train_data, configs[view], device=view) pred_probs.append(mu.predict_prob(net, untrain_data, configs[view], view)) test_preds.append(mu.predict_prob(net, data['test'], configs[view], view)) acc = mu.evaluate(net, data['test'], configs[view], view) save_checkpoint( { 'state_dict': net.state_dict(), 'epoch': 0, }, False, fpath=os.path.join( 'spaco/%s.epoch%d' % (configs[view].model_name, 0))) 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_num, gamma, regularizer) import pdb;pdb.set_trace() sel_ids.append(sel_id) weights.append(weight) # start iterative training gt_y = data['test'][1] for step in range(start_step, iter_steps): for view in range(num_view): print('Iter step: %d, view: %d, model name: %s' % (step+1,view,configs[view].model_name)) # update sample weights sel_ids[view], weights[view] = dp.update_ids_weights( view, pred_probs, sel_ids, weights, pred_y, train_data, add_num, gamma, regularizer) # update model parameter new_train_data, _ = dp.update_train_untrain( sel_ids[view], train_data, untrain_data, pred_y, weights[view]) configs[view] = adjust_config(configs[view], len(train_data[0]), step) net = models.create(configs[view].model_name).cuda() mu.train(net, new_train_data, configs[view], device=view) # update y pred_probs[view] = mu.predict_prob(model, untrain_data, configs[view]) # evaluation current model and save it acc = mu.evaluate(net, data['test'], configs[view], device=view) predictions = mu.predict_prob(net, data['train'], configs[view], device=view) save_checkpoint( { 'state_dict': net.state_dict(), 'epoch': step + 1, 'predictions': predictions, 'accuracy': acc }, False, fpath=os.path.join( 'spaco/%s.epoch%d' % (configs[view].model_name, step + 1))) test_preds[view] = mu.predict_prob(model, data['test'], configs[view], device=view) add_num += 4000 * num_view fuse_y = np.argmax(sum(test_preds), axis=1) print('Acc:%0.4f' % np.mean(fuse_y== gt_y))
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 """ assert iter_steps >= 1 assert len(configs) == 2 train_data, untrain_data = dp.split_dataset( data['train'], seed=args.seed, num_per_class=args.num_per_class) gt_y = data['test'][1] new_train_data = deepcopy(train_data) add_num = 8000 for step in range(iter_steps): pred_probs = [] test_preds = [] add_ids = [] for view in range(2): print('Iter step: %d, view: %d, model name: %s' % (step + 1, view, configs[view].model_name)) configs[view] = adjust_config(configs[view], len(train_data[0]), step) net = models.create(configs[view].model_name).to(device) mu.train(net, new_train_data, configs[view], device) mu.evaluate(net, data['test'], configs[view], device) save_checkpoint( { 'state_dict': net.state_dict(), 'epoch': step + 1, }, False, fpath=os.path.join('logs/cotrain/%s.epoch%d' % (configs[view].model_name, step))) test_preds.append( mu.predict_prob(net, data['test'], configs[view], device)) if len(untrain_data[0]) > configs[view].batch_size: pred_probs.append( mu.predict_prob(net, untrain_data, configs[view], device)) add_ids.append( dp.select_ids(pred_probs[view], train_data, add_num)) # update training data # import pdb;pdb.set_trace() pred_y = np.argmax(sum(pred_probs), axis=1) add_id = np.array(sum(add_ids), dtype=np.bool) fuse_y = np.argmax(sum(test_preds), axis=1) print('Fuse Acc:%0.4f' % np.mean(fuse_y == gt_y)) if args.tricks: new_train_data, _ = dp.update_train_untrain( add_id, train_data, untrain_data, pred_y) add_num += add_num else: if len(untrain_data[0]) < 1: break new_train_data, untrain_data = dp.update_train_untrain( add_id, new_train_data, untrain_data, pred_y)
def train_predict(net, train_data, untrain_data, test_data, config, device, pred_probs): mu.train(net, train_data, config, device) pred_probs.append(mu.predict_prob(net, untrain_data, configs[view], view))