ifnot sample_weights: sample_weights = [[1]] * len(img_files) # Make sample_weights equipped with a broadcastable shape when fed to a tf.Tensor sample_weights = np.array(sample_weights)
iflen(sample_weights.shape) == 1: sample_weights = sample_weights[..., np.newaxis] # Make sample_weights equipped with a broadcastable shape when fed to a tf.Tensor assert(len(sample_weights.shape) == 2and sample_weights.shape[-1] == 1)
if shuffle: indexes = np.arange(len(img_files)) np.random.shuffle(indexes) img_files = [ img_files[i] for i in indexes ] mask_files = [ mask_files[i] for i in indexes ] sample_weights = [ sample_weights[i] for i in indexes ]
deff(): for img, masks, sample_weight inzip(img_files, mask_files, sample_weights): yield img_masks_from_file(img, masks, sample_weight) return f
defvisualize(**images):# **images是(key,item)的方式,*images是item list的方式 """PLot images in one row.""" n = len(images) plt.figure(figsize=(16, 5)) for i, (name, image) inenumerate(images.items()): plt.subplot(1, n, i + 1) plt.xticks([]) plt.yticks([]) plt.title(' '.join(name.split('_')).title()) plt.imshow(image) plt.show()
# there are 11 classes in the dataset: one class for each digit (0 to 9) plus the background class n_classes = 11
# assign a random color for each class colors = [tuple(np.random.randint(256, size=3) / 255.0) for i inrange(n_classes)]
deffuse_with_pil(images): ''' Creates a blank image and pastes input images Args: images (list of numpy arrays) - numpy array representations of the images to paste Returns: PIL Image object containing the images '''
widths = (image.shape[1] for image in images) heights = (image.shape[0] for image in images) total_width = sum(widths) max_height = max(heights)
x_offset = 0 for im in images: pil_image = PIL.Image.fromarray(np.uint8(im)) new_im.paste(pil_image, (x_offset,0)) x_offset += im.shape[1] return new_im
defgive_color_to_annotation(annotation): ''' Converts a 2-D annotation to a numpy array with shape (height, width, 3) where the third axis represents the color channel. The label values are multiplied by 255 and placed in this axis to give color to the annotation Args: annotation (numpy array) - label map array Returns: the annotation array with an additional color channel/axis ''' seg_img = np.zeros( (annotation.shape[0],annotation.shape[1], 3) ).astype('float') for c inrange(n_classes): segc = (annotation == c) seg_img[:,:,0] += segc*( colors[c][0] * 255.0) seg_img[:,:,1] += segc*( colors[c][1] * 255.0) seg_img[:,:,2] += segc*( colors[c][2] * 255.0) return seg_img
defshow_annotation_and_prediction(image, annotation, prediction, iou_list, dice_score_list): ''' Displays the images with the ground truth and predicted label maps. Also overlays the metrics. Args: image (numpy array) -- the input image annotation (numpy array) -- the ground truth label map prediction (numpy array) -- the predicted label map iou_list (list of floats) -- the IOU values for each class dice_score_list (list of floats) -- the Dice Score for each class '''
deflist_show_annotation(dataset, num_images): ''' Displays images and its annotations side by side Args: dataset (tf Dataset) -- batch of images and annotations num_images (int) -- number of images to display ''' ds = dataset.unbatch()
plt.figure(figsize=(20, 15)) plt.title("Images And Annotations") plt.subplots_adjust(bottom=0.1, top=0.9, hspace=0.05)