Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unknown layer: 'FixedDropout' #150

Closed
gsuni opened this issue Feb 7, 2024 · 16 comments · Fixed by #157
Closed

Unknown layer: 'FixedDropout' #150

gsuni opened this issue Feb 7, 2024 · 16 comments · Fixed by #157
Assignees
Labels
bug Something isn't working enhancement New feature or request

Comments

@gsuni
Copy link

gsuni commented Feb 7, 2024

Error loading the model: Unknown layer: 'FixedDropout'. Please ensure you are using a keras.utils.custom_object_scope and that this object is included in the scope. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.
this is issue i am facing can you provide custom layer.py file where fixeddropout is defined

Copy link

github-actions bot commented Feb 7, 2024

Hello, thank you for your interest in this project😉

@Aydinhamedi
Copy link
Owner

Aydinhamedi commented Feb 9, 2024

I apologize for the delayed reply. This model was built using the EfficientNet library, which is a collection of state-of-the-art models for image classification. To use the model, you need to install the library and import it as follows:

import efficientnet.tfkeras

I hope this helps. Have a nice day! 😊

@Aydinhamedi
Copy link
Owner

And if you want the 'FixedDropout' here is a implementation of it in the original repo with the wrapper

def get_dropout(**kwargs):
    """Wrapper over custom dropout. Fix problem of ``None`` shape for tf.keras.
    It is not possible to define FixedDropout class as global object,
    because we do not have modules for inheritance at first time.

    Issue:
        https://github.com/tensorflow/tensorflow/issues/30946
    """
    backend, layers, models, keras_utils = get_submodules_from_kwargs(kwargs)

    class FixedDropout(layers.Dropout):
        def _get_noise_shape(self, inputs):
            if self.noise_shape is None:
                return self.noise_shape

            symbolic_shape = backend.shape(inputs)
            noise_shape = [symbolic_shape[axis] if shape is None else shape
                           for axis, shape in enumerate(self.noise_shape)]
            return tuple(noise_shape)

    return FixedDropout

@Aydinhamedi
Copy link
Owner

And I added Utils\FixedDropout.py for the layer:

from tensorflow.keras import layers, backend

class FixedDropout(layers.Dropout):
    def _get_noise_shape(self, inputs):
        if self.noise_shape is None:
            return self.noise_shape

        symbolic_shape = backend.shape(inputs)
        noise_shape = [symbolic_shape[axis] if shape is None else shape
                       for axis, shape in enumerate(self.noise_shape)]
        return tuple(noise_shape)

    def get_config(self):
        config = super().get_config()
        return config

    @classmethod
    def from_config(cls, config):
        return cls(**config)

Example:

from Utils.FixedDropout import FixedDropout
from keras.models import load_model

# Load the model
model = load_model('PAI_model_T.h5', custom_objects={'FixedDropout': FixedDropout})

@Aydinhamedi Aydinhamedi pinned this issue Feb 9, 2024
@Aydinhamedi Aydinhamedi self-assigned this Feb 9, 2024
@Aydinhamedi Aydinhamedi added enhancement New feature or request help wanted Extra attention is needed and removed help wanted Extra attention is needed labels Feb 9, 2024
@Aydinhamedi Aydinhamedi linked a pull request Feb 9, 2024 that will close this issue
@Aydinhamedi Aydinhamedi changed the title custom file issue Unknown layer: 'FixedDropout' Feb 9, 2024
@Aydinhamedi Aydinhamedi added the bug Something isn't working label Feb 9, 2024
@Aydinhamedi
Copy link
Owner

Aydinhamedi commented Feb 10, 2024

And the only thing that FixedDropout does
Is fixing a bug in the Dropout layer so you can use the normal Dropout class in newer tf.keras versions.

@gsuni
Copy link
Author

gsuni commented Feb 13, 2024 via email

@Aydinhamedi
Copy link
Owner

PAI_model_T.h5 file is the full model with the structure and layers but PAI_model_weights.h5 file only has weights (the parameters).
The weights only file is smaller but you need the EfficientNet library you can use the full ver PAI_model_T.h5 and just simply load the model:

from Utils.FixedDropout import FixedDropout
from keras.models import load_model

# Load the model
model = load_model('PAI_model_T.h5', custom_objects={'FixedDropout': FixedDropout})

and if you want a smaller model use PAI_model_light_T.h5 the acc is the same but its smaller and lighter.

@gsuni
Copy link
Author

gsuni commented Feb 28, 2024 via email

@gsuni
Copy link
Author

gsuni commented Apr 23, 2024 via email

@Aydinhamedi
Copy link
Owner

Certainly! It seems you’re asking for specific parts of the code related to EfficientNet B7. To clarify, are you looking for the code that involves creating the EfficientNet B7 model, or are you seeking the code for making predictions using a pre-trained EfficientNet B7 model?
And if you want to just try the model download this GUI I made Link

@Aydinhamedi
Copy link
Owner

And if possible create a new issue about your problem.

@gsuni
Copy link
Author

gsuni commented Apr 24, 2024 via email

@gsuni
Copy link
Author

gsuni commented Apr 24, 2024 via email

@Aydinhamedi
Copy link
Owner

If you're interested in the code for the model, I'm using this library: EfficientNet

The code that trains the model is in a Jupyter notebook named Model_T&T.ipynb. There are also backups for the codes of older models.

Please note that there are no separate notebooks for each model. This is because the data processing and training parts are the same for every model. In the notebook, there are multiple cells from which you can choose any model.

You can find the code for creating the EfficientNet B7 model in the same notebook.

@gsuni
Copy link
Author

gsuni commented Apr 26, 2024 via email

@Aydinhamedi
Copy link
Owner

I'm truly sorry but I can`t help you

@Aydinhamedi Aydinhamedi unpinned this issue Apr 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants