models icon indicating copy to clipboard operation
models copied to clipboard

Error running training in google ML engine - No matplotlib.pyplot module

Open sagi44222 opened this issue 9 years ago • 39 comments

Following the procedure to run a training in google ML engine I encounter a problem compiling the project. Here is a snipped from the job log:

Traceback (most recent call last): File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main "__main__", fname, loader, pkg_name) File "/usr/lib/python2.7/runpy.py", line 72, in _run_code exec code in run_globals File "/root/.local/lib/python2.7/site-packages/object_detection/eval.py", line 50, in <module> from object_detection import evaluator File "/root/.local/lib/python2.7/site-packages/object_detection/evaluator.py", line 24, in <module> from object_detection import eval_util File "/root/.local/lib/python2.7/site-packages/object_detection/eval_util.py", line 29, in <module> from object_detection.utils import visualization_utils as vis_utils File "/root/.local/lib/python2.7/site-packages/object_detection/utils/visualization_utils.py", line 24, in <module> import matplotlib.pyplot as plt ImportError: No module named matplotlib.pyplot

Evaluation job code: gcloud ml-engine jobs submit training object_detection_eval_date +%s \ --job-dir=gs://${TRAIN_DIR} \ --packages dist/object_detection-0.1.tar.gz,slim/dist/slim-0.1.tar.gz \ --module-name object_detection.eval \ --region us-central1 \ --scale-tier BASIC_GPU \ -- \ --checkpoint_dir=gs://${TRAIN_DIR} \ --eval_dir=gs://${EVAL_DIR} \ --pipeline_config_path=gs://${PIPELINE_CONFIG_PATH}

Training input:

{
  "scaleTier": "BASIC_GPU",
  "packageUris": [
    "gs://TTT/packages/9a42ca0914663739bd0e7eef559ea1e821bdd82971d3a4d3c0a9fc427fc982f1/object_detection-0.1.tar.gz",
    "gs://TTT/packages/9a42ca0914663739bd0e7eef559ea1e821bdd82971d3a4d3c0a9fc427fc982f1/slim-0.1.tar.gz"
  ],
  "pythonModule": "object_detection.eval",
  "args": [
    "--checkpoint_dir=gs://TTT/training",
    "--eval_dir=gs://TTT",
    "--pipeline_config_path=gs://TTT/ssd_mobilenet_v1_pets.config"
  ],
  "region": "asia-east1",
  "jobDir": "gs://TTT/"
}

Any solution for this?

sagi44222 avatar Nov 08 '17 11:11 sagi44222

I had exactly the same issue and posted in #2724. But unfortunately, they do not seem to care.

I managed to add the matplotlib as a dependency in the setup.py under research folder, which is hacky because I feel uncomfortable if I need to modify the source code to make it working. However, I met more errors after that.

glc12125 avatar Nov 08 '17 17:11 glc12125

@derekjchow This looks like a problem with the Pets Tutorial. Can you take a look?

angerson avatar Nov 08 '17 20:11 angerson

Why matplotlib is needed for training? While we can install matplotlib using setup.py script the python-tk could not be installed this way at Google ML engine.

andrew-veresov avatar Nov 09 '17 06:11 andrew-veresov

The ssd_meta_arch.py imports the visualization_utils and uses the add_cdf_image_summary function from it to generate images of anchor classification losses. Shouldn't we just add positive/negative losses to the summary and render images during summary visualization instead?

andrew-veresov avatar Nov 09 '17 07:11 andrew-veresov

Apologies, we will take a look at this and respond shortly.

tombstone avatar Nov 10 '17 16:11 tombstone

Same issue here, thanks.

lozuwa avatar Nov 16 '17 05:11 lozuwa

Having the same issue with pet's retrainer tutorial.

ashizhao avatar Nov 16 '17 06:11 ashizhao

I was having a similar issue in that whenever I tried importing matplotlib it would give some _tkinter error on the cluster I was training on. I installed matplotlib with pip and then as a temporary fix in research/object_detection/utils/visualization_utils.py before import matplotlib.pyplot as plt:

import os
import matplotlib as mpl
if os.environ.get('DISPLAY','') == '':
	print('no display found. Using non-interactive Agg backend')
mpl.use('Agg')

which is taken from https://stackoverflow.com/a/40931739/2698494. I have not tested it on Google's Cloud ML though.

floft avatar Nov 16 '17 15:11 floft

I have tried it on Google's Cloud ML. So I got around the matplotlib issue as @glc12125 suggested and around the tkinter error as @floft suggested. I am not sure if my problem is specific to running transfer learning on the new faster_rcnn_nas_coco model, but I get the following downstream error (after correcting for https://github.com/tensorflow/models/issues/2668 as well):

The replica master 0 exited with a non-zero status of 1. Termination reason: Error. Traceback (most recent call last): [...] File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 44, in run _sys.exit(main(_sys.argv[:1] + flags_passthrough)) File "/root/.local/lib/python2.7/site-packages/object_detection/train.py", line 159, in main worker_job_name, is_chief, FLAGS.train_dir) File "/root/.local/lib/python2.7/site-packages/object_detection/trainer.py", line 228, in train clones = model_deploy.create_clones(deploy_config, model_fn, [input_queue]) File "/root/.local/lib/python2.7/site-packages/deployment/model_deploy.py", line 193, in create_clones outputs = model_fn(*args, **kwargs) File "/root/.local/lib/python2.7/site-packages/object_detection/trainer.py", line 165, in _create_losses prediction_dict = detection_model.predict(images) File "/root/.local/lib/python2.7/site-packages/object_detection/meta_architectures/faster_rcnn_meta_arch.py", line 531, in predict image_shape) = self._extract_rpn_feature_maps(preprocessed_inputs) File "/root/.local/lib/python2.7/site-packages/object_detection/meta_architectures/faster_rcnn_meta_arch.py", line 685, in _extract_rpn_feature_maps preprocessed_inputs, scope=self.first_stage_feature_extractor_scope) File "/root/.local/lib/python2.7/site-packages/object_detection/meta_architectures/faster_rcnn_meta_arch.py", line 134, in extract_proposal_features return self._extract_proposal_features(preprocessed_inputs, scope) File "/root/.local/lib/python2.7/site-packages/object_detection/models/faster_rcnn_nas_feature_extractor.py", line 188, in _extract_proposal_features final_endpoint='Cell_11') File "/root/.local/lib/python2.7/site-packages/nets/nasnet/nasnet.py", line 378, in build_nasnet_large hparams = _large_imagenet_config(is_training=is_training) File "/root/.local/lib/python2.7/site-packages/nets/nasnet/nasnet.py", line 70, in _large_imagenet_config return tf.contrib.training.HParams( AttributeError: 'module' object has no attribute 'HParams' The replica worker 0 exited with a non-zero status of 1. Termination reason: Error. Traceback (most recent call last): [...] File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 44, in run _sys.exit(main(_sys.argv[:1] + flags_passthrough)) File "/root/.local/lib/python2.7/site-packages/object_detection/train.py", line 159, in main worker_job_name, is_chief, FLAGS.train_dir) File "/root/.local/lib/python2.7/site-packages/object_detection/trainer.py", line 228, in train clones = model_deploy.create_clones(deploy_config, model_fn, [input_queue]) File "/root/.local/lib/python2.7/site-packages/deployment/model_deploy.py", line 193, in create_clones outputs = model_fn(*args, **kwargs) File "/root/.local/lib/python2.7/site-packages/object_detection/trainer.py", line 165, in _create_losses prediction_dict = detection_model.predict(images) File "/root/.local/lib/python2.7/site-packages/object_detection/meta_architectures/faster_rcnn_meta_arch.py", line 531, in predict image_shape) = self._extract_rpn_feature_maps(preprocessed_inputs) File "/root/.local/lib/python2.7/site-packages/object_detection/meta_architectures/faster_rcnn_meta_arch.py", line 685, in _extract_rpn_feature_maps preprocessed_inputs, scope=self.first_stage_feature_extractor_scope) File "/root/.local/lib/python2.7/site-packages/object_detection/meta_architectures/faster_rcnn_meta_arch.py", line 134, in extract_proposal_features return self._extract_proposal_features(preprocessed_inputs, scope) File "/root/.local/lib/python2.7/site-packages/object_detection/models/faster_rcnn_nas_feature_extractor.py", line 188, in _extract_proposal_features final_endpoint='Cell_11') File "/root/.local/lib/python2.7/site-packages/nets/nasnet/nasnet.py", line 378, in build_nasnet_large hparams = _large_imagenet_config(is_training=is_training) File "/root/.local/lib/python2.7/site-packages/nets/nasnet/nasnet.py", line 70, in _large_imagenet_config return tf.contrib.training.HParams( AttributeError: 'module' object has no attribute 'HParams' The replica worker 1 exited with a non-zero status of 1. Termination reason: Error. Traceback (most recent call last): [...] File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 44, in run _sys.exit(main(_sys.argv[:1] + flags_passthrough)) File "/root/.local/lib/python2.7/site-packages/object_detection/train.py", line 159, in main worker_job_name, is_chief, FLAGS.train_dir) File "/root/.local/lib/python2.7/site-packages/object_detection/trainer.py", line 228, in train clones = model_deploy.create_clones(deploy_config, model_fn, [input_queue]) File "/root/.local/lib/python2.7/site-packages/deployment/model_deploy.py", line 193, in create_clones outputs = model_fn(*args, **kwargs) File "/root/.local/lib/python2.7/site-packages/object_detection/trainer.py", line 165, in _create_losses prediction_dict = detection_model.predict(images) File "/root/.local/lib/python2.7/site-packages/object_detection/meta_architectures/faster_rcnn_meta_arch.py", line 531, in predict image_shape) = self._extract_rpn_feature_maps(preprocessed_inputs) File "/root/.local/lib/python2.7/site-packages/object_detection/meta_architectures/faster_rcnn_meta_arch.py", line 685, in _extract_rpn_feature_maps preprocessed_inputs, scope=self.first_stage_feature_extractor_scope) File "/root/.local/lib/python2.7/site-packages/object_detection/meta_architectures/faster_rcnn_meta_arch.py", line 134, in extract_proposal_features return self._extract_proposal_features(preprocessed_inputs, scope) File "/root/.local/lib/python2.7/site-packages/object_detection/models/faster_rcnn_nas_feature_extractor.py", line 188, in _extract_proposal_features final_endpoint='Cell_11') File "/root/.local/lib/python2.7/site-packages/nets/nasnet/nasnet.py", line 378, in build_nasnet_large hparams = _large_imagenet_config(is_training=is_training) File "/root/.local/lib/python2.7/site-packages/nets/nasnet/nasnet.py", line 70, in _large_imagenet_config return tf.contrib.training.HParams( AttributeError: 'module' object has no attribute 'HParams' The replica worker 2 exited with a non-zero status of 1. Termination reason: Error. Traceback (most recent call last): [...] File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 44, in run _sys.exit(main(_sys.argv[:1] + flags_passthrough)) File "/root/.local/lib/python2.7/site-packages/object_detection/train.py", line 159, in main worker_job_name, is_chief, FLAGS.train_dir) File "/root/.local/lib/python2.7/site-packages/object_detection/trainer.py", line 228, in train clones = model_deploy.create_clones(deploy_config, model_fn, [input_queue]) File "/root/.local/lib/python2.7/site-packages/deployment/model_deploy.py", line 193, in create_clones outputs = model_fn(*args, **kwargs) File "/root/.local/lib/python2.7/site-packages/object_detection/trainer.py", line 165, in _create_losses prediction_dict = detection_model.predict(images) File "/root/.local/lib/python2.7/site-packages/object_detection/meta_architectures/faster_rcnn_meta_arch.py", line 531, in predict image_shape) = self._extract_rpn_feature_maps(preprocessed_inputs) File "/root/.local/lib/python2.7/site-packages/object_detection/meta_architectures/faster_rcnn_meta_arch.py", line 685, in _extract_rpn_feature_maps preprocessed_inputs, scope=self.first_stage_feature_extractor_scope) File "/root/.local/lib/python2.7/site-packages/object_detection/meta_architectures/faster_rcnn_meta_arch.py", line 134, in extract_proposal_features return self._extract_proposal_features(preprocessed_inputs, scope) File "/root/.local/lib/python2.7/site-packages/object_detection/models/faster_rcnn_nas_feature_extractor.py", line 188, in _extract_proposal_features final_endpoint='Cell_11') File "/root/.local/lib/python2.7/site-packages/nets/nasnet/nasnet.py", line 378, in build_nasnet_large hparams = _large_imagenet_config(is_training=is_training) File "/root/.local/lib/python2.7/site-packages/nets/nasnet/nasnet.py", line 70, in _large_imagenet_config return tf.contrib.training.HParams( AttributeError: 'module' object has no attribute 'HParams' The replica worker 3 exited with a non-zero status of 1. Termination reason: Error. Traceback (most recent call last): [...] File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 44, in run _sys.exit(main(_sys.argv[:1] + flags_passthrough)) File "/root/.local/lib/python2.7/site-packages/object_detection/train.py", line 159, in main worker_job_name, is_chief, FLAGS.train_dir) File "/root/.local/lib/python2.7/site-packages/object_detection/trainer.py", line 228, in train clones = model_deploy.create_clones(deploy_config, model_fn, [input_queue]) File "/root/.local/lib/python2.7/site-packages/deployment/model_deploy.py", line 193, in create_clones outputs = model_fn(*args, **kwargs) File "/root/.local/lib/python2.7/site-packages/object_detection/trainer.py", line 165, in _create_losses prediction_dict = detection_model.predict(images) File "/root/.local/lib/python2.7/site-packages/object_detection/meta_architectures/faster_rcnn_meta_arch.py", line 531, in predict image_shape) = self._extract_rpn_feature_maps(preprocessed_inputs) File "/root/.local/lib/python2.7/site-packages/object_detection/meta_architectures/faster_rcnn_meta_arch.py", line 685, in _extract_rpn_feature_maps preprocessed_inputs, scope=self.first_stage_feature_extractor_scope) File "/root/.local/lib/python2.7/site-packages/object_detection/meta_architectures/faster_rcnn_meta_arch.py", line 134, in extract_proposal_features return self._extract_proposal_features(preprocessed_inputs, scope) File "/root/.local/lib/python2.7/site-packages/object_detection/models/faster_rcnn_nas_feature_extractor.py", line 188, in _extract_proposal_features final_endpoint='Cell_11') File "/root/.local/lib/python2.7/site-packages/nets/nasnet/nasnet.py", line 378, in build_nasnet_large hparams = _large_imagenet_config(is_training=is_training) File "/root/.local/lib/python2.7/site-packages/nets/nasnet/nasnet.py", line 70, in _large_imagenet_config return tf.contrib.training.HParams( AttributeError: 'module' object has no attribute 'HParams' The replica worker 4 exited with a non-zero status of 1. Termination reason: Error. Traceback (most recent call last): [...] File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 44, in run _sys.exit(main(_sys.argv[:1] + flags_passthrough)) File "/root/.local/lib/python2.7/site-packages/object_detection/train.py", line 159, in main worker_job_name, is_chief, FLAGS.train_dir) File "/root/.local/lib/python2.7/site-packages/object_detection/trainer.py", line 228, in train clones = model_deploy.create_clones(deploy_config, model_fn, [input_queue]) File "/root/.local/lib/python2.7/site-packages/deployment/model_deploy.py", line 193, in create_clones outputs = model_fn(*args, **kwargs) File "/root/.local/lib/python2.7/site-packages/object_detection/trainer.py", line 165, in _create_losses prediction_dict = detection_model.predict(images) File "/root/.local/lib/python2.7/site-packages/object_detection/meta_architectures/faster_rcnn_meta_arch.py", line 531, in predict image_shape) = self._extract_rpn_feature_maps(preprocessed_inputs) File "/root/.local/lib/python2.7/site-packages/object_detection/meta_architectures/faster_rcnn_meta_arch.py", line 685, in _extract_rpn_feature_maps preprocessed_inputs, scope=self.first_stage_feature_extractor_scope) File "/root/.local/lib/python2.7/site-packages/object_detection/meta_architectures/faster_rcnn_meta_arch.py", line 134, in extract_proposal_features return self._extract_proposal_features(preprocessed_inputs, scope) File "/root/.local/lib/python2.7/site-packages/object_detection/models/faster_rcnn_nas_feature_extractor.py", line 188, in _extract_proposal_features final_endpoint='Cell_11') File "/root/.local/lib/python2.7/site-packages/nets/nasnet/nasnet.py", line 378, in build_nasnet_large hparams = _large_imagenet_config(is_training=is_training) File "/root/.local/lib/python2.7/site-packages/nets/nasnet/nasnet.py", line 70, in _large_imagenet_config return tf.contrib.training.HParams( AttributeError: 'module' object has no attribute 'HParams'

law826 avatar Nov 17 '17 16:11 law826

Was this issue ever addressed? I am unable to run a model I could previously run with an older version of the object_detection API due to the matplotlib and python-tk dependency issue.

staturecrane avatar Nov 20 '17 01:11 staturecrane

This blog mentions some changes in setup.py that addresses compatibility issues with Tensorflow and GCP: https://medium.com/google-cloud/object-detection-tensorflow-and-google-cloud-platform-72e0a3f3bdd6

ashizhao avatar Nov 21 '17 17:11 ashizhao

Hey ashizhao,

Thanks for pointing out the direction. I have tried the "fix" for setup.py in the blog you mentioned, and faced another GCP runtime version not-up-to-date issue in #2707. I followed the solution mentioned in #2707 again, where I have to change the source code, in both object_detection/evaluator.py(line 184) and object_detection/builders/optimizer_builder.py (line 103) to use tf.contrib.framework.get_or_create_global_step instead of tf.train.get_or_create_global_step. I finally got it working for both training and evaluation.

I guess the main issues are twofolds:

  1. GCP library dependency (matplotlib, python-tk, not sure why we need them though, because we do not need to visualise them when running in the cloud)
  2. GCP tensorflow version is not up to date.

glc12125 avatar Nov 21 '17 20:11 glc12125

Hi @glc12125 , thank you for your help. I tried everything you mentioned above and got this error:

Traceback (most recent call last): File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main "__main__", fname, loader, pkg_name) File "/usr/lib/python2.7/runpy.py", line 72, in _run_code exec code in run_globals File "/root/.local/lib/python2.7/site-packages/object_detection/train.py", line 163, in <module> tf.app.run() File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 48, in run _sys.exit(main(_sys.argv[:1] + flags_passthrough)) File "/root/.local/lib/python2.7/site-packages/object_detection/train.py", line 159, in main worker_job_name, is_chief, FLAGS.train_dir) File "/root/.local/lib/python2.7/site-packages/object_detection/trainer.py", line 254, in train var_map, train_config.fine_tune_checkpoint)) File "/root/.local/lib/python2.7/site-packages/object_detection/utils/variables_helper.py", line 122, in get_variables_available_in_checkpoint ckpt_reader = tf.train.NewCheckpointReader(checkpoint_path) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 110, in NewCheckpointReader return CheckpointReader(compat.as_bytes(filepattern), status) File "/usr/lib/python2.7/contextlib.py", line 24, in __exit__ self.gen.next() File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/errors_impl.py", line 466, in raise_exception_on_not_ok_status pywrap_tensorflow.TF_GetCode(status)) InvalidArgumentError: Unsuccessful TensorSliceReader constructor: Failed to get matching files on gs://${YOUR_GCS_BUCKET}/data/model.ckpt: Not found: Error executing an HTTP request (HTTP response code 404, error code 0, error message '') when reading gs://${YOUR_GCS_BUCKET}/data

are you familiar with this error?

ashizhao avatar Nov 28 '17 00:11 ashizhao

Hey @ashizhao,

I believe you need to replace the YOUR_GCS_BUCKET with your actual bucket name in the pipeline config file.

glc12125 avatar Nov 28 '17 03:11 glc12125

@glc12125 thanks for summarising how to fix these issues I succeeded in fixing the not-up-to-date issue also by adding 'tensorflow>=1.4' to the required packages in setup.py

EffePen avatar Dec 01 '17 16:12 EffePen

Thanks for the answers, I was able to solve the issue as well. Nonetheless, I ended up buying myself a GTX 1080 ti. Training locally is way better.

On 1 Dec 2017 12:14 p.m., "EffePen" [email protected] wrote:

@glc12125 https://github.com/glc12125 thanks for summarising how to fix these issues I succeeded in fixing the not-up-to-date issue also by adding 'tensorflow>=1.4' to the required packaged in setup.py

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/tensorflow/models/issues/2739#issuecomment-348536700, or mute the thread https://github.com/notifications/unsubscribe-auth/AKzzDiUnJFnvyUuzP4aZ_yg9VzZHbVe5ks5s8CX1gaJpZM4QWPQX .

lozuwa avatar Dec 02 '17 14:12 lozuwa

I think we are going to try out Amazon's Sagemaker, instead of dealing with all of these dependency issues with training and model version hosting. Somehow it supports Tensorflow > 1.2 and Google Cloud does not.

staturecrane avatar Dec 02 '17 23:12 staturecrane

FWIW TF 1.4 is now available on CloudML Engine.

rhaertel80 avatar Dec 12 '17 06:12 rhaertel80

Thanks @glc12125 for your help. Will post the current solution for this.

Now that TF 1.4 is available on CloudML Engine, the following changes fix this problem:

Make sure your yaml version is 1.4, eg:

trainingInput:
  runtimeVersion: "1.4"
  scaleTier: CUSTOM
  masterType: standard_gpu
  workerCount: 5
  workerType: standard_gpu
  parameterServerCount: 3
  parameterServerType: standard

Change setup.py to the following:

"""Setup script for object_detection."""

import logging
import subprocess
from setuptools import find_packages
from setuptools import setup
from setuptools.command.install import install

class CustomCommands(install):

	def RunCustomCommand(self, command_list):
		p = subprocess.Popen(
        command_list,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT)
		stdout_data, _ = p.communicate()
		logging.info('Log command output: %s', stdout_data)
		if p.returncode != 0:
			raise RuntimeError('Command %s failed: exit code: %s' %
                         (command_list, p.returncode))

	def run(self):
		self.RunCustomCommand(['apt-get', 'update'])
		self.RunCustomCommand(
          ['apt-get', 'install', '-y', 'python-tk'])
		install.run(self)

REQUIRED_PACKAGES = ['Pillow>=1.0', 'protobuf>=3.3.0', 'Matplotlib>=2.1']

setup(
    name='object_detection',
    version='0.1',
    install_requires=REQUIRED_PACKAGES,
    include_package_data=True,
    packages=[p for p in find_packages() if p.startswith('object_detection')],
    description='Tensorflow Object Detection Library',
 cmdclass={
        'install': CustomCommands,
    }
)

In object_detection/utils/visualization_utils.py, line 24 (before import matplotlib.pyplot as plt) add:

import matplotlib
matplotlib.use('agg')

In line 184 of object_detection/evaluator.py, change

tf.train.get_or_create_global_step()

to

tf.contrib.framework.get_or_create_global_step()

Finally, in line 103 of object_detection/builders/optimizer_builder.py, change

tf.train.get_or_create_global_step()

to

tf.contrib.framework.get_or_create_global_step()

Hope this helps!

andersskog avatar Dec 12 '17 22:12 andersskog

It's working great now with those fixes. Thanks!

law826 avatar Dec 15 '17 13:12 law826