{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Image classification training\n", "\n", "1. [Introduction](#Introduction)\n", "2. [Prerequisites and Preprocessing](#Prequisites-and-Preprocessing)\n", " 1. [Permissions and environment variables](#Permissions-and-environment-variables)\n", " 2. [Prepare the data](#Prepare-the-data)\n", "3. [Fine-tuning The Image Classification Model](#Fine-tuning-the-Image-classification-model)\n", " 1. [Training parameters](#Training-parameters)\n", " 2. [Start the training](#Start-the-training)\n", "4. [Inference](#Inference)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introduction\n", "\n", "Welcome to our end-to-end example of the image classification algorithm training with image format. In this demo, we will use the Amazon sagemaker image classification algorithm in transfer learning mode to fine-tune a pre-trained model (trained on imagenet data) to learn to classify a new dataset. In particular, the pre-trained model will be fine-tuned using bear images found in the Open Images Dataset. \n", "\n", "To get started, we need to set up the environment with a few prerequisite steps, for permissions, configurations, and so on." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Prequisites and Preprocessing\n", "\n", "### Permissions and environment variables\n", "\n", "Here we set up the linkage and authentication to AWS services. There are three parts to this:\n", "\n", "* The roles used to give learning and hosting access to your data. This will automatically be obtained from the role used to start the notebook\n", "* The S3 bucket that you want to use for training and model data\n", "* The Amazon sagemaker image classification docker image which need not be changed" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%time\n", "import sagemaker\n", "from sagemaker import get_execution_role\n", "\n", "role = get_execution_role()\n", "print(role)\n", "\n", "sess = sagemaker.Session()\n", "# NOTE: S3 bucket name must begin with \"deeplens-\" for DeepLens deployment\n", "bucket='deeplens-'\n", "prefix = 'ic-bear'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import boto3\n", "\n", "AWS_REGION = 'us-east-1'\n", "s3 = boto3.resource('s3')\n", "\n", "s3_bucket = s3.Bucket(bucket)\n", "\n", "if s3_bucket.creation_date == None:\n", " # create S3 bucket because it does not exist yet\n", " print('Creating S3 bucket {}.'.format(bucket))\n", " resp = s3.create_bucket(\n", " ACL='private',\n", " Bucket=bucket\n", " )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sagemaker.amazon.amazon_estimator import get_image_uri\n", "\n", "training_image = get_image_uri(sess.boto_region_name, 'image-classification', repo_version=\"latest\")\n", "print (training_image)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Download the data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!python download-images.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Prepare data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!python im2rec.py --list --recursive ./bear_train ./data/train/" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!python im2rec.py --list --recursive ./bear_val ./data/val/" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A .lst file is a tab-separated file with three columns that contains a list of image files. The first column specifies the image index, the second column specifies the class label index for the image, and the third column specifies the relative path of the image file. The image index in the first column should be unique across all of the images. Here we make an image list file using the [im2rec](https://github.com/apache/incubator-mxnet/blob/master/tools/im2rec.py) tool from MXNet. You can also create the .lst file in your own way. An example of .lst file is shown as follows. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!head -n 3 ./bear_val.lst > example.lst\n", "f = open('example.lst','r')\n", "lst_content = f.read()\n", "print(lst_content)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When you are bringing your own image files to train, please ensure that the .lst file follows the same format as described above. In order to train with the lst format interface, passing the lst file for both training and validation in the appropriate format is mandatory. Once we have the data available in the correct format for training, the next step is to upload the image and .lst file to S3 bucket." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Four channels: train, validation, train_lst, and validation_lst\n", "s3train = 's3://{}/{}/train/'.format(bucket, prefix)\n", "s3validation = 's3://{}/{}/validation/'.format(bucket, prefix)\n", "s3train_lst = 's3://{}/{}/train_lst/'.format(bucket, prefix)\n", "s3validation_lst = 's3://{}/{}/validation_lst/'.format(bucket, prefix)\n", "\n", "# upload the image files to train and validation channels\n", "!aws s3 cp ./data/train $s3train --recursive --quiet\n", "!aws s3 cp ./data/val $s3validation --recursive --quiet\n", "\n", "# upload the lst files to train_lst and validation_lst channels\n", "!aws s3 cp bear_train.lst $s3train_lst --quiet\n", "!aws s3 cp bear_val.lst $s3validation_lst --quiet" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we have all the data stored in S3 bucket. The image and lst files will be converted to RecordIO file internelly by the image classification algorithm. But if you want do the conversion, the following cell shows how to do it using the [im2rec](https://github.com/apache/incubator-mxnet/blob/master/tools/im2rec.py) tool. Note that this is just an example of creating RecordIO files. We are **_not_** using them for training in this notebook. More details on creating RecordIO files can be found in this [tutorial](https://mxnet.incubator.apache.org/how_to/recordio.html?highlight=im2rec)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#%%bash\n", "#python im2rec.py --resize 224 --quality 90 --num-thread 16 bear_val ../data/val/\n", "#python im2rec.py --resize 224 --quality 90 --num-thread 16 bear_train ../data/train/" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After you created the RecordIO files, you can upload them to the train and validation channels for training. To train with RecordIO format, you can follow \"[Image-classification-fulltraining.ipynb](https://github.com/awslabs/amazon-sagemaker-examples/blob/master/introduction_to_amazon_algorithms/imageclassification_caltech/Image-classification-fulltraining.ipynb)\" and \"[Image-classification-transfer-learning.ipynb](https://github.com/awslabs/amazon-sagemaker-examples/blob/master/introduction_to_amazon_algorithms/imageclassification_caltech/Image-classification-transfer-learning.ipynb)\". Again, we will **_not_** use the RecordIO file for the training. The following sections will only show you how to train a model with images and list files." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before training the model, we need to setup the training parameters. The next section will explain the parameters in detail." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fine-tuning the Image Classification Model\n", "Now that we are done with all the setup that is needed, we are ready to train our object detector. To begin, let us create a ``sageMaker.estimator.Estimator`` object. This estimator will launch the training job.\n", "### Training parameters\n", "There are two kinds of parameters that need to be set for training. The first one are the parameters for the training job. These include:\n", "\n", "* **Training instance count**: This is the number of instances on which to run the training. When the number of instances is greater than one, then the image classification algorithm will run in distributed settings. \n", "* **Training instance type**: This indicates the type of machine on which to run the training. Typically, we use GPU instances for these training \n", "* **Output path**: This the s3 folder in which the training output is stored" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s3_output_location = 's3://{}/{}/output'.format(bucket, prefix)\n", "ic = sagemaker.estimator.Estimator(training_image,\n", " role, \n", " train_instance_count=1, \n", " train_instance_type='ml.p2.xlarge',\n", " train_volume_size = 50,\n", " train_max_run = 360000,\n", " input_mode= 'File',\n", " output_path=s3_output_location,\n", " sagemaker_session=sess)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Apart from the above set of parameters, there are hyperparameters that are specific to the algorithm. These are:\n", "\n", "* **num_layers**: The number of layers (depth) for the network. We use 18 in this samples but other values such as 50, 152 can be used.\n", "* **use_pretrained_model**: Set to 1 to use pretrained model for transfer learning.\n", "* **image_shape**: The input image dimensions,'num_channels, height, width', for the network. It should be no larger than the actual image size. The number of channels should be same as the actual image.\n", "* **num_classes**: This is the number of output classes for the new dataset. Imagenet was trained with 1000 output classes but the number of output classes can be changed for fine-tuning. For caltech, we use 257 because it has 256 object categories + 1 clutter class.\n", "* **num_training_samples**: This is the total number of training samples. It is set to 15240 for caltech dataset with the current split.\n", "* **mini_batch_size**: The number of training samples used for each mini batch. In distributed training, the number of training samples used per batch will be N * mini_batch_size where N is the number of hosts on which training is run.\n", "* **epochs**: Number of training epochs.\n", "* **learning_rate**: Learning rate for training.\n", "* **top_k**: Report the top-k accuracy during training.\n", "* **resize**: Resize the image before using it for training. The images are resized so that the shortest side is of this parameter. If the parameter is not set, then the training data is used as such without resizing.\n", "* **precision_dtype**: Training datatype precision (default: float32). If set to 'float16', the training will be done in mixed_precision mode and will be faster than float32 mode\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "isConfigCell": true }, "outputs": [], "source": [ " ic.set_hyperparameters(num_layers=18,\n", " use_pretrained_model=1,\n", " image_shape = \"3,224,224\",\n", " num_classes=3,\n", " mini_batch_size=32,\n", " epochs=10,\n", " learning_rate=0.01,\n", " top_k=1,\n", " num_training_samples=1820,\n", " precision_dtype='float32')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Input data specification\n", "Set the data type and channels used for training" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "train_data = sagemaker.session.s3_input(s3train, distribution='FullyReplicated', \n", " content_type='application/x-image', s3_data_type='S3Prefix')\n", "validation_data = sagemaker.session.s3_input(s3validation, distribution='FullyReplicated', \n", " content_type='application/x-image', s3_data_type='S3Prefix')\n", "train_data_lst = sagemaker.session.s3_input(s3train_lst, distribution='FullyReplicated', \n", " content_type='application/x-image', s3_data_type='S3Prefix')\n", "validation_data_lst = sagemaker.session.s3_input(s3validation_lst, distribution='FullyReplicated', \n", " content_type='application/x-image', s3_data_type='S3Prefix')\n", "\n", "data_channels = {'train': train_data, 'validation': validation_data, \n", " 'train_lst': train_data_lst, 'validation_lst': validation_data_lst}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Start the training\n", "Start training by calling the fit method in the estimator" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "ic.fit(inputs=data_channels, logs=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Congrats!\n", "\n", "You've just finished training your custom image classification model!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Inference\n", "\n", "***\n", "\n", "A trained model does nothing on its own. We now want to use the model to perform inference. For this example, that means predicting the topic mixture representing a given document. You can deploy the created model by using the deploy method in the estimator" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ic_classifier = ic.deploy(initial_instance_count = 1,\n", " instance_type = 'ml.m4.xlarge')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import json\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def test_image(filename):\n", " with open(file_name, 'rb') as f:\n", " payload = f.read()\n", " payload = bytearray(payload)\n", " \n", " ic_classifier.content_type = 'application/x-image'\n", " result = json.loads(ic_classifier.predict(payload))\n", " # the result will output the probabilities for all classes\n", " # find the class with maximum probability and print the class index\n", " index = np.argmax(result)\n", " object_categories = ['brown','no','polar']\n", " print(\"Result: label - \" + object_categories[index] + \", probability - \" + str(result[index]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Download test image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!wget -O /tmp/test.jpg https://19mvmv3yn2qc2bdb912o1t2n-wpengine.netdna-ssl.com/science/files/2013/12/tnc_17745326_preview-1260x708.jpg\n", "file_name = '/tmp/test.jpg'\n", "test_image(file_name)\n", "from IPython.display import Image\n", "Image(file_name)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!wget -O /tmp/test_2.jpg https://www.nps.gov/lacl/learn/nature/images/Image-w-cred-cap_-1200w-_-Brown-Bear-page_-brown-bear-in-fog_2.jpg\n", "\n", "file_name = '/tmp/test_2.jpg'\n", "test_image(file_name)\n", "from IPython.display import Image\n", "Image(file_name)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!wget -O /tmp/test_3.jpg https://www.dollargeneral.com/media/catalog/product/cache/image/beff4985b56e3afdbeabfc89641a4582/p/l/plush_teddy-bear_giant_092018.jpg\n", "file_name = '/tmp/test_3.jpg'\n", "test_image(file_name)\n", "from IPython.display import Image\n", "Image(file_name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Clean up\n", "\n", "When we're done with the endpoint, we can just delete it and the backing instances will be released." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ic_classifier.delete_endpoint()" ] } ], "metadata": { "kernelspec": { "display_name": "conda_mxnet_p36", "language": "python", "name": "conda_mxnet_p36" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" }, "notice": "Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the \"License\"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/ or in the \"license\" file accompanying this file. This file is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License." }, "nbformat": 4, "nbformat_minor": 2 }