[docs]defantirect(x):"""Function that implements the antirectifier activation"""mean,_=tf.nn.moments(x,axes=-1,keepdims=True)x=tf.math.l2_normalize(x-mean,axis=-1)returntf.nn.crelu(x)
[docs]defget_activation(activation:str=None):"""Get activation function given string. Args: activation (str, optional): name of activation function. Defaults to None. Raises: ValueError: Cannot find activation function Returns: Tensorflow activation function """ifactivation.lower()=="relu":returnReLU()elifactivation.lower()=="leakyrelu":returnLeakyReLU()elifactivation.lower()=="swish":returnLambda(lambdax:keras.activations.swish(x))elifactivation.lower()=="sigmoid":returnLambda(lambdax:keras.activations.sigmoid(x))elifactivationisNone:returnLambda(lambdax:x)else:raiseValueError("Unknown activation function.")
[docs]deftf_cropping_and_padding(input_shape,target_shape):"""Crop or pad a tensor to the specified shape. Args: input_shape: A list or tuple of integers representing the input shape. target_shape: A list or tuple of integers representing the desired shape. Returns: A tensorflow cropping layer (2D) for 4D tensors. """assertlen(input_shape)==len(target_shape)==2,"can only do 2D cropping"# Calculate the amount of cropping needed for each dimensiondiff=np.array(input_shape)-np.array(target_shape)cropping=((0,diff[0]),(0,diff[1]))# replace negative values with zero and make padding tuple of tuplespadding=tuple(tuple(0ifx>0else-xforxinrow)forrowincropping)cropping=tuple(tuple(0ifx<0elsexforxinrow)forrowincropping)# Create a Cropping2D layer with the calculated crop amountscropping_layer=keras.layers.Cropping2D(cropping=cropping)# Create a ZeroPadding2D layer with the calculated padding amountspadding_layer=keras.layers.ZeroPadding2D(padding=padding)returncropping_layer,padding_layer