使用TensorFlow批量reshape图片

        最近需要把一些图片格式转成一样的,查找了一下大神们写的资料,用TensorFlow中的图片处理函数可以做reshape,于是就写了一段代码:读取一个文件夹中的所有图片,批量处理成同样格式的;

import tensorflow as tf
import os
import numpy as np
import matplotlib.pyplot as plt

path = 'Z:/jupyter_notebooks/icon_test'
files = os.listdir(path) #列出文件夹下所有的目录与文件
for file in files:#遍历文件夹;
    if not os.path.isdir(file): #判断是否是文件夹,不是文件夹才打开
        #你想对文件的操作
        image_raw_data = tf.gfile.FastGFile(path+"/"+file, 'rb').read()
        with tf.Session() as sess:
            img_data = tf.image.decode_png(image_raw_data)
            img_data.set_shape([1797, 2673, 3])
            #print(img_data.eval())
            print("文件名是 "+path+"/"+file)
            print('原图')
            plt.imshow(img_data.eval())
            plt.show() # 显示原图
 
            resized = tf.image.resize_images(img_data, [300,300],method=0)  
            #第一个参数为原始图像,第二个参数为图像大小,第三个参数给出了指定的算法
            resized = np.asarray(resized.eval(),dtype='uint8')
            plt.imshow(resized)
            print("变换后的图形")
            plt.show()
           
            encoded_image = tf.image.encode_png(resized)
            print("cnm")   # 中间老报错,贼气
            with tf.gfile.GFile("Z:/jupyter_notebooks/icon_new_test"+"/"+file, 'wb') as f:
                f.write(encoded_image.eval())

其中,path是图片所在的文件夹;最后倒数第二行的路径是我们要存放新的图片的路径;

新手上路,有错误请大家指正!

参考:https://blog.csdn.net/zsean/article/details/76383100

            https://blog.csdn.net/ZiTeng_Du/article/details/78305356?locationNum=5&fps=1

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,下面是使用TensorFlow.NET训练自己的数据集的例程: 1. 准备数据集 首先,需要准备自己的数据集。数据集应该包括训练集和测试集两部分,每个部分应该包括输入数据和标签。 2. 安装TensorFlow.NET 可以在官网下载最新版本的TensorFlow.NET,并按照官方文档进行安装。 3. 构建模型 使用TensorFlow.NET构建模型的方法与使用TensorFlow类似。可以使用Keras API或者直接调用TensorFlow.NET的API。 下面是一个使用Keras API构建模型的例子: ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TensorFlow; using Keras; using Keras.Layers; using Keras.Models; namespace TensorFlowNET.Examples { class Program { static void Main(string[] args) { var (x_train, y_train) = LoadData("train.csv"); var (x_test, y_test) = LoadData("test.csv"); var input = new Input(new TensorShape(28, 28)); var x = new Reshape(new int[] { 28 * 28 }).Apply(input); x = new Dense(128, activation: "relu").Apply(x); x = new Dense(10, activation: "softmax").Apply(x); var model = new Model(input, x); model.Compile(optimizer: "adam", loss: "categorical_crossentropy", metrics: new[] { "accuracy" }); model.Fit(x_train, y_train, batch_size: 128, epochs: 5, validation_split: 0.1); var score = model.Evaluate(x_test, y_test); Console.WriteLine($"Test loss: {score[0]}"); Console.WriteLine($"Test accuracy: {score[1]}"); } static (NDArray, NDArray) LoadData(string file) { // Load data from file return (x, y); } } } ``` 4. 训练模型 使用准备好的数据集和构建好的模型进行训练。可以使用模型的`Fit`方法进行批量训练。 ```csharp model.Fit(x_train, y_train, batch_size: 128, epochs: 5, validation_split: 0.1); ``` 5. 评估模型 在训练完成后,可以使用模型的`Evaluate`方法对模型进行评估。 ```csharp var score = model.Evaluate(x_test, y_test); Console.WriteLine($"Test loss: {score[0]}"); Console.WriteLine($"Test accuracy: {score[1]}"); ``` 以上就是使用TensorFlow.NET训练自己的数据集的例程。希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值