def dice_coef(y_true, y_pred, smooth, thresh): #y_pred = (y_pred > thresh).astype(float) y_true_f = KB.flatten(y_true) y_pred_f = KB.flatten(y_pred) intersection = KB.sum(y_true_f * y_pred_f, axis=-1) return (2. * intersection + smooth) / (KB.sum(KB.square(y_true_f), axis=-1) + KB.sum(KB.square(y_pred_f), axis=-1) + smooth)
def dice_coeff(y_true, y_pred): smooth = 0. y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) intersection = K.sum(y_true_f * y_pred_f) score = (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth) return score
def dice_coef(y_true, y_pred): #print((y_true).shape) y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) intersection = K.sum(y_true_f * y_pred_f) return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)
def vae_loss(x, x_decoded_mean): x = K.flatten(x) x_decoded_mean = K.flatten(x_decoded_mean) xent_loss = max_length * objectives.binary_crossentropy(x, x_decoded_mean) kl_loss = -0.5 * K.mean( 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1) return xent_loss + kl_loss
def sigmoid_cross_entropy(y_true, y_pred): z = K.flatten(y_true) x = K.flatten(y_pred) q = 10 l = (1 + (q - 1) * z) loss = (K.sum((1 - z) * x) + K.sum(l * (K.log(1 + K.exp(- K.abs(x))) + K.max(-x, 0)))) / 500 return loss
def vae_loss(x, x_decoded_mean): # NOTE: binary_crossentropy expects a batch_size by dim for x and x_decoded_mean, so we MUST flatten these! x = K.flatten(x) x_decoded_mean = K.flatten(x_decoded_mean) xent_loss = np.dot(original_dim, original_dim) * objectives.binary_crossentropy(x, x_decoded_mean) kl_loss = - 0.5 * K.mean(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1) return xent_loss + kl_loss
def recall(y_true, y_pred): y_true_f = KB.flatten(y_true) y_pred_f = KB.flatten(y_pred) true_positives = true_positive(y_true_f, y_pred_f) possible_positives = possible_positive(y_true_f, y_pred_f) recall = true_positives / (possible_positives + KB.epsilon()) return recall
def dice_loss_K(y_true,y_pred): y_true=K.flatten(y_true) y_pred=K.flatten(y_pred) intersection=K.sum(y_true*y_pred) denominator=(K.sum(y_true)+K.sum(y_pred)) score=(2*intersection)/denominator return 1-score
def dice_coef(y_true,y_pred): y_true = K.flatten(y_true) y_pred = K.flatten(y_pred) smooth = 0. intersection = K.sum(y_true*y_pred) return (2. * intersection + smooth) / (K.sum(y_true) + K.sum(y_pred) + smooth)
def vae_loss(x, x_decoded_mean): # NOTE: binary_crossentropy expects a batch_size by dim # for x and x_decoded_mean, so we MUST flatten these! x = K.flatten(x) x_decoded_mean = K.flatten(x_decoded_mean) xent_loss = img_rows * img_cols * metrics.binary_crossentropy(x, x_decoded_mean) kl_loss = - 0.5 * K.mean(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1) return xent_loss + kl_loss
def f1(y_true, y_pred): y_true_f = KB.flatten(y_true) y_pred_f = KB.flatten(y_pred) true_positives = KB.sum(KB.round(KB.clip(y_true_f * y_pred_f, 0, 1)), axis=-1) possible_positives = KB.sum(KB.round(KB.clip(y_true_f, 0, 1)), axis=-1) recall = true_positives / (possible_positives + KB.epsilon()) predicted_positives = KB.sum(KB.round(KB.clip(y_pred_f, 0, 1)), axis=-1) precision = true_positives / (predicted_positives + KB.epsilon()) return 2*((precision*recall)/(precision+recall+KB.epsilon()))
def vae_loss(self, x, x_decoded_mean_squash, z_mean, z_log_var): x = K.flatten(x) x_decoded_mean_squash = K.flatten(x_decoded_mean_squash) img_rows, img_cols = self._img_rows, self._img_cols # generative or reconstruction loss xent_loss = img_rows * img_cols * \ metrics.binary_crossentropy(x, x_decoded_mean_squash) # Kullback-Leibler divergence loss kl_loss = - 0.5 * K.mean( 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1) return K.mean(xent_loss + kl_loss)
def jacc_coef_th(y_true, y_pred, smooth=smooth_default): y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) intersection = K.sum(y_true_f * y_pred_f) jacc = (intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) - intersection + smooth) result = K.switch( jacc > 0.65, jacc, jacc * 0.1 ) return result
def dice_coef(y_true, y_pred): """Dice coefficient specification :param y_true: ground truth. :param y_pred: predictions. """ dice_smooth_factor = 1.0 y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) intersection = K.sum(y_true_f * y_pred_f) return (2. * intersection + dice_smooth_factor) / \ (K.sum(y_true_f) + K.sum(y_pred_f) + dice_smooth_factor)
def dice_coef(y_true, y_pred, smooth=smooth_default, per_batch=True): if not per_batch: y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) intersection = K.sum(y_true_f * y_pred_f) return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth) else: y_true_f = K.batch_flatten(y_true) y_pred_f = K.batch_flatten(y_pred) intersec = 2. * K.sum(y_true_f * y_pred_f, axis=1, keepdims=True) + smooth union = K.sum(y_true_f, axis=1, keepdims=True) + K.sum(y_pred_f, axis=1, keepdims=True) + smooth return K.mean(intersec / union)
def dice_coef(y_true, y_pred): y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) #p=y_true.eval() #print(p) if K.max(y_true_f)==0: y_true_ff=1-y_true_f y_pred_ff=1-y_pred_f intersection = K.sum(y_true_ff * y_pred_ff) return (2. * intersection + config["smooth"]) / (K.sum(y_true_ff) + K.sum(y_pred_ff) + config["smooth"]) else: intersection = K.sum(y_true_f * y_pred_f) return (2. * intersection + config["smooth"]) / (K.sum(y_true_f) + K.sum(y_pred_f) + config["smooth"])
def _make_regular_grids(self, batch_size, height, width): # making a single regular grid x_linspace = K_linspace(-1., 1., width) y_linspace = K_linspace(-1., 1., height) x_coordinates, y_coordinates = K_meshgrid(x_linspace, y_linspace) x_coordinates = K.flatten(x_coordinates) y_coordinates = K.flatten(y_coordinates) ones = K.ones_like(x_coordinates) grid = K.concatenate([x_coordinates, y_coordinates, ones], 0) # repeating grids for each batch grid = K.flatten(grid) grids = K.tile(grid, K.stack([batch_size])) return K.reshape(grids, (batch_size, 3, height * width))
def pixel_wise_coef(y_true, y_pred): # # DESCRIPTION # Computed a pixel wise distance from the true labels, # sum the absolute value and divide by the number of pixels to give a ratio # # INPUTS # y_true, y_pred # OUTPUTS # pixel_wise_coef between 0 and 1 # y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) summed_error = tf.reduce_sum(tf.abs(y_true_f - y_pred_f)) return tf.div(summed_error, 160000)
def sparse_accuracy_ignoring_last_label(y_true, y_pred): nb_classes = K.int_shape(y_pred)[-1] y_pred = K.reshape(y_pred, (-1, nb_classes)) y_true = K.one_hot(tf.to_int32(K.flatten(y_true)), nb_classes + 1) unpacked = tf.unstack(y_true, axis=-1) legal_labels = ~tf.cast(unpacked[-1], tf.bool) y_true = tf.stack(unpacked[:-1], axis=-1) return K.sum(tf.to_float(legal_labels & K.equal(K.argmax(y_true, axis=-1), K.argmax(y_pred, axis=-1)))) / K.sum(tf.to_float(legal_labels))
def softmax_sparse_crossentropy_ignoring_last_label(y_true, y_pred): y_pred = K.reshape(y_pred, (-1, K.int_shape(y_pred)[-1])) log_softmax = tf.nn.log_softmax(y_pred) y_true = K.one_hot(tf.to_int32(K.flatten(y_true)), K.int_shape(y_pred)[-1]+1) unpacked = tf.unstack(y_true, axis=-1) y_true = tf.stack(unpacked[:-1], axis=-1) cross_entropy = -K.sum(y_true * log_softmax, axis=1) cross_entropy_mean = K.mean(cross_entropy) return cross_entropy_mean
def get_output(self, train=False): """Shift the sequence the input X is (nb_samples, nb_timesteps, input_dim) """ if train: X = self.get_input(train) else: X = self.sequence_layer.get_output() head = K.expand_dims(K.flatten(self.initial_layer.get_output()), 1) output = K.concatenate((head, X[:, :-1, :]), axis=1) return output
def transform(position, theta): (batchSize, targetDim) = K.shape(position) # Reshaping the positions position = K.reshape(K.flatten(position), (batchSize, 2, -1)) position = K.permute_dimensions(position, (0, 2, 1)) position = K.concatenate([position, T.ones((batchSize, 1, K.shape(position)[2]))], axis=1) # Applying the transformation position = K.batch_dot(theta, position)[:, :2, :] # Reshaping the result position = K.permute_dimensions(position, (0, 2, 1)) position = K.reshape(position, (batchSize, targetDim)) return position
def step(self, x, states): input_shape = (self.batch_size, ) + self.reshape_dim hidden_dim = (self.batch_size, ) + self.output_dim nb_filter, nb_rows, nb_cols = self.output_dim h_tm1 = K.reshape(states[0], hidden_dim) x_t = K.reshape(x, input_shape) xz_t = apply_layer(self.conv_x_z, x_t) xr_t = apply_layer(self.conv_x_r, x_t) xh_t = apply_layer(self.conv_x_h, x_t) xz_t = apply_layer(self.max_pool, xz_t) xr_t = apply_layer(self.max_pool, xr_t) xh_t = apply_layer(self.max_pool, xh_t) z = self.inner_activation(xz_t + apply_layer(self.conv_z, h_tm1)) r = self.inner_activation(xr_t + apply_layer(self.conv_r, h_tm1)) hh_t = self.activation(xh_t + apply_layer(self.conv_h, r * h_tm1)) h_t = z * h_tm1 + (1 - z) * hh_t h_t = K.flatten(h_t) return h_t, [h_t, ]
def attention_step(self, x, args): ''' Attention step function #Arguments args: [h_, context, projected_context, W_h_prj, w_prj_att, b_att, W_x_h, U_h_h, W_ctx_h, b_h W_x_p, W_h_p, W_ctx_p, b_p]. h_: (batch_size, dim_hidden) context: (batch_size, nb_context, dim_context) projected_context: (batch_size, nb_context, dim_projected_context) projected_context = context dot W_ctx_prj + b_ctx_prj calculated before step. W_h_prj: (dim_hidden, dim_projected_context) w_prj_att: (dim_projected_context, 1) b_att: (1,) W_x_h: (dim_embedding, dim_hidden) U_h_h: (dim_hidden, dim_hidden) W_ctx_h: (dim_context, dim_hidden) b_h: (dim_hidden,) ''' assert len(args) == 1 + len(self.contexts) + len(self.params) [h_, context, projected_context, W_h_prj, w_prj_att, b_att, W_x_h, U_h_h, W_ctx_h, b_h] = args projected = K.expand_dims(K.dot(h_, W_h_prj), 1) + projected_context e = K.dot(K.tanh(projected), w_prj_att) + b_att alpha = K.softmax(K.flatten(e)) weighted_context = K.sum((context * K.expand_dims(alpha)), 1) pre_act = K.dot(x, W_x_h) + K.dot(h_, U_h_h) + K.dot(weighted_context, W_ctx_h) + b_h h = K.sigmoid(pre_act) return h, [alpha, weighted_context], [h]
x_test = np.load('MNIST_test_img.npy') y_test_pred = np.load('MNIST_ResNet50_test_pred_label.npy') #x perturbation #with tf.device('/cpu:0'): model = load_model( "training_lr1en4_MNIST_ResNet50_epoch151to200_dropout_128_best.h5") ###Setup 3: training and test sets wrt sum squared prob grad_K = tf.concat( axis=1, values=[ tf.concat(axis=0, values=[ K.flatten(b)[..., None] for b in K.gradients(model.output[0, k], model.input) ]) for k in range(n_class) ]) iterate = K.function([model.input], [grad_K, model.output]) FI_train = np.zeros(int(x_train.shape[0] / 1000.0)) FI_train_pred = np.zeros(int(x_train.shape[0] / 1000.0)) #for i in range(x_train.shape[0]): t = 0 for i in range(int(x_train.shape[0] / 1000.0 * (set_part - 1)), int(x_train.shape[0] / 1000.0 * set_part)): grad, pred_P = iterate([x_train[i][None]])
def dice_coef_sqr(y_true, y_pred): y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) intersection = K.square(K.sum(y_true_f * K.square(y_pred_f))) return (2. * intersection + DICE_SMOOTH) / \ (K.sum(K.square(y_true_f)) + K.sum(K.square(y_pred_f)) + DICE_SMOOTH)
def get_nz_parameters(self): return K.sum(K.concatenate([K.flatten(self.kernel_mask), K.flatten(self.bias_mask)]))
# latent_rep = E(X)[0] # output = G(latent_rep) E_mean, E_logsigma, Z = E(X) # Z = Input(shape=(512,)) # Z2 = Input(shape=(batch_size, 512)) output = G(Z) G_dec = G(E_mean + E_logsigma) D_fake, F_fake = D(output) D_fromGen, F_fromGen = D(G_dec) D_true, F_true = D(X) VAE = Model(X, output) kl = - 0.5 * K.sum(1 + E_logsigma - K.square(E_mean) - K.exp(E_logsigma), axis=-1) crossent = 64 * metrics.mse(K.flatten(X), K.flatten(output)) VAEloss = K.mean(crossent + kl) VAE.add_loss(VAEloss) VAE.compile(optimizer=SGDop) for epoch in range(epochs): latent_vect = E.predict(dataset)[0] encImg = G.predict(latent_vect) fakeImg = G.predict(noise) DlossTrue = D_true.train_on_batch(dataset, np.ones((batch_size, 1))) DlossEnc = D_fromGen.train_on_batch(encImg, np.ones((batch_size, 1))) DlossFake = D_fake.train_on_batch(fakeImg, np.zeros((batch_size, 1))) cnt = epoch while cnt > 3:
def dice_coef(y_true, y_pred, smooth=1.): y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) intersection = K.sum(y_true_f * y_pred_f) return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)
def dice_coef(y_true, y_pred): y_true_f = keras.flatten(y_true) y_pred_f = keras.flatten(y_pred) intersection = keras.sum(y_true_f * y_pred_f) return (2. * intersection + 1) / (keras.sum(y_true_f) + keras.sum(y_pred_f) + 1)
def build_model (): x = Input(shape=original_img_size, name='input') conv_1 = Conv2D(img_chns, kernel_size=(2, 2), padding='same', activation='relu', name='conv_1')(x) conv_2 = Conv2D(filters, kernel_size=(2, 2), padding='same', activation='relu', name='conv_2', strides=(2, 2))(conv_1) conv_3 = Conv2D(filters, kernel_size=num_conv, padding='same', activation='relu', name='conv_3', strides=1)(conv_2) conv_4 = Conv2D(filters, kernel_size=num_conv, padding='same', activation='relu', name='conv_4', strides=1)(conv_3) flat = Flatten(name='flat')(conv_4) hidden = Dense(intermediate_dim, activation='relu', name='hidden')(flat) z_mean = Dense(latent_dim, name='z_mean')(hidden) z_log_var = Dense(latent_dim, name='z_log_var')(hidden) # note that "output_shape" isn't necessary with the TensorFlow backend # so you could write `Lambda(sampling)([z_mean, z_log_var])` z = Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var]) # we instantiate these layers separately so as to reuse them later decoder_hid = Dense(intermediate_dim, activation='relu', name='decoder_hid') decoder_upsample = Dense(filters * up_dim * up_dim, activation='relu', name='decoder_upsample') if K.image_data_format() == 'channels_first': output_shape = (batch_size, filters, up_dim, up_dim) else: output_shape = (batch_size, up_dim, up_dim, filters) decoder_reshape = Reshape(output_shape[1:], name='decoder_reshape') decoder_deconv_1 = Conv2DTranspose(filters, kernel_size=num_conv, padding='same', strides=1, activation='relu', name='decoder_deconv_1') decoder_deconv_2 = Conv2DTranspose(filters, kernel_size=num_conv, padding='same', strides=1, activation='relu', name='decoder_deconv_2') decoder_deconv_3_upsamp = Conv2DTranspose(filters, kernel_size=(3, 3), strides=(2, 2), padding='valid', activation='relu', name='decoder_deconv_3_upsamp') decoder_mean_squash = Conv2D(img_chns, kernel_size=2, padding='valid', activation='sigmoid', name='decoder_mean_squash') hid_decoded = decoder_hid(z) up_decoded = decoder_upsample(hid_decoded) reshape_decoded = decoder_reshape(up_decoded) deconv_1_decoded = decoder_deconv_1(reshape_decoded) deconv_2_decoded = decoder_deconv_2(deconv_1_decoded) x_decoded_relu = decoder_deconv_3_upsamp(deconv_2_decoded) x_decoded_mean_squash = decoder_mean_squash(x_decoded_relu) # instantiate VAE model vae = Model(x, x_decoded_mean_squash) # save model as json with open(mpath, 'w') as outfile: json.dump(vae.to_json(), outfile) # Compute VAE loss xent_loss = img_rows * img_cols * metrics.binary_crossentropy( K.flatten(x), K.flatten(x_decoded_mean_squash)) kl_loss = - 0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1) vae_loss = K.mean(xent_loss + kl_loss) vae.add_loss(vae_loss) vae.compile(optimizer='rmsprop') # We also instantiate the encoder and decoder # encoder: a model to project inputs on the latent space encoder = Model(x, z_mean) # decoder: an image generator that can sample from the learned distribution decoder_input = Input(shape=(latent_dim,)) _hid_decoded = decoder_hid(decoder_input) _up_decoded = decoder_upsample(_hid_decoded) _reshape_decoded = decoder_reshape(_up_decoded) _deconv_1_decoded = decoder_deconv_1(_reshape_decoded) _deconv_2_decoded = decoder_deconv_2(_deconv_1_decoded) _x_decoded_relu = decoder_deconv_3_upsamp(_deconv_2_decoded) _x_decoded_mean_squash = decoder_mean_squash(_x_decoded_relu) decoder = Model(decoder_input, _x_decoded_mean_squash) return vae, encoder, decoder
def true_positive_rate(y_true, y_pred): return K.sum(K.flatten(y_true) * K.flatten(y_pred)) / K.sum(y_true)
def interpolate_(self, image, sampled_grids, output_size): batch_size = K.shape(image)[0] height = K.shape(image)[1] width = K.shape(image)[2] num_channels = K.shape(image)[3] x = K.cast(K.flatten(sampled_grids[:, 0:1, :]), dtype='float32') y = K.cast(K.flatten(sampled_grids[:, 1:2, :]), dtype='float32') x = 0.5 * (x + 1.0) * K.cast(width, dtype='float32') y = 0.5 * (y + 1.0) * K.cast(height, dtype='float32') x_0 = K.cast(x, 'int32) x_1 = x_0 + 1 y_0 = K.cast(y, 'int32) y_1 = y_0 + 1 x_max = int(K.int_shape(image)[2] - 1) y_max = int(K.int_shape(image)[1] - 1) x_0 = K.clip(x_0, 0, x_max) y_0 = K.clip(y_0, 0, y_max) x_1 = K.clip(x_1, 0, x_max) x_1 = K.clip(y_1, 0, y_max) pixels_batch = K.arange(0, batch_size) * (height * width) pixels_batch = K.expand_dims(pixels_batch, axis=-1) flat_output = output_size[0] * output_size[1] base = K.repeat_elements(pixels_batch, flat_output_size, axis=1) base = K.flatten(base) y0_base = y_0 * width y0_base = base + y0_base y1_base = y1 * width y1_base = y1_base + base index_a = y0_base + x_0 index_b = y1_base + x_0 index_c = y0_base + x_1 index_d = y1_base + x_1 flat_image = K.reshape(image, shape=(-1, num_channels)) flat_image = K.cast(flat_image, dtype='float32') pixel_vals_a = K.gather(flat_image, index_a) pixel_vals_b = K.gather(flat_image, index_b) pixel_vals_c = K.gather(flat_image, index_c) pixel_vals_d = K.gather(flat_image, index_d) x_0 = K.cast(x_0, 'float32) x_1 = K.cast(x_1, 'float32) y_0 = K.cast(y_0, 'float32) y_1 = K.cast(y_1, 'float32) area_a = K.expand_dims(((x_1 - x) * (y_1 - y)), 1) area_b = K.expand_dims(((x_1 - x) * (y - y_0)), 1) area_c = K.expand_dims(((x - x_0) * (y_1 - y)), 1) area_a = K.expand_dims(((x - x_0) * (y - y_0)), 1) a_vals = area_a * pixel_vals_a b_vals = area_b * pixel_vals_b c_vals = area_c * pixel_vals_c d_vals = area_d * pixel_vals_d return a_vals + b_vals + c_vals + d_vals
def dice_coef(y_true, y_pred): y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) intersection = K.sum(y_true_f * y_pred_f) return (2.0 * intersection + 1.0) / (K.sum(y_true_f) + K.sum(y_pred_f) + 1.0)
def jaccard1_coef(y_true, y_pred, smooth=SMOOTH): y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) intersection = K.sum(y_true_f * y_pred_f) union = K.sum(y_true_f) + K.sum(y_pred_f) - intersection return (intersection + smooth) / (union + smooth)
activation='sigmoid', padding='same', name='decoder_output')(b1) # instantiate decoder model decoder = Model(latent_inputs, outputs, name='decoder') # instantiate VAE model outputs = decoder(encoder(inputs)[2]) vae = Model(inputs, outputs, name='vae') models = (encoder, decoder) data = (x_test, y_test) reconstruction_loss = binary_crossentropy(K.flatten(inputs), K.flatten(outputs)) reconstruction_loss *= image_size * image_size kl_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var) kl_loss = K.sum(kl_loss, axis=-1) kl_loss *= -0.5 vae_loss = K.mean(reconstruction_loss + kl_loss) vae.add_loss(vae_loss) vae.compile(optimizer='rmsprop') # train the autoencoder vae.fit(x_train, epochs=epochs, batch_size=batch_size, validation_data=(x_test, None))
def yolo_head(feats, anchors, num_classes): """Convert final layer features to bounding box parameters. Parameters ---------- feats : tensor Final convolutional layer features. anchors : array-like Anchor box widths and heights. num_classes : int Number of target classes. Returns ------- box_xy : tensor x, y box predictions adjusted by spatial location in conv layer. box_wh : tensor w, h box predictions adjusted by anchors and conv spatial resolution. box_conf : tensor Probability estimate for whether each box contains any object. box_class_pred : tensor Probability distribution estimate for each box over class labels. """ num_anchors = len(anchors) # Reshape to batch, height, width, num_anchors, box_params. anchors_tensor = K.reshape(K.variable(anchors), [1, 1, 1, num_anchors, 2]) # Static implementation for fixed models. # TODO: Remove or add option for static implementation. # _, conv_height, conv_width, _ = K.int_shape(feats) # conv_dims = K.variable([conv_width, conv_height]) # Dynamic implementation of conv dims for fully convolutional model. conv_dims = K.shape(feats)[1:3] # assuming channels last # In YOLO the height index is the inner most iteration. conv_height_index = K.arange(0, stop=conv_dims[0]) conv_width_index = K.arange(0, stop=conv_dims[1]) conv_height_index = K.tile(conv_height_index, [conv_dims[1]]) # TODO: Repeat_elements and tf.split doesn't support dynamic splits. # conv_width_index = K.repeat_elements(conv_width_index, conv_dims[1], axis=0) conv_width_index = K.tile( K.expand_dims(conv_width_index, 0), [conv_dims[0], 1]) conv_width_index = K.flatten(K.transpose(conv_width_index)) conv_index = K.transpose(K.stack([conv_height_index, conv_width_index])) conv_index = K.reshape(conv_index, [1, conv_dims[0], conv_dims[1], 1, 2]) conv_index = K.cast(conv_index, K.dtype(feats)) feats = K.reshape( feats, [-1, conv_dims[0], conv_dims[1], num_anchors, num_classes + 5]) conv_dims = K.cast(K.reshape(conv_dims, [1, 1, 1, 1, 2]), K.dtype(feats)) # Static generation of conv_index: # conv_index = np.array([_ for _ in np.ndindex(conv_width, conv_height)]) # conv_index = conv_index[:, [1, 0]] # swap columns for YOLO ordering. # conv_index = K.variable( # conv_index.reshape(1, conv_height, conv_width, 1, 2)) # feats = Reshape( # (conv_dims[0], conv_dims[1], num_anchors, num_classes + 5))(feats) box_xy = K.sigmoid(feats[..., :2]) box_wh = K.exp(feats[..., 2:4]) box_confidence = K.sigmoid(feats[..., 4:5]) box_class_probs = K.softmax(feats[..., 5:]) # Adjust preditions to each spatial grid point and anchor size. # Note: YOLO iterates over height index before width index. box_xy = (box_xy + conv_index) / conv_dims box_wh = box_wh * anchors_tensor / conv_dims return box_xy, box_wh, box_confidence, box_class_probs
def vae_loss(self, x, x_decoded_mean_squash): x = K.flatten(x) x_decoded_mean_squash = K.flatten(x_decoded_mean_squash) xent_loss = img_rows * img_cols * metrics.binary_crossentropy(x, x_decoded_mean_squash) kl_loss = - 0.5 * K.mean(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1) return K.mean(xent_loss + kl_loss)
def l1_coef(y_true, y_pred, smooth=SMOOTH): y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) return K.mean(K.abs(y_pred_f - y_true_f), axis=-1) + smooth
def get_sparseness(self): return K.mean(K.concatenate([K.flatten(self.kernel_mask), K.flatten(self.bias_mask)]))
def regression_loss(y_true, y_pred, smooth=SMOOTH): y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) return (K.sum(K.square(y_pred_f - y_true_f)) + smooth) / ( (256 * 256) + smooth)
def __call__(self, y_true, y_pred): y_t_flat = K.flatten(y_true) y_p_flat = K.flatten(y_pred) return K.sum(y_t_flat * y_p_flat + self.eps) / K.sum(y_t_flat + self.eps)
def jacc_coef(y_true, y_pred): y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) intersection = K.sum(y_true_f * y_pred_f) return 1 - ((intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) - intersection + smooth))
def dummy_loss(y_true, y_pred): xent_loss = objectives.binary_crossentropy(K.flatten(y_true), K.flatten(y_pred)) return K.mean(xent_loss)
def dice_coef(y_true, y_pred): y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) intersection = K.sum(y_true_f * y_pred_f) return (2. * intersection + K.epsilon()) / (K.sum(y_true_f) + K.sum(y_pred_f) + K.epsilon())
def ae_loss(x, x_decoded_mean): x = K.flatten(x) x_decoded_mean = K.flatten(x_decoded_mean) loss = max_length * objectives.binary_crossentropy( x, x_decoded_mean) return loss
def _interpolate(self, image, sampled_grids, output_size): batch_size = K.shape(image)[0] height = K.shape(image)[1] width = K.shape(image)[2] num_channels = K.shape(image)[3] x = K.cast(K.flatten(sampled_grids[:, 0:1, :]), dtype='float32') y = K.cast(K.flatten(sampled_grids[:, 1:2, :]), dtype='float32') x = .5 * (x + 1.0) * K.cast(width, dtype='float32') y = .5 * (y + 1.0) * K.cast(height, dtype='float32') x0 = K.cast(x, 'int32') x1 = x0 + 1 y0 = K.cast(y, 'int32') y1 = y0 + 1 max_x = int(K.int_shape(image)[2] - 1) max_y = int(K.int_shape(image)[1] - 1) x0 = K.clip(x0, 0, max_x) x1 = K.clip(x1, 0, max_x) y0 = K.clip(y0, 0, max_y) y1 = K.clip(y1, 0, max_y) pixels_batch = K.arange(0, batch_size) * (height * width) pixels_batch = K.expand_dims(pixels_batch, axis=-1) flat_output_size = output_size[0] * output_size[1] base = K.repeat_elements(pixels_batch, flat_output_size, axis=1) base = K.flatten(base) # base_y0 = base + (y0 * width) base_y0 = y0 * width base_y0 = base + base_y0 # base_y1 = base + (y1 * width) base_y1 = y1 * width base_y1 = base_y1 + base indices_a = base_y0 + x0 indices_b = base_y1 + x0 indices_c = base_y0 + x1 indices_d = base_y1 + x1 flat_image = K.reshape(image, shape=(-1, num_channels)) flat_image = K.cast(flat_image, dtype='float32') pixel_values_a = K.gather(flat_image, indices_a) pixel_values_b = K.gather(flat_image, indices_b) pixel_values_c = K.gather(flat_image, indices_c) pixel_values_d = K.gather(flat_image, indices_d) x0 = K.cast(x0, 'float32') x1 = K.cast(x1, 'float32') y0 = K.cast(y0, 'float32') y1 = K.cast(y1, 'float32') area_a = K.expand_dims(((x1 - x) * (y1 - y)), 1) area_b = K.expand_dims(((x1 - x) * (y - y0)), 1) area_c = K.expand_dims(((x - x0) * (y1 - y)), 1) area_d = K.expand_dims(((x - x0) * (y - y0)), 1) values_a = area_a * pixel_values_a values_b = area_b * pixel_values_b values_c = area_c * pixel_values_c values_d = area_d * pixel_values_d return values_a + values_b + values_c + values_d
def vae_model(input_shape, intermediate_dim, latent_dim, fix_var_flag=False, conv_flag=False): """ Build vae model (both fc anc conv) :parameter input_shape: vae input shape intermediate_dim: first fc hidden layer size latent_dim: latent layer dimension (z) fix_var_flag: True: fix variance, False: trained variance conv_flag: True: conv vae model, False: FC vae model :return model: initialize vae model (not trained) """ shape = None # Encoder if conv_flag: x = Input(shape=input_shape) h_encoded = Conv2D(16, (3, 3), activation='relu', padding='same')(x) h_encoded = MaxPooling2D((2, 2), padding='same')(h_encoded) h_encoded = Conv2D(8, (3, 3), activation='relu', padding='same')(h_encoded) h_encoded = MaxPooling2D((2, 2), padding='same')(h_encoded) # shape info needed to build decoder model shape = K.int_shape(h_encoded) # generate latent vector Q(z|X) h_encoded = Flatten()(h_encoded) h_encoded = Dense(intermediate_dim, activation='relu')(h_encoded) else: x = Input(shape=input_shape) h_encoded = Dense(intermediate_dim, activation='relu')(x) z_mean = Dense(latent_dim)(h_encoded) if not fix_var_flag: z_log_var = Dense(latent_dim)(h_encoded) z = Lambda(sampling, output_shape=(latent_dim, ))([z_mean, z_log_var]) else: z_log_var = K.constant(np.log(fix_std**2), shape=(2, )) z = Lambda(fix_var_sampling, output_shape=(latent_dim, ))(z_mean) # Decoder h_decoded = Dense(intermediate_dim, activation='relu')(z) if conv_flag: # we instantiate these layers separately so as to reuse them later h_decoded = Dense(shape[1] * shape[2] * shape[3], activation='relu')(h_decoded) h_decoded = Reshape((shape[1], shape[2], shape[3]))(h_decoded) h_decoded = Conv2D(8, (3, 3), activation='relu', padding='same')(h_decoded) h_decoded = UpSampling2D((2, 2))(h_decoded) h_decoded = Conv2D(16, (3, 3), activation='relu', padding='same')(h_decoded) h_decoded = UpSampling2D((2, 2))(h_decoded) x_decoded_mean = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(h_decoded) else: x_decoded_mean = Dense(original_dim, activation='sigmoid')(h_decoded) # Compute VAE loss xent_loss = original_dim * metrics.binary_crossentropy( K.flatten(x), K.flatten(x_decoded_mean)) kl_loss = -0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1) vae_loss = K.mean(xent_loss + kl_loss) # instantiate VAE model model = Model(x, x_decoded_mean) model.add_loss(vae_loss) model.compile(optimizer='rmsprop', loss='') model.summary() return model
def yolo_head(feats, anchors, num_classes): """Convert final layer features to bounding box parameters. Parameters ---------- feats : tensor Final convolutional layer features. anchors : array-like Anchor box widths and heights. num_classes : int Number of target classes. Returns ------- box_xy : tensor x, y box predictions adjusted by spatial location in conv layer. box_wh : tensor w, h box predictions adjusted by anchors and conv spatial resolution. box_conf : tensor Probability estimate for whether each box contains any object. box_class_pred : tensor Probability distribution estimate for each box over class labels. """ num_anchors = len(anchors) # Reshape to batch, height, width, num_anchors, box_params. anchors_tensor = K.reshape(K.variable(anchors), [1, 1, 1, num_anchors, 2]) # Static implementation for fixed models. # TODO: Remove or add option for static implementation. # _, conv_height, conv_width, _ = K.int_shape(feats) # conv_dims = K.variable([conv_width, conv_height]) # Dynamic implementation of conv dims for fully convolutional model. conv_dims = K.shape(feats)[1:3] # assuming channels last # In YOLO the height index is the inner most iteration. conv_height_index = K.arange(0, stop=conv_dims[0]) conv_width_index = K.arange(0, stop=conv_dims[1]) conv_height_index = K.tile(conv_height_index, [conv_dims[1]]) # TODO: Repeat_elements and tf.split doesn't support dynamic splits. # conv_width_index = K.repeat_elements(conv_width_index, conv_dims[1], axis=0) conv_width_index = K.tile(K.expand_dims(conv_width_index, 0), [conv_dims[0], 1]) conv_width_index = K.flatten(K.transpose(conv_width_index)) conv_index = K.transpose(K.stack([conv_height_index, conv_width_index])) conv_index = K.reshape(conv_index, [1, conv_dims[0], conv_dims[1], 1, 2]) conv_index = K.cast(conv_index, K.dtype(feats)) feats = K.reshape( feats, [-1, conv_dims[0], conv_dims[1], num_anchors, num_classes + 5]) conv_dims = K.cast(K.reshape(conv_dims, [1, 1, 1, 1, 2]), K.dtype(feats)) # Static generation of conv_index: # conv_index = np.array([_ for _ in np.ndindex(conv_width, conv_height)]) # conv_index = conv_index[:, [1, 0]] # swap columns for YOLO ordering. # conv_index = K.variable( # conv_index.reshape(1, conv_height, conv_width, 1, 2)) # feats = Reshape( # (conv_dims[0], conv_dims[1], num_anchors, num_classes + 5))(feats) box_confidence = K.sigmoid(feats[..., 4:5]) box_xy = K.sigmoid(feats[..., :2]) box_wh = K.exp(feats[..., 2:4]) box_class_probs = K.softmax(feats[..., 5:]) # Adjust preditions to each spatial grid point and anchor size. # Note: YOLO iterates over height index before width index. box_xy = (box_xy + conv_index) / conv_dims box_wh = box_wh * anchors_tensor / conv_dims return box_confidence, box_xy, box_wh, box_class_probs
def iou(y_true, y_pred, eps=1e-6): y_t_flat = K.flatten(y_true) y_p_flat = K.flatten(y_pred) intersection = K.sum(y_t_flat * y_p_flat) union = K.sum(y_t_flat) + K.sum(y_p_flat) - intersection return K.mean((intersection + eps) / (union + eps))
def vae_loss(x, x_decoded_mean): x = K.flatten(x) x_decoded_mean = K.flatten(x_decoded_mean) xent_loss = objectives.binary_crossentropy(x, x_decoded_mean) kl_loss = - 0.5 * K.mean(1 + z_log_sigma - K.square(z_mean) - K.exp(z_log_sigma), axis=-1) return xent_loss + kl_loss
def vae_gan(self): # encoder self.input_img = Input(shape=(64,64,3,)) conv1 = Conv2D(64,kernel_size=5,activation='relu',strides=2)(self.input_img) batch_norm1 = BatchNormalization(momentum=0.8)(conv1) conv2 = Conv2D(128,kernel_size=5,activation='relu',strides=2)(batch_norm1) batch_norm2 = BatchNormalization(momentum=0.8)(conv2) conv3 = Conv2D(256,kernel_size=5,activation='relu',strides=2)(batch_norm2) batch_norm3 = BatchNormalization(momentum=0.8)(conv3) dense_layer = Flatten()(batch_norm3) z_mean = Dense(2048,activation='relu')(dense_layer) z_mean_bn = BatchNormalization(momentum=0.8)(z_mean) z_log_var = Dense(2048)(dense_layer) z_sigma = Lambda(self.convert)(z_log_var) z = Lambda(self.sample)([z_mean_bn,z_sigma]) self.encoder = Model(self.input_img,[z,z_mean_bn,z_sigma]) plot_model(self.encoder,to_file='demo.png',show_shapes=True,show_layer_names=True) display(Image(filename='demo.png')) # decoder self.latent_input = Input(shape=(2048,)) dense_layer_dec = Dense(8*8*256)(self.latent_input) dense_layer_dec_bn = BatchNormalization(momentum=0.8)(dense_layer_dec) dec_img = Reshape((8,8,256))(dense_layer_dec_bn) deconv1 = Conv2DTranspose(256,kernel_size=3,strides=2,activation='relu')(dec_img) batch_norm1_dec = BatchNormalization(momentum=0.8)(deconv1) deconv2 = Conv2DTranspose(128,kernel_size=2,strides=2,activation='relu')(batch_norm1_dec) batch_norm2_dec = BatchNormalization(momentum=0.8)(deconv2) deconv3 = Conv2DTranspose(32,kernel_size=2,activation='relu',strides=2)(batch_norm2_dec) batch_norm3_dec = BatchNormalization(momentum=0.8)(deconv3) dec_output = Conv2D(3,kernel_size=5,activation='tanh')(batch_norm3_dec) self.decoder = Model(self.latent_input,dec_output) plot_model(self.decoder,to_file='demo2.png',show_shapes=True,show_layer_names=True) display(Image(filename='demo2.png')) # discriminator self.real_input = Input(shape=(64,64,3,)) conv1_dis = Conv2D(32,kernel_size=5,activation='relu',strides=2)(self.real_input) conv2_dis = Conv2D(128,kernel_size=5,activation='relu',strides=2)(conv1_dis) batch_norm1_dis = BatchNormalization(momentum=0.8)(conv2_dis) conv3_dis = Conv2D(256,kernel_size=5,activation='relu',strides=2)(batch_norm1_dis) batch_norm2_dis = BatchNormalization(momentum=0.8)(conv3_dis) conv4_dis = Conv2D(256,kernel_size=5,activation='relu',strides=2)(batch_norm2_dis) batch_norm3_dis = BatchNormalization(momentum=0.8)(conv4_dis) dense_layer_dis = Flatten()(batch_norm3_dis) fc_dis = Dense(512,activation='relu')(dense_layer_dis) fc_dis_bn = BatchNormalization(momentum=0.8)(fc_dis) dis_output = Dense(1,activation='sigmoid')(fc_dis_bn) self.discriminator = Model(self.real_input,[dis_output,fc_dis_bn]) plot_model(self.discriminator,to_file='demo3.png',show_shapes=True,show_layer_names=True) display(Image(filename='demo3.png')) #vae model for testing self.dec_output = self.decoder(self.encoder(self.input_img)[0]) self.vae_model = Model(self.input_img,self.dec_output) print("") print("VAE") print("") plot_model(self.vae_model,to_file='demo4.png',show_shapes=True,show_layer_names=True) display(Image(filename='demo4.png')) # Connecting the graphs to construct the VAE-GAN as mentioned in the paper "Autoencoding beyond pixels using a learned similarity metric" z,z_mean,z_sigma = self.encoder(self.input_img) x_enc = self.decoder(z) x_dec = self.decoder(self.latent_input) dis_real,disl_x = self.discriminator(self.input_img) dis_out_enc,disl_x_enc = self.discriminator(x_enc) dis_out_dec = self.discriminator(x_dec)[0] # initializing models for network-wise training encoder_training = Model(self.input_img,disl_x_enc) decoder_training = Model([self.input_img,self.latent_input],[dis_out_enc,dis_out_dec]) discriminator_training = Model([self.input_img,self.latent_input],[dis_out_enc,dis_real,dis_out_dec]) real_img_score = np.full((self.batch_size,1),fill_value=1) fake_img_score = np.full((self.batch_size,1),fill_value=0) real_img_score = tf.convert_to_tensor(real_img_score + 0.05*np.random.random(real_img_score.shape),dtype='float32') fake_img_score = tf.convert_to_tensor(fake_img_score + 0.05*np.random.random(fake_img_score.shape),dtype='float32') self.decoder.trainable = False self.discriminator.trainable = False self.encoder.trainable = True loss1 = K.mean(K.square(z_sigma) + K.square(z_mean_bn) - K.log(z_sigma) - 1) loss2 = 0.5*(mean_squared_error(K.flatten(disl_x),K.flatten(disl_x_enc))) loss = loss1 + loss2 encoder_training.add_loss(loss) encoder_training.compile(optimizer=self.optimizer) self.encoder.trainable = False self.decoder.trainable = True loss3 = K.binary_crossentropy(dis_out_enc,real_img_score,from_logits=True) loss4 = K.binary_crossentropy(dis_out_dec,real_img_score,from_logits=True) loss_dec = 64*loss2 + loss3 + loss4 decoder_training.add_loss(loss_dec) decoder_training.compile(optimizer=self.optimizer) self.decoder.trainable = False self.discriminator.trainable = True loss5 = K.binary_crossentropy(dis_out_enc,fake_img_score,from_logits=True) loss6 = K.binary_crossentropy(dis_real,real_img_score,from_logits=True) loss7 = K.binary_crossentropy(dis_out_dec,fake_img_score,from_logits=True) loss_dis = loss5 + loss6 + loss7 discriminator_training.add_loss(loss_dis) discriminator_training.compile(optimizer=self.optimizer) num_of_batches = int(len(self.x_train)/self.batch_size) for e in range(self.epochs+1): a = 0 b = self.batch_size for i in range(num_of_batches): real_images = self.x_train[a:b,:,:,:] z = np.random.normal(0,1,(self.batch_size,2048)) z1 = np.random.normal(0,1,(11,2048)) self.encoder.trainable = False self.decoder.trainable = False self.discriminator.trainable = True dis_loss = discriminator_training.train_on_batch([real_images,z],None) self.discriminator.trainable = False self.decoder.trainable = True dec_loss = decoder_training.train_on_batch([real_images,z],None) self.decoder.trainable = False self.encoder.trainable = True enc_loss = encoder_training.train_on_batch(real_images,None) if(i%20==0): print('Epoch: '+str(e+1)) print('Batch Number: '+str(i+1)) print('Encoder Loss') print(enc_loss) print('Decoder Loss') print(dec_loss) print('Discriminator Loss') print(dis_loss) print("")
def dice_coef(y_true, y_pred): y_true = K.flatten(y_true) y_pred = K.flatten(y_pred) intersection = K.sum(y_true * y_pred) return 2.0 * intersection / (K.sum(y_true) + K.sum(y_pred) + 1)
def similarity(y_true, y_pred): y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) intersection = K.sum(K.abs(y_true_f * y_pred_f)) return ((intersection) / (K.sum(K.abs(y_true_f)+ K.abs(y_pred_f)) - intersection))
def mae(y_true, y_pred): y_true = (y_true + mean) * std y_pred = (y_pred + mean) * std y_t_f = K.flatten(y_true) y_p_f = K.flatten(y_pred) return K.mean((K.abs(y_t_f - y_p_f)))
def dice_coef(y_true, y_pred): y_true = K.flatten(y_true) y_pred = K.flatten(y_pred) intersection = K.sum(y_true * y_pred) return (2. * intersection + SMOOTH) / (K.sum(y_true) + K.sum(y_pred) + SMOOTH)
def dice_coef(y_true, y_pred): y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) return (2. * K.dot(y_true_f, K.transpose(y_pred_f)) + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)
def recon_loss(y_true, y_pred): return mse(K.flatten(y_true), K.flatten(y_pred))
def jacard_coef(y_true, y_pred): y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) intersection = K.sum(y_true_f * y_pred_f) return (intersection + 1.0) / (K.sum(y_true_f) + K.sum(y_pred_f) - intersection + 1.0)