added full model load script
This commit is contained in:
+25
-28
@@ -1,45 +1,42 @@
|
|||||||
"""Main script to be run by service."""
|
"""Main script to be run by service."""
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from hashlib import md5
|
import logging
|
||||||
from io import BytesIO
|
from pathlib import Path
|
||||||
|
|
||||||
import torch
|
import torch
|
||||||
from data_store import connect
|
from dotenv import load_dotenv
|
||||||
from data_store import put
|
|
||||||
from models import VisualCommunicationModel
|
from models import VisualCommunicationModel
|
||||||
from torchinfo import summary
|
from torchinfo import summary
|
||||||
from utils import check_env
|
from utils import get_model_name
|
||||||
|
|
||||||
|
from shared.data_store import connect, get_model
|
||||||
from shared.utils import setup_logging
|
from shared.utils import setup_logging
|
||||||
|
|
||||||
|
|
||||||
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# check that environment variables are set
|
# load in env file
|
||||||
check_env()
|
env_path = Path(__file__).parent.parent.parent / 'server.env'
|
||||||
|
assert env_path.exists()
|
||||||
|
load_dotenv(env_path)
|
||||||
# setup logging
|
# setup logging
|
||||||
setup_logging()
|
setup_logging()
|
||||||
|
# connect to minio
|
||||||
|
minio_client = connect()
|
||||||
# instantiate model
|
# instantiate model
|
||||||
model = VisualCommunicationModel().to(DEVICE)
|
model = VisualCommunicationModel()
|
||||||
|
# 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)
|
||||||
# for param in model.parameters():
|
print('loaded model')
|
||||||
# logging.info(param.data)
|
|
||||||
# save model to buffer
|
|
||||||
buffer = BytesIO()
|
|
||||||
torch.save(model.state_dict(), buffer)
|
|
||||||
print(f"buffer size: {len(buffer.getvalue())}")
|
|
||||||
# calculate buffer hash
|
|
||||||
hash_str = md5(buffer.getbuffer()).hexdigest()
|
|
||||||
print(f"hash string: {hash_str}")
|
|
||||||
# connect to minio
|
|
||||||
client = connect()
|
|
||||||
# put buffer in minio bucket
|
|
||||||
put(
|
|
||||||
client=client,
|
|
||||||
buffer=buffer,
|
|
||||||
object_name=hash_str,
|
|
||||||
)
|
|
||||||
print('saved to Minio')
|
|
||||||
|
|||||||
Reference in New Issue
Block a user