I'm working on this heart disease detection problem using different classifiers. What I'm doing is i'm saving my model in h5 file & creating an object of it & returning the response in json format.
but the same model won't work on flask api that ran perfectly on my terminal.
here is my Neural network:
def ANN():
global x_train,x_test,y_train,y_test
model = Sequential()
#implicit input layer combined with hidden layer
model.add(Dense(units = 13, kernel_initializer = 'uniform', activation = 'relu', input_dim = 13))
#hidden layer 2
model.add(Dense(units = 13, kernel_initializer = 'uniform', activation = 'relu', input_dim = 13))
#output layer
model.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))
model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
#fitting with optimal hyperparameters
model.fit(x_train, y_train, batch_size = 25, nb_epoch = 287)
return {'model':model,
'accuracy':accuracy_score(model.predict(x_test) > 0.5,y_test)*100}
after saving the model in .h5 file, In my flask api,
ann = load_model('ann8524.h5')
print(ann.predict(x_test)) #test set, for just checking.
here's the error message:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) [2018-12-20 23:37:43,548]
ERROR in app: Exception on /heart/predict
[GET] Traceback (most recent call last):
File "C:\python_installation\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\python_installation\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\python_installation\lib\site-packages\flask_restful\__init__.py", line 458, in wrapper
resp = resource(*args, **kwargs)
File "C:\python_installation\lib\site-packages\flask\views.py", line 88, in view
return self.dispatch_request(*args, **kwargs)
File "C:\python_installation\lib\site-packages\flask_restful\__init__.py", line 573, in dispatch_request
resp = meth(*args, **kwargs)
File "app.py", line 41, in get
print(ann.predict(x_test))
File "C:\python_installation\lib\site-packages\keras\engine\training.py", line 1164, in predict
self._make_predict_function()
File "C:\python_installation\lib\site-packages\keras\engine\training.py", line 554, in _make_predict_function
**kwargs)
File "C:\python_installation\lib\site-packages\keras\backend\tensorflow_backend.py", line 2744, in function
return Function(inputs, outputs, updates=updates, **kwargs)
File "C:\python_installation\lib\site-packages\keras\backend\tensorflow_backend.py", line 2546, in __init__
with tf.control_dependencies(self.outputs):
File "C:\python_installation\lib\site-packages\tensorflow\python\framework\ops.py", line 5004, in control_dependencies
return get_default_graph().control_dependencies(control_inputs)
File "C:\python_installation\lib\site-packages\tensorflow\python\framework\ops.py", line 4543, in control_dependencies
c = self.as_graph_element(c)
File "C:\python_installation\lib\site-packages\tensorflow\python\framework\ops.py", line 3490, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "C:\python_installation\lib\site-packages\tensorflow\python\framework\ops.py", line 3569, in _as_graph_element_locked
raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("dense_3/Sigmoid:0", shape=(?, 1), dtype=float32) is not an element of this graph.
127.0.0.1 - - [20/Dec/2018 23:37:43] "[1m[35mGET /heart/predict HTTP/1.1[0m" 500 -
But it works perfectly fine in Spyder. (exact same code)