class ClfModel(nn.Module): def __init__(self, config, n_class, input_info): super(ClfModel, self).__init__() self.forward_model = FilmedNet(config=config, n_class=n_class, input_info=input_info) # Init Loss self.loss_func = CrossEntropyLoss() if USE_CUDA: self.forward_model.cuda() self.loss_func.cuda() if config["weights_to_load"]: self.load_state_dict(torch.load(config["weights_to_load"]), strict=False) # To avoid batch norm problem, set training to false self.forward_model.train(mode=False) else: init_modules(self.modules()) def forward(self, x): return self.forward_model(x) def optimize(self, x, y): yhat = self.forward_model(x) loss = self.loss_func(yhat, y) self.forward_model.optimizer.zero_grad() loss.backward() # for param in self.forward_model.parameters(): # #logging.debug(param.grad.data.sum()) # param.grad.data.clamp_(-1., 1.) self.forward_model.optimizer.step() return loss.data.cpu(), yhat def new_task(self, num_task): raise NotImplementedError("Not available yet") if num_task == 0: self.forward_model.use_film = False # todo : change hardcoded, do scheduler of something like this ? if num_task > 0: # todo : check resnet self.forward_model.use_film = True for param_group in self.forward_model.optimizer.param_groups: if param_group['name'] == "base_net_params": param_group['lr'] = self.forward_model.after_lr elif param_group['name'] == "film_params": param_group['lr'] = self.forward_model.default_lr
def run(): from config import get_config config = get_config() print('%s/train_embeddings.csv' % config.result_dir) result_dir = config.result_dir #.replace("results", "best_results") print('%s/train_embeddings.csv' % result_dir) if os.path.exists( '%s/train_embeddings.csv' % result_dir) and os.path.exists( '%s/test_embeddings.csv' % result_dir): return True if not os.path.exists(result_dir): os.makedirs(result_dir) print("Saved Module Trainer Not Return") import losses import models from utils.make_dirs import create_dirs from datasets import loaders from torchsample.modules import ModuleTrainer create_dirs() cuda_device = -1 model = getattr(models, config.network).get_network()( channel=config.network_channel, embedding_size=config.embedding) check_point = os.path.join(config.result_dir, "ckpt.pth.tar") if os.path.isfile(check_point): print("=> loading checkpoint '{}'".format(check_point)) checkpoint = torch.load(check_point) model.load_state_dict(checkpoint['state_dict']) print("=> loaded checkpoint '{}' (epoch {})".format( check_point, checkpoint['epoch'])) else: print("=> no checkpoint found at '{}'".format(check_point)) #criterion = getattr(losses, config.loss)() criterion = CrossEntropyLoss() if config.cuda: model.cuda() criterion.cuda() trainer = ModuleTrainer(model) trainer.compile(loss=criterion, optimizer='adam') if config.cuda: cuda_device = 0 tr_data_loader, val_data_loader, te_data_loader = getattr( loaders, config.loader_name)(train=False, val=True) tr_y_pred = trainer.predict_loader(tr_data_loader, cuda_device=cuda_device) save_embeddings(tr_y_pred, '%s/train_embeddings.csv' % result_dir) save_labels(tr_data_loader, '%s/train_labels.csv' % result_dir) val_y_pred = trainer.predict_loader(val_data_loader, cuda_device=cuda_device) save_embeddings(val_y_pred, '%s/val_embeddings.csv' % result_dir) save_labels(val_data_loader, '%s/val_labels.csv' % result_dir) te_y_pred = trainer.predict_loader(te_data_loader, cuda_device=cuda_device) save_embeddings(te_y_pred, '%s/test_embeddings.csv' % result_dir) save_labels(te_data_loader, '%s/test_labels.csv' % result_dir)
def run(): device = 0 if torch.cuda.is_available() else -1 config = BaseConfig() logging.info('%s_cross_entropy/ckpt.pth.tar' % config.result_dir) if os.path.exists('%s_cross_entropy/ckpt.pth.tar' % config.result_dir): return True logging.info("Triplet Trainer Not Return") create_dirs() tr_data_loader, val_data_loader, te_data_loader = loaders.data_loaders( shuffle=True) model = getattr(models, config.network)(num_classes=len(tr_data_loader.dataset.y)) model criterion = CrossEntropyLoss() if device == 0: model.cuda() criterion.cuda() trainer = ModuleTrainer(model) epochs = config.epochs callbacks = [ EarlyStopping(monitor='val_acc', patience=20), ModelCheckpoint('%s_cross_entropy' % config.result_dir, save_best_only=True, verbose=1), CSVLogger("%s_cross_entropy/logger.csv" % config.result_dir) ] metrics = [CategoricalAccuracy()] trainer.compile(loss=criterion, optimizer='adam', metrics=metrics) trainer.set_callbacks(callbacks) trainer.fit_loader(tr_data_loader, val_loader=val_data_loader, num_epoch=epochs, verbose=2, cuda_device=device) tr_loss = trainer.evaluate_loader(tr_data_loader, cuda_device=device) logging.info(tr_loss) val_loss = trainer.evaluate_loader(val_data_loader, cuda_device=device) logging.info(val_loss) te_loss = trainer.evaluate_loader(te_data_loader, cuda_device=device) logging.info(te_loss) with open('%s_cross_entropy' % config.log_path, "a") as f: f.write('Train %s\nVal:%s\nTest:%s\n' % (str(tr_loss), str(val_loss), te_loss))
def test_unet() -> None: LEARN_RATE = 1e-4 batch_size = 1 channels = 1 n_classes = 2 model = UNet3d(initial_features=16) print(f"{ctime()}: Built U-Net.") total_params = sum(p.numel() for p in model.parameters()) trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad) print(f"{ctime()}: Total parameters: {total_params} ({trainable_params} trainable).") model.half() model.cuda() criterion = CrossEntropyLoss() criterion.cuda() print(f"{ctime()}: Creating Dataset...") subjects = get_cc539_subjects() transform = compose_transforms() subj_dataset = tio.ImagesDataset(subjects, transform=transform) # batch size has to be 1 for variable-sized inputs print(f"{ctime()}: Creating DataLoader...") training_loader = DataLoader(subj_dataset, batch_size=1, num_workers=8) print(f"{ctime()}: Created DataLoader...") filterwarnings("ignore", message="Image.*has negative values.*") for i, subjects_batch in enumerate(training_loader): print(f"{ctime()}: Augmenting input...") img = subjects_batch["img"][tio.DATA] print(f"{ctime()}: Augmented input...") target = subjects_batch["label"][tio.DATA] # if we are using half precision, must also half inputs img.half() target.half() # iF we don't convert inputs tensor to CUDA, we get an; # "Could not run 'aten::slow_conv3d_forward' with arguments from the # 'CUDATensorId' backend. 'aten::slow_conv3d_forward' is only available # for these backends: [CPUTensorId, VariableTensorId] # error. If we do, we run out of memory (since inputs are freaking # brains) img = img.cuda() x = F.interpolate(img, size=(90, 90, 90)) print(f"{ctime()}: Running model with batch of one brain...") out = model(x) print(f"{ctime()}: Got output tensor from one brain...") loss = criterion(out, target) print(f"{ctime()}: Computed loss for batch size of 1 brain...") raise
def train(net, cuda, start_epoch, epochs, batch_size, numpy_data, vocab, resume, output_dir): logger = Logger(os.path.join(output_dir, 'train_log.txt')) # Adam optimizer TODO: add optimizer selection optimizer = optim.Adam(filter(lambda p: p.requires_grad, net.parameters()), lr=0.001) # If loading from checkpoint if resume: if os.path.isfile(resume): print("=> loading checkpoint '{}'".format(resume)) checkpoint = torch.load(resume) start_epoch = checkpoint['epoch'] #best_prec1 = checkpoint['best_prec1'] net.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) print("=> loaded checkpoint '{}' (epoch {})" .format(resume, checkpoint['epoch'])) else: print("=> no checkpoint found at '{}', rerun with correct checkpoint".format(resume)) exit(0) criterion = CrossEntropyLoss() net.train() # Set cuda: if cuda: net = net.cuda() criterion = criterion.cuda() for epoch in range(start_epoch, epochs): print("Epoch {0}/{1}: {2}%".format(epoch, epochs, 100*float(epoch)/epochs)) # Shuffle for new set of mini batches random.shuffle(numpy_data) # Loop dataset in chunks of size batch_size for start, end in tqdm(utils.batch_index_gen(batch_size, len(numpy_data))): loss = forward_backward_pass(net, numpy_data[start:end], vocab, optimizer, batch_size, cuda, criterion) # Save model each epoch save_checkpoint({ 'epoch': epoch + 1, 'state_dict': net.state_dict(), 'optimizer' : optimizer.state_dict(), }, output_dir)
def run(operation): model = GoogLeNet(aux_logits=False) model.load_state_dict(torch.load(PATH, map_location=torch.device('cpu'))) criterion = CrossEntropyLoss() optimizer = SGD(model.parameters(), lr=0.001, momentum=0.9) if torch.cuda.is_available(): model = model.cuda() criterion = criterion.cuda() if operation == 'test': # test model test_data = getTestData() test_loader = getTestLoader() print('start testing model...') test_model(test_data=test_data, test_loader=test_loader, model=model) elif operation == 'train': # train model # train_data = getTrainData() train_loader = getTrainLoader() train_model(data_loader=train_loader, model=model, criterion=criterion, optimizer=optimizer, path=PATH, epochs=1000)
decoder = AttnDecoderRNN( d_dec_input, d_enc * 2, d_dec, vocab_size=len(en.vocab), pad_idx=en.vocab.stoi['<pad>'], bos_token=en.vocab.stoi['<bos>'], eos_token=en.vocab.stoi['<eos>'], ) criterion = CrossEntropyLoss() if torch.cuda.is_available(): print("Using cuda") encoder.cuda() decoder.cuda() criterion.cuda() learning_rate = 0.01 encoder_optimizer = optim.RMSprop(encoder.parameters(), lr=learning_rate) decoder_optimizer = optim.RMSprop(decoder.parameters(), lr=learning_rate) for epoch in range(1, 50): for b, batch in enumerate(train_loader): if b % 1000 == 0: for val_b, val_batch in enumerate(val_loader): sampled_outs_ = deploy(encoder, decoder, val_batch.src) sampled_outs = sampler(sampled_outs_) targets = sampler(val_batch.trg) for i in range(min(10, val_batch.src.size(1))): print("----")
from models import NoiseFilter from dataset import UCFCrime gpu_id = 1 iter_size = 16 frames_per_feat = 1 if __name__ == '__main__': feature_path = "/home/zjx/data/UCF_Crimes/C3D_features/c3d_fc6_features.hdf5" ucf_crime = UCFCrime(feature_path, graph_generator) model = NoiseFilter(nfeat=4096, nclass=2) criterion = CrossEntropyLoss() if gpu_id != -1: model = model.cuda(gpu_id) criterion = criterion.cuda(gpu_id) optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9, weight_decay=0.0005) iter_count = 0 avg_loss_train = 0 for epoch in range(8): model.train() for step, data in enumerate(train_loader): (feat, adj), is_normal, vid = data feat, adj, is_normal = Variable(feat), Variable(adj), Variable(is_normal) if gpu_id != -1: feat = feat.cuda(gpu_id) adj = adj.cuda(gpu_id)
class Trainer: def __init__(self, train_data, val_data, model, lr=0.003, batchsize=100): self.batchsize = batchsize self.train_data = train_data self.val_data = val_data self.labels = train_data.labels self.train_loader = DataLoader( dataset=train_data, batch_size=batchsize, shuffle=True ) self.val_loader = DataLoader( dataset=val_data, batch_size=batchsize, shuffle=True ) self.model = model(len(train_data.labels)) self.clear_training_logs() self.set_learningrate(lr) self.criterion = CrossEntropyLoss() if torch.cuda.is_available(): self.model = self.model.cuda() self.criterion = self.criterion.cuda() def set_learningrate(self, lr): self.lr = lr self.optimizer = Adam(self.model.parameters(), lr=lr) def clear_training_logs(self): # empty list to store training losses self.train_losses = [] self.train_accuracies = [] # empty list to store validation losses self.val_losses = [] self.val_accuracies = [] def train_epochs(self, n_epochs, lr=None): if lr is None: lr = self.lr for epoch in range(n_epochs): # training self.model.train() status = tqdm(total=len(self.train_loader), desc="Epoch: " + str(epoch)) self.model.train() batch_losses = [] batch_accuracies = [] for batch_idx, (x_train, y_train) in enumerate(self.train_loader): if torch.cuda.is_available(): x_train = x_train.cuda() y_train = y_train.cuda() x_train = Variable(x_train) y_train = Variable(y_train) # clearing the Gradients of the model parameters self.optimizer.zero_grad() # prediction for training and validation set output_train = self.model(x_train) # computing the training and validation loss loss_train = self.criterion(output_train, y_train) results_train = torch.softmax(output_train, dim=1).argmax(dim=1) batch_accuracy = (results_train == y_train).sum().float() / float( y_train.size(0) ) batch_accuracies.append(batch_accuracy.item()) # computing the updated weights of all the model parameters loss_train.backward() self.optimizer.step() batch_losses.append(loss_train.item()) status.update(1) status.set_postfix_str(loss_train.item()) train_mean = np.mean(batch_losses) self.train_losses.extend(batch_losses) train_accuracy_mean = np.mean(batch_accuracies) self.train_accuracies.extend(batch_accuracies) # testing self.model.eval() status.reset(total=len(self.val_loader)) batch_losses = [] batch_accuracies = [] with torch.no_grad(): for x_val, y_val in self.val_loader: if torch.cuda.is_available(): x_val = x_val.cuda() y_val = y_val.cuda() x_val = Variable(x_val) y_val = Variable(y_val) self.model.eval() output_val = self.model(x_val) loss_val = self.criterion(output_val, y_val) batch_losses.append(loss_val.item()) results_val = torch.softmax(output_val, dim=1).argmax(dim=1) batch_accuracy = (results_val == y_val).sum().float() / float( y_val.size(0) ) batch_accuracies.append(batch_accuracy.item()) status.update(1) status.set_postfix_str(loss_val.item()) status.close() self.val_losses.extend(batch_losses) self.val_accuracies.extend(batch_accuracies) val_mean = np.mean(batch_losses) val_accuracy_mean = np.mean(batch_accuracies) # pl.plot(self.train_losses) # pl.xlabel("batches") # pl.ylabel("training loss") # display.clear_output(wait=True) # display.display(pl.gcf()) print(train_mean, val_mean, train_accuracy_mean, val_accuracy_mean) # display.clear_output(wait=True) # print(train_mean, val_mean, train_accuracy_mean, val_accuracy_mean) def eval(self): return Eval(self)
def __call__(self, model: Model, training: Dataset, validation: Dataset, le: Encoder): """Train the model on the provided training data. Args: model (Model): Model to fit to the training data. training (Dataset): Data used in the training loop. validation (Dataset): Data only used for early stopping validation. le (Encoder): Mapping from human readable labels to model readable. """ # The PyTorch implementation of CrossEntropyLoss combines the softmax # and natural log likelihood loss into one (so none of the models # should have softmax included in them) criterion = CrossEntropyLoss() if self.gpu: model.cuda() criterion.cuda() optimizer = Adam(model.parameters(), lr=self.lr) train_data = DataLoader(training.as_torch(le=le), shuffle=True, batch_size=self.batch_size) val_data = DataLoader(validation.as_torch(le=le), shuffle=True, batch_size=self.batch_size) # Fit the data for the maximum number of epochs, bailing out early if # the early stopping condition is reached. Set the initial "best" very # high so the first epoch is always an improvement best_val_loss = 10e10 epochs_since_best = 0 best_epoch = 0 for epoch in range(0, self.max_epochs): train_loss = self._train_one_epoch(model=model, data=train_data, loss_fn=criterion, optimizer=optimizer) self._dispatch_epoch_completed(mean_loss=train_loss, epoch=epoch) val_loss = self._validate_once(model=model, data=val_data, loss_fn=criterion) self._dispatch_validation_completed(mean_loss=val_loss, epoch=epoch) if val_loss < best_val_loss: best_val_loss = val_loss epochs_since_best = 0 best_epoch = epoch model.save() else: epochs_since_best += 1 if epochs_since_best >= self.patience: break # Reload the "best" weights model.load() self._dispatch_training_completed(best_loss=best_val_loss, best_epoch=best_epoch, total_epochs=epoch)
def train(args): set_seed(args, use_gpu=torch.cuda.is_available()) train_loader, val_loader, test_loader, dataset_attributes = get_data(args) model = get_model(args, n_classes=dataset_attributes['n_classes']) criteria = CrossEntropyLoss() if args.use_gpu: torch.cuda.set_device(0) model.cuda() criteria.cuda() optimizer = SGD(model.parameters(), lr=args.lr, momentum=0.9, weight_decay=args.mu, nesterov=True) # Containers for storing metrics over epochs loss_train, acc_train, topk_acc_train = [], [], [] loss_val, acc_val, topk_acc_val, avgk_acc_val, class_acc_val = [], [], [], [], [] save_name = args.save_name_xp.strip() save_dir = os.path.join(os.getcwd(), 'results', save_name) if not os.path.exists(save_dir): os.makedirs(save_dir) print('args.k : ', args.k) lmbda_best_acc = None best_val_acc = float('-inf') for epoch in tqdm(range(args.n_epochs), desc='epoch', position=0): t = time.time() optimizer = update_optimizer( optimizer, lr_schedule=dataset_attributes['lr_schedule'], epoch=epoch) loss_epoch_train, acc_epoch_train, topk_acc_epoch_train = train_epoch( model, optimizer, train_loader, criteria, loss_train, acc_train, topk_acc_train, args.k, dataset_attributes['n_train'], args.use_gpu) loss_epoch_val, acc_epoch_val, topk_acc_epoch_val, \ avgk_acc_epoch_val, lmbda_val = val_epoch(model, val_loader, criteria, loss_val, acc_val, topk_acc_val, avgk_acc_val, class_acc_val, args.k, dataset_attributes, args.use_gpu) # save model at every epoch save(model, optimizer, epoch, os.path.join(save_dir, save_name + '_weights.tar')) # save model with best val accuracy if acc_epoch_val > best_val_acc: best_val_acc = acc_epoch_val lmbda_best_acc = lmbda_val save(model, optimizer, epoch, os.path.join(save_dir, save_name + '_weights_best_acc.tar')) print() print(f'epoch {epoch} took {time.time()-t:.2f}') print(f'loss_train : {loss_epoch_train}') print(f'loss_val : {loss_epoch_val}') print( f'acc_train : {acc_epoch_train} / topk_acc_train : {topk_acc_epoch_train}' ) print( f'acc_val : {acc_epoch_val} / topk_acc_val : {topk_acc_epoch_val} / ' f'avgk_acc_val : {avgk_acc_epoch_val}') # load weights corresponding to best val accuracy and evaluate on test load_model(model, os.path.join(save_dir, save_name + '_weights_best_acc.tar'), args.use_gpu) loss_test_ba, acc_test_ba, topk_acc_test_ba, \ avgk_acc_test_ba, class_acc_test = test_epoch(model, test_loader, criteria, args.k, lmbda_best_acc, args.use_gpu, dataset_attributes) # Save the results as a dictionary and save it as a pickle file in desired location results = { 'loss_train': loss_train, 'acc_train': acc_train, 'topk_acc_train': topk_acc_train, 'loss_val': loss_val, 'acc_val': acc_val, 'topk_acc_val': topk_acc_val, 'class_acc_val': class_acc_val, 'avgk_acc_val': avgk_acc_val, 'test_results': { 'loss': loss_test_ba, 'accuracy': acc_test_ba, 'topk_accuracy': topk_acc_test_ba, 'avgk_accuracy': avgk_acc_test_ba, 'class_acc_dict': class_acc_test }, 'params': args.__dict__ } with open(os.path.join(save_dir, save_name + '.pkl'), 'wb') as f: pickle.dump(results, f)
class MemnnAgent(Agent): """Memory Network agent.""" @staticmethod def add_cmdline_args(argparser): arg_group = argparser.add_argument_group('MemNN Arguments') arg_group.add_argument( '--init-model', type=str, default=None, help='load dict/features/weights/opts from this file') arg_group.add_argument('-lr', '--learning-rate', type=float, default=0.01, help='learning rate') arg_group.add_argument('--embedding-size', type=int, default=128, help='size of token embeddings') arg_group.add_argument('--hops', type=int, default=3, help='number of memory hops') arg_group.add_argument('--mem-size', type=int, default=100, help='size of memory') arg_group.add_argument('--time-features', type='bool', default=True, help='use time features for memory embeddings') arg_group.add_argument( '--position-encoding', type='bool', default=False, help='use position encoding instead of bag of words embedding') arg_group.add_argument('--output', type=str, default='rank', help='type of output (rank|generate)') arg_group.add_argument( '--rnn-layers', type=int, default=2, help='number of hidden layers in RNN decoder for generative output' ) arg_group.add_argument( '--dropout', type=float, default=0.1, help='dropout probability for RNN decoder training') arg_group.add_argument('--optimizer', default='adam', help='optimizer type (sgd|adam)') arg_group.add_argument('--no-cuda', action='store_true', default=False, help='disable GPUs even if available') arg_group.add_argument('--gpu', type=int, default=-1, help='which GPU device to use') arg_group.add_argument( '-histr', '--history-replies', default='label_else_model', type=str, choices=['none', 'model', 'label', 'label_else_model'], help='Keep replies in the history, or not.') DictionaryAgent.add_cmdline_args(argparser) return arg_group def __init__(self, opt, shared=None): self.use_cuda = not opt['no_cuda'] and torch.cuda.is_available() if self.use_cuda: torch.cuda.device(opt['gpu']) if not shared: self.id = 'MemNN' self.dict = DictionaryAgent(opt) self.answers = [None] * opt['batchsize'] self.model = MemNN(opt, len(self.dict), use_cuda=self.use_cuda) self.decoder = None if opt['output'] == 'generate' or opt['output'] == 'g': self.decoder = Decoder(opt['embedding_size'], opt['embedding_size'], opt['rnn_layers'], opt, self.dict) elif opt['output'] != 'rank' and opt['output'] != 'r': raise NotImplementedError('Output type not supported.') if self.use_cuda and self.decoder is not None: # don't call cuda on self.model, it is split cuda and cpu self.decoder.cuda() if opt['numthreads'] > 1: self.model.share_memory() if self.decoder is not None: self.decoder.share_memory() else: self.dict = shared['dict'] self.model = shared['model'] self.decoder = shared['decoder'] if 'threadindex' in shared: torch.set_num_threads(1) self.answers = [None] * opt['batchsize'] else: self.answers = shared['answers'] self.opt = opt self.mem_size = opt['mem_size'] self.longest_label = 1 self.NULL = self.dict.null_token self.NULL_IDX = self.dict[self.NULL] self.END = self.dict.end_token self.END_TENSOR = torch.LongTensor([self.dict[self.END]]) self.START = self.dict.start_token self.START_TENSOR = torch.LongTensor([self.dict[self.START]]) self.rank_loss = CrossEntropyLoss() self.gen_loss = CrossEntropyLoss(ignore_index=self.NULL_IDX) if self.use_cuda: self.rank_loss.cuda() self.gen_loss.cuda() if 'train' in self.opt.get('datatype', ''): optim_params = [ p for p in self.model.parameters() if p.requires_grad ] lr = opt['learning_rate'] if opt['optimizer'] == 'sgd': self.optimizers = {'memnn': optim.SGD(optim_params, lr=lr)} if self.decoder is not None: self.optimizers['decoder'] = optim.SGD( self.decoder.parameters(), lr=lr) elif opt['optimizer'] == 'adam': self.optimizers = {'memnn': optim.Adam(optim_params, lr=lr)} if self.decoder is not None: self.optimizers['decoder'] = optim.Adam( self.decoder.parameters(), lr=lr) else: raise NotImplementedError('Optimizer not supported.') if not shared: # load model # check first for 'init_model' for loading model from file if opt.get('init_model') and os.path.isfile(opt['init_model']): init_model = opt['init_model'] # next check for 'model_file' elif opt.get('model_file') and os.path.isfile(opt['model_file']): init_model = opt['model_file'] else: init_model = None if init_model is not None: print('Loading existing model parameters from ' + init_model) self.load(init_model) self.history = {} self.batch_idx = shared and shared.get('batchindex') or 0 self.episode_done = True self.last_cands, self.last_cands_list = None, None super().__init__(opt, shared) def share(self): shared = super().share() shared['answers'] = self.answers shared['dict'] = self.dict shared['model'] = self.model shared['decoder'] = self.decoder return shared def observe(self, observation): """Save observation for act. If multiple observations are from the same episode, concatenate them. """ self.episode_done = observation['episode_done'] # shallow copy observation (deep copy can be expensive) obs = observation.copy() obs['text'] = maintain_dialog_history( self.history, obs, reply=self.answers[self.batch_idx], historyLength=self.opt['mem_size'] + 1, useReplies=self.opt['history_replies'], dict=self.dict, useStartEndIndices=False, splitSentences=True) self.observation = obs self.answers[self.batch_idx] = None return obs def reset(self): self.observation = None self.history.clear() for i in range(len(self.answers)): self.answers[i] = None def predict(self, xs, cands, ys=None): is_training = ys is not None if is_training: # Subsample to reduce training time cands = [ list(set(random.sample(c, min(32, len(c))) + self.labels)) for c in cands ] else: # rank all cands to increase accuracy cands = [list(set(c)) for c in cands] self.model.train(mode=is_training) # Organize inputs for network (see contents of xs and ys in batchify method) output_embeddings = self.model(*xs) if self.decoder is None: scores = self.score(cands, output_embeddings) if is_training: label_inds = [ cand_list.index(self.labels[i]) for i, cand_list in enumerate(cands) ] if self.use_cuda: label_inds = torch.cuda.LongTensor(label_inds) else: label_inds = torch.LongTensor(label_inds) loss = self.rank_loss(scores, label_inds) predictions = self.ranked_predictions(cands, scores) else: self.decoder.train(mode=is_training) output_lines, loss = self.decode(output_embeddings, ys) predictions = self.generated_predictions(output_lines) if is_training: for o in self.optimizers.values(): o.zero_grad() loss.backward() for o in self.optimizers.values(): o.step() return predictions def score(self, cands, output_embeddings): last_cand = None max_len = max([len(c) for c in cands]) scores = output_embeddings.data.new(len(cands), max_len) for i, cand_list in enumerate(cands): if last_cand != cand_list: candidate_lengths, candidate_indices = to_tensors( cand_list, self.dict) candidate_embeddings = self.model.answer_embedder( candidate_lengths, candidate_indices) if self.use_cuda: candidate_embeddings = candidate_embeddings.cuda() last_cand = cand_list scores[i, :len(cand_list)] = self.model.score.one_to_many( output_embeddings[i].unsqueeze(0), candidate_embeddings).squeeze(0) return scores def ranked_predictions(self, cands, scores): # return [' '] * len(self.answers) _, inds = scores.sort(descending=True, dim=1) return [[cands[i][j] for j in r if j < len(cands[i])] for i, r in enumerate(inds)] def decode(self, output_embeddings, ys=None): batchsize = output_embeddings.size(0) hn = output_embeddings.unsqueeze(0).expand(self.opt['rnn_layers'], batchsize, output_embeddings.size(1)) x = self.model.answer_embedder(torch.LongTensor([1]), self.START_TENSOR) xes = x.unsqueeze(1).expand(x.size(0), batchsize, x.size(1)) loss = 0 output_lines = [[] for _ in range(batchsize)] done = [False for _ in range(batchsize)] total_done = 0 idx = 0 while (total_done < batchsize) and idx < self.longest_label: # keep producing tokens until we hit END or max length for each ex if self.use_cuda: xes = xes.cuda() hn = hn.contiguous() preds, scores = self.decoder(xes, hn) if ys is not None: y = ys[0][:, idx] temp_y = y.cuda() if self.use_cuda else y loss += self.gen_loss(scores, temp_y) else: y = preds # use the true token as the next input for better training xes = self.model.answer_embedder( torch.LongTensor(preds.numel()).fill_(1), y).unsqueeze(0) for b in range(batchsize): if not done[b]: token = self.dict.vec2txt(preds[b]) if token == self.END: done[b] = True total_done += 1 else: output_lines[b].append(token) idx += 1 return output_lines, loss def generated_predictions(self, output_lines): return [[ ' '.join(c for c in o if c != self.END and c != self.dict.null_token) ] for o in output_lines] def parse(self, memory): """Returns: query = tensor (vector) of token indices for query query_length = length of query memory = tensor (matrix) where each row contains token indices for a memory memory_lengths = tensor (vector) with lengths of each memory """ query = memory.pop() query = torch.LongTensor(query) query_length = torch.LongTensor([len(query)]) if len(memory) == 0: memory.append([self.NULL_IDX]) memory = [torch.LongTensor(m) for m in memory] memory_lengths = torch.LongTensor([len(m) for m in memory]) memory = torch.cat(memory) return (query, memory, query_length, memory_lengths) def batchify(self, obs): """Returns: xs = [memories, queries, memory_lengths, query_lengths] ys = [labels, label_lengths] (if available, else None) cands = list of candidates for each example in batch valid_inds = list of indices for examples with valid observations """ exs = [ex for ex in obs if 'text' in ex and len(ex['text']) > 0] valid_inds = [ i for i, ex in enumerate(obs) if 'text' in ex and len(ex['text']) > 0 ] if not exs: return [None] * 4 parsed = [self.parse(ex['text']) for ex in exs] queries = torch.cat([x[0] for x in parsed]) memories = torch.cat([x[1] for x in parsed]) query_lengths = torch.cat([x[2] for x in parsed]) memory_lengths = torch.LongTensor(len(exs), self.mem_size).zero_() for i in range(len(exs)): if len(parsed[i][3]) > 0: memory_lengths[i, -len(parsed[i][3]):] = parsed[i][3] xs = [memories, queries, memory_lengths, query_lengths] ys = None self.labels = [ random.choice(ex['labels']) for ex in exs if 'labels' in ex ] if len(self.labels) == len(exs): parsed = [self.dict.txt2vec(l) for l in self.labels] parsed = [torch.LongTensor(p) for p in parsed] label_lengths = torch.LongTensor([len(p) for p in parsed]).unsqueeze(1) self.longest_label = max(self.longest_label, label_lengths.max()) padded = [ torch.cat( (p, torch.LongTensor(self.longest_label - len(p)).fill_( self.END_TENSOR[0]))) for p in parsed ] labels = torch.stack(padded) ys = [labels, label_lengths] cands = [ ex['label_candidates'] for ex in exs if 'label_candidates' in ex ] # Use words in dict as candidates if no candidates are provided if len(cands) < len(exs): cands = build_cands(exs, self.dict) # Avoid rebuilding candidate list every batch if its the same if self.last_cands != cands: self.last_cands = cands self.last_cands_list = [list(c) for c in cands] cands = self.last_cands_list return xs, ys, cands, valid_inds def batch_act(self, observations): batchsize = len(observations) batch_reply = [{'id': self.getID()} for _ in range(batchsize)] xs, ys, cands, valid_inds = self.batchify(observations) if xs is None or len(xs[1]) == 0: return batch_reply # Either train or predict predictions = self.predict(xs, cands, ys) for i in range(len(valid_inds)): self.answers[valid_inds[i]] = predictions[i][0] batch_reply[valid_inds[i]]['text'] = predictions[i][0] batch_reply[valid_inds[i]]['text_candidates'] = predictions[i] return batch_reply def act(self): return self.batch_act([self.observation])[0] def save(self, path=None): path = self.opt.get('model_file', None) if path is None else path if path: checkpoint = {} checkpoint['memnn'] = self.model.state_dict() checkpoint['memnn_optim'] = self.optimizers['memnn'].state_dict() if self.decoder is not None: checkpoint['decoder'] = self.decoder.state_dict() checkpoint['decoder_optim'] = self.optimizers[ 'decoder'].state_dict() checkpoint['longest_label'] = self.longest_label with open(path, 'wb') as write: torch.save(checkpoint, write) def load(self, path): checkpoint = torch.load(path, map_location=lambda cpu, _: cpu) self.model.load_state_dict(checkpoint['memnn']) self.optimizers['memnn'].load_state_dict(checkpoint['memnn_optim']) if self.decoder is not None: self.decoder.load_state_dict(checkpoint['decoder']) self.optimizers['decoder'].load_state_dict( checkpoint['decoder_optim']) self.longest_label = checkpoint['longest_label']
validationset = SSNDataset(video_record_list_path=validationset_video_record_list_path, n_body_segment=args.n_body_segment, n_augmentation_segment=args.n_augmentation_segment, new_length=new_length, modality=args.modality, transform=validationset_transform, random_shift=False, proposal_per_video=8, fg_ratio=1, bg_ratio=1, incomplete_ratio=6, fg_iou_thresh=0.7, bg_iou_thresh=0.01, incomplete_iou_thresh=0.3, bg_coverage_thresh=0.02, incomplete_overlap_thresh=0.7, regression_constant=trainset.regression_constant) validationset_loader = DataLoader(dataset=validationset, batch_size=args.batch_size, shuffle=False, drop_last=True) n_class = 20 stpp_cfg = (1, 1, 1) ssn = SSN(base_model=args.base_model, n_class=n_class, dropout=args.dropout, stpp_cfg=stpp_cfg, bn_mode=args.bn_mode, modality=args.modality, n_body_segment=args.n_body_segment, n_augmentation_segment=args.n_augmentation_segment, new_length=new_length) if args.modality == 'Flow': if args.base_model == 'BNInception': url = 'https://yjxiong.blob.core.windows.net/ssn-models/bninception_thumos_flow_init-89dfeaf803e.pth' if args.base_model == 'InceptionV3': url = 'https://yjxiong.blob.core.windows.net/ssn-models/inceptionv3_thumos_flow_init-0527856bcec6.pth' ssn.base_model.load_state_dict(model_zoo.load_url(url)['state_dict']) ssn = ssn.cuda() activity_loss_function = CrossEntropyLoss() activity_loss_function = activity_loss_function.cuda() completeness_loss_function = CompletenessLoss() completeness_loss_function = completeness_loss_function.cuda() regression_loss_function = ClasswiseRegressionLoss() regression_loss_function = regression_loss_function.cuda() first_conv_weight = [] first_conv_bias = [] normal_weight = [] normal_bias = [] conv_count = 0 for m in ssn.modules(): if isinstance(m, nn.Conv2d) or isinstance(m, nn.Conv1d): ps = list(m.parameters()) conv_count += 1 if conv_count == 1:
if args.dataset == 'ucf101': n_class = 101 tsn = TSN(base_model=args.base_model, n_class=n_class, consensus_type=args.consensus_type, before_softmax=True, dropout=args.dropout, n_crop=1, modality=args.modality, n_segment=args.n_segment, new_length=new_length) tsn = tsn.cuda() loss_function = CrossEntropyLoss() loss_function = loss_function.cuda() first_conv_weight = [] first_conv_bias = [] normal_weight = [] normal_bias = [] bn = [] conv_count = 0 bn_count = 0 for m in tsn.modules(): if isinstance(m, nn.Conv2d) or isinstance(m, nn.Conv1d): ps = list(m.parameters()) conv_count += 1 if conv_count == 1: first_conv_weight += [ps[0]] if len(ps) == 2:
Checkpoint.CHECKPOINT_DIR_NAME, opt.load_checkpoint) checkpoint = Checkpoint.load(checkpoint_path) dual_encoder = checkpoint.model vocab = checkpoint.vocab else: # Prepare dataset train = Dataset.from_file(opt.train_path) dev = Dataset.from_file(opt.dev_path) vocab = train.vocab max_len = 500 # Prepare loss loss_func = CrossEntropyLoss() if torch.cuda.is_available(): loss_func.cuda() optimizer = None if not opt.resume: # Initialize model hidden_size = 128 bidirectional = True context_encoder = Encoder(vocab.get_vocab_size(), max_len, hidden_size, bidirectional=bidirectional, variable_lengths=True) response_encoder = Encoder(vocab.get_vocab_size(), max_len, hidden_size, bidirectional=bidirectional,
class UNetTrainer(object): """UNet trainer""" def __init__(self, start_epoch=0, save_dir='', resume="", devices_num=2, num_classes=2, color_dim=1): self.net = UNet(color_dim=color_dim, num_classes=num_classes) self.start_epoch = start_epoch if start_epoch != 0 else 1 self.save_dir = os.path.join('../models', save_dir) self.loss = CrossEntropyLoss() self.num_classes = num_classes if resume: checkpoint = torch.load(resume) if self.start_epoch == 0: self.start_epoch = checkpoint['epoch'] + 1 if not self.save_dir: self.save_dir = checkpoint['save_dir'] self.net.load_state_dict(checkpoint['state_dir']) if not os.path.exists(self.save_dir): os.makedirs(self.save_dir) self.net.cuda() self.loss.cuda() if devices_num == 2: self.net = DataParallel(self.net, device_ids=[0, 1]) #self.loss = DataParallel(self.loss, device_ids=[0, 1]) def train(self, train_loader, val_loader, lr=0.001, weight_decay=1e-4, epochs=200, save_freq=10): self.logfile = os.path.join(self.save_dir, 'log') sys.stdout = Logger(self.logfile) self.epochs = epochs self.lr = lr optimizer = torch.optim.Adam( self.net.parameters(), #lr, #momentum=0.9, weight_decay=weight_decay) for epoch in range(self.start_epoch, epochs + 1): self.train_(train_loader, epoch, optimizer, save_freq) self.validate_(val_loader, epoch) def train_(self, data_loader, epoch, optimizer, save_freq=10): start_time = time.time() self.net.train() #lr = self.get_lr(epoch) #for param_group in optimizer.param_groups: # param_group['lr'] = lr metrics = [] for i, (data, target) in enumerate(tqdm(data_loader)): data_t, target_t = data, target data = Variable(data.cuda(non_blocking=True)) target = Variable(target.cuda(non_blocking=True)) output = self.net(data) #unet输出结果 output = output.transpose(1, 3).transpose(1, 2).contiguous().view( -1, self.num_classes) target = target.view(-1) loss_output = self.loss(output, target) optimizer.zero_grad() loss_output.backward() #反向传播loss optimizer.step() loss_output = loss_output.data[0] #loss数值 acc = accuracy(output, target) metrics.append([loss_output, acc]) if i == 0: batch_size = data.size(0) _, output = output.data.max(dim=1) output = output.view(batch_size, 1, 1, 320, 480).cpu() #预测结果图 data_t = data_t[0, 0].unsqueeze(0).unsqueeze(0) #原img图 target_t = target_t[0].unsqueeze(0) #gt图 t = torch.cat([output[0].float(), data_t, target_t.float()], 0) #第一个参数为list,拼接3张图像 #show_list = [] #for j in range(10): # show_list.append(data_t[j, 0].unsqueeze(0).unsqueeze(0)) # show_list.append(target_t[j].unsqueeze(0)) # show_list.append(output[j].float()) # #t = torch.cat(show_list, 0) torchvision.utils.save_image(t, "temp_image/%02d_train.jpg" % epoch, nrow=3) #if i == 20: # break if epoch % save_freq == 0: if 'module' in dir(self.net): state_dict = self.net.module.state_dict() else: state_dict = self.net.state_dict() for key in state_dict.keys(): state_dict[key] = state_dict[key].cpu() torch.save( { 'epoch': epoch, 'save_dir': self.save_dir, 'state_dir': state_dict }, os.path.join(self.save_dir, '%03d.ckpt' % epoch)) end_time = time.time() metrics = np.asarray(metrics, np.float32) self.print_metrics(metrics, 'Train', end_time - start_time, epoch) def validate_(self, data_loader, epoch): start_time = time.time() self.net.eval() metrics = [] for i, (data, target) in enumerate(data_loader): data_t, target_t = data, target data = Variable(data.cuda(non_blocking=True), volatile=True) target = Variable(target.cuda(non_blocking=True), volatile=True) output = self.net(data) output = output.transpose(1, 3).transpose(1, 2).contiguous().view( -1, self.num_classes) target = target.view(-1) loss_output = self.loss(output, target) loss_output = loss_output.data[0] acc = accuracy(output, target) metrics.append([loss_output, acc]) if i == 0: batch_size = data.size(0) _, output = output.data.max(dim=1) output = output.view(batch_size, 1, 1, 320, 480).cpu() data_t = data_t[0, 0].unsqueeze(0).unsqueeze(0) target_t = target_t[0].unsqueeze(0) t = torch.cat([output[0].float(), data_t, target_t.float()], 0) # show_list = [] # for j in range(10): # show_list.append(data_t[j, 0].unsqueeze(0).unsqueeze(0)) # show_list.append(target_t[j].unsqueeze(0)) # show_list.append(output[j].float()) # # t = torch.cat(show_list, 0) torchvision.utils.save_image(t, "temp_image/%02d_val.jpg" % epoch, nrow=3) #if i == 10: # break end_time = time.time() metrics = np.asarray(metrics, np.float32) self.print_metrics(metrics, 'Validation', end_time - start_time) def print_metrics(self, metrics, phase, time, epoch=-1): """metrics: [loss, acc] """ if epoch != -1: print("Epoch: {}".format(epoch), ) print(phase, ) print('loss %2.4f, accuracy %2.4f, time %2.2f' % (np.mean(metrics[:, 0]), np.mean(metrics[:, 1]), time)) if phase != 'Train': print def get_lr(self, epoch): if epoch <= self.epochs * 0.5: lr = self.lr elif epoch <= self.epochs * 0.8: lr = 0.1 * self.lr else: lr = 0.01 * self.lr return lr def save_py_files(self, path): """copy .py files in exps dir, cfgs dir and current dir into save_dir, and keep the files structure """ #exps dir pyfiles = [f for f in os.listdir(path) if f.endswith('.py')] path = "/".join(path.split('/')[-2:]) exp_save_path = os.path.join(self.save_dir, path) mkdir(exp_save_path) for f in pyfiles: shutil.copy(os.path.join(path, f), os.path.join(exp_save_path, f)) #current dir pyfiles = [f for f in os.listdir('./') if f.endswith('.py')] for f in pyfiles: shutil.copy(f, os.path.join(self.save_dir, f)) #cfgs dir shutil.copytree('./cfgs', os.path.join(self.save_dir, 'cfgs'))
class ConfusionTrainingHelper: def __init__(self, model, problem, args, use_cuda, checkpoint_key=None): self.use_cuda = use_cuda self.problem = problem self.args = args if checkpoint_key is not None: self.model, self.training_losses, self.validation_losses = \ self.load_confusion_model(self.args.checkpoint_key) if use_cuda: self.model.cuda() else: self.model = ConfusionModel(model, problem) self.optimizer = torch.optim.SGD(self.model.parameters(), lr=args.lr, momentum=0.9, weight_decay=args.L2) self.lr_scheduler = ReduceLROnPlateau(self.optimizer, "min", factor=0.1, patience=5, verbose=True) self.criterion = CrossEntropyLoss() if use_cuda: self.model.cuda() self.criterion = self.criterion.cuda() def train(self, epoch, confusion_data): args = self.args optimizer = self.optimizer problem = self.problem performance_estimators = PerformanceList() performance_estimators += [FloatHelper("train_loss")] for performance_estimator in performance_estimators: performance_estimator.init_performance_metrics() self.model.train() shuffle(confusion_data) max = min(args.max_training, len(confusion_data)) confusion_data = confusion_data[0:max] for batch_idx, confusion_list in enumerate(batch(confusion_data, args.mini_batch_size)): batch_size = min(len(confusion_list), args.mini_batch_size) images = [None] * batch_size targets = torch.zeros(batch_size) optimizer.zero_grad() training_loss_input = torch.zeros(batch_size, 1) trained_with_input = torch.zeros(batch_size, 1) for index, confusion in enumerate(confusion_list): num_classes = problem.num_classes() targets[index] = class_label(num_classes, confusion.predicted_label, confusion.true_label) dataset = problem.train_set() if confusion.trained_with else problem.test_set() images[index], _ = dataset[confusion.example_index] training_loss_input[index] = confusion.train_loss trained_with_input[index] = 1.0 if confusion.trained_with else 0.0 image_input = Variable(torch.stack(images, dim=0), requires_grad=True) training_loss_input = Variable(training_loss_input, requires_grad=True) trained_with_input = Variable(trained_with_input, requires_grad=True) targets = Variable(targets, requires_grad=False).type(torch.LongTensor) if self.use_cuda: image_input = image_input.cuda() training_loss_input = training_loss_input.cuda() trained_with_input = trained_with_input.cuda() targets = targets.cuda() outputs = self.model(training_loss_input, trained_with_input, image_input) loss = self.criterion(outputs, targets) loss.backward() optimizer.step() performance_estimators.set_metric(batch_idx, "train_loss", loss.data[0]) if args.progress_bar: progress_bar(batch_idx * batch_size, len(confusion_data), " ".join([performance_estimator.progress_message() for performance_estimator in performance_estimators])) return performance_estimators def test(self, epoch, confusion_data): args = self.args problem = self.problem performance_estimators = PerformanceList() performance_estimators += [FloatHelper("test_loss")] self.model.eval() for performance_estimator in performance_estimators: performance_estimator.init_performance_metrics() shuffle(confusion_data) max = min(args.max_training, len(confusion_data)) confusion_data = confusion_data[0:max] for batch_idx, confusion_list in enumerate(batch(confusion_data, args.mini_batch_size)): batch_size = min(len(confusion_list), args.mini_batch_size) images = [None] * batch_size targets = torch.zeros(batch_size) training_loss_input = torch.zeros(batch_size, 1) trained_with_input = torch.zeros(batch_size, 1) for index, confusion in enumerate(confusion_list): num_classes = problem.num_classes() targets[index] = class_label(num_classes, confusion.predicted_label, confusion.true_label) dataset = problem.train_set() if confusion.trained_with else problem.test_set() images[index], _ = dataset[confusion.example_index] training_loss_input[index] = confusion.train_loss trained_with_input[index] = 1.0 if confusion.trained_with else 0.0 image_input = Variable(torch.stack(images, dim=0), volatile=True) training_loss_input = Variable(training_loss_input, volatile=True) trained_with_input = Variable(trained_with_input, volatile=True) targets = Variable(targets, volatile=True).type(torch.LongTensor) if self.use_cuda: image_input = image_input.cuda() training_loss_input = training_loss_input.cuda() trained_with_input = trained_with_input.cuda() targets = targets.cuda() outputs = self.model(training_loss_input, trained_with_input, image_input) loss = self.criterion(outputs, targets) performance_estimators.set_metric(batch_idx, "test_loss", loss.data[0]) if args.progress_bar: progress_bar(batch_idx * batch_size, len(confusion_data), " ".join([performance_estimator.progress_message() for performance_estimator in performance_estimators])) self.lr_scheduler.step(performance_estimators.get_metric("test_loss"), epoch=epoch) return performance_estimators def predict(self, max_examples=sys.maxsize, max_queue_size=10): val_losses = self.validation_losses pq = PriorityQueues(val_losses, max_queue_size=max_queue_size) args = self.args problem = self.problem self.model.eval() decoder = {} num_classes = problem.num_classes() for i in range(num_classes): for j in range(num_classes): decoder[class_label(num_classes, i, j)] = (i, j) unsup_set_length = len(problem.unsup_set()) image_index = 0 for batch_idx, tensors in enumerate(batch(problem.unsup_set(), args.mini_batch_size)): batch_size = min(len(tensors), args.mini_batch_size) image_index = batch_idx * len(tensors) tensor_images = (torch.stack([ti for ti, _ in tensors], dim=0)) image_input = Variable(torch.stack(tensor_images, dim=0), volatile=True) if self.use_cuda: image_input = image_input.cuda() for training_loss in val_losses: training_loss_input = torch.zeros(batch_size, 1) trained_with_input = torch.zeros(batch_size, 1) for index in range(batch_size): training_loss_input[index] = training_loss trained_with_input[index] = 0 # we are predicting on a set never seen by the model trained_with_input = Variable(trained_with_input, volatile=True) training_loss_input = Variable(training_loss_input, volatile=True) if self.use_cuda: training_loss_input = training_loss_input.cuda() trained_with_input = trained_with_input.cuda() outputs = self.model(training_loss_input, trained_with_input, image_input) max_values, indices = torch.max(outputs.data, dim=1) for index in range(batch_size): (predicted_index, true_index) = decoder[indices[index]] probability = max_values[index] if predicted_index != true_index: # off diagonal prediction, predicting an error on this image: unsup_index = image_index + index # print("training_loss={} probability={} predicted_index={}, true_index={} unsup_index={}".format( # training_loss, probability, predicted_index, true_index, unsup_index)) pq.put(training_loss, probability, (unsup_index, predicted_index, true_index)) if args.progress_bar: progress_bar(batch_idx * batch_size, unsup_set_length) if batch_idx * args.mini_batch_size > max_examples: break return pq def load_confusion_model(self, checkpoint_key): if not os.path.isdir('checkpoint'): os.mkdir('checkpoint') state = torch.load('./checkpoint/confusionmodel_{}.t7'.format(self.args.checkpoint_key)) model = state['confusion-model'] training_losses = state['training_losses'] validation_losses = state['validation_losses'] model.cpu() model.eval() return (model, training_losses, validation_losses) def save_confusion_model(self, epoch, test_loss, training_losses, validation_losses): # Save checkpoint. # print('Saving confusion model..') model = self.model model.eval() state = { 'confusion-model': self.model, 'test_loss': test_loss, 'epoch': epoch, 'training_losses': training_losses, 'validation_losses': validation_losses } if not os.path.isdir('checkpoint'): os.mkdir('checkpoint') torch.save(state, './checkpoint/confusionmodel_{}.t7'.format(self.args.checkpoint_key))
x = self.cnn_layers(x) x = x.view(x.size(0), -1) x = self.linear_layers(x) return x # defining the model model = Net() # defining the optimizer optimizer = Adam(model.parameters(), lr=0.07) # defining the loss function criterion = CrossEntropyLoss() # checking if GPU is available if torch.cuda.is_available(): model = model.cuda() criterion = criterion.cuda() print(model) def train(epoch): model.train() tr_loss = 0 # getting the training set x_train, y_train = Variable(train_x), Variable(train_y) # getting the validation set x_val, y_val = Variable(val_x), Variable(val_y) # converting the data into GPU format if torch.cuda.is_available(): x_train = x_train.cuda() y_train = y_train.cuda()
x=x.view(x.size(0),-1) output=self.linear_layers(x) return output,per_out ''' # defining the model model = Net() # defining the optimizer optimizer = Adam(model.parameters(), lr=0.07) # defining the loss function loss = CrossEntropyLoss() # checking if GPU is available if torch.cuda.is_available(): model = model.cuda() loss = loss.cuda() summary(model, (1, 100, 100)) print(model) ''' kernel=model.cnn_layers[0].weight.data.clone() print(kernel.shape) plt.figure() plt.imshow(kernel[0,0,:,:]) plt.show() plt.figure(1,kernel.shape[0]) for i in range(kernel.shape[0]): plt.subplot(1,4,i+1)