def augmentate(image, for_classify=False): if not for_classify: return tf.image.random_flip_left_right(image) threshold = tf.constant(0.1) r = tf.random.uniform([], 0.0, 1.0) image = tf.cond(greater(threshold, r), lambda: tf.image.flip_left_right(image), lambda: tf.identity(image)) r = tf.random.uniform([], 0.0, 1.0) image = tf.cond( greater(threshold, r), lambda: tf.contrib.image.rotate(image, (r - 0.05) * 3.14 * 900 / 180), lambda: tf.identity(image)) def crop_with_resize(image): image = tf.image.random_crop(image, [128, 128, 3]) return tf.image.resize_images(image, [160, 160], tf.image.ResizeMethod.NEAREST_NEIGHBOR) def crop_with_pad(image): image = tf.image.random_crop(image, [128, 128, 3]) return tf.image.resize_image_with_crop_or_pad(image, 160, 160) r = tf.random.uniform([], 0.0, 1.0) image = tf.cond( tf.logical_and(greater(threshold, r), greater(r, threshold / 2)), lambda: crop_with_resize(image), lambda: tf.identity(image)) image = tf.cond(greater(threshold / 2, r), lambda: crop_with_pad(image), lambda: tf.identity(image)) r = tf.random.uniform([], 0.0, 1.0) image = tf.cond(greater(threshold / 4, r), lambda: tf.image.random_brightness(image, 0.4), lambda: tf.identity(image)) r = tf.random.uniform([], 0.0, 1.0) image = tf.cond(greater(threshold / 4, r), lambda: tf.image.random_contrast(image, 0.5, 1.8), lambda: tf.identity(image)) r = tf.random.uniform([], 0.0, 1.0) image = tf.cond(greater(threshold / 4, r), lambda: tf.image.random_hue(image, 0.1), lambda: tf.identity(image)) r = tf.random.uniform([], 0.0, 1.0) image = tf.cond(greater(threshold / 4, r), lambda: tf.image.random_saturation(image, 0.8, 1.1), lambda: tf.identity(image)) return image
def adaptive_wing_loss(labels, output): alpha = 2.1 omega = 14 epsilon = 1 theta = 0.5 with tf.name_scope('adaptive_wing_loss'): x = output - labels theta_over_epsilon_tensor = tf.fill(tf.shape(labels), theta/epsilon) A = omega*(1/(1+pow(theta_over_epsilon_tensor, alpha-labels)))*(alpha-labels)*pow(theta_over_epsilon_tensor, alpha-labels-1)*(1/epsilon) C = theta*A-omega*log(1+pow(theta_over_epsilon_tensor, alpha-labels)) absolute_x = abs(x) losses = tf.where(greater(theta, absolute_x), omega*log(1+pow(absolute_x/epsilon, alpha-labels)), A*absolute_x-C) loss = reduce_mean(reduce_sum(losses, axis=[1, 2]), axis=0) return loss
def body(i, extras, n_set, current_val, seg): def case1(i, extras, n_set, current_val: tf.Tensor, seg): # current_val += 0 extras -= 1 n_set += 1 seg = tf.sparse.add( seg, tf.SparseTensor(indices=[[0, i]], values=[current_val], dense_shape=[1, n])) return extras, n_set, current_val, seg def case2(i, extras, n_set, current_val, seg): current_val += 1 n_set -= n_set n_set += 1 seg = tf.sparse.add( seg, tf.SparseTensor(indices=[[0, i]], values=[current_val], dense_shape=[1, n])) return extras, n_set, current_val, seg def case3(i, extras, n_set, current_val, seg): # current_val += 0 n_set += 1 seg = tf.sparse.add( seg, tf.SparseTensor(indices=[[0, i]], values=[current_val], dense_shape=[1, n])) return extras, n_set, current_val, seg extras, n_set, current_val, seg = tf.cond( logical_and(greater(extras, 0), equal(n_set, y)), lambda: case1(i, extras, n_set, current_val, seg), lambda: tf.cond( greater_equal(n_set, y), lambda: case2( i, extras, n_set, current_val, seg), lambda: case3( i, extras, n_set, current_val, seg))) return b(i), extras, n_set, current_val, seg
def decode_and_preprocess_func(file, label=None): image = tf.image.decode_and_crop_jpeg(file, [29, 9, 160, 160]) #image = tf.image.resize_images(image, [128, 128], tf.image.ResizeMethod.BICUBIC) if augmentation: image = augmentate(image, for_classify) image.set_shape((160, 160, 3)) image = tf.cast(image, tf.float32) if augmentation and for_classify: r = tf.random.uniform([], 0.0, 1.0) image = tf.cond(greater(tf.constant(0.1), r), lambda: tf.image.per_image_standardization(image), lambda: image / 127.5 - 1) else: image = image / 127.5 - 1 if for_classify: return image, label else: return image, image
def basis2(self, x): # b_2_k = K.variable(np.zeros(x.shape[0], )) k_1 = tf.constant(self.breakpoints[self.k - 1], dtype=tf.float32) # print(k_1.shape) k = tf.constant(self.breakpoints[self.k], dtype=tf.float32) def f1(): return tfm.add( tfm.subtract(tfm.divide(tfm.multiply(x, x), 2), tfm.multiply(k_1, x)), tfm.divide(tfm.multiply(k_1, k_1), 2)) def f2(): val1 = tfm.divide( tfm.multiply(tfm.subtract(k, k_1), tfm.subtract(k, k_1)), 2) val2 = tfm.multiply(tfm.subtract(k, k_1), tfm.subtract(x, k)) val = tfm.add(val1, val2) return val b2ks = [0] * x.shape[0] for i in range(x.shape[0]): b2ks[i] = tf.cond( tfm.logical_and(tfm.greater(x[i, 0], k_1), tfm.less(x[i, 0], k)), lambda: f1(), lambda: f2()) # print(b2ks[i].shape) b_2_k = K.concatenate(b2ks) # if tfm.greater(x, k_1) and tfm.less(x, k): # elif tfm.less(k, x): return tfm.multiply(self.w[self.i][self.k - 1], b_2_k)
def decode_and_preprocess_func(file, label=None): image = tf.image.decode_and_crop_jpeg(file, [29, 9, 160, 160]) #image = tf.image.resize_images(image, [128, 128], tf.image.ResizeMethod.BICUBIC) if aug_level: image = augmentate(image, level=aug_level) image.set_shape((160, 160, 3)) image = tf.cast(image, tf.float32) if aug_level == 'enhanced': r = tf.random.uniform([], 0.0, 1.0) image = tf.cond(greater(tf.constant(0.1), r), lambda: tf.image.per_image_standardization(image), lambda: image / 127.5 - 1) else: image = image / 127.5 - 1 if consumer == 'classifier': return image, label elif consumer == 'vae': return image, image elif consumer == 'translator': return image, label '''
def dice_whole_tumor(y_true, y_pred): mask_true, mask_pred = get_one_hot_from_output(y_true, y_pred) mask_true = tfmth.greater(mask_true, 0) mask_pred = tfmth.greater(mask_pred, 0) return dice_coefficient(mask_true, mask_pred)
def tf_ssim(x, y, is_normalized=False): """ k1 = 0.01 k2 = 0.03 L = 1.0 if is_normalized else 255.0 c1 = np.power(k1 * L, 2) c2 = np.power(k2 * L, 2) c3 = c2 / 2 """ k1 = 0.01 k2 = 0.03 L = 1.0 if is_normalized else 255.0 c1 = tf_pow(multiply(k1, L), 2.0) c2 = tf_pow(multiply(k2, L), 2.0) c3 = divide(c2, 2.0) # if type(x) is np.ndarray: # x = tf.convert_to_tensor(x, dtype=tf.float32) # if type(y) is np.ndarray: # y = tf.convert_to_tensor(y, dtype=tf.float32) """ ux = x.mean() uy = y.mean() """ ux = tf_mean(x) uy = tf_mean(y) """ std_x = x.std() std_y = y.std() """ std_x = tf_std(x) std_y = tf_std(y) """ xy = (x - ux) * (y - uy) std_xy = xy.mean() """ xy = multiply(subtract(x, ux), subtract(y, uy)) std_xy = tf_mean(xy) """ l_xy = (2 * ux * uy + c1) / (np.power(ux, 2) + np.power(uy, 2) + c1) """ l_son = add(multiOperation(multiply, 2.0, ux, uy), c1) l_mom = multiOperation(add, tf_pow(ux, 2.0), tf_pow(uy, 2.0), c1) l_xy = divide(l_son, l_mom) """ c_xy = (2 * std_x * std_y + c2) / (np.power(std_x, 2) + np.power(std_y, 2) + c2) """ c_son = add(multiOperation(multiply, 2.0, std_x, std_y), c2) c_mom = multiOperation(add, tf_pow(std_x, 2.0), tf_pow(std_y, 2.0), c2) c_xy = divide(c_son, c_mom) """ s_xy = (std_xy + c3) / (std_x * std_y + c3) """ s_son = add(std_xy, c3) s_mom = add(multiply(std_x, std_y), c3) s_xy = divide(s_son, s_mom) one = tf.constant(1.0) _ssim = multiOperation(multiply, l_xy, c_xy, s_xy) _result = tf.cond(greater(_ssim, one), lambda: one, lambda: _ssim) return _result