def eval_throughput(duration, request_url, batch_size=1, dataset='test', use_graphpipe=False, emnist_folder_path='emnist_data/'): """ Performs a Throughput test running sequential queries against webservice counting the number of requests performed within duration Args: duration (int): number of seconds for running test queries request_url (str): URL of model service used for classification batch_size (int): no. of examples per batch dataset (str): `train` or `test` data to pick evaluation examples from use_graphpipe (bool): use graphpipe client to execute queries (uses flatbuffers instead) emnist_folder_path (str): folder containing EMNIST data Returns: num_reqs (int): total number of requests performed reqs_per_second (float): thoughput as the number of requests performed per second """ x_train, _, x_test, _, _ = load_emnist(emnist_folder_path) if dataset == 'train': data = x_train else: data = x_test data = data.reshape(-1, 1, 28 * 28) / 255 num_reqs = 0 _logger.info("Throughput Evaluation on URL {} for {} seconds ...".format( request_url, duration)) if not use_graphpipe: end_time = time.time() + duration while time.time() <= end_time: batch = data[num_reqs:(num_reqs + batch_size)].reshape(-1, 28 * 28) test_img_payload = \ {"instances": batch.tolist()} test_img_payload = json.dumps(test_img_payload) test_img_softmax_pred = requests.post(request_url, data=test_img_payload) test_img_softmax_pred.json()['predictions'] num_reqs += batch_size else: end_time = time.time() + duration while time.time() <= end_time: remote.execute(request_url, data[num_reqs:(num_reqs + batch_size)]) num_reqs += batch_size reqs_per_second = num_reqs / duration _logger.info(("Throughput Summary:\nURL: {}\nDuration: {} [s]\n" + "Total Requests: {}\nRequests/Second: {:.2f}").format( request_url, duration, num_reqs, reqs_per_second)) return num_reqs, reqs_per_second
def predict(self, data): """ Calculate the prediction of the data. Args: data(numpy.ndarray): input data with shape (size, height, width, channels). Return: numpy.ndarray: predictions of the data with shape (batch_size, num_of_classes). """ scaled_data = self._process_input(data) predict = remote.execute(self._remote_url, scaled_data) predict = np.squeeze(predict, axis=0) return predict
def main(image_path): print("image_path:{}".format(image_path)) data = np.array(Image.open(image_path)) data = data.reshape([1] + list(data.shape)) data = np.rollaxis(data, 3, 1).astype(np.float32) # channels first #print(data.shape) pred = remote.execute("http://127.0.0.1:9000", data) print(pred.shape) pred = np.squeeze(pred, axis=0) print(pred.shape) print("{}".format(np.argmax(pred, axis=1)))
def evaluate(boards): x = np.concatenate([parse_board(board)[np.newaxis, :] for board in boards]) weights, values = remote.execute(MODEL, x) for i in range(len(weights)): weight = weights[i] value = values[i][0] move = int(x[i].sum()) player = move % 2 print("Move {}: {} to play".format(move, "O" if player else "X")) print_board(x[i]) print_weights(weight) if player: value = -value if value > 0: print("Evaluation: X wins {:02.0f}%".format(value * 100)) else: print("Evaluation: O wins {:02.0f}%".format(value * -100)) print()
def predict(self, formatted_data, batch_size=None): y = remote.execute(self.graph_pipe_url, formatted_data) self.result = y
from io import BytesIO from PIL import Image, ImageOps import numpy as np import requests from graphpipe import remote data = np.array(Image.open("mug227.png")) data = data.reshape([1] + list(data.shape)) data = np.rollaxis(data, 3, 1).astype(np.float32) # channels first print(data.shape) pred = remote.execute("http://127.0.0.1:9000", data) print("Expected 504 (Coffee mug), got: %s", np.argmax(pred, axis=1))
#!/usr/bin/env python """ Serves a simple model with model_server.py and makes a request to it """ import subprocess import time import numpy as np from graphpipe import remote # create a simple protobuf model that squares its input process = subprocess.Popen(["./tf_graph.py"]) process.wait() port = "4242" process = subprocess.Popen( ["./model_server.py", "--port", port, "--model", "tf_graph.pb"]) # make sure the process has time to start time.sleep(3) # send a request x = np.array(0.42) y = remote.execute("http://127.0.0.1:" + port, x) # print the response print(y) # kill the server process.kill()
import argparse import numpy as np from graphpipe import remote SHAPE = (10, 1, 2, 3, 4) def get_sample_data(): return np.random.rand(*SHAPE) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument("--url", default="http://127.0.0.1:10000", help="Url", type=str) args = parser.parse_args() X = get_sample_data() pred = remote.execute(args.url, X) assert (pred.shape == SHAPE) assert (np.allclose(X, pred)) print('Hooray! We got our data back with shape: %s' % str(SHAPE))
def eval_serving_performance(n_examples, n_print_examples, request_url, seed=42, dataset='test', use_graphpipe=False, emnist_folder_path='emnist_data/'): """ Fires queries against a model service, evaluates the result in terms of classification accuracy and processing time, and prints examples Args: n_examples (int): number of queries n_print_examples (int): number of examples to print img and classification request_url (str): URL of model service used for classification seed (int): seed for NumPy random number generator dataset (str): `train` or `test` data to pick evaluation examples from use_graphpipe (bool): use graphpipe client to execute queries (uses flatbuffers instead) emnist_folder_path (str): folder containing EMNIST data Returns: durations ([int]): list of ms every request took """ x_train, y_train, x_test, y_test, _ = load_emnist(emnist_folder_path) mapping = get_emnist_mapping() acc = 0 durations = [] if dataset == 'train': x_eval, y_eval = x_train, y_train else: x_eval, y_eval = x_test, y_test np.random.seed(seed) eval_img_indices = np.random.choice(np.arange(x_eval.shape[0]), n_examples, replace=False) for idx, test_img_idx in enumerate(eval_img_indices): test_img_flatten = x_eval[test_img_idx].reshape(1, 784) / 255 if not use_graphpipe: start = time.time() test_img_payload = {'instances': test_img_flatten.tolist()} test_img_payload = json.dumps(test_img_payload) test_img_softmax_pred = requests.post(request_url, data=test_img_payload) test_img_softmax_pred = test_img_softmax_pred.json()['predictions'] else: start = time.time() test_img_softmax_pred = remote.execute(request_url, test_img_flatten) durations.append(int((time.time() - start) * 1000000) / 1000) test_img_class_pred = np.argmax(test_img_softmax_pred) acc += (test_img_class_pred == y_eval[test_img_idx][0]) # print the first 10 images with their true and predicted label if idx < n_print_examples: show_img(test_img_idx, x_train, y_train, x_test, y_test, mapping, mode=dataset) print("Predicted Label: {}".format(mapping[test_img_class_pred])) print("Accuracy on {} test images: {:.2%}".format(n_examples, acc / n_examples)) return durations
} keypoints_symmetry = metadata['keypoints_symmetry'] kps_left, kps_right = list(keypoints_symmetry[0]), list(keypoints_symmetry[1]) joints_left, joints_right = list([4, 5, 6, 11, 12, 13]), list([1, 2, 3, 14, 15, 16]) pad = (243 - 1) // 2 batch_2d = np.expand_dims(np.pad(data, ((pad, pad), (0, 0), (0, 0)), 'edge'), axis=0) batch_2d = np.concatenate((batch_2d, batch_2d), axis=0) batch_2d[1, :, :, 0] *= -1 batch_2d[1, :, kps_left + kps_right] = batch_2d[1, :, kps_right + kps_left] batch_2d = batch_2d.astype(np.float32) # link server predicted_3d_pos = remote.execute("http://127.0.0.1:9000", batch_2d) # post processing predicted_3d_pos = np.copy(predicted_3d_pos) predicted_3d_pos[1, :, :, 0] *= -1 predicted_3d_pos[1, :, joints_left + joints_right] = predicted_3d_pos[1, :, joints_right + joints_left] prediction = np.mean(predicted_3d_pos, axis=0, keepdims=True)[0] rot = params.rot() prediction = camera_to_world(prediction, R=rot, t=0) # We don't have the trajectory, but at least we can rebase the height prediction[:, :, 2] -= np.min(prediction[:, :, 2]) anim_output = {'Reconstruction': prediction}