-
Notifications
You must be signed in to change notification settings - Fork 282
/
Copy pathinstall.R
164 lines (151 loc) · 5.75 KB
/
install.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#' Install Keras and the TensorFlow backend
#'
#' 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
#'
#' @param method Installation method ("virtualenv" or "conda")
#'
#' @param version Version of Keras to install. Specify "default" to install
#' the latest release. Otherwise specify an alternate version (e.g. "2.2.2").
#'
#' @param tensorflow TensorFlow version to install. Specify "default" to install
#' the CPU version of the latest release. Specify "gpu" to install the GPU
#' version of the latest release.
#'
#' You can also provide a full major.minor.patch specification (e.g. "1.1.0"),
#' appending "-gpu" if you want the GPU version (e.g. "1.1.0-gpu").
#'
#' Alternatively, you can provide the full URL to an installer binary (e.g.
#' for a nightly binary).
#'
#' @param extra_packages Additional PyPI packages to install along with
#' Keras and TensorFlow.
#'
#' @param ... Other arguments passed to [tensorflow::install_tensorflow()].
#'
#' @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_keras()`. For example:
#'
#' ```
#' install_keras(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_keras()` 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_keras()`,
#' or alternatively install them into an existing environment using the
#' [reticulate::py_install()] function.
#'
#' @examples
#' \dontrun{
#'
#' # default installation
#' library(keras)
#' install_keras()
#'
#' # install using a conda environment (default is virtualenv)
#' install_keras(method = "conda")
#'
#' # install with GPU version of TensorFlow
#' # (NOTE: only do this if you have an NVIDIA GPU + CUDA!)
#' install_keras(tensorflow = "gpu")
#'
#' # install a specific version of TensorFlow
#' install_keras(tensorflow = "1.2.1")
#' install_keras(tensorflow = "1.2.1-gpu")
#'
#' }
#'
#' @importFrom reticulate py_available conda_binary
#'
#' @export
install_keras <- function(method = c("auto", "virtualenv", "conda"),
conda = "auto",
version = "default",
tensorflow = "default",
extra_packages = c("tensorflow-hub"),
...) {
# verify method
method <- match.arg(method)
# resolve version
if (identical(version, "default"))
version <- ""
else
version <- paste0("==", version)
# some special handling for windows
if (is_windows()) {
# conda is the only supported method on windows
method <- "conda"
# confirm we actually have conda
have_conda <- !is.null(tryCatch(conda_binary(conda), error = function(e) NULL))
if (!have_conda) {
stop("Keras installation failed (no conda binary found)\n\n",
"Install Anaconda for Python 3.x (https://www.anaconda.com/download/#windows)\n",
"before installing Keras.",
call. = FALSE)
}
# avoid DLL in use errors
if (py_available()) {
stop("You should call install_keras() only in a fresh ",
"R session that has not yet initialized Keras and TensorFlow (this is ",
"to avoid DLL in use errors during installation)")
}
}
extra_packages <- unique(c(
paste0("keras", version),
extra_packages,
"h5py",
"pyyaml",
"requests",
"Pillow",
"scipy"
))
# perform the install
install_tensorflow(method = method,
conda = conda,
version = tensorflow,
extra_packages = extra_packages,
pip_ignore_installed = FALSE,
...)
}