pierluigiferrari / ssd_keras

A Keras port of Single Shot MultiBox Detector
Apache License 2.0
1.86k stars 938 forks source link

TypeError: a bytes-like object is required, not 'str' #365

Closed sachinkmohan closed 4 years ago

sachinkmohan commented 4 years ago

TypeError: a bytes-like object is required, not 'str'

The code is working fine on conda environments, but when I switch to pip installation. I get the following error while saving .h5 file after ever epoch


Epoch 1/10
100/100 [==============================] - 54s 537ms/step - loss: 3.7701 - val_loss: 3.6612

Epoch 00001: val_loss improved from 3.89106 to 3.66121, saving model to ssd7_epoch-01_loss-3.7701_val_loss-3.6612.h5
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-247572e1c2da> in <module>
     11                               validation_data=val_generator,
     12                               validation_steps=ceil(val_dataset_size/batch_size),
---> 13                               initial_epoch=initial_epoch)

~/mlp/git/mltf114_pip1/lib/python3.6/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
     89                 warnings.warn('Update your `' + object_name + '` call to the ' +
     90                               'Keras 2 API: ' + signature, stacklevel=2)
---> 91             return func(*args, **kwargs)
     92         wrapper._original_function = func
     93         return wrapper

~/mlp/git/mltf114_pip1/lib/python3.6/site-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
   1656             use_multiprocessing=use_multiprocessing,
   1657             shuffle=shuffle,
-> 1658             initial_epoch=initial_epoch)
   1659 
   1660     @interfaces.legacy_generator_methods_support

~/mlp/git/mltf114_pip1/lib/python3.6/site-packages/keras/engine/training_generator.py in fit_generator(model, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
    253                     break
    254 
--> 255             callbacks.on_epoch_end(epoch, epoch_logs)
    256             epoch += 1
    257             if callbacks.model.stop_training:

~/mlp/git/mltf114_pip1/lib/python3.6/site-packages/keras/callbacks.py in on_epoch_end(self, epoch, logs)
    150         logs = logs or {}
    151         for callback in self.callbacks:
--> 152             callback.on_epoch_end(epoch, logs)
    153 
    154     def on_train_batch_begin(self, batch, logs=None):

~/mlp/git/mltf114_pip1/lib/python3.6/site-packages/keras/callbacks.py in on_epoch_end(self, epoch, logs)
    717                             self.model.save_weights(filepath, overwrite=True)
    718                         else:
--> 719                             self.model.save(filepath, overwrite=True)
    720                     else:
    721                         if self.verbose > 0:

~/mlp/git/mltf114_pip1/lib/python3.6/site-packages/keras/engine/network.py in save(self, filepath, overwrite, include_optimizer)
   1137             raise NotImplementedError
   1138         from ..models import save_model
-> 1139         save_model(self, filepath, overwrite, include_optimizer)
   1140 
   1141     @saving.allow_write_to_gcs

~/mlp/git/mltf114_pip1/lib/python3.6/site-packages/keras/engine/saving.py in save_wrapper(obj, filepath, overwrite, *args, **kwargs)
    413                 os.remove(tmp_filepath)
    414         else:
--> 415             save_function(obj, filepath, overwrite, *args, **kwargs)
    416 
    417     return save_wrapper

~/mlp/git/mltf114_pip1/lib/python3.6/site-packages/keras/engine/saving.py in save_model(model, filepath, overwrite, include_optimizer)
    505                 return
    506         with H5Dict(filepath, mode='w') as h5dict:
--> 507             _serialize_model(model, h5dict, include_optimizer)
    508     elif hasattr(filepath, 'write') and callable(filepath.write):
    509         # write as binary stream

~/mlp/git/mltf114_pip1/lib/python3.6/site-packages/keras/engine/saving.py in _serialize_model(model, h5dict, include_optimizer)
    129         layer_group['weight_names'] = weight_names
    130         for name, val in zip(weight_names, weight_values):
--> 131             layer_group[name] = val
    132     if include_optimizer and model.optimizer:
    133         if isinstance(model.optimizer, optimizers.TFOptimizer):

~/mlp/git/mltf114_pip1/lib/python3.6/site-packages/keras/utils/io_utils.py in __setitem__(self, attr, val)
    231                            'Group with name "{}" exists.'.format(attr))
    232         if is_np:
--> 233             dataset = self.data.create_dataset(attr, val.shape, dtype=val.dtype)
    234             if not val.shape:
    235                 # scalar

~/mlp/git/mltf114_pip1/lib/python3.6/site-packages/h5py/_hl/group.py in create_dataset(self, name, shape, dtype, data, **kwds)
    141             group = self
    142             if name:
--> 143                 if '/' in name:
    144                     h5objects = [obj for obj in name.split('/') if len(obj)]
    145                     name = h5objects[-1]

TypeError: a bytes-like object is required, not 'str'

OS : Linux TF version : 1.14.0 Keras: 2.2.5

This error happens because in python3, data are formatted as bytes and not strings. Any idea on how to solve this one.

sachinkmohan commented 4 years ago

Thought on the internet various searches were regarding python 2.7 vs python 3+ versions, the issue got resolved when I downgraded my h5py version from 3.0.0 to 2.10.0 in pip using the below command.

pip install 'h5py<3.0.0'