implemented load_model function

This commit is contained in:
Brian Bjarke Jensen
2024-07-24 09:13:28 +02:00
parent 96d868a40c
commit 8b1893b30a
+3 -20
View File
@@ -1,42 +1,25 @@
"""Main script to be run by service.""" """Main script to be run by service."""
import logging
from pathlib import Path from pathlib import Path
import torch
from dotenv import load_dotenv from dotenv import load_dotenv
from models import VisualCommunicationModel from models import VisualCommunicationModel
from torchinfo import summary from torchinfo import summary
from utils import get_model_name from utils import load_model
from shared.data_store import connect, get_model from shared.data_store import connect
from shared.utils import setup_logging from shared.utils import setup_logging
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
if __name__ == '__main__': if __name__ == '__main__':
# load in env file # load in env file
env_path = Path(__file__).parent.parent.parent / 'server.env' env_path = Path(__file__).parent.parent.parent / 'server.env'
assert env_path.exists()
load_dotenv(env_path) load_dotenv(env_path)
# setup logging # setup logging
setup_logging() setup_logging()
# connect to minio # connect to minio
minio_client = connect() minio_client = connect()
# instantiate model # instantiate model
model = VisualCommunicationModel() model: VisualCommunicationModel = load_model(client=minio_client)
# get model object name
model_name_path = Path(__file__).parent / 'model_name.txt'
model_object_name = get_model_name(path=model_name_path)
logging.info('using model: %s', model_object_name)
# load model from minio
model_checkpoint = get_model(
client=minio_client,
object_name=model_object_name,
)
model.load_state_dict(model_checkpoint)
# move model to selected device
model = model.to(DEVICE)
# show model weights # show model weights
summary(model) summary(model)
print('loaded model') print('loaded model')