当您第一次启动Flask并将请求发送到服务器时,一切运行正常(错误代码= 200),但是如果您发送相同的重复请求或其他重复请求,则会出现代码500的错误。加载.h5文件的权重时,它会崩溃。有什么问题吗?
app.py(运行烧瓶)
@app.route('/', methods=['GET'])
def get_tasks():
jsondata = request.get_json()
urls = jsondata["urls"]
distance = DistanceGenerator()
dis = distance.get_distance(urls[0], urls[1])
return jsonify(dis)
if __name__ == '__main__':
app.run(debug=True)
DistanceGenerator类 构造函数:
def __init__(self):
self.model = self.deep_rank_model()
self.model.load_weights("./deepranking.h5")
方法get_distance:
def get_distance(self, im1, im2):
# for layer in model.layers:
# print(layer.name, layer.output_shape)
image1 = Image.open(requests.get(im1, stream=True).raw)
image1 = img_to_array(image1).astype("float64")
image1 = transform.resize(image1, (224, 224))
image1 *= 1. / 255
image1 = np.expand_dims(image1, axis=0)
embedding1 = self.model.predict([image1, image1, image1])[0]
image2 = Image.open(requests.get(im2, stream=True).raw)
image2 = img_to_array(image2).astype("float64")
image2 = transform.resize(image2, (224, 224))
image2 *= 1. / 255
image2 = np.expand_dims(image2, axis=0)
embedding2 = self.model.predict([image2, image2, image2])[0]
distance = sum([(embedding1[idx] - embedding2[idx]) ** 2 for idx in range(len(embedding1))]) ** (0.5)
print(distance)
return distance
跟踪列表,登录
Traceback (most recent call last):
File "C:\Users\gibki\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\client\session.py", line 1092, in _run
subfeed, allow_tensor=True, allow_operation=False)
File "C:\Users\gibki\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\ops.py", line 3478, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "C:\Users\gibki\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\ops.py", line 3557, in _as_graph_element_locked
raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("Placeholder:0", shape=(3, 3, 3, 64), dtype=float32) is not an element of this graph.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\gibki\AppData\Roaming\Python\Python37\site-packages\flask\app.py", line 2328, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\gibki\AppData\Roaming\Python\Python37\site-packages\flask\app.py", line 2314, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\gibki\AppData\Roaming\Python\Python37\site-packages\flask\app.py", line 1760, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\gibki\AppData\Roaming\Python\Python37\site-packages\flask\_compat.py", line 36, in reraise
raise value
File "C:\Users\gibki\AppData\Roaming\Python\Python37\site-packages\flask\app.py", line 2311, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\gibki\AppData\Roaming\Python\Python37\site-packages\flask\app.py", line 1834, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\gibki\AppData\Roaming\Python\Python37\site-packages\flask\app.py", line 1737, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\gibki\AppData\Roaming\Python\Python37\site-packages\flask\_compat.py", line 36, in reraise
raise value
File "C:\Users\gibki\AppData\Roaming\Python\Python37\site-packages\flask\app.py", line 1832, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\gibki\AppData\Roaming\Python\Python37\site-packages\flask\app.py", line 1818, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\gibki\PycharmProjects\image-similarity-deep-ranking1\app.py", line 14, in get_tasks
distance = DistanceGenerator()
File "C:\Users\gibki\PycharmProjects\image-similarity-deep-ranking1\deepranking_get_distance.py", line 19, in __init__
self.model.load_weights("./deepranking.h5")
File "C:\Users\gibki\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\network.py", line 1166, in load_weights
f, self.layers, reshape=reshape)
File "C:\Users\gibki\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\saving.py", line 1058, in load_weights_from_hdf5_group
K.batch_set_value(weight_value_tuples)
File "C:\Users\gibki\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\backend\tensorflow_backend.py", line 2470, in batch_set_value
get_session().run(assign_ops, feed_dict=feed_dict)
File "C:\Users\gibki\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\client\session.py", line 929, in run
run_metadata_ptr)
File "C:\Users\gibki\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\client\session.py", line 1095, in _run
'Cannot interpret feed_dict key as Tensor: ' + e.args[0])
TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("Placeholder:0", shape=(3, 3, 3, 64), dtype=float32) is not an element of this graph.