-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPredictAllParallel.py
More file actions
139 lines (95 loc) · 3.73 KB
/
PredictAllParallel.py
File metadata and controls
139 lines (95 loc) · 3.73 KB
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
import tensorflow as tf
import pandas as pd
import numpy as np
from time import time
from threading import Thread
from keras.models import load_model
import glob
import pickle
import os
import math
from keras.datasets import cifar10
from sklearn.metrics import accuracy_score
CPU_PERCENTAGES = np.linspace(0, 1, 11) # 10 values from 0 to 1 (inclusive)
#BATCHES = [1, 8, 16, 32, 64, 128, 256, 512, 1024, 2048]
BATCHES = [2, 4]
NRUNS = 3
MODELS_FOLDER = 'models'
RESULTS_FOLDER = 'results'
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_test = x_test.astype('float32')
x_test /= 255
def split_in_batches(data, batch_size):
ret = []
start = 0
end = 0
num_batches = math.ceil(data.shape[0] / batch_size)
for batch_num in range(num_batches):
end += batch_size
if end > data.shape[0]: #last batch
ret.append(data[-batch_size:])
else:
ret.append(data[start:end])
start = end
return np.array(ret)
def run_prediction(session, y, data, x):
for batch in data:
session.run(y, feed_dict={x : batch})
def load_model_cpu_gpu(modelname):
with tf.device('/cpu:0'):
x = tf.placeholder(name='x', dtype=tf.float32, shape=(None, 32,32,3))
with tf.device('/gpu:0'):
model_gpu = load_model(modelname)
gpu = model_gpu(x)
with tf.device('/cpu:0'):
model_cpu = load_model(modelname)
cpu = model_cpu(x)
return cpu, gpu, x
def predict_cpu_gpu(cpu, data_cpu, gpu, data_gpu, x):
with tf.Session(config=tf.ConfigProto(log_device_placement=False, intra_op_parallelism_threads=8)) as sess:
sess.run(tf.global_variables_initializer())
coord = tf.train.Coordinator()
threads = []
# comment out 0 or 1 of the following 2 lines:
threads += [Thread(target=run_prediction, args=(sess, cpu, data_cpu, x))]
threads += [Thread(target=run_prediction, args=(sess, gpu, data_gpu, x))]
t0 = time()
for t in threads:
t.start()
#coord.join(threads)
for t in threads:
t.join()
t1 = time()
return (t1 - t0)
def predict_parallel(modelname, data, batch_sizes = [1], cpu_percentage = [0.5], nruns = 1):
cpu, gpu, x = load_model_cpu_gpu(modelname)
results = []
for batch_size in batch_sizes:
batches = split_in_batches(data, batch_size)
nbatches = len(batches)
for cpu_perc in cpu_percentage:
batches_cpu = int(nbatches * cpu_perc)
data_cpu = batches[:batches_cpu]
data_gpu = batches[batches_cpu:]
tmp_sum = 0
for run in range(nruns):
print ("Running run {0} using {1:.2f}% CPU........".format(run, cpu_perc))
time_spent = predict_cpu_gpu(cpu, data_cpu, gpu, data_gpu, x)
print('{0:.5f}'.format(time_spent))
tmp_sum += time_spent
results.append([batch_size, cpu_perc, run, time_spent])
print ("Average time: {0:.4f}".format(tmp_sum / nruns))
return results
def evaluate_model(modelname):
results = predict_parallel(modelname, x_test, BATCHES, CPU_PERCENTAGES, NRUNS)
results_df = pd.DataFrame(results, columns = ['BATCH_SIZE', 'CPU_PERC', 'RUN', 'TIME'])
basename = os.path.basename(modelname)[:modelname.rfind('.')]
outfolder = os.path.join(RESULTS_FOLDER, basename + '.csv')
results_df.to_csv(outfolder, index=None)
infolder = os.path.join(MODELS_FOLDER, 'model_*')
for model in glob.glob(infolder):
try:
evaluate_model(model)
except Exception as e:
print (e)
import gc; gc.collect()