-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added hubconf.py to allow models to be downloaded from torch.hub
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
dependencies = ['torch', 'torchvision'] | ||
|
||
import torch | ||
from model import network | ||
|
||
|
||
AVAILABLE_MODELS = { | ||
"VGG16": [ 64, 128, 256, 512], | ||
"ResNet18": [32, 64, 128, 256, 512], | ||
"ResNet50": [32, 64, 128, 256, 512, 1024, 2048], | ||
"ResNet101": [32, 64, 128, 256, 512, 1024, 2048], | ||
"ResNet152": [32, 64, 128, 256, 512, 1024, 2048], | ||
} | ||
|
||
|
||
def get_trained_model(backbone : str = "ResNet18", fc_output_dim : int = 32) -> torch.nn.Module: | ||
"""Return a model trained with CosPlace on San Francisco eXtra Large. | ||
Args: | ||
backbone (str): which torchvision backbone to use. Must be VGG16 or a ResNet. | ||
fc_output_dim (int): the output dimension of the last fc layer, equivalent to | ||
the descriptors dimension. Must be between 32 and 2048, depending on model's availability. | ||
Return: | ||
model (torch.nn.Module): a trained model. | ||
""" | ||
print(f"Returning CosPlace model with backbone: {backbone} with features dimension {fc_output_dim}") | ||
if backbone not in AVAILABLE_MODELS: | ||
raise ValueError(f"Parameter `backbone` is set to {backbone} but it must be one of {list(AVAILABLE_MODELS.keys())}") | ||
try: | ||
fc_output_dim = int(fc_output_dim) | ||
except: | ||
raise ValueError(f"Parameter `fc_output_dim` must be an integer, but it is set to {fc_output_dim}") | ||
if fc_output_dim not in AVAILABLE_MODELS[backbone]: | ||
raise ValueError(f"Parameter `fc_output_dim` is set to {fc_output_dim}, but for backbone {backbone} " | ||
f"it must be one of {list(AVAILABLE_MODELS[backbone])}") | ||
model = network.GeoLocalizationNet(backbone, fc_output_dim) | ||
model.load_state_dict( | ||
torch.hub.load_state_dict_from_url( | ||
f'https://github.com/gmberton/CosPlace/releases/download/v0.1.0/{backbone}_{fc_output_dim}_cosplace.pth', | ||
map_location=torch.device('cpu')) | ||
) | ||
return model |