tensorflow/addons

HammingLoss while training

shamindraparui opened this issue · 0 comments

I am building a network for multi-label image classifier (Colab). As the metric, I am using HammingLoss.. While training, it is throwing ValueError: None values not supported. What can be the point that I am missing? I am using Tensorflow ImageDataGenerator to make a batch of 8 images along with its labels. Below is the network architecture and fit method:

vgg16 = tf.keras.applications.VGG16
weight = vgg16(weights='imagenet', include_top=False, input_shape=(256,256,3))
weight.trainable = False
model = tf.keras.models.Sequential()
model.add(weight)
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(512, activation='relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(256, activation='relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(12, activation='sigmoid'))

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate = 0.001), loss=tf.keras.losses.BinaryCrossentropy(), metrics= [tfa.metrics.HammingLoss(mode='multilabel', threshold=0.5, name='hamming_loss')])
spe = int(57918 / 8)
spev = int(10000 / 8)
history = model.fit(train_data, epochs=15, steps_per_epoch=spe, validation_steps=spev, validation_data=validation_data)#, callbacks=[tensorboard_callback, save_best, rl, es])

The error stack is:

Epoch 1/15

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

[<ipython-input-21-512ee23ecaf7>](https://localhost:8080/#) in <module>
----> 1 history = model.fit(train_data, epochs=15, steps_per_epoch=spe, validation_steps=spev, validation_data=validation_data)#, callbacks=[tensorboard_callback, save_best, rl, es])

4 frames

[/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py](https://localhost:8080/#) in error_handler(*args, **kwargs)
     65     except Exception as e:  # pylint: disable=broad-except
     66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
     68     finally:
     69       del filtered_tb

[/usr/local/lib/python3.8/dist-packages/keras/engine/training.py](https://localhost:8080/#) in tf__train_function(iterator)
     13                 try:
     14                     do_return = True
---> 15                     retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
     16                 except:
     17                     do_return = False

[/usr/local/lib/python3.8/dist-packages/tensorflow_addons/metrics/utils.py](https://localhost:8080/#) in tf__update_state(self, y_true, y_pred, sample_weight)
     12                 y_true = ag__.converted_call(ag__.ld(tf).cast, (ag__.ld(y_true), ag__.ld(self)._dtype), None, fscope)
     13                 y_pred = ag__.converted_call(ag__.ld(tf).cast, (ag__.ld(y_pred), ag__.ld(self)._dtype), None, fscope)
---> 14                 matches = ag__.converted_call(ag__.ld(self)._fn, (ag__.ld(y_true), ag__.ld(y_pred)), dict(**ag__.ld(self)._fn_kwargs), fscope)
     15                 try:
     16                     do_return = True

[/usr/local/lib/python3.8/dist-packages/tensorflow_addons/metrics/hamming.py](https://localhost:8080/#) in tf__hamming_loss_fn(y_true, y_pred, threshold, mode)
     69                         raise
     70                 nonzero = ag__.Undefined('nonzero')
---> 71                 ag__.if_stmt((ag__.ld(mode) == 'multiclass'), if_body_2, else_body_2, get_state_2, set_state_2, ('do_return', 'retval_'), 2)
     72                 return fscope.ret(retval_, do_return)
     73         return tf__hamming_loss_fn

[/usr/local/lib/python3.8/dist-packages/tensorflow_addons/metrics/hamming.py](https://localhost:8080/#) in else_body_2()
     64                     try:
     65                         do_return = True
---> 66                         retval_ = (ag__.ld(nonzero) / ag__.converted_call(ag__.ld(y_true).get_shape, (), None, fscope)[(- 1)])
     67                     except:
     68                         do_return = False

ValueError: in user code:

    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1051, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.8/dist-packages/tensorflow_addons/metrics/utils.py", line 66, in update_state  *
        matches = self._fn(y_true, y_pred, **self._fn_kwargs)
    File "/usr/local/lib/python3.8/dist-packages/tensorflow_addons/metrics/hamming.py", line 100, in hamming_loss_fn  *
        return nonzero / y_true.get_shape()[-1]

    ValueError: None values not supported.