Skip to content

Commit

Permalink
Added install_autokeras function
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrodriguez1989 committed Mar 6, 2019
1 parent b2ed455 commit 4c78ee0
Show file tree
Hide file tree
Showing 21 changed files with 365 additions and 29 deletions.
5 changes: 4 additions & 1 deletion NAMESPACE
Expand Up @@ -4,18 +4,21 @@ S3method(export_autokeras_model,AutokerasModel)
S3method(export_keras_model,AutokerasModel)
S3method(final_fit,AutokerasModel)
S3method(get_keras_model,AutokerasModel)
S3method(predict,AutokerasModel)
export(evaluate.AutokerasModel)
export(export_autokeras_model)
export(export_keras_model)
export(final_fit)
export(fit.AutokerasModel)
export(get_keras_model)
export(install_autokeras)
export(model_image_classifier)
export(model_image_regressor)
export(model_tabular_classifier)
export(model_tabular_regressor)
export(model_text_classifier)
export(model_text_regressor)
export(predict.AutokerasModel)
importFrom(keras,install_keras)
importFrom(keras,load_model_hdf5)
importFrom(reticulate,import)
importFrom(reticulate,py_config)
13 changes: 9 additions & 4 deletions R/final-fit.R
Expand Up @@ -9,6 +9,7 @@
# @param trainer_args A dictionary containing the parameters of the
# ModelTrainer constructor.
#' @param retrain A boolean of whether reinitialize the weights of the model.
#' @param time_limit The time limit to fit in seconds.
#'
#' @name final_fit
NULL
Expand All @@ -22,9 +23,13 @@ final_fit <- function(object, ...) {
#' @rdname final_fit
#' @export
final_fit.AutokerasModel <- function(autokeras_model, x_train, y_train,
x_test, y_test, retrain=FALSE) {
autokeras_model@model$final_fit(x_train=x_train, y_train=y_train,
x_test=x_test, y_test=y_test,
retrain=retrain);
x_test, y_test, retrain=FALSE,
time_limit=Inf) {
setTimeLimit(elapsed=time_limit);
try({
autokeras_model@model$final_fit(x_train=x_train, y_train=y_train,
x_test=x_test, y_test=y_test,
retrain=retrain);
})
return(invisible(autokeras_model));
}
123 changes: 123 additions & 0 deletions R/install.R
@@ -0,0 +1,123 @@

#' Install Auto-Keras, Keras, and the TensorFlow backend
#'
#' Auto-Keras, Keras, and TensorFlow will be installed into an "r-tensorflow"
#' virtual or conda environment. Note that "virtualenv" is not available on
#' Windows (as this isn't supported by TensorFlow).
#'
# @inheritParams tensorflow::install_tensorflow
#' @inheritParams keras::install_keras
#'
#' @param version Version of Auto-Keras to install. Specify "default" to install
#' the latest release. Otherwise specify an alternate version (e.g. "0.3.5").
#' The default value is "0.3.7" as it is the latest tested version.
#'
#' @param keras Keras version to install. Specify "default" to install
#' the latest release. Otherwise specify an alternate version (e.g. "2.2.2").
#'
#' @section GPU Installation:
#'
#' Keras and TensorFlow can be configured to run on either CPUs or GPUs. The CPU
#' version is much easier to install and configure so is the best starting place
#' especially when you are first learning how to use Keras. Here's the guidance
#' on CPU vs. GPU versions from the TensorFlow website:
#'
#' - *TensorFlow with CPU support only*. If your system does not have a NVIDIA®
#' GPU, you must install this version. Note that this version of TensorFlow is
#' typically much easier to install, so even if you have an NVIDIA GPU, we
#' recommend installing this version first.
#'
#' - *TensorFlow with GPU support*. TensorFlow programs typically run
#' significantly faster on a GPU than on a CPU. Therefore, if your system has a
#' NVIDIA® GPU meeting all prerequisites and you need to run
#' performance-critical applications, you should ultimately install this
#' version.
#'
#' To install the GPU version:
#'
#' 1) Ensure that you have met all installation prerequisites including
#' installation of the CUDA and cuDNN libraries as described in
#' [TensorFlow GPU Prerequistes](https://tensorflow.rstudio.com/installation_gpu.html#prerequisites).
#'
#' 2) Pass `tensorflow = "gpu"` to `install_autokeras()`. For example:
#'
#' ```
#' install_autokeras(tensorflow="gpu")
#' ````
#'
#' @section Windows Installation:
#'
#' The only supported installation method on Windows is "conda". This means that
#' you should install Anaconda 3.x for Windows prior to installing Keras.
#'
#' @section Custom Installation:
#'
#' Installing Keras and TensorFlow using `install_autokeras()` isn't required
#' to use the Keras R package. You can do a custom installation of Keras (and
#' desired backend) as described on the
#' [Keras website](https://keras.io/#installation) and the Keras R package will
#' find and use that version.
#'
#' See the documentation on [custom installations](https://tensorflow.rstudio.com/installation.html#custom-installation)
#' for additional information on how version of Keras and TensorFlow are located
#' by the Keras package.
#'
#' @section Additional Packages:
#'
#' If you wish to add additional PyPI packages to your Keras / TensorFlow
#' environment you can either specify the packages in the `extra_packages`
#' argument of `install_autokeras()`, or alternatively install them into an
#' existing environment using the [reticulate::py_install()] function.
#'
#' @examples
#' \dontrun{
#'
#' # default installation
#' library("autokeras")
#' install_autokeras()
#'
#' # install using a conda environment (default is virtualenv)
#' install_autokeras(method="conda")
#'
#' # install with GPU version of TensorFlow
#' # (NOTE: only do this if you have an NVIDIA GPU + CUDA!)
#' install_autokeras(tensorflow="gpu")
#'
#' # install a specific version of TensorFlow
#' install_autokeras(tensorflow="1.2.1")
#' install_autokeras(tensorflow="1.2.1-gpu")
#'
#' # install a specific version of Keras and TensorFlow
#' install_autokeras(keras="2.2.2", tensorflow="1.2.1")
#'
#' }
#'
#' @importFrom keras install_keras
#' @importFrom reticulate py_config
#'
#' @export
install_autokeras <- function(method=c("auto", "virtualenv", "conda"),
conda="auto",
version="0.3.7", # R autokeras built with this ver
keras="default",
tensorflow="default",
extra_packages=NULL) {

if (as.numeric(reticulate::py_config()$version) < 3.6)
stop("Currently, Auto-Keras is only compatible with: Python 3.6.",
"Please update Python version and re-run install_autokeras()",
call.=FALSE)

# resolve version
if (identical(version, "default"))
version <- ""
else
version <- paste0("==", version)

# perform the install
install_keras(method=method,
conda=conda,
version=keras,
tensorflow=tensorflow,
extra_packages=c(paste0("autokeras", version), extra_packages))
}
5 changes: 5 additions & 0 deletions R/model-image-classifier.R
Expand Up @@ -5,6 +5,11 @@
#' It is used for image classification. It searches convolutional neural
#' network architectures for the best configuration for the image dataset.
#'
#' Important: The object returned by this function behaves like an R6 object,
#' i.e., within function calls with this object as parameter, it is most likely
#' that the object will be modified. Therefore it is not necessary to assign
#' the result of the functions to the same object.
#'
#' @param path A path to the directory to save the classifier as well as
#' intermediate results.
#' @param verbose A boolean of whether the search process will be printed to
Expand Down
5 changes: 5 additions & 0 deletions R/model-image-regressor.R
Expand Up @@ -5,6 +5,11 @@
#' It is used for image regression. It searches convolutional neural network
#' architectures for the best configuration for the image dataset.
#'
#' Important: The object returned by this function behaves like an R6 object,
#' i.e., within function calls with this object as parameter, it is most likely
#' that the object will be modified. Therefore it is not necessary to assign
#' the result of the functions to the same object.
#'
#' @param path A path to the directory to save the classifier as well as
#' intermediate results.
#' @param verbose A boolean of whether the search process will be printed to
Expand Down
5 changes: 5 additions & 0 deletions R/model-tabular-classifier.R
Expand Up @@ -3,6 +3,11 @@
#'
#' TabularClassifier class.
#'
#' Important: The object returned by this function behaves like an R6 object,
#' i.e., within function calls with this object as parameter, it is most likely
#' that the object will be modified. Therefore it is not necessary to assign
#' the result of the functions to the same object.
#'
#' @param path A path to the directory to save the classifier as well as
#' intermediate results.
#' @param verbose A boolean of whether the search process will be printed to
Expand Down
5 changes: 5 additions & 0 deletions R/model-tabular-regressor.R
Expand Up @@ -3,6 +3,11 @@
#'
#' TabularRegressor class.
#'
#' Important: The object returned by this function behaves like an R6 object,
#' i.e., within function calls with this object as parameter, it is most likely
#' that the object will be modified. Therefore it is not necessary to assign
#' the result of the functions to the same object.
#'
#' @param path A path to the directory to save the classifier as well as
#' intermediate results.
#' @param verbose A boolean of whether the search process will be printed to
Expand Down
5 changes: 5 additions & 0 deletions R/model-text-classifier.R
Expand Up @@ -3,6 +3,11 @@
#'
#' A TextClassifier class based on Google AI's BERT model.
#'
#' Important: The object returned by this function behaves like an R6 object,
#' i.e., within function calls with this object as parameter, it is most likely
#' that the object will be modified. Therefore it is not necessary to assign
#' the result of the functions to the same object.
#'
#' @param path A path to the directory to save the classifier as well as
#' intermediate results.
# @param device Specific hardware for using/running the model. E.g:- CPU, GPU
Expand Down
5 changes: 5 additions & 0 deletions R/model-text-regressor.R
Expand Up @@ -3,6 +3,11 @@
#'
#' TextRegressor class.
#'
#' Important: The object returned by this function behaves like an R6 object,
#' i.e., within function calls with this object as parameter, it is most likely
#' that the object will be modified. Therefore it is not necessary to assign
#' the result of the functions to the same object.
#'
#' @param path A path to the directory to save the classifier as well as
#' intermediate results.
#' @param verbose A boolean of whether the search process will be printed to
Expand Down
14 changes: 7 additions & 7 deletions R/package.R
Expand Up @@ -19,18 +19,18 @@ autokeras <- NULL
.onLoad <- function(libname, pkgname) {
# browser()
# delay load keras
autokeras <<- reticulate::import("autokeras", delay_load = list(
# todo: remove? priority, environment, and get_module not documented in
autokeras <<- reticulate::import("autokeras", delay_load=list(
# todo: remove? priority, and get_module not documented in
# reticulate package
priority = 10,
environment = "r-tensorflow",
get_module = function() {
priority=10,
environment="r-tensorflow",
get_module=function() {
"autokeras"
},
on_load = function() {
on_load=function() {
NULL
},
on_error = function(e) {
on_error=function(e) {
NULL
}
))
Expand Down
3 changes: 2 additions & 1 deletion R/predict.R
Expand Up @@ -8,7 +8,8 @@
NULL

#' @rdname predict
#' @export
# @export
#' @export predict.AutokerasModel
predict.AutokerasModel <- function(autokeras_model, x_test) {
autokeras_model@model$predict(x_test=x_test);
}
26 changes: 15 additions & 11 deletions README.Rmd
Expand Up @@ -14,10 +14,7 @@ learning models.

## Dependencies

* have fully functional [Auto-Keras](https://autokeras.com/), i.e. TensorFlow,
Keras, etc.

* have fully functional TensorFlow and Keras R libraries.
* [Auto-Keras](https://autokeras.com/) requires Python 3.6 .

## Installation

Expand All @@ -30,11 +27,18 @@ if (!require("devtools"))
devtools::install_github("jcrodriguez1989/autokeras")
```

Then, use the `install_autokeras()` function to install TensorFlow:

```{r eval=FALSE}
library("autokeras")
install_autokeras()
```

## Examples

### CIFAR-10 dataset

```{r eval=TRUE}
```{r eval=FALSE}
library("autokeras")
library("keras")
Expand All @@ -45,26 +49,26 @@ c(x_train, y_train) %<-% cifar10$train
c(x_test, y_test) %<-% cifar10$test
```

```{r eval=TRUE, include=FALSE}
```{r eval=FALSE, include=FALSE}
x_train <- x_train[1:1000,,,]
y_train <- y_train[1:1000]
x_test <- x_test[1:1000,,,]
y_test <- y_test[1:1000]
```

```{r eval=TRUE}
# Create an image classifier, and train different models for 3 minutes
```{r eval=FALSE}
# Create an image classifier, and train different models for 2 hours
clf <- model_image_classifier(verbose=TRUE, augment=FALSE) %>%
fit(x_train, y_train, time_limit=3*60)
fit(x_train, y_train, time_limit=2*60*60)
```

```{r eval=FALSE}
# Get the best trained model
# For some bug, this needs to be killed (Ctrl+c), but fits the model anyways
clf %>% final_fit(x_train, y_train, x_test, y_test, retrain=TRUE)
clf %>% final_fit(x_train, y_train, x_test, y_test, retrain=TRUE, time_limit=30)
```

```{r eval=TRUE}
```{r eval=FALSE}
# And use it to evaluate, predict
clf %>% evaluate(x_test, y_test)
clf %>% predict(x_test[1:10,,,])
Expand Down
13 changes: 9 additions & 4 deletions README.md
Expand Up @@ -6,9 +6,7 @@ R interface to Auto-Keras
Dependencies
------------

- have fully functional [Auto-Keras](https://autokeras.com/), i.e. TensorFlow, Keras, etc.

- have fully functional TensorFlow and Keras R libraries.
- [Auto-Keras](https://autokeras.com/) requires Python 3.6 .

Installation
------------
Expand All @@ -21,6 +19,13 @@ if (!require("devtools"))
devtools::install_github("jcrodriguez1989/autokeras")
```

Then, use the `install_autokeras()` function to install TensorFlow:

``` r
library("autokeras")
install_autokeras()
```

Examples
--------

Expand All @@ -46,7 +51,7 @@ clf <- model_image_classifier(verbose=TRUE, augment=FALSE) %>%
``` r
# Get the best trained model
# For some bug, this needs to be killed (Ctrl+c), but fits the model anyways
clf %>% final_fit(x_train, y_train, x_test, y_test, retrain=TRUE)
clf %>% final_fit(x_train, y_train, x_test, y_test, retrain=TRUE, time_limit=30)
```

``` r
Expand Down
4 changes: 3 additions & 1 deletion man/final_fit.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4c78ee0

Please sign in to comment.