Mask_RCNN
Mask_RCNN copied to clipboard
model.detect not working in Flask on server (without GPU)
I have following code in my app.py
@app.route('/upload', methods=['POST'])
def upload_file():
#
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
extension = os.path.splitext(filename)[1]
filename = str(uuid.uuid4()) + extension
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
start = time.time()
print("Process image ", filepath)
image = cv2.imread(filepath)
image = image_resize(image, 640)
results = model.detect([ image ], verbose=1)
r = results[0]
output_file = open(os.path.join(app.config['UPLOAD_FOLDER'], filename + '.' + str(r['rois'].shape[0])), 'w')
output_file.close()
out = blur_instances(image, r['rois'], r['masks'], r['class_ids'], dataset.class_names, r['scores'])
cv2.imwrite(filepath, out)
end = time.time()
return {
'image': url_for('uploaded_file', filename=filename),
'time': '{:.6f} seconds'.format(end - start),
'cnt': r['rois'].shape[0]
}
This code works great on my local machine with GTX 1060 6GB, but dont work on server with following error:
Processing 1 images
image shape: (171, 640, 3) min: 4.00000 max: 231.00000 uint8
molded_images shape: (1, 1024, 1024, 3) min: -123.70000 max: 119.10000 float64
image_metas shape: (1, 14) min: 0.00000 max: 1024.00000 float64
anchors shape: (1, 261888, 4) min: -0.35390 max: 1.29134 float32
[2019-12-01 11:06:01,298] ERROR in app: Exception on /upload [POST]
Traceback (most recent call last):
File "/home/striker/.local/lib/python3.5/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/home/striker/.local/lib/python3.5/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/striker/.local/lib/python3.5/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/striker/.local/lib/python3.5/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/striker/.local/lib/python3.5/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/home/striker/.local/lib/python3.5/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "app.py", line 178, in upload_file
results = model.detect([ image ], verbose=1)
File "/home/striker/Mask_RCNN/mrcnn/model.py", line 2524, in detect
self.keras_model.predict([molded_images, image_metas, anchors], verbose=0)
File "/home/striker/.local/lib/python3.5/site-packages/keras/engine/training.py", line 1462, in predict
callbacks=callbacks)
File "/home/striker/.local/lib/python3.5/site-packages/keras/engine/training_arrays.py", line 324, in predict_loop
batch_outs = f(ins_batch)
File "/home/striker/.local/lib/python3.5/site-packages/tensorflow/python/keras/backend.py", line 3289, in __call__
self._make_callable(feed_arrays, feed_symbols, symbol_vals, session)
File "/home/striker/.local/lib/python3.5/site-packages/tensorflow/python/keras/backend.py", line 3222, in _make_callable
callable_fn = session._make_callable_from_options(callable_opts)
File "/home/striker/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1489, in _make_callable_from_options
return BaseSession._Callable(self, callable_options)
File "/home/striker/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1446, in __init__
session._session, options_ptr)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Tensor input_image:0, specified in either feed_devices or fetch_devices was not found in the Graph
185.8.126.48 - - [01/Dec/2019 11:06:01] "POST /upload HTTP/1.1" 500 -
Running model.detect just in any python script (without flask) working great. What should i do to run model detect in flask environment?
Hmm not sure on this, we're using tensorflow serving successfully.
@marcfielding1 how did you convert the model for tf serving?
It depends on which model you have, you could just load the model and try(with the right inputs and outputs):
with tf.keras.backend.get_session() as sess:
tf.saved_model.simple_save(
sess,
export_path,
inputs={'input_image': model.input},
outputs={t.name: t for t in model.outputs})
If that doesn't work you can:
https://github.com/bendangnuksung/mrcnn_serving_ready - if you get an error when it tries to convert it about shape, change line 108 of main.py to
model.load_weights(H5_WEIGHT_PATH, by_name=True, exclude=[ "mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"])
simple_save not working,
File ".\export.py", line 50, in <module>
inputs={'input_image': model.input},
AttributeError: 'MaskRCNN' object has no attribute 'input'
I alsow try with keras_model
with tf.keras.backend.get_session() as sess:
tf.saved_model.simple_save(
sess,
'model',
inputs={'input_image': model.keras_model.input},
outputs={t.name: t for t in model.keras_model.outputs})
and it gives me error:
WARNING:tensorflow:From .\export.py:51: simple_save (from tensorflow.python.saved_model.simple_save) is deprecated and w
ill be removed in a future version.
Instructions for updating:
This function will only be available through the v1 compatibility library as tf.compat.v1.saved_model.simple_save.
WARNING:tensorflow:From D:\Development\python\3.7.5\lib\site-packages\tensorflow\python\saved_model\signature_def_utils_
impl.py:201: build_tensor_info (from tensorflow.python.saved_model.utils_impl) is deprecated and will be removed in a fu
ture version.
Instructions for updating:
This function will only be available through the v1 compatibility library as tf.compat.v1.saved_model.utils.build_tensor
_info or tf.compat.v1.saved_model.build_tensor_info.
Traceback (most recent call last):
File ".\export.py", line 51, in <module>
outputs={t.name: t for t in model.keras_model.outputs})
File "D:\Development\python\3.7.5\lib\site-packages\tensorflow\python\util\deprecation.py", line 324, in new_func
return func(*args, **kwargs)
File "D:\Development\python\3.7.5\lib\site-packages\tensorflow\python\saved_model\simple_save.py", line 81, in simple_
save
signature_def_utils.predict_signature_def(inputs, outputs)
File "D:\Development\python\3.7.5\lib\site-packages\tensorflow\python\saved_model\signature_def_utils_impl.py", line 2
01, in predict_signature_def
for key, tensor in inputs.items()}
File "D:\Development\python\3.7.5\lib\site-packages\tensorflow\python\saved_model\signature_def_utils_impl.py", line 2
01, in <dictcomp>
for key, tensor in inputs.items()}
File "D:\Development\python\3.7.5\lib\site-packages\tensorflow\python\util\deprecation.py", line 324, in new_func
return func(*args, **kwargs)
File "D:\Development\python\3.7.5\lib\site-packages\tensorflow\python\saved_model\utils_impl.py", line 63, in build_te
nsor_info
return build_tensor_info_internal(tensor)
File "D:\Development\python\3.7.5\lib\site-packages\tensorflow\python\saved_model\utils_impl.py", line 69, in build_te
nsor_info_internal
dtype=dtypes.as_dtype(tensor.dtype).as_datatype_enum,
AttributeError: 'list' object has no attribute 'dtype'
Are you trying to deploy the model in a website? I want help in doing the same. Can somebody guide me through how its to be done?
I have following code in my app.py
@app.route('/upload', methods=['POST']) def upload_file(): # if file and allowed_file(file.filename): filename = secure_filename(file.filename) extension = os.path.splitext(filename)[1] filename = str(uuid.uuid4()) + extension filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(filepath) start = time.time() print("Process image ", filepath) image = cv2.imread(filepath) image = image_resize(image, 640) results = model.detect([ image ], verbose=1) r = results[0] output_file = open(os.path.join(app.config['UPLOAD_FOLDER'], filename + '.' + str(r['rois'].shape[0])), 'w') output_file.close() out = blur_instances(image, r['rois'], r['masks'], r['class_ids'], dataset.class_names, r['scores']) cv2.imwrite(filepath, out) end = time.time() return { 'image': url_for('uploaded_file', filename=filename), 'time': '{:.6f} seconds'.format(end - start), 'cnt': r['rois'].shape[0] }This code works great on my local machine with GTX 1060 6GB, but dont work on server with following error:
Processing 1 images image shape: (171, 640, 3) min: 4.00000 max: 231.00000 uint8 molded_images shape: (1, 1024, 1024, 3) min: -123.70000 max: 119.10000 float64 image_metas shape: (1, 14) min: 0.00000 max: 1024.00000 float64 anchors shape: (1, 261888, 4) min: -0.35390 max: 1.29134 float32 [2019-12-01 11:06:01,298] ERROR in app: Exception on /upload [POST] Traceback (most recent call last): File "/home/striker/.local/lib/python3.5/site-packages/flask/app.py", line 2446, in wsgi_app response = self.full_dispatch_request() File "/home/striker/.local/lib/python3.5/site-packages/flask/app.py", line 1951, in full_dispatch_request rv = self.handle_user_exception(e) File "/home/striker/.local/lib/python3.5/site-packages/flask/app.py", line 1820, in handle_user_exception reraise(exc_type, exc_value, tb) File "/home/striker/.local/lib/python3.5/site-packages/flask/_compat.py", line 39, in reraise raise value File "/home/striker/.local/lib/python3.5/site-packages/flask/app.py", line 1949, in full_dispatch_request rv = self.dispatch_request() File "/home/striker/.local/lib/python3.5/site-packages/flask/app.py", line 1935, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "app.py", line 178, in upload_file results = model.detect([ image ], verbose=1) File "/home/striker/Mask_RCNN/mrcnn/model.py", line 2524, in detect self.keras_model.predict([molded_images, image_metas, anchors], verbose=0) File "/home/striker/.local/lib/python3.5/site-packages/keras/engine/training.py", line 1462, in predict callbacks=callbacks) File "/home/striker/.local/lib/python3.5/site-packages/keras/engine/training_arrays.py", line 324, in predict_loop batch_outs = f(ins_batch) File "/home/striker/.local/lib/python3.5/site-packages/tensorflow/python/keras/backend.py", line 3289, in __call__ self._make_callable(feed_arrays, feed_symbols, symbol_vals, session) File "/home/striker/.local/lib/python3.5/site-packages/tensorflow/python/keras/backend.py", line 3222, in _make_callable callable_fn = session._make_callable_from_options(callable_opts) File "/home/striker/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1489, in _make_callable_from_options return BaseSession._Callable(self, callable_options) File "/home/striker/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1446, in __init__ session._session, options_ptr) tensorflow.python.framework.errors_impl.InvalidArgumentError: Tensor input_image:0, specified in either feed_devices or fetch_devices was not found in the Graph 185.8.126.48 - - [01/Dec/2019 11:06:01] "POST /upload HTTP/1.1" 500 -Running model.detect just in any python script (without flask) working great. What should i do to run model detect in flask environment?
Have you managed to solve it?
Have you managed to solve it?
still no luck. do you have same issue?
@wiistriker @GoldenWings i will push the working one this tonight on some repo, i will update you
@wiistriker @GoldenWings i will push the working one this tonight on some repo, i will update you
any news?
@wiistriker Have you found any solution to that yet?
any news?