tensorflow / tensorflow/models
[object_detection] How to use random_jpeg_quality in tensorflow 1.*
Open
Nobody has claimed this yet.
models:research:odapi
type:bug
- Dominant language
- Python
- Stars
- 77.7k
- Forks
- 44.8k
- PR merge metrics
- No merged PRs in 30d
Description
Prerequisites
Please answer the following questions for yourself before submitting an issue.
- [ X ] I am using the latest TensorFlow Model Garden release and TensorFlow 2.
- [ O ] I am reporting the issue to the correct repository. (Model Garden official or research directory)
- [ O ] I checked to make sure that this issue has not already been filed.
1. The entire URL of the file you are using
https://github.com/tensorflow/models/tree/master/research/object_detection
2. Describe the bug
pipeline.config
train_config: {
data_augmentation_options {
random_jpeg_quality {
}
}
}
Error
Traceback (most recent call last):
File "object_detection/model_main.py", line 108, in <module>
tf.app.run()
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\tensorflow_core\python\platform\app.py", line 40, in run
_run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\absl\app.py", line 299, in run
_run_main(main, args)
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\absl\app.py", line 250, in _run_main
sys.exit(main(argv))
File "object_detection/model_main.py", line 104, in main
tf.estimator.train_and_evaluate(estimator, train_spec, eval_specs[0])
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\tensorflow_estimator\python\estimator\training.py", line 473, in train_and_evaluate
return executor.run()
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\tensorflow_estimator\python\estimator\training.py", line 613, in run
return self.run_local()
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\tensorflow_estimator\python\estimator\training.py", line 714, in run_local
saving_listeners=saving_listeners)
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 370, in train
loss = self._train_model(input_fn, hooks, saving_listeners)
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 1161, in _train_model
return self._train_model_default(input_fn, hooks, saving_listeners)
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 1191, in _train_model_default
features, labels, ModeKeys.TRAIN, self.config)
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 1149, in _call_model_fn
model_fn_results = self._model_fn(features=features, **kwargs)
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\object_detection\model_lib.py", line 464, in model_fn
features[fields.InputDataFields.true_image_shape], **side_inputs)
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\object_detection\meta_architectures\ssd_meta_arch.py", line 579, in predict
preprocessed_inputs)
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\object_detection\models\ssd_mobilenet_v3_feature_extractor.py", line 149, in extract_features
scope=scope)
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\tensorflow_core\contrib\framework\python\ops\arg_scope.py", line 182, in func_with_args
return func(*args, **current_args)
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\slim-0.1-py3.7.egg\nets\mobilenet\mobilenet_v3.py", line 338, in mobilenet_base
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\tensorflow_core\contrib\framework\python\ops\arg_scope.py", line 182, in func_with_args
return func(*args, **current_args)
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\slim-0.1-py3.7.egg\nets\mobilenet\mobilenet_v3.py", line 328, in mobilenet
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\tensorflow_core\contrib\framework\python\ops\arg_scope.py", line 182, in func_with_args
return func(*args, **current_args)
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\slim-0.1-py3.7.egg\nets\mobilenet\mobilenet.py", line 368, in mobilenet
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\tensorflow_core\contrib\framework\python\ops\arg_scope.py", line 182, in func_with_args
return func(*args, **current_args)
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\slim-0.1-py3.7.egg\nets\mobilenet\mobilenet.py", line 287, in mobilenet_base
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\tensorflow_core\contrib\framework\python\ops\arg_scope.py", line 182, in func_with_args
return func(*args, **current_args)
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\tensorflow_core\contrib\layers\python\layers\layers.py", line 1159, in convolution2d
conv_dims=2)
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\tensorflow_core\contrib\framework\python\ops\arg_scope.py", line 182, in func_with_args
return func(*args, **current_args)
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\tensorflow_core\contrib\layers\python\layers\layers.py", line 1057, in convolution
outputs = layer.apply(inputs)
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\tensorflow_core\python\util\deprecation.py", line 324, in new_func
return func(*args, **kwargs)
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 1700, in apply
return self.__call__(inputs, *args, **kwargs)
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\tensorflow_core\python\layers\base.py", line 548, in __call__
outputs = super(Layer, self).__call__(inputs, *args, **kwargs)
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 824, in __call__
self._maybe_build(inputs)
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 2146, in _maybe_build
self.build(input_shapes)
File "C:\ProgramData\Anaconda3\envs\model\lib\site-packages\tensorflow_core\python\keras\layers\convolutional.py", line 154, in build
raise ValueError('The channel dimension of the inputs '
ValueError: The channel dimension of the inputs should be defined. Found `None`.
3. Steps to reproduce
Use random_jpeg_quality in tensorflow 1.*
4. Expected behavior
Cause
tf.image.adjust_jpeg_quality does not include image channel in tensorflow 1.* (current lastest version 1.15.4)
tf v1: shape=(?, ?, ?)
tf v2: shape=(?, ?, CHANNEL)
5. Additional context
Solution
Copy tf.image.adjust_jpeg_quality to your python library from tensorflow 2.*
How
Open and edit PYTHON_DIR/Lib/site-packages/tensorflow_core/python/ops/image_ops_impl.py
before
@tf_export('image.adjust_jpeg_quality')
def adjust_jpeg_quality(image, jpeg_quality, name=None):
"""Adjust jpeg encoding quality of an RGB image.
This is a convenience method that adjusts jpeg encoding quality of an
RGB image.
`image` is an RGB image. The image's encoding quality is adjusted
to `jpeg_quality`.
`jpeg_quality` must be in the interval `[0, 100]`.
Args:
image: RGB image or images. Size of the last dimension must be 3.
jpeg_quality: Python int or Tensor of type int32. jpeg encoding quality.
name: A name for this operation (optional).
Returns:
Adjusted image(s), same shape and DType as `image`.
Usage Example:
```python
>> import tensorflow as tf
>> x = tf.random.normal(shape=(256, 256, 3))
>> tf.image.adjust_jpeg_quality(x, 75)
```
Raises:
InvalidArgumentError: quality must be in [0,100]
InvalidArgumentError: image must have 1 or 3 channels
"""
with ops.name_scope(name, 'adjust_jpeg_quality', [image]) as name:
image = ops.convert_to_tensor(image, name='image')
# Remember original dtype to so we can convert back if needed
orig_dtype = image.dtype
# Convert to uint8
image = convert_image_dtype(image, dtypes.uint8)
# Encode image to jpeg with given jpeg quality
if compat.forward_compatible(2019, 4, 4):
if not _is_tensor(jpeg_quality):
# If jpeg_quality is a int (not tensor).
jpeg_quality = ops.convert_to_tensor(jpeg_quality, dtype=dtypes.int32)
image = gen_image_ops.encode_jpeg_variable_quality(image, jpeg_quality)
else:
image = gen_image_ops.encode_jpeg(image, quality=jpeg_quality)
# Decode jpeg image
image = gen_image_ops.decode_jpeg(image)
# Convert back to original dtype and return
return convert_image_dtype(image, orig_dtype)
after
@tf_export('image.adjust_jpeg_quality')
def adjust_jpeg_quality(image, jpeg_quality, name=None):
"""Adjust jpeg encoding quality of an RGB image.
This is a convenience method that adjusts jpeg encoding quality of an
RGB image.
`image` is an RGB image. The image's encoding quality is adjusted
to `jpeg_quality`.
`jpeg_quality` must be in the interval `[0, 100]`.
Args:
image: RGB image or images. Size of the last dimension must be 3.
jpeg_quality: Python int or Tensor of type int32. jpeg encoding quality.
name: A name for this operation (optional).
Returns:
Adjusted image(s), same shape and DType as `image`.
Usage Example:
```python
>> import tensorflow as tf
>> x = tf.random.normal(shape=(256, 256, 3))
>> tf.image.adjust_jpeg_quality(x, 75)
```
Raises:
InvalidArgumentError: quality must be in [0,100]
InvalidArgumentError: image must have 1 or 3 channels
"""
"""with ops.name_scope(name, 'adjust_jpeg_quality', [image]) as name:
image = ops.convert_to_tensor(image, name='image')
# Remember original dtype to so we can convert back if needed
orig_dtype = image.dtype
# Convert to uint8
image = convert_image_dtype(image, dtypes.uint8)
# Encode image to jpeg with given jpeg quality
if compat.forward_compatible(2019, 4, 4):
if not _is_tensor(jpeg_quality):
# If jpeg_quality is a int (not tensor).
jpeg_quality = ops.convert_to_tensor(jpeg_quality, dtype=dtypes.int32)
image = gen_image_ops.encode_jpeg_variable_quality(image, jpeg_quality)
else:
image = gen_image_ops.encode_jpeg(image, quality=jpeg_quality)
# Decode jpeg image
image = gen_image_ops.decode_jpeg(image)
# Convert back to original dtype and return
return convert_image_dtype(image, orig_dtype)
"""
with ops.name_scope(name, 'adjust_jpeg_quality', [image]):
image = ops.convert_to_tensor(image, name='image')
channels = image.shape.as_list()[-1]
# Remember original dtype to so we can convert back if needed
orig_dtype = image.dtype
image = convert_image_dtype(image, dtypes.uint8, saturate=True)
if not _is_tensor(jpeg_quality):
# If jpeg_quality is a int (not tensor).
jpeg_quality = ops.convert_to_tensor(jpeg_quality, dtype=dtypes.int32)
image = gen_image_ops.encode_jpeg_variable_quality(image, jpeg_quality)
image = gen_image_ops.decode_jpeg(image, channels=channels)
return convert_image_dtype(image, orig_dtype, saturate=True)
6. System information
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Windows 10
- Mobile device name if the issue happens on a mobile device: X
- TensorFlow installed from (source or binary): binary
- TensorFlow version (use command below): v1.15.0-1-gd4211884c1 1.15.0
- Python version: 3.7.7
- Bazel version (if compiling from source): X
- GCC/Compiler version (if compiling from source): X
- CUDA/cuDNN version: X
- GPU model and memory: X
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.