Initializers

In the document we learn how to use different initializers for morphological layers.

import tensorflow
import numpy as np
import matplotlib.pyplot as plt
plt.rc('font', family='serif')
plt.rc('xtick', labelsize='x-small')
plt.rc('ytick', labelsize='x-small')
import skimage
print(skimage.__version__)
print(tensorflow.__version__)
print(np.__version__)
0.15.0
2.2.0
1.18.1
from morpholayers import *
from morpholayers.layers import *
from morpholayers.initializers import *
from tensorflow.keras.layers import Input,Lambda,ZeroPadding2D
from tensorflow.keras.models import Model
from imageio import imread
I=imread('./Images/airplane00.png')*1.
I=(I[:,:,0:1]>200)*1.
size_im=I.shape

plt.figure(figsize=(6,6))
plt.imshow(I[:,:,0])
plt.gray()
plt.show()
_images/tutorial2initializers_6_0.png

Initializers define the way to set the initial random weights of Keras layers. For the case of a morphological layer, one should take into account that the operators is performed in the max-plus algebra.

Keras proposes some initializations (https://keras.io/initializers/) for linear convolution than can be used in morpholayers:

  1. Zeros: Flat SE (Used as default value):

    Dilation2D(1,kernel_size=(7,7),kernel_initializer='Zeros'), describe a squared flat structuring element of side 7 pixels.

  2. RandomNormal(mean=0.0, stddev=0.05):

    Dilation2D(1,kernel_size=(7,7),kernel_initializer=RandomNormal(stddev=0.01)), describe a non-flat structuring element of side 7 pixels with random values from a Normal distribution with zero-mean and 0.01 as standard deviation.

  3. RandomUniform(minval=-0.05, maxval=0.05)

  4. Ones: All value on 1. (Non-sense in the case of a morphological layer)

New initializations for structuring elements are availables in morpholayers.py:

  1. Quadratic: Quadratic(t,c)

  2. RandomLattice: Random values between (-\(\infty\) and \(\infty\))

  3. RandomNegativeLattice: Random values between (-\(\infty\) and \(0\))

  4. MinusOnesZeroCenter: Identity SE (0 in the center and -\(\infty\) otherwise)

  5. SignedOnes: Random (-\(\infty\) or \(\infty\)) values

  6. SparseZeros: SparseZeros(th=.95),(th*100)% values in -\(\infty\) and the rest to \(0\).

  7. SparseNumZeros: SparseNumZeros(th=0),(th+1) values in \(0\) and the rest to -\(\infty\).

  8. RandomwithMinLattice: Random values between (-\(\infty\) and \(\infty\)) and center to \(-\infty\)

  9. RandomwithZeroLattice: Random values between (-\(\infty\) and \(\infty\)) and center to \(0\)

  10. RandomwithMaxLattice: Random values between (-\(\infty\) and \(\infty\)) and center to \(\infty\)

Note

Note -\(\infty\) and \(\infty\) values are defined in morpho.py file. By default, we consider -\(\infty=-1\) or \(\infty=1\), i.e., images values in \([0,1]\).

Zeros

InLayer=Input(size_im)
x=Dilation2D(1,kernel_size=(7,7),kernel_initializer='Zeros')(InLayer)
model=Model(InLayer,x)
model.summary()
Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 256, 256, 1)]     0         
_________________________________________________________________
dilation2d (Dilation2D)      (None, 256, 256, 1)       49        
=================================================================
Total params: 49
Trainable params: 49
Non-trainable params: 0
_________________________________________________________________

Visualization of \(W\), in this case a constant value equation to zero.

listW=model.get_weights()
SE=listW[-1]
print(SE.shape)
plt.figure()
plt.imshow(SE[:,:,0,0],cmap='RdBu',vmax=1,vmin=-1)
plt.show()
print('Max Value',SE.max(),'Min Value',SE.min())

Z=model.predict(np.expand_dims(I,axis=0))
plt.figure(figsize=(6,6))
plt.title('Dilation with Flat SE')
plt.imshow(Z[0,:,:,0]/255)
plt.show()
(7, 7, 1, 1)
_images/tutorial2initializers_11_1.png
Max Value 0.0 Min Value 0.0
_images/tutorial2initializers_11_3.png
InLayer=Input(size_im)
x=Dilation2D(1,kernel_size=(7,7),kernel_initializer='RandomUniform')(InLayer)

model=Model(InLayer,x)
model.summary()
Model: "model_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_2 (InputLayer)         [(None, 256, 256, 1)]     0         
_________________________________________________________________
dilation2d_1 (Dilation2D)    (None, 256, 256, 1)       49        
=================================================================
Total params: 49
Trainable params: 49
Non-trainable params: 0
_________________________________________________________________

Visualization of \(W\), in this case random uniform values between -0.05 and 0.05.

listW=model.get_weights()
SE=listW[-1]
print(SE.shape)
plt.figure()
plt.imshow(SE[:,:,0,0],cmap='RdBu',vmax=1,vmin=-1)
plt.show()
print(SE.max(),SE.min())


Z=model.predict(np.expand_dims(I,axis=0))
plt.figure(figsize=(6,6))
plt.title('Dilation with Nonflat SE')
plt.imshow(Z[0,:,:,0]/255)
plt.show()
(7, 7, 1, 1)
_images/tutorial2initializers_14_1.png
0.046920624 -0.049540937
_images/tutorial2initializers_14_3.png

Quadratic

InLayer=Input(size_im)
x=Dilation2D(1,kernel_size=(7,7),kernel_initializer=Quadratic(cvalue=.2,tvalue=2))(InLayer)
model=Model(InLayer,x)
model.summary()
Model: "model_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_3 (InputLayer)         [(None, 256, 256, 1)]     0         
_________________________________________________________________
dilation2d_2 (Dilation2D)    (None, 256, 256, 1)       49        
=================================================================
Total params: 49
Trainable params: 49
Non-trainable params: 0
_________________________________________________________________

\(W\) is a quadratic SE with default value \(t=2\) and \(c=0.02\)

listW=model.get_weights()
SE=listW[-1]
print(SE.shape)
plt.figure()
plt.imshow(SE[:,:,0,0],cmap='RdBu',vmax=1,vmin=-1)
plt.colorbar()
print(SE.max(),SE.min())

Z=model.predict(np.expand_dims(I,axis=0))
plt.figure(figsize=(6,6))
plt.title('Dilation with Nonflat Quadratic SE')
plt.imshow(Z[0,:,:,0]/255)
plt.show()
(7, 7, 1, 1)
-0.0 -0.9
_images/tutorial2initializers_18_1.png _images/tutorial2initializers_18_2.png

RandomLattice

InLayer=Input(size_im)
x=Dilation2D(1,kernel_size=(7,7),padding='same',kernel_initializer=RandomLattice)(InLayer)
model=Model(InLayer,x)
model.summary()
Model: "model_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_4 (InputLayer)         [(None, 256, 256, 1)]     0         
_________________________________________________________________
dilation2d_3 (Dilation2D)    (None, 256, 256, 1)       49        
=================================================================
Total params: 49
Trainable params: 49
Non-trainable params: 0
_________________________________________________________________

\(W\) is a random values between \(-1\) a \(1\)

listW=model.get_weights()
SE=listW[-1]
print(SE.shape)
plt.figure()
plt.imshow(SE[:,:,0,0],cmap='RdBu',vmax=1,vmin=-1)
plt.colorbar()
print(SE.max(),SE.min())

Z=model.predict(np.expand_dims(I,axis=0))
plt.figure(figsize=(6,6))
plt.title('Dilation with Random Lattice SE')
plt.imshow(Z[0,:,:,0]/255)
plt.show()
(7, 7, 1, 1)
0.9823985 -0.9326203
_images/tutorial2initializers_22_1.png _images/tutorial2initializers_22_2.png

MinusOnesZeroCenter

InLayer=Input(size_im)
x=Dilation2D(1,kernel_size=(7,7),padding='same',kernel_initializer=MinusOnesZeroCenter)(InLayer)
model=Model(InLayer,x)
model.summary()
Model: "model_4"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_5 (InputLayer)         [(None, 256, 256, 1)]     0         
_________________________________________________________________
dilation2d_4 (Dilation2D)    (None, 256, 256, 1)       49        
=================================================================
Total params: 49
Trainable params: 49
Non-trainable params: 0
_________________________________________________________________
listW=model.get_weights()
SE=listW[-1]
print(SE.shape)
plt.figure()
plt.imshow(SE[:,:,0,0],cmap='RdBu',vmax=1,vmin=-1)
plt.colorbar()
print(SE.max(),SE.min())

Z=model.predict(np.expand_dims(I,axis=0))
plt.figure(figsize=(6,6))
plt.title('Dilation with MinusOnesZeroCenter SE')
plt.imshow(Z[0,:,:,0]/255)
plt.show()
(7, 7, 1, 1)
0.0 -1.0
_images/tutorial2initializers_25_1.png _images/tutorial2initializers_25_2.png

SignedOnes

InLayer=Input(size_im)
x=Dilation2D(1,kernel_size=(7,7),padding='same',kernel_initializer=SignedOnes)(InLayer)
model=Model(InLayer,x)
model.summary()
Model: "model_5"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_6 (InputLayer)         [(None, 256, 256, 1)]     0         
_________________________________________________________________
dilation2d_5 (Dilation2D)    (None, 256, 256, 1)       49        
=================================================================
Total params: 49
Trainable params: 49
Non-trainable params: 0
_________________________________________________________________
listW=model.get_weights()
SE=listW[-1]
print(SE.shape)
plt.figure()
plt.imshow(SE[:,:,0,0],cmap='RdBu',vmax=1,vmin=-1)
plt.colorbar()
print(SE.max(),SE.min())
print(SE[3,3])

Z=model.predict(np.expand_dims(I,axis=0))
plt.figure(figsize=(6,6))
plt.title('Dilation with SignedOnes SE')
plt.imshow(Z[0,:,:,0]/255)
plt.show()
(7, 7, 1, 1)
1.0 -1.0
[[-1.]]
_images/tutorial2initializers_28_1.png _images/tutorial2initializers_28_2.png

SparseZeros

InLayer=Input(size_im)
x=Dilation2D(1,kernel_size=(7,7),padding='same',kernel_initializer=SparseZeros(th=.9))(InLayer)
model=Model(InLayer,x)
model.summary()
Model: "model_6"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_7 (InputLayer)         [(None, 256, 256, 1)]     0         
_________________________________________________________________
dilation2d_6 (Dilation2D)    (None, 256, 256, 1)       49        
=================================================================
Total params: 49
Trainable params: 49
Non-trainable params: 0
_________________________________________________________________
listW=model.get_weights()
SE=listW[-1]
print(SE.shape)
plt.figure()
plt.imshow(SE[:,:,0,0],cmap='RdBu',vmax=1,vmin=-1)
plt.colorbar()
print(SE.max(),SE.min())

Z=model.predict(np.expand_dims(I,axis=0))
plt.figure(figsize=(6,6))
plt.title('Dilation with SparseZeros SE')
plt.imshow(Z[0,:,:,0]/255)
plt.show()
(7, 7, 1, 1)
0.0 -1.0
_images/tutorial2initializers_31_1.png _images/tutorial2initializers_31_2.png

SparseNumZeros

InLayer=Input(size_im)
x=Dilation2D(1,kernel_size=(7,7),padding='same',kernel_initializer=SparseNumZeros(th=0))(InLayer)
model=Model(InLayer,x)
model.summary()
Model: "model_7"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_8 (InputLayer)         [(None, 256, 256, 1)]     0         
_________________________________________________________________
dilation2d_7 (Dilation2D)    (None, 256, 256, 1)       49        
=================================================================
Total params: 49
Trainable params: 49
Non-trainable params: 0
_________________________________________________________________

\(W\) has all values in \(-\infty\) except for one. This produce a translation of the image!

listW=model.get_weights()
SE=listW[-1]
print(SE.shape)
plt.figure()
plt.imshow(SE[:,:,0,0],cmap='RdBu',vmax=1,vmin=-1)
plt.colorbar()
print(SE.max(),SE.min())

Z=model.predict(np.expand_dims(I,axis=0))
plt.figure(figsize=(6,6))
plt.title('Dilation with SparseNumZeros SE')
plt.imshow(Z[0,:,:,0]/255)
plt.show()
(7, 7, 1, 1)
0.0 -1.0
_images/tutorial2initializers_35_1.png _images/tutorial2initializers_35_2.png

RandomNegativeLattice

InLayer=Input(size_im)
x=Dilation2D(1,kernel_size=(7,7),padding='same',kernel_initializer=RandomNegativeLattice())(InLayer)
model=Model(InLayer,x)
model.summary()

listW=model.get_weights()
SE=listW[-1]
print(SE.shape)
plt.figure()
plt.imshow(SE[:,:,0,0],cmap='RdBu',vmax=1,vmin=-1)
plt.colorbar()
print(SE.max(),SE.min())

Z=model.predict(np.expand_dims(I,axis=0))
plt.figure(figsize=(6,6))
plt.title('Dilation with RandomNegativeLattice SE')
plt.imshow(Z[0,:,:,0]/255)
plt.show()
Model: "model_8"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_9 (InputLayer)         [(None, 256, 256, 1)]     0         
_________________________________________________________________
dilation2d_8 (Dilation2D)    (None, 256, 256, 1)       49        
=================================================================
Total params: 49
Trainable params: 49
Non-trainable params: 0
_________________________________________________________________
(7, 7, 1, 1)
-0.04034376 -0.99245477
_images/tutorial2initializers_37_1.png _images/tutorial2initializers_37_2.png

RandomwithZeroLattice

InLayer=Input(size_im)
x=Dilation2D(1,kernel_size=(7,7),padding='same',kernel_initializer=RandomwithZeroLattice())(InLayer)
model=Model(InLayer,x)
model.summary()


listW=model.get_weights()
SE=listW[-1]
print(SE.shape)
plt.figure()
plt.imshow(SE[:,:,0,0],cmap='RdBu',vmax=1,vmin=-1)
plt.colorbar()
print(SE.max(),SE.min())

Z=model.predict(np.expand_dims(I,axis=0))
plt.figure(figsize=(6,6))
plt.title('Dilation with RandomwithZeroLattice SE')
plt.imshow(Z[0,:,:,0]/255)
plt.show()
Model: "model_9"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_10 (InputLayer)        [(None, 256, 256, 1)]     0         
_________________________________________________________________
dilation2d_9 (Dilation2D)    (None, 256, 256, 1)       49        
=================================================================
Total params: 49
Trainable params: 49
Non-trainable params: 0
_________________________________________________________________
(7, 7, 1, 1)
0.9844159 -0.99327016
_images/tutorial2initializers_39_1.png _images/tutorial2initializers_39_2.png

RandomwithMaxLattice

InLayer=Input(size_im)
x=Dilation2D(1,kernel_size=(7,7),padding='same',kernel_initializer=RandomwithMaxLattice())(InLayer)
model=Model(InLayer,x)
model.summary()

listW=model.get_weights()
SE=listW[-1]
print(SE.shape)
plt.figure()
plt.imshow(SE[:,:,0,0],cmap='RdBu',vmax=1,vmin=-1)
plt.colorbar()
print(SE.max(),SE.min())

Z=model.predict(np.expand_dims(I,axis=0))
plt.figure(figsize=(6,6))
plt.title('Dilation with RandomwithMaxLattice SE')
plt.imshow(Z[0,:,:,0]/255)
plt.show()
Model: "model_10"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_11 (InputLayer)        [(None, 256, 256, 1)]     0         
_________________________________________________________________
dilation2d_10 (Dilation2D)   (None, 256, 256, 1)       49        
=================================================================
Total params: 49
Trainable params: 49
Non-trainable params: 0
_________________________________________________________________
(7, 7, 1, 1)
1.0 -0.91787416
_images/tutorial2initializers_41_1.png _images/tutorial2initializers_41_2.png

RandomwithMinLattice

InLayer=Input(size_im)
x=Dilation2D(1,kernel_size=(7,7),padding='same',kernel_initializer=RandomwithMinLattice())(InLayer)
model=Model(InLayer,x)
model.summary()

listW=model.get_weights()
SE=listW[-1]
print(SE.shape)
plt.figure()
plt.imshow(SE[:,:,0,0],cmap='RdBu',vmax=1,vmin=-1)
plt.colorbar()
print(SE.max(),SE.min())

Z=model.predict(np.expand_dims(I,axis=0))
plt.figure(figsize=(6,6))
plt.title('Dilation with RandomwithMinLattice SE')
plt.imshow(Z[0,:,:,0]/255)
plt.show()
Model: "model_11"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_12 (InputLayer)        [(None, 256, 256, 1)]     0         
_________________________________________________________________
dilation2d_11 (Dilation2D)   (None, 256, 256, 1)       49        
=================================================================
Total params: 49
Trainable params: 49
Non-trainable params: 0
_________________________________________________________________
(7, 7, 1, 1)
0.97744006 -1.0
_images/tutorial2initializers_43_1.png _images/tutorial2initializers_43_2.png