jatinchowdhury18 / RTNeural

Real-time neural network inferencing
BSD 3-Clause "New" or "Revised" License
543 stars 57 forks source link

Problem saving Tensorflow models #133

Closed dsagman closed 3 months ago

dsagman commented 3 months ago

First, thanks for making this awesome library! We are using it as part of a project where we are seeing if we can increase performance on larger network layers using approximation techniques such as approximate activation, approximate matrix multiply, and loop perforation. If we get that working, we will share our results with you.

However, I ran into an issue where save_layer in model_utils.py is giving me an error that:

AttributeError: 'Dense' object has no attribute 'output_shape'

I found that layer.output.shape works, but isn't always a tuple, which runs afoul of the JSON encoder. So I added check for that too.

I modified the code as follows and it seems to work on Macos, Linux, Windows. I've included my debugging print statements so you can see what I was looking at.

def save_layer(layer):
    # print("Debugging output shape")
    # try:
    #     print("layer.output.shape: ", layer.output.shape, type(layer.output.shape))
    # except:
    #     print("layer.output.shape: ", "None")
    # try:
    #     print("layer.output_shape: ", layer.output_shape, type(layer.output_shape))
    # except:
    #     print("layer.output_shape: ", "None")
    try:
        outshape = layer.output_shape # this is the original code, but sometimes doesn't work
    except:
        outshape = layer.output.shape # if not, use this and make sure it's a tuple so it can be JSON encoded
        if type(outshape) != tuple:  
            outshape = tuple(outshape.as_list()) 
   .... rest of code is unchanged...

I suspect something changed in the keras internals in one of the versions, but can't find any reference to it.

Regardless, I hope this helps and is useful.

jatinchowdhury18 commented 3 months ago

Thanks for the report! I think you're correct that the issue was caused by some "internal" change in Keras or TensorFlow... I'd be curious which versions of those libraries you're using when seeing this issue?

The fix that you've suggested looks good to me. If you wouldn't mind creating a PR with this change that would be great! I can do a little more testing merge the change this weekend.

dsagman commented 3 months ago

Great! I've submitted the pull request. Hopefully correctly. (This is my first PR.)

I am using Tensorflow 2.16.1, keras 3.1.1, and Python 3.11.6 on my Windows 11 laptop where I'm submitting from. I also have roughly the same current versions of each running on: Windows 11 desktop with NVIDIA, Mac M1 Mini, and Framework Intel 12th Gen Laptop with Manjaro (Arch variant). I've been doing development with RTNeural on all of these.

(Why not just use the desktop with the NVIDIA card? Because it's my son's so he only let's me use it when I buy him pizza and am able to distract him long enough to try to train a model.)