def trainer(): # 1. Load dataset dataset_train = dataset_provider(config['dataset_name'], config['dataset_root'], is_train=True) dataloader_train = DataLoader(dataset_train, config['batch_size'], shuffle=True, num_workers=config['num_workers']) dataset_eval = dataset_provider(config['dataset_name'], config['dataset_root'], is_train=False) dataloader_eval = DataLoader(dataset_eval, config['batch_size'], shuffle=False, num_workers=config['num_workers']) # 2. Build model net = model_provider(config['model'], **config['model_param']).cuda(config['device_ids'][0]) net = nn.DataParallel(net, device_ids=config['device_ids']) # 3. Criterion criterion = nn.CrossEntropyLoss().cuda(config['device_ids'][0]) # 4. Optimizer optimizer = config['optimizer'](net.parameters(), **config['optimizer_param']) scheduler = config['scheduler']( optimizer, ** config['scheduler_param']) if config['scheduler'] else None # 5. Tensorboard logger logger_train = Logger('logs/train') logger_eval = Logger('logs/eval') # 6. Train loop for epoch in range(config['num_epoch']): # train print('---------------------- Train ----------------------') train_op(net, dataloader_train, criterion, optimizer, epoch, logger_train) # evaluation if epoch % config['eval_per_epoch'] == config['eval_per_epoch'] - 1: print('---------------------- Evaluation ----------------------') eval_op(net, dataloader_eval, criterion, epoch, logger_eval) # save weights torch.save( net.state_dict(), 'weights/{}/{}_{}.newest.pkl'.format(config['dataset_name'], config['model'], time_id)) # scheduler if scheduler is not None: scheduler.step()
def trainer(index=0): # 1. Load dataset dataset_train = dataset_provider(config['dataset_name'], config['dataset_root'], is_train=True, fold_idx=index) dataloader_train = DataLoader(dataset_train, config['batch_size'], shuffle=True, num_workers=config['num_workers']) dataloader_eval = dataset_provider(config['dataset_name'], config['dataset_root'], is_train=False, fold_idx=index) dataloader_eval = DataLoader(dataloader_eval, 10, shuffle=False, num_workers=config['num_workers']) # 2. Build model net = model_provider(config['model'], img_chn=config['image_channels'], n_cls=config['num_class']).cuda(config['device_ids'][0]) net = nn.DataParallel(net, device_ids=config['device_ids']) # 3. Criterion criterion = criterion_provider(config['criterion']).cuda(config['device_ids'][0]) # 4. Optimizer optimizer = config['optimizer'](net.parameters(), lr=config['lr']) scheduler = None if config['lr_scheduler']: step_size = len(dataloader_train) * config['num_epoch'] // (4 * 21) + 1 scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=step_size , gamma=0.9) # 5. Evaluation eval_op = eval_op_provider(config['eval_op_name']) # 6. Tensorboard logger logger_train = Logger('logs/{}/{}/fold_{}/train'.format(config['model'], time_id, index)) logger_eval = Logger('logs/{}/{}/fold_{}/eval'.format(config['model'], time_id, index)) # 7. Train loop dice_max = -1 for epoch in range(config['num_epoch']): # train print('---------------------- Train ----------------------') train_op(net, dataloader_train, criterion, optimizer, scheduler, epoch, logger_train) # evaluation if epoch % 10 == 9: print('---------------------- Evaluation ----------------------') dice, IoU, sensitivity, specificity = \ eval_op(net, dataloader_eval, config['device_ids'][0], criterion, config['num_class'], epoch, logger_eval, config['log_image']) # save weights torch.save(net.state_dict(), 'weights/{}/{}.newest.{}.pkl'.format(time_id, config['model'], index)) if dice >= dice_max: dice_max = dice torch.save(net.state_dict(), 'weights/{}/{}.best.{}.pkl'.format(time_id, config['model'], index)) return dice, IoU, sensitivity, specificity
def test_real_histo_data(tmpdir): logger = Logger(str(tmpdir), flush_secs=0.1) logger.log_histogram('hist2', [1, 7, 6, 9, 8, 1, 4, 5, 3, 7], step=1) logger.log_histogram('hist2', [5, 3, 2, 0, 8, 5, 7, 7, 7, 2], step=2) logger.log_histogram('hist2', [1, 2, 2, 1, 5, 1, 8, 4, 4, 1], step=3) tf_log, = glob.glob(str(tmpdir) + '/*') assert os.path.basename(tf_log).startswith('events.out.tfevents.')
def test_smoke_logger(tmpdir): logger = Logger(str(tmpdir), flush_secs=0.1) for step in range(10): logger.log_value('v1', step * 1.5, step) logger.log_value('v2', step**1.5 - 2) time.sleep(0.5) tf_log, = tmpdir.listdir() assert tf_log.basename.startswith('events.out.tfevents.')
def __init__(self, env='default', log_dir='runs/BiGRU', **kwargs): # self.vis = visdom.Visdom(env=env, **kwargs) self.tenbd = Logger(log_dir, flush_secs=2) # 记录数据的横向坐标{'img':2, 'loss':12} self.index = {} # 记录一些log信息 self.log_text = ''
def test_dummy(): logger = Logger(None, is_dummy=True) for step in range(3): logger.log_value('A v/1', step, step) logger.log_value('A v/2', step * 2, step) assert dict(logger.dummy_log) == { 'A_v/1': [(0, 0), (1, 1), (2, 2)], 'A_v/2': [(0, 0), (1, 2), (2, 4)], }
def test_real_image_data(tmpdir): logger = Logger(str(tmpdir), flush_secs=0.1) img = np.random.rand(10, 10) images = [img, img] logger.log_images('key', images, step=1) logger.log_images('key', images, step=2) logger.log_images('key', images, step=3) tf_log, = glob.glob(str(tmpdir) + '/*') assert os.path.basename(tf_log).startswith('events.out.tfevents.')
def main(args): datadir = get_data_dir(args.db) outputdir = get_output_dir(args.db) logger = None if args.tensorboard: # One should create folder for storing logs loggin_dir = os.path.join(outputdir, 'runs', 'pretraining') if not os.path.exists(loggin_dir): os.makedirs(loggin_dir) loggin_dir = os.path.join(loggin_dir, '%s' % (args.id)) if args.clean_log: remove_files_in_dir(loggin_dir) logger = Logger(loggin_dir) use_cuda = torch.cuda.is_available() # Set the seed for reproducing the results random.seed(args.manualSeed) np.random.seed(args.manualSeed) torch.manual_seed(args.manualSeed) if use_cuda: torch.cuda.manual_seed_all(args.manualSeed) torch.backends.cudnn.enabled = True cudnn.benchmark = True kwargs = {'num_workers': 0, 'pin_memory': True} if use_cuda else {} trainset = DCCPT_data(root=datadir, train=True, h5=args.h5) testset = DCCPT_data(root=datadir, train=False, h5=args.h5) nepoch = int( np.ceil( np.array(args.niter * args.batchsize, dtype=float) / len(trainset))) step = int( np.ceil( np.array(args.step * args.batchsize, dtype=float) / len(trainset))) trainloader = torch.utils.data.DataLoader(trainset, batch_size=args.batchsize, shuffle=True, **kwargs) testloader = torch.utils.data.DataLoader(testset, batch_size=100, shuffle=True, **kwargs) return pretrain( args, outputdir, { 'nlayers': 4, 'dropout': 0.2, 'reluslope': 0.0, 'nepoch': nepoch, 'lrate': [args.lr], 'wdecay': [0.0], 'step': step }, use_cuda, trainloader, testloader, logger)
def train_mlp_epoch(epoch, args, rnn, output, data_loader, optimizer_mlp, optimizer_output, scheduler_mlp, scheduler_output): rnn.train() output.train() loss_sum = 0 for batch_idx, data in enumerate(data_loader): rnn.zero_grad() output.zero_grad() x_unsorted = data['x'].float() y_unsorted = data['y'].float() y_len_unsorted = data['len'] y_len_max = max(y_len_unsorted) x_unsorted = x_unsorted[:, 0:y_len_max, :] y_unsorted = y_unsorted[:, 0:y_len_max, :] # Initialize gru hidden state according to batch size rnn.hidden = rnn.init_hidden(batch_size=x_unsorted.size(0)) # Sort input y_len,sort_index = torch.sort(y_len_unsorted,0,descending=True) y_len = y_len.numpy().tolist() x = torch.index_select(x_unsorted,0,sort_index) y = torch.index_select(y_unsorted,0,sort_index) x = Variable(x) y = Variable(y) h = rnn(x, pack=True, input_len=y_len) y_pred = output(h) y_pred = torch.sigmoid(y_pred) y_pred = pack_padded_sequence(y_pred, y_len, batch_first=True) y_pred = pad_packed_sequence(y_pred, batch_first=True)[0] # Use cross entropy loss loss = binary_cross_entropy_weight(y_pred, y) loss.backward() # Update deterministic and gru optimizer_output.step() optimizer_mlp.step() scheduler_output.step() scheduler_mlp.step() # Output only the first batch's statistics for each epoch if batch_idx==0: print('Epoch: {}/{}, train loss: {:.6f}, graph type: {}, num_layer: {}, hidden: {}'.format( epoch, args.epochs,loss.item(), args.graph_type, args.num_layers, args.hidden_size_rnn)) # Logging Logger('loss_'+args.fname, loss.item(), epoch*args.batch_ratio+batch_idx) # Update the loss sum loss_sum += loss.item() return loss_sum/(batch_idx+1)
def test_dummy_histo(): logger = Logger(None, is_dummy=True) bins = [0, 1, 2, 3] logger.log_histogram('key', (bins, [0.0, 1.0, 2.0]), step=1) logger.log_histogram('key', (bins, [1.0, 1.5, 2.5]), step=2) logger.log_histogram('key', (bins, [0.0, 1.0, 2.0]), step=3) assert dict(logger.dummy_log) == { 'key': [(1, (bins, [0.0, 1.0, 2.0])), (2, (bins, [1.0, 1.5, 2.5])), (3, (bins, [0.0, 1.0, 2.0]))] }
def test_dummy_images(): logger = Logger(None, is_dummy=True) img = np.random.rand(10, 10) images = [img, img] logger.log_images('key', images, step=1) logger.log_images('key', images, step=2) logger.log_images('key', images, step=3) assert dict(logger.dummy_log) == { 'key': [(1, images), (2, images), (3, images)] }
def main(): parser=argparse.ArgumentParser() parser.add_argument("--model_save_path", "-s", type = str, default = "./saved_models", help = "path to save the model") parser.add_argument("--data_path", "-d", type = str, default = "./", help = "path to computed mfcc numpy files") parser.add_argument("--log_path", "-l", type = str, default = "./summary/", help = "path to log dir") parser.add_argument("--batch_size", type=int, default=1000, help="batch size") parser.add_argument("--epochs", type=int, default=500, help="batch size") parser.add_argument("--epochs_per_save", type=int, default=10, help="number of epochs after which to save model and training checkpoint") parser.add_argument("--noise", type=float, default=0.0, help="with what probability to add noise to augment training") args=parser.parse_args() BATCH_SIZE = args.batch_size epochs = args.epochs epochs_per_save = args.epochs_per_save curr_dir = os.getcwd() try: os.chdir(args.model_save_path) except OSError: os.mkdir(args.model_save_path) os.chdir(curr_dir) data_path = args.data_path dataset = AudioMFCCDataset(aud_mfcc_file=os.path.join(data_path, "train_aud_mfcc_norm.npy"), aud_mfcc_lengths=os.path.join(data_path,"train_sample_lengths_norm.npy"), data_size=None) print("loaded dataset") loader = DataLoader(dataset, batch_size=BATCH_SIZE, collate_fn=AudioMFCCDataset.pack_batch) print("created iter on loaded data") TOTAL_OBS = dataset.__len__() # dev set not std trimmmed to check if longer audio is also getting properly encoded. dev_dataset = AudioMFCCDataset(aud_mfcc_file=os.path.join(data_path, "dev_aud_mfcc_norm.npy"), aud_mfcc_lengths=os.path.join(data_path,"dev_sample_lengths_norm.npy"), data_size=None) print("loaded dev dataset") dev_loader = DataLoader(dev_dataset, batch_size=BATCH_SIZE, collate_fn=AudioMFCCDataset.pack_batch) print("created iter on loaded dev data") # logging information now = datetime.datetime.now() logger = Logger(args.log_path + now.strftime("%Y-%m-%d_%H_%M"), flush_secs=5) aud2vec = Seq2SeqAutoencoder(dataset.num_features(), noise_prob=0.0) device = torch.device(DEVICE_ID if torch.cuda.is_available() else "cpu") gpu_aud2vec = aud2vec.to(device) print("model created") print("training begins") losses = gpu_aud2vec.train(loader, dev_loader, epochs, epochs_per_save, args.model_save_path, logger) torch.save(aud2vec.state_dict(), os.path.join(args.model_save_path, "aud2vec.pth"))
def test_unique(): logger = Logger(None, is_dummy=True) for step in range(1, 3): # names that normalize to the same valid name logger.log_value('A v/1', step, step) logger.log_value('A\tv/1', step * 2, step) logger.log_value('A v/1', step * 3, step) assert dict(logger.dummy_log) == { 'A_v/1': [(1, 1), (2, 2)], 'A_v/1/1': [(1, 2), (2, 4)], 'A_v/1/2': [(1, 3), (2, 6)], }
def test_serialization(tmpdir): logger = Logger(str(tmpdir), flush_secs=0.1, dummy_time=256.5) logger.log_value('v/1', 1.5, 1) logger.log_value('v/22', 16.0, 2) time.sleep(0.5) tf_log, = tmpdir.listdir() assert tf_log.read_binary() == ( # step = 0, initial record b'\x18\x00\x00\x00\x00\x00\x00\x00\xa3\x7fK"\t\x00\x00\x00\x00\x00\x08p@\x1a\rbrain.Event:2\xbc\x98!+' # v/1 b'\x19\x00\x00\x00\x00\x00\x00\x00\x8b\xf1\x08(\t\x00\x00\x00\x00\x00\x08p@\x10\x01*\x0c\n\n\n\x03v/1\x15\x00\x00\xc0?,\xec\xc0\x87' # v/22 b'\x1a\x00\x00\x00\x00\x00\x00\x00\x12\x9b\xd8-\t\x00\x00\x00\x00\x00\x08p@\x10\x02*\r\n\x0b\n\x04v/22\x15\x00\x00\x80A\x8f\xa3\xb6\x88' )
def test_real_histo_tuple(tmpdir): """ from tests.test_tensorboard_logger import * import ubelt as ub ub.delete(ub.ensure_app_cache_dir('tf_logger')) tmpdir = ub.ensure_app_cache_dir('tf_logger/runs/run1') """ logger = Logger(str(tmpdir), flush_secs=0.1) bins = [-.5, .5, 1.5, 2.5] logger.log_histogram('hist1', (bins, [0.0, 1.0, 2.0]), step=1) logger.log_histogram('hist1', (bins, [1.0, 1.5, 2.5]), step=2) logger.log_histogram('hist1', (bins, [0.0, 1.0, 2.0]), step=3) tf_log, = glob.glob(str(tmpdir) + '/*') assert os.path.basename(tf_log).startswith('events.out.tfevents.')
def _save(self, checkpoint_dir): file_path = checkpoint_dir + '/ray_SNN_W_epoch_' + str( self.epoch) + '.pickle' # self.model.save_weights(file_path) if self.epoch % 20 == 0: with open(file_path, 'w') as fw: save = {'w_h': self.model.w_h, 'w_o': self.model.w_o} pickle.dump(save, fw) # writer = SummaryWriter(checkpoint_dir) logger = Logger(checkpoint_dir + '/images/') # logger_hist = Logger(checkpoint_dir + '/histograms/') # img = np.random.rand(10, 10) images = [] # mx = np.max(self.w_h[0]) # mn = np.min(self.w_h[0]) for tp in range(self.model.outputs): tpp = self.model.w_o[:, tp] sz = np.ceil((np.sqrt(np.size(tpp)))).astype(int) tpm = np.zeros(sz * sz, dtype=float) tpm[0:len(tpp)] = tpp tpp = tpm tpp = np.reshape(tpp, newshape=[sz, sz]) images.append(scale(tpp)) cimages = combine_matrix(*images) logger.log_images('key_out', [cimages], step=self.epoch) logger.log_histogram('key_out', [self.model.w_o], step=self.epoch) for k in range(len(self.h)): images = [] for tp in range(self.h[k]): tpp = self.model.w_h[k][:, tp] sz = np.ceil((np.sqrt(np.size(tpp)))).astype(int) tpm = np.zeros(sz * sz, dtype=float) tpm[0:np.size(tpp)] = tpp tpp = tpm tpp = np.reshape(tpp, newshape=[sz, sz]) images.append(scale(tpp)) cimages = combine_matrix(*images) logger.log_images('key_' + str(k), [cimages], step=self.epoch) logger.log_histogram('key' + str(k), [self.model.w_h[k]], step=self.epoch) return file_path
def create_experiment_folder(folder='experiments', tag=None, args=None): if folder is None: folder = 'experiments' if os.path.exists(folder + '/to_delete'): shutil.rmtree(folder + '/to_delete') folder = folder.replace(' ', '_') if os.path.exists(folder): print(" - Folder for experiments found") else: print(" - Creating folder for experiments") os.makedirs(folder) # Load cvs of experiments experiment_csv = '/'.join([folder, "experiments.csv"]) if os.path.isfile(experiment_csv): print(" - Loading experiments.csv file") df = pd.read_csv(experiment_csv, index_col=0) else: print(" - experiments.csv not found, creating one") df = pd.DataFrame(columns=args.keys()) df.to_csv(experiment_csv) #df = df.append(args, ignore_index=True) id = 0 if len(df.index) == 0 or pd.isna( df.index[-1]) else df.index.max() + 1 df.loc[df.index.max() + 1] = pd.Series(args) df.to_csv(experiment_csv) # Creating folder for experiment if tag is None: experiment_folder = '/'.join([folder, str(df.index[-1])]) else: experiment_folder = '/'.join([folder, str(df.index[-1]) + '_' + tag]) os.makedirs(experiment_folder) logs_folder = experiment_folder + '/logs' logger = Logger(logs_folder + "/extra") del df return id, logger, logs_folder, experiment_csv, experiment_folder
def main(): global args args = parser.parse_args() model = define_model(is_resnet=False, is_densenet=False, is_senet=True) model_final = net.modelfinal() model = model.cuda() model_final = model_final.cuda() batch_size = 1 train_loader = loaddata.getTrainingData(batch_size) optimizer = torch.optim.Adam(model_final.parameters(), args.lr, weight_decay=args.weight_decay) logger = Logger(logdir='experiment_cnn', flush_secs=1) for epoch in range(args.start_epoch, args.epochs): adjust_learning_rate(optimizer, epoch) train(train_loader, model,model_final, optimizer, epoch,logger ) if epoch % 10 == 0: save_checkpoint({'state_dict': model.state_dict()},filename='modelcheckpoint.pth.tar') save_checkpoint({'state_dict_final': model_final.state_dict()},filename='finalmodelcheckpoint.pth.tar') print('save: (epoch: %d)' % (epoch+ 1))
def use_tensorboard(self): from tensorboard_logger import Logger self.logger = Logger(self.log_path)
import sys import os import warnings from model import CSRNet from utils import save_checkpoint from tensorboard_logger import Logger logger = Logger(logdir="./tensorboard_logs", flush_secs=10) import torch import torch.nn as nn from torch.autograd import Variable from torchvision import datasets, transforms import numpy as np import argparse import json import cv2 import dataset import time parser = argparse.ArgumentParser(description='PyTorch CSRNet') parser.add_argument('train_json', metavar='TRAIN', help='path to train json') parser.add_argument('test_json', metavar='TEST', help='path to test json') parser.add_argument('--pre', '-p', metavar='PRETRAINED',
def main(args, net=None): global oldassignment datadir = get_data_dir(args.db) outputdir = get_output_dir(args.db) logger = None if args.tensorboard: # One should create folder for storing logs loggin_dir = os.path.join(outputdir, 'runs', 'DCC') if not os.path.exists(loggin_dir): os.makedirs(loggin_dir) loggin_dir = os.path.join(loggin_dir, '%s' % (args.id)) if args.clean_log: remove_files_in_dir(loggin_dir) logger = Logger(loggin_dir) use_cuda = torch.cuda.is_available() # Set the seed for reproducing the results random.seed(args.manualSeed) np.random.seed(args.manualSeed) torch.manual_seed(args.manualSeed) if use_cuda: torch.cuda.manual_seed_all(args.manualSeed) torch.backends.cudnn.enabled = True cudnn.benchmark = True startepoch = 0 kwargs = {'num_workers': 5, 'pin_memory': True} if use_cuda else {} # setting up dataset specific objects trainset = DCCPT_data(root=datadir, train=True, h5=args.h5) testset = DCCPT_data(root=datadir, train=False, h5=args.h5) numeval = len(trainset) + len(testset) # extracting training data from the pretrained.mat file data, labels, pairs, Z, sampweight = makeDCCinp(args) # For simplicity, I have created placeholder for each datasets and model load_pretraining = True if net is None else False if net is None: net = dp.load_predefined_extract_net(args) # reshaping data for some datasets if args.db == 'cmnist': data = data.reshape((-1, 1, 28, 28)) elif args.db == 'ccoil100': data = data.reshape((-1, 3, 128, 128)) elif args.db == 'cytf': data = data.reshape((-1, 3, 55, 55)) elif args.db == 'cyale': data = data.reshape((-1, 1, 168, 192)) totalset = torch.utils.data.ConcatDataset([trainset, testset]) # computing and initializing the hyperparams _sigma1, _sigma2, _lambda, _delta, _delta1, _delta2, lmdb, lmdb_data = computeHyperParams(pairs, Z) oldassignment = np.zeros(len(pairs)) stopping_threshold = int(math.ceil(cfg.STOPPING_CRITERION * float(len(pairs)))) # Create dataset and random batch sampler for Finetuning stage trainset = DCCFT_data(pairs, data, sampweight) batch_sampler = DCCSampler(trainset, shuffle=True, batch_size=args.batchsize) # copying model params from Pretrained (SDAE) weights file if load_pretraining: load_weights(args, outputdir, net) # creating objects for loss functions, U's are initialized to Z here # Criterion1 corresponds to reconstruction loss criterion1 = DCCWeightedELoss(size_average=True) # Criterion2 corresponds to sum of pairwise and data loss terms criterion2 = DCCLoss(Z.shape[0], Z.shape[1], Z, size_average=True) if use_cuda: net.cuda() criterion1 = criterion1.cuda() criterion2 = criterion2.cuda() # setting up data loader for training and testing phase trainloader = torch.utils.data.DataLoader(trainset, batch_sampler=batch_sampler, **kwargs) testloader = torch.utils.data.DataLoader(totalset, batch_size=args.batchsize, shuffle=False, **kwargs) # setting up optimizer - the bias params should have twice the learning rate w.r.t. weights params bias_params = filter(lambda x: ('bias' in x[0]), net.named_parameters()) bias_params = list(map(lambda x: x[1], bias_params)) nonbias_params = filter(lambda x: ('bias' not in x[0]), net.named_parameters()) nonbias_params = list(map(lambda x: x[1], nonbias_params)) optimizer = optim.Adam([{'params': bias_params, 'lr': 2*args.lr}, {'params': nonbias_params}, {'params': criterion2.parameters(), 'lr': args.lr}, ], lr=args.lr, betas=(0.99, 0.999)) # this is needed for WARM START if args.resume: filename = outputdir+'/FTcheckpoint_%d.pth.tar' % args.level if os.path.isfile(filename): print("==> loading checkpoint '{}'".format(filename)) checkpoint = torch.load(filename) net.load_state_dict(checkpoint['state_dict']) criterion2.load_state_dict(checkpoint['criterion_state_dict']) startepoch = checkpoint['epoch'] optimizer.load_state_dict(checkpoint['optimizer']) _sigma1 = checkpoint['sigma1'] _sigma2 = checkpoint['sigma2'] _lambda = checkpoint['lambda'] _delta = checkpoint['delta'] _delta1 = checkpoint['delta1'] _delta2 = checkpoint['delta2'] else: print("==> no checkpoint found at '{}'".format(filename)) raise ValueError # This is the actual Algorithm flag = 0 for epoch in range(startepoch, args.nepoch): if logger: logger.log_value('sigma1', _sigma1, epoch) logger.log_value('sigma2', _sigma2, epoch) logger.log_value('lambda', _lambda, epoch) train(trainloader, net, optimizer, criterion1, criterion2, epoch, use_cuda, _sigma1, _sigma2, _lambda, logger) Z, U, change_in_assign, assignment = test(testloader, net, criterion2, epoch, use_cuda, _delta, pairs, numeval, flag, logger) if flag: # As long as the change in label assignment < threshold, DCC continues to run. # Note: This condition is always met in the very first epoch after the flag is set. # This false criterion is overwritten by checking for the condition twice. if change_in_assign > stopping_threshold: flag += 1 if flag == 4: break if((epoch+1) % args.M == 0): _sigma1 = max(_delta1, _sigma1 / 2) _sigma2 = max(_delta2, _sigma2 / 2) if _sigma2 == _delta2 and flag == 0: # Start checking for stopping criterion flag = 1 # Save checkpoint index = (epoch // args.M) * args.M save_checkpoint({'epoch': epoch+1, 'state_dict': net.state_dict(), 'criterion_state_dict': criterion2.state_dict(), 'optimizer': optimizer.state_dict(), 'sigma1': _sigma1, 'sigma2': _sigma2, 'lambda': _lambda, 'delta': _delta, 'delta1': _delta1, 'delta2': _delta2, }, index, filename=outputdir) output = {'Z': Z, 'U': U, 'gtlabels': labels, 'w': pairs, 'cluster':assignment} sio.savemat(os.path.join(outputdir, 'features'), output)
args = options.parser.parse_args() torch.manual_seed(args.seed) torch.cuda.manual_seed_all(args.seed) device = torch.device("cuda") t_max = 750 t_max_ctc = 2800 if args.activity_net: t_max = 200 t_max_ctc = 400 dataset = Dataset(args) os.system('mkdir -p ./ckpt/') os.system('mkdir -p ./logs/' + args.model_name) logger = Logger('./logs/' + args.model_name) model = Model(dataset.feature_size, dataset.num_class, dataset.labels101to20).to(device) if args.eval_only and args.pretrained_ckpt is None: print('***************************') print('Pretrained Model NOT Loaded') print('Evaluating on Random Model') print('***************************') if args.pretrained_ckpt is not None: model.load_state_dict(torch.load(args.pretrained_ckpt)) best_acc = 0 optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=0.0005)
tf.flags.DEFINE_string('rnn_type', 'lstm', 'rnn_type: lstm / gru') tf.flags.DEFINE_integer('batch_size', 32, 'batch size') tf.flags.DEFINE_float('drop_keep_rate', 0.8, 'drop_keep_rate') tf.flags.DEFINE_float('learning_rate', 1e-3, 'learning rate') tf.flags.DEFINE_float('lambda_l2', 1e-3, 'lambda_l2') tf.flags.DEFINE_float('clipper', 30, 'clipper') FLAGS._parse_flags() random.seed(1234) np.random.seed(1234) tf.set_random_seed(1234) # Tensorboard Part name = utils.get_time_name('run_{}'.format(FLAGS.model)) train_logger = Logger('runs/{}/train'.format(name)) dev_logger = Logger('runs/{}/dev'.format(name)) test_logger = Logger('runs/{}/test'.format(name)) # Predict Part train_predict_file = 'runs/{}/train_predict.tsv'.format(name) dev_predict_file = 'runs/{}/dev_predict.tsv'.format(name) test_predict_file = 'runs/{}/test_predict.tsv'.format(name) # Logger Part # Change the log_file FLAGS.log_file = 'runs/{}/SemEval18.log'.format(name) logger = utils.get_logger(FLAGS.log_file) logger.info(FLAGS.__flags)
# -*- coding: utf-8 -*- # @TIME : 2021/3/26 12:37 # @AUTHOR : Xu Bai # @FILE : 5-3-1.TensorBoard.py # @DESCRIPTION : from tensorboard_logger import Logger # ternsorboard --logdir experimient_cnn # 构建logger对象,logdir用来指定log文件路径 # flush_secs指定刷新同步间隔 logger = Logger(logdir='experimient_cnn', flush_secs=2) for ii in range(100): logger.log_value('loss', 10 - ii * .5, step=ii) logger.log_value('accuracy', ii * .5 / 10)
def train(args): # model print(args.model_name) config.train_data = config.train_data + str(args.fold) + '.pth' # config.train_data = config.train_data + 'trainsfer_' + str(args.fold) + '.pth' config.model_name = args.model_name model = getattr(models, config.model_name)() model = model.to(device) # data if args.model_kind == 1: import dataset2 train_dataset = dataset2.ECGDataset(data_path=config.train_data, train=True, transform=True) train_dataloader = DataLoader(train_dataset, collate_fn=my_collate_fn, batch_size=config.batch_size, shuffle=True, num_workers=6) else: train_dataset = ECGDataset(data_path=config.train_data, train=True, transform=True) train_dataloader = DataLoader( train_dataset, #collate_fn=my_collate_fn, batch_size=config.batch_size, shuffle=True, num_workers=6) val_dataset = ECGDataset(data_path=config.train_data, train=False) val_dataloader = DataLoader(val_dataset, batch_size=config.batch_size, num_workers=6) print("train_datasize", len(train_dataset), "val_datasize", len(val_dataset)) # optimizer and loss optimizer = optim.Adam(model.parameters(), lr=config.lr) # optimizer = optim.RMSprop(model.parameters(), lr=config.lr) w = torch.tensor(train_dataset.wc, dtype=torch.float).to(device) if args.model_kind == 1: criterion = utils.WeightedMultilabel(w) print(1) else: criterion = utils2.WeightedMultilabel(w) # criterion = utils.My_loss(w) # 模型保存文件夹 model_save_dir = '%s/%s' % (config.ckpt + str(args.model_kind), config.model_name + '_' + str(args.fold)) args.ckpt = model_save_dir # if args.ex: model_save_dir += args.ex best_f1 = -1 lr = config.lr start_epoch = 1 stage = 1 # 从上一个断点,继续训练 if args.resume: if os.path.exists(args.ckpt): # 这里是存放权重的目录 # model_save_dir = args.ckpt current_w = torch.load(os.path.join(args.ckpt, config.current_w)) best_w = torch.load(os.path.join(model_save_dir, config.best_w)) best_f1 = best_w['best_f'] start_epoch = current_w['epoch'] + 1 lr = current_w['lr'] stage = current_w['stage'] model.load_state_dict(current_w['state_dict']) # 如果中断点恰好为转换stage的点 if start_epoch - 1 in config.stage_epoch: stage += 1 lr /= config.lr_decay utils.adjust_learning_rate(optimizer, lr) model.load_state_dict(best_w['state_dict']) print("=> loaded checkpoint (epoch {})".format(start_epoch - 1)) else: path = '%s/%s' % (config.ckpt, config.model_name + '_transfer') print(path) current_w = torch.load(os.path.join(path, config.best_w)) model.load_state_dict(current_w['state_dict']) logger = Logger(logdir=model_save_dir, flush_secs=2) # =========>开始训练<========= val_loss = 10 val_f1 = -1 state = {} for epoch in range(start_epoch, config.max_epoch + 1): since = time.time() train_loss, train_f1, best_f1 = train_epoch( model, optimizer, criterion, train_dataloader, epoch, lr, best_f1, val_dataloader, model_save_dir, state, 0) # if epoch % 2 == 1: val_loss, val_f1, _, _ = val_epoch(model, criterion, val_dataloader) print( '#epoch:%02d stage:%d train_loss:%.3e train_f1:%.3f val_loss:%0.3e val_f1:%.3f time:%s' % (epoch, stage, train_loss, train_f1, val_loss, val_f1, utils.print_time_cost(since))) logger.log_value('train_loss', train_loss, step=epoch) logger.log_value('train_f1', train_f1, step=epoch) logger.log_value('val_loss', val_loss, step=epoch) logger.log_value('val_f1', val_f1, step=epoch) state = { "state_dict": model.state_dict(), "epoch": epoch, "loss": val_loss, 'f1': val_f1, 'lr': lr, 'stage': stage, "best_f": best_f1 } if best_f1 < val_f1: save_ckpt(state, best_f1 < val_f1, model_save_dir) print('save best') else: save_ckpt(state, False, model_save_dir) best_f1 = max(best_f1, val_f1) if epoch in config.stage_epoch: stage += 1 lr /= config.lr_decay # best_w = os.path.join(model_save_dir, config.best_w) # model.load_state_dict(torch.load(best_w)['state_dict']) print("*" * 10, "step into stage%02d lr %.3ef" % (stage, lr)) utils.adjust_learning_rate(optimizer, lr)
def train(args): # model model = getattr(models, config.model_name)() if args.ckpt and not args.resume: state = torch.load(args.ckpt, map_location='cpu') model.load_state_dict(state['state_dict']) print('train with pretrained weight val_f1', state['f1']) model = model.to(device) # data train_dataset = ECGDataset(data_path=config.train_data, train=True) train_dataloader = DataLoader(train_dataset, batch_size=config.batch_size, shuffle=True, num_workers=6) val_dataset = ECGDataset(data_path=config.train_data, train=False) val_dataloader = DataLoader(val_dataset, batch_size=config.batch_size, num_workers=4) print("train_datasize", len(train_dataset), "val_datasize", len(val_dataset)) # optimizer and loss optimizer = optim.Adam(model.parameters(), lr=config.lr) w = torch.tensor(train_dataset.wc, dtype=torch.float).to(device) criterion = utils.WeightedMultilabel(w) # 模型保存文件夹 model_save_dir = '%s/%s_%s' % (config.ckpt, config.model_name, time.strftime("%Y%m%d%H%M")) if args.ex: model_save_dir += args.ex best_f1 = -1 lr = config.lr start_epoch = 1 stage = 1 # 从上一个断点,继续训练 if args.resume: if os.path.exists(args.ckpt): # 这里是存放权重的目录 model_save_dir = args.ckpt current_w = torch.load(os.path.join(args.ckpt, config.current_w)) best_w = torch.load(os.path.join(model_save_dir, config.best_w)) best_f1 = best_w['loss'] start_epoch = current_w['epoch'] + 1 lr = current_w['lr'] stage = current_w['stage'] model.load_state_dict(current_w['state_dict']) # 如果中断点恰好为转换stage的点 if start_epoch - 1 in config.stage_epoch: stage += 1 lr /= config.lr_decay utils.adjust_learning_rate(optimizer, lr) model.load_state_dict(best_w['state_dict']) print("=> loaded checkpoint (epoch {})".format(start_epoch - 1)) logger = Logger(logdir=model_save_dir, flush_secs=2) # =========>开始训练<========= for epoch in range(start_epoch, config.max_epoch + 1): since = time.time() train_loss, train_f1 = train_epoch(model, optimizer, criterion, train_dataloader, show_interval=100) val_loss, val_f1 = val_epoch(model, criterion, val_dataloader) print( '#epoch:%02d stage:%d train_loss:%.3e train_f1:%.3f val_loss:%0.3e val_f1:%.3f time:%s\n' % (epoch, stage, train_loss, train_f1, val_loss, val_f1, utils.print_time_cost(since))) logger.log_value('train_loss', train_loss, step=epoch) logger.log_value('train_f1', train_f1, step=epoch) logger.log_value('val_loss', val_loss, step=epoch) logger.log_value('val_f1', val_f1, step=epoch) state = { "state_dict": model.state_dict(), "epoch": epoch, "loss": val_loss, 'f1': val_f1, 'lr': lr, 'stage': stage } save_ckpt(state, best_f1 < val_f1, model_save_dir) best_f1 = max(best_f1, val_f1) if epoch in config.stage_epoch: stage += 1 lr /= config.lr_decay best_w = os.path.join(model_save_dir, config.best_w) model.load_state_dict(torch.load(best_w)['state_dict']) print("*" * 10, "step into stage%02d lr %.3ef" % (stage, lr)) utils.adjust_learning_rate(optimizer, lr)
def main(): opt = parse_opts() ecd_name, cls_name = opt.model_name.split('-') cfg.encoder_model = ecd_name cfg.classification_model = cls_name if opt.debug: cfg.debug = opt.debug else: if opt.tensorboard == 'TEST': cfg.tensorboard = opt.model_name else: cfg.tensorboard = opt.tensorboard cfg.flag = opt.flag model, parameters = get_model(2) cfg.video_path = os.path.join(cfg.root_path, cfg.video_path) cfg.annotation_path = os.path.join(cfg.root_path, cfg.annotation_path) cfg.list_all_member() torch.manual_seed(cfg.manual_seed) print('##########################################') print('####### model 仅支持单GPU') print('##########################################') print(model) criterion = nn.CrossEntropyLoss() if cfg.cuda: criterion = criterion.cuda() print('##########################################') print('####### train') print('##########################################') training_data = FaceRecognition(cfg, '/share5/public/lijianwei/faces/', TemporalRandomCrop(14), train_spatial_transform) train_loader = torch.utils.data.DataLoader(training_data, batch_size=cfg.batch_size, shuffle=True, num_workers=cfg.n_threads, drop_last=False, pin_memory=True) optimizer = torch.optim.SGD(parameters, lr=cfg.lr, momentum=0.9, dampening=0.9, weight_decay=1e-3) scheduler = lr_scheduler.ReduceLROnPlateau(optimizer, 'min', patience=cfg.lr_patience) print('##########################################') print('####### val') print('##########################################') validation_data = FaceRecognition(cfg, '/share5/public/lijianwei/faces/', TemporalRandomCrop(14), val_spatial_transform, phase='val') val_loader = torch.utils.data.DataLoader(validation_data, batch_size=cfg.batch_size, shuffle=False, num_workers=cfg.n_threads, drop_last=False, pin_memory=True) print('##########################################') print('####### run') print('##########################################') if cfg.debug: logger = None else: path = get_log_dir(cfg.logdir, name=cfg.tensorboard, flag=cfg.flag) logger = Logger(logdir=path) cfg.save_config(path) for i in range(cfg.begin_epoch, cfg.n_epochs + 1): train_epoch(i, train_loader, model, criterion, optimizer, cfg, logger) validation_loss = val_epoch(i, val_loader, model, criterion, cfg, logger) scheduler.step(validation_loss)
def main(): DIR = args.DIR embedding_file = args.embedding_dir best_network_file = "./model/network_model_pretrain.best.top" print >> sys.stderr, "Read model from ", best_network_file best_network_model = torch.load(best_network_file) embedding_matrix = numpy.load(embedding_file) "Building torch model" worker = network.Network( nnargs["pair_feature_dimention"], nnargs["mention_feature_dimention"], nnargs["word_embedding_dimention"], nnargs["span_dimention"], 1000, nnargs["embedding_size"], nnargs["embedding_dimention"], embedding_matrix).cuda() net_copy(worker, best_network_model) best_network_file = "./model/network_model_pretrain.best.top" print >> sys.stderr, "Read model from ", best_network_file best_network_model = torch.load(best_network_file) manager = network.Network( nnargs["pair_feature_dimention"], nnargs["mention_feature_dimention"], nnargs["word_embedding_dimention"], nnargs["span_dimention"], 1000, nnargs["embedding_size"], nnargs["embedding_dimention"], embedding_matrix).cuda() net_copy(manager, best_network_model) reduced = "" if args.reduced == 1: reduced = "_reduced" print >> sys.stderr, "prepare data for train ..." #train_docs_iter = DataReader.DataGnerater("train"+reduced) train_docs_iter = DataReader.DataGnerater("dev" + reduced) print >> sys.stderr, "prepare data for dev and test ..." dev_docs_iter = DataReader.DataGnerater("dev" + reduced) test_docs_iter = DataReader.DataGnerater("test" + reduced) print "Performance after pretraining..." print "DEV" metric = performance.performance(dev_docs_iter, worker, manager) print "Average:", metric["average"] print "TEST" metric = performance.performance(test_docs_iter, worker, manager) print "Average:", metric["average"] print "***" print sys.stdout.flush() lr = nnargs["lr"] top_k = nnargs["top_k"] model_save_dir = "./model/reinforce/" utils.mkdir(model_save_dir) score_softmax = nn.Softmax() optimizer_manager = optim.RMSprop(manager.parameters(), lr=lr, eps=1e-6) optimizer_worker = optim.RMSprop(worker.parameters(), lr=lr, eps=1e-6) MAX_AVE = 2048 for echo in range(nnargs["epoch"]): start_time = timeit.default_timer() print "Pretrain Epoch:", echo reward_log = Logger(Tensorboard + args.tb + "/acl2018/%d/reward/" % echo, flush_secs=3) entropy_log_manager = Logger(Tensorboard + args.tb + "/acl2018/%d/entropy/worker" % echo, flush_secs=3) entropy_log_worker = Logger(Tensorboard + args.tb + "/acl2018/%d/entropy/manager" % echo, flush_secs=3) #train_docs = utils.load_pickle(args.DOCUMENT + 'train_docs.pkl') train_docs = utils.load_pickle(args.DOCUMENT + 'dev_docs.pkl') docs_by_id = {doc.did: doc for doc in train_docs} ave_reward = [] ave_manager_entropy = [] ave_worker_entropy = [] print >> sys.stderr, "Link docs ..." tmp_data = [] cluster_info = {0: [0]} cluster_list = [0] current_new_cluster = 1 predict_action_embedding = [] choose_action = [] mid = 1 step = 0 statistic = { "worker_hits": 0, "manager_hits": 0, "total": 0, "manager_predict_last": 0, "worker_predict_last": 0 } for data in train_docs_iter.rl_case_generater(shuffle=True): rl = data["rl"] scores_manager, representations_manager = get_score_representations( manager, data) for s, e in zip(rl["starts"], rl["ends"]): action_embeddings = representations_manager[s:e] probs = F.softmax(torch.transpose(scores_manager[s:e], 0, 1)) m = Categorical(probs) this_action = m.sample() index = this_action.data.cpu().numpy()[0] if index == (e - s - 1): should_cluster = current_new_cluster cluster_info[should_cluster] = [] current_new_cluster += 1 else: should_cluster = cluster_list[index] choose_action.append(index) cluster_info[should_cluster].append(mid) cluster_list.append(should_cluster) mid += 1 cluster_indexs = torch.cuda.LongTensor( cluster_info[should_cluster]) action_embedding_predict = torch.mean( action_embeddings[cluster_indexs], 0, keepdim=True) predict_action_embedding.append(action_embedding_predict) tmp_data.append(data) if rl["end"] == True: inside_index = 0 manager_path = [] worker_path = [] doc = docs_by_id[rl["did"]] for data in tmp_data: rl = data["rl"] pair_target = data["pair_target"] anaphoricity_target = 1 - data["anaphoricity_target"] target = numpy.concatenate( (pair_target, anaphoricity_target))[rl["reindex"]] scores_worker, representations_worker = get_score_representations( worker, data) for s, e in zip(rl["starts"], rl["ends"]): action_embeddings = representations_worker[s:e] score = score_softmax( torch.transpose(scores_worker[s:e], 0, 1)).data.cpu().numpy()[0] action_embedding_choose = predict_action_embedding[ inside_index] similarities = torch.sum( torch.abs(action_embeddings - action_embedding_choose), 1) similarities = similarities.data.cpu().numpy() action_probabilities = [] action_list = [] action_candidates = heapq.nlargest( top_k, -similarities) for action in action_candidates: action_index = numpy.argwhere( similarities == -action)[0][0] action_probabilities.append(score[action_index]) action_list.append(action_index) manager_action = choose_action[inside_index] if not manager_action in action_list: action_list.append(manager_action) action_probabilities.append(score[manager_action]) this_target = target[s:e] manager_action = choose_action[inside_index] sample_action = utils.sample_action( numpy.array(action_probabilities)) worker_action = action_list[sample_action] if this_target[worker_action] == 1: statistic["worker_hits"] += 1 if this_target[manager_action] == 1: statistic["manager_hits"] += 1 if worker_action == (e - s - 1): statistic["worker_predict_last"] += 1 if manager_action == (e - s - 1): statistic["manager_predict_last"] += 1 statistic["total"] += 1 inside_index += 1 #link = manager_action link = worker_action m1, m2 = rl['ids'][s + link] doc.link(m1, m2) manager_path.append(manager_action) worker_path.append(worker_action) reward = doc.get_f1() for data in tmp_data: for s, e in zip(rl["starts"], rl["ends"]): ids = rl['ids'][s:e] ana = ids[0, 1] old_ant = doc.ana_to_ant[ana] doc.unlink(ana) costs = rl['costs'][s:e] for ant_ind in range(e - s): costs[ant_ind] = doc.link(ids[ant_ind, 0], ana, hypothetical=True, beta=1) doc.link(old_ant, ana) #costs = autograd.Variable(torch.from_numpy(costs).type(torch.cuda.FloatTensor)) inside_index = 0 worker_entropy = 0.0 for data in tmp_data: new_step = step # worker scores_worker, representations_worker = get_score_representations( worker, data, dropout=nnargs["dropout_rate"]) optimizer_worker.zero_grad worker_loss = None for s, e in zip(rl["starts"], rl["ends"]): costs = rl['costs'][s:e] costs = autograd.Variable( torch.from_numpy(costs).type( torch.cuda.FloatTensor)) action = worker_path[inside_index] score = F.softmax( torch.transpose(scores_worker[s:e], 0, 1)) if not score.size()[1] == costs.size()[0]: continue score = torch.squeeze(score) baseline = torch.sum(costs * score) this_cost = torch.log( score[action]) * -1.0 * (reward - baseline) if worker_loss is None: worker_loss = this_cost else: worker_loss += this_cost worker_entropy += torch.sum( score * torch.log(score + 1e-7) ).data.cpu().numpy()[ 0] #+ 0.001*torch.sum(score*torch.log(score+1e-7)) inside_index += 1 worker_loss.backward() torch.nn.utils.clip_grad_norm(worker.parameters(), nnargs["clip"]) optimizer_worker.step() ave_worker_entropy.append(worker_entropy) if len(ave_worker_entropy) >= MAX_AVE: ave_worker_entropy = ave_worker_entropy[1:] entropy_log_worker.log_value( 'entropy', float(sum(ave_worker_entropy)) / float(len(ave_worker_entropy)), new_step) new_step += 1 inside_index = 0 manager_entropy = 0.0 for data in tmp_data: new_step = step rl = data["rl"] ave_reward.append(reward) if len(ave_reward) >= MAX_AVE: ave_reward = ave_reward[1:] reward_log.log_value( 'reward', float(sum(ave_reward)) / float(len(ave_reward)), new_step) scores_manager, representations_manager = get_score_representations( manager, data, dropout=nnargs["dropout_rate"]) optimizer_manager.zero_grad manager_loss = None for s, e in zip(rl["starts"], rl["ends"]): score = F.softmax( torch.transpose(scores_manager[s:e], 0, 1)) costs = rl['costs'][s:e] costs = autograd.Variable( torch.from_numpy(costs).type( torch.cuda.FloatTensor)) if not score.size()[1] == costs.size()[0]: continue action = manager_path[inside_index] score = torch.squeeze(score) baseline = torch.sum(costs * score) this_cost = torch.log(score[action]) * -1.0 * ( reward - baseline ) # + 0.001*torch.sum(score*torch.log(score+1e-7)) #this_cost = torch.sum(score*costs) + 0.001*torch.sum(score*torch.log(score+1e-7)) if manager_loss is None: manager_loss = this_cost else: manager_loss += this_cost manager_entropy += torch.sum( score * torch.log(score + 1e-7)).data.cpu().numpy()[0] inside_index += 1 manager_loss.backward() torch.nn.utils.clip_grad_norm(manager.parameters(), nnargs["clip"]) optimizer_manager.step() ave_manager_entropy.append(manager_entropy) if len(ave_manager_entropy) >= MAX_AVE: ave_manager_entropy = ave_manager_entropy[1:] entropy_log_manager.log_value( 'entropy', float(sum(ave_manager_entropy)) / float(len(ave_manager_entropy)), new_step) new_step += 1 step = new_step tmp_data = [] cluster_info = {0: [0]} cluster_list = [0] current_new_cluster = 1 mid = 1 predict_action_embedding = [] choose_action = [] end_time = timeit.default_timer() print >> sys.stderr, "TRAINING Use %.3f seconds" % (end_time - start_time) print >> sys.stderr, "save model ..." #print "Top k",top_k print "Worker Hits", statistic[ "worker_hits"], "Manager Hits", statistic[ "manager_hits"], "Total", statistic["total"] print "Worker predict last", statistic[ "worker_predict_last"], "Manager predict last", statistic[ "manager_predict_last"] #torch.save(network_model, model_save_dir+"network_model_rl_worker.%d"%echo) #torch.save(ana_network, model_save_dir+"network_model_rl_manager.%d"%echo) print "DEV" metric = performance.performance(dev_docs_iter, worker, manager) print "Average:", metric["average"] print "DEV manager" metric = performance_manager.performance(dev_docs_iter, worker, manager) print "Average:", metric["average"] print "TEST" metric = performance.performance(test_docs_iter, worker, manager) print "Average:", metric["average"] print sys.stdout.flush()
num_workers=4, drop_last=True) tag_size = DatasetDict[set_].n_classes model = LSTMTagger(args.nhid, tag_size, args.batch_size, use_gpu, args.nlayers, args.dropout, args.mode) if use_gpu: model.cuda() loss_function = nn.NLLLoss() optimizer = optim.SGD(model.parameters(), lr=args.lr) # optional scheduler # scheduler = optim.lr_scheduler.StepLR(optimizer, 100000, gamma=0.1) logger = Logger(os.path.join('logs', args.name)) def test(dataloader): with torch.no_grad(): if args.set in ['val', 'test']: model.load_state_dict( torch.load(os.path.join('models', args.test_name))) running_acc = 0 model.eval() for batch_idx, sample in enumerate(dataloader): x, y = sample['data'].transpose(0,1), sample['label'].transpose(0,1) if use_gpu: x, y = x.cuda(), y.cuda() pred = model(x) pred = pred.argmax(2) correct = y.eq(pred.long()).sum()
"[email protected]": [], "[email protected]": [], "[email protected]": [], "[email protected]": [] } best_mAP = -1 criterion = BaS_Net_loss(config.alpha) optimizer = torch.optim.Adam(net.parameters(), lr=config.lr[0], betas=(0.9, 0.999), weight_decay=0.0005) logger = Logger(config.log_path) loader_iter = iter(train_loader) for step in tqdm(range(1, config.num_iters + 1), total=config.num_iters, dynamic_ncols=True): if step > 1 and config.lr[step - 1] != config.lr[step - 2]: for param_group in optimizer.param_groups: param_group["lr"] = config.lr[step - 1] train(net, train_loader, loader_iter, optimizer, criterion, logger, step) test(net, config, logger, test_loader, test_info, step)