Ejemplo n.º 1
0
def main(Params):
    os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
    # Name of dataset
    Dataset = Params['Dataset']  # 'KTH_TIPS'
    # print('ytorch version ', torch.__version__)
    
    # Model(s) to be used
    model_name = Params['Model_name']
    
    # Number of classes in dataset
    num_classes = Params['num_classes'][Dataset]
    
    # Number of runs and/or splits for dataset
    numRuns = Params['Splits'][Dataset]
    
    # Number of bins and input convolution feature maps after channel-wise pooling
    numBins = Params['numBins']
    num_feature_maps = Params['out_channels'][model_name]
    
    # Local area of feature map after histogram layer
    feat_map_size = Params['feat_map_size']
    
    # Detect if we have a GPU available
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    
    # Location to store trained models
    current_directory = os.getcwd()
    final_directory = os.path.join(current_directory, Params['folder'])
    
    # Class names for embeddings
    class_names = Class_names[Dataset]
    
    print('Starting Experiments...')
    for divergence_method in Params['divergence_method']:
        for current_dim in Params['embed_dim']:
            for alpha in Params['alpha']:
                for current_weight in Params['weights']:
                    for split in range(0, numRuns):
                        print(current_dim)
                        # Keep track of the bins and widths as these values are updated each
                        # epoch
                        saved_bins = np.zeros((Params['num_epochs'] + 1,
                                               numBins * int(num_feature_maps / (feat_map_size * numBins))))
                        saved_widths = np.zeros((Params['num_epochs'] + 1,
                                                 numBins * int(num_feature_maps / (feat_map_size * numBins))))
    
                        histogram_layer = HistogramLayer(int(num_feature_maps / (feat_map_size * numBins)),
                                                         Params['kernel_size'][model_name],
                                                         num_bins=numBins, stride=Params['stride'],
                                                         normalize_count=Params['normalize_count'],
                                                         normalize_bins=Params['normalize_bins'])
    
                        # Initialize the histogram model for this run
                        model_ft, input_size = initialize_model(model_name, num_classes,
                                                                Params['in_channels'][model_name],
                                                                num_feature_maps,
                                                                feature_extract=Params['feature_extraction'],
                                                                histogram=Params['histogram'],
                                                                histogram_layer=histogram_layer,
                                                                parallel=Params['parallel'],
                                                                use_pretrained=Params['use_pretrained'],
                                                                add_bn=Params['add_bn'],
                                                                scale=Params['scale'],
                                                                feat_map_size=feat_map_size,
                                                                embed_dim=current_dim)
    
                        # Send the model to GPU if available, use multiple if available
                        if torch.cuda.device_count() > 1:
                            print("Using", torch.cuda.device_count(), "GPUs!")
                            # dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs
                            model_ft = nn.DataParallel(model_ft)
                        model_ft = model_ft.to(device)
    
                        # Print number of trainable parameters
                        num_params = sum(p.numel() for p in model_ft.parameters() if p.requires_grad)
                        print("Number of parameters: %d" % (num_params))
                        print("Initializing Datasets and Dataloaders...")
    
                        # Create training and validation dataloaders
                        dataloaders_dict = Prepare_DataLoaders(Params, split, input_size=input_size)
    
                        # Save the initial values for bins and widths of histogram layer
                        # Set optimizer for model
                        if (Params['histogram']):
                            reduced_dim = int((num_feature_maps / feat_map_size) / (numBins))
                            if (Params['in_channels'][model_name] == reduced_dim):
                                dim_reduced = False
                                saved_bins[0, :] = model_ft.features.histogram_layer[
                                    -1].centers.detach().cpu().numpy()
                                saved_widths[0, :] = model_ft.features.histogram_layer[-1].widths.reshape(
                                    -1).detach().cpu().numpy()
                            else:
                                dim_reduced = True
                                saved_bins[0, :] = model_ft.features.histogram_layer[
                                    -1].centers.detach().cpu().numpy()
                                saved_widths[0, :] = model_ft.features.histogram_layer[-1].widths.reshape(
                                    -1).detach().cpu().numpy()
                                # When running on hpg, model_ft.features...
                        else:
                            saved_bins = None
                            saved_widths = None
                            dim_reduced = None
    
                        # Setup the loss fxn
                        class_criterion = nn.CrossEntropyLoss()
                     
                        embedding_criterion = TSNE_Loss(reduction='mean', device=device, dof=Params['dof'], alpha=alpha,
                                                        loss_metric=divergence_method)
                        criterion = {'class': class_criterion, 'embed': embedding_criterion}
                     
                        scheduler = None
                        
                    
                        params = list(model_ft.parameters()) + list(embedding_criterion.parameters())
                        optimizer_ft = optim.Adam(params, lr=Params['lr'])
    
                        # Train and evaluate
                        train_dict = train_model(model_ft, dataloaders_dict, criterion, optimizer_ft, device,
                                                 saved_bins=saved_bins, saved_widths=saved_widths,
                                                 histogram=Params['histogram'],
                                                 num_epochs=Params['num_epochs'],
                                                 scheduler=scheduler,
                                                 dim_reduced=dim_reduced,
                                                 weight=current_weight, class_names=class_names)
                        test_dict = test_model(dataloaders_dict['test'], model_ft, criterion,
                                               device, current_weight)
    
                        # Save results
                        if (Params['save_results']):
                            save_results(train_dict, test_dict, split, Params,
                                         num_params, current_weight, current_dim, divergence_method, alpha)
                            del train_dict, test_dict
                            torch.cuda.empty_cache()
    
                        if (Params['histogram']):
                            print('**********Run ' + str(split + 1) + ' For '
                                  + Params['hist_model'] + ' Weight = ' +
                                  str(current_weight) + ' Finished**********')
                        else:
                            print('**********Run ' + str(split + 1) + ' For GAP_' +
                                  model_name + ' Weight = ' + str(current_weight)
                                  + ' Finished**********')
Ejemplo n.º 2
0
                        int(num_feature_maps / (feat_map_size * numBins)),
                        Results_parameters['kernel_size'][model_name],
                        num_bins=numBins,
                        stride=Results_parameters['stride'],
                        normalize_count=Results_parameters['normalize_count'],
                        normalize_bins=Results_parameters['normalize_bins'])

                    # Initialize the histogram model for this run
                    model, input_size = initialize_model(
                        model_name,
                        num_classes,
                        Results_parameters['in_channels'][model_name],
                        num_feature_maps,
                        feature_extract=Results_parameters[
                            'feature_extraction'],
                        histogram=Results_parameters['histogram'],
                        histogram_layer=histogram_layer,
                        parallel=Results_parameters['parallel'],
                        use_pretrained=Results_parameters['use_pretrained'],
                        add_bn=Results_parameters['add_bn'],
                        scale=Results_parameters['scale'],
                        feat_map_size=feat_map_size,
                        embed_dim=dimension)

                    # Set device to cpu or gpu (if available)
                    device_loc = torch.device(device)

                    # If parallelized, need to set change model
                    if Results_parameters['Parallelize']:
                        model = nn.DataParallel(model)
Ejemplo n.º 3
0
            Network_parameters['kernel_size'][model_name],
            num_bins=numBins,
            stride=Network_parameters['stride'],
            normalize_count=Network_parameters['normalize_count'],
            normalize_bins=Network_parameters['normalize_bins'])
    else:
        raise RuntimeError('Invalid type for histogram layer')

    # Initialize the histogram model for this run
    model_ft, input_size = initialize_model(
        model_name,
        num_classes,
        Network_parameters['in_channels'][model_name],
        num_feature_maps,
        feature_extract=Network_parameters['feature_extraction'],
        histogram=Network_parameters['histogram'],
        histogram_layer=histogram_layer,
        parallel=Network_parameters['parallel'],
        use_pretrained=Network_parameters['use_pretrained'],
        add_bn=Network_parameters['add_bn'],
        scale=Network_parameters['scale'],
        feat_map_size=feat_map_size)
    # Send the model to GPU if available
    model_ft = model_ft.to(device)

    #Print number of trainable parameters
    num_params = sum(p.numel() for p in model_ft.parameters()
                     if p.requires_grad)
    print("Number of parameters: %d" % (num_params))
    print("Initializing Datasets and Dataloaders...")