def plot_segmentation( im_or_path: Union[np.ndarray, Union[str, Path]], pred_mask: Union[np.ndarray, Union[str, Path]], pred_scores: np.ndarray, gt_mask_or_path: Union[np.ndarray, Union[str, Path]] = None, show: bool = True, figsize: Tuple[int, int] = (16, 4), cmap: ListedColormap = cm.get_cmap("Set3"), ignore_background_label = True ) -> None: """ Plot an image, its predicted mask with associated scores, and optionally the ground truth mask. Args: im_or_path: image or path to image pred_mask: predicted mask pred_scores: pixel-wise confidence scores in the predictions gt_mask_or_path: ground truth mask or path to mask show: set to true to call matplotlib's show() figsize: figure size cmap: mask color map. ignore_background_label: set to True to ignore the 0 label. """ im = load_im(im_or_path) pred_mask = pil2tensor(pred_mask, np.float32) if ignore_background_label: start_label = 1 else: start_label = 0 max_scores = np.max(np.array(pred_scores[start_label:]), axis=0) max_scores = pil2tensor(max_scores, np.float32) # Plot groud truth mask if provided if gt_mask_or_path: fig, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4, figsize=figsize) gt_mask = load_mask(gt_mask_or_path) show_image(gt_mask, ax=ax4, cmap=cmap) ax4.set_title("Ground truth mask") else: fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=figsize) # Plot image, predicted mask, and prediction scores show_image(im, ax=ax1) show_image(pred_mask, ax=ax2, cmap=cmap) show_image(max_scores, ax=ax3, cmap=cm.get_cmap("gist_heat")) ax1.set_title("Image") ax2.set_title("Predicted mask") ax3.set_title("Predicted scores") if show: plt.show()
def open(self, obj_fn, part_fn): obj = self.get_obj_mask(obj_fn) part = self.get_part_mask(part_fn) if part_fn else None # self.get_part_mask might return None if part is None: part = np.zeros_like(obj) obj = self.obj_mapping[obj] part = self.part_mapping[part] if self.to_tensor: obj = fv.pil2tensor(obj, np.float32) part = fv.pil2tensor(part, np.float32) return obj, part
def handle_aws_lambda_event(self, event, func): if event["headers"].get("Content-Type", "").startswith("images/"): # decodebytes introduced at python3.1 try: image_data = imread(base64.decodebytes(event["body"]), pilmode=self.pilmode) except AttributeError: image_data = imread( base64.decodestring(event["body"]), # pylint: disable=W1505 pilmode=self.convert_mode, ) else: raise BentoMLException( "BentoML currently doesn't support Content-Type: {content_type} for " "AWS Lambda".format( content_type=event["headers"]["Content-Type"])) if self.after_open: image_data = self.after_open(image_data) image_data = pil2tensor(image_data, np.float32) if self.div: image_data = image_data.div_(255) if self.cls: image_data = self.cls(image_data) else: image_data = Image(image_data) result = func(image_data) result = get_output_str(result, event["headers"].get("output", "json")) return {"statusCode": 200, "body": result}
def open(self, fn): """TODO""" rgb_nir_swir = ["B04", "B03", "B02", "B08", "B11", "B12"] indices = [rgb_nir_swir.index(band) for band in self.bands] data = np.load(fn)[:, :, indices] data = pil2tensor(data, np.float32) return Image(data)
def contrast_and_crop(torch_tensor, image_dim=None, path=None, sigmaX=10, median_blur=True): if path is None: np_image = image2np( torch_tensor) * 255 # convert tensor image to numpy array np_image = np_image.astype(np.uint8) else: np_image = cv2.imread( path) # may return a string, in which case treat it as a path image = cv2.cvtColor(np_image, cv2.COLOR_BGR2RGB) # image = cv2.resize(image, (image_dim, image_dim)) image = resize_image(image, image_dim) if median_blur: k = np.max(image.shape) // 20 * 2 + 1 blur = cv2.medianBlur(image, k) else: blur = cv2.GaussianBlur(image, (0, 0), sigmaX) image = cv2.addWeighted(image, 4, blur, -4, 128) image = Radius_Reduction(image, PARAM) return pil2tensor(image, np.float32).div_(255) # return tensor
async def analyze(request): data = await request.body() instances = json.loads(data.decode('utf-8'))['instances'] # convert from image bytes to images to tensors img_bytes = [b64decode(inst['image_bytes']['b64']) for inst in instances] tensors = [ pil2tensor(Image.open(BytesIO(byts)), dtype=np.float32).div_(255) for byts in img_bytes ] tfm_tensors = [ learner.data.valid_dl.tfms[0]((tensor, torch.zeros(0)))[0] for tensor in tensors ] # batch predict, dummy labels for the second argument dummy_labels = torch.zeros(len(tfm_tensors)) tensor_stack = torch.stack(tfm_tensors) if torch.cuda.is_available(): tensor_stack = tensor_stack.cuda() pred_tensor = learner.pred_batch(batch=(tensor_stack, dummy_labels)) # find the maximum value along the prediction axis classes = np.argmax(np.array(pred_tensor), axis=1) return JSONResponse(dict(predictions=classes.tolist()))
def get_prediction_image(path, sz=256): bgr_img = imread(path) b, g, r = split(bgr_img) rgb_img = merge([r, g, b]) rgb_img = rgb_img / 255.0 img = Image(px=pil2tensor(rgb_img, np.float32)) img = img.resize((3, sz, sz)) return img.px.reshape(1, 3, sz, sz)
def __init__(self, img, model, path_to_learner="./models"): self.path_to_learner = path_to_learner self.learn = load_learner(path=self.path_to_learner, file=model) "Convert np.ndarray to torch style Image" self._img = np.copy(img) self.img = Image(pil2tensor(img, np.float32).div_(255))
def handle_request(self, request, func): try: from fastai.vision import Image, pil2tensor except ImportError: raise ImportError( "fastai package is required to use FastaiImageHandler") try: from imageio import imread except ImportError: raise ImportError( "imageio package is required to use FastaiImageHandler") if request.method != "POST": return Response(response="Only accept POST request", status=400) input_streams = [] for filename in self.input_names: file = request.files.get(filename) if file is not None: file_name = secure_filename(file.filename) check_file_format(file_name, self.accept_file_extensions) input_streams.append(BytesIO(file.read())) if len(input_streams) == 0: if request.data: input_streams = (request.data, ) else: raise ValueError( "BentoML#ImageHandler unexpected HTTP request: %s" % request) input_data = [] for input_stream in input_streams: data = imread(input_stream, pilmode=self.convert_mode) if self.after_open: data = self.after_open(data) data = pil2tensor(data, np.float32) if self.div: data = data.div_(255) if self.cls: data = self.cls(data) else: data = Image(data) input_data.append(data) result = func(*input_data) result = get_output_str(result, request.headers.get("output", "json")) return Response(response=result, status=200, mimetype="application/json")
def convert_to_fastai(frame): """ Makes an opencv::mat image into a fastai compatible image. """ swapped_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) img_fastai = Image(pil2tensor(swapped_image, dtype=np.float32).div_(255)) # using Umat. I found it to be slow, but I'm leaving it here for posterity. # mat_image = cv2.UMat.get(swapped_image) # img_fastai = Image(pil2tensor(mat_image, dtype=np.float32).div_(255)) return img_fastai
def custom_open_mask(filename, div=False, convert_mode='L', after_open=None): x = Image.open(filename).convert('RGBA') if after_open: x = after_open(x) x = pil2tensor(x, np.float32) return ImageSegment(x)
def open(self, fn): """TODO""" patch = Path(fn).name paths = [f"{fn}/{patch}_{band}.tif" for band in self.bands] bands = [ np.array(PIL.Image.open(path).resize((120, 120))) for path in paths ] data = np.dstack(bands) if self.scale: data = np.clip(data / 2750, 0, 1) data = pil2tensor(data, np.float32) return Image(data)
def open_dcm_image(file_name, *args, **kwargs) -> Image: arr = read_HU_array(file_name) windowed_arrays = [] for window_min, window_max in conf[WINDOWS]: array = np.clip(arr, a_min=window_min, a_max=window_max) array = _normalize(array, window_min, window_max) windowed_arrays.append(array) final_array = np.dstack(windowed_arrays) return Image(pil2tensor(final_array, np.float32).div_(255))
def open_im_by_id(ID, ROOT=ROOT, sufx=suffixes): """ ID : Base name for each sample ROOT : Folder containing all samples sufx : color suffixes for samples """ imnames = [ROOT / ID / (ID + o) for o in sufx] # Generating file names for given image ID imgs = [cv2.imread(str(o), cv2.IMREAD_GRAYSCALE) for o in imnames] # List of 4 channels of given file ID imgs = np.stack(imgs, 2) return Image(pil2tensor( imgs, np.float32).float()) # Creating a Fastai Image object from the image
def api_batch_predict(payload): instances = json.loads(payload)['instances'] img_bytes = [b64decode(inst['image_bytes']['b64']) for inst in instances] tensors = [ pil2tensor(Image.open(BytesIO(byts)), dtype=np.float32).div_(255) for byts in img_bytes ] # batch predict, dummy labels for the second argument dummy_labels = torch.zeros(len(tensors)) tensor_stack = torch.stack(tensors) if torch.cuda.is_available(): tensor_stack = tensor_stack.cuda() learner.pred_batch(batch=(tensor_stack, dummy_labels))
def open_dcm_image(fn, *args, **kwargs) -> Image: window_min = -100 window_max = 100 array = pydicom.dcmread(fn).pixel_array array = np.clip(array, a_min=window_min, a_max=window_max) array = ((array - window_min) / (window_max - window_min) * (255 - 0) + 0).astype(np.uint8) array = cv2.equalizeHist(array.astype(np.uint8)) array = np.repeat(array[:, :, None], 3, axis=2) # we can store images in this format :top: to make stuff faster... return Image(pil2tensor(array, np.float32).div_(255))
def transform_and_predict(input_bytes): image_data = cv2.imdecode(input_bytes, cv2.IMREAD_COLOR) if self.after_open: image_data = self.after_open(image_data) image_data = pil2tensor(image_data, np.float32) if self.div: image_data = image_data.div_(255) if self.cls: image_data = self.cls(image_data) else: image_data = Image(image_data) return func(image_data)
def open_im_by_folder(FOLDER, sufx=suffixes): """ FOLDER : Folder containing sample files sufx : color suffixes for samples """ FOLDER = Path(FOLDER) fnames = sorted(FOLDER.ls()) fnames = [fnames[2], fnames[1], fnames[0], fnames[3]] # Sorting according to RGBY from BGRY imgs = [cv2.imread(str(o), cv2.IMREAD_GRAYSCALE) for o in fnames] # List of 4 channels of given file ID imgs = np.stack(imgs, 2) return Image(pil2tensor( imgs, np.float32).float()) # Creating a Fastai Image object from the image
def predict_single_slice(self, data): """Takes in a 2d data array and returns the max and argmax of the predicted probabilities. Args: data (numpy.array): The 2d data array to be fed into the U-net. Returns: torch.tensor: A 3d torch tensor containing a 2d array with max probabilities and a 2d array with argmax indices. """ data = img_as_float(data) data = Image(pil2tensor(data, dtype=np.float32)) self.fix_odd_sides(data) prediction = self.model.predict(data)[2] return torch.max(prediction, dim=0)
def predict_single_slice(self, axis, index, data, output_path): """Takes in a 2d data array and saves the predicted U-net segmentation to disk. Args: axis (str): The name of the axis to incorporate in the output filename. index (int): The slice number to incorporate in the output filename. data (numpy.array): The 2d data array to be fed into the U-net. output_path (pathlib.Path): The path to directory for file output. """ data = img_as_float(data) img = Image(pil2tensor(data, dtype=np.float32)) self.fix_odd_sides(img) prediction = self.predictor.model.predict(img) pred_slice = img_as_ubyte(prediction[1][0]) io.imsave(output_path / f"unet_prediction_{axis}_stack_{index}.png", pred_slice)
def handle_request(self, request, func): if request.method != "POST": return Response(response="Only accept POST request", status=400) input_streams = [] for filename in self.input_names: file = request.files.get(filename) if file is not None: file_name = secure_filename(file.filename) verify_image_format_or_raise(file_name, self.accept_image_formats) input_streams.append(BytesIO(file.read())) if len(input_streams) == 0: data = request.get_data() if data: input_streams = (data, ) else: raise ValueError( "BentoML#ImageHandler unexpected HTTP request: %s" % request) input_data = [] for input_stream in input_streams: data = imread(input_stream, pilmode=self.convert_mode) if self.after_open: data = self.after_open(data) data = pil2tensor(data, np.float32) if self.div: data = data.div_(255) if self.cls: data = self.cls(data) else: data = Image(data) input_data.append(data) result = func(*input_data) result = get_output_str(result, request.headers.get("output", "json")) return Response(response=result, status=200, mimetype="application/json")
def prediction(f_buff): learner = create_learner() img = PIL.Image.open(f_buff) st.sidebar.image(img.resize((300, 300)), caption="Uploaded Image") st.write('Detecting faces...') pix = np.asarray(img) output = pred(pix) boxes = output['instances'].get_fields()['pred_boxes'].tensor.to( 'cpu').numpy() flag = 1 for (x1, y1, x2, y2) in boxes: face_img = pix[int(y1):int(y2), int(x1):int(x2)] img_t = pil2tensor(face_img, np.float32) img_t.div_(255.0) image = Image(img_t) label = learner.predict(image)[0] st.write(f'{len(boxes)} Face Detected') #uncomment for # v = Visualizer(pix[:,:,::-1], # scale=0.5, # instance_mode=ColorMode.IMAGE_BW) # out = v.draw_instance_predictions(output['instances'].to('cpu')) # st.image(out.get_image()[:,:,::-1],caption="Detectron2 visualization") st.image(face_img, caption="faces detected in image") st.write(f''' ## You look {label} years old.''') st.write(f"Am I correct??") if st.button('yes'): st.write(''' ## Thank you for your feedback ''') if st.button('No'): st.write(''' ## Oops! My Bad.. ''') flag = 0 if flag == 1: st.write('face not detected')
def handle_request(self, request, func): try: from fastai.vision import Image, pil2tensor import numpy as np except ImportError: raise ImportError( "fastai package is required to use FastaiImageHandler") try: from imageio import imread except ImportError: raise ImportError( "imageio package is required to use FastaiImageHandler") return if request.method != "POST": return Response(response="Only accept POST request", status=400) input_file = request.files.get(self.input_name) file_name = secure_filename(input_file.filename) check_file_format(file_name, self.accept_file_extensions) input_stream = BytesIO(input_file.read()) input_data = imread(input_stream, pilmode=self.convert_mode) if self.after_open: input_data = self.after_open(input_data) input_data = pil2tensor(input_data, np.float32) if self.div: input_data = input_data.div_(255) if self.cls: input_data = self.cls(input_data) else: input_data = Image(input_data) result = func(input_data) result = get_output_str(result, request.headers.get("output", "json")) return Response(response=result, status=200, mimetype="application/json")
def handle_aws_lambda_event(self, event, func): try: from imageio import imread except ImportError: raise ImportError( "imageio package is required to use ImageHandler") if event["headers"].get("Content-Type", None) in ACCEPTED_CONTENT_TYPES: # decodebytes introduced at python3.1 try: image_data = imread(base64.decodebytes(event["body"]), pilmode=self.pilmode) except AttributeError: image_data = imread( base64.decodestring(event["body"]), # pylint: disable=W1505 pilmode=self.convert_mode, ) else: raise BentoMLException( "BentoML currently doesn't support Content-Type: {content_type} for " "AWS Lambda".format( content_type=event["headers"]["Content-Type"])) try: from fastai.vision import pil2tensor, Image import numpy as np except ImportError: raise ImportError("fastai package is required") if self.after_open: image_data = self.after_open(image_data) image_data = pil2tensor(image_data, np.float32) if self.div: image_data = image_data.div_(255) if self.cls: image_data = self.cls(image_data) else: image_data = Image(image_data) result = func(image_data) result = get_output_str(result, event["headers"].get("output", "json")) return {"statusCode": 200, "body": result}
def to_python(self, data): """ Check that the file-upload field data contains a valid image (GIF, JPG, PNG, etc. -- whatever Pillow supports). """ f = super().to_python(data) if f is None: return None from PIL import Image from fastai.vision import Image as fImage # We need to get a file object for Pillow. We might have a path or we might # have to read the data into memory. if hasattr(data, 'temporary_file_path'): file = data.temporary_file_path() else: if hasattr(data, 'read'): file = BytesIO(data.read()) else: file = BytesIO(data['content']) try: # load() could spot a truncated JPEG, but it loads the entire # image in memory, which is a DoS vector. See #3848 and #18520. image = Image.open(file).convert('RGB') # verify() must be called immediately after the constructor. image.verify() # Annotating so subclasses can reuse it for their own validation f.image = fImage(pil2tensor(image, dtype=np.float32).div_(255)) # Pillow doesn't detect the MIME type of all formats. In those # cases, content_type will be None. f.content_type = Image.MIME.get(image.format) except Exception as exc: # Pillow doesn't recognize it as an image. raise ValidationError( self.error_messages['invalid_image'], code='invalid_image', ) from exc if hasattr(f, 'seek') and callable(f.seek): f.seek(0) return f
def api_batch_tfm_predict(payload): instances = json.loads(payload)['instances'] img_bytes = [b64decode(inst['image_bytes']['b64']) for inst in instances] tensors = [ pil2tensor(Image.open(BytesIO(byts)), dtype=np.float32).div_(255) for byts in img_bytes ] tfm_tensors = [ learner.data.valid_dl.tfms[0]((tensor, torch.zeros(0)))[0] for tensor in tensors ] # batch predict, dummy labels for the second argument dummy_labels = torch.zeros(len(tfm_tensors)) tensor_stack = torch.stack(tfm_tensors) if torch.cuda.is_available(): tensor_stack = tensor_stack.cuda() return np.argmax(np.array( learner.pred_batch(batch=(tensor_stack, dummy_labels))), axis=1)
def open_fat2019_image(fn, convert_mode, after_open)->Image: x = PIL.Image.open(fn).convert(convert_mode) # crop (128x321 for a 5 second long audio clip) time_dim, base_dim = x.size #How many crops can we take? maxCrops = int(np.ceil(time_dim / base_dim)) #What's the furthest point at which we can take a crop without running out of pixels lastValidCrop = time_dim - base_dim crop_x = (counter % maxCrops) * base_dim # We don't want to crop any further than the last 128 pixels crop_x = min(crop_x, lastValidCrop) x1 = x.crop([crop_x, 0, crop_x+base_dim, base_dim]) # standardize return Image(pil2tensor(x1, np.float32).div_(255))
def post(self): global NETWORK_MODEL_TAG global NETWORK global NETWORK_VALUES response = {'success': False} # ut.embed() try: with ut.Timer('Pre'): parser = reqparse.RequestParser() parser.add_argument('image', type=str) parser.add_argument('config', type=dict) args = parser.parse_args() image_base64_str = args['image'] image = get_image_from_base64_str(image_base64_str) config = args['config'] model_tag = config.get('model_tag', None) num_returns = config.get('topk', 100) model_url = model_url_dict.get(model_tag, None) assert model_url is not None, 'Model tag %r is not recognized' % ( model_tag, ) if model_tag != NETWORK_MODEL_TAG: with ut.Timer('Loading network'): print('Loading network from weights %r' % (model_tag, )) values_url = model_url.replace('.pth', '.values.pth') # Download files model_filepath = ut.grab_file_url(model_url, appname='kaggle7', check_hash=True) values_filepath = ut.grab_file_url(values_url, appname='kaggle7', check_hash=True) model_values = torch.load(values_filepath) classes = model_values['classes'] num_classes = len(classes) model_weights = torch.load(model_filepath, map_location=get_device()) network_model, mutliple = make_new_network( num_classes, RING_HEADS, GEM_CONST, pretrained=False) if mutliple: pass if torch.cuda.is_available(): network_model = network_model.cuda() # model_weights = model_weights['model'] network_model.load_state_dict(model_weights) network_model.eval() NETWORK_MODEL_TAG = model_tag NETWORK = network_model NETWORK_VALUES = model_values print('Using network %r' % (NETWORK_MODEL_TAG, )) with ut.Timer('Loading input tensor'): input_image = image.convert(CMODE).convert('LA').convert(CMODE) input_image = TFRM_RESIZE(input_image) input_image = pil2tensor(input_image, np.float32) input_image = input_image.div_(255) input_image = TFRM_WHITEN(input_image) size = input_image.size() input_tensor = input_image.view(-1, size[0], size[1], size[2]) input_tensor = input_tensor.to(get_device()) # Run inference with ut.Timer('Inference'): print('Running inference on input tensor %r' % (input_tensor.size(), )) output = NETWORK(input_tensor) print('...done') preds_list, feats_list = output with ut.Timer('Post1'): print('Performing post-processing') prediction_raw = preds_list[-1][0] features_raw = TFRM_L2NORM(torch.cat(feats_list, dim=1))[0] with ut.Timer('Post2'): print('...classifier') # Post Process classification classifier_temp = NETWORK_VALUES['thresholds'][ 'classifier_softmax_temp'] classifier_prediction = torch.softmax(prediction_raw / classifier_temp, dim=0) with ut.Timer('Post3'): # Post process features print('...features') train_feats = NETWORK_VALUES['train_feats'] train_gt = NETWORK_VALUES['train_gt'] size = features_raw.size() features = features_raw.view(-1, size[0]) distance_matrix_imgs = batched_dmv(features, train_feats) distance_matrix_classes = dm2cm(distance_matrix_imgs, train_gt) features_sim = (2.0 - distance_matrix_classes) * 0.5 features_sim = features_sim[0] features_temp = NETWORK_VALUES['thresholds'][ 'feature_softmax_temp'] features_prediction = torch.softmax(features_sim / features_temp, dim=0) with ut.Timer('Post4'): print('...mixing') p = NETWORK_VALUES['thresholds']['mixing_value'] classifier_prediction = classifier_prediction.to('cpu') final_prediction = (p * classifier_prediction + (1.0 - p) * features_prediction) with ut.Timer('Collection'): print('Collecting prediction') top_k_score_list, top_k_index_list = final_prediction.topk( num_returns, 0) top_k_score_list = top_k_score_list.detach().tolist() classes = NETWORK_VALUES['classes'] top_k_class_list = ut.take(classes, top_k_index_list) response['scores'] = {} for top_k_class, top_k_score in zip(top_k_class_list, top_k_score_list): response['scores'][top_k_class] = top_k_score response['success'] = True print('...done') except Exception as ex: message = str(ex) response['message'] = message print('!!!ERROR!!!') print(response) # if torch.cuda.is_available(): # torch.cuda.empty_cache() return response
# f_movie = "data/movies/Die.Hard.1988.720p.BRRip.x264-x0r.mkv" f_movie = sys.argv[1] assert os.path.exists(f_movie) save_dest = f"data/shot_detection" os.system(f"mkdir -p {save_dest}") f_save = os.path.join(save_dest, os.path.basename(f_movie) + ".npy") data = [] for n, frame in tqdm(Streamer(f_movie, None)): # Convert to torch.Tensor, then fastai.tensor img = pil2tensor(frame, np.float32).div_(255) img = fastImage(img) pred = learn.predict(img) prob = pred[2].numpy() item = {"frame_n": n} for k, i in learn.data.c2i.items(): item[k] = prob[i] data.append(item) # if len(data)> 10: # break cols = [ "Close-Up",
def predict(self, image): fastai_image = pil2tensor(image, np.float32) fastai_image = Image(fastai_image) result = self.artifacts.pet_classifer.predict(fastai_image) return str(result)