Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 18.49 KB | None | 0 0
  1. import os
  2.  
  3. import numpy as np
  4. import tensorflow as tf
  5. from random import randint
  6. from math import sqrt, ceil
  7.  
  8. import cv2
  9.  
  10. def image_scaling(img, label, input_size):
  11.     """
  12.    Randomly scales the images between 0.4 to 1.0 times the original size.
  13.  
  14.    Args:
  15.      img: Training image to scale.
  16.      label: Segmentation mask to scale.
  17.    """
  18.    
  19.     scale = tf.random_uniform([1], minval = 0.5, maxval = 1.3, dtype=tf.float32, seed=None)
  20.     h_new = tf.to_int32(tf.multiply(tf.to_float(tf.shape(img)[0]), scale))
  21.     w_new = tf.to_int32(tf.multiply(tf.to_float(tf.shape(img)[1]), scale))
  22.     new_shape = tf.squeeze(tf.stack([h_new, w_new]), squeeze_dims=[1])
  23.    
  24.     img = tf.image.resize_images(img, new_shape)
  25.     label = tf.image.resize_nearest_neighbor(tf.expand_dims(label, 0), new_shape)
  26.     label = tf.squeeze(label, squeeze_dims=[0])
  27.  
  28.     img = tf.image.resize_image_with_crop_or_pad(img, input_size[0], input_size[1])
  29.     label = tf.image.resize_image_with_crop_or_pad(label, input_size[0], input_size[1])
  30.  
  31.    
  32.     return img, label
  33.  
  34. def image_mirroring(img, label):
  35.     """
  36.    Randomly mirrors the images.
  37.  
  38.    Args:
  39.      img: Training image to mirror.
  40.      label: Segmentation mask to mirror.
  41.    """
  42.    
  43.     distort_left_right_random = tf.random_uniform([1], 0, 1.0, dtype=tf.float32)[0]
  44.     mirror = tf.less(tf.stack([1.0, distort_left_right_random, 1.0]), 0.5)
  45.     mirror = tf.boolean_mask([0, 1, 2], mirror)
  46.     img = tf.reverse(img, mirror)
  47.     label = tf.reverse(label, mirror)
  48.     return img, label
  49.  
  50. def random_roi(img, answer, y_min, y_max, x_left_gt, x_right_gt, shape, roi_params):
  51.     """
  52.    Randomly mirrors the images.
  53.  
  54.    Args:
  55.      img: Training image.
  56.      answer: Segmentation mask.
  57.      y_min, y_max - y roi values
  58.      x_left_gt, x_right_gt - x bounds of grountruth object's location, from this values will be random indenting
  59.      shape: Shape of image
  60.      roi_params: max and min available indenting
  61.    """
  62.     max_x_indent, min_x_indent = roi_params
  63.    
  64.     init_w = shape[0]
  65.  
  66.     min_indent = tf.constant(min_x_indent,dtype=tf.int32)
  67.     max_indent = tf.constant(max_x_indent,dtype=tf.int32)
  68.  
  69.     tf_zero = tf.constant(0, dtype=tf.int32)
  70.  
  71.     x_left  = tf.random_uniform(shape = [1], minval = tf.maximum(tf.subtract(x_left_gt, max_indent), tf_zero), maxval = tf.maximum(tf.subtract(x_left_gt, min_indent), tf_zero), dtype = tf.int32)[0]
  72.     x_right = tf.random_uniform(shape = [1], minval = tf.minimum(tf.add(x_right_gt, min_indent), init_w), maxval = tf.minimum(tf.add(x_right_gt, max_indent), init_w), dtype = tf.int32)[0]
  73.  
  74.     y_top = y_min
  75.     y_bot = y_max
  76.  
  77.     image = tf.slice(img, [y_top, x_left, 0], [tf.subtract(y_bot,y_top), tf.subtract(x_right,x_left), 3])
  78.     label = tf.slice(answer, [y_top, x_left, 0], [tf.subtract(y_bot,y_top), tf.subtract(x_right,x_left), 1])
  79.  
  80.     new_shape = tf.convert_to_tensor([tf.shape(image)[1], tf.shape(image)[0]], dtype = tf.int32)
  81.  
  82.     return image, label, new_shape
  83.  
  84. def get_rois(max_x_indent, min_x_indent, num_rois):
  85.     '''
  86.    Get num_rois number of x indents
  87.    '''
  88.     assert(num_rois>1),"Number of rois for validation mist be greater then 1!"
  89.     if (num_rois == 2):
  90.         return [(min_x_indent, min_x_indent), (max_x_indent, max_x_indent)]
  91.  
  92.     num_of_values = ceil(sqrt(num_rois))-1
  93.  
  94.     step = (max_x_indent - min_x_indent)/num_of_values
  95.  
  96.     step = ceil(step)
  97.     val_indents = [i for i in range(min_x_indent, max_x_indent, step)]
  98.     val_indents.append(max_x_indent)
  99.     val_roi_indents = []    
  100.    
  101.     for indent in val_indents:
  102.         for indent_ in val_indents:
  103.             val_roi_indents.append((indent, indent_))
  104.  
  105.             if len(val_roi_indents) == num_rois:
  106.                 return val_roi_indents
  107.  
  108.     return val_roi_indents
  109.  
  110. def fixed_roi(img, answer, y_min, y_max, x_left_gt, x_right_gt, shape, roi_params, num_of_valid_step, num_rois):
  111.     '''
  112.    Apply on of fixed roi(depends on num_of_valid_step value) during validation
  113.    '''
  114.     init_w = shape[0]
  115.  
  116.     max_x_indent,min_x_indent = roi_params
  117.     val_roi_indents = get_rois(max_x_indent, min_x_indent, num_rois)
  118.     #print("\nROI indents==========>",val_roi_indents[num_of_valid_step])
  119.  
  120.     x_left_indent = tf.constant(val_roi_indents[num_of_valid_step][0],dtype=tf.int32)
  121.     x_right_indent = tf.constant(val_roi_indents[num_of_valid_step][1],dtype=tf.int32)
  122.  
  123.     tf_zero = tf.constant(0, dtype=tf.int32)
  124.  
  125.     x_left  = tf.math.maximum(tf.subtract(x_left_gt, x_left_indent), tf_zero)
  126.     x_right = tf.math.minimum(tf.math.add(x_right_gt, x_right_indent), init_w)
  127.  
  128.     y_top = y_min
  129.     y_bot = y_max
  130.  
  131.     image = tf.slice(img, [y_top, x_left, 0], [tf.subtract(y_bot,y_top), tf.subtract(x_right,x_left), 3])
  132.     label = tf.slice(answer, [y_top, x_left, 0], [tf.subtract(y_bot,y_top), tf.subtract(x_right,x_left), 1])
  133.  
  134.     new_shape = tf.convert_to_tensor([tf.shape(image)[1], tf.shape(image)[0]], dtype = tf.int32)
  135.  
  136.     return image, label, new_shape
  137.  
  138. def random_crop_and_pad_image_and_labels(image, label, crop_h, crop_w, pad_h, pad_w, input_size, CROP_PROB, PAD_PROB, ignore_label):
  139.     """
  140.    Randomly crop and pads the input images.
  141.  
  142.    Args:
  143.      image: Training image to crop/ pad.
  144.      label: Segmentation mask to crop/ pad.
  145.      crop_h: Height of cropped segment.
  146.      crop_w: Width of cropped segment.
  147.    """
  148.  
  149.     label = tf.cast(label, dtype = tf.float32)
  150.     label = label - ignore_label # Needs to be subtracted and later added due to 0 padding.
  151.     combined = tf.concat(axis = 2, values = [image, label])
  152.    
  153.     last_image_dim = tf.shape(image)[-1]
  154.     #last_label_dim = tf.shape(label)[-1]
  155.  
  156.     use_crop = tf.random_uniform(shape = [1], minval = 0.0, maxval = 1.0, dtype = tf.float32)[0]
  157.     use_pad = tf.random_uniform(shape = [1], minval = 0.0, maxval = 1.0, dtype = tf.float32)[0]
  158.     #use_zoom = tf.random_uniform(shape = [1], minval = 0.0, maxval = 1.0, dtype = tf.float32)[0]
  159.     #focal_len = tf.random_uniform(shape = [1], minval = MIN_FOCAL, maxval = MAX_FOCAL, dtype = tf.float32)[0]
  160.     crop_size = tf.stack([crop_h, crop_w, 4], axis = 0)
  161.    
  162.     rows_shift = tf.cast((pad_h - input_size[1]) / 2, dtype = tf.int32)
  163.     cols_shift = tf.cast((pad_w - input_size[0]) / 2, dtype = tf.int32)
  164.     pads = tf.stack([[rows_shift, rows_shift], [cols_shift, cols_shift], [0, 0]])
  165.  
  166.     combined_crop = tf.cond(use_crop > CROP_PROB, lambda : combined, lambda : tf.random_crop(combined, crop_size))
  167.     combined_crop = tf.cond(use_pad > PAD_PROB, lambda : combined_crop, lambda : tf.pad(combined_crop, pads))
  168.  
  169.     img_crop = combined_crop[:, :, :last_image_dim]
  170.     label_crop = combined_crop[:, :, last_image_dim:]
  171.     label_crop = label_crop + ignore_label
  172.     label_crop = tf.cast(label_crop, dtype = tf.uint8)
  173.  
  174.     return img_crop, label_crop  
  175.  
  176. def random_rotation(image, label, rot_params):
  177.     ANGLE_RADIANS, ROT_PROB = rot_params
  178.     # max rotation angle ~ 13 degrees
  179.     use_rotation = tf.random_uniform(shape = [1], minval = 0.0, maxval = 1.0, dtype = tf.float32)[0]
  180.     rotation_radians = tf.random_uniform([], minval = -ANGLE_RADIANS, maxval = ANGLE_RADIANS, dtype=tf.float32, seed=None)
  181.  
  182.     img = tf.cond(use_rotation > ROT_PROB, lambda : image, lambda : tf.contrib.image.rotate(image, rotation_radians, interpolation='BILINEAR'))
  183.     lbl = tf.cond(use_rotation > ROT_PROB, lambda : label, lambda : tf.contrib.image.rotate(label, rotation_radians, interpolation='BILINEAR'))
  184.  
  185.     return img, lbl
  186.  
  187. def read_labeled_image_list(data_list, input_shape):
  188.     """Reads txt file containing paths to images and ground truth masks.
  189.    
  190.    Args:
  191.      data_list: path to the file with lines of the form '/path/to/image /path/to/mask real_dx real_dy min_y max_y left_x right_x'.
  192.      
  193.    Returns:
  194.      Lists with:
  195.            all file names for images
  196.            all file names for masks
  197.            all x-pixel's sizes for each image
  198.            all y pixel's sizes for each image
  199.            all initial shapes of images
  200.            all top y coordinates of roi for each image
  201.            all bottom y coordinates of roi for each image
  202.            all max left x coordinates of roi for each image
  203.            all min right x coordinates of roi for each image
  204.    """
  205.     f = open(data_list, 'r')
  206.     images = []
  207.     masks = []    
  208.  
  209.     # x-pixel's size in cm
  210.     PhysicalDeltaXs = []
  211.     # y pixel's size in cm
  212.     PhysicalDeltaYs= []
  213.  
  214.     # ys for roi
  215.     yMins = []
  216.     yMaxs = []
  217.  
  218.     # x bounds of gt bbox (around gt points)
  219.     leftXs = []
  220.     rightXs = []
  221.  
  222.     init_shapes = []
  223.  
  224.     for line in f:
  225.         real_dx = None
  226.         real_dy = None
  227.  
  228.         # Roi top coordinate
  229.         min_y = None
  230.         # Roi bottom coordinate
  231.         max_y = None
  232.  
  233.         # Maximum left x coordinate of roi (need for random roi selection)
  234.         left_x = None
  235.         # minimum right x coordinate of roi (need for random roi selection)
  236.         right_x = None
  237.  
  238.         try:
  239.             data = line.strip("\n").split(' ')
  240.             image, mask, real_dx, real_dy, min_y, max_y, left_x, right_x = data
  241.  
  242.         except ValueError:
  243.             image = mask = line.strip("\n")
  244.  
  245.         images.append(image)
  246.         masks.append(mask)
  247.  
  248.         yMins.append(int(min_y))
  249.         yMaxs.append(int(max_y))
  250.  
  251.         leftXs.append(int(left_x))
  252.         rightXs.append(int(right_x))
  253.  
  254.         if not real_dx is None:
  255.             PhysicalDeltaX = float(real_dx)#pixel size in cm
  256.             PhysicalDeltaX = PhysicalDeltaX if PhysicalDeltaX > 0 else -1 * PhysicalDeltaX
  257.             PhysicalDeltaXs.append(PhysicalDeltaX)
  258.        
  259.         if not real_dy is None:
  260.             PhysicalDeltaY = float(real_dy)#pixel size in cm
  261.             PhysicalDeltaY = PhysicalDeltaY if PhysicalDeltaY > 0 else -1 * PhysicalDeltaY
  262.             PhysicalDeltaYs.append(PhysicalDeltaY)
  263.  
  264.         im = cv2.imread(image)
  265.         #if im is None:
  266.         #    print(image)
  267.         init_h, init_w, _ = im.shape            
  268.         init_shapes.append([init_w, init_h])    
  269.        
  270.     return images, masks, PhysicalDeltaXs, PhysicalDeltaYs, init_shapes, yMins, yMaxs, leftXs, rightXs
  271.  
  272. def read_images_from_disk(input_queue, input_size,
  273.                 random_scale, random_mirror, random_rotate, random_roi_,
  274.                 img_mean, roi_params, crop_params, pad_params, rot_params, ignore_label,
  275.                 train = True, num_of_valid_step = 0, num_rois = 12, gray = False):
  276.     """Read one image and its corresponding mask with optional pre-processing.
  277.    
  278.    Args:
  279.      input_queue: tf queue with paths to the image and its mask.
  280.      input_size: a tuple with (height, width) values.
  281.                  If not given, return images of original size.
  282.      random_scale: whether to randomly scale the images prior
  283.                    to random crop.
  284.      random_mirror: whether to randomly mirror the images prior
  285.                    to random crop.
  286.      random_rotate: whether to randomly rotate the images prior
  287.                    to random rotation.
  288.      random_roi_: whether to apply random rois to images
  289.  
  290.      img_mean: vector of mean colour values.
  291.      roi_params: maximum available and minimum available x-axis indents during calculating random roi
  292.    Returns:
  293.      
  294.    """    
  295.     img_contents = tf.read_file(input_queue[0])
  296.     label_contents = tf.read_file(input_queue[1])
  297.  
  298.     img = tf.image.decode_jpeg(img_contents, channels=3)
  299.     answer = tf.image.decode_png(label_contents, channels=1)
  300.  
  301.     ## select random roi and crop
  302.     new_init_shape = input_queue[4]
  303.     if random_roi_:
  304.  
  305.         init_shape = input_queue[4]
  306.  
  307.         y_min = input_queue[5]
  308.         y_max = input_queue[6]
  309.  
  310.         x_left = input_queue[7]
  311.         x_right = input_queue[8]
  312.  
  313.         if train:
  314.             img, answer, new_init_shape = random_roi(img, answer, y_min, y_max, x_left, x_right, init_shape, roi_params)
  315.         else:
  316.             img, answer, new_init_shape = fixed_roi(img, answer, y_min, y_max, x_left, x_right, init_shape, roi_params, num_of_valid_step, num_rois)
  317.    
  318.     if train and random_rotate:
  319.         img, answer = random_rotation(img, answer, rot_params)
  320.    
  321.     img = tf.image.resize_images(img, input_size)
  322.     answer = tf.image.resize_nearest_neighbor(tf.expand_dims(answer, 0), input_size)
  323.     answer = tf.squeeze(answer, squeeze_dims=[0])
  324.    
  325.     # converting to bgr
  326.     #img_r, img_g, img_b = tf.split(axis=2, num_or_size_splits=3, value=img)
  327.     #img = tf.cast(tf.concat(axis=2, values=[img_b, img_g, img_r]), dtype=tf.float32)
  328.    
  329.     # Extract mean.
  330.     if not gray:
  331.         image = img - img_mean
  332.     else:
  333.         image = img
  334.  
  335.     if input_size is None:
  336.         print('WTF?!?!')
  337.         quit()
  338.  
  339.     h, w = input_size
  340.  
  341.     # Randomly scale the images and labels.
  342.     if train and random_scale:
  343.         image, answer = image_scaling(image, answer, input_size)
  344.  
  345.     # Randomly mirror the images and labels.
  346.     if train and random_mirror:
  347.         image, answer = image_mirroring(image, answer)
  348.  
  349.     if train:
  350.         MIN_CROP, MAX_CROP, CROP_PROB = crop_params
  351.         h_rate = tf.random_uniform(shape = [1], minval = MIN_CROP, maxval = MAX_CROP, dtype = tf.float32)
  352.         w_rate = tf.random_uniform(shape = [1], minval = MIN_CROP, maxval = MAX_CROP, dtype = tf.float32)
  353.         crop_h = tf.cast(tf.cast(h, tf.float32) * h_rate, tf.int32)[0]
  354.         crop_w = tf.cast(tf.cast(w, tf.float32) * w_rate, tf.int32)[0]
  355.  
  356.         MIN_PAD, MAX_PAD, PAD_PROB = pad_params
  357.         h_rate = tf.random_uniform(shape = [1], minval = MIN_PAD, maxval = MAX_PAD, dtype = tf.float32)
  358.         w_rate = tf.random_uniform(shape = [1], minval = MIN_PAD, maxval = MAX_PAD, dtype = tf.float32)
  359.         pad_h = tf.cast(tf.cast(h, tf.float32) * h_rate, tf.int32)[0]
  360.         pad_w = tf.cast(tf.cast(w, tf.float32) * w_rate, tf.int32)[0]
  361.  
  362.         img, label = random_crop_and_pad_image_and_labels(image, answer, crop_h, crop_w, pad_h, pad_w, input_size, CROP_PROB, PAD_PROB, ignore_label)
  363.  
  364.     else:
  365.         img = image
  366.         label = answer
  367.  
  368.     img = tf.image.resize_images(img, input_size)
  369.     if gray:
  370.         img = tf.image.rgb_to_grayscale(img)
  371.    
  372.     label = tf.image.resize_nearest_neighbor(tf.expand_dims(label, 0), input_size)
  373.     label = tf.squeeze(label, squeeze_dims=[0])
  374.     if gray:
  375.         img.set_shape([h, w, 1])
  376.     else:
  377.         img.set_shape([h, w, 3])
  378.     label.set_shape([h, w, 1])
  379.    
  380.     return img, label, input_queue[2], input_queue[3], new_init_shape
  381.  
  382. class ImageReader(object):
  383.     '''Generic ImageReader which reads images and corresponding segmentation
  384.       masks from the disk, and enqueues them into a TensorFlow queue.
  385.    '''
  386.     def __init__(self, coord, params, train = True, num_of_valid_step = 0):
  387.         max_x_indent = params["MAX_X_INDENT"]
  388.         min_x_indent = params["MIN_X_INDENT"]
  389.         self.is_gray = params["IS_GRAY"]
  390.         self.data_list = params["DATA_LIST"]
  391.         self.input_size = params["INPUT_SIZE"]
  392.         self.img_mean = params["IMG_MEAN"]
  393.         self.ignore_label = params["IGNORE_LABEL"]
  394.         self.random_roi = params["RANDOM_ROI"]
  395.  
  396.         if train:
  397.             self.random_rotate = params["RANDOM_ROTATE"]
  398.             self.random_scale = params["RANDOM_SCALE"]
  399.             self.random_mirror = params["RANDOM_MIRROR"]
  400.             self.num_rois = 0
  401.         else:
  402.             self.random_rotate, self.random_scale, self.random_mirror = [False]*3
  403.             self.num_rois = params["NUM_ROIS"]
  404.  
  405.         self.coord = coord
  406.         self.train = train
  407.         self.num_of_valid_step = num_of_valid_step
  408.  
  409.         self.roi_params = [max_x_indent, min_x_indent]
  410.         if train:
  411.             self.crop_params = [params["MIN_CROP"], params["MAX_CROP"], params["CROP_PROB"]]
  412.             self.pad_params = [params["MIN_PAD"], params["MAX_PAD"], params["PAD_PROB"]]
  413.             self.rot_params = [params["ANGLE_RADIANS"], params["ROT_PROB"]]
  414.         else:
  415.             self.crop_params = []
  416.             self.pad_params = []
  417.             self.rot_params = []
  418.  
  419.  
  420.         self.image_list, self.label_list, self.x_pixels_data, self.y_pixels_data, self.init_shapes, \
  421.                         self.yMins, self.yMaxs, self.leftXs, self.rightXs = read_labeled_image_list(self.data_list, self.input_size)
  422.        
  423.         self.images = tf.convert_to_tensor(self.image_list, dtype=tf.string)
  424.         self.labels = tf.convert_to_tensor(self.label_list, dtype=tf.string)
  425.  
  426.         self.y_min_data = tf.convert_to_tensor(self.yMins, dtype=tf.int32)
  427.         self.y_max_data = tf.convert_to_tensor(self.yMaxs, dtype=tf.int32)
  428.  
  429.         self.x_left_data = tf.convert_to_tensor(self.leftXs, dtype=tf.int32)
  430.         self.x_right_data = tf.convert_to_tensor(self.rightXs, dtype=tf.int32)
  431.        
  432.         self.x_datas = tf.convert_to_tensor(self.x_pixels_data, dtype=tf.float32)
  433.         self.y_datas = tf.convert_to_tensor(self.y_pixels_data, dtype=tf.float32)
  434.  
  435.         self.shapes = tf.convert_to_tensor(self.init_shapes, dtype=tf.int32)            
  436.        
  437.         self.queue = tf.train.slice_input_producer([self.images, self.labels, self.x_datas, self.y_datas, self.shapes,
  438.                                                     self.y_min_data, self.y_max_data, self.x_left_data, self.x_right_data],
  439.                                                    shuffle=self.train) # not shuffling if it is val
  440.         self.image, self.label, self.x_data, self.y_data, self.shape = read_images_from_disk(self.queue, self.input_size,
  441.                                                                                                 self.random_scale, self.random_mirror, self.random_rotate, self.random_roi,
  442.                                                                                                 self.img_mean, self.roi_params, self.crop_params, self.pad_params, self.rot_params,
  443.                                                                                                 self.ignore_label, self.train, self.num_of_valid_step, self.num_rois, self.is_gray)
  444.  
  445.     def dequeue(self, num_elements):
  446.         '''Pack images, labels, real x-pixel's sizes, real y-pixel's sizes and images shapes before applying roi (need for calculating in cm)
  447.            
  448.        Args:
  449.          num_elements: the batch size.'''
  450.         image_batch, label_batch, x_data_batch, y_data_batch, shapes_batch = tf.train.batch([self.image, self.label, self.x_data, self.y_data, self.shape],
  451.                                                                                             num_elements)
  452.  
  453.         return image_batch, label_batch, x_data_batch, y_data_batch, shapes_batch
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement