|
- import base64
-
- import cv2
- import numpy as np
- import tensorflow as tf
-
- from libs.configs import cfgs
- from libs.networks import build_whole_network_batch
-
-
- class inference_service(object):
- """
- Model template. You can load your model parameters in __init__ from a location accessible at runtime
- """
-
- def __init__(self, **kargs):
- """
- Add any initialization parameters. These will be passed at runtime from the graph definition parameters defined in your seldondeployment kubernetes resource manifest.
- """
- print("Initializing")
- self.loaded = False
- self.net_name = kargs.get("net_name", "resnet_v1_50")
- self.model_path = kargs.get("model_path", "/workspace/inference_model")
- self.dataset_name = kargs.get("dataset_name", "rongyuan")
-
- def max_length_limitation(self, length, length_limitation):
- return tf.cond(
- tf.less(length, length_limitation),
- true_fn=lambda: length,
- false_fn=lambda: length_limitation,
- )
-
- def short_side_resize_for_inference_data(
- self, img_tensor, target_shortside_len, length_limitation=1200
- ):
- img_h, img_w = tf.shape(img_tensor)[0], tf.shape(img_tensor)[1]
-
- new_h, new_w = tf.cond(
- tf.less(img_h, img_w),
- true_fn=lambda: (
- target_shortside_len,
- self.max_length_limitation(
- target_shortside_len * img_w // img_h, length_limitation
- ),
- ),
- false_fn=lambda: (
- self.max_length_limitation(
- target_shortside_len * img_h // img_w, length_limitation
- ),
- target_shortside_len,
- ),
- )
-
- img_tensor = tf.expand_dims(img_tensor, axis=0)
- img_tensor = tf.image.resize_bilinear(img_tensor, [new_h, new_w])
-
- img_tensor = tf.squeeze(img_tensor, axis=0) # ensure image tensor rank is 3
- return img_tensor
-
- def predict(self, X, features_names):
- """
- Return a prediction.
-
- Parameters
- ----------
- X : array-like
- feature_names : array of feature names (optional)
- """
- print("Predict called - will run identity function")
- if not self.loaded:
- config = tf.ConfigProto()
- config.gpu_options.allow_growth = True
- self.sess = tf.Session(config=config)
- self.img_plac = tf.placeholder(
- dtype=tf.uint8, shape=[None, None, 3]
- ) # is RGB. not GBR
- self.img_batch = tf.cast(self.img_plac, tf.float32)
- self.img_batch = self.short_side_resize_for_inference_data(
- img_tensor=self.img_batch,
- target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN,
- length_limitation=cfgs.IMG_MAX_LENGTH,
- )
- if self.net_name in ["resnet152_v1d", "resnet101_v1d", "resnet50_v1d"]:
- self.img_batch = (
- self.img_batch / 255 - tf.constant(cfgs.PIXEL_MEAN_)
- ) / tf.constant(cfgs.PIXEL_STD)
- else:
- self.img_batch = self.img_batch - tf.constant(cfgs.PIXEL_MEAN)
- self.img_batch = tf.expand_dims(
- self.img_batch, axis=0
- ) # [1, None, None, 3]
-
- det_net = build_whole_network_batch.DetectionNetwork(
- base_network_name=self.net_name,
- dataset_name=self.dataset_name,
- is_training=False,
- batch_size=1,
- )
- (
- self.detection_boxes,
- self.detection_scores,
- self.detection_category,
- ) = det_net.build_whole_detection_network(
- input_img_batch=self.img_batch, gtboxes_batch=None
- )
-
- init_op = tf.group(
- tf.global_variables_initializer(), tf.local_variables_initializer()
- )
- self.sess.run(init_op)
-
- restorer, restore_ckpt = det_net.get_restorer(self.model_path)
- if not restorer is None:
- restorer.restore(self.sess, restore_ckpt)
- print("restore model")
- self.loaded = True
-
- input = base64.b64decode(X.encode("ascii"))
- nparr = np.fromstring(input, np.uint8)
- raw_img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
- (
- resized_img,
- detected_boxes,
- detected_scores,
- detected_categories,
- ) = self.sess.run(
- [
- self.img_batch,
- self.detection_boxes,
- self.detection_scores,
- self.detection_category,
- ],
- feed_dict={self.img_plac: raw_img[:, :, ::-1]}, # cv is BGR. But need RGB
- )
- show_indices = detected_scores >= cfgs.VIS_SCORE
- show_scores = detected_scores[show_indices]
- show_boxes = detected_boxes[show_indices]
- show_categories = detected_categories[show_indices]
- show_categories = list(
- map(lambda x: self.dataset_name, show_categories.tolist())
- )
- h = raw_img.shape[0]
- w = raw_img.shape[1]
- resized_h, resized_w = resized_img.shape[1], resized_img.shape[2]
- show_boxes = list(
- map(
- lambda x: (
- x[0] * w / resized_w,
- x[1] * h / resized_h,
- (x[2] - x[0]) * w / resized_w,
- (x[3] - x[1]) * h / resized_h,
- ),
- show_boxes.tolist(),
- )
- )
- ret = {
- "dscores": show_scores.tolist(),
- "dobjects": show_boxes,
- "dclasses": show_categories,
- }
- print(ret)
- return ret
-
-
- if __name__ == "__main__":
- service = inference_service()
- with open("/workspace/dataset/public/rongyuan/n01629819_608.JPEG", "rb") as f:
- img = f.read()
- base64_bytes = base64.b64encode(img).decode("ascii")
- print(service.predict(base64_bytes, []))
|