Home > OS >  Training Deep Neural Network Using Tensorflow
Training Deep Neural Network Using Tensorflow

Time:01-13

I am trying to immplement the VGG16 network using Tensorflow. to test the model, i want to classify a dataset of images. i sarted by creating a train_data with tensorflow.data.Dataset:

<TensorSliceDataset element_spec=(TensorSpec(shape=(215, 160, 3), dtype=tf.float64, name=None), TensorSpec(shape=(20,), dtype=tf.float64, name=None))>

(their is 20 ouput classes)

created a custom 2d Conv Layer:

`class CustomConv2d(Layer):

def __init__(self,filters,kernel_size,padding,name):
    super(CustomConv2d,self).__init__()
    self.conv = Conv2D(filters=filters,
                        kernel_size=kernel_size,
                        activation='relu',
                        padding =padding,
                        name=name,
                        )
    self.batchN = BatchNormalization()

def call(self,x,training=True):
    output = self.conv(x)
    output = self.batchN(output)
    return output`

Created a Sub Model Class

`class VGG16(Model):

def __init__(self,input_shape,NUM_OF_CLASSES=20,dropout_parameters=0.5):
    super(VGG16,self).__init__()
    self.dropout = Dropout(dropout_parameters)
    ### First Conv Block 
    self.conv_11 = Conv2D(filters=53,
                        kernel_size=(3,3),
                        activation='relu',
                        padding ='same',
                        name='conv11',
                        input_shape=input_shape
                        )
    self.conv_12 = CustomConv2d(64,(3,3),padding='same',name='conv_12')
    self.maxpool = MaxPool2D(pool_size=(2,2),padding='same')`

    ## Second Conv Block 
    self.conv21 = CustomConv2d(64,(3,3),padding='same',name='conv_21')
    self.conv22 = CustomConv2d(64,(3,3),padding='same',name='conv_22')
    ## Third Conv Block 
    self.conv31 = CustomConv2d(256,(3,3),padding='same',name='conv_31')
    self.conv32 = CustomConv2d(256,(3,3),padding='same',name='conv_32')
    self.conv33 = CustomConv2d(256,(3,3),padding='same',name='conv_33')
    ## Fourth Conv Block 
    self.conv41 = CustomConv2d(512,(3,3),padding='same',name='conv_41')
    self.conv42 = CustomConv2d(512,(3,3),padding='same',name='conv_42')
    self.conv43 = CustomConv2d(512,(3,3),padding='same',name='conv_43')
    ## Fifth CConv Block 
    self.conv51 = CustomConv2d(512,(3,3),padding='same',name='conv_51')
    self.conv52 = CustomConv2d(512,(3,3),padding='same',name='conv_52')
    self.conv53 = CustomConv2d(512,(3,3),padding='same',name='conv_53')
    #####
    self.flatten = Flatten()
    self.dense1 = Dense(1024,activation='relu',name='Dense_1')
    self.dense2 = Dense(512,activation='relu',name='Dense_2')
    self.dense3 = Dense(NUM_OF_CLASSES,activation='softmax',name='Dense_3')

def call(self,x,training=True):
    x = self.maxpool(self.conv_12(self.conv_11(x)))
    x = self.maxpool(self.conv22(self.conv21(x)))
    x = self.maxpool(self.conv33(self.conv32(self.conv31(x))))
    x = self.maxpool(self.conv43(self.conv42(self.conv41(x))))
    x = self.maxpool(self.conv53(self.conv52(self.conv51(x))))
    
    x = self.flatten(x)
    x = self.dense3(self.dense2(self.dense1(x)))
    return x 

model = VGG16((215,160,3,1)) ` by the way i dont know why i have to put 1 at the end of shape After Compiling the model when i try to fit the data to the model i have this error:

`ValueError: in user code:

` File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1021, in train_function * return step_function(self, iterator) File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1010, in step_function ** outputs = model.distribute_strategy.run(run_step, args=(data,)) File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1000, in run_step ** outputs = model.train_step(data) File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 859, in train_step y_pred = self(x, training=True) File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler raise e.with_traceback(filtered_tb) from None

ValueError: Exception encountered when calling layer "vgg16_2" (type VGG16).

in user code:

    File "/tmp/ipykernel_49926/980605695.py", line 38, in call  *
        x = self.maxpool(self.conv_12(self.conv_11(x)))
    File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler  **
        raise e.with_traceback(filtered_tb) from None
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/input_spec.py", line 228, in assert_input_compatibility
        raise ValueError(f'Input {input_index} of layer "{layer_name}" '

    ValueError: Input 0 of layer "conv11" is incompatible with the layer: expected min_ndim=4, found ndim=3. Full shape received: (215, 160, 3)


Call arguments received:
  • x=tf.Tensor(shape=(215, 160, 3), dtype=float32)
  • training=True``

Tought maybe their is problem in the shape of the Dataset

CodePudding user response:

Edit: the problem was solved after spliting the data into batches with:

trainDataset = trainDataset.shuffle(buffer_size=20).prefetch(buffer_size=15).batch(32)

but now the training is very slow. is it normal to take about 5 sec on each image of the batch?

CodePudding user response:

It should be

model = VGG16((215,160,3))

Because conv2d input shape parameter take 3 values. (Height,width,channel)

you can input model like model(np.ones((1,215,160,3))) where 1 is the batch size

  • Related