VeeranjaneyuluToka / tensorflow

TF experimentations
0 stars 0 forks source link

Encoder (Dense connection without flatten) & Decoder input does not have reshape #1

Open anugrahasinha opened 4 years ago

anugrahasinha commented 4 years ago

@VeeranjaneyuluToka

Please let me know if my understanding is correct here or not?

Encoder - Dense connection without Flatten

class ConvAutoEncoder(HyperModel):
    def __init__(self, input_shape, is_deeper=False):
--snip--
    """ encoder """
    def encoder(self, input_img, hp):

        conv1 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same')(input_img) #224 x 224 x 32
        conv1 = tf.keras.layers.BatchNormalization()(conv1)
        conv1 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same')(conv1)
--snip--
        conv6 = tf.keras.layers.Conv2D(512, (3, 3), activation='relu', padding='same')(conv5)
        conv6 = tf.keras.layers.BatchNormalization()(conv6)
        conv6 = tf.keras.layers.Conv2D(512, (3, 3), activation='relu', padding='same')(conv6)
        conv6 = tf.keras.layers.BatchNormalization()(conv6)

        # Comment (asinha) : conv6 output would be (14,14,512)
        # However, we do not flatten this before building a dense below this
        # would this be correct?

        dense = tf.keras.layers.Dense(16, activation='relu', name='encoder')(conv6)

Decoder : Input does not reshape the encoded input before applying a Conv layer

class ConvAutoEncoder(HyperModel):
    def __init__(self, input_shape, is_deeper=False):
--snip--
    """ decoder """
    def decoder(self, dense, hp):

        # Comment (asinha) : If we are getting a dense input, which I am believing a vector if 16 (as 
        # per last layer at encoder), needs to be reshaped before we apply a CONV2D(256)
        # Also I do not understand why do we have #56x56x128 added ahead of conv5?

        conv5 = tf.keras.layers.Conv2D(256, (3, 3), activation='relu', padding='same')(dense) #56 x 56 x 128
        conv5 = tf.keras.layers.BatchNormalization()(conv5)
        conv5 = tf.keras.layers.Conv2D(256, (3, 3), activation='relu', padding='same')(conv5)
        conv5 = tf.keras.layers.BatchNormalization()(conv5)
VeeranjaneyuluToka commented 4 years ago

Really do not matter looks like though i do not have answers what advantage it has with or without flatten. There are some cases where implementation do not use Flatten but did not state any reason behind that for example YOLO (source https://datascience.stackexchange.com/questions/27135/should-there-be-a-flat-layer-in-between-the-conv-layers-and-dense-layer-in-yolo). Here is the summary of my network around dense layer

conv2d_9 (Conv2D) (None, 28, 28, 512) 2359808


batch_normalization_9 (Batch (None, 28, 28, 512) 2048


max_pooling2d_3 (MaxPooling2 (None, 14, 14, 512) 0


conv2d_10 (Conv2D) (None, 14, 14, 512) 2359808


batch_normalization_10 (Batc (None, 14, 14, 512) 2048


conv2d_11 (Conv2D) (None, 14, 14, 512) 2359808


batch_normalization_11 (Batc (None, 14, 14, 512) 2048


encoder (Dense) (None, 14, 14, 16) 8208


conv2d_12 (Conv2D) (None, 14, 14, 256) 37120


batch_normalization_12 (Batc (None, 14, 14, 256) 1024


conv2d_13 (Conv2D) (None, 14, 14, 256) 590080


batch_normalization_13 (Batc (None, 14, 14, 256) 1024


conv2d_14 (Conv2D) (None, 14, 14, 128) 295040


batch_normalization_14 (Batc (None, 14, 14, 128) 512


conv2d_15 (Conv2D) (None, 14, 14, 128) 147584


batch_normalization_15 (Batc (None, 14, 14, 128) 512


up_sampling2d (UpSampling2D) (None, 28, 28, 128) 0


conv2d_16 (Conv2D) (None, 28, 28, 64) 73792

However i have experimented with Flatten() and reshape() and i do not see any change using them in accuracy but there is some difference in no.of parameters. However i am yet to conclude on this and post my observations on this point as soon as i find.

Thank you very much for point out this.