Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/javierluraschi/kartmodels


https://github.com/javierluraschi/kartmodels

Last synced: about 1 month ago
JSON representation

Awesome Lists containing this project

README

        

---
title: "kartmodels: Kart simulation training models"
output:
github_document:
fig_width: 7
fig_height: 5
---

This project makes use of `kartsim` to explore multiple training models.

## Capturing Data

We will use the `kartsim` package and `kartsim_capture()` to capture training and
test data to train the models in this repo.

1. Capture training data using:

```{r eval=FALSE}
library(kartsim)
kartsim_capture("capture/train", circuit = 3)
```

2. Followed by test data with:

```{r eval=FALSE}
kartsim_capture("capture/test", circuit = 3)
```

## Training using the CIFAR model

We will first try modeling this as an image classification problem using the
[cifar10_cnn](https://tensorflow.rstudio.com/keras/articles/examples/cifar10_cnn.html)
Keras example.

```{r eval=FALSE}
tfruns::training_run("models/tf-cifar.R")
```

or in `cloudml` runnning:

```{r eval=FALSE}
cloudml::cloudml_train("models/tf-cifar.R")
```

Next we will preload the model and predict over a `graph` object as follows:

```{r eval=F}
sess <- tensorflow::tf$Session()
graph <- tfdeploy::load_savedmodel(sess, "savedmodel")

tfdeploy::predict_savedmodel(list(array(0, c(32,32,3))), graph, type = "graph", sess = sess)
```
```
user system elapsed
0.027 0.001 0.025
```

Which we can use to control the kart based on this model:

```{r eval=F}
kartsim::kartsim_control(function(image, direction) {
labels <- c("left", "forward", "right")
input <- array(png::readPNG(image), c(32,32,3))
result <- tfdeploy::predict_savedmodel(input, graph, type = "graph", sess = sess)
scores <- result$predictions[[1]]$activation

labels[which(scores == max(scores))][[1]]
}, circuit = 3)
```

```
517/517 [==============================] - 54s 105ms/step - loss: 6.4093 - acc: 0.6013 - val_loss: 5.4995 - val_acc: 0.6588
Epoch 2/2
517/517 [==============================] - 55s 107ms/step - loss: 6.3691 - acc: 0.6048 - val_loss: 5.5059 - val_acc: 0.6577
```

## Improving the CIFAR model with context

In order to improve accuracy, we can consider making the model "remember" the
state of the previous direction to help give continuity while steering.

```{r eval=FALSE}
tfruns::training_run("models/tf-cifar-prev.R")
```

```{r eval=F}
library(tensorflow)
sess <- tensorflow::tf$Session()
graph <- tfdeploy::load_savedmodel(sess, "savedmodel/")

tfdeploy::predict_savedmodel(
list(
list(
conv2d_1_input = array(0, c(32,32,3)),
previous = 0
)
),
graph,
type = "graph",
sess = sess)
```

```
$predictions
activation_6
1 0.0462, 0.9226, 0.0312
```

We can apply this control policy as follows:

```{r eval=F}

previous <- 0
control_cifar_prev <- function(image, direction) {
labels <- c("left", "forward", "right")
input <- array(png::readPNG(image), c(32,32,3))

result <- tfdeploy::predict_savedmodel(
list(
list(
conv2d_1_input = input,
previous = previous
)
),
graph, type = "graph", sess = sess)

message(result)

angle <- result$predictions$activation[[1]]
previous <<- angle

angle
}

kartsim::kartsim_control(control_cifar_prev)
```