嘿,我已经建立了一个Tensorflow / keras模型,我想使用AzureML Service部署它并在某些体系结构中使用它。这是我从https://docs.microsoft.com/bs-latn-ba/azure/machine-learning/service/how-to-deploy-existing-model那里获得教程的地方,但似乎我所做的Docker容器都需要进行长时间的运行状况检查并最终失败。
代码
import azureml.core
from azureml.core.model import Model
from azureml.core import Workspace
from azureml.core import Experiment
from azureml.core.webservice import Webservice
from azureml.core.webservice import AciWebservice
from azureml.core.model import InferenceConfig
from azureml.core import Environment
from azureml.core.conda_dependencies import CondaDependencies
from azureml.core.image import ContainerImage
ws = Workspace.from_config(path="config.json")
# register the model
from azureml.core.model import Model
model = Model.register(workspace = ws,
model_path ="./models/kerastfmodel.h5",
model_name = "cifar10final",
tags = {"cifar": "demo"},
description = "image classification cifar 10",)
from azureml.core.model import InferenceConfig
from azureml.core import Environment
from azureml.core.environment import CondaDependencies
myenv = Environment(name="myenv")
conda_dep = CondaDependencies(conda_dependencies_file_path="conda_dependencies.yml",_underlying_structure=None)
myenv.python.conda_dependencies=conda_dep
inference_config = InferenceConfig(entry_script="score.py",
environment=myenv)
from azureml.core.webservice import LocalWebservice
deployment_config = LocalWebservice.deploy_configuration()
from azureml.core.model import Model
model = Model(ws, name='cifar10final')
service = Model.deploy(ws, 'keras-model-deployment', [model], inference_config, deployment_config)
service.wait_for_deployment(True)
print(service.state)
这是错误,请让我知道为什么检查容器运行状况需要很长时间。
Downloading model cifar10final:3 to C:\Users\ABULEL~1\AppData\Local\Temp\azureml_d_fwvcz7\cifar10final\3
Generating Docker build context.
Package creation Succeeded
Logging into Docker registry kerasmodeldeffa73d43.azurecr.io
Logging into Docker registry kerasmodeldeffa73d43.azurecr.io
Building Docker image from Dockerfile...
Step 1/5 : FROM kerasmodeldeffa73d43.azurecr.io/azureml/azureml_9fb3b9e6e894adc2f743210abdfc65f2
---> a0ca47155eec
Step 2/5 : COPY azureml-app /var/azureml-app
---> 128db996e654
Step 3/5 : COPY model_config_map.json /var/azureml-app/model_config_map.json
---> bfef0e504378
Step 4/5 : RUN mv '/var/azureml-app/tmp0iexkyel.py' /var/azureml-app/main.py
---> Running in b37f9a31d373
---> d2b56987e09d
Step 5/5 : CMD ["runsvdir","/var/runit"]
---> Running in f0ad50cfa171
---> 93ecc27f6bb0
Successfully built 93ecc27f6bb0
Successfully tagged keras-model-deployment:latest
Container (name:epic_engelbart, id:0161f2701a4f464329ded605040036e3235f6b58fb87953a468f64443b19c89c) cannot be killed.
Container has been successfully cleaned up.
Image sha256:60d5518b12b286b14a3a66ed577b3315625bc19ccfb2dea68d4e2df83d26a972 successfully removed.
Starting Docker container...
Docker container running.
Checking container health...
ERROR - Error: Connection to container has failed. Did your init method fail?
Container Logs:
2019-11-26T20:42:23,110654800+00:00 - rsyslog/run
2019-11-26T20:42:23,110309600+00:00 - gunicorn/run
2019-11-26T20:42:23,112216900+00:00 - iot-server/run
2019-11-26T20:42:23,116419800+00:00 - nginx/run
/usr/sbin/nginx: /azureml-envs/azureml_42871c4a067c137d25936bcfe6c4b51e/lib/libcrypto.so.1.0.0: no version information available (required by /usr/sbin/nginx)
/usr/sbin/nginx: /azureml-envs/azureml_42871c4a067c137d25936bcfe6c4b51e/lib/libcrypto.so.1.0.0: no version information available (required by /usr/sbin/nginx)
/usr/sbin/nginx: /azureml-envs/azureml_42871c4a067c137d25936bcfe6c4b51e/lib/libssl.so.1.0.0: no version information available (required by /usr/sbin/nginx)
/usr/sbin/nginx: /azureml-envs/azureml_42871c4a067c137d25936bcfe6c4b51e/lib/libssl.so.1.0.0: no version information available (required by /usr/sbin/nginx)
/usr/sbin/nginx: /azureml-envs/azureml_42871c4a067c137d25936bcfe6c4b51e/lib/libssl.so.1.0.0: no version information available (required by /usr/sbin/nginx)
EdgeHubConnectionString and IOTEDGE_IOTHUBHOSTNAME are not set. Exiting...
2019-11-26T20:42:23,266440400+00:00 - iot-server/finish 1 0
2019-11-26T20:42:23,271149700+00:00 - Exit code 1 is normal. Not restarting iot-server.
Starting gunicorn 19.9.0
Listening at: http://127.0.0.1:31311 (10)
Using worker: sync
worker timeout is set to 300
Booting worker with pid: 39
Initialized PySpark session.
Initializing logger
Starting up app insights client
Starting up request id generator
Starting up app insight hooks
Invoking user's init function
Executing init() method...
Python version: 3.6.6 | packaged by conda-forge | (default, Oct 12 2018, 14:43:46)
[GCC 7.3.0], keras version: 2.3.1
User's init function failed
Encountered Exception Traceback (most recent call last):
File "/var/azureml-server/aml_blueprint.py", line 162, in register
main.init()
File "/var/azureml-app/main.py", line 20, in init
model = joblib.load(model_path)
File "/azureml-envs/azureml_42871c4a067c137d25936bcfe6c4b51e/lib/python3.6/site-packages/joblib/numpy_pickle.py", line 605, in load
obj = _unpickle(fobj, filename, mmap_mode)
File "/azureml-envs/azureml_42871c4a067c137d25936bcfe6c4b51e/lib/python3.6/site-packages/joblib/numpy_pickle.py", line 529, in _unpickle
obj = unpickler.load()
File "/azureml-envs/azureml_42871c4a067c137d25936bcfe6c4b51e/lib/python3.6/pickle.py", line 1050, in load
dispatch[key[0]](self)
KeyError: 72
Using TensorFlow backend.
/azureml-envs/azureml_42871c4a067c137d25936bcfe6c4b51e/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint8 = np.dtype([("qint8", np.int8, 1)])
/azureml-envs/azureml_42871c4a067c137d25936bcfe6c4b51e/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/azureml-envs/azureml_42871c4a067c137d25936bcfe6c4b51e/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint16 = np.dtype([("qint16", np.int16, 1)])
/azureml-envs/azureml_42871c4a067c137d25936bcfe6c4b51e/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint16 = np.dtype([("quint16", np.uint16, 1)])
/azureml-envs/azureml_42871c4a067c137d25936bcfe6c4b51e/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint32 = np.dtype([("qint32", np.int32, 1)])
/azureml-envs/azureml_42871c4a067c137d25936bcfe6c4b51e/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
np_resource = np.dtype([("resource", np.ubyte, 1)])
/azureml-envs/azureml_42871c4a067c137d25936bcfe6c4b51e/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint8 = np.dtype([("qint8", np.int8, 1)])
/azureml-envs/azureml_42871c4a067c137d25936bcfe6c4b51e/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/azureml-envs/azureml_42871c4a067c137d25936bcfe6c4b51e/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint16 = np.dtype([("qint16", np.int16, 1)])
/azureml-envs/azureml_42871c4a067c137d25936bcfe6c4b51e/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:544: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_quint16 = np.dtype([("quint16", np.uint16, 1)])
/azureml-envs/azureml_42871c4a067c137d25936bcfe6c4b51e/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:545: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
_np_qint32 = np.dtype([("qint32", np.int32, 1)])
/azureml-envs/azureml_42871c4a067c137d25936bcfe6c4b51e/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
np_resource = np.dtype([("resource", np.ubyte, 1)])
/azureml-envs/azureml_42871c4a067c137d25936bcfe6c4b51e/lib/python3.6/site-packages/sklearn/externals/joblib/__init__.py:15: DeprecationWarning: sklearn.externals.joblib is deprecated in 0.21 and will be removed in 0.23. Please import this functionality directly from joblib, which can be installed with: pip install joblib. If this warning is raised when loading pickled models, you may need to re-serialize those models with scikit-learn 0.21+.
warnings.warn(msg, category=DeprecationWarning)
Worker exiting (pid: 39)
Shutting down: Master
Reason: Worker failed to boot.
2019-11-26T20:42:25,010111700+00:00 - gunicorn/finish 3 0
2019-11-26T20:42:25,011155300+00:00 - Exit code 3 is not normal. Killing image.
---------------------------------------------------------------------------
WebserviceException Traceback (most recent call last)
<ipython-input-38-4721d39c1e0c> in <module>
4 service = Model.deploy(ws, 'keras-model-deployment', [model], inference_config, deployment_config)
5
----> 6 service.wait_for_deployment(True)
7 print(service.state)
~\Anaconda3\lib\site-packages\azureml\core\webservice\local.py in decorated(self, *args, **kwargs)
63 raise WebserviceException('Cannot call {}() when service is {}.'.format(func.__name__, self.state),
64 logger=module_logger)
---> 65 return func(self, *args, **kwargs)
66 return decorated
67 return decorator
~\Anaconda3\lib\site-packages\azureml\core\webservice\local.py in wait_for_deployment(self, show_output)
566 self._container,
567 health_url=self._internal_base_url,
--> 568 cleanup_if_failed=False)
569
570 self.state = LocalWebservice.STATE_RUNNING
~\Anaconda3\lib\site-packages\azureml\_model_management\_util.py in container_health_check(docker_port, container, health_url, cleanup_if_failed)
788 cleanup_container(container)
789 raise WebserviceException('Error: Connection to container has failed. Did your init method fail?',
--> 790 logger=module_logger)
791
792
WebserviceException: WebserviceException:
Message: Error: Connection to container has failed. Did your init method fail?
InnerException None
ErrorResponse
{
"error": {
"message": "Error: Connection to container has failed. Did your init method fail?"
}
}
这是我的score.py文件(进行预测)。在出现错误之前,通常先找到此文件的问题。
import numpy as np
import os
import sys
import keras as K
from io import BytesIO
from PIL import Image, ImageOps
import base64
import json
from azureml.core.model import Model
from sklearn.externals import joblib
def init():
global model
print("Executing init() method...")
print("Python version: " + str(sys.version) + ", keras version: " + K.__version__)
# Load the model
model_path = Model.get_model_path('kerastfmodel.h5')
model = joblib.load(model_path)
def run(inputString):
responses = []
base64Dict = json.loads(inputString)
for k, v in base64Dict.items():
img_file_name, base64Img = k, v
decoded_img = base64.b64decode(base64Img)
img_buffer = BytesIO(decoded_img)
imageData = Image.open(img_buffer).convert("RGB")
# Evaluate the model using the input data
img = ImageOps.fit(imageData, (32, 32), Image.ANTIALIAS)
img_conv = np.array(img) # shape: (32, 32, 3)
# Scale pixel intensity
x_test = img_conv / 255.0
# Reshape
x_test = np.moveaxis(x_test, -1, 0)
x_test = np.expand_dims(x_test, 0) # shape (1, 3, 32, 32)
y_pred = model.predict(x_test)
y_pred = np.argmax(y_pred, axis=-1)
# print(y_pred)
LABELS = ["airplane", "automobile", "bird", "cat", "deer", "fog", "frog", "horse", "ship", "truck"]
resp = {img_file_name: str(LABELS[y_pred[0]])}
responses.append(resp)
return json.dumps(responses)