def eval_fidelity(tree: ProtoTree, test_loader: DataLoader, device, log: Log = None, progress_prefix: str = 'Fidelity') -> dict: tree = tree.to(device) # Keep an info dict about the procedure info = dict() # Make sure the model is in evaluation mode tree.eval() # Show progress on progress bar test_iter = tqdm(enumerate(test_loader), total=len(test_loader), desc=progress_prefix, ncols=0) distr_samplemax_fidelity = 0 distr_greedy_fidelity = 0 # Iterate through the test set for i, (xs, ys) in test_iter: xs, ys = xs.to(device), ys.to(device) # Use the model to classify this batch of input data, with 3 types of routing out_distr, _ = tree.forward(xs, 'distributed') ys_pred_distr = torch.argmax(out_distr, dim=1) out_samplemax, _ = tree.forward(xs, 'sample_max') ys_pred_samplemax = torch.argmax(out_samplemax, dim=1) out_greedy, _ = tree.forward(xs, 'greedy') ys_pred_greedy = torch.argmax(out_greedy, dim=1) # Calculate fidelity distr_samplemax_fidelity += torch.sum( torch.eq(ys_pred_samplemax, ys_pred_distr)).item() distr_greedy_fidelity += torch.sum( torch.eq(ys_pred_greedy, ys_pred_distr)).item() # Update the progress bar test_iter.set_postfix_str(f'Batch [{i + 1}/{len(test_iter)}]') del out_distr del out_samplemax del out_greedy distr_samplemax_fidelity = distr_samplemax_fidelity / float( len(test_loader.dataset)) distr_greedy_fidelity = distr_greedy_fidelity / float( len(test_loader.dataset)) info['distr_samplemax_fidelity'] = distr_samplemax_fidelity info['distr_greedy_fidelity'] = distr_greedy_fidelity log.log_message( "Fidelity between standard distributed routing and sample_max routing: " + str(distr_samplemax_fidelity)) log.log_message( "Fidelity between standard distributed routing and greedy routing: " + str(distr_greedy_fidelity)) return info
def save_tree_description(tree: ProtoTree, optimizer, scheduler, description: str, log: Log): tree.eval() # Save model with description tree.save(f'{log.checkpoint_dir}/' + description) tree.save_state(f'{log.checkpoint_dir}/' + description) torch.save(optimizer.state_dict(), f'{log.checkpoint_dir}/' + description + '/optimizer_state.pth') torch.save(scheduler.state_dict(), f'{log.checkpoint_dir}/' + description + '/scheduler_state.pth')
def eval(tree: ProtoTree, test_loader: DataLoader, epoch, device, log: Log = None, sampling_strategy: str = 'distributed', log_prefix: str = 'log_eval_epochs', progress_prefix: str = 'Eval Epoch') -> dict: tree = tree.to(device) # Keep an info dict about the procedure info = dict() if sampling_strategy != 'distributed': info['out_leaf_ix'] = [] # Build a confusion matrix cm = np.zeros((tree._num_classes, tree._num_classes), dtype=int) # Make sure the model is in evaluation mode tree.eval() # Show progress on progress bar test_iter = tqdm(enumerate(test_loader), total=len(test_loader), desc=progress_prefix + ' %s' % epoch, ncols=0) # Iterate through the test set for i, (xs, ys) in test_iter: xs, ys = xs.to(device), ys.to(device) # Use the model to classify this batch of input data out, test_info = tree.forward(xs, sampling_strategy) ys_pred = torch.argmax(out, dim=1) # Update the confusion matrix cm_batch = np.zeros((tree._num_classes, tree._num_classes), dtype=int) for y_pred, y_true in zip(ys_pred, ys): cm[y_true][y_pred] += 1 cm_batch[y_true][y_pred] += 1 acc = acc_from_cm(cm_batch) test_iter.set_postfix_str( f'Batch [{i + 1}/{len(test_iter)}], Acc: {acc:.3f}') # keep list of leaf indices where test sample ends up when deterministic routing is used. if sampling_strategy != 'distributed': info['out_leaf_ix'] += test_info['out_leaf_ix'] del out del ys_pred del test_info info['confusion_matrix'] = cm info['test_accuracy'] = acc_from_cm(cm) log.log_message("\nEpoch %s - Test accuracy with %s routing: " % (epoch, sampling_strategy) + str(info['test_accuracy'])) return info
def save_best_test_tree(tree: ProtoTree, optimizer, scheduler, best_test_acc: float, test_acc: float, log: Log): tree.eval() if test_acc > best_test_acc: best_test_acc = test_acc tree.save(f'{log.checkpoint_dir}/best_test_model') tree.save_state(f'{log.checkpoint_dir}/best_test_model') torch.save( optimizer.state_dict(), f'{log.checkpoint_dir}/best_test_model/optimizer_state.pth') torch.save( scheduler.state_dict(), f'{log.checkpoint_dir}/best_test_model/scheduler_state.pth') return best_test_acc
def train_leaves_epoch(tree: ProtoTree, train_loader: DataLoader, epoch: int, device, progress_prefix: str = 'Train Leafs Epoch' ) -> dict: #Make sure the tree is in eval mode for updating leafs tree.eval() with torch.no_grad(): _old_dist_params = dict() for leaf in tree.leaves: _old_dist_params[leaf] = leaf._dist_params.detach().clone() # Optimize class distributions in leafs eye = torch.eye(tree._num_classes).to(device) # Show progress on progress bar train_iter = tqdm(enumerate(train_loader), total=len(train_loader), desc=progress_prefix+' %s'%epoch, ncols=0) # Iterate through the data set update_sum = dict() # Create empty tensor for each leaf that will be filled with new values for leaf in tree.leaves: update_sum[leaf] = torch.zeros_like(leaf._dist_params) for i, (xs, ys) in train_iter: xs, ys = xs.to(device), ys.to(device) #Train leafs without gradient descent out, info = tree.forward(xs) target = eye[ys] #shape (batchsize, num_classes) for leaf in tree.leaves: if tree._log_probabilities: # log version update = torch.exp(torch.logsumexp(info['pa_tensor'][leaf.index] + leaf.distribution() + torch.log(target) - out, dim=0)) else: update = torch.sum((info['pa_tensor'][leaf.index] * leaf.distribution() * target)/out, dim=0) update_sum[leaf] += update for leaf in tree.leaves: leaf._dist_params -= leaf._dist_params #set current dist params to zero leaf._dist_params += update_sum[leaf] #give dist params new value
def save_tree(tree: ProtoTree, optimizer, scheduler, epoch: int, log: Log, args: argparse.Namespace): tree.eval() # Save latest model tree.save(f'{log.checkpoint_dir}/latest') tree.save_state(f'{log.checkpoint_dir}/latest') torch.save(optimizer.state_dict(), f'{log.checkpoint_dir}/latest/optimizer_state.pth') torch.save(scheduler.state_dict(), f'{log.checkpoint_dir}/latest/scheduler_state.pth') # Save model every 10 epochs if epoch == args.epochs or epoch % 10 == 0: tree.save(f'{log.checkpoint_dir}/epoch_{epoch}') tree.save_state(f'{log.checkpoint_dir}/epoch_{epoch}') torch.save(optimizer.state_dict(), f'{log.checkpoint_dir}/epoch_{epoch}/optimizer_state.pth') torch.save(scheduler.state_dict(), f'{log.checkpoint_dir}/epoch_{epoch}/scheduler_state.pth')
def project_with_class_constraints( tree: ProtoTree, project_loader: DataLoader, device, args: argparse.Namespace, log: Log, log_prefix: str = 'log_projection_with_constraints', # TODO progress_prefix: str = 'Projection') -> dict: log.log_message( "\nProjecting prototypes to nearest training patch (with class restrictions)..." ) # Set the model to evaluation mode tree.eval() torch.cuda.empty_cache() # The goal is to find the latent patch that minimizes the L2 distance to each prototype # To do this we iterate through the train dataset and store for each prototype the closest latent patch seen so far # Also store info about the image that was used for projection global_min_proto_dist = {j: np.inf for j in range(tree.num_prototypes)} global_min_patches = {j: None for j in range(tree.num_prototypes)} global_min_info = {j: None for j in range(tree.num_prototypes)} # Get the shape of the prototypes W1, H1, D = tree.prototype_shape # Build a progress bar for showing the status projection_iter = tqdm(enumerate(project_loader), total=len(project_loader), desc=progress_prefix, ncols=0) with torch.no_grad(): # Get a batch of data xs, ys = next(iter(project_loader)) batch_size = xs.shape[0] # For each internal node, collect the leaf labels in the subtree with this node as root. # Only images from these classes can be used for projection. leaf_labels_subtree = dict() for branch, j in tree._out_map.items(): leaf_labels_subtree[branch.index] = set() for leaf in branch.leaves: leaf_labels_subtree[branch.index].add( torch.argmax(leaf.distribution()).item()) for i, (xs, ys) in projection_iter: xs, ys = xs.to(device), ys.to(device) # Get the features and distances # - features_batch: features tensor (shared by all prototypes) # shape: (batch_size, D, W, H) # - distances_batch: distances tensor (for all prototypes) # shape: (batch_size, num_prototypes, W, H) # - out_map: a dict mapping decision nodes to distances (indices) features_batch, distances_batch, out_map = tree.forward_partial(xs) # Get the features dimensions bs, D, W, H = features_batch.shape # Get a tensor containing the individual latent patches # Create the patches by unfolding over both the W and H dimensions # TODO -- support for strides in the prototype layer? (corresponds to step size here) patches_batch = features_batch.unfold(2, W1, 1).unfold( 3, H1, 1) # Shape: (batch_size, D, W, H, W1, H1) # Iterate over all decision nodes/prototypes for node, j in out_map.items(): leaf_labels = leaf_labels_subtree[node.index] # Iterate over all items in the batch # Select the features/distances that are relevant to this prototype # - distances: distances of the prototype to the latent patches # shape: (W, H) # - patches: latent patches # shape: (D, W, H, W1, H1) for batch_i, (distances, patches) in enumerate( zip(distances_batch[:, j, :, :], patches_batch)): #Check if label of this image is in one of the leaves of the subtree if ys[batch_i].item() in leaf_labels: # Find the index of the latent patch that is closest to the prototype min_distance = distances.min() min_distance_ix = distances.argmin() # Use the index to get the closest latent patch closest_patch = patches.view(D, W * H, W1, H1)[:, min_distance_ix, :, :] # Check if the latent patch is closest for all data samples seen so far if min_distance < global_min_proto_dist[j]: global_min_proto_dist[j] = min_distance global_min_patches[j] = closest_patch global_min_info[j] = { 'input_image_ix': i * batch_size + batch_i, 'patch_ix': min_distance_ix.item( ), # Index in a flattened array of the feature map 'W': W, 'H': H, 'W1': W1, 'H1': H1, 'distance': min_distance.item(), 'nearest_input': torch.unsqueeze(xs[batch_i], 0), 'node_ix': node.index, } # Update the progress bar if required projection_iter.set_postfix_str( f'Batch: {i + 1}/{len(project_loader)}') del features_batch del distances_batch del out_map # Copy the patches to the prototype layer weights projection = torch.cat(tuple(global_min_patches[j].unsqueeze(0) for j in range(tree.num_prototypes)), dim=0, out=tree.prototype_layer.prototype_vectors) del projection return global_min_info, tree
def train_epoch(tree: ProtoTree, train_loader: DataLoader, optimizer: torch.optim.Optimizer, epoch: int, disable_derivative_free_leaf_optim: bool, device, log: Log = None, log_prefix: str = 'log_train_epochs', progress_prefix: str = 'Train Epoch' ) -> dict: tree = tree.to(device) # Make sure the model is in eval mode tree.eval() # Store info about the procedure train_info = dict() total_loss = 0. total_acc = 0. # Create a log if required log_loss = f'{log_prefix}_losses' nr_batches = float(len(train_loader)) with torch.no_grad(): _old_dist_params = dict() for leaf in tree.leaves: _old_dist_params[leaf] = leaf._dist_params.detach().clone() # Optimize class distributions in leafs eye = torch.eye(tree._num_classes).to(device) # Show progress on progress bar train_iter = tqdm(enumerate(train_loader), total=len(train_loader), desc=progress_prefix+' %s'%epoch, ncols=0) # Iterate through the data set to update leaves, prototypes and network for i, (xs, ys) in train_iter: # Make sure the model is in train mode tree.train() # Reset the gradients optimizer.zero_grad() xs, ys = xs.to(device), ys.to(device) # Perform a forward pass through the network ys_pred, info = tree.forward(xs) # Learn prototypes and network with gradient descent. # If disable_derivative_free_leaf_optim, leaves are optimized with gradient descent as well. # Compute the loss if tree._log_probabilities: loss = F.nll_loss(ys_pred, ys) else: loss = F.nll_loss(torch.log(ys_pred), ys) # Compute the gradient loss.backward() # Update model parameters optimizer.step() if not disable_derivative_free_leaf_optim: #Update leaves with derivate-free algorithm #Make sure the tree is in eval mode tree.eval() with torch.no_grad(): target = eye[ys] #shape (batchsize, num_classes) for leaf in tree.leaves: if tree._log_probabilities: # log version update = torch.exp(torch.logsumexp(info['pa_tensor'][leaf.index] + leaf.distribution() + torch.log(target) - ys_pred, dim=0)) else: update = torch.sum((info['pa_tensor'][leaf.index] * leaf.distribution() * target)/ys_pred, dim=0) leaf._dist_params -= (_old_dist_params[leaf]/nr_batches) F.relu_(leaf._dist_params) #dist_params values can get slightly negative because of floating point issues. therefore, set to zero. leaf._dist_params += update # Count the number of correct classifications ys_pred_max = torch.argmax(ys_pred, dim=1) correct = torch.sum(torch.eq(ys_pred_max, ys)) acc = correct.item() / float(len(xs)) train_iter.set_postfix_str( f'Batch [{i + 1}/{len(train_loader)}], Loss: {loss.item():.3f}, Acc: {acc:.3f}' ) # Compute metrics over this batch total_loss+=loss.item() total_acc+=acc if log is not None: log.log_values(log_loss, epoch, i + 1, loss.item(), acc) train_info['loss'] = total_loss/float(i+1) train_info['train_accuracy'] = total_acc/float(i+1) return train_info