Dobiasd / frugally-deep

A lightweight header-only library for using Keras (TensorFlow) models in C++.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is there a support for Rescaling layer?

TrueWodzu opened this issue Β· comments

Hi Tobias,

just as today, I've converted EfficientNetB3 model from Keras Applications. This model contains Rescaling layer as a way to preprocess data. Conversion went fine and I am just wondering, did you add support for Rescaling layer? Last time when I did some conversion, I had to simulate Rescaling, but now, the conversion script has not returned me any errors. So am I all good? (Rescaling layer is not mentioned on the readme page as being supported).

Hi.

I did not implement support for the Rescaling layer in frugally-deep. But since it's no longer experimental in Keras, I will think about adding it. πŸ™‚ Thanks for reminding me. πŸ‘

The reason why your conversion succeeded is, that EfficientNetB3 in Keras (2.8.0) Applications does not use a Rescaling layer. βœ…

Hi,

Thanks for answering, but I don't understand. You've pointed me to the source and even to the exact line:

x = layers.Rescaling(1. / 255.)(x)
which basically adds Rescaling layer to the model?:confused:

And when I print model summary, I will get:

Model: "efficientnetb3"
__________________________________________________________________________________________________
 Layer (type)                   Output Shape         Param #     Connected to
==================================================================================================
 input_1 (InputLayer)           [(None, 300, 300, 3  0           [])]
 rescaling (Rescaling)          (None, 300, 300, 3)  0           ['input_1[0][0]']
 normalization (Normalization)  (None, 300, 300, 3)  7           ['rescaling[0][0]']

Could you please explain? πŸ™

Ah, sorry. My mistake. πŸ₯΄

I thought, only the "normal" EfficientNet would use Rescaling, and EfficientNetB3 would not. But it turns out EfficientNetB3 is an EfficientNet instance, so (as your model summary confirms), it is using Rescaling too. I'll debug what's going on and get back to you.

Interesting! πŸ•΅οΈ

When trying to load tf.keras.applications.efficientnet.EfficientNetB3 using fdeep::load_model, it errors with unknown layer type: Rescaling as expected. So I can't reproduce the effect you observe.

Dockerfile:

FROM python:3.9

RUN apt-get update
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get install -y build-essential cmake

RUN pip3 install tensorflow==2.8.0

RUN apt-get remove --purge -y cmake
RUN pip install cmake --upgrade

RUN git clone -b 'v0.2.18-p0' --single-branch --depth 1 https://github.com/Dobiasd/FunctionalPlus && cd FunctionalPlus && mkdir -p build && cd build && cmake .. && make && make install
RUN git clone -b '3.4.0' --single-branch --depth 1 https://gitlab.com/libeigen/eigen.git && cd eigen && mkdir -p build && cd build && cmake .. && make && make install && ln -s /usr/local/include/eigen3/Eigen /usr/local/include/Eigen
RUN git clone -b 'v3.10.5' --single-branch --depth 1 https://github.com/nlohmann/json && cd json && mkdir -p build && cd build && cmake -DJSON_BuildTests=OFF .. && make && make install
RUN git clone -b 'v0.15.16-p0' --single-branch --depth 1 https://github.com/Dobiasd/frugally-deep && cd frugally-deep && mkdir -p build && cd build && cmake .. && make && make install

WORKDIR /frugally-deep

RUN python3 -c "import tensorflow as tf; tf.keras.applications.efficientnet.EfficientNetB3().save('efficientnetb3.h5')"

RUN CUDA_VISIBLE_DEVICES='' taskset --cpu-list 1 python3 ./keras_export/convert_model.py efficientnetb3.h5 efficientnetb3.json

RUN echo '#include "fdeep/fdeep.hpp"\n\
int main()\n\
{\n\
    const auto model = fdeep::load_model("efficientnetb3.json");\n\
}' >> main.cpp

RUN g++ -std=c++14 -O3 -march=native main.cpp -o main

RUN ./main

Output:

Loading json ... done. elapsed time: 0.929186 s
Building model ... terminate called after throwing an instance of 'std::runtime_error'
  what():  unknown layer type: Rescaling
Aborted (core dumped)

I'll add support for the Rescaling layer. πŸ™‚

I confirm the exception. I thought that if something is not supported it will be reported during the conversion of the model with Python script.

How long do you think it will take you to add this layer? Is it a matter of weeks or longer?:)

The Rescaling layer looks very simple. So the actual effort should be less than one hour. If nothing unexpected happens, I'll have it done on the weekend (perhaps earlier). πŸ™‚

An outage of jcenter.bintray.com is kinda blocking me at work, so I found some time for Rescaling already. 😁 Once the CI of the PR is green, I'll merge it into master and tag a new version.

Done. βœ”οΈ (see version 0.15.17 πŸ™‚)

Splendid! πŸ‘ Thank you. πŸ™ πŸ˜„