### Initialize NetVLAD MATLAB Paths Source: https://context7.com/relja/netvlad/llms.txt Sets up the necessary MATLAB paths for NetVLAD and its dependencies, including MatConvNet, relja_matlab utilities, and the optional Yael library for nearest neighbor search. This is a prerequisite for using other NetVLAD functions. ```matlab % Initialize NetVLAD environment setup; % Verify paths are set correctly paths = localPaths(); disp(paths.libMatConvNet); % Should show MatConvNet path ``` -------------------------------- ### Create Custom Dataset using dbBase Source: https://context7.com/relja/netvlad/llms.txt Provides a template for creating custom datasets by inheriting from the 'dbBase' class. Requires implementing the constructor to set the dataset name and path, and loading a 'dbStruct.mat' file containing image paths and ground truth information. ```matlab classdef dbMyDataset < dbBase methods function db = dbMyDataset(whichSet) db.whichSet = whichSet; db.name = sprintf('mydataset_%s', whichSet); paths = localPaths(); db.dbPath = [paths.dsetRootMyData, 'images/']; db.qPath = [paths.dsetRootMyData, 'queries/']; db.dbLoad(); % Load dbStruct.mat end end end ``` -------------------------------- ### Configure Library Paths Source: https://context7.com/relja/netvlad/llms.txt Defines essential paths for the NetVLAD library, including dependencies, datasets, pretrained models, and output directories. Users should copy 'localPaths.m.setup' to 'localPaths.m' and edit it for their specific system configuration. ```matlab function paths = localPaths() % Dependencies paths.libReljaMatlab = '~/Code/relja_matlab/'; paths.libMatConvNet = '~/Code/matconvnet/'; paths.libYaelMatlab = '~/Code/yael_matlab/'; % or 'yael_dummy/' for slow fallback % Dataset specifications (*.mat files with dbStruct) paths.dsetSpecDir = '~/Data/netvlad/datasets/'; % Dataset root directories paths.dsetRootPitts = '~/Databases/Pittsburgh/'; paths.dsetRootTokyo247 = '~/Databases/Tokyo247/'; paths.dsetRootTokyoTM = '~/Databases/TokyoTM/'; paths.dsetRootOxford = '~/Databases/OxfordBuildings/'; paths.dsetRootParis = '~/Databases/Paris/'; paths.dsetRootHolidays = '~/Databases/Holidays/'; % Pretrained networks paths.ourCNNs = '~/Data/models/'; % NetVLAD trained models paths.pretrainedCNNs = '~/Data/pretrained/'; % ImageNet pretrained % Initialization data (cluster centers) paths.initData = '~/Data/netvlad/initdata/'; % Output directory (features, checkpoints) paths.outPrefix = '~/Data/netvlad/output/'; end % Usage paths = localPaths(); fprintf('Output will be saved to: %s\n', paths.outPrefix); ``` -------------------------------- ### Train NetVLAD Model Source: https://github.com/relja/netvlad/blob/master/README.md This snippet demonstrates the core process of training a NetVLAD model. It involves setting up MATLAB paths, loading training and validation datasets, and initiating the training using the `trainWeakly` function. Key parameters like network ID, layer names, and training methods are configurable. ```matlab setup; dbTrain= dbTokyoTimeMachine('train'); dbVal= dbTokyoTimeMachine('val'); sessionID= trainWeakly(dbTrain, dbVal, ... 'netID', 'vd16', 'layerName', 'conv5_3', 'backPropToLayer', 'conv5_1', ... 'method', 'vlad_preL2_intra', ... 'learningRate', 0.0001, ... 'doDraw', true); ``` -------------------------------- ### Configuration (localPaths) Source: https://context7.com/relja/netvlad/llms.txt Configuration management for library dependencies, dataset roots, and output directories. ```APIDOC ## GET /config/paths ### Description Retrieves system-specific paths for dependencies and data storage. ### Method MATLAB Function Call ### Response #### Success Response (200) - **paths** (struct) - Contains fields for libReljaMatlab, libMatConvNet, dsetRootPitts, outPrefix, etc. ``` -------------------------------- ### Reshaping NetVLAD Output for Product Quantization Source: https://github.com/relja/netvlad/blob/master/README_more.md Demonstrates how to reshape the NetVLAD output matrix (KxD) into a format suitable for Product Quantization (PQ). This reshaping groups values associated with cluster centers, potentially improving PQ performance. It involves reshaping the KxD matrix to K*D x 1. ```python netvladForPQ= reshape( reshape(netvlad, [K, D])', [K*D, 1] ); ``` -------------------------------- ### Load and Initialize NetVLAD Network Source: https://github.com/relja/netvlad/blob/master/README.md Initializes the environment and loads a pre-trained NetVLAD network model. It includes a utility to tidy the network structure for compatibility with the latest MatConvNet versions. ```MATLAB setup; netID = 'vd16_tokyoTM_conv5_3_vlad_preL2_intra_white'; paths = localPaths(); load(sprintf('%s%s.mat', paths.ourCNNs, netID), 'net'); net = relja_simplenn_tidy(net); ``` -------------------------------- ### POST /trainWeakly Source: https://github.com/relja/netvlad/blob/master/README_more.md Configures and initiates the weakly supervised training process for the NetVLAD network architecture. ```APIDOC ## POST /trainWeakly ### Description Initiates the training process for the NetVLAD network. This function accepts a configuration structure containing network architecture settings, SGD hyperparameters, and training process controls. ### Method POST ### Endpoint /trainWeakly ### Parameters #### Request Body - **netID** (string) - Required - Network name ('caffe' or 'vd16') - **layerName** (string) - Required - Layer to crop the network at (e.g., 'conv5') - **method** (string) - Optional - Aggregation method (default: 'vlad_preL2_intra') - **margin** (float) - Optional - Margin parameter for the loss function - **learningRate** (float) - Optional - SGD learning rate - **batchSize** (integer) - Optional - Number of training tuples per batch - **useGPU** (boolean) - Optional - Whether to use GPU acceleration - **sessionID** (string) - Optional - Unique identifier for the training run ### Request Example { "netID": "vd16", "layerName": "conv5_3", "method": "vlad_preL2_intra", "margin": 0.1, "learningRate": 0.001, "batchSize": 4, "useGPU": true, "sessionID": "run_001" } ### Response #### Success Response (200) - **status** (string) - Training initialization status - **sessionID** (string) - The ID assigned to the training session #### Response Example { "status": "success", "sessionID": "run_001" } ``` -------------------------------- ### Full NetVLAD Layer Implementation Source: https://context7.com/relja/netvlad/llms.txt Provides the complete NetVLAD layer implementation as described in the paper, featuring trainable bias parameters. This version may yield better results but converges more slowly than the simplified 'layerVLAD'. It is enabled using the 'vladv2_preL2_intra' method. ```matlab % Use 'vladv2_preL2_intra' method to enable this layer opts.method = 'vladv2_preL2_intra'; net = addLayers(net, opts, dbTrain); ``` -------------------------------- ### Load Training Output Data Source: https://github.com/relja/netvlad/blob/master/README_more.md Loads specific variables ('obj', 'opts', 'auxData') from a saved training checkpoint file. These variables contain performance metrics, training options, and auxiliary data necessary for analyzing training progress. ```matlab load('0fd5_ep000020_latest.mat', 'obj', 'opts', 'auxData'); ``` -------------------------------- ### Simplified NetVLAD Layer Implementation Source: https://context7.com/relja/netvlad/llms.txt Implements a simplified version of the NetVLAD layer with fixed bias terms, designed for faster convergence. It assumes L2-normalized input descriptors and is the version used in the paper's experiments. The output dimension is K*D, where K is the number of clusters and D is the descriptor dimension. ```matlab % The layer is automatically added by addLayers() % Internal structure: % - Soft assignment via softmax(alpha * clstsAssign' * x) % - Residual computation: x - clst_k for each cluster k % - Aggregation: sum of assignment-weighted residuals % - Output: K*D dimensional vector (K clusters, D descriptor dim) % For K=64 clusters and D=512 (conv5_3), output is 32768-D before whitening ``` -------------------------------- ### Custom Dataset Implementation (dbBase) Source: https://context7.com/relja/netvlad/llms.txt Base class for integrating custom datasets into the NetVLAD pipeline. ```APIDOC ## POST /datasets/custom ### Description Create a custom dataset by inheriting from dbBase and providing the required structure. ### Method Class Inheritance ### Parameters #### Request Body - **dbImageFns** (cell array) - Required - Paths to database images. - **qImageFns** (cell array) - Required - Paths to query images. - **utmDb** (matrix) - Required - UTM coordinates for database images. - **utmQ** (matrix) - Required - UTM coordinates for query images. ### Request Example classdef dbMyDataset < dbBase methods function db = dbMyDataset(whichSet) db.name = 'mydataset'; db.dbLoad(); end end end ``` -------------------------------- ### Load Oxford/Paris Buildings Dataset Source: https://context7.com/relja/netvlad/llms.txt Loads the Oxford 5k and Paris 6k buildings datasets for image retrieval evaluation. The 'dbVGG' function takes the dataset name and an optional receptive field size for ROI cropping. Using -1 for receptive field size is not recommended for standard evaluation. ```matlab recFieldSize = 163; dbOxford = dbVGG('ox5k', recFieldSize); dbParis = dbVGG('paris', recFieldSize); dbOxford_full = dbVGG('ox5k', -1); ``` -------------------------------- ### Train NetVLAD Network (trainWeakly) Source: https://context7.com/relja/netvlad/llms.txt Trains a NetVLAD network using weakly supervised learning with triplet ranking loss and hard negative mining, leveraging geolocation data. ```APIDOC ## trainWeakly - Train NetVLAD Network ### Description Main training function that performs weakly supervised learning using triplet ranking loss with hard negative mining. The training uses only weak supervision from image geolocation (UTM coordinates) without requiring landmark annotations. ### Method This is a conceptual API call, represented by the MATLAB function `trainWeakly`. ### Endpoint N/A (MATLAB function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```matlab % Set up training and validation datasets dbTrain = dbTokyoTimeMachine('train'); dbVal = dbTokyoTimeMachine('val'); % Train VGG-16 + NetVLAD, fine-tuning from conv5_1 sessionID = trainWeakly(dbTrain, dbVal, ... 'netID', 'vd16', ... % Base network 'layerName', 'conv5_3', ... % Crop at this layer 'backPropToLayer', 'conv5_1', ... % Fine-tune down to this layer 'method', 'vlad_preL2_intra', ... % Aggregation method 'learningRate', 0.0001, ... % Initial learning rate 'momentum', 0.9, ... % SGD momentum 'weightDecay', 0.001, ... % L2 regularization 'batchSize', 4, ... % Training batch size (tuples) 'nEpoch', 30, ... % Number of epochs 'margin', 0.1, ... % Triplet loss margin 'nNegChoice', 1000, ... % Random negatives to sample 'nNegCap', 10, ... % Hard negatives per tuple 'nNegCache', 10, ... % Cached hard negatives 'lrDownFreq', 5, ... % LR decay frequency (epochs) 'lrDownFactor', 2, ... % LR decay factor 'compFeatsFrequency', 1000, ... % Cache refresh frequency 'saveFrequency', 2000, ... % Checkpoint frequency 'doDraw', true, ... % Plot training curves 'useGPU', true); % Training outputs files like: _ep000001_latest.mat % Contains: net, obj (performance curves), opts, auxData % For Pittsburgh dataset with different learning rate dbTrainPitts = dbPitts(false, 'train'); % Pitts30k dbValPitts = dbPitts(false, 'val'); sessionID = trainWeakly(dbTrainPitts, dbValPitts, ... 'netID', 'vd16', 'layerName', 'conv5_3', ... 'method', 'vlad_preL2_intra', ... 'learningRate', 0.001, ... % Higher LR for Pitts30k 'backPropToLayer', 'conv5_1'); % Train AlexNet + max pooling (faster, lower performance) sessionID = trainWeakly(dbTrain, dbVal, ... 'netID', 'caffe', 'layerName', 'conv5', ... 'method', 'max', ... 'backPropToLayer', 'conv2', ... 'learningRate', 0.0001); ``` ### Response #### Success Response (200) Returns a session ID for the training run. Training progress and results are saved to files. #### Response Example ```matlab % sessionID string, e.g., 'vd16_conv5_3_vlad_preL2_intra_20231027T100000' ``` ``` -------------------------------- ### Plot Training Performance Curves Source: https://github.com/relja/netvlad/blob/master/README_more.md Visualizes the training performance using the loaded data. It plots various metrics such as dynamic loss, evaluated loss on training and validation sets, and recall@N for both sets. Requires 'obj', 'opts', and 'auxData' to be loaded. ```matlab plotResults(obj, opts, auxData); ``` -------------------------------- ### Dataset Loaders (dbPitts, dbVGG, dbHolidays) Source: https://context7.com/relja/netvlad/llms.txt Functions to load standard computer vision datasets for training and evaluation of place recognition and retrieval models. ```APIDOC ## GET /datasets/load ### Description Loads standard datasets for place recognition and image retrieval benchmarking. ### Method MATLAB Function Call ### Parameters #### Path Parameters - **doPitts250k** (boolean) - Required - Flag to use 250k variant of Pittsburgh dataset. - **set** (string) - Required - Dataset split ('train', 'val', 'test'). - **dsetName** (string) - Required - Name of VGG dataset ('ox5k', 'paris'). - **recFieldSize** (integer) - Required - Receptive field size for ROI cropping. ### Request Example % Load Pittsburgh 250k test set dbTest250k = dbPitts(true, 'test'); ### Response #### Success Response (200) - **db** (object) - Dataset object containing image paths and metadata. ``` -------------------------------- ### Visualize Training Progress Source: https://context7.com/relja/netvlad/llms.txt Generates plots for training curves including dynamic triplet loss, ranking loss, and recall@N metrics. This is essential for monitoring convergence and diagnosing training issues. ```matlab load('abc1_ep000020_latest.mat', 'obj', 'opts', 'auxData'); plotResults(obj, opts, auxData); ``` -------------------------------- ### Load Pretrained CNN Base Network for NetVLAD Source: https://context7.com/relja/netvlad/llms.txt Loads a specified pretrained CNN (VGG-16, VGG-19, AlexNet, or Places-CNN) and optionally crops it at a particular convolutional layer to serve as the feature extractor for NetVLAD. This is the initial step in building a custom NetVLAD network. ```matlab % Load VGG-16 cropped at conv5_3 (last conv layer) net = loadNet('vd16', 'conv5_3'); % Load AlexNet cropped at conv5 net_alex = loadNet('caffe', 'conv5'); % Load full VGG-19 network (without cropping) net_full = loadNet('vd19'); % Supported network IDs: % 'vd16' - VGG-16 (imagenet-vgg-verydeep-16) % 'vd19' - VGG-19 (imagenet-vgg-verydeep-19) % 'caffe' - AlexNet (imagenet-caffe-ref) % 'places' - Places-CNN (places-caffe) % Check network metadata disp(net.meta.netID); % 'vd16' disp(net.meta.sessionID); % 'vd16_offtheshelf_conv5_3' ``` -------------------------------- ### Access Tokyo 24/7 Test Dataset Source: https://context7.com/relja/netvlad/llms.txt Provides an interface to the Tokyo 24/7 benchmark dataset, allowing access to image paths and metadata for evaluation. ```matlab dbTest = dbTokyo247(); fprintf('Database images: %d\n', dbTest.numImages); fprintf('Query images: %d\n', dbTest.numQueries); dbTest.dbImageFns{1}; dbTest.qImageFns{1}; ``` -------------------------------- ### MATLAB: Reading Binary Feature Files Source: https://github.com/relja/netvlad/blob/master/README_more.md Reads binary feature files generated by the serialAllFeats function. It assumes a D-dimensional representation for each image and reads them as 32-bit floats. The function requires the feature file name (featFn), the dimensionality of the features (D), and the number of images (numImages). ```matlab feats= fread( fopen(featFn, 'rb'), [D, numImages], 'float32=>single'); ``` -------------------------------- ### Train NetVLAD Network Source: https://context7.com/relja/netvlad/llms.txt Executes weakly supervised training using triplet ranking loss and hard negative mining based on geolocation data. ```matlab dbTrain = dbTokyoTimeMachine('train'); dbVal = dbTokyoTimeMachine('val'); sessionID = trainWeakly(dbTrain, dbVal, 'netID', 'vd16', 'layerName', 'conv5_3', 'backPropToLayer', 'conv5_1', 'method', 'vlad_preL2_intra', 'learningRate', 0.0001, 'momentum', 0.9, 'weightDecay', 0.001, 'batchSize', 4, 'nEpoch', 30, 'margin', 0.1, 'nNegChoice', 1000, 'nNegCap', 10, 'nNegCache', 10, 'lrDownFreq', 5, 'lrDownFactor', 2, 'compFeatsFrequency', 1000, 'saveFrequency', 2000, 'doDraw', true, 'useGPU', true); dbTrainPitts = dbPitts(false, 'train'); dbValPitts = dbPitts(false, 'val'); sessionID = trainWeakly(dbTrainPitts, dbValPitts, 'netID', 'vd16', 'layerName', 'conv5_3', 'method', 'vlad_preL2_intra', 'learningRate', 0.001, 'backPropToLayer', 'conv5_1'); sessionID = trainWeakly(dbTrain, dbVal, 'netID', 'caffe', 'layerName', 'conv5', 'method', 'max', 'backPropToLayer', 'conv2', 'learningRate', 0.0001); ``` -------------------------------- ### Evaluate Tokyo 24/7 Recall Metrics Source: https://github.com/relja/netvlad/blob/master/README_more.md Calculates recall metrics for the Tokyo 24/7 dataset. It demonstrates how to extract overall recall and filter results by specific time-of-day categories (daytime, sunset, nighttime) using index slicing on the allRecalls matrix. ```matlab [recall, ~, allRecalls, opts]= testFromFn(dbTest, dbFeatFn, qFeatFn); recalls= mean( allRecalls, 1 )'; recallDay= mean( allRecalls(1:3:end,:), 1)'; recallSunsetNight= mean( allRecalls([2:3:end, 3:3:end],:), 1)'; ``` -------------------------------- ### Select Best Network from Training Results Source: https://context7.com/relja/netvlad/llms.txt Analyzes training results to identify the network epoch with the highest validation recall. This helps prevent overfitting by selecting the optimal model state. ```matlab [bestEpoch, bestNet] = pickBestNet(sessionID); [bestEpoch, bestNet] = pickBestNet(sessionID, 10); [bestEpoch, bestNet] = pickBestNet(sessionID, 5, 0); finalNet = addPCA(bestNet, dbTrain, 'doWhite', true, 'pcaDim', 4096); ``` -------------------------------- ### Evaluate Network Performance on Datasets Source: https://context7.com/relja/netvlad/llms.txt Tests a trained network on place recognition datasets by computing recall@N metrics. Supports dimensionality reduction via cropping and loading precomputed features. ```matlab dbTest = dbTokyo247(); [recall, rankloss, allRecalls, opts] = testFromFn(dbTest, dbFeatFn, qFeatFn); recall_256 = testFromFn(dbTest, dbFeatFn, qFeatFn, [], 'cropToDim', 256); [~, ~, allRecalls, opts] = testFromFn(dbTest, dbFeatFn, qFeatFn); recallDay = mean(allRecalls(1:3:end,:), 1)'; ``` -------------------------------- ### Batch Feature Extraction for Multiple Images Source: https://context7.com/relja/netvlad/llms.txt Efficiently extracts NetVLAD representations for a set of images using batched GPU processing. Outputs are saved in a binary format for memory efficiency. This method is recommended for processing multiple images as it optimizes GPU usage and handles batching. ```matlab % Load network netID = 'vd16_tokyoTM_conv5_3_vlad_preL2_intra_white'; paths = localPaths(); load(sprintf('%s%s.mat', paths.ourCNNs, netID), 'net'); net = relja_simplenn_tidy(net); % Define image set imPath = '/path/to/images/'; imageFns = {'img001.jpg', 'img002.jpg', 'img003.jpg'}; % cell array of filenames outputFn = '/path/to/output/features.bin'; % Extract features with batch processing serialAllFeats(net, imPath, imageFns, outputFn, ... 'batchSize', 10, ... % Process 10 images at a time (adjust for GPU memory) 'useGPU', true, ... % Use GPU acceleration 'numThreads', 12); % Parallel image loading threads % For variable-sized images (common in place recognition), use batchSize=1 serialAllFeats(net, imPath, imageFns, outputFn, 'batchSize', 1); % Read the binary output file D = 4096; % Feature dimensionality numImages = length(imageFns); feats = fread(fopen(outputFn, 'rb'), [D, numImages], 'float32=>single'); % feats is now a 4096 x numImages matrix ``` -------------------------------- ### Load INRIA Holidays Dataset Source: https://context7.com/relja/netvlad/llms.txt Loads the INRIA Holidays dataset for image retrieval evaluation. The 'dbHolidays' function takes a boolean argument to select between the original dataset and a rotated variant. ```matlab dbHolidays_orig = dbHolidays(false); dbHolidays_rot = dbHolidays(true); ``` -------------------------------- ### Manage Tokyo Time Machine Training Data Source: https://context7.com/relja/netvlad/llms.txt Handles loading of training and validation splits for the Tokyo Time Machine dataset, including utilities for sampling negatives and identifying positive matches. ```matlab dbTrain = dbTokyoTimeMachine('train'); dbVal = dbTokyoTimeMachine('val'); potPosIDs = dbTrain.nontrivialPosQ(1); negIDs = dbTrain.sampleNegsQ(1, 100); ``` -------------------------------- ### Evaluate Place Recognition Performance Source: https://github.com/relja/netvlad/blob/master/README.md Evaluates the network on a place recognition dataset by computing database and query features, then calculating recall@N metrics. ```MATLAB dbTest = dbTokyo247(); serialAllFeats(net, dbTest.dbPath, dbTest.dbImageFns, dbFeatFn, 'batchSize', 10); serialAllFeats(net, dbTest.qPath, dbTest.qImageFns, qFeatFn, 'batchSize', 1); [recall, ~, ~, opts] = testFromFn(dbTest, dbFeatFn, qFeatFn); plot(opts.recallNs, recall, 'ro-'); ``` -------------------------------- ### Add PCA and Whitening to Network Source: https://github.com/relja/netvlad/blob/master/README.md This function applies Principal Component Analysis (PCA) and whitening to the best trained network. This step is crucial for reducing the dimensionality of image representations and often leads to improved performance. ```matlab finalNet= addPCA(bestNet, dbTrain, 'doWhite', true, 'pcaDim', 4096); ``` -------------------------------- ### Add Aggregation Layers to Network Source: https://context7.com/relja/netvlad/llms.txt Configures and appends pooling layers to a base CNN. Supports NetVLAD variants, global max pooling, and global average pooling. ```matlab net = loadNet('vd16', 'conv5_3'); dbTrain = dbPitts(false, 'train'); opts.netID = 'vd16'; opts.layerName = 'conv5_3'; opts.method = 'vlad_preL2_intra'; net = addLayers(net, opts, dbTrain); opts_max.netID = 'vd16'; opts_max.layerName = 'conv5_3'; opts_max.method = 'max'; net_max = addLayers(loadNet('vd16', 'conv5_3'), opts_max, dbTrain); ``` -------------------------------- ### NetVLAD Layers (layerVLAD) Source: https://context7.com/relja/netvlad/llms.txt Implementation of the NetVLAD aggregation layer for feature extraction. ```APIDOC ## POST /layers/vlad ### Description Adds a NetVLAD layer to the network architecture. ### Method MATLAB Function Call ### Parameters #### Request Body - **opts** (struct) - Required - Configuration options including method type ('vladv2_preL2_intra'). - **dbTrain** (object) - Required - Training dataset object. ### Request Example opts.method = 'vladv2_preL2_intra'; net = addLayers(net, opts, dbTrain); ``` -------------------------------- ### Load Pittsburgh Dataset Source: https://context7.com/relja/netvlad/llms.txt Loads the Pittsburgh place recognition dataset in either 30k or 250k variants for training, validation, or testing. The 'dbPitts' function takes a boolean to select the dataset size and a string for the set type. ```matlab dbTrain = dbPitts(false, 'train'); % doPitts250k=false dbVal = dbPitts(false, 'val'); dbTest = dbPitts(false, 'test'); dbTrain250k = dbPitts(true, 'train'); dbVal250k = dbPitts(true, 'val'); dbTest250k = dbPitts(true, 'test'); ``` -------------------------------- ### Add Aggregation Layer (addLayers) Source: https://context7.com/relja/netvlad/llms.txt Adds an aggregation layer such as NetVLAD, max pooling, or average pooling to a base CNN. It supports various configuration options for aggregation and normalization. ```APIDOC ## addLayers - Add Aggregation Layer to Network ### Description Adds the NetVLAD pooling layer (or max/avg pooling) on top of a base CNN. For NetVLAD, this also computes or loads cluster centers from the training set. The method parameter controls the aggregation type and normalization options. ### Method This is a conceptual API call, represented by the MATLAB function `addLayers`. ### Endpoint N/A (MATLAB function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```matlab % Load base network and training dataset net = loadNet('vd16', 'conv5_3'); dbTrain = dbPitts(false, 'train'); % Pittsburgh 30k training set % Configure options opts.netID = 'vd16'; opts.layerName = 'conv5_3'; opts.method = 'vlad_preL2_intra'; % NetVLAD with L2-normalized inputs and intra-normalization % Add NetVLAD layer net = addLayers(net, opts, dbTrain); % Alternative aggregation methods: % 'vlad_preL2_intra' - NetVLAD with input L2-norm and intra-normalization (recommended) % 'vlad_preL2' - NetVLAD with input L2-norm only % 'vladv2_preL2_intra' - Full NetVLAD (trainable biases) % 'max' - Global max pooling % 'avg' - Global average pooling % For max pooling opts_max.netID = 'vd16'; opts_max.layerName = 'conv5_3'; opts_max.method = 'max'; net_max = addLayers(loadNet('vd16', 'conv5_3'), opts_max, dbTrain); ``` ### Response #### Success Response (200) Returns the modified network object with the added layer. #### Response Example ```matlab % net object after modification ``` ``` -------------------------------- ### Pick Best Trained Network Source: https://github.com/relja/netvlad/blob/master/README.md After training, this function is used to identify the network that achieved the best performance on the validation set, typically evaluated using recall@N metrics. ```matlab [~, bestNet]= pickBestNet(sessionID); ``` -------------------------------- ### Add PCA Whitening Layer Source: https://context7.com/relja/netvlad/llms.txt Appends a PCA dimensionality reduction layer to the network to compress feature vectors and improve performance through whitening. ```matlab dbTrain = dbTokyoTimeMachine('train'); finalNet = addPCA(bestNet, dbTrain, 'doWhite', true, 'pcaDim', 4096, 'nTrainCap', 10000, 'batchSize', 10, 'useGPU', true); compactNet = addPCA(bestNet, dbTrain, 'doWhite', true, 'pcaDim', 256); net_pca = addPCA(bestNet, dbTrain, 'doWhite', false, 'pcaDim', 4096); ``` -------------------------------- ### Add PCA Whitening Layer (addPCA) Source: https://context7.com/relja/netvlad/llms.txt Applies PCA dimensionality reduction and optional whitening to a network's output. This is crucial for improving place recognition performance and reducing feature dimensionality. ```APIDOC ## addPCA - Add PCA Whitening Layer ### Description Adds a PCA dimensionality reduction layer with optional whitening to the network. Whitening significantly improves place recognition performance and reduces the representation dimensionality from the full NetVLAD vector (32768 for K=64, D=512) to a more compact form (typically 4096 or smaller). ### Method This is a conceptual API call, represented by the MATLAB function `addPCA`. ### Endpoint N/A (MATLAB function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```matlab % After training, add PCA+whitening to the best network dbTrain = dbTokyoTimeMachine('train'); % Add whitening with 4096 output dimensions finalNet = addPCA(bestNet, dbTrain, ... 'doWhite', true, ... % Enable whitening (highly recommended) 'pcaDim', 4096, ... % Output dimensionality 'nTrainCap', 10000, ... % Max training images for PCA 'batchSize', 10, ... % Batch size for feature extraction 'useGPU', true); % For smaller representations (e.g., 256-D for mobile deployment) compactNet = addPCA(bestNet, dbTrain, 'doWhite', true, 'pcaDim', 256); % PCA without whitening (not recommended) net_pca = addPCA(bestNet, dbTrain, 'doWhite', false, 'pcaDim', 4096); ``` ### Response #### Success Response (200) Returns the network object with the PCA layer added. #### Response Example ```matlab % finalNet, compactNet, or net_pca object after modification ``` ``` -------------------------------- ### Compute Single Image Representation using NetVLAD Source: https://context7.com/relja/netvlad/llms.txt Calculates the NetVLAD feature vector for a single image by performing a forward pass through a loaded CNN. It requires images to be in single precision format. The function supports GPU acceleration and can be configured to use the CPU. ```matlab % Load a pretrained NetVLAD network netID = 'vd16_tokyoTM_conv5_3_vlad_preL2_intra_white'; paths = localPaths(); load(sprintf('%s%s.mat', paths.ourCNNs, netID), 'net'); net = relja_simplenn_tidy(net); % Load and process an image (must use vl_imreadjpeg for correct format) im = vl_imreadjpeg({which('football.jpg')}); im = im{1}; % Compute the image representation (4096-D vector by default) feats = computeRepresentation(net, im); % feats is a 4096x1 single precision vector % Use CPU instead of GPU (slower but works without CUDA) feats_cpu = computeRepresentation(net, im, 'useGPU', false); % Check dimensionality fprintf('Feature dimension: %d\n', length(feats)); % Output: Feature dimension: 4096 ``` -------------------------------- ### Compute Image Representations Source: https://github.com/relja/netvlad/blob/master/README.md Extracts feature representations from images using the loaded network. Supports single image processing and batch processing for efficiency. ```MATLAB im = vl_imreadjpeg({which('football.jpg')}); im = im{1}; feats = computeRepresentation(net, im); serialAllFeats(net, imPath, imageFns, outputFn); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.