-
Notifications
You must be signed in to change notification settings - Fork 33
/
digits-walkthrough
executable file
·240 lines (186 loc) · 6.6 KB
/
digits-walkthrough
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env python2
# Copyright (c) 2014-2016, NVIDIA CORPORATION. All rights reserved.
import argparse
import json
import os
import requests
import socket
import sys
import time
from urlparse import urlparse
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
wait_time = 2
def wait(s=wait_time):
time.sleep(s)
def get_page(driver, url):
driver.get(url)
wait()
def create_dataset(driver, name, folder):
dropdown_elements = driver.find_elements_by_class_name('dropdown-toggle')
dropdown_dataset = dropdown_elements[0]
dropdown_dataset.click()
wait()
dropdown_menu = driver.find_elements_by_class_name('dropdown-menu')
dropdown_menu_dataset = dropdown_menu[0]
dataset_link = dropdown_menu_dataset.find_element_by_tag_name('a')
dataset_link.click()
wait()
folder_train_tooltip = driver.find_element_by_name('folder_train_explanation')
folder_train_tooltip.click()
wait()
folder_train = driver.find_element_by_name('folder_train')
folder_train.send_keys(folder)
wait()
resize_channels_tooltip = driver.find_element_by_name('resize_channels_explanation')
resize_channels_tooltip.click()
wait()
image_type = driver.find_element_by_name('resize_channels')
image_type.click()
wait()
for option in image_type.find_elements_by_tag_name('option'):
if option.text == 'Grayscale':
image_type.click()
option.click()
break
wait()
resize_width_tooltip = driver.find_element_by_name('resize_dims_explanation')
resize_width_tooltip.click()
wait()
resize_width = driver.find_element_by_name('resize_width')
resize_width.clear()
resize_width.send_keys('28')
wait()
resize_height = driver.find_element_by_name('resize_height')
resize_height.clear()
resize_height.send_keys('28')
wait()
dataset_name = driver.find_element_by_name('dataset_name')
dataset_name.click()
dataset_name.send_keys(name)
wait()
create_button = driver.find_element_by_name('create-dataset')
create_button.click()
job_url = driver.current_url.replace('datasets', 'jobs')
status_url = job_url + '/status'
done = False
while not done:
r = requests.get(status_url)
status = json.loads(r.content)
done = status['status'] == 'Done'
wait()
wait()
def create_model(driver, name, dataset_name, test_image):
dropdown_elements = driver.find_elements_by_class_name('dropdown-toggle')
dropdown_model = dropdown_elements[1]
dropdown_model.click()
wait()
dropdown_menu = driver.find_elements_by_class_name('dropdown-menu')
dropdown_menu_model = dropdown_menu[1]
model_link = dropdown_menu_model.find_element_by_tag_name('a')
model_link.click()
# move to 0,0 so we don't accidentally select a hover element
body = driver.find_element_by_css_selector('body')
body.click()
wait()
dataset_tooltip = driver.find_element_by_name('dataset_explanation')
dataset_tooltip.click()
wait()
datasets = driver.find_element_by_name('dataset')
for option in datasets.find_elements_by_tag_name('option'):
if option.text == dataset_name:
option.click()
break
wait()
standard_networks = driver.find_elements_by_name('standard_networks')
lenet = standard_networks[0]
lenet.click()
wait()
model_name = driver.find_element_by_name('model_name')
model_name.click()
model_name.send_keys(name)
wait()
create_button = driver.find_element_by_name('create-model')
create_button.click()
job_url = driver.current_url.replace('models', 'jobs')
status_url = job_url + '/status'
done = False
while not done:
r = requests.get(status_url)
status = json.loads(r.content)
done = status['status'] == 'Done'
wait()
# driver.refresh()
# test image
print 'Testing...'
image_path = driver.find_element_by_name('image_url')
image_path.send_keys(test_image)
wait()
show_visualizations_tooltip = driver.find_element_by_name('show_visualizations_explanation')
show_visualizations_tooltip.click()
wait()
show_visualizations = driver.find_element_by_name('show_visualizations')
show_visualizations.click()
wait()
test_button = driver.find_element_by_name('classify-one-btn')
test_button.click()
# Opens in a new window - switch to it
driver.close()
driver.switch_to_window(driver.window_handles[-1])
def main(argv):
parser = argparse.ArgumentParser(description='Run a Selenium demo of DIGITS')
# Positional arguments
parser.add_argument('mnist_image_folder',
type=str,
help='Path to the MNIST dataset folder')
parser.add_argument('test_image',
type=str,
help='Image to test with')
# Optional arguments
parser.add_argument('-p', '--port',
type=int,
default=80,
help='Port the server is running on (default 80)')
args = vars(parser.parse_args())
home_page = 'http://0.0.0.0:%d/' % args['port']
dataset_path = args['mnist_image_folder']
dataset_name = 'MNIST Dataset'
model_name = 'MNIST Model'
test_image = args['test_image']
r = requests.get(home_page)
assert r.status_code == requests.codes.ok, 'page "%s" does not exist - are you looking on the wrong port?' % home_page
# Start selenium driver.
driver = webdriver.Firefox()
print 'Firefox webdriver started.'
mouse = webdriver.ActionChains(driver)
try:
driver.maximize_window()
get_page(driver, home_page)
print 'Creating dataset...'
create_dataset(driver, dataset_name, dataset_path)
get_page(driver, home_page)
print 'Creating model...'
create_model(driver, model_name, dataset_name, test_image)
print 'Done.'
# display an alert message
get_page(driver, "javascript:alert('Completed Walkthrough!');void(0);")
wait()
driver.switch_to_alert().accept()
# wait until the window is closed
while True:
try:
get_page(driver, "javascript:console.log('Waiting...');void(0);")
except Exception as e:
print e
break
wait()
except KeyboardInterrupt:
pass
finally:
try:
driver.quit()
except socket.error:
pass
if __name__ == '__main__':
main(sys.argv)