forked from dvgodoy/PyTorchStepByStep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
191 lines (166 loc) · 6.38 KB
/
config.py
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
189
190
191
import os
import sys
import errno
import requests
import subprocess
import shutil
from IPython.display import HTML, display
from tensorboard import manager
def tensorboard_cleanup():
info_dir = manager._get_info_dir()
shutil.rmtree(info_dir)
FOLDERS = {
0: ['plots'],
1: ['plots'],
2: ['plots', 'data_generation', 'data_preparation', 'model_configuration', 'model_training'],
21: ['plots', 'data_generation', 'data_preparation', 'model_configuration', 'stepbystep'],
3: ['plots', 'stepbystep'],
4: ['plots', 'stepbystep', 'data_generation'],
5: ['plots', 'stepbystep', 'data_generation', ''],
6: ['plots', 'stepbystep', 'stepbystep', 'data_generation', 'data_generation', 'data_preparation'],
7: ['plots', 'stepbystep', 'data_generation'],
71: ['plots', 'stepbystep', 'data_generation'],
8: ['plots', 'plots', 'stepbystep', 'data_generation'],
9: ['plots', 'plots', 'plots', 'stepbystep', 'data_generation'],
10: ['plots', 'plots', 'plots', 'plots', 'stepbystep', 'data_generation', 'data_generation', '', ''],
11: ['plots', 'stepbystep', 'data_generation', ''],
}
FILENAMES = {
0: ['chapter0.py'],
1: ['chapter1.py'],
2: ['chapter2.py', 'simple_linear_regression.py', 'v0.py', 'v0.py', 'v0.py'],
21: ['chapter2_1.py', 'simple_linear_regression.py', 'v2.py', '', 'v0.py'],
3: ['chapter3.py', 'v0.py'],
4: ['chapter4.py', 'v0.py', 'image_classification.py'],
5: ['chapter5.py', 'v1.py', 'image_classification.py', 'helpers.py'],
6: ['chapter6.py', 'v2.py', 'v3.py', 'rps.py', 'simple_linear_regression.py', 'v2.py'],
7: ['chapter7.py', 'v3.py', 'rps.py'],
71: ['chapterextra.py', 'v3.py', 'ball.py'],
8: ['chapter8.py', 'replay.py', 'v4.py', 'square_sequences.py'],
9: ['chapter8.py', 'chapter9.py', 'replay.py', 'v4.py', 'square_sequences.py'],
10: ['chapter8.py', 'chapter9.py', 'chapter10.py', 'replay.py', 'v4.py', 'square_sequences.py', 'image_classification.py', 'helpers.py', 'seq2seq.py'],
11: ['chapter11.py', 'v4.py', 'nlp.py', 'seq2seq.py'],
}
try:
host = os.environ['BINDER_SERVICE_HOST']
IS_BINDER = True
except KeyError:
IS_BINDER = False
try:
import google.colab
IS_COLAB = True
except ModuleNotFoundError:
IS_COLAB = False
IS_LOCAL = (not IS_BINDER) and (not IS_COLAB)
def download_to_colab(chapter, branch='master'):
base_url = 'https://raw.githubusercontent.com/dvgodoy/PyTorchStepByStep/{}/'.format(branch)
folders = FOLDERS[chapter]
filenames = FILENAMES[chapter]
for folder, filename in zip(folders, filenames):
if len(folder):
try:
os.mkdir(folder)
except OSError as e:
if e.errno != errno.EEXIST:
raise
if len(filename):
path = os.path.join(folder, filename)
url = '{}{}'.format(base_url, path)
r = requests.get(url, allow_redirects=True)
open(path, 'wb').write(r.content)
try:
os.mkdir('runs')
except OSError as e:
if e.errno != errno.EEXIST:
raise
TB_LINK = ''
if IS_BINDER:
TB_LINK = HTML('''
<a href="" target="_blank" id="tb">Click here to open TensorBoard</a>
<script>
var address=document.location.href;
a = document.getElementById('tb');
a.href = address.substr(0, address.lastIndexOf("/")-9).concat("proxy/6006/");
</script>
''')
def config_chapter0(branch='master'):
if IS_COLAB:
print('Downloading files from GitHub repo to Colab...')
download_to_colab(0, branch)
print('Finished!')
def config_chapter1(branch='master'):
if IS_COLAB:
print('Installing torchviz...')
subprocess.run([sys.executable, '-m', 'pip', 'install', 'torchviz'])
print('Downloading files from GitHub repo to Colab...')
download_to_colab(1, branch)
print('Creating folders...')
folders = ['data_preparation', 'model_configuration', 'model_training']
for folder in folders:
try:
os.mkdir(folder)
except OSError as e:
e.errno
if e.errno != errno.EEXIST:
raise
print('Finished!')
def config_chapter2(branch='master'):
if IS_COLAB:
print('Downloading files from GitHub repo to Colab...')
download_to_colab(2, branch)
print('Finished!')
def config_chapter2_1(branch='master'):
if IS_COLAB:
print('Downloading files from GitHub repo to Colab...')
download_to_colab(21, branch)
print('Finished!')
def config_chapter3(branch='master'):
if IS_COLAB:
print('Downloading files from GitHub repo to Colab...')
download_to_colab(3, branch)
print('Finished!')
def config_chapter4(branch='master'):
if IS_COLAB:
print('Downloading files from GitHub repo to Colab...')
download_to_colab(4, branch)
print('Finished!')
def config_chapter5(branch='master'):
if IS_COLAB:
print('Downloading files from GitHub repo to Colab...')
download_to_colab(5, branch)
print('Finished!')
def config_chapter6(branch='master'):
if IS_COLAB:
print('Downloading files from GitHub repo to Colab...')
download_to_colab(6, branch)
print('Finished!')
def config_chapter7(branch='master'):
if IS_COLAB:
print('Downloading files from GitHub repo to Colab...')
download_to_colab(7, branch)
print('Finished!')
def config_chapterextra(branch='master'):
if IS_COLAB:
print('Downloading files from GitHub repo to Colab...')
download_to_colab(71, branch)
print('Finished!')
def config_chapter8(branch='master'):
if IS_COLAB:
print('Downloading files from GitHub repo to Colab...')
download_to_colab(8, branch)
print('Finished!')
def config_chapter9(branch='master'):
if IS_COLAB:
print('Downloading files from GitHub repo to Colab...')
download_to_colab(9, branch)
print('Finished!')
def config_chapter10(branch='master'):
if IS_COLAB:
print('Downloading files from GitHub repo to Colab...')
download_to_colab(10, branch)
print('Finished!')
def config_chapter11(branch='master'):
if IS_COLAB:
print('Downloading files from GitHub repo to Colab...')
download_to_colab(11, branch)
print('Finished!')