def clipped_relu(value): """Returns clipped relu, clip value set to 20.""" return K.relu(value, max_value=20)
def relu6(x): return K.relu(x,max_value=6)
def train_step(self, images, style, noise, perform_gp=True, perform_pl=False): with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape: #Get style information w_space = [] pl_lengths = self.pl_mean for i in range(len(style)): w_space.append(self.GAN.S(style[i])) #Generate images generated_images = self.GAN.G(w_space + [noise]) #Discriminate real_output = self.GAN.D(images, training=True) fake_output = self.GAN.D(generated_images, training=True) #Hinge loss function gen_loss = K.mean(fake_output) divergence = K.mean( K.relu(1 + real_output) + K.relu(1 - fake_output)) disc_loss = divergence if perform_gp: #R1 gradient penalty disc_loss += gradient_penalty(images, real_output, 10) if perform_pl: #Slightly adjust W space w_space_2 = [] for i in range(len(style)): std = 0.1 / (K.std(w_space[i], axis=0, keepdims=True) + 1e-8) w_space_2.append(w_space[i] + K.random_normal(tf.shape(w_space[i])) / (std + 1e-8)) #Generate from slightly adjusted W space pl_images = self.GAN.G(w_space_2 + [noise]) #Get distance after adjustment (path length) delta_g = K.mean(K.square(pl_images - generated_images), axis=[1, 2, 3]) pl_lengths = delta_g if self.pl_mean > 0: gen_loss += K.mean(K.square(pl_lengths - self.pl_mean)) #Get gradients for respective areas gradients_of_generator = gen_tape.gradient( gen_loss, self.GAN.GM.trainable_variables) gradients_of_discriminator = disc_tape.gradient( disc_loss, self.GAN.D.trainable_variables) #Apply gradients self.GAN.GMO.apply_gradients( zip(gradients_of_generator, self.GAN.GM.trainable_variables)) self.GAN.DMO.apply_gradients( zip(gradients_of_discriminator, self.GAN.D.trainable_variables)) return disc_loss, gen_loss, divergence, pl_lengths
Y_test.sort(axis=1) #排一下序,因为只比较集合,不比较顺序 #搭建普通CNN分类模型 input_image = Input(shape=(None, None, 1)) cnn = Conv2D(64, (3, 3), activation='relu')(input_image) cnn = Conv2D(64, (3, 3), activation='relu')(cnn) cnn = AveragePooling2D((2, 2))(cnn) cnn = Conv2D(128, (3, 3), activation='relu')(cnn) cnn = Conv2D(128, (3, 3), activation='relu')(cnn) cnn = GlobalAveragePooling2D()(cnn) dense = Dense(128, activation='relu')(cnn) output = Dense(10, activation='sigmoid')(dense) model = Model(inputs=input_image, outputs=output) model.compile( loss=lambda y_true, y_pred: y_true * K.relu(0.9 - y_pred)**2 + 0.25 * (1 - y_true) * K.relu(y_pred - 0.1)**2, optimizer='adam', metrics=['accuracy']) model.summary() model.fit(x_train, y_train, batch_size=batch_size, epochs=20, verbose=1, validation_data=(x_test, y_test)) Y_pred = model.predict(X_test) #用模型进行预测 greater = np.sort(Y_pred, axis=1)[:, -2] > 0.5 #判断预测结果是否大于0.5
def call(self, x, mask=None): x_lo = K.relu(self.lo - x) x_hi = K.relu(x - self.hi) return x + self.gamma * x_lo - self.gamma * x_hi
def e_insensitive_loss(y_true, y_pred): return K.relu(K.abs(y_true - y_pred) - .1)
def hard_swish(inputs): return inputs * K.relu(inputs + 3.0, max_value=6.0) / 6.0
def call(self, x): # print("!",x.shape) E = K.reshape(x[:, :, 0], (-1, self.gs, 1)) p1 = K.reshape(x[:, :, 1], (-1, self.gs, 1)) p2 = K.reshape(x[:, :, 2], (-1, self.gs, 1)) p3 = K.reshape(x[:, :, 3], (-1, self.gs, 1)) pt = K.sqrt(p1**2 + p2**2) p = K.sqrt(pt**2 + p3**2) iszero = p**2 + E**2 iszero = 1 - K.relu(1 - self.numericC * iszero) + K.relu( -self.numericC * iszero) #return iszero eta = iszero * 0.5 * K.log(0.0000000001 + (p + p3) / (p - p3 + 0.0000000001)) #phi=iszero*t.math.acos(p3/(p+0.0000001)) phi = iszero * t.math.atan2(p2, p1) #print("eta",eta.shape,"phi",phi.shape) eta = K.reshape(eta, (-1, self.gs)) phi = K.reshape(phi, (-1, self.gs)) meta = K.mean(eta, axis=-1) mp1 = K.mean(p1, axis=-2) mp2 = K.mean(p2, axis=-2) #print(p2.shape,p1.shape) #print(mp1.shape,mp2.shape) #exit() mphi = t.math.atan2(mp2, mp1) #print("meta",meta.shape,"mphi",mphi.shape) #mphi=K.mean(phi,axis=-1) #exit() meta = K.reshape(K.repeat_elements(meta, self.gs, 0), (-1, self.gs)) mphi = K.repeat_elements(mphi, self.gs, 1) #print("meta",meta.shape,"mphi",mphi.shape) #deta=eta#-meta##not sure if abs here #dphi=phi#-mphi##not sure here either siszero = K.reshape(iszero, (-1, self.gs)) deta = K.reshape(siszero * (eta - meta), (-1, self.gs, 1)) dphi = K.reshape(siszero * (phi - mphi), (-1, self.gs, 1)) pi = t.constant(math.pi) #dphi=K.min([t.math.floormod(dphi,2*pi),t.math.floormod(-dphi,2*pi)],axis=0) opta = t.math.floormod(dphi, 2 * pi) optb = t.math.floormod(-dphi, 2 * pi) dphi = t.where(t.greater(opta, optb), optb, -opta) #dphi=K.reshape(dphi,(-1,self.gs,1))#should actually be useless? #dphi=K.min(K.concatenate((t.math.floormod(dphi,2*pi),t.math.floormod(-dphi,2*pi)),axis=-1),axis=-1) #dphi=K.reshape(dphi,(-1,self.gs,1)) #deta=iszero*K.permute_dimensions(K.permute_dimensions(eta,(1,0,2))-meta,(1,0,2))#not sure if abs here required #dphi=iszero*K.permute_dimensions(K.permute_dimensions(phi,(1,0,2))-mphi,(1,0,2))#also not sure here either spt = K.sum(pt, axis=-2) ppt = -iszero * K.permute_dimensions( K.log(0.000000001 + K.permute_dimensions(pt, (1, 0, 2)) / (spt + 0.0000001)), (1, 0, 2) ) #please note the added sign in comparison to the original paper #print(iszero.shape,deta.shape,dphi.shape,ppt.shape) #print(eta.shape,meta.shape) #print(phi.shape,mphi.shape) #exit() #print(iszero.shape,deta.shape,dphi.shape,pt.shape) #exit() #meta=K.reshape(meta,(-1,self.gs,1)) #mphi=K.reshape(mphi,(-1,self.gs,1)) #phi=K.reshape(phi,(-1,self.gs,1)) ret = K.concatenate((iszero, deta, dphi, ppt), axis=-1) #adding iszero for numerical reasons #print(ret.shape,x.shape) #exit() return ret
def call(self, x): # print("!",x.shape) E = K.reshape(x[:, :, 0], (-1, self.gs, 1)) p1 = K.reshape(x[:, :, 1], (-1, self.gs, 1)) p2 = K.reshape(x[:, :, 2], (-1, self.gs, 1)) p3 = K.reshape(x[:, :, 3], (-1, self.gs, 1)) pt = K.sqrt(p1**2 + p2**2) p = K.sqrt(pt**2 + p3**2) iszero = p**2 + E**2 iszero = 1 - K.relu(1 - self.numericC * iszero) + K.relu( -self.numericC * iszero) #return iszero eta = iszero * 0.5 * K.log(0.0000000001 + (p + p3) / (p - p3 + 0.0000000001)) #phi=iszero*t.math.acos(p3/(p+0.0000001)) phi = iszero * t.math.atan2(p2, p1) #print("eta",eta.shape,"phi",phi.shape) meta = K.mean(eta, axis=-2) mphi = K.mean(phi, axis=-2) #print("meta",meta.shape,"mphi",mphi.shape) #deta=eta#-meta##not sure if abs here #dphi=phi#-mphi##not sure here either deta = iszero * K.permute_dimensions( K.permute_dimensions(eta, (1, 0, 2)) - meta, (1, 0, 2)) #not sure if abs here required dphi = iszero * K.permute_dimensions( K.permute_dimensions(phi, (1, 0, 2)) - mphi, (1, 0, 2)) #also not sure here either #print("deta",deta.shape,"dphi",dphi.shape) lpt = iszero * K.log(pt + 0.0000001) ## lE = iszero * K.log(E + 0.0000001) ## #print("lpt",lpt.shape,"lE",lE.shape) #rpt=K.reshape(pt,(-1,self.gs)) spt = K.sum(pt, axis=-2) #print("spt",spt.shape) #return K.permute_dimensions(K.log(1.0+K.abs(K.permute_dimensions(pt,(1,0,2))/(K.abs(spt)+1.0))),(1,0,2)) #ispt=1/(spt+0.000000001) #print("ispt",ispt.shape) ppt = -iszero * K.permute_dimensions( K.log(0.000000001 + K.permute_dimensions(pt, (1, 0, 2)) / (spt + 0.0000001)), (1, 0, 2) ) #please note the added sign in comparison to the original paper #ppt=K.reshape(K.permute_dimensions(K.log(0.000000001+K.permute_dimensions(rpt,(1,0,2))/(spt+0.0000001)),(1,0)),(-1,self.gs,1))## #ppt=K.reshape(K.permute_dimensions(K.log(0.000000001+K.permute_dimensions(rpt,(1,0))/(spt+0.0000001)),(1,0)),(-1,self.gs,1))## #ppt=K.reshape(K.log(0.000000001+K.permute_dimensions(rpt,(1,0))/(spt+0.0000001)),(-1,self.gs,1))## #ppt=iszero*K.log(pt/(spt+0.00000001))## #print("ppt",ppt.shape) sE = K.sum(E, axis=-2) #print("sE",sE.shape) pE = -iszero * K.permute_dimensions( K.log(0.000000001 + K.permute_dimensions(E, (1, 0, 2)) / (sE + 0.0000001)), (1, 0, 2)) #here was also a sign added #pE=-iszero*K.log(sE/(E+0.00000001))## #pE=-iszero #print("pE",pE.shape) dR = K.sqrt(deta**2 + dphi**2) ## #print("dR",dR.shape) ret = K.concatenate((iszero, deta, dphi, lpt, lE, ppt, pE, dR), axis=-1) #adding iszero for numerical reasons #print(ret.shape,x.shape) #exit() return ret
def call(self, x): return self.w_2(self.dropout(K.relu(self.w_1(x))))
def _build_model(self, graph: Graph): """Return Siamese model.""" # Creating the inputs layers inputs = [Input((1, ), dtype=tf.int32) for _ in range(4)] edge_types = Input((1, ), dtype=tf.int32) # Creating the embedding layer for the contexts node_embedding_layer = Embedding(input_dim=graph.get_number_of_nodes(), output_dim=self._embedding_size, input_length=1, name="node_embeddings") # Get the node embedding node_embeddings = [ UnitNorm(axis=-1)(node_embedding_layer(node_input)) for node_input in inputs ] if self.requires_node_types(): max_node_types = graph.get_maximum_multilabel_count() multilabel = graph.has_multilabel_node_types() unknown_node_types = graph.has_unknown_node_types() node_types_offset = int(multilabel or unknown_node_types) node_type_inputs = [ Input((max_node_types, ), dtype=tf.int32) for _ in range(4) ] node_type_embedding_layer = Embedding( input_dim=graph.get_number_of_node_types() + node_types_offset, output_dim=self._embedding_size, input_length=max_node_types, name="node_type_embeddings", mask_zero=multilabel or unknown_node_types) node_embeddings = [ Add()([ GlobalAveragePooling1D()( node_type_embedding_layer(node_type_input)), node_embedding ]) for node_type_input, node_embedding in zip( node_type_inputs, node_embeddings) ] else: node_type_inputs = [] inputs.extend(node_type_inputs) inputs.append(edge_types) edge_types_number = graph.get_number_of_edge_types() unknown_edge_types = graph.has_unknown_edge_types() edge_types_offset = int(unknown_edge_types) edge_type_embedding = GlobalAveragePooling1D()(Embedding( input_dim=edge_types_number, output_dim=self._embedding_size, input_length=1 + edge_types_offset, mask_zero=unknown_edge_types, name="edge_type_embeddings", )(edge_types)) (srcs_embedding, dsts_embedding, not_srcs_embedding, not_dsts_embedding, edge_type_embedding) = self._build_output(*node_embeddings, edge_type_embedding, edge_types) loss = K.relu(self._relu_bias + tf.norm( srcs_embedding + edge_type_embedding - dsts_embedding, axis=-1) - tf.norm(not_srcs_embedding + edge_type_embedding - not_dsts_embedding, axis=-1)) # Creating the actual model model = Model(inputs=inputs, outputs=loss, name=self.model_name()) model.add_loss(loss) model.compile(optimizer=self._optimizer) return model
def __call__(self, loss, seed_input, penultimate_layer=-1, seek_penultimate_conv_layer=True, activation_modifier=lambda cam: K.relu(cam), normalize_gradient=True, expand_cam=True): """Generate a gradient based class activation map (CAM) by using positive gradient of penultimate_layer with respect to loss. For details on Grad-CAM, see the paper: [Grad-CAM: Why did you say that? Visual Explanations from Deep Networks via Gradient-based Localization](https://arxiv.org/pdf/1610.02391v1.pdf). # Arguments loss: A loss function. If the model has multiple outputs, you can use a different loss on each output by passing a list of losses. seed_input: An N-dim Numpy array. If the model has multiple inputs, you have to pass a list of N-dim Numpy arrays. penultimate_layer: A number of integer or a tf.keras.layers.Layer object. seek_penultimate_conv_layer: True to seek the penultimate layter that is a subtype of `keras.layers.convolutional.Conv` class. If False, the penultimate layer is that was elected by penultimate_layer index. normalize_gradient: True to normalize gradients. activation_modifier: A function to modify gradients. expand_cam: True to expand cam to same as input image size. ![Note] Even if the model has multiple inputs, this function return only one cam value (That's, when `expand_cam` is True, multiple cam images are generated from a model that has multiple inputs). # Returns The heatmap image or a list of their images that indicate the `seed_input` regions whose change would most contribute the loss value, # Raises ValueError: In case of invalid arguments for `loss`, or `penultimate_layer`. """ # Preparing losses = self._get_losses_for_multiple_outputs(loss) seed_inputs = self._get_seed_inputs_for_multiple_inputs(seed_input) penultimate_output_tensor = self._find_penultimate_output( penultimate_layer, seek_penultimate_conv_layer) # Processing gradcam model = tf.keras.Model(inputs=self.model.inputs, outputs=self.model.outputs + [penultimate_output_tensor]) with tf.GradientTape() as tape: tape.watch(seed_inputs) outputs = model(seed_inputs) outputs, penultimate_output = outputs[:-1], outputs[-1] loss_values = [loss(y) for y, loss in zip(outputs, losses)] grads = tape.gradient(loss_values, penultimate_output) if normalize_gradient: grads = K.l2_normalize(grads) weights = K.mean(grads, axis=tuple(range(grads.ndim)[1:-1]), keepdims=True) cam = np.sum(penultimate_output * weights, axis=-1) if activation_modifier is not None: cam = activation_modifier(cam) if not expand_cam: return cam # Visualizing cam = self._zoom_for_visualizing(seed_inputs, cam) if len(self.model.inputs) == 1 and not isinstance(seed_input, list): cam = cam[0] return cam
def relu6(x): return K.relu(x)
def __call__(self, loss, seed_input, penultimate_layer=-1, seek_penultimate_conv_layer=True, activation_modifier=lambda cam: K.relu(cam), expand_cam=True, training=False): """Generate gradient based class activation maps (CAM) by using positive gradient of penultimate_layer with respect to loss. For details on GradCAM++, see the paper: [GradCAM++: Improved Visual Explanations for Deep Convolutional Networks] (https://arxiv.org/pdf/1710.11063.pdf). # Arguments loss: A loss function. If the model has multiple outputs, you can use a different loss on each output by passing a list of losses. seed_input: An N-dim Numpy array. If the model has multiple inputs, you have to pass a list of N-dim Numpy arrays. penultimate_layer: A number of integer or a tf.keras.layers.Layer object. seek_penultimate_conv_layer: True to seek the penultimate layter that is a subtype of `keras.layers.convolutional.Conv` class. If False, the penultimate layer is that was elected by penultimate_layer index. activation_modifier: A function to modify gradients. expand_cam: True to expand cam to same as input image size. ![Note] Even if the model has multiple inputs, this function return only one cam value (That's, when `expand_cam` is True, multiple cam images are generated from a model that has multiple inputs). training: A bool whether the model's trainig-mode turn on or off. # Returns The heatmap image or a list of their images that indicate the `seed_input` regions whose change would most contribute the loss value, # Raises ValueError: In case of invalid arguments for `loss`, or `penultimate_layer`. """ # Preparing losses = self._get_losses_for_multiple_outputs(loss) seed_inputs = self._get_seed_inputs_for_multiple_inputs(seed_input) penultimate_output_tensor = self._find_penultimate_output( penultimate_layer, seek_penultimate_conv_layer) # Processing gradcam model = tf.keras.Model(inputs=self.model.inputs, outputs=self.model.outputs + [penultimate_output_tensor]) with tf.GradientTape(watch_accessed_variables=False) as tape: tape.watch(seed_inputs) outputs = model(seed_inputs, training=training) outputs, penultimate_output = outputs[:-1], outputs[-1] loss_values = [loss(y) for y, loss in zip(outputs, losses)] grads = tape.gradient( loss_values, penultimate_output, unconnected_gradients=tf.UnconnectedGradients.ZERO) score = sum([K.exp(tf.reshape(v, (-1, ))) for v in loss_values]) score = tf.reshape(score, (-1, ) + tuple(np.ones(grads.ndim - 1, np.int))) first_derivative = score * grads second_derivative = first_derivative * grads third_derivative = second_derivative * grads global_sum = K.sum(penultimate_output, axis=tuple( np.arange(len(penultimate_output.shape))[1:-1]), keepdims=True) alpha_denom = second_derivative * 2.0 + third_derivative * global_sum alpha_denom = alpha_denom + tf.cast( (second_derivative == 0.0), second_derivative.dtype) alphas = second_derivative / alpha_denom alpha_normalization_constant = K.sum( alphas, axis=tuple(np.arange(len(alphas.shape))[1:-1]), keepdims=True) alpha_normalization_constant = alpha_normalization_constant + tf.cast( (alpha_normalization_constant == 0.0), alpha_normalization_constant.dtype) alphas = alphas / alpha_normalization_constant if activation_modifier is None: weights = first_derivative else: weights = activation_modifier(first_derivative) deep_linearization_weights = weights * alphas deep_linearization_weights = K.sum( deep_linearization_weights, axis=tuple(np.arange(len(deep_linearization_weights.shape))[1:-1]), keepdims=True) cam = K.sum(deep_linearization_weights * penultimate_output, axis=-1) if activation_modifier is not None: cam = activation_modifier(cam) if not expand_cam: return cam factors = (zoom_factor(cam.shape, X.shape) for X in seed_inputs) cam = [zoom(cam, factor) for factor in factors] if len(self.model.inputs) == 1 and not isinstance(seed_input, list): cam = cam[0] return cam
def hard_sigmoid(x): return backend.relu(x + 3.0, max_value=6.0) / 6.0
def call(self, inputs): # The first part of the input is the pointcloud. point_cloud = inputs[0] assert_shape_is(point_cloud, (1024, 3)) # The second part of the input are the KNNs. knn = inputs[1] assert_shape_is(knn, (1024, 20, 3)) # Reshape the pointcloud if necessary. if len(point_cloud.shape) == 4: pass elif len(point_cloud.shape) == 3: point_cloud = K.expand_dims(point_cloud, axis=2) else: raise Exception("Invalid shape!") assert_shape_is(point_cloud, (1024, 1, 3)) # Tile the pointcloud to make it compatible with KNN. point_cloud_tiled = K.tile(point_cloud, [1, 1, self.k, 1]) assert_shape_is(point_cloud_tiled, (1024, 20, 3)) # Compute difference between tiled pointcloud and knn. point_cloud_knn_difference = point_cloud_tiled - knn assert_shape_is(point_cloud_knn_difference, (1024, 20, 3)) # MLP 1 for self attention including batch normalization. self_attention = self.self_attention_mlp1(point_cloud) if self.batch_normalization == True: self_attention = self.self_attention_bn1(self_attention) assert_shape_is(self_attention, (1024, 1, 16)) # MLP 2 for self attention including batch normalization. self_attention = self.self_attention_mlp2(self_attention) if self.batch_normalization == True: self_attention = self.self_attention_bn2(self_attention) assert_shape_is(self_attention, (1024, 1, 1)) # MLP 1 for neighbor attention including batch normalization. neighbor_attention = self.neighbor_attention_mlp1(point_cloud_knn_difference) if self.batch_normalization == True: neighbor_attention = self.neighbor_attention_bn1(neighbor_attention) assert_shape_is(neighbor_attention, (1024, 20, 16)) # Graph features are the ouput of the first MLP. graph_features = neighbor_attention # MLP 2 for neighbor attention including batch normalization. neighbor_attention = self.neighbor_attention_mlp2(neighbor_attention) if self.batch_normalization == True: neighbor_attention = self.neighbor_attention_bn2(neighbor_attention) assert_shape_is(neighbor_attention, (1024, 20, 1)) # Merge self attention and neighbor attention to get attention coefficients. logits = self_attention + neighbor_attention assert_shape_is(logits, (1024, 20, 1)) logits = K.permute_dimensions(logits, (0, 1, 3, 2)) assert_shape_is(logits, (1024, 1, 20)) # Apply leaky relu and softmax to logits to get attention coefficents. logits = K.relu(logits, alpha=0.2) attention_coefficients = K.softmax(logits) assert_shape_is(attention_coefficients, (1024, 1, 20)) # Compute attention features from attention coefficients and graph features. attention_features = tf.matmul(attention_coefficients, graph_features) attention_features = tf.add(attention_features, self.output_bias) attention_features = K.relu(attention_features) assert_shape_is(attention_features, (1024, 1, 16)) # Reshape graph features. #graph_features = K.expand_dims(graph_features, axis=2) assert_shape_is(graph_features, (1024, 20, 16)) # Done. return attention_features, graph_features, attention_coefficients
def __call__(self, loss, seed_input, penultimate_layer=-1, activation_modifier=lambda cam: K.relu(cam), normalize_gradient=True): """Generate a gradient based class activation map (CAM) by using positive gradient of penultimate_layer with respect to loss. For details on Grad-CAM, see the paper: [Grad-CAM: Why did you say that? Visual Explanations from Deep Networks via Gradient-based Localization](https://arxiv.org/pdf/1610.02391v1.pdf). # Arguments loss: A loss function. If the model has multipul outputs, you can use a different loss on each output by passing a list of losses. seed_input: An N-dim Numpy array. If the model has multipul inputs, you have to pass a list of N-dim Numpy arrays. penultimate_layer: A number of integer or a tf.keras.layers.Layer object. normalize_gradient: True to normalize gradients. activation_modifier: A function to modify gradients. # Returns The heatmap image or a list of their images that indicate the `seed_input` regions whose change would most contribute the loss value, # Raises ValueError: In case of invalid arguments for `loss`, or `penultimate_layer`. """ losses = self._prepare_losses(loss) seed_inputs = [ x if tf.is_tensor(x) else tf.constant(x) for x in listify(seed_input) ] seed_inputs = [ tf.expand_dims(seed_input, axis=0) if X.shape == input_tensor.shape[1:] else X for X, input_tensor in zip(seed_inputs, self.model.inputs) ] if len(seed_inputs) != len(self.model.inputs): raise ValueError('') penultimate_output_tensor = self._find_penultimate_output( self.model, penultimate_layer) model = tf.keras.Model(inputs=self.model.inputs, outputs=self.model.outputs + [penultimate_output_tensor]) with tf.GradientTape() as tape: tape.watch(seed_inputs) outputs = model(seed_inputs) outputs = listify(outputs) loss_values = [loss(y) for y, loss in zip(outputs[:-1], losses)] penultimate_outputs = outputs[-1] grads = tape.gradient(loss_values, penultimate_outputs) if normalize_gradient: grads = K.l2_normalize(grads) weights = K.mean(grads, axis=tuple(np.arange(len(grads.shape))[1:-1])) cam = np.asarray([ np.sum(o * w, axis=-1) for o, w in zip(penultimate_outputs, weights) ]) if activation_modifier is not None: cam = activation_modifier(cam) input_dims_list = (X.shape[1:-1] for X in seed_inputs) output_dims = penultimate_outputs.shape[1:-1] zoom_factors = ([ i / (j * 1.0) for i, j in iter(zip(input_dims, output_dims)) ] for input_dims in input_dims_list) cams = [ np.asarray([zoom(v, factor) for v in cam]) for factor in zoom_factors ] if len(self.model.inputs) == 1 and not isinstance(seed_input, list): cams = cams[0] return cams
def margin_loss(y_true, y_pred): lamb, margin = 0.5, 0.1 return K.sum((y_true * K.square(K.relu(1 - margin - y_pred)) + lamb * (1 - y_true) * K.square(K.relu(y_pred - margin))), axis=-1)
def relu6(inputs): return K.relu(inputs, max_value=6.0)
def call(self, x, **kwargs): return (K.concatenate((K.relu(x, alpha=0), K.relu(-x, alpha=0)), axis=self.channel_axis) * self._get_coef())
def HS(self, x): """ Construct Hard Swish activation function x : input to activation function """ return (x * K.relu(x + 3, max_value=6.0)) / 6.0
def call(self, inputs, **kwargs): inputs -= K.mean(inputs, axis=1, keepdims=True) inputs = K.l2_normalize(inputs, axis=1) pos = K.relu(inputs) neg = K.relu(-inputs) return K.concatenate([pos, neg], axis=1)
def call(self, inputs): if K.dtype(inputs) != 'float32': inputs = K.cast(inputs, 'float32') inner_out = K.relu(K.dot(inputs, self.weights_inner) + self.bais_inner) outputs = K.dot(inner_out, self.weights_out) + self.bais_out return outputs
def call(self, inputs, **kwargs): pos = K.relu(inputs) neg = K.relu(-inputs) return K.concatenate([pos, neg])
def discriminator_loss_fn(self, real, fake): loss = K.mean(K.relu(1. - real), axis=0) loss += K.mean(K.relu(1. + fake), axis=0) return loss
def sparse_softmax(x): x = K.relu(x) e = K.exp(x - K.max(x, axis=(1, 2, 3), keepdims=True)) s = K.sum(e, axis=(1, 2, 3), keepdims=True) return e / s
def relu6(x): return backend.relu(x, max_value=6)
def HS(x): """ Hard Swish activation """ return (x * K.relu(x + 3, max_value=6.0)) / 6.0
def hinge_d(y_true, y_pred): return K.mean(K.relu(1.0 + (y_true * y_pred)))
def hard_swish(x): return x * K.relu(x + 3.0, max_value=6.0) / 6.0