zoukankan      html  css  js  c++  java
  • tensorflow 笔记

    1. matMul

    #-*-coding:utf-8 -*-

    import tensorflow as tf w1 = tf.Variable(tf.random_normal([2,3], stddev= 1, seed= 1)) w2 = tf.Variable(tf.random_normal([3,1], stddev= 1, seed= 1)) x = tf.constant([[0.7, 0.9]]) # 1×2 a = tf.matmul(x, w1) # 1×3 y = tf.matmul(a, w2) # 1×1 sess = tf.Session() # w1 w2 sess.run(w1.initializer) sess.run(w2.initializer) sess.run(y) print(y) sess.close()

    2. eval 函数 作用: 

    1.eval(): 将字符串string对象转化为有效的表达式参与求值运算返回计算结果
    2.eval()也是启动计算的一种方式。基于Tensorflow的基本原理,首先需要定义图,然后计算图,其中计算图的函数常见的有run()函数,如sess.run()。同样eval()也是此类函数,
    3.要注意的是,eval()只能用于tf.Tensor类对象,也就是有输出的Operation。对于没有输出的Operation, 可以用.run()或者Session.run();Session.run()没有这个限制。

    import tensorflow as tf
    
    x = tf.Variable(3, name="x")
    y = tf.Variable(4, name="y")
    z = tf.Variable(4, name="z")
    w = tf.Variable(4, name="w")
    f = x * y * z + 3 - w
    
    //单个初始化,变量增多,还真是个事
    with tf.Session() as sess:
        x.initializer.run()
        y.initializer.run()
        z.initializer.run()
        w.initializer.run()
        print(x)
        print(y)
        print(z)
        print(w)
       //f fuction eval 方式和js的eval一样,作为方法函数执行
        result = f.eval()
     print(result)

    3.   矩阵初始化 :

    w1 = tf.constant([[1,1,1],[2,2,2]],tf.float32)
    w2 = tf.constant([[3],[3],[3]],tf.float32)

    4. 使用GPU计算

    g = tf.Graph()
    with g.device('/gpu:0'):
        a = tf.matmul(x, w1) # 1×3
        y = tf.matmul(a, w2) # 1×1

    5.变量     

    weights = tf.Variable(tf.random_normal([2,3], stddev = 2))// 产生2行3列  标准差为2 的变量
    biases = tf.Variable(tf.zeros([3]) //初值为0 , 含有3个元素的变量

    //使用w2变量初值来初始化w3
    w2 = tf.Variable(weights.initialized_value())
    w3 = tf.Varibale(weights.initialized_value() * 2.0)

    符号变量

     #定义‘符号’变量,也称为占位符
     a = tf.placeholder("float")
     b = tf.placeholder("float")

    6 tensorflow 训练神经网络

    # 定义损失函数
    cross_entropy = -tf.reduce_mean(y_ * tf/log(tf.clip_by_value(y, 1e-10, 1.0)))   //  -y_ln(y)
    # 定义学习率
    learning_rate = 0.01
    #定义反响传播算法
    train_step = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy)  //使损失函数最小

    7. 完整的神经网络训练程序

    # -*- coding: utf-8 -*-
    """
    Created on Thu Apr 12 15:58:38 2018
    
    @author: 无尾君
    """
    
    import tensorflow as tf
    from numpy.random import RandomState
    
    batch_size = 8
    
    w1 = tf.Variable(tf.random_normal([2,3], stddev= 1, seed= 1))
    w2 = tf.Variable(tf.random_normal([3,1], stddev= 1, seed= 1))
    
    x = tf.placeholder(tf.float32, shape= (None,2), name= 'x-input')
    y_ = tf.placeholder(tf.float32, shape= (None,1), name= 'y-input')
    
    a = tf.matmul(x, w1)
    y = tf.matmul(a, w2)
    
    cross_entropy = -tf.reduce_mean(y_ * tf.log(tf.clip_by_value(y, 1e-10, 1.0)))
    train_step = tf.train.AdamOptimizer(0.001).minimize(cross_entropy)
    
    rdm = RandomState(1)
    dataset_size = 128
    X = rdm.rand(dataset_size, 2)
    Y = [[int(x1+ x2 < 1)] for (x1, x2) in X]
    
    with tf.Session() as sess:
        tf.global_variables_initializer().run()
        print(sess.run(w1))
        print(sess.run(w2))
    
        STEPS = 5000
        for i in range(STEPS):
            start = (i * batch_size) % dataset_size
            end = min(start + batch_size, dataset_size)
    
            sess.run(train_step, feed_dict = {x: X[start:end], y_: Y[start:end]})
            if i % 1000 ==0:
                total_cross_entropy = sess.run(cross_entropy, feed_dict = {x: X, y_: Y})
                print("After %d training step(s), cross entropy on all data is %g" % (i, total_cross_entropy))

    8 expanddims  :

    您可以使用expand_dims(image,0)使其成为1个图像,这将使形状[1,高度,宽度,通道]。

    9 运行会话

     #运行会话,输入数据,并计算节点,同时打印结果
     sess.run(y, feed_dict={a: 3, b: 3})

    10  算术

     

    操作描述
    tf.add(x, y, name=None) 求和
    tf.sub(x, y, name=None) 减法
    tf.mul(x, y, name=None) 乘法
    tf.div(x, y, name=None) 除法
    tf.mod(x, y, name=None) 取模
    tf.abs(x, name=None) 求绝对值
    tf.neg(x, name=None) 取负 (y = -x).
    tf.sign(x, name=None) 返回符号 y = sign(x) = -1 if x < 0; 0 if x == 0; 1 if x > 0.
    tf.inv(x, name=None) 取反
    tf.square(x, name=None) 计算平方 (y = x * x = x^2).
    tf.round(x, name=None) 舍入最接近的整数
    # ‘a’ is [0.9, 2.5, 2.3, -4.4]
    tf.round(a) ==> [ 1.0, 3.0, 2.0, -4.0 ]
    tf.sqrt(x, name=None) 开根号 (y = sqrt{x} = x^{1/2}).
    tf.pow(x, y, name=None) 幂次方 
    # tensor ‘x’ is [[2, 2], [3, 3]]
    # tensor ‘y’ is [[8, 16], [2, 3]]
    tf.pow(x, y) ==> [[256, 65536], [9, 27]]
    tf.exp(x, name=None) 计算e的次方
    tf.log(x, name=None) 计算log,一个输入计算e的ln,两输入以第二输入为底
    tf.maximum(x, y, name=None) 返回最大值 (x > y ? x : y)
    tf.minimum(x, y, name=None) 返回最小值 (x < y ? x : y)
    tf.cos(x, name=None) 三角函数cosine
    tf.sin(x, name=None) 三角函数sine
    tf.tan(x, name=None) 三角函数tan
    tf.atan(x, name=None) 三角函数ctan

    11. transpose

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


    Transpose(root.WithOpName("transpose"), dived, { 0,2,1,3 });

    12:   tensorflow C++ API 示例

    这个貌似有问题  运行崩溃

    #include <iostream>
    #include <map>
    
    #include "tensorflow/cc/ops/const_op.h"
    #include "tensorflow/cc/ops/image_ops.h"
    #include "tensorflow/cc/ops/standard_ops.h"
    #include "tensorflow/core/framework/graph.pb.h"
    #include "tensorflow/core/framework/tensor.h"
    #include "tensorflow/core/graph/default_device.h"
    #include "tensorflow/core/graph/graph_def_builder.h"
    #include "tensorflow/core/lib/core/errors.h"
    #include "tensorflow/core/lib/core/stringpiece.h"
    #include "tensorflow/core/lib/core/threadpool.h"
    #include "tensorflow/core/lib/io/path.h"
    #include "tensorflow/core/lib/strings/stringprintf.h"
    #include "tensorflow/core/platform/init_main.h"
    #include "tensorflow/core/platform/logging.h"
    #include "tensorflow/core/platform/types.h"
    #include "tensorflow/core/public/session.h"
    #include "tensorflow/core/util/command_line_flags.h"
    
    using namespace std ;
    using namespace tensorflow;
    using tensorflow::Flag;
    using tensorflow::Tensor;
    using tensorflow::Status;
    using tensorflow::string;
    using tensorflow::int32;
    
    map<int,string> int2char;
    string s = "KDA0123456789 ";
    for(int i=0;i<s.size();i++){
        int2char[i]=s[i];
    }
    
    //从文件名中读取数据
    Status ReadTensorFromImageFile(string file_name, const int input_height,
                                   const int input_width,
                                   vector<Tensor>* out_tensors) {
        auto root = Scope::NewRootScope();
        using namespace ops;
    
        auto file_reader = ops::ReadFile(root.WithOpName("file_reader"),file_name);
        const int wanted_channels = 1;
        Output image_reader;
        std::size_t found = file_name.find(".png");
        //判断文件格式
        if (found!=std::string::npos) {
            image_reader = DecodePng(root.WithOpName("png_reader"), file_reader,DecodePng::Channels(wanted_channels));
        } 
        else {
            image_reader = DecodeJpeg(root.WithOpName("jpeg_reader"), file_reader,DecodeJpeg::Channels(wanted_channels));
        }
        // 下面几步是读取图片并处理
        auto float_caster =Cast(root.WithOpName("float_caster"), image_reader, DT_FLOAT);
        auto dims_expander = ExpandDims(root, float_caster, 0);
        auto resized = ResizeBilinear(root, dims_expander,Const(root.WithOpName("resize"), {input_height, input_width}));
        // Div(root.WithOpName(output_name), Sub(root, resized, {input_mean}),{input_std});
        Transpose(root.WithOpName("transpose"),resized,{0,2,1,3});
    
        GraphDef graph;
        root.ToGraphDef(&graph);
    
        unique_ptr<Session> session(NewSession(SessionOptions()));
        session->Create(graph);
        session->Run({}, {"transpose"}, {}, out_tensors);//Run,获取图片数据保存到Tensor中
    
        return Status::OK();
    }
    
    int main(int argc, char* argv[]) {
    
        string graph_path = "aov_crnn.pb";
        GraphDef graph_def;
        //读取模型文件
        if (!ReadBinaryProto(Env::Default(), graph_path, &graph_def).ok()) {
            cout << "Read model .pb failed"<<endl;
            return -1;
        }
    
        //新建session
        unique_ptr<Session> session;
        SessionOptions sess_opt;
        sess_opt.config.mutable_gpu_options()->set_allow_growth(true);
        (&session)->reset(NewSession(sess_opt));
        if (!session->Create(graph_def).ok()) {
            cout<<"Create graph failed"<<endl;
            return -1;
        }
    
        //读取图像到inputs中
        int input_height = 40;
        int input_width = 240;
        vector<Tensor> inputs;
        // string image_path(argv[1]);
        string image_path("test.jpg");
        if (!ReadTensorFromImageFile(image_path, input_height, input_width,&inputs).ok()) {
            cout<<"Read image file failed"<<endl;
            return -1;
        }
    
        vector<Tensor> outputs;
        string input = "inputs_sq";
        string output = "results_sq";//graph中的输入节点和输出节点,需要预先知道
    
        pair<string,Tensor>img(input,inputs[0]);
        Status status = session->Run({img},{output}, {}, &outputs);//Run,得到运行结果,存到outputs中
        if (!status.ok()) {
            cout<<"Running model failed"<<endl;
            cout<<status.ToString()<<endl;
            return -1;
        }
    
    
        //得到模型运行结果
        Tensor t = outputs[0];        
        auto tmap = t.tensor<int64, 2>(); 
        int output_dim = t.shape().dim_size(1); 
    
    
        //预测结果解码为字符串
        string res="";
        for (int j = 0; j < output_dim; j++) {
            res+=int2char[tmap(0,j)];
        }
        cout<<res<<endl;
    
        return 0;
    }
    View Code

    这个才是对的

    #include <fstream>
    #include <utility>
    #include <vector>
    // #include <Eigen/Core>
    // #include <Eigen/Dense>
    
    #include "tensorflow/cc/ops/const_op.h"
    #include "tensorflow/cc/ops/image_ops.h"
    #include "tensorflow/cc/ops/standard_ops.h"
    #include "tensorflow/core/framework/graph.pb.h"
    #include "tensorflow/core/framework/tensor.h"
    #include "tensorflow/core/graph/default_device.h"
    #include "tensorflow/core/graph/graph_def_builder.h"
    #include "tensorflow/core/lib/core/errors.h"
    #include "tensorflow/core/lib/core/stringpiece.h"
    #include "tensorflow/core/lib/core/threadpool.h"
    #include "tensorflow/core/lib/io/path.h"
    #include "tensorflow/core/lib/strings/stringprintf.h"
    #include "tensorflow/core/platform/env.h"
    #include "tensorflow/core/platform/init_main.h"
    #include "tensorflow/core/platform/logging.h"
    #include "tensorflow/core/platform/types.h"
    #include "tensorflow/core/public/session.h"
    #include "tensorflow/core/util/command_line_flags.h"
    
    
    using tensorflow::Flag;
    using tensorflow::Tensor;
    using tensorflow::Status;
    using tensorflow::string;
    using tensorflow::int32;
    
    
    
    string model_path = "/work/dl/mosaic/keras/mobilenetv2/trans_model/mosaic.mobilenet_v2_20190425_448X448_DSPD_afew.20.pb";
    
    
    const std::string class_name[2] = {
        "dummy",
        "kit fox" };
    // Given an image file name, read in the data, try to decode it as an image,
    // resize it to the requested size, and then scale the values as desired.
    Status ReadTensorFromImageFile(string file_name, const int input_height,
        const int input_width, const float input_mean,
        const float input_std,
        std::vector<Tensor>* out_tensors) {
        auto root = tensorflow::Scope::NewRootScope();
        using namespace ::tensorflow::ops;  // NOLINT(build/namespaces)
    
        string input_name = "file_reader";
        string output_name = "normalized";
        auto file_reader = tensorflow::ops::ReadFile(root.WithOpName(input_name),
            file_name);
        // Now try to figure out what kind of file it is and decode it.
        const int wanted_channels = 3;
        tensorflow::Output image_reader;
        //if (tensorflow::StringPiece(file_name).ends_with(".png")) {
        //    image_reader = DecodePng(root.WithOpName("png_reader"), file_reader,
        //        DecodePng::Channels(wanted_channels));
        //}
        //else if (tensorflow::StringPiece(file_name).ends_with(".gif")) {
        //    image_reader = DecodeGif(root.WithOpName("gif_reader"), file_reader);
        //}
        //else 
        {
            // Assume if it's neither a PNG nor a GIF then it must be a JPEG.
            image_reader = DecodeJpeg(root.WithOpName("jpeg_reader"), file_reader,
                DecodeJpeg::Channels(wanted_channels));
        }
        // Now cast the image data to float so we can do normal math on it.
        auto float_caster =
            Cast(root.WithOpName("float_caster"), image_reader, tensorflow::DT_FLOAT);
        // The convention for image ops in TensorFlow is that all images are expected
        // to be in batches, so that they're four-dimensional arrays with indices of
        // [batch, height, width, channel]. Because we only have a single image, we
        // have to add a batch dimension of 1 to the start with ExpandDims().
        auto dims_expander = ExpandDims(root, float_caster, 0);
        // Bilinearly resize the image to fit the required dimensions.
        auto resized = ResizeBilinear(
            root, dims_expander,
            Const(root.WithOpName("size"), { input_height, input_width }));
        // Subtract the mean and divide by the scale.
        Div(root.WithOpName(output_name), Sub(root, resized, { input_mean }),
        { input_std });
    
        // This runs the GraphDef network definition that we've just constructed, and
        // returns the results in the output tensor.
        tensorflow::GraphDef graph;
        TF_RETURN_IF_ERROR(root.ToGraphDef(&graph));
    
        std::unique_ptr<tensorflow::Session> session(
            tensorflow::NewSession(tensorflow::SessionOptions()));
        TF_RETURN_IF_ERROR(session->Create(graph));
        TF_RETURN_IF_ERROR(session->Run({}, { output_name }, {}, out_tensors));
        return Status::OK();
    }
    
    int main(int argc, char* argv[]) {
    
        string graph_path = model_path;
        tensorflow::port::InitMain(argv[0], &argc, &argv);
    
        tensorflow::GraphDef graph_def;
        if (!ReadBinaryProto(tensorflow::Env::Default(), graph_path, &graph_def).ok()) {
            LOG(ERROR) << "Read proto";
            return -1;
        }
    
        std::unique_ptr<tensorflow::Session> session;
        tensorflow::SessionOptions sess_opt;
        sess_opt.config.mutable_gpu_options()->set_allow_growth(true);
        (&session)->reset(tensorflow::NewSession(sess_opt));
        if (!session->Create(graph_def).ok()) {
            LOG(ERROR) << "Create graph";
            return -1;
        }
    
        const int batch_size = argc - 1;
        if (batch_size != 1) {
            LOG(ERROR) << "Batch mode for the pretrained inception-v3 is unsupported";
            LOG(ERROR) << " - https://github.com/tensorflow/tensorflow/issues/554";
            return -1;
        }
    
        int32 input_dim = 448;
        float input_mean = 128;
        float input_std = 128;
        std::vector<Tensor> inputs;
        std::string image_path(argv[1]);
        if (!ReadTensorFromImageFile(image_path, input_dim, input_dim, input_mean,
            input_std, &inputs).ok()) {
            LOG(ERROR) << "Load image";
            return -1;
        }
    
        std::vector<Tensor> outputs;
        //string input_layer = "Mul";
        //string output_layer = "softmax";
        string input_layer = "input_1";
        string output_layer = "dense_2/Softmax";
    
        if (!session->Run({ { input_layer, inputs[0] } },
        { output_layer }, {}, &outputs).ok()) {
            LOG(ERROR) << "Running model failed";
            return -1;
        }
    
    
        printf("Is predicting ... 
    ");
        //得到模型运行结果
        Tensor t = outputs[0];
        auto tmap = t.tensor<float, 2>();
        for (int i = 0; i != 2; ++i) {
            printf("p[%d]=%.2f
    ", i, tmap(0, i));
        }
        int output_dim = t.shape().dim_size(1);
        for (int j = 0; j < output_dim; j++) {
            tmap(0, j);
        }
        printf("predicting  OK 
    ");
    
        //Eigen::Map<Eigen::VectorXf> pred(outputs[0].flat<float>().data(),
        //    outputs[0].NumElements());
        //int maxIndex; float maxValue = pred.maxCoeff(&maxIndex);
        //LOG(INFO) << "P( " << class_name[maxIndex] << " | image ) = " << maxValue;
    
        return 0;
    }
    View Code

    编译 :

    g++ -g -D_GLIBCXX_USE_CXX11_ABI=0 tf_predict.cpp -o tf_predict -I /usr/include/eigen3 -I /usr/local/include/tf  -L/usr/local/lib/ `pkg-config --cflags --libs protobuf`  -ltensorflow_cc  -ltensorflow_framework

    使用Opencv 读取tensor

    tensorflow::Tensor readTensor(string filename){
        tensorflow::Tensor input_tensor(DT_FLOAT,TensorShape({1,240,40,1}));
        Mat src=imread(filename,0);
        Mat dst;
        resize(src,dst,Size(240,40));//resize
        Mat dst_transpose=dst.t();//transpose
    
        auto tmap=input_tensor.tensor<float,4>();
    
        for(int i=0;i<240;i++){//Mat复制到Tensor
            for(int j=0;j<40;j++){
                tmap(0,i,j,0)=dst_transpose.at<uchar>(i,j);
            }
        }
    
        return input_tensor;
    }

    13  tensorflow 取指针

    float* p = input_tensor.flat<float>().data()
  • 相关阅读:
    qt忙等与非忙等
    获得文件路径 _pgmptr, _makepath, _splitpath
    RGB2YCbCr RGB2Gray
    qt Cannot connect creator comm socket /tmp/qt_temp.S26613/stub-socket: No such
    64位Ubuntu系统安装OpenCV 2.4.x+ffmpeg 完美解决方案
    vim按下ctrl+s僵死
    win32程序应用mfc库
    error LNK2005: _DllMain@12 已经在 dllmain.obj 中定义
    JavaScript中的浅拷贝和深拷贝
    Set和Map
  • 原文地址:https://www.cnblogs.com/luoyinjie/p/10875569.html
Copyright © 2011-2022 走看看