-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2018-11-19-reliable-training-hack-on-google-colaboratory.html
188 lines (182 loc) · 15.6 KB
/
2018-11-19-reliable-training-hack-on-google-colaboratory.html
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Noon van der Silk - Reliable training hack on the Google Colaboratory</title>
<link rel="stylesheet" type="text/css" href="../css/default.css" />
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/javascript">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [['$','$'], ['\\(','\\)']],
processEscapes: true
}
});
</script>
<script>
var host = "silky.github.io";
if ((host == window.location.host) && (window.location.protocol != "https:")) {
window.location.protocol = "https";
}
</script>
<link href="https://fonts.googleapis.com/css?family=Taviraj|PT+Mono" rel="stylesheet">
</head>
<body>
<div id="header">
<div id="logo">
<h1><a href="../">Noon van der Silk</a></h1>
</div>
<div id="navigation">
<a href="../about.html">About</a>
<a href="../archive.html">Archive</a>
<a href="https://betweenbooks.com.au/">Between Books</a>
<a href="../talks.html">Talks</a>
<a href="../links.html">Links</a>
<small><a href="../atom.xml">Atom Feed</a></small>
</div>
</div>
<div id="content">
<h2>Reliable training hack on the Google Colaboratory</h2>
<div class="info">
Posted on November 19, 2018
by Noon van der Silk
</div>
<p>Google’s
<a href="https://colab.research.google.com/notebooks/welcome.ipynb#recent=true">Colaboratory</a>
is a hosted notebook environment, with access to GPUs, and even TPUs!</p>
<p>It’s really quite handy, but by far the biggest downside is that the sessions
time out. It makes sense; I’m sure even Google can’t give out an unlimited
amount of compute-resources for free to every person.</p>
<h3 id="backgroundproblem">Background/Problem</h3>
<p>On the weekend, I wanted to train a few <a href="https://github.com/tensorflow/magenta/tree/master/magenta/models/sketch_rnn">sketch-rnn
models</a>
on the <a href="https://quickdraw.withgoogle.com/data">quickdraw data</a>.</p>
<p>Naively, I figured this would be really easy with Google colab. While it was
straightforward to start training, what I noticed is that getting data on to
and off of the instance was frustrating, and the timeouts blocked me from
getting a good amount of training time.</p>
<h3 id="solution">Solution</h3>
<p>Happily, colab supports very nice integration with Google services, so my plan
was:</p>
<ol type="1">
<li>Download data from Google Cloud Platform (GCP),</li>
<li>Train, or continue training,</li>
<li>Push a checkpoint to Google Drive occasionally,</li>
<li>Repeat until happy.</li>
</ol>
<p>Here’s how it looks, in code:</p>
<p><strong>Download data from GCP</strong></p>
<p>As I’m working with the quickdraw data, it’s already on the Google Cloud
Platform, so this was very easy. In a cell, I simply ran the following to get
the “eye” quickdraw data:</p>
<pre><code>!gsutil cp gs://quickdraw_dataset/sketchrnn/eye.npz .</code></pre>
<p>(Note that the <code>gsutil</code> command is already installed on the instance.)</p>
<p><strong>Train, or continue training, and save to Drive</strong></p>
<p>As I’m using the <code>sketch_rnn</code> model, I first simply install <code>magenta</code> (and I
have to pick a Python 2 environment.)</p>
<pre><code>!pip install magenta</code></pre>
<p>Now, there’s some considerations. Recalling that I’m going to be pushing my
checkpoints to Google Drive, I need to authenticate with Google Drive. This
is how that looks:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> google.colab <span class="im">import</span> auth</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>auth.authenticate_user()</span></code></pre></div>
<p>Then you’ll be prompted to copy in a code. Once that’s done, you can connect
to Google Drive like so:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> googleapiclient.discovery <span class="im">import</span> build</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a>drive_service <span class="op">=</span> build(<span class="st">'drive'</span>, <span class="st">'v3'</span>)</span></code></pre></div>
<p>Now, if I’m training from scratch, I’ll run something like this:</p>
<pre><code>!sketch_rnn_train --log_root=logs --data_dir=./ --hparams="data_set=[eye.npz],num_steps=501"</code></pre>
<p>This will run for however long, and utlimately produce checkpoints in the
<code>./logs</code> folder, supposing that <code>eye.npz</code> exists in the present directory.</p>
<p>Once that’s completed, I start my main training-pushing loop. Firstly, there’s
a bit of busywork to zip files, get the latest checkpoint number, and upload
it to Google Drive:</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> os</span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> zipfile</span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> googleapiclient.http <span class="im">import</span> MediaFileUpload</span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> get_largest_num (<span class="bu">dir</span><span class="op">=</span><span class="st">"logs"</span>, prefix<span class="op">=</span><span class="st">"vector"</span>):</span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a> files <span class="op">=</span> os.listdir(<span class="bu">dir</span>)</span>
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb6-9"><a href="#cb6-9" aria-hidden="true" tabindex="-1"></a> biggest <span class="op">=</span> <span class="dv">0</span></span>
<span id="cb6-10"><a href="#cb6-10" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb6-11"><a href="#cb6-11" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> f <span class="kw">in</span> files:</span>
<span id="cb6-12"><a href="#cb6-12" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> f.startswith(prefix):</span>
<span id="cb6-13"><a href="#cb6-13" aria-hidden="true" tabindex="-1"></a> k <span class="op">=</span> <span class="bu">int</span>( f.split(<span class="st">"."</span>)[<span class="dv">0</span>].split(<span class="st">"-"</span>)[<span class="dv">1</span>] ) </span>
<span id="cb6-14"><a href="#cb6-14" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> k <span class="op">></span> biggest:</span>
<span id="cb6-15"><a href="#cb6-15" aria-hidden="true" tabindex="-1"></a> biggest <span class="op">=</span> k</span>
<span id="cb6-16"><a href="#cb6-16" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb6-17"><a href="#cb6-17" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> biggest</span>
<span id="cb6-18"><a href="#cb6-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-19"><a href="#cb6-19" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-20"><a href="#cb6-20" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> zip_model (name, k):</span>
<span id="cb6-21"><a href="#cb6-21" aria-hidden="true" tabindex="-1"></a> sk <span class="op">=</span> <span class="bu">str</span>(k)</span>
<span id="cb6-22"><a href="#cb6-22" aria-hidden="true" tabindex="-1"></a> zipobj <span class="op">=</span> zipfile.ZipFile(name <span class="op">+</span> <span class="st">".zip"</span>, <span class="st">"w"</span>, zipfile.ZIP_DEFLATED)</span>
<span id="cb6-23"><a href="#cb6-23" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-24"><a href="#cb6-24" aria-hidden="true" tabindex="-1"></a> files <span class="op">=</span> [ <span class="st">"checkpoint"</span></span>
<span id="cb6-25"><a href="#cb6-25" aria-hidden="true" tabindex="-1"></a> , <span class="st">"model_config.json"</span></span>
<span id="cb6-26"><a href="#cb6-26" aria-hidden="true" tabindex="-1"></a> , <span class="st">"vector-"</span> <span class="op">+</span> sk <span class="op">+</span> <span class="st">".meta"</span></span>
<span id="cb6-27"><a href="#cb6-27" aria-hidden="true" tabindex="-1"></a> , <span class="st">"vector-"</span> <span class="op">+</span> sk <span class="op">+</span> <span class="st">".index"</span></span>
<span id="cb6-28"><a href="#cb6-28" aria-hidden="true" tabindex="-1"></a> , <span class="st">"vector-"</span> <span class="op">+</span> sk <span class="op">+</span> <span class="st">".data-00000-of-00001"</span>]</span>
<span id="cb6-29"><a href="#cb6-29" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb6-30"><a href="#cb6-30" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> f <span class="kw">in</span> files:</span>
<span id="cb6-31"><a href="#cb6-31" aria-hidden="true" tabindex="-1"></a> zipobj.write(<span class="st">"logs/"</span> <span class="op">+</span> f, f)</span>
<span id="cb6-32"><a href="#cb6-32" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-33"><a href="#cb6-33" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-34"><a href="#cb6-34" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-35"><a href="#cb6-35" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> upload_to_drive (name<span class="op">=</span><span class="st">"model.zip"</span>):</span>
<span id="cb6-36"><a href="#cb6-36" aria-hidden="true" tabindex="-1"></a> file_metadata <span class="op">=</span> {</span>
<span id="cb6-37"><a href="#cb6-37" aria-hidden="true" tabindex="-1"></a> <span class="st">"name"</span>: name,</span>
<span id="cb6-38"><a href="#cb6-38" aria-hidden="true" tabindex="-1"></a> <span class="st">"mimeType"</span>: <span class="st">"binary/octet-stream"</span> }</span>
<span id="cb6-39"><a href="#cb6-39" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-40"><a href="#cb6-40" aria-hidden="true" tabindex="-1"></a> media <span class="op">=</span> MediaFileUpload(name, </span>
<span id="cb6-41"><a href="#cb6-41" aria-hidden="true" tabindex="-1"></a> mimetype<span class="op">=</span><span class="st">"binary/octet-stream"</span>,</span>
<span id="cb6-42"><a href="#cb6-42" aria-hidden="true" tabindex="-1"></a> resumable<span class="op">=</span><span class="va">True</span>)</span>
<span id="cb6-43"><a href="#cb6-43" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-44"><a href="#cb6-44" aria-hidden="true" tabindex="-1"></a> created <span class="op">=</span> drive_service.files().create(body<span class="op">=</span>file_metadata,</span>
<span id="cb6-45"><a href="#cb6-45" aria-hidden="true" tabindex="-1"></a> media_body<span class="op">=</span>media,</span>
<span id="cb6-46"><a href="#cb6-46" aria-hidden="true" tabindex="-1"></a> fields<span class="op">=</span><span class="st">"id"</span>).execute()</span>
<span id="cb6-47"><a href="#cb6-47" aria-hidden="true" tabindex="-1"></a> file_id <span class="op">=</span> created.get(<span class="st">"id"</span>)</span>
<span id="cb6-48"><a href="#cb6-48" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> file_id</span></code></pre></div>
<p>Then, the main loop:</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>iterations <span class="op">=</span> <span class="dv">200</span></span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> k <span class="kw">in</span> <span class="bu">range</span>(iterations):</span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="st">"Iteration "</span> <span class="op">+</span> <span class="bu">str</span>(k))</span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a> cmd <span class="op">=</span> <span class="st">'sketch_rnn_train --log_root=logs --resume_training --data_dir=./ '</span> <span class="op">+</span> <span class="op">\</span></span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a> <span class="st">' --hparams="data_set=[eye.npz],num_steps=1001"'</span></span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a> x <span class="op">=</span> os.system(cmd)</span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a> zip_model(<span class="st">"model"</span>, get_largest_num())</span>
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a> upload_to_drive()</span></code></pre></div>
<p>So, all that does is run the main training command, to reload the model from
the latest checkpoint and continue training, then zips and uploads!</p>
<p>Set the iterations to whatever you wish; chances are your instance will never
run for that long anyway; the main point is to push up the checkpoints
every-so-often (for me, every 1000 steps of the sketch_rnn model; which takes
about 1 hour or so, depending on params.)</p>
<p><strong>Brining down the most recent Drive checkpoint</strong></p>
<p>Now, when your instance goes away, you’ll need to bring down the most recent
checkpoint <em>from</em> Drive. I did this somewhat manually, but it works well
enough:</p>
<pre><code># Mount Google Drive as a folder
from google.colab import drive
drive.mount('/content/gdrive')</code></pre>
<pre><code># Extract latest model zip file
!cp /content/gdrive/My\ Drive/model\ \(3\).zip logs/model.zip && cd logs && unzip model.zip</code></pre>
<p>Note that Google Drive numbers all the files as copies, like “model (4).zip”,
“model (5).zip”, when you upload the same name. On the web interface, it only
shows one file, but gives you history. Do as you wish here; I was a bit lazy.</p>
<h3 id="thats-it">That’s it!</h3>
<p>Hope this helps you do some training!</p>
<p>You can read more about other ways to access data from Google Colaboratory
<a href="https://colab.research.google.com/notebooks/io.ipynb">here</a>.</p>
</div>
<div id="footer">
<small><a href="http://jaspervdj.be/hakyll">Hakyll</a> was involved here (<a href="https://github.com/silky/silky.github.com">source</a>).
ن
</small>
</div>
<div id="image_footer"> </div>
</body>
</html>