|
| 1 | +# This is a sample Python program that trains a simple TensorFlow CIFAR-10 model. |
| 2 | +# This implementation will work on your *local computer* or in the *AWS Cloud*. |
| 3 | +# To run training and inference *locally* set: `config = get_config(LOCAL_MODE)` |
| 4 | +# To run training and inference on the *cloud* set: `config = get_config(CLOUD_MODE)` and set a valid IAM role value in get_config() |
| 5 | +# |
| 6 | +# Prerequisites: |
| 7 | +# 1. Install required Python packages: |
| 8 | +# `pip install -r requirements.txt` |
| 9 | +# 2. Docker Desktop installed and running on your computer: |
| 10 | +# `docker ps` |
| 11 | +# 3. You should have AWS credentials configured on your local machine |
| 12 | +# in order to be able to pull the docker image from ECR. |
| 13 | +############################################################################################### |
| 14 | + |
| 15 | +import os |
| 16 | + |
| 17 | +import numpy as np |
| 18 | +import pandas as pd |
| 19 | +from sklearn.datasets import * |
| 20 | +import sklearn.model_selection |
| 21 | +from sklearn.preprocessing import StandardScaler |
| 22 | +from sagemaker.tensorflow import TensorFlow |
| 23 | + |
| 24 | + |
| 25 | +DUMMY_IAM_ROLE = 'arn:aws:iam::111111111111:role/service-role/AmazonSageMaker-ExecutionRole-20200101T000001' |
| 26 | + |
| 27 | + |
| 28 | +def download_training_and_eval_data(): |
| 29 | + if os.path.isfile('./data/train/x_train.npy') and \ |
| 30 | + os.path.isfile('./data/test/x_test.npy') and \ |
| 31 | + os.path.isfile('./data/train/y_train.npy') and \ |
| 32 | + os.path.isfile('./data/test/y_test.npy'): |
| 33 | + print('Training and evaluation datasets exist. Skipping Download') |
| 34 | + else: |
| 35 | + print('Downloading training and evaluation dataset') |
| 36 | + data_dir = os.path.join(os.getcwd(), 'data') |
| 37 | + os.makedirs(data_dir, exist_ok=True) |
| 38 | + |
| 39 | + train_dir = os.path.join(os.getcwd(), 'data/train') |
| 40 | + os.makedirs(train_dir, exist_ok=True) |
| 41 | + |
| 42 | + test_dir = os.path.join(os.getcwd(), 'data/test') |
| 43 | + os.makedirs(test_dir, exist_ok=True) |
| 44 | + |
| 45 | + data_set = fetch_california_housing() |
| 46 | + |
| 47 | + X = pd.DataFrame(data_set.data, columns=data_set.feature_names) |
| 48 | + Y = pd.DataFrame(data_set.target) |
| 49 | + |
| 50 | + # We partition the dataset into 2/3 training and 1/3 test set. |
| 51 | + x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(X, Y, test_size=0.33) |
| 52 | + |
| 53 | + scaler = StandardScaler() |
| 54 | + scaler.fit(x_train) |
| 55 | + x_train = scaler.transform(x_train) |
| 56 | + x_test = scaler.transform(x_test) |
| 57 | + |
| 58 | + np.save(os.path.join(train_dir, 'x_train.npy'), x_train) |
| 59 | + np.save(os.path.join(test_dir, 'x_test.npy'), x_test) |
| 60 | + np.save(os.path.join(train_dir, 'y_train.npy'), y_train) |
| 61 | + np.save(os.path.join(test_dir, 'y_test.npy'), y_test) |
| 62 | + |
| 63 | + print('Downloading completed') |
| 64 | + |
| 65 | + |
| 66 | +def do_inference_on_local_endpoint(predictor): |
| 67 | + print(f'\nStarting Inference on endpoint (local).') |
| 68 | + |
| 69 | + x_test = np.load('./data/test/x_test.npy') |
| 70 | + y_test = np.load('./data/test/y_test.npy') |
| 71 | + |
| 72 | + results = predictor.predict(x_test[:10])['predictions'] |
| 73 | + flat_list = [float('%.1f' % (item)) for sublist in results for item in sublist] |
| 74 | + print('predictions: \t{}'.format(np.array(flat_list))) |
| 75 | + print('target values: \t{}'.format(y_test[:10].round(decimals=1))) |
| 76 | + |
| 77 | + |
| 78 | +def main(): |
| 79 | + download_training_and_eval_data() |
| 80 | + |
| 81 | + print('Starting model training.') |
| 82 | + print( |
| 83 | + 'Note: if launching for the first time in local mode, container image download might take a few minutes to complete.') |
| 84 | + california_housing_estimator = TensorFlow(entry_point='california_housing_tf2.py', |
| 85 | + source_dir='code', |
| 86 | + role=DUMMY_IAM_ROLE, |
| 87 | + instance_count=1, |
| 88 | + instance_type='local', |
| 89 | + framework_version='2.4.1', |
| 90 | + py_version='py37') |
| 91 | + |
| 92 | + inputs = {'train': 'file://./data/train', 'test': 'file://./data/test'} |
| 93 | + california_housing_estimator.fit(inputs) |
| 94 | + print('Completed model training') |
| 95 | + |
| 96 | + print('Deploying endpoint in local mode') |
| 97 | + predictor = california_housing_estimator.deploy(initial_instance_count=1, instance_type='local') |
| 98 | + |
| 99 | + do_inference_on_local_endpoint(predictor) |
| 100 | + |
| 101 | + print('About to delete the endpoint to stop paying (if in cloud mode).') |
| 102 | + predictor.delete_endpoint(predictor.endpoint_name) |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + main() |
0 commit comments