def main(_): t1 = t.time() host, port = FLAGS.server.split(':') channel = grpc.insecure_channel(FLAGS.server) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) # request = predict_pb2.PredictRequest() predict = [] image_data = img_data(image_path) imag_data = [] for i in range(len(image_data)): imag_data.append(image_data[i].numpy().tolist()) t2 = t.time() for h in range(0, 5): request = model_name(h) t3 = t.time() # Build a batch of images. request.inputs['input_1'].CopyFrom( tf.make_tensor_proto(imag_data, shape=[len(image_data), 224, 224, 3])) result_future = stub.Predict.future(request, 10.0) # 10 secs timeout result = result_future.result() t4 = t.time() classes = result.outputs['output_1'].float_val prediction = [] for i in range(0, 80): prediction.append(classes[i]) if len(prediction) % 16 == 0: classification = np.argmax(prediction) predict.append(classification) prediction = [] print(predict) fine_label = get_label(max(predict, key=predict.count)) print(fine_label, t4 - t2, t2 - t1, t4 - t1)
def prediction(): t1 = t.time() predict = [] img = img_data(image_path) # print(img[1]) print(t.time() - t1) t2 = t.time() for a in range(0, 1): SERVER_URL = url(a) for i in range(0, 5): j = json.dumps(img[i].numpy().tolist()) predict_request = '{"instances":%s}' % j # print(t.time()-t1) # print(predict_request) start_time = t.time() response = requests.post(SERVER_URL, data=predict_request) print(t.time() - start_time) # print(type(response)) # t2=t.time() prediction = response.json()['predictions'][0] # print(t.time()-t2) classification = np.argmax(prediction) # print(t.time()-t2) predict.append(classification) # print(t.time()-t2) print(predict) fine_label = get_label(max(predict, key=predict.count)) print(t.time() - t1, t.time() - t2) print(fine_label)
def main(): # Build a batch of images. image_data = img_data(image_path) imag_data = [] for i in range(len(image_data)): imag_data.append(image_data[i].numpy().tolist()) with tf.io.TFRecordWriter("tf_serving_warmup_requests") as writer: request = predict_pb2.PredictRequest() request.model_spec.name = 'model1' request.model_spec.signature_name = 'serving_default' request.inputs['input_1'].CopyFrom( tf.make_tensor_proto(imag_data, shape=[len(imag_data),224,224,3])) log = prediction_log_pb2.PredictionLog( predict_log=prediction_log_pb2.PredictLog(request=request)) writer.write(log.SerializeToString())
# GPU settings gpus = tf.config.experimental.list_physical_devices('GPU') if gpus: for gpu in gpus: tf.config.experimental.set_memory_growth(gpu, True) t1=t.time() # load the model # model = get_model() # model.load_weights(save_model_dir) model = tf.saved_model.load(save_model_dir) #image data predict=[] probabilit=[] img=img_data(image_path) #Take the category with the most predicted results # for i in range(len(img)): # image=tf.expand_dims(img[i],0) # predictions = model(image, training=False) # probabilities=tf.nn.softmax(predictions) # label = np.argmax(predictions,1) # probability=np.max(tf.keras.backend.eval((tf.math.top_k(probabilities,1)).values)) # predict.append(int(label)) # print(max(predict, key=predict.count)) # print('time:',(t.time()-t1))