This project makes use of kartsim
to explore multiple training models.
We will use the kartsim
package and kartsim_capture()
to capture training and
test data to train the models in this repo.
library(kartsim) kartsim_capture("capture/train", circuit = 3)
kartsim_capture("capture/test", circuit = 3)
We will first try modeling this as an image classification problem using the cifar10_cnn Keras example.
tfruns::training_run("models/tf-cifar.R")
or in cloudml
runnning:
cloudml::cloudml_train("models/tf-cifar.R")
Next we will preload the model and predict over a graph
object as follows:
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:
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
In order to improve accuracy, we can consider making the model "remember" the state of the previous direction to help give continuity while steering.
tfruns::training_run("models/tf-cifar-prev.R")
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:
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.