Reputation: 346
So i try for making web app on flask, when I try to run my prediction it seems show some error like this:
return self.view_functions[rule.endpoint](**req.view_args)
File "E:\revisi 1.2\web skripsi\app.py", line 114, in upload
pre_path = model_predict(secure_filename(paths), model)
File "E:\revisi 1.2\web skripsi\app.py", line 63, in model_predict
conts, preds, masks = fungsi_vis(model, session, img, _gt=None)
File "E:\revisi 1.2\web skripsi\segmentation_function.py", line 14, in fungsi_vis
pr_mask = model.predict(image).round()
File "C:\Users\DIS\AppData\Local\conda\conda\envs\myenv\Lib\site-packages\tensorflow\python\keras\engine\training.py", line 130, in _method_wrapper
return method(self, *args, **kwargs)
File "C:\Users\DIS\AppData\Local\conda\conda\envs\myenv\Lib\site-packages\tensorflow\python\keras\engine\training.py", line 1562, in predict
version_utils.disallow_legacy_graph('Model', 'predict')
File "C:\Users\DIS\AppData\Local\conda\conda\envs\myenv\Lib\site-packages\tensorflow\python\keras\utils\version_utils.py", line 122, in disallow_legacy_graph
raise ValueError(error_msg)
ValueError: Calling `Model.predict` in graph mode is not supported when the `Model` instance was constructed with eager mode enabled. Please construct your `Model` instance in graph mode or call `Model.predict` with eager mode enabled.
I'm trying some trick by using
model.compile()
model.run_eagerly = True
but it seems can't work, so I'm wondering where I take it wrong on my code, and here's my code:
image = np.expand_dims(_image, axis=0)
# with graph.as_default():
with session.as_default():
with session.graph.as_default():
pr_mask = model.predict(image).round()
# b,g,r = cv2.split(_image)
r,g,b = cv2.split(_image)
rgb_img = cv2.merge([r,g,b])
print('success predict')
I'm aware for my case I'm trying to solve it by myself and looking from here, and here but it seems those 2 can't solve my answer, or I take it wrong.
Upvotes: 1
Views: 2666
Reputation:
Tensorflow 2.x works in eager mode where as Tensorflow 1.x it works on graph mode.
So in TF 1.x, session is required for running tensorflow operations.
From comments by @uzrsa
I've solved this case, cause I use session it seems the program can't work so I delete all the session code lines, and it works so I solved it.
Here you are using model.run_eagerly = True
, that means it evaluates operations immediately, without building graphs. Hence after deleting session code it works.
image = np.expand_dims(_image, axis=0)
r,g,b = cv2.split(_image)
rgb_img = cv2.merge([r,g,b])
print('success predict')
Upvotes: 2