def get_batch(self, batch_size, crop_size, random_mirror, random_blur, random_rotate, color_switch, random_scale, scale_rate=None): label_contents = tf.read_file(self.queue[1]) label = tf.image.decode_png(label_contents, channels=1) if 'ADE' in self.dataset_name: # the first label (0) of ADE is background. label -= 1 img_contents = tf.read_file(self.queue[0]) img = tf.image.decode_image(img_contents, channels=3) img.set_shape((None, None, 3)) # decode_image does not returns no shape. img = tf.cast(img, dtype=tf.float32) if random_blur: img = tf.image.random_brightness(img, max_delta=63. / 255.) img = tf.image.random_saturation(img, lower=0.5, upper=1.5) img = tf.image.random_contrast(img, lower=0.2, upper=1.8) # Extract mean. img -= self.img_mean if color_switch: # this depends on the model we are using. # if provided by tensorflow, no need to switch color # if a model converted from caffe, need to switch color. img_r, img_g, img_b = tf.split(axis=2, num_or_size_splits=3, value=img) img = tf.concat([img_b, img_g, img_r], 2) # Randomly mirror the images and labels. if random_mirror: print('\t applying random mirror ...') img, label = helper.image_mirroring(img, label) # Randomly scale the images and labels. if random_scale: if scale_rate is not None: print('\t applying random scale [%f, %f]...' % (scale_rate[0], scale_rate[1])) else: print('\t applying random scale [0.5, 2.0]...') img, label = helper.image_scaling(img, label, scale_rate) # Randomly rotate the images and lables. if random_rotate: print('\t applying random rotation...') rd_rotatoin = tf.random_uniform([], -10.0, 10.0) angle = rd_rotatoin / 180.0 * math.pi img = rotate(img, angle, 'BILINEAR') label -= 255 label = rotate(label, angle, 'NEAREST') label += 255 # Randomly crops the images and labels. img, label = helper.random_crop_and_pad_image_and_labels(img, label, crop_size, crop_size) image_batch, label_batch = tf.train.batch([img, label], batch_size, batch_size * 4, 32 * batch_size) return image_batch, tf.cast(label_batch, dtype=tf.int32)
def _training_data_preprocess(image_filename, label_filename): img_contents = tf.read_file(image_filename) label_contents = tf.read_file(label_filename) img = tf.image.decode_image(img_contents, channels=3) img.set_shape( (None, None, 3)) # decode_image does not returns no shape. img = tf.cast(img, dtype=tf.float32) if random_blur: img = tf.image.random_brightness(img, max_delta=63. / 255.) img = tf.image.random_saturation(img, lower=0.5, upper=1.5) img = tf.image.random_contrast(img, lower=0.2, upper=1.8) img -= img_mean if color_switch: img_r, img_g, img_b = tf.split(axis=2, num_or_size_splits=3, value=img) img = tf.concat([img_b, img_g, img_r], 2) label = tf.image.decode_png(label_contents, channels=1) label = tf.cast(label, tf.int32) if 'ADE' in dataset_name: # the first label (0) of ADE is background. label -= 1 # Randomly mirror the images and labels. if random_mirror: print('\t applying random mirror ...') img, label = helper.image_mirroring(img, label) # Randomly scale the images and labels. if random_scale: if scale_rate is not None: print('\t applying random scale [%f, %f]...' % (scale_rate[0], scale_rate[1])) else: print('\t applying random scale [0.5, 2.0]...') img, label = helper.image_scaling(img, label, scale_rate) # Randomly rotate the images and lables. if random_rotate: print('\t applying random rotation...') rd_rotatoin = tf.random_uniform([], -10.0, 10.0) angle = rd_rotatoin / 180.0 * math.pi img = rotate(img, angle, 'BILINEAR') label -= 255 label = rotate(label, angle, 'NEAREST') label += 255 # Randomly crops the images and labels. img, label = helper.random_crop_and_pad_image_and_labels( img, label, crop_size, crop_size) return img, label
def __call__(self, image: tf.Tensor): image = tf.image.random_flip_left_right( image) # randomly left-right flip image = tf.image.random_flip_up_down(image) # randomly up-down flip if self.crop: s = np.random.randint(low=int(SIZE[0] * 0.8), high=SIZE[0], size=2) image = tf.image.random_crop(image, size=[s[0], s[1], CHANNEL]) if self.brightness: # randomly change brightness image = tf.image.random_brightness(image, max_delta=4) if self.contrast: # randomly change contrast image = tf.image.random_contrast(image, lower=0.2, upper=1.4) if self.shuffle: image = tf.random_shuffle(image) # shuffle the 0-axis image = tf.image.resize(image, SIZE) if self.rotated: from tensorflow.contrib.image import rotate image = rotate(image, 30) if self.norm: image = image / 255.0 if self.flatten: image = tf.reshape(image, [SIZE[0] * SIZE[1] * CHANNEL]) return image
def random_rotation(sess, img): img = img.squeeze() isrotate = random.randrange(0, 2) if isrotate and args.is_rotate: get_rotated_img = rotate(img, random.randrange(1, 80)) rotated_img = sess.run(get_rotated_img) else: rotated_img = img return rotated_img
def _augment_rotation(patch): """Augments the patch by rotating it by a small amount.""" max_rotation_radians = math.radians( FLAGS.augmentation_max_rotation_degrees) rotation = tf.random_uniform((), minval=-max_rotation_radians, maxval=max_rotation_radians) # Background is white (1.0) but tf.contrib.image.rotate currently always fills # the edges with black (0). Invert the patch before rotating. return 1. - contrib_image.rotate( 1. - patch, rotation, interpolation='BILINEAR')
def preprocess_for_train(image, height, width, scope=None): """Distort one image for training a network. Distorting images provides a useful technique for augmenting the data set during training in order to make the network invariant to aspects of the image that do not effect the label. Additionally it would create image_summaries to display the different transformations applied to the image. Args: image: 3-D Tensor of image. If dtype is tf.float32 then the range should be [-1, 1], otherwise it would converted to tf.float32 assuming that the range is [0, MAX], where MAX is largest positive representable number for int(8/16/32) data type (see `tf.image.convert_image_dtype` for details). height: integer width: integer scope: Optional scope for name_scope. Returns: 3-D float Tensor of distorted image used for training with range [-1, 1]. """ with tf.name_scope(scope, 'distort_image', [image, height, width]): if image.dtype != tf.float32: image = tf.image.convert_image_dtype(image, dtype=tf.float32) # Randomly select resize method. distorted_image = apply_with_random_selector( image, lambda x, method: tf.image.resize_images(x, [height, width], method ), num_cases=4) # Randomly flip the image horizontally. distorted_image = tf.image.random_flip_left_right(distorted_image) # Randomly rotate the image. random_angles = tf.random_uniform([], minval=-np.pi, maxval=np.pi, dtype=tf.float32) distorted_image = rotate(distorted_image, random_angles) # Randomly distort the colors. distorted_image = apply_with_random_selector( distorted_image, lambda x, ordering: distort_color(x, ordering), num_cases=2) # Randomly distort the colors. distorted_image = tf.subtract(distorted_image, 0.5) distorted_image = tf.multiply(distorted_image, 2.0) return distorted_image
def data_augment(input, output_size, zoom_ratio, translation, afa, beta, if_flip): '''Scaling, Rotating, Flipping via Tensorflow api in 3d space. Args: #TODO ''' outputsize3 = [CUT_RAW_VOLUME_SIZE, CUT_RAW_VOLUME_SIZE, CUT_RAW_VOLUME_SIZE] newsize = tf.cast((outputsize3)*zoom_ratio, tf.int32) #newsize = tf.concat([newlen, newlen, newlen], 0) rotate_input_len = int((3*0.5)*output_size) with tf.name_scope('resize'), tf.device('/gpu:0'): image_size = newsize[:2] first_output = tf.image.resize_images(input, image_size) transposed = tf.transpose(first_output, perm=[0,2,1]) transposed = tf.image.resize_images(transposed, image_size) zoomed = tf.transpose(transposed, perm=[0,2,1]) # Make sure the Slicing is done inside the input zone. trans_enable = tf.cast(tf.cast(newsize,tf.float32)/2- rotate_input_len/2, tf.int32) max_trans = tf.reduce_max(tf.abs(trans_enable)) max_trans = tf.cast(max_trans, tf.float32) translation = tf.cast(tf.clip_by_value(translation, -max_trans, max_trans), tf.int32) crop_left = translation + trans_enable cropped_input = tf.slice(zoomed, begin=crop_left, size=[rotate_input_len, rotate_input_len, rotate_input_len]) with tf.name_scope('rotate'), tf.device('/gpu:0'): with tf.name_scope('afa'): # afa. rotated_cropped_input = tfImage.rotate(cropped_input, afa, interpolation='BILINEAR') # beta. transposed_input = tf.transpose(rotated_cropped_input, perm=[0,2,1]) with tf.name_scope('beta'): rotated_cropped_input = tf.contrib.image.rotate(transposed_input, beta, interpolation='BILINEAR') rotated_cropped_input = tf.cond(if_flip[0] < 0.5, lambda:tf.image.flip_left_right(rotated_cropped_input), lambda: rotated_cropped_input) rotated_cropped_input = tf.transpose(rotated_cropped_input, perm=[0,2,1]) crop_l = int((rotate_input_len-output_size)/2) image = tf.slice(rotated_cropped_input, begin=[crop_l,crop_l,crop_l], size=[output_size,output_size,output_size]) with tf.name_scope('flip'), tf.device('/gpu:0'): image = tf.cond(if_flip[1] < 0.5, lambda: tf.image.flip_up_down(image), lambda: image) image = tf.cond(if_flip[2] < 0.5, lambda: tf.image.flip_left_right(image), lambda: image) return image
def _read_and_decode(filename_queue, image_pixel=28, distort=0): """Read tf records of MNIST images and labels.""" reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example( serialized_example, # Defaults are not specified since both keys are required. features={ 'image_raw': tf.FixedLenFeature([], tf.string), 'label': tf.FixedLenFeature([], tf.int64), 'height': tf.FixedLenFeature([], tf.int64), 'width': tf.FixedLenFeature([], tf.int64), 'depth': tf.FixedLenFeature([], tf.int64) }) # Convert from a scalar string tensor (whose single string has # length image_pixels) to a uint8 tensor with shape # [image_pixels]. image = tf.decode_raw(features['image_raw'], tf.uint8) image = tf.reshape(image, [image_pixel, image_pixel, 1]) print(image.get_shape()[0].value) image.set_shape([image_pixel, image_pixel, 1]) # OPTIONAL: Could reshape into a 28x28 image and apply distortions # here. Since we are not applying any distortions in this # example, and the next step expects the image to be flattened # into a vector, we don't bother. # Convert from [0, 255] -> [-0.5, 0.5] floats. image = tf.cast(image, tf.float32) * (1. / 255) if distort == 1: image = tf.reshape(image, [28, 28]) image = tf.random_crop(image, [24, 24]) # 0.26179938779 is 15 degress in radians image = contrib_image.rotate( image, random.uniform(-0.26179938779, 0.26179938779)) image = tf.reshape(image, [24, 24, 1]) elif distort == 2: image = tf.reshape(image, [28, 28]) image = tf.expand_dims(image, 2) image = tf.image.central_crop(image, central_fraction=24 / 28) image = tf.squeeze(image, 2) image = tf.reshape(image, [24, 24, 1]) # Convert label from a scalar uint8 tensor to an int32 scalar. label = tf.cast(features['label'], tf.int32) return image, label
def rotate(image, degrees, replace): """Rotates the image by degrees either clockwise or counterclockwise. Args: image: An image Tensor of type uint8. degrees: Float, a scalar angle in degrees to rotate all images by. If degrees is positive the image will be rotated clockwise otherwise it will be rotated counterclockwise. replace: A one or three value 1D tensor to fill empty pixels caused by the rotate operation. Returns: The rotated version of image. """ # Convert from degrees to radians. degrees_to_radians = math.pi / 180.0 radians = degrees * degrees_to_radians # In practice, we should randomize the rotation degrees by flipping # it negatively half the time, but that's done on 'degrees' outside # of the function. image = contrib_image.rotate(wrap(image), radians) return unwrap(image, replace)
def random_rotation(image, angle=math.pi / 180): return 255. - contrib_image.rotate(255. - tf.to_float(image), tf.random_uniform((), -angle, angle), interpolation='BILINEAR')
def mapCut(self, map_data, particle_map_length, old_partcile_states, new_particle_states): """ 给定前后坐标,根据运动方向旋转并裁剪地图,得到基于当前坐标的局部地图 :param map_data: 原始地图数据 Tensor[height,width,channel=3] int :param particle_map_length: 局部地图的形状大小,默认正方形 Tensor[1] float32 :param old_partcile_states: 前一时刻的坐标 Tensor[2](loc_x,loc_y) float32 :param new_particle_states: 当前时刻的坐标 Tensor[2](loc_x,loc_y) float32 :return: particle_map 基于运动方向的局部地图 Tensor[height,width,channel=3] float32 """ old_partcile_states = tf.cast(old_partcile_states, tf.float32) new_particle_states = tf.cast(new_particle_states, tf.float32) particle_map_length = particle_map_length map_shape = tf.shape(map_data)[0:] map_shape = tf.cast(map_shape, tf.float32) # 计算前后坐标的变化量,并以此计算运动向量的角度temp_theta dis = (tf.subtract(new_particle_states[1], old_partcile_states[1]), tf.subtract(new_particle_states[0], old_partcile_states[0])) temp_theta = tf.to_float(math_ops.atan2(dis[1], dis[0])) # 计算围绕在新坐标周围的,没有经过旋转的局部地图的四个顶点坐标 top_left_point = [ new_particle_states[0] - particle_map_length / 2, new_particle_states[1] - particle_map_length / 2 ] top_right_point = [ new_particle_states[0] + particle_map_length / 2, new_particle_states[1] - particle_map_length / 2 ] bottom_left_point = [ new_particle_states[0] - particle_map_length / 2, new_particle_states[1] + particle_map_length / 2 ] bottom_right_point = [ new_particle_states[0] + particle_map_length / 2, new_particle_states[1] + particle_map_length / 2 ] # 计算四个顶点坐标旋转后得到的新坐标 new_top_left_point = self.getRotatePoint(map_shape, new_particle_states, temp_theta, top_left_point) new_top_right_point = self.getRotatePoint(map_shape, new_particle_states, temp_theta, top_right_point) new_bottom_left_point = self.getRotatePoint(map_shape, new_particle_states, temp_theta, bottom_left_point) new_bottom_right_point = self.getRotatePoint(map_shape, new_particle_states, temp_theta, bottom_right_point) new_point = tf.reshape([ new_top_left_point, new_top_right_point, new_bottom_left_point, new_bottom_right_point ], [4, 2]) # 计算四个新坐标的横纵坐标之差的极值,该极值构成了能完整圈住旋转后的局部地图的大矩形的shape new_shape_x = tf.cast( new_point[tf.argmax(new_point[:, 0], 0), 0] - new_point[tf.argmin(new_point[:, 0], 0), 0], tf.int32) new_shape_y = tf.cast( new_point[tf.argmax(new_point[:, 1], 0), 1] - new_point[tf.argmin(new_point[:, 1], 0), 1], tf.int32) # 对四个新坐标的合法性进行判断,使其不会越出地图的边界 new_point = self.pointLegalCheck(map_shape, new_point, [new_shape_x, new_shape_y]) # 计算该大矩形的左上顶点坐标,依据大矩形的左上顶点坐标切割地图 new_corner_x = tf.cast(new_point[tf.argmin(new_point[:, 0], 0), 0], tf.int32) new_corner_y = tf.cast(new_point[tf.argmin(new_point[:, 1], 0), 1], tf.int32) map_cut = tf.image.crop_to_bounding_box(map_data, offset_height=new_corner_y, offset_width=new_corner_x, target_height=new_shape_y, target_width=new_shape_x) # 反向旋转切割后的地图,使原本的运动方向与坐标轴方向(Y轴正方向)平行 map_rotate = image.rotate(map_cut, -1.0 * temp_theta) # 计算位于居中的,我们最终需要的particle_map_length大小的局部地图的左上顶点,进行第二次切割 particle_map_length = tf.to_int32(particle_map_length) temp_corner_width = tf.cast( tf.div(tf.subtract(new_shape_x, particle_map_length), 2), tf.int32) temp_corner_height = tf.cast( tf.div(tf.subtract(new_shape_y, particle_map_length), 2), tf.int32) particle_map = tf.image.crop_to_bounding_box( map_rotate, offset_height=temp_corner_height, offset_width=temp_corner_width, target_height=particle_map_length, target_width=particle_map_length) return particle_map
def rot(self, state, img): return rotate(img.astype(np.float32), state, name='rot_image_tensor')
def random_rotate_image(image): """Do random rotate to a image (shape = (height,weigh,channels))""" with tf.device('/cpu:0'): angle = tf.random_uniform(shape=(1, ), minval=-25, maxval=25) return rotate(image, angle)
def create_instance(self, input_image, gt_image, width, height, gt_width, gt_height, scaling, flipping, rotating): original_height = 530 original_width = 500 file_learn = tf.train.string_input_producer([input_image]) gt_file = tf.train.string_input_producer([gt_image]) reader = tf.WholeFileReader() learn_key, learn_value = reader.read(file_learn) gt_key, gt_value = reader.read(gt_file) image_learn = tf.image.decode_png(learn_value) image_gt = tf.image.decode_png(gt_value) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) image_learn.eval() image_gt.eval() new_height_original = tf.to_int32(original_height) new_width_original = tf.to_int32(original_width) image_learn = tf.image.resize_images( image_learn, [new_width_original, new_height_original]) image_gt = tf.image.resize_images( image_gt, [new_width_original, new_height_original]) image_learn = tf.convert_to_tensor(image_learn.eval()) image_gt = tf.convert_to_tensor(image_gt.eval()) image_learn = tf.image.flip_left_right(image_learn) image_gt = tf.image.flip_left_right(image_gt) image_learn = rotate(image_learn, angles=360, interpolation='BILINEAR') image_gt = rotate(image_gt, angles=360, interpolation='NEAREST') new_height_learn = tf.to_int32(height) new_width_learn = tf.to_int32(width) new_height_gt = tf.to_int32(gt_height) new_width_gt = tf.to_int32(gt_width) image_learn = tf.image.resize_images( image_learn, [new_width_learn, new_height_learn]) image_gt = tf.image.resize_images(image_gt, [new_width_gt, new_height_gt]) n_instances = tf.reduce_max(image_gt) n_instances = n_instances.eval() numberofInstancesPerImage = n_instances nPixels = width * height gt_tensor = tf.zeros([n_instances, gt_width, gt_height], tf.int32) gt_tensor_value = gt_tensor.eval() max_val = tf.to_float(numberofInstancesPerImage) for i in range(0, n_instances): current_mask_value = gt_tensor_value[i, :, :] current_mask_value[tf.cast(tf.greater_equal(image_gt, max_val), tf.int32).eval()] = 1 gt_tensor_value[tf.cast(tf.greater_equal(image_gt, max_val), tf.int32).eval()] = 0 current_mask = tf.convert_to_tensor(current_mask_value) image_gt = tf.convert_to_tensor(gt_tensor_value) coord.request_stop() coord.join(threads) return image_learn, image_gt, current_mask