概述
tf.image.resize_bilinear
https://github.com/tensorflow/docs/tree/r1.4/site/en/api_docs/api_docs/python/tf/image
site/en/api_docs/api_docs/python/tf/image/resize_bilinear.md
resize_bilinear(
images,
size,
align_corners=False,
name=None
)
bilinear [baɪ'lɪnɪə]:adj. 双线性的
Defined in tensorflow/python/ops/gen_image_ops.py
.
See the guide: Images > Resizing
Resize images
to size
using bilinear interpolation.
使用双线性插值缩放 images
为 size
。
Input images can be of different types but output images are always float.
输入图像可以是不同的类型,但输出图像总是浮点型的。
1. Args:
images
: ATensor
. Must be one of the following types:uint8
,int8
,int16
,int32
,int64
,half
,float32
,float64
. 4-D with shape[batch, height, width, channels]
. (4 维的并且具有形状[batch, height, width, channels]
。)size
: A 1-D int32 Tensor of 2 elements:new_height, new_width
. The new size for the images.align_corners
: An optionalbool
. Defaults toFalse
. If true, rescale input by (new_height - 1) / (height - 1), which exactly aligns the 4 corners of images and resized images. If false, rescale by new_height / height. Treat similarly the width dimension. (可选的bool
,默认为False
。如果为 true,则输入和输出张量的 4 个角像素的中心对齐。)name
: A name for the operation (optional). (操作的名称 (可选)。)
2. Returns:
A Tensor
of type float32
. 4-D with shape [batch, new_height, new_width, channels]
.
该函数返回 float32 类型的 Tensor。
3. TensorFlow 可视化缩放图像
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
current_directory = os.path.dirname(os.path.abspath(__file__))
import numpy as np
import tensorflow as tf
import cv2
import time
def short_side_resize_for_inference_data(img_tensor, target_shortside_len, is_resize=True):
h, w, = tf.shape(img_tensor)[0], tf.shape(img_tensor)[1]
img_tensor = tf.expand_dims(img_tensor, axis=0)
if is_resize:
new_h, new_w = tf.cond(tf.less(h, w),
true_fn=lambda: (target_shortside_len, target_shortside_len * w // h),
false_fn=lambda: (target_shortside_len * h // w, target_shortside_len))
img_tensor = tf.image.resize_bilinear(img_tensor, [new_h, new_w], align_corners=True)
return img_tensor # [1, h, w, c]
def inference(image_file):
# preprocess image
img_placeholder = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3])
img_batch = tf.cast(img_placeholder, tf.float32)
resize_image_batch = short_side_resize_for_inference_data(img_tensor=img_batch, target_shortside_len=512,
is_resize=True)
init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
sess.run(init_op)
img = cv2.imread(image_file)
height, width, channels = img.shape
size = (int(height), int(width))
tmp_directory = current_directory + "/tmp"
if not os.path.exists(tmp_directory):
os.makedirs(tmp_directory)
start = time.time()
resized_img_batch = sess.run([resize_image_batch], feed_dict={img_placeholder: img})
resized_img = np.squeeze(resized_img_batch, 0)
print("tf.shape(img):", sess.run(tf.shape(img))) # tf.shape(img): [1080 1920 3]
print("tf.shape(resized_img):", sess.run(tf.shape(resized_img))) # tf.shape(resized_img): [512 910 3]
print("img type:", img.dtype) # img type: uint8
print("resized_img type:", resized_img.dtype) # resized_img type: float32
resized_img = np.asarray(resized_img, dtype='uint8')
end = time.time()
cv2.putText(img, text="source image", org=(10, 10), fontFace=1, fontScale=1, color=(0, 0, 255))
cv2.putText(resized_img, text="resized image", org=(10, 10), fontFace=1, fontScale=1, color=(0, 0, 255))
# Display the resulting frame
cv2.imshow("Press ESC on keyboard to exit. 1", img)
cv2.imshow("Press ESC on keyboard to exit. 2", resized_img)
k = cv2.waitKey(0)
if k == 27: # wait for ESC key to exit
cv2.destroyAllWindows()
elif k == ord('s'): # wait for 's' key to save and exit
image_name = "%s/%s.jpg" % (tmp_directory, "resized_image")
cv2.imwrite(image_name, resized_img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
image_name = "%s/%s.jpg" % (tmp_directory, "source_image")
cv2.imwrite(image_name, img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
# When everything done, release the capture
cv2.destroyAllWindows()
if __name__ == '__main__':
image_file = "./tmp/000505.jpg"
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
print("os.environ['CUDA_VISIBLE_DEVICES']:", os.environ['CUDA_VISIBLE_DEVICES'])
inference(image_file)
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/image_resize_bilinear.py
os.environ['CUDA_VISIBLE_DEVICES']: 0
2019-08-13 21:34:17.188989: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2019-08-13 21:34:17.261320: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-08-13 21:34:17.261558: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties:
name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7335
pciBusID: 0000:01:00.0
totalMemory: 7.92GiB freeMemory: 7.42GiB
2019-08-13 21:34:17.261569: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1)
tf.shape(img): [1080 1920 3]
tf.shape(resized_img): [512 910 3]
img type: uint8
resized_img type: float32
QObject::moveToThread: Current thread (0x490ddf0) is not the object's thread (0x498c0b0).
Cannot move to target thread (0x490ddf0)
Process finished with exit code 0
new_h, new_w = tf.cond(tf.less(h, w),
true_fn=lambda: (target_shortside_len, target_shortside_len * w // h),
false_fn=lambda: (target_shortside_len * h // w, target_shortside_len)
)
h < w
tf.less(h, w)
returns the truth value of (x < y
) element-wise,tf.less(h, w) = True
.
true_fn
: The callable to be performed if pred is true.true_fn=lambda: (target_shortside_len, target_shortside_len * w // h)
,
h target_shortside_len = w target_longside_len target_shortside_len h = target_longside_len w target_shortside_len h ∗ w = target_longside_len begin{aligned} frac{h}{text{target_shortside_len}} = frac{w}{text{target_longside_len}}\ frac{text{target_shortside_len}}{h} = frac{text{target_longside_len}}{w}\ frac{text{target_shortside_len}}{h} * w = text{target_longside_len} end{aligned} target_shortside_lenh=target_longside_lenwhtarget_shortside_len=wtarget_longside_lenhtarget_shortside_len∗w=target_longside_len
h >= w
tf.less(h, w)
returns the truth value of (x < y
) element-wise,tf.less(h, w) = False
.
false_fn
: The callable to be performed if pred is false.false_fn=lambda: (target_shortside_len * h // w, target_shortside_len)
,
h target_longside_len = w target_shortside_len target_longside_len h = target_shortside_len w target_longside_len = target_shortside_len w ∗ h begin{aligned} frac{h}{text{target_longside_len}} = frac{w}{text{target_shortside_len}}\ frac{text{target_longside_len}}{h} = frac{text{target_shortside_len}}{w}\ text{target_longside_len} = frac{text{target_shortside_len}}{w} * h end{aligned} target_longside_lenh=target_shortside_lenwhtarget_longside_len=wtarget_shortside_lentarget_longside_len=wtarget_shortside_len∗h
最后
以上就是俊逸诺言为你收集整理的tf.image.resize_bilinear tf.image.resize_bilinear 的全部内容,希望文章能够帮你解决tf.image.resize_bilinear tf.image.resize_bilinear 所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复