def call(self, tensor_in, training=False, **kwargs): root = self.root(tensor_in, training=training) encoder1a = self.encoder1a(root, training=training) encoder1a_skip = self.encoder1a_skip(encoder1a, training=training) encoder1b = self.encoder1b(encoder1a, training=training) encoder2a = self.encoder2a(encoder1b, training=training) encoder2a_skip = self.encoder2a_skip(encoder2a, training=training) encoder2b = self.encoder2b(encoder2a, training=training) encoder3a = self.encoder3a(encoder2b, training=training) encoder3a_skip = self.encoder3a_skip(encoder3a, training=training) encoder3b = self.encoder3b(encoder3a, training=training) encoder4a = self.encoder4a(encoder3b, training=training) encoder4b = self.encoder4b(encoder4a, training=training) decoder1 = self.decoder1(encoder4b, training=training) _, h, w, _ = get_shape(encoder3a_skip) decoder1 = tf.image.resize(decoder1, [h, w]) + encoder3a_skip decoder2 = self.decoder2(decoder1, training=training) _, h, w, _ = get_shape(encoder2a_skip) decoder2 = tf.image.resize(decoder2, [h, w]) + encoder2a_skip decoder3 = self.decoder3(decoder2, training=training) _, h, w, _ = get_shape(encoder1a_skip) decoder3 = tf.image.resize(decoder3, [h, w]) + encoder1a_skip return self.logit(decoder3)
def _preprocessing(self, image): # random resize 1~1.3 if self.is_train: scale = tf.random.uniform([], minval=1.0, maxval=1.3, dtype=tf.float32) h, w, _ = get_shape(image) new_h = tf.cast(h, tf.float32) * scale new_w = tf.cast(w, tf.float32) * scale new_dim = tf.cast([new_h, new_w], tf.int32) image = tf.squeeze( tf.image.resize_bilinear(tf.expand_dims(image, 0), new_dim, align_corners=True), [0]) image = tf.image.random_crop(image, [28, 28, 1]) # random rotate angle = tf.random.uniform((), minval=-20, maxval=20, dtype=tf.float32) image = tf.contrib.image.rotate(image, angle, interpolation='BILINEAR') else: image = tf.cast(image, tf.float32) image.set_shape([28, 28, 1]) return image / 255.0
def __init__(self, train_data, common, name): self.common = common self.name = name self.conv_size = [] self.conv_depth = [] self.conv_stride = [] self.model_length = randint(self.common.model_depth_range[0], self.common.model_depth_range[1] + 1) for i in range(self.model_length): self.conv_size.append( randint(self.common.conv_size_range[0], self.common.conv_size_range[1] + 1)) self.conv_depth.append( randint(self.common.conv_depth_range[0], self.common.conv_depth_range[1] + 1)) self.conv_stride.append( randint(self.common.conv_stride_range[0], self.common.conv_stride_range[1] + 1)) logit = self.build(train_data["image"], True) # count number of parameters all_trainables = tf.trainable_variables() self.total_parameters = 0 for variable in all_trainables: if "batch_normalization" not in variable.name: self.total_parameters += prod( [int(para) for para in get_shape(variable)]) self.loss = self.xntropy(logit, train_data["gt"]) self.encode_gene()
def execute_fn(image, gt, shred_range): image_shape = get_shape(image) gt_shape = get_shape(gt) shred_num = tf.random.uniform([], minval=shred_range[0], maxval=shred_range[1] + 1, dtype=tf.uint8) for split_axis in [0, 1]: split_indices = np.linspace(0, image_shape[split_axis], shred_num + 1, dtype=np.int32) # tf.linspace(0, image_shape[split_axis], shred_num+1) split_indices = split_indices[1:] - split_indices[:-1] splitted_image = tf.split(image, split_indices, split_axis) splitted_gt = tf.split(gt, split_indices, split_axis) pad_size = int(image_shape[split_axis] * self.aug_config.shred_shift_ratio) padded_image_container = [] padded_gt_container = [] for strip_image, strip_gt in zip(splitted_image, splitted_gt): rnd0 = tf.random.uniform((), maxval=2, dtype=tf.int32) rnd1 = 1 - rnd0 range1 = rnd0 * pad_size range2 = rnd1 * pad_size pad = tf.cond(tf.equal(split_axis, 0), lambda: [[0, 0], [range1, range2], [0, 0]], lambda: [[range1, range2], [0, 0], [0, 0]]) padded_image_container.append( tf.pad(strip_image, pad, "REFLECT")) padded_gt_container.append(tf.pad(strip_gt, pad, "REFLECT")) shredded_image = tf.concat(padded_image_container, split_axis) shredded_gt = tf.concat(padded_gt_container, split_axis) range1 = int(pad_size * 0.5) range2 = pad_size - range1 shredded_image = tf.cond( tf.equal(split_axis, 0), lambda: shredded_image[:, range1:-range2, :], lambda: shredded_image[range1:-range2, ::]) shredded_gt = tf.cond( tf.equal(split_axis, 0), lambda: shredded_gt[:, range1:-range2, :], lambda: shredded_gt[range1:-range2, ::]) shredded_image.set_shape(image_shape) shredded_gt.set_shape(gt_shape) return shredded_image, shredded_gt
def execute_fn(shade_src, image): shade_n, shade_h, shade_w, shade_c = get_shape(shade_src) image_h, image_w, image_c = get_shape(image) min_shade_length = tf.cast(tf.reduce_min([shade_h, shade_w]), tf.float32) max_image_length = tf.cast(tf.reduce_max([image_h, image_w]), tf.float32) def reverse_value(shade_source): return shade_source * -1 + 1 shade_src = tf.cond( tf.equal(tf.random.uniform((), maxval=2, dtype=tf.int32), tf.constant(1)), lambda: reverse_value(shade_src), lambda: shade_src) scale_factor = max_image_length / min_shade_length rnd_modifier = tf.random.uniform([], minval=1.0, maxval=2.0) shade_h = tf.cast( tf.cast(shade_h, tf.float32) * scale_factor * rnd_modifier, tf.int32) shade_w = tf.cast( tf.cast(shade_w, tf.float32) * scale_factor * rnd_modifier, tf.int32) shade_src = tf.cond( tf.not_equal(max_image_length, min_shade_length), lambda: tf.image.resize_nearest_neighbor( shade_src, [shade_h, shade_w], align_corners=True), lambda: shade_src) # now shade is always bigger than image size # random crop shade_src = tf.squeeze(shade_src, axis=0) shade_src = tf.image.random_crop(shade_src, [image_h, image_w, 1]) alpha = tf.random.uniform((), minval=0.3, maxval=1.0, dtype=tf.float32) shade = shade_src * alpha case_true = tf.reshape( tf.multiply( tf.ones(get_shape(tf.reshape(shade, [-1])), tf.float32), 1.0), get_shape(shade)) case_false = shade shade = tf.where(tf.equal(shade, 0), case_true, case_false) return tf.multiply(tf.cast(image, tf.float32), shade)
def build(self, image, is_train): net = image with tf.variable_scope("model", reuse=tf.AUTO_REUSE): for conv_size, conv_depth, conv_stride in zip( self.conv_size, self.conv_depth, self.conv_stride): net = self.conv_block(net, conv_size, conv_stride, conv_depth, is_train, False) _, h, w, c = get_shape(net) net = tf.layers.average_pooling2d(net, [h, w], 1) return tf.squeeze( self.conv_block(net, 1, 1, self.common.num_classes, is_train, True))
def miou_loss(gt, logit): # calculated bache mean intersection over union loss prob_map = tf.nn.softmax(logit) onehot_gt = tf.one_hot(tf.cast(tf.squeeze(gt, 3), tf.uint8), get_shape(logit)[-1]) if prob_map.shape.as_list() != onehot_gt.shape.as_list(): raise ValueError('dimension mismatching') # calculate iou loss intersection_logit = prob_map * onehot_gt # [batch, height, width, class] union_logit = prob_map + onehot_gt - intersection_logit # [batch, height, width, class] iou_logit = tf.reduce_sum(intersection_logit, [0, 1, 2]) / tf.reduce_sum(union_logit, [0, 1, 2]) # class miou_logit = tf.reduce_mean(iou_logit) return 1.0 - tf.reduce_mean(miou_logit)
def call(self, tensor_in, **kwargs): n, h, w, c = get_shape(tensor_in) flatten = tf.reshape(tensor_in, [n, h * w, c]) context = self.context_conv(tensor_in) context = tf.reshape(context, [n, h * w, 1]) context = softmax(context, axis=1) context = tf.matmul(flatten, context, transpose_a=True) context = tf.reshape(context, [n, 1, 1, c]) transform = self.transform_shrink_conv(context) transform = self.transform_lnorm(transform) transform = relu(transform) transform = self.transform_expand_conv(transform) transform = sigmoid(transform) return tensor_in + transform
def __init__(self, train_data, offspring_gene, common, name): self.name = name self.conv_size = offspring_gene["conv_size"] self.conv_depth = offspring_gene["conv_depth"] self.conv_stride = offspring_gene["conv_stride"] self.model_length = len(self.conv_size) super(Offspring, self).__init__(train_data, common, name) logit = self.build(train_data["image"], True) # count number of parameters all_trainables = tf.trainable_variables() self.total_parameters = 0 for variable in all_trainables: if "batch_normalization" not in variable.name: self.total_parameters += prod( [int(para) for para in get_shape(variable)]) self.loss = self.xntropy(logit, train_data["gt"])
def _randomly_scale_image_and_label(self, image, gt): """Randomly scales image and label. Args: image: Image with original_shape [height, width, 3]. label: Label with original_shape [height, width, 1]. scale: The value to scale image and label. Returns: Scaled image and label. """ scale = self._get_random_scale() h, w, c = get_shape(image) new_dim = tf.cast(tf.cast([h, w], tf.float32) * scale, tf.int32) image = tf.image.resize(image, new_dim, method='area') gt = tf.image.resize(gt, new_dim, method='nearest') # gt = tf.image.resize(tf.expand_dims(gt, 2), new_dim, method='nearest') # in debugging return image, gt