if __name__ == "__main__": model = MaskRCNN(mode="inference", model_dir="None", config=MyConfig()) # Raise the maximum message size to 100MB max_message_size = 100 * 1024 * 1024 options = [ ("grpc.max_message_length", max_message_size), ("grpc.max_receive_message_length", max_message_size), ] channel = grpc.insecure_channel("127.0.0.1:8500", options=options) stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) image_data = imageio.imread("data/155806.jpg") molded_images, image_metas, windows = model.mold_inputs([image_data]) image_shape = molded_images[0].shape anchors = model.get_anchors(image_shape) anchors = np.broadcast_to(anchors, (model.config.BATCH_SIZE, ) + anchors.shape) req = predict_pb2.PredictRequest() req.model_spec.name = "maskrcnn" req.model_spec.signature_name = "serving_default" req.inputs["input_image:0"].CopyFrom(make_tensor_proto(molded_images)) req.inputs["input_anchors:0"].CopyFrom(make_tensor_proto(anchors)) req.inputs["input_image_meta:0"].CopyFrom(make_tensor_proto(image_metas)) start_time = time.time() result_future = stub.Predict.future(req, 5.0)
class ImageRCNN: ''' ''' def __init__(self, networkpath, modelpath): self._config = InferenceConfig() # config.display() self._model = MaskRCNN(mode="inference", model_dir=modelpath, config=self._config) # Load trained weights self._model.load_weights(networkpath, by_name=True) def write_summary(self, filename): with open(filename, "w") as f: stdout = sys.stdout sys.stdout = f self._model.keras_model.summary() sys.stdout = stdout def get_model_inputs(self, loaded_images: list): # images are recast to standardized shapes molded_images, image_metas, windows = self._model.mold_inputs( loaded_images) # at this point, molded images are reshaped and ready to use in the neural net image_shape = molded_images[0].shape anchors = self._model.get_anchors(image_shape) anchors = np.broadcast_to(anchors, (len(loaded_images), ) + anchors.shape) return [molded_images, image_metas, anchors, windows] def unmold_predictions(self, detections, mrcnn_mask, original_image_shape, image_shape, window): ''' copied from mrcnn, changes in loop allows return of probability map ''' # Detections array is padded with zeros. Find the first class_id == 0. zero_ix = np.where(detections[:, 4] == 0)[0] N = zero_ix[0] if zero_ix.shape[0] > 0 else detections.shape[0] # Extract boxes, class_ids, scores, and class-specific masks boxes = detections[:N, :4] class_ids = detections[:N, 4].astype(np.int32) scores = detections[:N, 5] masks = mrcnn_mask[np.arange(N), :, :, class_ids] # Translate normalized coordinates in the resized image to pixel # coordinates in the original image before resizing window = utils.norm_boxes(window, image_shape[:2]) wy1, wx1, wy2, wx2 = window shift = np.array([wy1, wx1, wy1, wx1]) wh = wy2 - wy1 # window height ww = wx2 - wx1 # window width scale = np.array([wh, ww, wh, ww]) # Convert boxes to normalized coordinates on the window boxes = np.divide(boxes - shift, scale) # Convert boxes to pixel coordinates on the original image boxes = utils.denorm_boxes(boxes, original_image_shape[:2]) # Filter out detections with zero area. Happens in early training when # network weights are still random exclude_ix = np.where((boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1]) <= 0)[0] if exclude_ix.shape[0] > 0: boxes = np.delete(boxes, exclude_ix, axis=0) class_ids = np.delete(class_ids, exclude_ix, axis=0) scores = np.delete(scores, exclude_ix, axis=0) masks = np.delete(masks, exclude_ix, axis=0) N = class_ids.shape[0] # Resize masks to original image size and set boundary threshold. full_masks = [] full_pmap = [] for i in range(N): # Convert neural network mask to full size mask threshold = 0.5 y1, x1, y2, x2 = boxes[i] full_pmap.append( skimage.transform.resize(masks[i], (y2 - y1, x2 - x1), order=1, mode="constant")) mask = np.where(full_pmap[i] >= threshold, 1, 0).astype(np.bool) # Put the mask in the right location. full_mask = np.zeros(image_shape[:2], dtype=np.bool) full_mask[y1:y2, x1:x2] = mask # full_mask = utils.unmold_mask(masks[i], boxes[i], original_image_shape) full_masks.append(full_mask) full_masks = np.stack(full_masks, axis=-1)\ if full_masks else np.empty(original_image_shape[:2] + (0,)) return boxes, class_ids, scores, full_masks, full_pmap def get_results(self, loaded_images): ''' mold image, process molds in neural net, unmold and return outputs ''' molded_images, image_metas, windows = self._model.mold_inputs( loaded_images) image_shape = molded_images[0].shape anchors = self._model.get_anchors(image_shape) anchors = np.broadcast_to(anchors, (self._model.config.BATCH_SIZE, ) + anchors.shape) detections, _, _, mrcnn_mask, _, _, _ =\ self._model.keras_model.predict([molded_images, image_metas, anchors], verbose=0) results = [] for i, image in enumerate(loaded_images): final_rois, final_class_ids, final_scores, final_masks, final_pmaps =\ self.unmold_predictions(detections[i], mrcnn_mask[i], image.shape, molded_images[i].shape, windows[i]) results.append({ "rois": final_rois, "class_ids": final_class_ids, "scores": final_scores, "masks": final_masks, "pmaps": final_pmaps }) return results