google / automl

Google Brain AutoML
Apache License 2.0
6.24k stars 1.45k forks source link

Inference on multiple images with different sizes, thanks #282

Closed junyongyou closed 3 years ago

junyongyou commented 4 years ago

I have trained a model, and want to make inference on multiple images with different sizes. It is working normally if I do the images separately. However, when I tried to do them in one script, even though the ModelInspector is built individually for each image, I still get a ValueError: Variable efficientnet-b4/stem/conv2d/kernel already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? See the full error track below. Can anybody help on this? Thanks a lot.

`File "C:/automl/efficientdet/model_inspect_steinsvik.py", line 219, in inference_single_image outputs_np = driver.inference(image_path, output_dir, config_dict) File "C:\automl\efficientdet\inference.py", line 605, in inference self.params) File "C:\automl\efficientdet\inference.py", line 118, in build_model class_outputs, box_outputs = model_arch(inputs, model_name, *kwargs) File "C:\automl\efficientdet\efficientdet_arch.py", line 682, in efficientdet features = build_backbone(features, config) File "C:\automl\efficientdet\efficientdet_arch.py", line 412, in build_backbone override_params=override_params) File "C:\automl\efficientdet\backbone\efficientnet_builder.py", line 328, in build_model_base features = model(images, training=training, features_only=True) File "C:\Users\junyong\AppData\Local\Continuum\anaconda3\envs\tensorflow2\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 778, in call outputs = call_fn(cast_inputs, args, **kwargs) File "C:\Users\junyong\AppData\Local\Continuum\anaconda3\envs\tensorflow2\lib\site-packages\tensorflow_core\python\autograph\impl\api.py", line 237, in wrapper raise e.ag_error_metadata.to_exception(e) ValueError: in converted code:

C:\automl\efficientdet\backbone\efficientnet_model.py:637 call  *
    outputs = self._relu_fn(
C:\Users\junyong\AppData\Local\Continuum\anaconda3\envs\tensorflow2\lib\site-packages\tensorflow_core\python\layers\base.py:547 __call__
    outputs = super(Layer, self).__call__(inputs, *args, **kwargs)
C:\Users\junyong\AppData\Local\Continuum\anaconda3\envs\tensorflow2\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py:748 __call__
    self._maybe_build(inputs)
C:\Users\junyong\AppData\Local\Continuum\anaconda3\envs\tensorflow2\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py:2116 _maybe_build
    self.build(input_shapes)
C:\Users\junyong\AppData\Local\Continuum\anaconda3\envs\tensorflow2\lib\site-packages\tensorflow_core\python\keras\layers\convolutional.py:158 build
    dtype=self.dtype)
C:\Users\junyong\AppData\Local\Continuum\anaconda3\envs\tensorflow2\lib\site-packages\tensorflow_core\python\layers\base.py:460 add_weight
    **kwargs)
C:\Users\junyong\AppData\Local\Continuum\anaconda3\envs\tensorflow2\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py:446 add_weight
    caching_device=caching_device)
C:\Users\junyong\AppData\Local\Continuum\anaconda3\envs\tensorflow2\lib\site-packages\tensorflow_core\python\training\tracking\base.py:744 _add_variable_with_custom_getter
    **kwargs_for_getter)
C:\Users\junyong\AppData\Local\Continuum\anaconda3\envs\tensorflow2\lib\site-packages\tensorflow_core\python\ops\variable_scope.py:1572 get_variable
    aggregation=aggregation)
C:\Users\junyong\AppData\Local\Continuum\anaconda3\envs\tensorflow2\lib\site-packages\tensorflow_core\python\ops\variable_scope.py:1315 get_variable
    aggregation=aggregation)
C:\Users\junyong\AppData\Local\Continuum\anaconda3\envs\tensorflow2\lib\site-packages\tensorflow_core\python\ops\variable_scope.py:568 get_variable
    aggregation=aggregation)
C:\Users\junyong\AppData\Local\Continuum\anaconda3\envs\tensorflow2\lib\site-packages\tensorflow_core\python\ops\variable_scope.py:520 _true_getter
    aggregation=aggregation)
C:\Users\junyong\AppData\Local\Continuum\anaconda3\envs\tensorflow2\lib\site-packages\tensorflow_core\python\ops\variable_scope.py:861 _get_single_variable
    raise ValueError(err_msg)

ValueError: Variable efficientnet-b4/stem/conv2d/kernel already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope?

`

junyongyou commented 4 years ago

I got a temporal solution by using tf.reset_default_graph(), but perhaps there are better approaches.

fsx950223 commented 4 years ago

I got a temporal solution by using tf.reset_default_graph(), but perhaps there are better approaches.

Try ServingDriver

abhisaar commented 4 years ago

I get following error with serving driver. My image input is two jpg images of size 400x400 both.

Code imgs = [] for f in ['/tf/notebooks/resized100/158.jpg', '/tf/notebooks/resized100/5800.jpg']: imgs.append(np.array(Image.open(f))) driver_serving = ServingDriver( 'efficientdet-d0', '/tf/notebooks/automl/efficientdet/efficientdet-d0', batch_size=len(imgs)) driver_serving.build() predictions = driver_serving.serve_images(imgs) for i in range(len(imgs)): driver.visualize(imgs[i], predictions[i])

Error

InvalidArgumentError Traceback (most recent call last) /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py in _do_call(self, fn, args) 1364 try: -> 1365 return fn(args) 1366 except errors.OpError as e:

/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata) 1349 return self._call_tf_sessionrun(options, feed_dict, fetch_list, -> 1350 target_list, run_metadata) 1351

/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata) 1442 fetch_list, target_list, -> 1443 run_metadata) 1444

InvalidArgumentError: 2 root error(s) found. (0) Invalid argument: Shapes of all inputs must match: values[0].shape = [1,7] != values[1].shape = [0,7] [[{{node detections}}]] [[detections/_1519]] (1) Invalid argument: Shapes of all inputs must match: values[0].shape = [1,7] != values[1].shape = [0,7] [[{{node detections}}]] 0 successful operations. 0 derived errors ignored.

During handling of the above exception, another exception occurred:

InvalidArgumentError Traceback (most recent call last)

in 5 'efficientdet-d0', '/tf/notebooks/automl/efficientdet/efficientdet-d0', batch_size=len(imgs)) 6 driver_serving.build() ----> 7 predictions = driver_serving.serve_images(imgs) 8 for i in range(len(imgs)): 9 driver.visualize(imgs[i], predictions[i]) /tf/notebooks/automl/efficientdet/inference.py in serve_images(self, image_arrays) 524 predictions = self.sess.run( 525 self.signitures['prediction'], --> 526 feed_dict={self.signitures['image_arrays']: image_arrays}) 527 return predictions 528 /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata) 954 try: 955 result = self._run(None, fetches, feed_dict, options_ptr, --> 956 run_metadata_ptr) 957 if run_metadata: 958 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr) /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata) 1178 if final_fetches or final_targets or (handle and feed_dict_tensor): 1179 results = self._do_run(handle, final_targets, final_fetches, -> 1180 feed_dict_tensor, options, run_metadata) 1181 else: 1182 results = [] /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata) 1357 if handle is None: 1358 return self._do_call(_run_fn, feeds, fetches, targets, options, -> 1359 run_metadata) 1360 else: 1361 return self._do_call(_prun_fn, handle, feeds, fetches) /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py in _do_call(self, fn, *args) 1382 '\nsession_config.graph_options.rewrite_options.' 1383 'disable_meta_optimizer = True') -> 1384 raise type(e)(node_def, op, message) 1385 1386 def _extend_graph(self): InvalidArgumentError: 2 root error(s) found. (0) Invalid argument: Shapes of all inputs must match: values[0].shape = [1,7] != values[1].shape = [0,7] [[node detections (defined at /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/framework/ops.py:1751) ]] [[detections/_1519]] (1) Invalid argument: Shapes of all inputs must match: values[0].shape = [1,7] != values[1].shape = [0,7] [[node detections (defined at /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/framework/ops.py:1751) ]] 0 successful operations. 0 derived errors ignored. Original stack trace for 'detections': File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main "__main__", mod_spec) File "/usr/lib/python3.7/runpy.py", line 85, in _run_code exec(code, run_globals) File "/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py", line 16, in app.launch_new_instance() File "/usr/local/lib/python3.7/dist-packages/traitlets/config/application.py", line 664, in launch_instance app.start() File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelapp.py", line 583, in start self.io_loop.start() File "/usr/local/lib/python3.7/dist-packages/tornado/platform/asyncio.py", line 149, in start self.asyncio_loop.run_forever() File "/usr/lib/python3.7/asyncio/base_events.py", line 534, in run_forever self._run_once() File "/usr/lib/python3.7/asyncio/base_events.py", line 1771, in _run_once handle._run() File "/usr/lib/python3.7/asyncio/events.py", line 88, in _run self._context.run(self._callback, *self._args) File "/usr/local/lib/python3.7/dist-packages/tornado/ioloop.py", line 690, in lambda f: self._run_callback(functools.partial(callback, future)) File "/usr/local/lib/python3.7/dist-packages/tornado/ioloop.py", line 743, in _run_callback ret = callback() File "/usr/local/lib/python3.7/dist-packages/tornado/gen.py", line 787, in inner self.run() File "/usr/local/lib/python3.7/dist-packages/tornado/gen.py", line 748, in run yielded = self.gen.send(value) File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 365, in process_one yield gen.maybe_future(dispatch(*args)) File "/usr/local/lib/python3.7/dist-packages/tornado/gen.py", line 209, in wrapper yielded = next(result) File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 268, in dispatch_shell yield gen.maybe_future(handler(stream, idents, msg)) File "/usr/local/lib/python3.7/dist-packages/tornado/gen.py", line 209, in wrapper yielded = next(result) File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 545, in execute_request user_expressions, allow_stdin, File "/usr/local/lib/python3.7/dist-packages/tornado/gen.py", line 209, in wrapper yielded = next(result) File "/usr/local/lib/python3.7/dist-packages/ipykernel/ipkernel.py", line 300, in do_execute res = shell.run_cell(code, store_history=store_history, silent=silent) File "/usr/local/lib/python3.7/dist-packages/ipykernel/zmqshell.py", line 536, in run_cell return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs) File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2858, in run_cell raw_cell, store_history, silent, shell_futures) File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2886, in _run_cell return runner(coro) File "/usr/local/lib/python3.7/dist-packages/IPython/core/async_helpers.py", line 68, in _pseudo_sync_runner coro.send(None) File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 3063, in run_cell_async interactivity=interactivity, compiler=compiler, result=result) File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 3254, in run_ast_nodes if (await self.run_code(code, result, async_=asy)): File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 3331, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 6, in driver_serving.build() File "/tf/notebooks/automl/efficientdet/inference.py", line 414, in build self.max_boxes_to_draw) File "/tf/notebooks/automl/efficientdet/inference.py", line 216, in det_post_process return tf.stack(detections_batch, name='detections') File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/util/dispatch.py", line 180, in wrapper return target(*args, **kwargs) File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/array_ops.py", line 1154, in stack return ops.convert_to_tensor(values, name=name) File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/framework/ops.py", line 1184, in convert_to_tensor return convert_to_tensor_v2(value, dtype, preferred_dtype, name) File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/framework/ops.py", line 1242, in convert_to_tensor_v2 as_ref=False) File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/framework/ops.py", line 1296, in internal_convert_to_tensor ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref) File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/array_ops.py", line 1278, in _autopacking_conversion_function return _autopacking_helper(v, dtype, name or "packed") File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/array_ops.py", line 1214, in _autopacking_helper return gen_array_ops.pack(elems_as_tensors, name=scope) File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/gen_array_ops.py", line 6304, in pack "Pack", values=values, axis=axis, name=name) File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/framework/op_def_library.py", line 793, in _apply_op_helper op_def=op_def) File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/util/deprecation.py", line 507, in new_func return func(*args, **kwargs) File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/framework/ops.py", line 3360, in create_op attrs, op_def, compute_device) File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/framework/ops.py", line 3429, in _create_op_internal op_def=op_def) File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/framework/ops.py", line 1751, in __init__ self._traceback = tf_stack.extract_stack()