Hii, doing fruit classification using transfer learning ,its multiclass problem so what should I do changes in code here
def alpaca_model(image_shape=IMG_SIZE, data_augmentation=data_augmenter()):
‘’’ Define a tf.keras model for binary classification out of the MobileNetV2 model
Arguments:
image_shape – Image width and height
data_augmentation – data augmentation function
Returns:
Returns:
tf.keras.model
‘’’
input_shape = image_shape + (3,)
### START CODE HERE
base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
include_top=False, # <== Important!!!!
weights='imagenet') # From imageNet
# freeze the base model by making it non trainable
base_model.trainable = False
# 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(inputs)
# data preprocessing using the same weights the model was trained on
x = preprocess_input(x)
# set training to False to avoid keeping track of statistics in the batch norm layer
x = base_model(x, training=False)
# add the new Binary classification layers
# use global avg pooling to summarize the info in each channel
x = tf.keras.layers.GlobalAveragePooling2D()(x)
# include dropout with probability of 0.2 to avoid overfitting
x = tfl.Dropout(f.2)(x)
# use a prediction layer with one neuron (as a binary classifier only needs one)
prediction_layer = tf.keras.layers.Dense(1)
outputs = prediction_layer(x)
### END CODE HERE
model = tf.keras.Model(inputs, outputs)
return model
#I have done changes in this line only:
prediction_layer = tf.keras.layers.Dense(131,activation = ‘softmax’) # because there are 131 classes
#but when I run this below line getting error:
model2 = fruit_model(IMG_SIZE, data_augmentation)
#error:
NotImplementedError Traceback (most recent call last)
in
----> 1 model2 = fruit_model(IMG_SIZE, data_augmentation)
in fruit_model(image_shape, data_augmentation)
25
26 # apply data augmentation to the inputs
—> 27 x = data_augmentation(inputs)
28
29 # data preprocessing using the same weights the model was trained on
~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in call(self, *args, **kwargs)
923 # >> model = tf.keras.Model(inputs, outputs)
924 if _in_functional_construction_mode(self, inputs, args, kwargs, input_list):
→ 925 return self._functional_construction_call(inputs, args, kwargs,
926 input_list)
927
~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in _functional_construction_call(self, inputs, args, kwargs, input_list)
1115 try:
1116 with ops.enable_auto_cast_variables(self._compute_dtype_object):
→ 1117 outputs = call_fn(cast_inputs, *args, **kwargs)
1118
1119 except errors.OperatorNotAllowedInGraphError as e:
~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\sequential.py in call(self, inputs, training, mask)
384 kwargs[‘training’] = training
385
→ 386 outputs = layer(inputs, **kwargs)
387
388 if len(nest.flatten(outputs)) != 1:
~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in call(self, *args, **kwargs)
923 # >> model = tf.keras.Model(inputs, outputs)
924 if _in_functional_construction_mode(self, inputs, args, kwargs, input_list):
→ 925 return self._functional_construction_call(inputs, args, kwargs,
926 input_list)
927
~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in _functional_construction_call(self, inputs, args, kwargs, input_list)
1115 try:
1116 with ops.enable_auto_cast_variables(self._compute_dtype_object):
→ 1117 outputs = call_fn(cast_inputs, *args, **kwargs)
1118
1119 except errors.OperatorNotAllowedInGraphError as e:
~\anaconda3\lib\site-packages\tensorflow\python\keras\layers\preprocessing\image_preprocessing.py in call(self, inputs, training)
820 interpolation=self.interpolation)
821
→ 822 output = tf_utils.smart_cond(training, random_rotated_inputs,
823 lambda: inputs)
824 output.set_shape(inputs.shape)
~\anaconda3\lib\site-packages\tensorflow\python\keras\utils\tf_utils.py in smart_cond(pred, true_fn, false_fn, name)
62 return control_flow_ops.cond(
63 pred, true_fn=true_fn, false_fn=false_fn, name=name)
—> 64 return smart_module.smart_cond(
65 pred, true_fn=true_fn, false_fn=false_fn, name=name)
66
~\anaconda3\lib\site-packages\tensorflow\python\framework\smart_cond.py in smart_cond(pred, true_fn, false_fn, name)
52 if pred_value is not None:
53 if pred_value:
—> 54 return true_fn()
55 else:
56 return false_fn()
~\anaconda3\lib\site-packages\tensorflow\python\keras\layers\preprocessing\image_preprocessing.py in random_rotated_inputs()
816 return transform(
817 inputs,
→ 818 get_rotation_matrix(angles, img_hd, img_wd),
819 fill_mode=self.fill_mode,
820 interpolation=self.interpolation)
~\anaconda3\lib\site-packages\tensorflow\python\keras\layers\preprocessing\image_preprocessing.py in get_rotation_matrix(angles, image_height, image_width, name)
720 math_ops.cos(angles)[:, None],
721 y_offset[:, None],
→ 722 array_ops.zeros((num_angles, 2), dtypes.float32),
723 ],
724 axis=1)
~\anaconda3\lib\site-packages\tensorflow\python\util\dispatch.py in wrapper(*args, **kwargs)
199 “”“Call target, and fall back on dispatchers if there is a TypeError.”""
200 try:
→ 201 return target(*args, **kwargs)
202 except (TypeError, ValueError):
203 # Note: convert_to_eager_tensor currently raises a ValueError, not a
~\anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py in wrapped(*args, **kwargs)
2745
2746 def wrapped(*args, **kwargs):
→ 2747 tensor = fun(*args, **kwargs)
2748 tensor._is_zeros_tensor = True
2749 return tensor
~\anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py in zeros(shape, dtype, name)
2792 # Create a constant if it won’t be very big. Otherwise create a fill
2793 # op to prevent serialized GraphDefs from becoming too large.
→ 2794 output = _constant_if_small(zero, shape, dtype, name)
2795 if output is not None:
2796 return output
~\anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py in _constant_if_small(value, shape, dtype, name)
2730 def _constant_if_small(value, shape, dtype, name):
2731 try:
→ 2732 if np.prod(shape) < 1000:
2733 return constant(value, shape=shape, dtype=dtype, name=name)
2734 except TypeError:
<array_function internals> in prod(*args, **kwargs)
~\anaconda3\lib\site-packages\numpy\core\fromnumeric.py in prod(a, axis, dtype, out, keepdims, initial, where)
3028 10
3029 “”"
→ 3030 return _wrapreduction(a, np.multiply, ‘prod’, axis, dtype, out,
3031 keepdims=keepdims, initial=initial, where=where)
3032
~\anaconda3\lib\site-packages\numpy\core\fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
85 return reduction(axis=axis, out=out, **passkwargs)
86
—> 87 return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
88
89
~\anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in array(self)
843
844 def array(self):
→ 845 raise NotImplementedError(
846 “Cannot convert a symbolic Tensor ({}) to a numpy array.”
847 " This error may indicate that you’re trying to pass a Tensor to"
NotImplementedError: Cannot convert a symbolic Tensor (sequential_1/random_rotation_1/rotation_matrix_4/strided_slice:0) to a numpy array. This error may indicate that you’re trying to pass a Tensor to a NumPy call, which is not supported
#please help!