Repository navigation
Description
Describe the bug
When working with sagemaker's inference pipeline in building a custom transformer for data processing and feature engineering, I am unable to read files in s3 with pandas(pd.read_csv). Using the SKLearn estimator, providing a custom scikit-learn script as the entry point the following error was raised during training :
ImportError: Missing optional dependency 's3fs'. The s3fs package is required to handle s3 files. Use pip or conda to install s3fs.
I checked the conda environment I was working on, s3fs is installed.
When I try reading the csv file from s3 using pandas(pd.read_csv) in the sagemaker's notebook instance, it works.
custom scikit-learn script
`import sys
from io import StringIO
import os
import argparse
import csv
import json
import numpy as np
import pandas as pd
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.pipeline import Pipeline
from sklearn.externals import joblib
from sagemaker_containers.beta.framework import (
content_types, encoders, env, modules, transformer, worker)
#custom transformers
...
s3_train_dir = "s3://krypton-data/ml-data/train/train.csv"
s3_test_dir = "s3://krypton-data/ml-data/test/test.csv"
s3_model_dir = "s3://krypton-data/model/"
s3_output_dir = "s3://krypton-data/output/"
if name == "main":
parser = argparse.ArgumentParser()
# Sagemaker specific arguments. Defaults are set in the environment variables.
parser.add_argument('--output-data-dir', type=str, default=s3_output_dir)
parser.add_argument('--model-dir', type=str, default=s3_model_dir)
parser.add_argument('--train', type=str, default=s3_train_dir)
args = parser.parse_args()
raw_data = pd.read_csv(args.train)
raw_data_X = raw_data.iloc[:, :-1]
raw_data_y = raw_data[[target]]
print('fitting transformers....')
house_price_preprocessing_pipeline.fit(raw_data_X, raw_data_y)
joblib.dump(house_price_preprocessing_pipeline,
os.path.join(args.model_dir, "model.joblib"))
print("Saving model....")
def input_fn(input_data, content_type):
if content_type == 'text/csv':
raw_data = pd.read_csv(input_dataV)
return raw_data
else:
raise ValueError('This script only takes csv')
def output_fn(prediction, accept):
if accept == "application/json":
instances = []
for row in prediction.tolist():
instances.append({"features": row})
json_output = {"instances": instances}
return worker.Response(json.dumps(json_output), accept, mimetype=accept)
elif accept == 'text/csv':
return worker.Response(encoders.encode(prediction, accept), accept, mimetype=accept)
else:
raise RuntimeException("{} accept type is not supported by this script.".format(accept))
def predict_fn(input_data, model):
features = model.transform(input_data)
return features
def model_fn(model_dir):
preprocessor = joblib.load(os.path.join(model_dir, "model.joblib"))
return preprocessor`
System information
- SageMaker Python SDK version:1.55.3
- Pandas version: 0.24.0
- S3FS version: 0.15
- Python version: 3.6.5
- CPU or GPU: CPU
Activity
edwardjkim commentedon May 15, 2020
Hi @theKoladeAkande,
s3fsis an optional dependency and it's not installed by default in the scikit-learn container because the recommended way of using the scikit-learn container with Python SDK is to use theEstimator.fit()method.From your example, it looks like you have two S3 paths
trainandtest, so in the call to the estimator'sfit()method, you would want to use two input channels namedtrainandtest:Once you pass the S3 paths to the channels in
fit(), the following environment variables are set in the container, following the formatSM_CHANNEL_[channel_name]:SM_CHANNEL_TRAIN: A string representing the path to the directory containing data in thetrainchannelSM_CHANNEL_TEST: Same as above, but for thetestchannel.Now the training script can load data from the input channels, so you may want to pass these to your script as arguments:
Then you can use pandas as follows:
By using channels, you can control the S3 paths from the SDK and avoid hard-coding the paths in the script. There are some example notebooks that demonstrate this approach, e.g., this notebook. Please let us know if you still having issues.
theKoladeAkande commentedon May 15, 2020
Hi @edwardjkim
it worked!
Thank you for the help, I wasn't building the pipeline with the dependencies of the container in mind. I was building based on the dependencies of the environment I was working with. Is the documentation clear on this? a lot of assumption seems to be made about the reader in sagemaker's documentations.
edwardjkim commentedon May 15, 2020
The SageMaker Python SDK has documentation here. For me personally, I find the example notebooks most useful when exploring Python SDK and different algorithms. You can also access the notebooks from the "SageMaker examples" tab in the notebook instances on SageMaker.
theKoladeAkande commentedon May 15, 2020
Thanks @edwardjkim, I would close this issue now.
Bug fixes (aws#1496)
feature: model optimization (#4775)
feature: model optimization (aws#4775)