aws_lambda/pytorch-serverless/aws/pytorch/prediction.py
def load_model():
    """Loads the PyTorch model and the classes into memory from a tar.gz file on S3."""

    tmp_dir = '/tmp/pytorch-serverless'
    local_model = f'{tmp_dir}/model.tar.gz'
    os.makedirs(tmp_dir, exist_ok=True)
    logger.info(f'Loading {MODEL} from S3 bucket {S3_BUCKET} to {local_model}')
    s3.download_file(S3_BUCKET, MODEL, local_model)
    tarfile.open(local_model).extractall(tmp_dir)
    os.remove(local_model)
    classes = open(f'{tmp_dir}/classes', 'r').read().splitlines()
    logger.info(f'Classes are {classes}')    
    model_path = glob.glob(f'{tmp_dir}/*_jit.pth')[0]
    logger.info(f'Model path is {model_path}')
    model = torch.jit.load(model_path, map_location=torch.device('cpu'))
    return model.eval(), classes
Loads the PyTorch model and the classes into memory from a tar.gz file on S3.
Loads the PyTorch model and the classes into memory from a tar.gz file on S3.
Loads the PyTorch model and the classes into memory from a tar . gz file on S3 .
def load_model():
    tmp_dir = '/tmp/pytorch-serverless'
    local_model = f'{tmp_dir}/model.tar.gz'
    os.makedirs(tmp_dir, exist_ok=True)
    logger.info(f'Loading {MODEL} from S3 bucket {S3_BUCKET} to {local_model}')
    s3.download_file(S3_BUCKET, MODEL, local_model)
    tarfile.open(local_model).extractall(tmp_dir)
    os.remove(local_model)
    classes = open(f'{tmp_dir}/classes', 'r').read().splitlines()
    logger.info(f'Classes are {classes}')    
    model_path = glob.glob(f'{tmp_dir}/*_jit.pth')[0]
    logger.info(f'Model path is {model_path}')
    model = torch.jit.load(model_path, map_location=torch.device('cpu'))
    return model.eval(), classes
def load_model ( ) : tmp_dir = '/tmp/pytorch-serverless' local_model = f'{tmp_dir}/model.tar.gz' os . makedirs ( tmp_dir , exist_ok = True ) logger . info ( f'Loading {MODEL} from S3 bucket {S3_BUCKET} to {local_model}' ) s3 . download_file ( S3_BUCKET , MODEL , local_model ) tarfile . open ( local_model ) . extractall ( tmp_dir ) os . remove ( local_model ) classes = open ( f'{tmp_dir}/classes' , 'r' ) . read ( ) . splitlines ( ) logger . info ( f'Classes are {classes}' ) model_path = glob . glob ( f'{tmp_dir}/*_jit.pth' ) [ 0 ] logger . info ( f'Model path is {model_path}' ) model = torch . jit . load ( model_path , map_location = torch . device ( 'cpu' ) ) return model . eval ( ) , classes
Loads the PyTorch model and the classes into memory from a tar.gz file on S3.
Loads the PyTorch model and the classes into memory from a tar . gz file on S3 .
"""Loads the PyTorch model and the classes into memory from a tar.gz file on S3."""