def load_model(path=None): ''' Function to load model ''' model = tiramisu.FCDenseNet103(n_classes=8).cuda() weights = torch.load(path) model.load_state_dict(weights['state_dict']) return model
def load_model(path=None): ''' parameters: path to the model weights return: Model with desired architecture and weights ''' model = tiramisu.FCDenseNet103(n_classes=8).cuda() weights = torch.load(path) model.load_state_dict(weights['state_dict']) return model
def load_model(path=None): ''' function to load the model parameters : path : the path of the model state dictionary return: model: the model along with the random weights if path is none otherwise initalized with pretrained weight ''' model = tiramisu.FCDenseNet103(n_classes=8).to(device) if path is not None: weights = torch.load(path, map_location = device) model.load_state_dict(weights['state_dict']) return model
val_loader = torch.utils.data.DataLoader(val_cilia, batch_size=1, shuffle=True) print("Loaded training set!") test_cilia = getCilia.CiliaData(ROOT, 'test', joint_transform=None, input_transform=img_transform) test_loader = torch.utils.data.DataLoader(test_cilia, batch_size=1, shuffle=False) print("Loaded testing set!") model = tiramisu.FCDenseNet103(n_classes=3, in_channels=1).cuda() model.apply(training_utils.weights_init) optimizer = torch.optim.Adam(model.parameters(), lr=LR, weight_decay=1e-4) criterion = nn.NLLLoss().cuda() # Training process for epoch in range(1, N_EPOCHS + 1): since = time.time() ### Train ### trn_loss, trn_err = training_utils.train(model, train_loader, optimizer, criterion, epoch) print('Epoch {:d}\nTrain - Loss: {:.4f}, Acc: {:.4f}'.format( epoch, trn_loss, 1 - trn_err)) time_elapsed = time.time() - since print('Train Time {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))
dropout_data = { 'mode': 'concrete_dropout', 'init_min': 0.01, 'init_max': 0.05, 'weight_reg': 1e-8, 'dropout_reg': 1e0, 'gpu_id': gpu_id } elif mode == 'dropout': dropout_data = {'mode': 'dropout', 'prob': 0.2} else: dropout_data = {'mode': 'dropout', 'prob': 0.0} print(dropout_data) model = tiramisu.FCDenseNet103(12, dropout_data).cuda(gpu_id) model.apply(train_utils.weights_init) optimizer = torch.optim.RMSprop(model.parameters(), lr=LR, weight_decay=1e-4) criterion = nn.NLLLoss2d(weight=camvid.class_weight.cuda(gpu_id)).cuda(gpu_id) print('\n######### TRAINING #########\n') def collect_dropout_probabilities(model): dropout_probabilities = [] def add_dropout_prob(module): nonlocal dropout_probabilities if module.__class__.__name__.endswith('ConcreteDropout'):
#utils.imgs.view_annotated(targets[0]) # ## Train # In[5]: LR = 1e-4 LR_DECAY = 0.995 DECAY_EVERY_N_EPOCHS = 1 N_EPOCHS = 100 torch.cuda.manual_seed(0) # In[6]: #model = tiramisu.FCDenseNet67(n_classes=12).cuda() model = tiramisu.FCDenseNet103(n_classes=12).cuda() model.apply(train_utils.weights_init) optimizer = torch.optim.RMSprop(model.parameters(), lr=LR, weight_decay=1e-4) criterion = nn.NLLLoss2d(weight=camvid.class_weight.cuda()).cuda() # In[7]: def train(epoch): since = time.time() ### Train ### trn_loss, trn_err = train_utils.train(model, train_loader, optimizer, criterion, epoch) #print('Epoch {:d}, Train - Loss: {:.4f}, Acc: {:.4f}'.format(epoch, trn_loss, 1-trn_err)) print("Epoch: %d, Train - Loss: %.4f, Acc: %.4f" % (epoch, trn_loss, 1 - trn_err)) time_elapsed = time.time() - since
def load_model(path=None): if path == None: model = tiramisu.FCDenseNet103(n_classes=30).cuda() model.apply(train_utils.weights_init) return model