You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在AWS SageMaker中通过Dockerfile运行Python文件及部署预训练模型

Got it, let's walk through how to get your existing Python script and pre-trained model.pkl working smoothly on AWS SageMaker. There are two main paths depending on what you need: running your script as a batch job, or deploying it as a real-time inference endpoint. Let's break both down:

1. Run Your Script as a Batch Processing Job

If you just need to execute your script one-time (or on a schedule) without exposing a live endpoint, SageMaker Processing Jobs are perfect for this. Here's how to set it up:

  • Step 1: Upload your files to S3
    First, upload your filename.py and model.pkl to an S3 bucket. For example, put them in s3://your-bucket-name/your-project-files/.

  • Step 2: Use SageMaker Python SDK to launch a Processing Job
    You'll need a small script to define and start the job. This tells SageMaker to spin up an instance, pull your files from S3, and run your filename.py command.

    Example code:

    import sagemaker
    from sagemaker.processing import ScriptProcessor, ProcessingInput, ProcessingOutput
    
    # Initialize SageMaker session and get your execution role
    sagemaker_session = sagemaker.Session()
    role = sagemaker.get_execution_role()
    
    # Use an official Python 3 container (adjust the URI for your region if needed)
    script_processor = ScriptProcessor(
        command=['python3'],
        image_uri='763104351884.dkr.ecr.us-east-1.amazonaws.com/mxnet-inference:1.8.0-py3',
        role=role,
        instance_count=1,
        instance_type='ml.t2.medium' # Pick an instance size that fits your needs
    )
    
    # Launch the job
    script_processor.run(
        code='filename.py', # Path to your script in S3 or local (SDK uploads local files automatically)
        inputs=[
            ProcessingInput(
                source='s3://your-bucket-name/your-project-files/model.pkl',
                destination='/opt/ml/processing/input/model'
            )
        ],
        outputs=[
            ProcessingOutput(
                source='/opt/ml/processing/output',
                destination='s3://your-bucket-name/your-project-output/'
            )
        ],
        # Pass arguments to your script if you need to specify the model path
        arguments=['--model-path', '/opt/ml/processing/input/model/model.pkl']
    )
    
  • Step 3: Update your filename.py
    Modify your script to load the model from the path provided by the Processing Job (either the hardcoded /opt/ml/processing/input/model/model.pkl or the command-line argument you passed).

2. Deploy as a Real-Time Inference Endpoint

If you need to serve predictions on-demand (like an API), you'll need to adapt your code to follow SageMaker's inference specifications. Here's how:

  • Step 1: Create an inference script
    SageMaker requires a script with specific functions to handle loading the model, processing inputs, running predictions, and formatting outputs. Create a file named inference.py with these functions:

    import pickle
    import os
    import json
    
    def model_fn(model_dir):
        # SageMaker extracts your model.tar.gz to this directory
        model_path = os.path.join(model_dir, 'model.pkl')
        with open(model_path, 'rb') as f:
            model = pickle.load(f)
        return model
    
    def input_fn(request_body, request_content_type):
        # Parse incoming requests (adjust based on your input format)
        if request_content_type == 'application/json':
            return json.loads(request_body)
        raise ValueError(f"Unsupported content type: {request_content_type}")
    
    def predict_fn(input_data, model):
        # Run prediction with your model
        return model.predict(input_data)
    
    def output_fn(prediction, response_content_type):
        # Format the prediction output
        if response_content_type == 'application/json':
            return json.dumps(prediction.tolist() if hasattr(prediction, 'tolist') else prediction)
        raise ValueError(f"Unsupported content type: {response_content_type}")
    
  • Step 2: Package your model and script
    Bundle inference.py and model.pkl into a model.tar.gz file:

    tar -czvf model.tar.gz inference.py model.pkl
    
  • Step 3: Upload the package to S3
    Upload model.tar.gz to your S3 bucket, e.g., s3://your-bucket-name/your-model-artifacts/model.tar.gz.

  • Step 4: Deploy the endpoint
    Use the SageMaker SDK to create a model and deploy it as an endpoint:

    import sagemaker
    from sagemaker.model import Model
    from sagemaker.predictor import Predictor
    
    sagemaker_session = sagemaker.Session()
    role = sagemaker.get_execution_role()
    
    # Define your model using the packaged artifacts and a Python inference container
    model = Model(
        model_data='s3://your-bucket-name/your-model-artifacts/model.tar.gz',
        image_uri='763104351884.dkr.ecr.us-east-1.amazonaws.com/mxnet-inference:1.8.0-py3',
        role=role,
        predictor_cls=Predictor
    )
    
    # Deploy the endpoint
    predictor = model.deploy(
        initial_instance_count=1,
        instance_type='ml.t2.medium'
    )
    
    # Test the endpoint (adjust input to match your model's expected format)
    test_input = json.dumps([[1.2, 3.4, 5.6, 7.8]])
    response = predictor.predict(test_input, initial_args={'ContentType': 'application/json'})
    print("Prediction:", response)
    
Key Notes to Remember
  • Dependencies: If your script uses libraries like pandas or scikit-learn, either use a custom Docker container with these pre-installed, or include a requirements.txt file in your model.tar.gz (some SageMaker containers will automatically install dependencies from this file).
  • IAM Permissions: Ensure your SageMaker execution role has permissions to read from and write to your S3 bucket (add AmazonS3FullAccess or more granular policies if needed).
  • Local Testing: Use SageMaker's Local Mode to test your setup locally before deploying to the cloud—it saves time and avoids unnecessary costs.

内容的提问来源于stack exchange,提问作者tarun mittal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:15:53