tensorflow图像处理函数(1)

1、tensorflow中对jpeg格式图像的编码/解码函数:

import matplotlib.pyplot as plt
import tensorflow as tf
image_raw_data=tf.gfile.FastGFile(‘/Users/jk/Downloads/timg.jpeg‘,‘rb‘).read()
with tf.Session() as sess:
    img_data=tf.image.decode_jpeg(image_raw_data)    #通过tf.img.decode_jpeg函数对jpeg格式的图像进行解码,解码后的结果为一个张量
    print(img_data.eval())      #输出解码后的三维矩阵
    plt.imshow(img_data.eval())
    plt.show()
    img_data=tf.image.convert_image_dtype(img_data,dtype=tf.uint8)
    encode_image=tf.image.encode_jpeg(img_data)     #将图像的三维矩阵重新按照jpeg格式编码存入文件,打开该图像可以得到和原始图像一样的图像
    with tf.gfile.GFile(‘/Users/jk/Downloads/output‘,‘wb‘) as f:   #将文件写入目标路径,生成图像文件
        f.write(encode_image.eval())

2、图像大小调整(和上面的类似,仅多了图像大小调整的部分,下面的例子将类似):

import matplotlib.pyplot as plt
import tensorflow as tf
image_raw_data=tf.gfile.FastGFile(‘/Users/jk/Downloads/timg.jpeg‘,‘rb‘).read()
with tf.Session() as sess:
    img_data=tf.image.decode_jpeg(image_raw_data)
    print(img_data.eval())
    plt.imshow(img_data.eval())
    plt.show()
    img_data=tf.image.convert_image_dtype(img_data,dtype=tf.uint8)
    resized=tf.image.resize_images(img_data,size=[300,300],method=1)    #将图像大小转为[300,300],图像深度在没有明确设置前会是?,
    print(resized.get_shape())
    resized=tf.image.convert_image_dtype(resized,dtype=tf.uint8)     #数据预处理时候会把dtype转为tf.float32,因此需要手动转回tf.uint8
    encode_image=tf.image.encode_jpeg(resized)
    with tf.gfile.GFile(‘/Users/jk/Downloads/output‘,‘wb‘) as f:    #返回调整大小后的图像
        f.write(encode_image.eval())      

原文地址:https://www.cnblogs.com/xiaochouk/p/8525006.html

时间: 2024-08-07 09:21:42

tensorflow图像处理函数(1)的相关文章

如何用TensorFlow图像处理函数裁剪图像?

当给定大量不同质量的训练数据时,CNN往往能够很好地工作. –图像能够通过可视化的方式,传达复杂场景所蕴含的某种目标主题. –在Stanford Dogs数据集中,重要的是图像能够以可视化的方式,突出图片中狗的重要性. –一幅狗位于画面中心的图像,会被认为比狗作为背景的图像更有价值. 并非所有数据集都拥有最有价值的图像.下面所示的两幅图像,按照假设,该数据集本应突出不同的狗的品种 左图突出的是一条典型的墨西哥无毛犬的重要属性,而右图是两个参加聚会的人,在逗一条墨西哥无毛犬.右图中充斥了大量的无关

吴裕雄 python 神经网络——TensorFlow 图像处理函数

import numpy as np import tensorflow as tf import matplotlib.pyplot as plt image_raw_data = tf.gfile.FastGFile("F:\\TensorFlowGoogle\\201806-github\\datasets\\cat.jpg",'rb').read() with tf.Session() as sess: img_data = tf.image.decode_jpeg(image

『TensorFlow』函数查询列表_神经网络相关

神经网络(Neural Network) 激活函数(Activation Functions) 操作 描述 tf.nn.relu(features, name=None) 整流函数:max(features, 0) tf.nn.relu6(features, name=None) 以6为阈值的整流函数:min(max(features, 0), 6) tf.nn.elu(features, name=None) elu函数,exp(features) - 1 if < 0,否则featuresE

tensorflow l2_normalize函数

1.l2_normalize函数 tf.nn.l2_normalize(x, dim, epsilon=1e-12, name=None) 解释:这个函数的作用是利用 L2 范数对指定维度 dim 进行标准化. 比如,对于一个一维的张量,指定维度 dim = 0,那么计算结果为: output = x / sqrt( max( sum( x ** 2 ) , epsilon ) ) 假设 x 是多维度的,那么标准化只会独立的对维度 dim 进行,不会影响到别的维度. 2.tensorflow实现

tensorflow l2_loss函数

1.l2_loss函数 tf.nn.l2_loss(t, name=None) 解释:这个函数的作用是利用 L2 范数来计算张量的误差值,但是没有开方并且只取 L2 范数的值的一半,具体如下: output = sum(t ** 2) / 2 2.tensorflow实现 import tensorflow as tf a=tf.constant([1,2,3],dtype=tf.float32) b=tf.constant([[1,1],[2,2],[3,3]],dtype=tf.float3

tensorflow elu函数应用

1.elu函数 图像: 2.tensorflow elu应用 import tensorflow as tf input=tf.constant([0,-1,2,-3],dtype=tf.float32) output=tf.nn.elu(input) with tf.Session() as sess: print('input:') print(sess.run(input)) print('output:') print(sess.run(output)) sess.close() 输出结

tensorflow softsign函数应用

1.softsign函数 图像 2.tensorflow softsign应用 import tensorflow as tf input=tf.constant([0,-1,2,-30,30],dtype=tf.float32) output=tf.nn.softsign(input) with tf.Session() as sess: print('input:') print(sess.run(input)) print('output:') print(sess.run(output)

(原)tensorflow中函数执行完毕,显存不自动释放

转载请注明出处: http://www.cnblogs.com/darkknightzh/p/7608916.html 参考网址: https://stackoverflow.com/questions/39758094/clearing-tensorflow-gpu-memory-after-model-execution https://github.com/tensorflow/tensorflow/issues/1727#issuecomment-285815312s tensorflo

tensorflow常用函数解析

一.tf.transpose函数的用法 tf.transpose(input, [dimension_1, dimenaion_2,..,dimension_n]):这个函数主要适用于交换输入张量的不同维度用的,如果输入张量是二维,就相当是转置.dimension_n是整数,如果张量是三维,就是用0,1,2来表示.这个列表里的每个数对应相应的维度.如果是[2,1,0],就把输入张量的第三维度和第一维度交换. import numpy as np import tensorflow as tf A