### Install dependencies for deployment Source: https://github.com/gengchenmai/space2vec/blob/master/geo_prior/web_app/readme.md Commands to set up the environment, including Anaconda, PyTorch, Gunicorn, and Nginx. ```bash sudo apt-get update wget https://repo.anaconda.com/archive/Anaconda3-2019.07-Linux-x86_64.sh bash Anaconda3-2019.07-Linux-x86_64.sh export PATH="/home/ubuntu/anaconda2/bin:$PATH" sudo apt-get install unzip sudo apt-get install software-properties-common sudo apt-add-repository universe sudo apt-get update && sudo apt-get install python-pip conda install pytorch-cpu torchvision-cpu -c pytorch conda install gunicorn sudo apt-get install nginx ``` -------------------------------- ### Launch application with Gunicorn Source: https://github.com/gengchenmai/space2vec/blob/master/geo_prior/web_app/readme.md Start the application server using Gunicorn within a screen session. ```bash screen gunicorn application:application ``` -------------------------------- ### Setup NeighGraphEncoderDecoder Components Source: https://context7.com/gengchenmai/space2vec/llms.txt Sets up the necessary components for the NeighGraphEncoderDecoder model, including PointSetEncoder, GridCellSpatialRelationEncoder, and MeanAggregator. ```python from spacegraph_codebase.model import NeighGraphEncoderDecoder from spacegraph_codebase.encoder import PointSetEncoder from spacegraph_codebase.decoder import MeanAggregator from spacegraph_codebase.SpatialRelationEncoder import GridCellSpatialRelationEncoder # Setup components pointset = load_pointset("./data/pointset.pkl") # POI dataset poi_encoder = PointSetEncoder(pointset, embed_dim=64) spatial_encoder = GridCellSpatialRelationEncoder( spa_embed_dim=64, frequency_num=16, max_radius=10000 ) decoder = MeanAggregator(embed_dim=64, spa_embed_dim=64) ``` -------------------------------- ### Setup GlobalPositionEncoderDecoder Components Source: https://context7.com/gengchenmai/space2vec/llms.txt Sets up components for the GlobalPositionEncoderDecoder, a location-only model. It uses GridCellSpatialRelationEncoder and DirectPositionDecoder. ```python from spacegraph_codebase.model import GlobalPositionEncoderDecoder from spacegraph_codebase.decoder import DirectPositionDecoder # Global position encoder for location-only prediction g_spa_encoder = GridCellSpatialRelationEncoder( spa_embed_dim=64, frequency_num=32, max_radius=40000, min_radius=50 ) position_decoder = DirectPositionDecoder(g_spa_embed_dim=64, embed_dim=64) ``` -------------------------------- ### Train Location Encoder Model Source: https://github.com/gengchenmai/space2vec/blob/master/README.md Use this script to train the location encoder model. Ensure all dependencies are installed. ```python python3 train_geo_net.py ``` -------------------------------- ### Get class-specific embedding Source: https://context7.com/gengchenmai/space2vec/llms.txt Extracts the probability for a specific class from the model. ```python class_of_interest = 42 single_class_prob = model(loc_feat, class_of_interest=class_of_interest) ``` -------------------------------- ### Initialize Project Dependencies Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb Imports core project modules, training helpers, and PyTorch utilities. ```python ''' Developed by Gengchen Mai gengchen.mai@gmail.com 05/08/2019 ''' from argparse import ArgumentParser from spacegraph_codebase.utils import * from spacegraph_codebase.Place2Vec.cur_data_utils import load_pointset from spacegraph_codebase.data_utils import load_ng from spacegraph_codebase.model import NeighGraphEncoderDecoder from spacegraph_codebase.train_helper import run_train, run_eval, run_joint_train from torch import optim import numpy as np ``` -------------------------------- ### Configure Nginx site links and restart Source: https://github.com/gengchenmai/space2vec/blob/master/geo_prior/web_app/readme.md Commands to enable the Nginx site configuration and apply changes. ```bash sudo rm /etc/nginx/sites-enabled/default sudo nano /etc/nginx/sites-available/example.com sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com sudo service nginx restart ``` -------------------------------- ### Initialize and use FCNet Source: https://context7.com/gengchenmai/space2vec/llms.txt Demonstrates initializing the FCNet architecture and encoding locations for inference or feature extraction. ```python from geo_prior.geo_prior.models import FCNet # FCNet with residual layers fcnet = FCNet( num_inputs=4, # sin(lon), cos(lon), sin(lat), cos(lat) num_classes=500, num_filts=256, num_users=1 ).to("cuda") # Encode locations using standard approach def encode_location(lon, lat): """Convert lon/lat to sin/cos features.""" lon_rad = torch.tensor(lon) * np.pi / 180 lat_rad = torch.tensor(lat) * np.pi / 180 return torch.stack([ torch.sin(lon_rad), torch.cos(lon_rad), torch.sin(lat_rad), torch.cos(lat_rad) ], dim=-1).float() loc_feats = encode_location([-122.4, 139.7], [37.8, 35.7]).cuda() predictions = fcnet(loc_feats) # Get location embeddings without classification embeddings = fcnet(loc_feats, return_feats=True) print(f"Location embeddings: {embeddings.shape}") # (2, 256) ``` -------------------------------- ### Initialize Encoder and Decoder Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb Sets up logging, loads point set data, and initializes the feature encoder. ```python def make_enc_dec(args): args_combine = make_args_combine(args) log_file = args.log_dir + args_combine + ".log" model_file = args.model_dir + args_combine + ".pth" logger = setup_logging(log_file, filemode='a') print("Loading PointSet data..") pointset, feature_embedding = load_pointset(data_dir=args.data_dir, embed_dim=args.embed_dim, do_feature_sampling = False) if args.cuda: pointset.feature_embed_lookup = cudify(feature_embedding) # make feature encoder enc = get_encoder(pointset.feature_embed_lookup, feature_embedding, pointset, args.enc_agg) if args.model_type == "relative" or args.model_type == "join" or args.model_type == "together": # make relative space encoder ``` -------------------------------- ### Retrieve and extract application code Source: https://github.com/gengchenmai/space2vec/blob/master/geo_prior/web_app/readme.md Download and prepare the web application source files. ```bash wget PATH_TO_CODE/web_app.zip unzip web_app.zip cd web_app/ ``` -------------------------------- ### Initialize Species Data and UI State Source: https://github.com/gengchenmai/space2vec/blob/master/geo_prior/web_app/templates/index.html Populates global arrays for time steps and base64 images, sets the initial distribution image, and disables the submit button. ```javascript var time_step_txt = []; {% for item in time_steps_txt %} time_step_txt[{{loop.index0}}] = "{{item}}"; {% endfor %} var images = []; {% for item in images %} images[{{loop.index0}}] = "data:image/png;base64,{{item}}"; {% endfor %} document.getElementById("distribution_im").src = images[0]; $("#class_of_interest").val('{{class_data["our_id"]}}'); $('#submit_btn').prop('disabled', true); ``` -------------------------------- ### Generate Configuration String Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb Creates a formatted string representing the model configuration based on provided arguments. ```python def make_args_combine(args): args_combine = "/{data:s}-{num_context_sample:d}-{embed_dim:d}-{dropout:.1f}-{enc_agg:s}-{model_type:s}-{spa_enc:s}-{spa_embed_dim:d}-{freq:d}-{max_radius:.1f}-{spa_f_act:s}-{freq_init:s}-{g_spa_enc:s}-{g_spa_embed_dim:d}-{g_freq:d}-{g_max_radius:.1f}-{g_spa_f_act:s}-{g_freq_init:s}-{use_dec:s}-{init_decoder_atten_type:s}-{init_decoder_atten_act:s}-{init_decoder_atten_f_act:s}-{init_decoder_atten_num:d}-{init_decoder_use_layn:s}-{init_decoder_use_postmat:s}-{decoder_atten_type:s}-{decoder_atten_act:s}-{decoder_atten_f_act:s}-{decoder_atten_num:d}-{decoder_use_layn:s}-{decoder_use_postmat:s}-{join_dec_type:s}-{act:s}-{opt:s}-{lr:.6f}-{batch_size:d}".format( data=args.data_dir.strip().split("/")[-2], num_context_sample=args.num_context_sample, embed_dim=args.embed_dim, dropout=args.dropout, enc_agg=args.enc_agg, model_type=args.model_type, spa_enc=args.spa_enc, spa_embed_dim=args.spa_embed_dim, freq=args.freq, max_radius=args.max_radius, spa_f_act=args.spa_f_act, freq_init=args.freq_init, g_spa_enc=args.g_spa_enc, g_spa_embed_dim=args.g_spa_embed_dim, g_freq=args.g_freq, g_max_radius=args.g_max_radius, g_spa_f_act=args.g_spa_f_act, g_freq_init=args.g_freq_init, use_dec=args.use_dec, init_decoder_atten_type=args.init_decoder_atten_type, init_decoder_atten_act=args.init_decoder_atten_act, init_decoder_atten_f_act=args.init_decoder_atten_f_act, init_decoder_atten_num=args.init_decoder_atten_num, init_decoder_use_layn=args.init_decoder_use_layn, init_decoder_use_postmat=args.init_decoder_use_postmat, decoder_atten_type=args.decoder_atten_type, decoder_atten_act=args.decoder_atten_act, decoder_atten_f_act=args.decoder_atten_f_act, decoder_atten_num=args.decoder_atten_num, decoder_use_layn=args.decoder_use_layn, decoder_use_postmat=args.decoder_use_postmat, join_dec_type = args.join_dec_type, act = args.act, opt=args.opt, lr=args.lr, batch_size=args.batch_size ) return args_combine ``` -------------------------------- ### Initialize Encoder-Decoder Model Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb Instantiates the model using provided arguments. ```python g_enc_dec = make_enc_dec(args) ``` -------------------------------- ### Import Visualization and Manifold Libraries Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb Initializes matplotlib and imports scikit-learn components for dimensionality reduction and clustering. ```python import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt from sklearn.manifold import TSNE from sklearn.cluster import AgglomerativeClustering ``` -------------------------------- ### Initialize Optimizer Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb Initializes the optimizer for the model parameters. Supports SGD and Adam optimizers. ```python optimizer = optim.SGD(filter(lambda p : p.requires_grad, enc_dec.parameters()), lr=args.lr, momentum=0) ``` ```python optimizer = optim.Adam(filter(lambda p : p.requires_grad, enc_dec.parameters()), lr=args.lr) ``` -------------------------------- ### Run the application locally Source: https://github.com/gengchenmai/space2vec/blob/master/geo_prior/web_app/readme.md Execute the Flask application using the Python interpreter. ```bash python application.py ``` -------------------------------- ### Define Command-Line Arguments Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb Configures the ArgumentParser with parameters for data directories, model architecture, and spatial encoder settings. ```python parser = ArgumentParser() # dir parser.add_argument("--data_dir", type=str, default="./Place2Vec/") parser.add_argument("--model_dir", type=str, default="./") parser.add_argument("--log_dir", type=str, default="./") parser.add_argument("--num_context_sample", type=int, default=10, help='The number of context points we can sample, maximum is 10') # model parser.add_argument("--embed_dim", type=int, default=64, help='Point feature embedding dim') parser.add_argument("--dropout", type=float, default=0.5, help='The dropout rate used in all fully connected layer') # encoder parser.add_argument("--enc_agg", type=str, default="mean", help='the type of aggragation function for feature encoder') # model type parser.add_argument("--model_type", type=str, default="relative", help='''the type pf model we use, relative: only relatve position; global: only global position; join: relative and global position together: use global position of center point in context prediction''') # space encoder parser.add_argument("--spa_enc", type=str, default="gridcell", help='the type of spatial relation encoder, gridcell/naive') parser.add_argument("--spa_embed_dim", type=int, default=64, help='Point Spatial relation embedding dim') parser.add_argument("--freq", type=int, default=16, help='The number of frequency used in the space encoder') parser.add_argument("--max_radius", type=float, default=10e4, help='The maximum spatial context radius in the space encoder') parser.add_argument("--spa_f_act", type=str, default='sigmoid', help='The final activation function used by spatial relation encoder') parser.add_argument("--freq_init", type=str, default='geometric', help='The frequency list initialization method') # global space/position encoder parser.add_argument("--g_spa_enc", type=str, default="gridcell", help='the type of spatial relation encoder, gridcell/naive') parser.add_argument("--g_spa_embed_dim", type=int, default=64, help='Point Spatial relation embedding dim') parser.add_argument("--g_freq", type=int, default=16, help='The number of frequency used in the space encoder') parser.add_argument("--g_max_radius", type=float, default=10e4, help='The maximum spatial context radius in the space encoder') parser.add_argument("--g_spa_f_act", type=str, default='sigmoid', help='The final activation function used by spatial relation encoder') parser.add_argument("--g_freq_init", type=str, default='geometric', help='The frequency list initialization method') parser.add_argument("--use_dec", type=str, default='T', help='whether to use another decoder following the initial decoder') ``` -------------------------------- ### Train POI type classification model Source: https://context7.com/gengchenmai/space2vec/llms.txt Command-line interface for training Place2Vec with theory-based grid cell encoding. ```bash # Example: Train with theory-based grid cell encoder python -m spacegraph_codebase.Place2Vec.train \ --data_dir ./data_collection/Place2Vec_center/ \ --model_dir ./model_dir/Place2Vec_center/ \ --model_type global \ --spa_enc theory \ --spa_embed_dim 64 \ --freq 16 \ --max_radius 10000 \ --min_radius 100 \ --g_spa_enc gridcell \ --g_spa_embed_dim 64 \ --g_freq 32 \ --g_max_radius 40000 \ --num_hidden_layer 1 \ --hidden_dim 512 \ --batch_size 512 \ --lr 0.001 \ --max_iter 2000 ``` -------------------------------- ### NeighGraphEncoderDecoder Training Loop Source: https://context7.com/gengchenmai/space2vec/llms.txt Demonstrates a basic training loop for the NeighGraphEncoderDecoder model using Adam optimizer and softmax loss. Gradients are backpropagated and weights are updated. ```python # Training loop optimizer = torch.optim.Adam(model.parameters(), lr=0.001) for batch in train_loader: ng_list = batch # List of NeighborGraph objects loss = model.softmax_loss(ng_list, do_full_eval=False) optimizer.zero_grad() loss.backward() optimizer.step() ``` -------------------------------- ### Initialize Context Decoder Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb Initializes a context decoder, used for query embedding and main decoding. Supports various attention mechanisms and configurations. ```python init_dec = get_context_decoder(dec_type=args.init_decoder_atten_type, query_dim=args.embed_dim, key_dim=args.embed_dim, spa_embed_dim=args.spa_embed_dim, g_spa_embed_dim=args.g_spa_embed_dim, have_query_embed = False, num_attn = args.init_decoder_atten_num, activation = args.init_decoder_atten_act, f_activation = args.init_decoder_atten_f_act, layn = args.init_decoder_use_layn, use_postmat = args.init_decoder_use_postmat, dropout = args.dropout) ``` ```python dec = get_context_decoder(dec_type=args.decoder_atten_type, query_dim=args.embed_dim, key_dim=args.embed_dim, spa_embed_dim=args.spa_embed_dim, g_spa_embed_dim=args.g_spa_embed_dim, have_query_embed = True, num_attn = args.decoder_atten_num, activation = args.decoder_atten_act, f_activation = args.decoder_atten_f_act, layn = args.decoder_use_layn, use_postmat = args.decoder_use_postmat, dropout = args.dropout) ``` -------------------------------- ### Parse Model Arguments Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb Defines a list of configuration arguments and parses them into an object. ```python args_list = """--data_dir ../data_collection/Place2Vec/ --model_dir ./model_dir/Place2Vec/ --log_dir ./model_dir/Place2Vec/ --num_context_sample 10 --embed_dim 64 --dropout 0.5 --enc_agg mean --model_type global --spa_enc theory --spa_embed_dim 64 --freq 16 --max_radius 10000 --spa_f_act sigmoid --freq_init geometric --g_spa_enc theory --g_spa_embed_dim 64 --g_freq 16 --g_max_radius 10e5 --g_spa_f_act sigmoid --g_freq_init geometric --use_dec T --init_decoder_atten_type concat --init_decoder_atten_act leakyrelu --init_decoder_atten_f_act sigmoid --init_decoder_atten_num 1 --init_decoder_use_layn T --init_decoder_use_postmat T --decoder_atten_type concat --decoder_atten_act leakyrelu --decoder_atten_f_act sigmoid --decoder_atten_num 1 --decoder_use_layn T --decoder_use_postmat T --join_dec_type max --act sigmoid --opt adam --lr 0.001 --max_iter 2000 --batch_size 512 --log_every 50 --val_every 50 """ args = parser.parse_args(args_list.split()) ``` -------------------------------- ### Training Parameters Configuration Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb Specifies training hyperparameters such as the optimizer ('adam'), learning rate, maximum training iterations, burn-in period, batch size, and tolerance for convergence. ```python parser.add_argument("--opt", type=str, default="adam") ``` ```python parser.add_argument("--lr", type=float, default=0.01, help='learning rate') ``` ```python parser.add_argument("--max_iter", type=int, default=50000000, help='the maximum iterator for model converge') ``` ```python parser.add_argument("--max_burn_in", type=int, default=5000, help='the maximum iterator for relative/global model converge') ``` ```python parser.add_argument("--batch_size", type=int, default=512) ``` ```python parser.add_argument("--tol", type=float, default=0.000001) ``` -------------------------------- ### Initialize NeighGraphEncoderDecoder Model Source: https://context7.com/gengchenmai/space2vec/llms.txt Creates the NeighGraphEncoderDecoder model, which predicts POI types using spatial context of neighboring POIs. Requires a pointset, encoders, and a decoder. ```python # Create encoder-decoder model model = NeighGraphEncoderDecoder( pointset=pointset, enc=poi_encoder, spa_enc=spatial_encoder, init_dec=decoder, dec=None, # Optional second decoder layer activation="sigmoid", num_context_sample=10, # Number of neighbors to sample num_neg_resample=10 # Negative samples for training ) ``` -------------------------------- ### Main Decoder Attention Configuration Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb Sets up the attention mechanism for the main decoder. Similar to the initial decoder, it allows configuration of attention type, activation functions, number of attention layers, and the use of layer normalization and post-matrix. ```python parser.add_argument("--decoder_atten_type", type=str, default='concat', help='''the type of the intersection operator attention concat: the relative model g_pos_concat: the together model''') ``` ```python parser.add_argument("--decoder_atten_act", type=str, default='leakyrelu', help='the activation function of the intersection operator attention, see GAT paper Equ 3') ``` ```python parser.add_argument("--decoder_atten_f_act", type=str, default='sigmoid', help='the final activation function of the intersection operator attention, see GAT paper Equ 6') ``` ```python parser.add_argument("--decoder_atten_num", type=int, default=0, help='the number of the intersection operator attention') ``` ```python parser.add_argument("--decoder_use_layn", type=str, default='T', help='whether to use layer normalzation') ``` ```python parser.add_argument("--decoder_use_postmat", type=str, default='T', help='whether to use post matrix') ``` -------------------------------- ### Load and Evaluate Model with Spatial Prior Source: https://context7.com/gengchenmai/space2vec/llms.txt This snippet demonstrates loading a pre-trained model, combining its spatial prior predictions with CNN predictions, and computing top-k accuracy. Ensure the model is in evaluation mode and gradients are not computed during inference. ```python model = ut.get_model(train_locs=op['train_locs'], params=params, ...) model.load_state_dict(net_params['state_dict']) model.eval() top_k_acc = {1: 0, 3: 0, 5: 0, 10: 0} for i in range(len(val_classes)): cnn_pred = val_preds[i, :] with torch.no_grad(): spatial_prior = model(val_feats[i:i+1]).cpu().numpy()[0] combined_pred = cnn_pred * spatial_prior predicted_class = np.argmax(combined_pred) top_k = np.argsort(combined_pred)[-10:] for k in top_k_acc.keys(): if val_classes[i] in top_k[-k:]: top_k_acc[k] += 1 print(f"Top-1 Accuracy: {top_k_acc[1]/len(val_classes)*100:.2f}%") print(f"Top-5 Accuracy: {top_k_acc[5]/len(val_classes)*100:.2f}%") ``` -------------------------------- ### Set Data Mode and Neighbor Tuple Path Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_test.ipynb Sets the data mode to 'test' and defines the path for the neighbor tuple pickle file. ```python data_mode = "test" neighbor_tuple_path = "/neighbortuple_{}.pkl".format(data_mode) ``` -------------------------------- ### Import Necessary Libraries Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_test.ipynb Imports PyTorch and custom modules for data handling and encoding from the spacegraph_codebase. ```python import torch import cPickle as pickle from spacegraph_codebase.data import PointSet, NeighborGraph,Point from spacegraph_codebase.Place2Vec.data_utils import load_pointset from spacegraph_codebase.encoder import PointFeatureEncoder ``` -------------------------------- ### Load Neighborhood Graph Data Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb Loads training, validation, and testing neighborhood graph data from pickle files. ```python print("Loading NeighGraph data..") print("Loading training NeighGraph data..") train_ng_list = load_ng(args.data_dir + "/neighborgraphs_training.pkl") print("Loading validation NeighGraph data..") val_ng_list = load_ng(args.data_dir + "/neighborgraphs_validation.pkl") print("Loading testing NeighGraph data..") test_ng_list = load_ng(args.data_dir + "/neighborgraphs_test.pkl") ``` -------------------------------- ### Serialize Neighbor Graph Sample Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_test.ipynb Serializes a specific neighbor graph sample from the generated list. ```python ng_list[100].serialize() ``` -------------------------------- ### Load Model State Dictionary Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb Loads the pre-trained state dictionary for the encoder-decoder model from a specified file. ```python print("Load model from {}".format(args_combine + ".pth")) enc_dec.load_state_dict(torch.load(model_file)) ``` -------------------------------- ### Generate Data Samples Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_test.ipynb Calls the make_data_samples function to generate neighbor graph samples for the test data mode. ```python ng_list = make_data_samples(data_mode, pointset, data_dir, neighbor_tuple_path) ``` -------------------------------- ### Evaluation Parameters Configuration Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb Sets parameters for model evaluation, including how often to log training progress and how often to validate the model. ```python parser.add_argument("--log_every", type=int, default=50) ``` ```python parser.add_argument("--val_every", type=int, default=5000) ``` -------------------------------- ### Initial Decoder Attention Configuration Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb Configures the attention mechanism within the initial decoder. Options include 'concat' and 'g_pos_concat' for attention types, 'leakyrelu' for activation, and 'sigmoid' for final activation. Layer normalization and post-matrix usage can be enabled or disabled. ```python parser.add_argument("--init_decoder_atten_type", type=str, default='concat', help='''the type of the intersection operator attention in initial decoder concat: the relative model g_pos_concat: the together model''') ``` ```python parser.add_argument("--init_decoder_atten_act", type=str, default='leakyrelu', help='the activation function of the intersection operator attention, see GAT paper Equ 3 in initial decoder') ``` ```python parser.add_argument("--init_decoder_atten_f_act", type=str, default='sigmoid', help='the final activation function of the intersection operator attention, see GAT paper Equ 6 in initial decoder') ``` ```python parser.add_argument("--init_decoder_atten_num", type=int, default=1, help='the number of the intersection operator attention in initial decoder') ``` ```python parser.add_argument("--init_decoder_use_layn", type=str, default='T', help='whether to use layer normalzation in initial decoder') ``` ```python parser.add_argument("--init_decoder_use_postmat", type=str, default='T', help='whether to use post matrix in initial decoder') ``` -------------------------------- ### Configure image resolution Source: https://github.com/gengchenmai/space2vec/blob/master/geo_prior/pre_process/inat2018/readme.md Update the resolution setting in the loader script to match the model requirements (e.g., 299 or 520). ```python inat2018_loader.py ``` -------------------------------- ### Configure Autocomplete and Input Handling Source: https://github.com/gengchenmai/space2vec/blob/master/geo_prior/web_app/templates/index.html Manages the autocomplete search behavior and toggles the submit button state based on user input. ```javascript $("#autocomplete").keyup(function() { if (!this.value) { $('#submit_btn').prop('disabled', true); } }); $(function() { $("#autocomplete").autocomplete({ source:function(request, response) { $.getJSON("/autocomplete",{ query: request.term, }, function(data) { response(data.matching_classes); }); }, minLength: 3, select: function(event, ui) { $('#class_of_interest').val(ui.item.idx.toString()); $('#submit_btn').prop('disabled', false); console.log(ui.item.value); console.log(ui.item.idx); } }); }) ``` -------------------------------- ### Create Encoder-Decoder Model Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb Constructs the main encoder-decoder model by combining various components based on the model type. ```python enc_dec = get_enc_dec(model_type=args.model_type, pointset=pointset, enc = enc, spa_enc = spa_enc, g_spa_enc = g_spa_enc, g_spa_dec = g_spa_dec, init_dec=init_dec, dec=dec, joint_dec=joint_dec, activation = args.act, num_context_sample = args.num_context_sample, num_neg_resample = 10) ``` -------------------------------- ### Initialize Spatial Encoder Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb Initializes a spatial encoder based on specified arguments. Used for both local and global spatial encoding. ```python spa_enc = get_spa_encoder(spa_enc_type=args.spa_enc, spa_embed_dim=args.spa_embed_dim, coord_dim = 2, frequency_num = args.freq, max_radius = args.max_radius, dropout = args.dropout, freq_init = args.freq_init) ``` ```python g_spa_enc = get_spa_encoder(spa_enc_type=args.g_spa_enc, spa_embed_dim=args.g_spa_embed_dim, coord_dim = 2, frequency_num = args.g_freq, max_radius = args.g_max_radius, dropout = args.dropout, freq_init = args.g_freq_init) ``` -------------------------------- ### Resize and Convert BirdSnap Images Source: https://github.com/gengchenmai/space2vec/blob/master/geo_prior/pre_process/birdsnap/readme.md Use this command to resize and convert raw BirdSnap images to JPG format. Ensure you run this command from within the directory containing the data. ```bash mogrify -geometry "800x800>" -format jpg -path ../images_sm * ``` -------------------------------- ### Initialize LocationEncoder Model Source: https://context7.com/gengchenmai/space2vec/llms.txt Creates a LocationEncoder model for geo-aware image classification. It combines spatial encoding with a classifier to predict species/object probabilities based on location. ```python from geo_prior.geo_prior.models import LocationEncoder # Create location encoder model model = LocationEncoder( spa_enc=spa_enc, num_inputs=96, # Input feature dimension num_classes=500, # Number of species/classes num_filts=256, # Hidden dimension num_users=100 # Optional user modeling ).to("cuda") ``` -------------------------------- ### Initialize NaiveSpatialRelationEncoder Source: https://context7.com/gengchenmai/space2vec/llms.txt Initializes a NaiveSpatialRelationEncoder for normalizing coordinates to a fixed extent range. Useful for baseline comparisons. ```python from geo_prior.geo_prior.SpatialRelationEncoder import NaiveSpatialRelationEncoder # Define spatial extent (x_min, x_max, y_min, y_max) extent = (-180.0, 180.0, -90.0, 90.0) # Global lon/lat range naive_encoder = NaiveSpatialRelationEncoder( spa_embed_dim=2, # Output same as coord_dim for naive extent=extent, coord_dim=2, device="cuda" ) ``` -------------------------------- ### Evaluate image classification with spatial prior Source: https://context7.com/gengchenmai/space2vec/llms.txt Loads test data and a pre-trained model to generate spatial features for evaluation. ```python # geo_prior/geo_prior/run_evaluation.py usage import numpy as np import torch from geo_prior.geo_prior import utils as ut from geo_prior.geo_prior import datasets as dt eval_params = { 'dataset': 'birdsnap', 'eval_split': 'test', 'meta_type': 'ebird_meta', 'spa_enc': 'theory', 'device': 'cuda' } # Load test data and CNN predictions op = dt.load_dataset(eval_params, 'test', True, False, True, False, False) val_preds = op['val_preds'] # CNN softmax predictions (N, num_classes) val_classes = op['val_classes'] # Ground truth labels val_locs = op['val_locs'] # Test locations # Load trained spatial prior model net_params = torch.load("model_birdsnap_ebird_meta_theory.pth.tar") params = net_params['params'] # Prepare location features val_feats = ut.generate_model_input_feats( spa_enc_type=params['spa_enc_type'], locs=val_locs, dates=op['val_dates'], params=params, device=eval_params['device'] ) ``` -------------------------------- ### Configure IPython Autoreload Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb Enables automatic reloading of modules in an IPython environment. ```python %edit %load_ext autoreload %autoreload 2 ``` -------------------------------- ### Define Data Sample Generation Function Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_test.ipynb Defines a function to generate data samples, including loading neighbor tuples, performing negative sampling, and creating NeighborGraph objects. ```python def make_data_samples(data_mode, pointset, data_dir, neighbor_tuple_path, neg_sample_num = 10): # print("Load PointSet....") # pointset = load_pointset(data_dir, point_data_path) print("Load {} neighbor_tuple_list".format(data_mode)) neighbor_tuple_list = pickle.load(open(data_dir + "/" + neighbor_tuple_path, "rb")) print("Do negative sampling and get NeighborGraph") ng_list = pointset.get_data_samples(neighbor_tuple_list, neg_sample_num, data_mode) return ng_list # print("Dump {} NeighborGraph()".format(data_mode)) # pickle.dump([ng.serialize() for ng in ng_list], open(data_dir+"/neighborgraphs_{}.pkl".format(data_mode), "w"), protocol=pickle.HIGHEST_PROTOCOL) ``` -------------------------------- ### Generate Coordinate Grids Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb Creates nested lists of coordinate pairs for spatial processing. ```python # longitude for x in range(-1713000, -1670000+interval, interval): coord.append([x,y]) coords.append(coord) extent = (-1713000, -1670000, 1600000, 1650000) ``` ```python rel_coords = [] for y in range(0, 10010, 10): coord = [] for x in range(0, 10010, 10): coord.append([x,y]) rel_coords.append(coord) ``` -------------------------------- ### Initialize Point Feature Encoder Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_test.ipynb Initializes the PointFeatureEncoder with the feature lookup function, embeddings, and the pointset object. ```python enc = PointFeatureEncoder(pointset.feature_embed_lookup, feature_embedding, pointset) ``` -------------------------------- ### Access Point Dictionary Keys Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_test.ipynb Retrieves the keys from the pointset's point dictionary. ```python a = pointset.pt_dict.keys() ``` -------------------------------- ### Load Autoreload Extension Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_test.ipynb Loads the autoreload extension for IPython to automatically reload modules before executing code. ```python %load_ext autoreload %autoreload 2 ``` -------------------------------- ### Initialize Global Space Decoder Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb Initializes a DirectPositionEmbeddingDecoder for global space decoding, used in 'global' or 'join' model types. ```python g_spa_dec = DirectPositionEmbeddingDecoder(g_spa_embed_dim=args.g_spa_embed_dim, feature_embed_dim=args.embed_dim, f_act = args.act, dropout = args.dropout) ``` -------------------------------- ### Initialize TheoryGridCellSpatialRelationEncoder Source: https://context7.com/gengchenmai/space2vec/llms.txt Initializes a TheoryGridCellSpatialRelationEncoder, which incorporates a feed-forward network for spatial encoding. Requires spatial embedding dimensions, frequency number, and radius parameters. ```python from geo_prior.geo_prior.SpatialRelationEncoder import TheoryGridCellSpatialRelationEncoder spa_enc = TheoryGridCellSpatialRelationEncoder( spa_embed_dim=256, frequency_num=16, max_radius=360, min_radius=0.0005, ffn=ffn, device="cuda" ) ``` -------------------------------- ### Initialize and use TheoryGridCellSpatialRelationEncoder Source: https://context7.com/gengchenmai/space2vec/llms.txt Creates hexagonal grid-like response patterns using three unit vectors at 120-degree angles. The output dimension must be a multiple of 6. ```python from geo_prior.geo_prior.SpatialRelationEncoder import TheoryGridCellSpatialRelationEncoder # Initialize theory-based grid cell encoder theory_encoder = TheoryGridCellSpatialRelationEncoder( spa_embed_dim=96, # Output dimension (must be 6 * frequency_num) coord_dim=2, frequency_num=16, max_radius=10000, min_radius=1000, freq_init="geometric", device="cuda" ) # The encoder projects coordinates onto three unit vectors at 0, 120, 240 degrees # unit_vec1 = [1.0, 0.0] # 0 degrees # unit_vec2 = [-0.5, sqrt(3)/2] # 120 degrees # unit_vec3 = [-0.5, -sqrt(3)/2] # 240 degrees # Encode relative spatial offsets between POIs relative_coords = [ [[50.0, 30.0], [-20.0, 45.0]], # deltaX, deltaY from center [[100.0, -50.0], [0.0, 80.0]] ] embeddings = theory_encoder(relative_coords) print(f"Hexagonal grid embeddings: {embeddings.shape}") # torch.Size([2, 2, 96]) ``` -------------------------------- ### Initialize Joint Decoder Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb Initializes a JointRelativeGlobalDecoder for models of type 'join'. ```python joint_dec = JointRelativeGlobalDecoder(feature_embed_dim = args.embed_dim, f_act = args.act, dropout = args.dropout, join_type = args.join_dec_type) ``` -------------------------------- ### Initialize and use GridCellSpatialRelationEncoder Source: https://context7.com/gengchenmai/space2vec/llms.txt Encodes 2D coordinates using multi-scale sinusoidal functions. Requires geometric frequency spacing parameters. ```python import torch import numpy as np from geo_prior.geo_prior.SpatialRelationEncoder import GridCellSpatialRelationEncoder # Initialize the grid cell encoder encoder = GridCellSpatialRelationEncoder( spa_embed_dim=64, # Output embedding dimension coord_dim=2, # 2D coordinates (x, y) frequency_num=16, # Number of frequency bands max_radius=10000, # Maximum spatial scale (meters) min_radius=10, # Minimum spatial scale (meters) freq_init="geometric", # Geometric frequency spacing device="cuda" ) # Encode batch of spatial coordinates # coords shape: (batch_size, num_context_points, 2) coords = [ [[100.5, 200.3], [150.2, 180.7], [120.0, 210.5]], # Batch 1: 3 points [[300.1, 400.2], [310.5, 390.8], [290.3, 415.1]] # Batch 2: 3 points ] # Forward pass returns spatial embeddings # Output shape: (batch_size, num_context_points, spa_embed_dim) spatial_embeddings = encoder(coords) print(f"Output shape: {spatial_embeddings.shape}") # torch.Size([2, 3, 64]) # The encoder computes multi-scale sinusoidal features: # PE(x, 2i) = sin(x * freq[i]) # PE(x, 2i+1) = cos(x * freq[i]) # where freq[i] follows geometric progression from 1/max_radius to 1/min_radius ``` -------------------------------- ### Configure Nginx server block Source: https://github.com/gengchenmai/space2vec/blob/master/geo_prior/web_app/readme.md Nginx configuration to proxy traffic from port 80 to the local Flask application. ```nginx server { listen 80; location / { proxy_pass http://127.0.0.1:8000/; } } ``` -------------------------------- ### Initialize GlobalPositionEncoderDecoder Model Source: https://context7.com/gengchenmai/space2vec/llms.txt Initializes the GlobalPositionEncoderDecoder model for predicting POI types directly from geographic coordinates. It requires a pointset, encoders, and a decoder. ```python global_model = GlobalPositionEncoderDecoder( pointset=pointset, enc=poi_encoder, g_spa_enc=g_spa_encoder, g_spa_dec=position_decoder, activation="sigmoid", num_neg_resample=10 ) ``` -------------------------------- ### Generate Coordinates Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb Generates a list of coordinates within a specified range and interval, intended for latitude calculations. ```python coords = [] interval = 1000 # latitude for y in range(1600000, 1650000+interval, interval): coord = [] ``` -------------------------------- ### Extract iNat2018 features Source: https://github.com/gengchenmai/space2vec/blob/master/geo_prior/pre_process/inat2018/readme.md Execute the extraction script to obtain features before the final fully connected layer. ```bash python extract_inat_feats.py ``` -------------------------------- ### Initialize and use RBFSpatialRelationEncoder Source: https://context7.com/gengchenmai/space2vec/llms.txt Computes distances to learned anchor points using Radial Basis Functions. Requires training locations for anchor point selection. ```python from geo_prior.geo_prior.SpatialRelationEncoder import RBFSpatialRelationEncoder # Training locations for anchor point selection train_locs = np.random.uniform(-1000, 1000, size=(10000, 2)) # Initialize RBF encoder rbf_encoder = RBFSpatialRelationEncoder( model_type="global", # "global" for absolute, "relative" for context train_locs=train_locs, # Training locations for anchor selection spa_embed_dim=100, coord_dim=2, num_rbf_anchor_pts=100, # Number of RBF anchor points rbf_kernal_size=1000, # RBF kernel bandwidth (sigma) max_radius=10000, device="cuda" ) # Each point's embedding = exp(-dist^2 / (2 * sigma^2)) for each anchor coords = [[[500.0, 300.0]], [[-200.0, 150.0]]] rbf_embeddings = rbf_encoder(coords) print(f"RBF embeddings: {rbf_embeddings.shape}") # torch.Size([2, 1, 100]) ``` -------------------------------- ### Load Point Set Data Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_test.ipynb Loads a point set and its corresponding feature embeddings from a specified directory. ```python data_dir="/home/gengchen/Position_Encoding/data_collection/Place2Vec/" pointset, feature_embedding = load_pointset(data_dir) ``` -------------------------------- ### Load Model Flag Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb A boolean flag to indicate whether to load a pre-existing model. When set, the training process will attempt to load weights from a previously saved model. ```python parser.add_argument("--load_model", action='store_true') ``` -------------------------------- ### Evaluate Location Encoder Model Source: https://github.com/gengchenmai/space2vec/blob/master/README.md This script evaluates the location encoder model by combining pre-trained CNN features. It requires the trained model and CNN features. ```python python3 run_evaluation.py ``` -------------------------------- ### Create MultiLayerFeedForwardNN for Spatial Encoding Source: https://context7.com/gengchenmai/space2vec/llms.txt Defines a MultiLayerFeedForwardNN used within the TheoryGridCellSpatialRelationEncoder. Configurable with input/output dimensions, hidden layers, and activation functions. ```python from geo_prior.geo_prior.module import MultiLayerFeedForwardNN # Create spatial encoder with feed-forward network ffn = MultiLayerFeedForwardNN( input_dim=96, # 6 * frequency_num output_dim=256, # num_filts num_hidden_layers=1, hidden_dim=512, use_layernorm=True, skip_connection=True, dropout=0.5, f_act="relu" ) ``` -------------------------------- ### Normalize Global Coordinates with NaiveSpatialRelationEncoder Source: https://context7.com/gengchenmai/space2vec/llms.txt Uses a NaiveSpatialRelationEncoder to normalize global coordinates to a [-1, 1] range. The output values are within this range. ```python # Coordinates are normalized to [-1, 1] range global_coords = [[[-122.4, 37.8]], [[139.7, 35.7]]] # San Francisco, Tokyo normalized = naive_encoder(global_coords) print(f"Normalized coords: {normalized}") # Values in [-1, 1] ``` -------------------------------- ### Encoder-Decoder Interaction Configuration Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb Defines how the encoder and decoder interact. This includes the type of join operation ('max', 'min', 'mean', 'cat') and the activation function used for the encoder-decoder interface. ```python parser.add_argument("--join_dec_type", type=str, default='max', help='the type of join_dec, min/max/mean/cat') ``` ```python parser.add_argument("--act", type=str, default='sigmoid', help='the activation function for the encoder decoder') ``` -------------------------------- ### Train location encoder for image classification Source: https://context7.com/gengchenmai/space2vec/llms.txt Configures and trains a Space2Vec model on geo-tagged image datasets. ```python # geo_prior/geo_prior/train_geo_net.py usage import torch from geo_prior.geo_prior import utils as ut from geo_prior.geo_prior import datasets as dt from geo_prior.geo_prior import losses as lo # Configuration parameters params = { 'dataset': 'birdsnap', # inat_2018, inat_2017, birdsnap, nabirds 'meta_type': 'ebird_meta', # orig_meta or ebird_meta 'batch_size': 1024, 'lr': 0.001, 'lr_decay': 0.98, 'num_filts': 256, 'num_epochs': 30, 'device': 'cuda', # Space2Vec parameters 'spa_enc_type': 'theory', # theory, gridcell, rbf, wrap, naive 'frequency_num': 64, 'max_radius': 360, 'min_radius': 0.0005, 'freq_init': 'geometric', # Feed-forward network parameters 'num_hidden_layer': 1, 'hidden_dim': 512, 'use_layn': True, 'skip_connection': True, 'dropout': 0.5 } # Load dataset op = dt.load_dataset(params, 'val', True, True) train_locs, train_classes = op['train_locs'], op['train_classes'] # Generate input features train_feats = ut.generate_model_input_feats( spa_enc_type=params['spa_enc_type'], locs=train_locs, dates=op['train_dates'], params=params, device=params['device'] ) # Create model model = ut.get_model( train_locs=train_locs, params=params, spa_enc_type=params['spa_enc_type'], num_inputs=train_feats.shape[1], num_classes=op['num_classes'], num_filts=params['num_filts'], num_users=params['num_users'], device=params['device'] ) # Training optimizer = torch.optim.Adam(model.parameters(), lr=params['lr']) for epoch in range(params['num_epochs']): for loc_feat, loc_class, user_ids in train_loader: optimizer.zero_grad() loss = lo.embedding_loss(model, params, loc_feat, loc_class, user_ids) loss.backward() optimizer.step() ``` -------------------------------- ### Update Distribution Image Source: https://github.com/gengchenmai/space2vec/blob/master/geo_prior/web_app/templates/index.html Updates the displayed image and corresponding time-of-year label based on the provided index. ```javascript function show_image(im_id){ document.getElementById("time_of_year").innerHTML = time_step_txt[im_id]; document.getElementById("distribution_im").src = images[im_id]; } ``` -------------------------------- ### NeighGraphEncoderDecoder Inference Source: https://context7.com/gengchenmai/space2vec/llms.txt Performs inference with the NeighGraphEncoderDecoder model to predict the center POI embedding from its context. Returns predictions, true values, and negative samples. ```python # Inference: predict center POI embedding from context center_pred, center_true, neg_samples = model(ng_list, do_full_eval=True) ``` -------------------------------- ### Lookup Feature Embeddings Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_test.ipynb Performs a lookup for feature embeddings for a given list of point IDs. ```python pointset.feature_embed_lookup([1,2]) ``` -------------------------------- ### Forward Pass with Location Features in LocationEncoder Source: https://context7.com/gengchenmai/space2vec/llms.txt Performs a forward pass with the LocationEncoder model using location features (longitude, latitude) to predict class probabilities. The output shape is (batch_size, num_classes). ```python # Forward pass with location features # loc_feat: (batch_size, 2) - longitude, latitude loc_feat = torch.tensor([[-122.4, 37.8], [139.7, 35.7]]).cuda() class_probs = model(loc_feat) # (batch_size, num_classes) print(f"Class probabilities shape: {class_probs.shape}") ``` -------------------------------- ### Define CUDA Argument Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_visualize.ipynb Adds a boolean flag for CUDA support to the argument parser. ```python parser.add_argument("--cuda", action='store_true') ``` -------------------------------- ### Access Point Data Mode Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_test.ipynb Accesses the data mode of a specific point in the pointset. ```python pointset.pt_dict[100].data_mode ``` -------------------------------- ### Encode Point Features Source: https://github.com/gengchenmai/space2vec/blob/master/spacegraph/spacegraph_test.ipynb Encodes a list of point IDs using the initialized PointFeatureEncoder. ```python enc(list(range(1,5))) ```