MLH-Fellowship / neuro-art

Cool art from your photos, powered by Neural Style Transfer and deep learning.
MIT License
10 stars 3 forks source link

BentoML/Keras `AttributeError: 'Functional' object has no attribute '_make_predict_function'` #19

Open ShayanRiyaz opened 3 years ago

ShayanRiyaz commented 3 years ago

When I try to run this code:

import tensorflow as tf
from bentoml import BentoService, api, artifacts
from bentoml.adapters import ImageInput

from bentoml.frameworks.keras import KerasModelArtifact
from typing import List

import numpy as np

def vgg_layers(layer_names):
    vgg = tf.keras.applications.VGG19(include_top = False,weights = 'imagenet')
    vgg.trainable = False
    tf_outs = [vgg.get_layer(layer).output for layer in layer_names]

    model = tf.keras.Model([vgg.input],tf_outs)
    return model

# Most Commonly used layers for Neural Style Transfer
content_layers = ['block5_conv2']
style_layers = ['block1_conv1','block2_conv1','block3_conv1','block4_conv1','block5_conv1']
vgg = vgg_layers(content_layers+style_layers)

@bentoml.env(infer_pip_packages=True)
@bentoml.artifacts([KerasModelArtifact('vgg')])
class MyService(bentoml.BentoService):
    @bentoml.api(input=ImageInput(), batch=False)
        train_step(image)

        return img

svc = KerasModelService()
svc.pack('vgg', vgg)

I'm getting the error:

WARNING:tensorflow:From /home/shayanriyaz/anaconda3/envs/bentodeploy/lib/python3.8/site-packages/bentoml/frameworks/keras.py:123: The name tf.keras.backend.get_session is deprecated. Please use tf.compat.v1.keras.backend.get_session instead.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-36-d563d8785485> in <module>
----> 1 svc.pack('vgg', vgg)

~/anaconda3/envs/bentodeploy/lib/python3.8/site-packages/bentoml/service/__init__.py in pack(self, name, *args, **kwargs)
    687         :return: this BentoService instance
    688         """
--> 689         self.artifacts.get(name).pack(*args, **kwargs)
    690         return self
    691 

~/anaconda3/envs/bentodeploy/lib/python3.8/site-packages/bentoml/service/artifacts/__init__.py in wrapped_pack(*args, **kwargs)
    106                         "behaviors"
    107                     )
--> 108                 ret = original(*args, **kwargs)
    109                 # do not set `self._pack` if `pack` has failed with an exception raised
    110                 self._packed = True

~/anaconda3/envs/bentodeploy/lib/python3.8/site-packages/bentoml/frameworks/keras.py in pack(self, data)
    174 
    175         self.bind_keras_backend_session()
--> 176         model._make_predict_function()
    177 
    178         self._model = model

AttributeError: 'Functional' object has no attribute '_make_predict_function'

I haven't found useful documentation in the BentoML docs or the Kears docs. I think the issue has to do with the Keras backend and the fact that _make_predict_function is a part of the v1 library

yy823 commented 3 years ago

Changing the "_make_predict_function()" to "make_predict_function()" may help.

erdivyang10 commented 2 years ago

Yes @yy823 I was also facing the same issue and your solution works like a charm Changing the "_make_predict_function()" to "make_predict_function()" working perfectly.