def test_read(self): input1 = mx.nd.random.uniform(0, 255, shape=(3, 256, 256)) input_buf1 = self._write_image(input1) output1 = image.read(input_buf1) assert output1.shape == (256, 256, 3), "Read method failed. Got %s shape." % (str(output1.shape)) input2 = mx.nd.random.uniform(0, 255, shape=(1, 128, 128)) input_buf2 = self._write_image(input2, flag=0) output2 = image.read(input_buf2, flag=0) assert output2.shape == (128, 128, 1), "Read method failed. Got %s shape." % (str(output2.shape))
def _preprocess(self, data): img_list = [] for idx, img in enumerate(data): input_shape = self.signature['inputs'][idx]['data_shape'] # We are assuming input shape is NCHW [h, w] = input_shape[2:] if input_shape[1] == 1: img_arr = image.read(img, 0) else: img_arr = image.read(img) img_arr = image.resize(img_arr, w, h) img_arr = image.transform_shape(img_arr) img_list.append(img_arr) return img_list
def _preprocess(self, data): input_shape = self.signature['inputs'][0]['data_shape'] height, width = input_shape[2:] img_arr = image.read(data[0]) img_arr = image.resize(img_arr, width, height) img_arr = image.color_normalize(img_arr, nd.array([127.5]), nd.array([127.5])) img_arr = image.transform_shape(img_arr) return [img_arr]
def _preprocess(self, data): img_list = [] for idx, img in enumerate(data): input_shape = self.signature['inputs'][idx]['data_shape'] # We are assuming input shape is NCHW [h, w] = input_shape[2:] if input_shape[1] == 1: img_arr = image.read(img, 0) else: img_arr = image.read(img) img_arr = image.resize(img_arr, w, h) rgb_mean = mx.nd.array([123.68, 116.779, 103.939]) rgb_std = mx.nd.array([58.395, 57.12, 57.375]) img_arr = img_arr.astype('float32') img_arr = mx.image.color_normalize(img_arr, mean=rgb_mean, std=rgb_std) img_arr = mx.nd.reshape(img_arr, (w, h, input_shape[1])) img_arr = image.transform_shape(img_arr) img_list.append(img_arr) return img_list
def _preprocess(self, data): img_list = [] for idx, img in enumerate(data): input_shape = self.signature['inputs'][idx][ 'data_shape'] # Input shape is NCHW [h, w] = input_shape[2:] img_arr = image.read( img, 0) #Set flag to 0 for reading grayscale images img_arr = image.resize(img_arr, w, h) img_arr = image.transform_shape(img_arr) img_list.append(img_arr) return img_list
def preprocess(self, request): """ Pre-process requests by attempting to extract face image, and transforming to fit the model's input Returns ------- list of NDArray Processed images in the model's expected input shape """ img_list = [] input_shape = self.signature['inputs'][0]['data_shape'] [height, width] = input_shape[2:] param_name = self.signature['inputs'][0]['data_name'] # Iterate over all input images provided with the request, transform and append for inference for idx, data in enumerate(request): # Extract the input image img = data.get(param_name) if img is None: img = data.get("body") if img is None: img = data.get("data") if img is None or len(img) == 0: self.error = "Empty image input" return None try: img_arr = image.read(img).asnumpy() except Exception as e: logging.warning(e, exc_info=True) self.error = "Corrupted image input" return None # Try to identify face to crop face = crop_face(img_arr) if face is not None: face = transform.resize(face, (height, width)) # If no face identified - use the entire input image else: face = cv.cvtColor(img_arr, cv.COLOR_BGR2GRAY) # Transform image into tensor of the required shape face = np.resize(face, input_shape) face = normalize(face, height, width) face = mx.nd.array(face) img_list.append(face) return img_list
def _preprocess(self, data): """ Input image buffer from data is read into NDArray. Then, resized to expected shape. Swaps axes to convert image from BGR format to RGB. Returns the preprocessed NDArray as a list for next step, Inference. """ # Read input input_image = image.read(data[0]) # Save original input image shape. # This is required for preparing the bounding box of the detected object relative to # original input self.input_height = input_image.shape[0] self.input_width = input_image.shape[1] # Transform input image - resize, BGR to RGB. # Reuse MXNetVisionService _preprocess to achieve above transformations. return super(SSDService, self)._preprocess(data)
def _preprocess(self, data): """ Input image buffer from data is read into NDArray. Then, resized to expected shape. Swaps axes to convert image from BGR format to RGB. Returns the preprocessed NDArray as a list for next step, Inference. """ # Read input input_image = image.read(data[0]) # Save original input image shape. # This is required for preparing the bounding box of the detected object relative to # original input self.input_height = input_image.shape[0] self.input_width = input_image.shape[1] # Transform input image - resize, BGR to RGB. # Reuse MXNetVisionService _preprocess to achieve above transformations. return super(SSDService, self)._preprocess(data)
def _preprocess(self, data): img_list = [] for idx, img in enumerate(data): input_shape = self.signature['inputs'][idx]['data_shape'] # We are assuming input shape is NCHW img_arr = image.read(img) img_arr, im_scale = rcnn_resize(img_arr, SHORTER_SIZE, MAX_SIZE, stride=IMAGE_STRIDE) rgb_mean = mx.nd.array([[[0, 0, 0]]]) img_arr = img_arr.astype('float32') img_arr = img_arr - rgb_mean img_arr = image.transform_shape(img_arr) img_list.append(img_arr) im_info = [[img_arr.shape[2], img_arr.shape[3], im_scale]] self.scale = im_scale img_list.append(mx.nd.array(im_info)) return img_list