C4 W2 A2 Preprocess input type error

my code:

 inputs = tf.keras.Input(shape=input_shape) 
    
    # apply data augmentation to the inputs
    x = data_augmenter(inputs)
    
    # data preprocessing using the same weights the model was trained on
    x = preprocess_input(x) 

output:

TypeError                                 Traceback (most recent call last)
<ipython-input-41-11ffc7a7acb3> in <module>
----> 1 model2 = alpaca_model(IMG_SIZE, data_augmentation)

<ipython-input-40-a7e00bcc0585> in alpaca_model(image_shape, data_augmentation)
     27 
     28     # apply data augmentation to the inputs
---> 29     x = data_augmenter(inputs)
     30 
     31     # data preprocessing using the same weights the model was trained on

TypeError: data_augmenter() takes 0 positional arguments but 1 was given

how do i apply the data augmentation to inputs if it is not accepting any positional arguments?

You declare the function assigned to another variable without parameters first, and then you use that variable as you did here instead of the function directly.

Right, use the helper function.

1 Like
  • is this correct?
# create the input layer (Same as the imageNetv2 input size)
    inputs = tf.keras.Input(shape=input_shape) 
    
    # apply data augmentation to the inputs
    x = data_augmentation
    
    # data preprocessing using the same weights the model was trained on
    x = preprocess_input(x) 
  • but there is still error
WARNING:tensorflow:`input_shape` is undefined or non-square, or `rows` is not in [96, 128, 160, 192, 224]. Weights for input shape (224, 224) will be loaded as the default.
Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/mobilenet_v2/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_224_no_top.h5
9412608/9406464 [==============================] - 0s 0us/step
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-11ffc7a7acb3> in <module>
----> 1 model2 = alpaca_model(IMG_SIZE, data_augmentation)

<ipython-input-15-4c90c4d7794f> in alpaca_model(image_shape, data_augmentation)
     30 
     31     # data preprocessing using the same weights the model was trained on
---> 32     x = preprocess_input(x)
     33 
     34     # set training to False to avoid keeping track of statistics in the batch norm layer

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/applications/mobilenet_v2.py in preprocess_input(x, data_format)
    500 @keras_export('keras.applications.mobilenet_v2.preprocess_input')
    501 def preprocess_input(x, data_format=None):
--> 502   return imagenet_utils.preprocess_input(x, data_format=data_format, mode='tf')
    503 
    504 

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/applications/imagenet_utils.py in preprocess_input(x, data_format, mode)
    117   else:
    118     return _preprocess_symbolic_input(
--> 119         x, data_format=data_format, mode=mode)
    120 
    121 

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/applications/imagenet_utils.py in _preprocess_symbolic_input(x, data_format, mode)
    261   """
    262   if mode == 'tf':
--> 263     x /= 127.5
    264     x -= 1.
    265     return x

TypeError: unsupported operand type(s) for /=: 'Sequential' and 'float'

No, it is not correct.
You can tell because it is throwing an error.

1 Like

i know that, i am asking for a solution . do tell the hint if you know , not the things I already know

@saifkhanengr can you please help on this, you are the best mentor ever.

Hello @Rohan_Devaki!

Please note that no one will give you a solution. All mentors are the best and guide learners voluntarily.

Regarding your query, the error lies in this line.

Hint: The data_augmentation needs an input image(s). You can read more about it here.

1 Like
  • thankyou that error got solved , i just needed hint, not a solution

but i got new error

   # set training to False to avoid keeping track of statistics in the batch norm layer
    x = base_model(None, training=False) 

what do i need to pass in First arugment to base model ? i didnt understand
output

AttributeError                            Traceback (most recent call last)
<ipython-input-16-11ffc7a7acb3> in <module>
----> 1 model2 = alpaca_model(IMG_SIZE, data_augmentation)

<ipython-input-15-87e42a440f89> in alpaca_model(image_shape, data_augmentation)
     33 
     34     # set training to False to avoid keeping track of statistics in the batch norm layer
---> 35     x = base_model(None, training=False)
     36 
     37     # add the new Binary classification layers

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
    983 
    984         with ops.enable_auto_cast_variables(self._compute_dtype_object):
--> 985           outputs = call_fn(inputs, *args, **kwargs)
    986 
    987         if self._activity_regularizer:

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/functional.py in call(self, inputs, training, mask)
    384     """
    385     return self._run_internal_graph(
--> 386         inputs, training=training, mask=mask)
    387 
    388   def compute_output_shape(self, input_shape):

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/functional.py in _run_internal_graph(self, inputs, training, mask)
    482       masks = self._flatten_to_reference_inputs(mask)
    483     for input_t, mask in zip(inputs, masks):
--> 484       input_t._keras_mask = mask
    485 
    486     # Dictionary mapping reference tensors to computed tensors.

AttributeError: 'NoneType' object has no attribute '_keras_mask'

The base_model takes the input data, the preprocess one.

  • ok the model2 got compiled

but there is new error

Test failed 
 Expected value 

 ['Functional', (None, 5, 5, 1280), 2257984] 

 does not match the input value: 

 ['Functional', (None, None, None, 1280), 2257984]
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-19-0346cb4bf847> in <module>
     10                     ['Dense', (None, 1), 1281, 'linear']] #linear is the default activation
     11 
---> 12 comparator(summary(model2), alpaca_summary)
     13 
     14 for layer in summary(model2):

~/work/W2A2/test_utils.py in comparator(learner, instructor)
     21                   "\n\n does not match the input value: \n\n",
     22                   colored(f"{a}", "red"))
---> 23             raise AssertionError("Error in test")
     24     print(colored("All tests passed!", "green"))
     25 

AssertionError: Error in test

Try to get the benefit of the search option to find similar threads, like this one. You may use keyword like “alpaca_model AssertionError: Error in test” or any other keywords. Update us here.

I have access to my course materials now. To answer the original question, this line of code should reference data_augmentation(…), not data_augmenter(…).

data_augmentation(…) is the helper function that is defined in this cell:

To expand a bit, that’s the input data after augmentation and preprocessing. It’s named ‘x’ in the notebook.

I think there’s a mistake in the value you are passing to base_model(…).

thankyou i figured it out already , thanks for your time

Could you reply with the root cause, so that other students might benefit?

1 Like