AWS DeepRacer - Training a Model
Tags: aws, deepracer, beginners, tutorial
Published: 2020-06-20 05:36:18

---
title: AWS DeepRacer - Training a Model
published: true
description: Getting started with AWS DeepRacer can be daunting, but in reality is a really simple process. In this guide we go through the steps needed to create your very own DeepRacer model
canonical_url: https://devopstar.com/bricks/aws-deepracer-training-a-model
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/d7gr1mrrm7qgp66qkq0v.jpg
tags: aws, deepracer, beginner, tutorial
---

Welcome to the **AWS DeepRacer - Training a Model** brick. In this guild we'll go through setting training your very own DeepRacer model from scratch. You don't need to be an expert either!

### Prerequisites

An [AWS Account](https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/) is all you should need for this tutorial.

> **Note**: There will be some costs associated with training. These costs are very minimal (less then $2.00) though, and will **likely** be covered under the Free-tier if you have a new account.

### Setting up your Account

The very first thing I recommend doing is to navigate to the [AWS DeepRacer console](https://console.aws.amazon.com/deepracer/home?region=us-east-1) in the `us-east1` region and click on **Get started** under **Reinforcement learning**.

![AWS DeepRacer get started](https://dev-to-uploads.s3.amazonaws.com/i/3q4u3uemnrsff58yj0qw.png)

We will now work down the list of requirements that will help us train a new model. **Step 0** is have AWS reset/create some background resources that will be needed to allow AWS DeepRacer to function.

If this is not the first time working with DeepRacer, you might have ❌'s next to the resources. If this is the case then click **Reset resources**.

![AWS DeepRacer Reset resources](https://dev-to-uploads.s3.amazonaws.com/i/efs7625zldtf7otuaauk.png)

Once the resources have been reset, you will be able to click **Create resources** next which should eventually turn to ✅'s when completed

![AWS DeepRacer Create resources](https://dev-to-uploads.s3.amazonaws.com/i/7l1n9td2cea3t66hoegh.png)

> **Note**: *This process can take up to 5 minutes to complete*

### Create a model

We're up to the fun bit in the tutorial where we can train our very first model. To get started, click **Create model** under the **Get started** section.

![AWS DeepRacer Create model](https://dev-to-uploads.s3.amazonaws.com/i/ioojel1t12xpxlqq0vpt.png)

Start by giving your new model a name and description. The name of the model must be unique to your account.

![AWS DeepRacer name and describe model](https://dev-to-uploads.s3.amazonaws.com/i/76a1t1m126d57anz8pcf.png)

Under environment simulation, select **The 2019 DeepRacer Championship Cup**

> **Note**: *If you have a reason to train your model for any of the other tracks, definitely select the one that seems most appropriate for you.*

![AWS DeepRacer environment simulation](https://dev-to-uploads.s3.amazonaws.com/i/w7g648sltlbsfj6aoid3.png)

Click **Next** at the bottom of the page to move onto *Step 2*.

In this step we now need to define what kind of race type we would like to train for. Given we're training for just a normal timed race, select **Time trail**.

![AWS DeepRacer race type](https://dev-to-uploads.s3.amazonaws.com/i/t3xxjjkbaa7ku3xnhqw8.png)

Selecting your **Agent** (Car type) is next, and this is where the guide can start to become more interesting if you want it to be. By default you can choose to train with *The Original DeepRacer* agent which has the following specs

![AWS DeepRacer default vehicle](https://dev-to-uploads.s3.amazonaws.com/i/etselfi6h620pne2rv0j.png)

If you are interested in creating a brand new agent (vehicle) from scratch, then move through the next section **Creating an Agent**. Otherwise, feel free to select *The Original DeepRacer* and skip to [**Reward function**](#Reward-function).

#### Creating an Agent [Optional]

We're going to move away from the guide AWS provides us for a second to create our very own **Agent**. An agent in the context of DeepRacer is the car that drives around the track.

Navigate to the [**Your garage**](https://console.aws.amazon.com/deepracer/home?region=us-east-1#garage) menu under **Reinforcement learning** and you should be able to see the *The Original DeepRacer* agent that is created for you.

![AWS DeepRacer default agent](https://dev-to-uploads.s3.amazonaws.com/i/s0pww8tcfaledr4y78om.png)

This default agent has all the default settings applied, and only uses one camera as its input for information. To understand what I mean by **inputs**, lets walk through creating a new vehicle by clicking **Build new vehicle***.

> **Note**: *It is best to understand what kind of track you will be racing on before you create your agent. This is because the track type will heavily influence what types of input data might be considered important when training your model.*

You will be presented with a number of options for modifying your agent. Since we picked **The 2019 DeepRacer Championship Cup** track, lets use it as a foundation for deciding how to build our vehicle.

![AWS DeepRacer championship cup track](https://dev-to-uploads.s3.amazonaws.com/i/4x87ku6hhraj08ti3fyu.png)

The first part of designing our vehicle is to select the modifications. We have options for a variety of different camera setups and even the addition of LIDAR.

![AWS DeepRacer mod specifications](https://dev-to-uploads.s3.amazonaws.com/i/okjg3fkv15xplrnuxjeo.png)

To keep things simple for now though we're going to stick with the **single Camera** setup.

> *Having more information from two cameras can be useful, but only if you write a fantastic reward function to use that data in a meaningful way.*

For the **Action space** we're able to select a variety of different steering & speed optimizations. These variables give the vehicle more or less control over how far it is able to turn, and the speed in which it can move.

Lets breakdown the information we have into some assumptions:

* Taking a look at the type of track we are racing on, note that there **aren't any hairpin turns** or anything that could benefit from having a high degree for steering angle.
* **Long stretches of track** that could benefit from a higher then normal top speed.
* **8 opportunities for turns** and none of them are particularly wide.

> **Note**: *The action list is a set of actions the vehicle can take at any point in time. Sometimes having less available actions can be more beneficial for simpler courses.*

Here is an example of my desired **Action space**. Feel free to tinker with these settings, and try something different yourself.

![AWS DeepRacer action space](https://dev-to-uploads.s3.amazonaws.com/i/gmxtbiql0aknmd910pvh.png)

Finally, click **Next** and you'll need to name your vehicle something unique, along with giving it some cool colors

![AWS DeepRacer vehicle appearance](https://dev-to-uploads.s3.amazonaws.com/i/lbcdmp2he4favrzk6u8t.png)

Click done when you are happy with the car and then head back to the **Create model** part of the guide. Select your brand new Agent from the list and hit **Next**.

![AWS DeepRacer select agent](https://dev-to-uploads.s3.amazonaws.com/i/e73eaof0hjvdykaz70li.png)

### Reward function

The Reward function is the core of your model; It makes the decisions about what actions to take and when based on a set of (potentially complex) parameters. By default your reward function will look something like the following

```python
def reward_function(params):
    '''
    Example of rewarding the agent to follow center line
    '''
    
    # Read input parameters
    track_width = params['track_width']
    distance_from_center = params['distance_from_center']
    
    # Calculate 3 markers that are at varying distances away from the center line
    marker_1 = 0.1 * track_width
    marker_2 = 0.25 * track_width
    marker_3 = 0.5 * track_width
    
    # Give higher reward if the car is closer to center line and vice versa
    if distance_from_center <= marker_1:
        reward = 1.0
    elif distance_from_center <= marker_2:
        reward = 0.5
    elif distance_from_center <= marker_3:
        reward = 0.1
    else:
        reward = 1e-3  # likely crashed/ close to off track
    
    return float(reward)
```

This is the simplest way to approach training a model, and for beginners I would recommend just training with it before trying to dive in too deep too fast.

> **Note**: *Reward functions & Hyperparameter tweaking is what makes any model GREAT. Once you have a good understanding of how things work, I recommend exploring these advanced settings in more detail. Watch out for future Bricks/Blog posts on these topics!*

When you are ready to begin training, click **Create model**. The training process will take 60 minutes by default and you can watch how progression is going in the Simulation video stream.

![AWS DeepRacer training process](https://dev-to-uploads.s3.amazonaws.com/i/wna4u9gvbbk05pw0da7l.png)

### Model evaluation

Once the training process is finished you will see a ✅ indicating its completion. We are now able to run an evaluation by clicking **Start evaluation** under the model.

![AWS DeepRacer model evaluation](https://dev-to-uploads.s3.amazonaws.com/i/el779zoi2p3q3lxars75.png)

When prompted for the **Evaluate criteria**, select **The 2019 DeepRacer Championship Cup**.

![AWS DeepRacer evaluate criteria](https://dev-to-uploads.s3.amazonaws.com/i/ltnw122m9oczz3794tqj.png)

Under **Race type** select **Time trial**

![AWS DeepRacer race type](https://dev-to-uploads.s3.amazonaws.com/i/6if6ckfk4o8lwnqwkf29.png)

For **Virtual race submission**, you can opt into competing in the 2020 June Qualifier Time trial. For this example however we will be opting out.

![AWS DeepRacer virtual race submission](https://dev-to-uploads.s3.amazonaws.com/i/hciidahlr3gznjn6fzig.png)

Click **Start evaluation** to kick off the ~4 minute job. While it is running, you'll be able to preview how your model is performing in the simulation video stream. This view also includes your lap results.

![AWS DeepRacer training evaluation](https://dev-to-uploads.s3.amazonaws.com/i/5bms47lcz78br2gfjqgy.png)

### Summary

Congratulations on training your very first DeepRacer model! You can now start to build on your model and make adjustments. Once you have a winning model, you can even submit them to free community events and win prizes.

If you had any issues setting things up, or you have other questions, please let me know by reaching out on Twitter [@nathangloverAUS](https://twitter.com/nathangloverAUS) or dropping a comment below.