Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8b1893b30a | ||
|
|
96d868a40c | ||
|
|
e698d058ff | ||
|
|
8aafbdae6c |
+1
-1
@@ -44,7 +44,7 @@ COPY --from=builder ${VIRTUAL_ENV} ${VIRTUAL_ENV}
|
|||||||
ENV PATH="${VIRTUAL_ENV}/bin:${PATH}"
|
ENV PATH="${VIRTUAL_ENV}/bin:${PATH}"
|
||||||
|
|
||||||
# create home directory and app user
|
# create home directory and app user
|
||||||
RUN mkdir -p /home/app
|
RUN mkdir -p $APP_HOME
|
||||||
|
|
||||||
# add code while changing ownership
|
# add code while changing ownership
|
||||||
WORKDIR $APP_HOME
|
WORKDIR $APP_HOME
|
||||||
|
|||||||
+3
-20
@@ -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')
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
from .get_model_name import get_model_name
|
from .load_model import load_model
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
"""Definition of load_model function."""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import torch
|
||||||
|
from minio import Minio
|
||||||
|
from models import VisualCommunicationModel
|
||||||
|
|
||||||
|
from shared.data_store import get_model
|
||||||
|
|
||||||
|
from .get_model_name import get_model_name
|
||||||
|
|
||||||
|
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
||||||
|
|
||||||
|
|
||||||
|
def load_model(
|
||||||
|
client: Minio,
|
||||||
|
) -> VisualCommunicationModel:
|
||||||
|
"""Instantiate model with weights loaded from latest model saved in
|
||||||
|
MinIO."""
|
||||||
|
assert isinstance(client, Minio)
|
||||||
|
# instantiate model
|
||||||
|
model = VisualCommunicationModel()
|
||||||
|
# get model object name
|
||||||
|
model_name_path = Path('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=client,
|
||||||
|
object_name=model_object_name,
|
||||||
|
)
|
||||||
|
model.load_state_dict(model_checkpoint)
|
||||||
|
# clean memory
|
||||||
|
model_checkpoint.clear()
|
||||||
|
# move model to selected device
|
||||||
|
model = model.to(DEVICE)
|
||||||
|
logging.debug('finished')
|
||||||
|
return model
|
||||||
@@ -23,7 +23,6 @@ def get_model(
|
|||||||
client=client,
|
client=client,
|
||||||
object_name=object_name,
|
object_name=object_name,
|
||||||
)
|
)
|
||||||
logging.debug('buffer size: %s', buffer.getbuffer().nbytes)
|
|
||||||
# convert data to model checkpoint
|
# convert data to model checkpoint
|
||||||
buffer.seek(0)
|
buffer.seek(0)
|
||||||
model_content = torch.load(buffer)
|
model_content = torch.load(buffer)
|
||||||
|
|||||||
Reference in New Issue
Block a user