ProHoster > Blog > Administration > Quick Draw Doodle Recognition: how to make R, C++ and neural networks friends
Quick Draw Doodle Recognition: how to make R, C++ and neural networks friends
Hey Habr!
Last fall, Kaggle hosted the Quick Draw Doodle Recognition competition for classifying hand-drawn pictures, in which, among others, a team of R-students participated in Artem Klevtsov, Philip the Administrator ΠΈ Andrey Ogurtsov. We will not describe the competition in detail, this has already been done in recent publication.
This time it didnβt work out with medals farming, but a lot of valuable experience was gained, so I would like to tell the community about a number of the most interesting and useful things on Kagle and in everyday work. Among the topics covered: a difficult life without OpenCV, parsing JSONs (these examples consider integrating C++ code into R scripts or packages using rcpp), parameterization of scripts and dockerization of the final solution. All code from the message in a form suitable for running is available in repositories.
1. Efficient loading of data from CSV to the MonetDB database
The data in this competition is not provided in the form of ready-made images, but in the form of 340 CSV files (one file for each class) containing JSONs with point coordinates. By connecting these points with lines, we get the final image of 256x256 pixels. Also, for each entry, a label is given, whether the picture was correctly recognized by the classifier used at the time the dataset was collected, a two-letter code of the country of residence of the author of the picture, a unique identifier, a timestamp, and a class name that matches the file name. The simplified version of the original data weighs 7.4 GB in the archive and about 20 GB after unpacking, the full data after unpacking takes 240 GB. The organizers guaranteed that both versions reproduce the same drawings, that is, the full version is redundant. In any case, storing 50 million images in graphic files or as arrays was immediately recognized as unprofitable, and we decided to merge all CSV files from the archive train_simplified.zip to the database with subsequent generation of images of the required size "on the fly" for each batch.
As a DBMS, a well-proven MonetDB, namely the implementation for R as a package MonetDBLite. The package includes an embedded version of the database server and allows you to raise the server directly from the R session and work with it there. Creating a database and connecting to it is done with one command:
con <- DBI::dbConnect(drv = MonetDBLite::MonetDBLite(), Sys.getenv("DBDIR"))
We will need to create two tables: one for all data, the other for service information about uploaded files (it will come in handy if something goes wrong and the process has to be resumed after uploading several files):
The fastest way to load data into the database was to directly copy CSV files using SQL - the command COPY OFFSET 2 INTO tablename FROM path USING DELIMITERS ',','n','"' NULL AS '' BEST EFFORTWhere tablename - table name and path - the path to the file. While working with the archive, it was found that the built-in implementation unzip in R does not work correctly with a number of files from the archive, so we used the system unzip (using the parameter getOption("unzip")).
If you need to convert the table before writing to the database, it is enough to pass in the argument preprocess a function that will transform the data.
Code for sequential loading of data into the database:
Data download time may vary depending on the speed characteristics of the drive used. In our case, reading and writing within one SSD or from a flash drive (source file) to an SSD (DB) takes less than 10 minutes.
It takes a few more seconds to create a column with an integer class label and an index column (ORDERED INDEX) with line numbers, according to which observations will be sampled when creating batches:
Create additional columns and index
message("Generate lables")
invisible(DBI::dbExecute(con, "ALTER TABLE doodles ADD label_int int"))
invisible(DBI::dbExecute(con, "UPDATE doodles SET label_int = dense_rank() OVER (ORDER BY word) - 1"))
message("Generate row numbers")
invisible(DBI::dbExecute(con, "ALTER TABLE doodles ADD id serial"))
invisible(DBI::dbExecute(con, "CREATE ORDERED INDEX doodles_id_ord_idx ON doodles(id)"))
To solve the problem of forming a batch on the fly, we needed to achieve the maximum speed of extracting random rows from the table doodles. To do this, we used 3 tricks. The first was to reduce the dimension of the type in which the observation ID is stored. In the original dataset, ID storage requires a type bigint, but the number of observations allows us to fit their identifiers, equal to the ordinal number, into the type int. The search is much faster. The second trick was to use ORDERED INDEX - we came to this decision empirically, going through all the available options. The third was to use parameterized queries. The essence of the method is to execute the command once PREPARE with the subsequent use of a prepared expression when creating a bunch of queries of the same type, but in fact, a win over comparison with a simple SELECT was in the region of statistical error.
The process of pouring data consumes no more than 450 MB of RAM. That is, the described approach allows you to move datasets weighing tens of gigabytes on almost any budget hardware, including some single-board devices, which is pretty cool.
It remains to measure the speed of extracting (random) data and evaluate the scaling when sampling batches of different sizes:
The whole batch preparation process consists of the following steps:
Parsing several JSONs containing vectors of strings with point coordinates.
Drawing colored lines based on the coordinates of points on the image of the required size (for example, 256x256 or 128x128).
Converting the resulting images into a tensor.
As part of the competition among Python kernels, the problem was solved mainly by means of OpenCV. One of the simplest and most obvious analogues in R would look like this:
Drawing is performed by standard R tools, saving to a temporary PNG stored in RAM (on Linux, R temporary directories are located in the directory /tmpmounted in RAM). This file is then read in as a 0D array with numbers ranging from 1 to XNUMX. This is important because a more conventional BMP would be read into a raw array with hex color codes.
This implementation seemed suboptimal to us, since the formation of large batches takes an indecently long time, and we decided to take advantage of the experience of colleagues by using a powerful library OpenCV. At that time, there was no ready-made package for R (there is none now), so a minimal implementation of the required functionality was written in C ++ with integration into R code using rcpp.
The following packages and libraries were used to solve the problem:
OpenCV for working with images and drawing lines. We used pre-installed system libraries and header files, as well as dynamic linking.
xtensor for working with multidimensional arrays and tensors. We used the header files included in the R-package of the same name. The library allows you to work with multidimensional arrays, both in row major and column major order.
ndjson for parsing JSON. This library is used in xtensor automatically if it is present in the project.
RcppThread for organizing multi-threaded processing of a vector from JSONs. Used the header files provided by this package. From more popular RcppParallel the package, among other things, is distinguished by a built-in interrupt mechanism for the cycle (interrupt).
It is worth noting that xtensor turned out to be just a godsend: besides the fact that it has extensive functionality and high performance, its developers turned out to be quite responsive and promptly and in detail answered questions that arose. With their help, it was possible to implement the transformation of OpenCV matrices into xtensor tensors, as well as a way to combine 3-dimensional image tensors into a 4-dimensional tensor of the correct dimension (actually a batch).
Materials for learning Rcpp, xtensor and RcppThread
To compile files using system files and dynamic linking with libraries installed in the system, we used the plugin mechanism implemented in the package rcpp. To automatically find paths and flags, we used the popular linux utility pkg-config.
Rcpp plugin implementation to use the OpenCV library
The implementation code for parsing JSON and generating a batch for transfer to the model is given under the spoiler. First, add a local project directory to search for header files (needed for ndjson):
// [[Rcpp::plugins(cpp14)]]
// [[Rcpp::plugins(opencv)]]
// [[Rcpp::depends(xtensor)]]
// [[Rcpp::depends(RcppThread)]]
#include <xtensor/xjson.hpp>
#include <xtensor/xadapt.hpp>
#include <xtensor/xview.hpp>
#include <xtensor-r/rtensor.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <Rcpp.h>
#include <RcppThread.h>
// Π‘ΠΈΠ½ΠΎΠ½ΠΈΠΌΡ Π΄Π»Ρ ΡΠΈΠΏΠΎΠ²
using RcppThread::parallelFor;
using json = nlohmann::json;
using points = xt::xtensor<double,2>; // ΠΠ·Π²Π»Π΅ΡΡΠ½Π½ΡΠ΅ ΠΈΠ· JSON ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΡ ΡΠΎΡΠ΅ΠΊ
using strokes = std::vector<points>; // ΠΠ·Π²Π»Π΅ΡΡΠ½Π½ΡΠ΅ ΠΈΠ· JSON ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΡ ΡΠΎΡΠ΅ΠΊ
using xtensor3d = xt::xtensor<double, 3>; // Π’Π΅Π½Π·ΠΎΡ Π΄Π»Ρ Ρ ΡΠ°Π½Π΅Π½ΠΈΡ ΠΌΠ°ΡΡΠΈΡΡ ΠΈΠ·ΠΎΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ
using xtensor4d = xt::xtensor<double, 4>; // Π’Π΅Π½Π·ΠΎΡ Π΄Π»Ρ Ρ ΡΠ°Π½Π΅Π½ΠΈΡ ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²Π° ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠΉ
using rtensor3d = xt::rtensor<double, 3>; // ΠΠ±ΡΡΡΠΊΠ° Π΄Π»Ρ ΡΠΊΡΠΏΠΎΡΡΠ° Π² R
using rtensor4d = xt::rtensor<double, 4>; // ΠΠ±ΡΡΡΠΊΠ° Π΄Π»Ρ ΡΠΊΡΠΏΠΎΡΡΠ° Π² R
// Π‘ΡΠ°ΡΠΈΡΠ΅ΡΠΊΠΈΠ΅ ΠΊΠΎΠ½ΡΡΠ°Π½ΡΡ
// Π Π°Π·ΠΌΠ΅Ρ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ Π² ΠΏΠΈΠΊΡΠ΅Π»ΡΡ
const static int SIZE = 256;
// Π’ΠΈΠΏ Π»ΠΈΠ½ΠΈΠΈ
// Π‘ΠΌ. https://en.wikipedia.org/wiki/Pixel_connectivity#2-dimensional
const static int LINE_TYPE = cv::LINE_4;
// Π’ΠΎΠ»ΡΠΈΠ½Π° Π»ΠΈΠ½ΠΈΠΈ Π² ΠΏΠΈΠΊΡΠ΅Π»ΡΡ
const static int LINE_WIDTH = 3;
// ΠΠ»Π³ΠΎΡΠΈΡΠΌ ΡΠ΅ΡΠ°ΠΉΠ·Π°
// https://docs.opencv.org/3.1.0/da/d54/group__imgproc__transform.html#ga5bb5a1fea74ea38e1a5445ca803ff121
const static int RESIZE_TYPE = cv::INTER_LINEAR;
// Π¨Π°Π±Π»ΠΎΠ½ Π΄Π»Ρ ΠΊΠΎΠ½Π²Π΅ΡΡΠΈΡΠΎΠ²Π°Π½ΠΈΡ OpenCV-ΠΌΠ°ΡΡΠΈΡΡ Π² ΡΠ΅Π½Π·ΠΎΡ
template <typename T, int NCH, typename XT=xt::xtensor<T,3,xt::layout_type::column_major>>
XT to_xt(const cv::Mat_<cv::Vec<T, NCH>>& src) {
// Π Π°Π·ΠΌΠ΅ΡΠ½ΠΎΡΡΡ ΡΠ΅Π»Π΅Π²ΠΎΠ³ΠΎ ΡΠ΅Π½Π·ΠΎΡΠ°
std::vector<int> shape = {src.rows, src.cols, NCH};
// ΠΠ±ΡΠ΅Π΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ² Π² ΠΌΠ°ΡΡΠΈΠ²Π΅
size_t size = src.total() * NCH;
// ΠΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ cv::Mat Π² xt::xtensor
XT res = xt::adapt((T*) src.data, size, xt::no_ownership(), shape);
return res;
}
// ΠΡΠ΅ΠΎΠ±ΡΠ°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ JSON Π² ΡΠΏΠΈΡΠΎΠΊ ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°Ρ ΡΠΎΡΠ΅ΠΊ
strokes parse_json(const std::string& x) {
auto j = json::parse(x);
// Π Π΅Π·ΡΠ»ΡΡΠ°Ρ ΠΏΠ°ΡΡΠΈΠ½Π³Π° Π΄ΠΎΠ»ΠΆΠ΅Π½ Π±ΡΡΡ ΠΌΠ°ΡΡΠΈΠ²ΠΎΠΌ
if (!j.is_array()) {
throw std::runtime_error("'x' must be JSON array.");
}
strokes res;
res.reserve(j.size());
for (const auto& a: j) {
// ΠΠ°ΠΆΠ΄ΡΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΠΌΠ°ΡΡΠΈΠ²Π° Π΄ΠΎΠ»ΠΆΠ΅Π½ Π±ΡΡΡ 2-ΠΌΠ΅ΡΠ½ΡΠΌ ΠΌΠ°ΡΡΠΈΠ²ΠΎΠΌ
if (!a.is_array() || a.size() != 2) {
throw std::runtime_error("'x' must include only 2d arrays.");
}
// ΠΠ·Π²Π»Π΅ΡΠ΅Π½ΠΈΠ΅ Π²Π΅ΠΊΡΠΎΡΠ° ΡΠΎΡΠ΅ΠΊ
auto p = a.get<points>();
res.push_back(p);
}
return res;
}
// ΠΡΡΠΈΡΠΎΠ²ΠΊΠ° Π»ΠΈΠ½ΠΈΠΉ
// Π¦Π²Π΅ΡΠ° HSV
cv::Mat ocv_draw_lines(const strokes& x, bool color = true) {
// ΠΡΡ ΠΎΠ΄Π½ΡΠΉ ΡΠΈΠΏ ΠΌΠ°ΡΡΠΈΡΡ
auto stype = color ? CV_8UC3 : CV_8UC1;
// ΠΡΠΎΠ³ΠΎΠ²ΡΠΉ ΡΠΈΠΏ ΠΌΠ°ΡΡΠΈΡΡ
auto dtype = color ? CV_32FC3 : CV_32FC1;
auto bg = color ? cv::Scalar(0, 0, 255) : cv::Scalar(255);
auto col = color ? cv::Scalar(0, 255, 220) : cv::Scalar(0);
cv::Mat img = cv::Mat(SIZE, SIZE, stype, bg);
// ΠΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π»ΠΈΠ½ΠΈΠΉ
size_t n = x.size();
for (const auto& s: x) {
// ΠΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΠΎΡΠ΅ΠΊ Π² Π»ΠΈΠ½ΠΈΠΈ
size_t n_points = s.shape()[1];
for (size_t i = 0; i < n_points - 1; ++i) {
// Π’ΠΎΡΠΊΠ° Π½Π°ΡΠ°Π»Π° ΡΡΡΠΈΡ Π°
cv::Point from(s(0, i), s(1, i));
// Π’ΠΎΡΠΊΠ° ΠΎΠΊΠΎΠ½ΡΠ°Π½ΠΈΡ ΡΡΡΠΈΡ Π°
cv::Point to(s(0, i + 1), s(1, i + 1));
// ΠΡΡΠΈΡΠΎΠ²ΠΊΠ° Π»ΠΈΠ½ΠΈΠΈ
cv::line(img, from, to, col, LINE_WIDTH, LINE_TYPE);
}
if (color) {
// ΠΠ΅Π½ΡΠ΅ΠΌ ΡΠ²Π΅Ρ Π»ΠΈΠ½ΠΈΠΈ
col[0] += 180 / n;
}
}
if (color) {
// ΠΠ΅Π½ΡΠ΅ΠΌ ΡΠ²Π΅ΡΠΎΠ²ΠΎΠ΅ ΠΏΡΠ΅Π΄ΡΡΠ°Π²Π»Π΅Π½ΠΈΠ΅ Π½Π° RGB
cv::cvtColor(img, img, cv::COLOR_HSV2RGB);
}
// ΠΠ΅Π½ΡΠ΅ΠΌ ΡΠΎΡΠΌΠ°Ρ ΠΏΡΠ΅Π΄ΡΡΠ°Π²Π»Π΅Π½ΠΈΡ Π½Π° float32 Ρ Π΄ΠΈΠ°ΠΏΠ°Π·ΠΎΠ½ΠΎΠΌ [0, 1]
img.convertTo(img, dtype, 1 / 255.0);
return img;
}
// ΠΠ±ΡΠ°Π±ΠΎΡΠΊΠ° JSON ΠΈ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΠ΅ ΡΠ΅Π½Π·ΠΎΡΠ° Ρ Π΄Π°Π½Π½ΡΠΌΠΈ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ
xtensor3d process(const std::string& x, double scale = 1.0, bool color = true) {
auto p = parse_json(x);
auto img = ocv_draw_lines(p, color);
if (scale != 1) {
cv::Mat out;
cv::resize(img, out, cv::Size(), scale, scale, RESIZE_TYPE);
cv::swap(img, out);
out.release();
}
xtensor3d arr = color ? to_xt<double,3>(img) : to_xt<double,1>(img);
return arr;
}
// [[Rcpp::export]]
rtensor3d cpp_process_json_str(const std::string& x,
double scale = 1.0,
bool color = true) {
xtensor3d res = process(x, scale, color);
return res;
}
// [[Rcpp::export]]
rtensor4d cpp_process_json_vector(const std::vector<std::string>& x,
double scale = 1.0,
bool color = false) {
size_t n = x.size();
size_t dim = floor(SIZE * scale);
size_t channels = color ? 3 : 1;
xtensor4d res({n, dim, dim, channels});
parallelFor(0, n, [&x, &res, scale, color](int i) {
xtensor3d tmp = process(x[i], scale, color);
auto view = xt::view(res, i, xt::all(), xt::all(), xt::all());
view = tmp;
});
return res;
}
This code should be placed in a file src/cv_xt.cpp and compile with command Rcpp::sourceCpp(file = "src/cv_xt.cpp", env = .GlobalEnv); also required to work nlohmann/json.hpp of repository. The code is divided into several functions:
to_xt is a templated function for transforming the image matrix (cv::Mat) into a tensor xt::xtensor;
parse_json - the function parses a JSON string, extracts the coordinates of points, packing them into a vector;
ocv_draw_lines - from the received vector of points draws multi-colored lines;
process - combines the above functions, and also adds the ability to scale the resulting image;
cpp_process_json_str - a wrapper over a function process, which exports the result to an R-object (multidimensional array);
cpp_process_json_vector - a wrapper over a function cpp_process_json_str, which allows you to process a string vector in multithreaded mode.
To draw multi-colored lines, the HSV color model was used, followed by conversion to RGB. Let's test the result:
As you can see, the speed increase turned out to be very significant, and it is not possible to catch up with C++ code using R code parallelization.
3. Iterators for unloading batches from the database
R has a well-deserved reputation as a language for processing data that fits in RAM, while Python is more characterized by iterative data processing, which allows you to easily implement out-of-core calculations (calculations using external memory). Classical and relevant for us in the context of the described problem, an example of such calculations are deep neural networks trained by the gradient descent method with gradient approximation at each step using a small portion of observations, or a mini-batch.
Deep learning frameworks written in Python have special classes that implement iterators over data: tables, pictures in folders, binary formats, etc. You can use ready-made options or write your own for specific tasks. In R, we can take advantage of all the features of the Python library hard with its various backends using the package of the same name, which in turn runs on top of the package reticulate. The latter deserves a separate large article; not only does it allow you to run Python code from within R, but it also allows objects to be passed between R and Python sessions, automatically doing all the necessary type conversions.
We got rid of the need to store all the data in RAM by using MonetDBLite, all the βneural networkβ work will be done by the original Python code, we just have to write an iterator over the data, since neither R nor Python is ready for such a situation. There are essentially only two requirements for it: it must return batches in an infinite loop and save its state between iterations (the latter in R is implemented in the simplest way using closures). Previously, it was required to explicitly convert R arrays to numpy arrays inside the iterator, but the current version of the package hard does it herself.
The iterator for the training and validation data is as follows:
The function takes as input a variable with a database connection, the numbers of rows used, the number of classes, the batch size, the scale (scale = 1 corresponds to drawing pictures of 256x256 pixels, scale = 0.5 - 128x128 pixels), color indicator (color = FALSE specifies rendering in grayscale, when using color = TRUE each stroke is drawn in a new color) and a preprocessing indicator for networks pretrained on imagenet. The latter is needed in order to scale the pixel values ββfrom the interval [0, 1] to the interval [-1, 1], which was used when training the supplied hard models.
External function contains argument type checking, table data.table with randomly shuffled line numbers from samples_index and batch numbers, the counter and the maximum number of batches, as well as an SQL expression for unloading data from the database. Additionally, we have defined inside a fast analogue of the function keras::to_categorical(). We used almost all the data for training, leaving half a percent for validation, so the epoch size was limited by the parameter steps_per_epoch when called keras::fit_generator(), and the condition if (i > max_i) only worked for the validation iterator.
In the internal function, row indices are selected for the next batch, records are unloaded from the database with an increase in the batch counter, JSON parsing (function cpp_process_json_vector(), written in C++) and creating arrays corresponding to pictures. Then one-hot vectors with class labels are created, arrays with pixel values ββand labels are combined into a list, which is the returned value. To speed up work, we used the creation of indexes in tables data.table and modification by reference - without these "chips" of the package data.table it is rather difficult to imagine efficient work with any significant amount of data in R.
The results of measuring the speed of work on a notebook Core i5 are as follows:
If you have enough RAM, you can seriously speed up the database by moving it to this same RAM (32 GB is enough for our task). On Linux, the partition is mounted by default. /dev/shmoccupies up to half the amount of RAM. You can highlight more by editing /etc/fstabto get a record like tmpfs /dev/shm tmpfs defaults,size=25g 0 0. Be sure to reboot and check the result by running the command df -h.
The test data iterator looks much simpler, since the test dataset fits entirely in RAM:
The first architecture used was mobile net v1, the features of which are analyzed in This message. It is included in the standard delivery. hard and, accordingly, is available in the package of the same name for R. But when trying to use it with single-channel images, a strange thing turned out: the input tensor must always have the dimension (batch, height, width, 3), that is, the number of channels cannot be changed. There is no such restriction in Python, so we hurried and wrote our own implementation of this architecture, following the original article (without the dropout, which is in the keras version):
The disadvantages of this approach are obvious. I want to check a lot of models, but I donβt want to rewrite each architecture manually, on the contrary. Also, we were deprived of the opportunity to use the weights of models previously trained on imagenet. As usual, studying the documentation helped. Function get_config() allows you to get a description of the model in a form suitable for editing (base_model_conf$layers is an ordinary R-list), and the function from_config() performs the reverse transformation to the model object:
When using single-band images, pre-trained weights are not used. This could be corrected by using the function get_weights() get the weights of the model as a list of R arrays, change the dimension of the first element of this list (by taking one color channel or averaging all three), and then load the weights back into the model with the function set_weights(). We never added this functionality, because at this stage it was already clear that it was more productive to work with color pictures.
We did the bulk of the experiments using mobilenet versions 1 and 2, as well as resnet34. In this competition, more modern architectures such as SE-ResNeXt performed well. Unfortunately, we did not have ready-made implementations at our disposal, and we did not write our own (but we will definitely write).
5. Parameterization of scripts
For convenience, the entire code for starting training was formatted as a single script, parameterized using docpt in the following way:
doc <- '
Usage:
train_nn.R --help
train_nn.R --list-models
train_nn.R [options]
Options:
-h --help Show this message.
-l --list-models List available models.
-m --model=<model> Neural network model name [default: mobilenet_v2].
-b --batch-size=<size> Batch size [default: 32].
-s --scale-factor=<ratio> Scale factor [default: 0.5].
-c --color Use color lines [default: FALSE].
-d --db-dir=<path> Path to database directory [default: Sys.getenv("db_dir")].
-r --validate-ratio=<ratio> Validate sample ratio [default: 0.995].
-n --n-gpu=<number> Number of GPUs [default: 1].
'
args <- docopt::docopt(doc)
Plastic bag docpt is an implementation http://docopt.org/ for R. With its help, scripts are launched with simple commands of the form Rscript bin/train_nn.R -m resnet50 -c -d /home/andrey/doodle_db or ./bin/train_nn.R -m resnet50 -c -d /home/andrey/doodle_dbif the file train_nn.R is executable (this command will start training the model resnet50 on three-color images of 128x128 pixels, the database must be located in the folder /home/andrey/doodle_db). You can add learning rate, optimizer type, and any other customizable parameters to the list. In the process of preparing the publication, it turned out that the architecture mobilenet_v2 from current version hard in R use must not due to changes not taken into account in the R-package - we wait until they fix it.
This approach allowed us to significantly speed up experiments with different models compared to the more traditional launch of scripts in RStudio (as a possible alternative, we note the package tfruns). But the main advantage is the ability to easily manage the launch of scripts in docker or just on the server without installing RStudio for this.
6. Dockerize scripts
We used docker to provide a portable environment for model training between team members and for live deployment in the cloud. You can start your acquaintance with this relatively unusual tool for an R programmer with this series of publications or video course.
Docker allows you to create your own images from scratch, or use other images as a basis for creating your own. When analyzing the available options, we came to the conclusion that installing NVIDIA drivers, CUDA + cuDNN and Python libraries is a rather voluminous part of the image, and decided to take the official image as a basis tensorflow/tensorflow:1.12.0-gpu, adding the necessary R packages there.
For convenience, the packages used have been moved to variables; the main part of the written scripts is copied inside the containers during assembly. We also changed the command shell to /bin/bash for ease of use of content /etc/os-release. This avoided the need to specify the OS version in the code.
Additionally, a small bash script was written that allows you to run a container with various commands. For example, these can be scripts for training neural networks, previously placed inside the container, or a command shell for debugging and monitoring the operation of the container:
Script to run container
#!/bin/sh
DBDIR=${PWD}/db
LOGSDIR=${PWD}/logs
MODELDIR=${PWD}/models
DATADIR=${PWD}/data
ARGS="--runtime=nvidia --rm -v ${DBDIR}:/db -v ${LOGSDIR}:/app/logs -v ${MODELDIR}:/app/models -v ${DATADIR}:/app/data"
if [ -z "$1" ]; then
CMD="Rscript /app/train_nn.R"
elif [ "$1" = "bash" ]; then
ARGS="${ARGS} -ti"
else
CMD="Rscript /app/train_nn.R $@"
fi
docker run ${ARGS} doodles-tf ${CMD}
If this bash script is run without parameters, the script will be called inside the container train_nn.R with default values; if the first positional argument is "bash", then the container will start in interactive mode with a shell. In all other cases, the values ββof the positional arguments are substituted: CMD="Rscript /app/train_nn.R $@".
It is worth noting that the directories with the source data and the database, as well as the directory for saving the trained models, are mounted inside the container from the host system, which allows you to access the results of the scripts without unnecessary manipulations.
7. Using multiple GPUs in Google Cloud
One of the highlights of the competition was some very noisy data (see header image borrowed from @Leigh.plt from ODS Slack). Large batch sizes help to combat this, and after experimenting on a PC with 1 GPU, we decided to learn how to train models on multiple GPUs in the cloud. Used Google Cloudgood basics guide) due to the large selection of available configurations, reasonable prices and bonus $300. Out of greed, I ordered an instance with a 4xV100 with an SSD and a bunch of RAM, and this was a big mistake. Such a machine eats money quickly, and you can go broke on experiments without a proven pipeline. For training purposes, it is better to take the K80. But a large amount of RAM came in handy - the cloud SSD did not impress with speed, so the database was transferred to dev/shm.
Of greatest interest is the code snippet responsible for using multiple GPUs. First, the model is created on the CPU using the context manager, just like in Python:
The classic technique with freezing all layers except the last one, training the last layer, defrosting and retraining the entire model for several GPUs could not be implemented.
Training was followed without using tensorboard, limited to writing logs and saving models with informative names after each epoch:
A number of problems that we encountered have not yet been overcome:
Π² hard there is no ready-made function for automatically searching for the optimal learning rate (similar to lr_finder in library fast.ai); with some effort, third-party implementations can be ported to R, for example, this;
as a consequence of the previous paragraph, it was not possible to find the correct learning rate when using multiple GPUs;
there are not enough modern architectures of neural networks, especially those pre-trained on imagenet;
no one cycle policy and discriminative learning rates (cosine annealing at our request was implemented, Thank you skeydan).
What was useful to take away from this competition:
On relatively low-power hardware, you can work without pain with decent (a multiple of the size of RAM) amounts of data. Plastic bag data.table saves memory due to in-place modification of tables, which avoids copying them, and with the right use of its capabilities, it almost always demonstrates the highest speed among all scripting language tools known to us. Saving data in the database allows in many cases not to think at all about the need to squeeze the entire dataset into RAM.
Slow functions in R can be replaced with fast functions in C++ using the package rcpp. If in addition to use RcppThread or RcppParallel, we get cross-platform multi-threaded implementations, so the code at the R level does not need to be parallelized.
By package rcpp can be used without serious knowledge of C ++, the necessary minimum is set out here. Header files for a number of cool sish libraries like xtensor available on CRAN, that is, an infrastructure is being formed for the implementation of projects that integrate ready-made high-performance C ++ code into R. An additional convenience is syntax highlighting and a static C++ code analyzer in RStudio.
docpt allows you to run self-contained scripts with parameters. This is convenient for use on a remote server, incl. under docker. In RStudio, it is inconvenient to conduct many hours of experiments with training neural networks, and the installation of the IDE on the server itself is not always justified.
Docker provides portability of code and reproducibility of results between developers with different versions of OS and libraries, as well as ease of running on servers. You can run the entire pipeline for training with just one command.
Google Cloud is a budget way to experiment on expensive hardware, but you need to choose your configurations thoughtfully.
Measuring the speed of individual code fragments is very useful, especially when combining R and C ++, and with a package bench - also very easy.
Overall, this experience has been very helpful and we are continuing to work on resolving some of the issues that have been raised.