def inference(saved_model_filepath, image_folder, output_filepath, image_format): img_filepath_list = [ os.path.join(image_folder, fn) for fn in os.listdir(image_folder) if fn.endswith('.{}'.format(image_format)) ] model = tf.saved_model.load(saved_model_filepath) with open(output_filepath, 'w') as fh: print('Starting inference of file list') for i in range(len(img_filepath_list)): img_filepath = img_filepath_list[i] _, img_name = os.path.split(img_filepath) print('{}/{} : {}'.format(i, len(img_filepath_list), img_name)) img = imagereader.imread(img_filepath) img = img.astype(np.float32) # normalize with whole image stats img = imagereader.zscore_normalize(img) pred = _inference(img, model) fh.write('{}, {}\n'.format(img_name, float(pred))) print(' Regression Value: {} '.format(pred))
def inference(checkpoint_filepath, image_folder, output_folder, number_classes, number_channels, image_format): print('Arguments:') print('checkpoint_filepath = {}'.format(checkpoint_filepath)) print('image_folder = {}'.format(image_folder)) print('output_folder = {}'.format(output_folder)) print('number_classes = {}'.format(number_classes)) print('number_channels = {}'.format(number_channels)) print('image_format = {}'.format(image_format)) # create output filepath if not os.path.exists(output_folder): os.mkdir(output_folder) img_filepath_list = [os.path.join(image_folder, fn) for fn in os.listdir(image_folder) if fn.endswith('.{}'.format(image_format))] fcd_model = model.FCDensenet(number_classes, 1, number_channels, 1e-4) fcd_model.load_checkpoint(checkpoint_filepath) print('Starting inference of file list') for i in range(len(img_filepath_list)): img_filepath = img_filepath_list[i] _, slide_name = os.path.split(img_filepath) print('{}/{} : {}'.format(i, len(img_filepath_list), slide_name)) print('Loading image: {}'.format(img_filepath)) img = imagereader.imread(img_filepath) img = img.astype(np.float32) # normalize with whole image stats img = imagereader.zscore_normalize(img) print(' img.shape={}'.format(img.shape)) if img.shape[0] > INF_TILE_SIZE or img.shape[1] > INF_TILE_SIZE: tile_size = INF_TILE_SIZE segmented_mask = _inference_tiling(img, fcd_model, tile_size) else: segmented_mask = _inference(img, fcd_model) if 0 <= np.max(segmented_mask) <= 255: segmented_mask = segmented_mask.astype(np.uint8) if 255 < np.max(segmented_mask) < 65536: segmented_mask = segmented_mask.astype(np.uint16) if np.max(segmented_mask) > 65536: segmented_mask = segmented_mask.astype(np.int32) if 'tif' in image_format: skimage.io.imsave(os.path.join(output_folder, slide_name), segmented_mask, compress=6, bigtiff=True, tile=(1024,1024)) else: try: skimage.io.imsave(os.path.join(output_folder, slide_name), segmented_mask, compress=6) except TypeError: # compress option not valid skimage.io.imsave(os.path.join(output_folder, slide_name), segmented_mask)
def inference(image_folder, image_format, saved_model_filepath, output_folder, min_box_size): # create output filepath if not os.path.exists(output_folder): os.mkdir(output_folder) if image_format.startswith('.'): image_format = image_format[1:] img_filepath_list = [ os.path.join(image_folder, fn) for fn in os.listdir(image_folder) if fn.endswith('.{}'.format(image_format)) ] # load the saved model yolo_model = tf.saved_model.load(saved_model_filepath) print('Starting inference of file list') for i in range(len(img_filepath_list)): img_filepath = img_filepath_list[i] _, file_name = os.path.split(img_filepath) print('{}/{} : {}'.format(i, len(img_filepath_list), file_name)) print('Loading image: {}'.format(img_filepath)) img = imagereader.imread(img_filepath) height, width, channels = img.shape img = img.astype(np.float32) # normalize with whole image stats img = imagereader.zscore_normalize(img) print(' img.shape={}'.format(img.shape)) # convert HWC to CHW batch_data = img.transpose((2, 0, 1)) # convert CHW to NCHW batch_data = batch_data.reshape( (1, batch_data.shape[0], batch_data.shape[1], batch_data.shape[2])) batch_data = tf.convert_to_tensor(batch_data) boxes = yolo_model(batch_data, training=False) # boxes are [x1,y1,x2,y2,c] # constrain boxes to image coordinates boxes[:, 0] = np.clip(boxes[:, 0], 0, width) boxes[:, 1] = np.clip(boxes[:, 1], 0, height) boxes[:, 2] = np.clip(boxes[:, 2], 0, width) boxes[:, 3] = np.clip(boxes[:, 3], 0, height) # strip out batch_size which is fixed at one boxes = np.array(boxes) boxes = boxes[0, ] boxes = bbox_utils.filter_small_boxes(boxes, min_box_size) objectness = boxes[:, 4:5] class_probs = boxes[:, 5:] boxes = boxes[:, 0:4] # nms boxes per tile being passed through the network boxes, scores, class_label = bbox_utils.per_class_nms( boxes, objectness, class_probs) # convert [x1, y1, x2, y2] to [x, y, w, h] boxes[:, 2] = boxes[:, 2] - boxes[:, 0] boxes[:, 3] = boxes[:, 3] - boxes[:, 1] class_label = np.reshape(class_label, (-1, 1)) boxes = np.concatenate((boxes, class_label), axis=-1) boxes = boxes.astype(np.int32) # # draw boxes on the images and save # import skimage.io # ofp = './inference-results' # img = img - np.min(img) # img = img / np.max(img) # img = np.asarray(img * 255, dtype=np.uint8) # fn = '{:08d}.png'.format(i) # print(boxes) # skimage.io.imsave(os.path.join(ofp, fn), bbox_utils.draw_boxes(img.copy(), boxes)) # write merged rois print('Found: {} rois'.format(boxes.shape[0])) output_csv_file = os.path.join(output_folder, file_name.replace(image_format, 'csv')) bbox_utils.write_boxes_from_xywhc(boxes, output_csv_file)