【问题标题】:Exporting tensorflow to matlab将张量流导出到matlab
【发布时间】:2019-03-07 04:03:08
【问题描述】:

我正在尝试从通过调整此示例 https://github.com/decentralion/tf-dev-summit-tensorboard-tutorial 获得的模型中导出权重和偏差值。

首先我想我只是在屏幕上打印变量并将它们复制到一个 excel 文件中,然后将它们导出为 .csv 文件以便我可以在 MATLAB 中使用它们。

但由于权重太多,这不是一个可行的选择。所以我改为使用 numpy 将它们保存为 .csv 文件。一切正常,但是当我在 Matlab 中运行该模型时,它似乎不起作用。当然,我的 Matlab 代码有可能出错。

但我注意到我的代码打印的值与写入 .csv 文件的值不同。

由于我是 tensorflow 和 python 的新手,我通过将不同的示例“拼接”在一起来编写代码。

这里是张量流代码:

#BREZ KONVOLUCIJE

import os
import os.path
import shutil
import tensorflow as tf
import numpy as np

LOGDIR =  "/home/ubuntu/ml/tf-dev-summit-tensorboard-tutorial-master/mnist_NOCONV/"
LABELS = os.path.join(os.getcwd(), "labels_1024.tsv")
SPRITES = os.path.join(os.getcwd(), "sprite_1024.png")
### MNIST EMBEDDINGS ###
mnist = tf.contrib.learn.datasets.mnist.read_data_sets(train_dir=LOGDIR + "data", one_hot=True)
### Get a sprite and labels file for the embedding projector ###

if not (os.path.isfile(LABELS) and os.path.isfile(SPRITES)):
    print("Necessary data files were not found. Run this command from inside the "
    "repo provided at "
    "https://github.com/dandelionmane/tf-dev-summit-tensorboard-tutorial.")
    exit(1)


def fc_layer(input, size_in, size_out, name="fc"):
    with tf.name_scope(name):
        w = tf.Variable(tf.truncated_normal([size_in, size_out], stddev=0.1), name="W")
        b = tf.Variable(tf.constant(0.1, shape=[size_out]), name="B")
        act = tf.matmul(input, w) + b
        tf.summary.histogram("weights", w)
        tf.summary.histogram("biases", b)
        tf.summary.histogram("activations", act)
        return act


def mnist_model(learning_rate, use_two_fc, use_two_conv, hparam):
    tf.reset_default_graph()
    sess = tf.Session()

    # Setup placeholders, and reshape the data
    x = tf.placeholder(tf.float32, shape=[None, 784], name="x")
    x_image = tf.reshape(x, [-1, 28, 28, 1])
    tf.summary.image('input', x_image, 10)
    y = tf.placeholder(tf.float32, shape=[None, 10], name="labels")


    # FC layer 1
    w1 = tf.Variable(tf.truncated_normal([784, 1024], stddev=0.1), name="W1")
    b1 = tf.Variable(tf.constant(0.1, shape=[1024]), name="B1")
    act1 = tf.matmul(x, w1) + b1
    tf.summary.histogram("weights", w1)
    tf.summary.histogram("biases", b1)
    tf.summary.histogram("activations", act1)
    relu = tf.nn.relu(act1)
    embedding_input = relu
    embedding_size = 1024
    tf.summary.histogram("fc1/relu", relu)

    # FC layer 2
    w2 = tf.Variable(tf.truncated_normal([1024, 10], stddev=0.1), name="W2")
    b2 = tf.Variable(tf.constant(0.1, shape=[10]), name="B2")
    logits = tf.matmul(relu, w2) + b2
    tf.summary.histogram("weights", w2)
    tf.summary.histogram("biases", b2)
    tf.summary.histogram("activations", logits)



    #if use_two_fc:
     #   fc1 = fc_layer(x, 784, 1024, "fc1")
      #  relu = tf.nn.relu(fc1)
       # embedding_input = relu
        #tf.summary.histogram("fc1/relu", relu)
       # embedding_size = 1024
        #logits = fc_layer(relu, 1024, 10, "fc2")

    with tf.name_scope("xent"):
        xent = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(
                logits=logits, labels=y), name="xent")
        tf.summary.scalar("xent", xent)

    with tf.name_scope("train"):
        train_step = tf.train.AdamOptimizer(learning_rate).minimize(xent)

    with tf.name_scope("accuracy"):
        correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
        tf.summary.scalar("accuracy", accuracy)

    summ = tf.summary.merge_all()


    embedding = tf.Variable(tf.zeros([1024, embedding_size]), name="test_embedding")
    assignment = embedding.assign(embedding_input)
    saver = tf.train.Saver()

    sess.run(tf.global_variables_initializer())
    writer = tf.summary.FileWriter(LOGDIR + hparam)
    writer.add_graph(sess.graph)

    config = tf.contrib.tensorboard.plugins.projector.ProjectorConfig()
    embedding_config = config.embeddings.add()
    embedding_config.tensor_name = embedding.name
    embedding_config.sprite.image_path = SPRITES
    embedding_config.metadata_path = LABELS
    # Specify the width and height of a single thumbnail.
    embedding_config.sprite.single_image_dim.extend([28, 28])
    tf.contrib.tensorboard.plugins.projector.visualize_embeddings(writer, config)

    for i in range(2001):
        batch = mnist.train.next_batch(100)
        if i % 5 == 0:
            [train_accuracy, s] = sess.run([accuracy, summ], feed_dict={x: batch[0], y: batch[1]})
            writer.add_summary(s, i)
        if i % 500 == 0:
            sess.run(assignment, feed_dict={x: mnist.test.images[:1024], y: mnist.test.labels[:1024]})
            saver.save(sess, os.path.join(LOGDIR, "model.ckpt"), i)
        sess.run(train_step, feed_dict={x: batch[0], y: batch[1]})


    # Get the values of variables
    w1_val, b1_val, w2_val, b2_val = sess.run([w1, b1, w2, b2])
    np.savetxt("w1.csv", w1_val, delimiter=",")
    np.savetxt("b1.csv", b1_val, delimiter=",")
    np.savetxt("w2.csv", w2_val, delimiter=",")
    np.savetxt("b2.csv", b2_val, delimiter=",")

    tvars = tf.trainable_variables()
    tvars_vals = sess.run(tvars)
    print("\n \n \n")
    for var, val in zip(tvars, tvars_vals):
        print(var.name, val)  # Prints the name of the variable alongside its value.

    print("\n \n \n")

def main():
    # You can try adding some more learning rates
    learning_rate = 1E-4

    use_two_fc = True
    hparam = "OCR_2FC_NOCONV"
    print('Starting run for %s' % hparam)

    # Actually run with the new settings
    mnist_model(learning_rate, use_two_fc, False, hparam)

    print('Done training!')
    print('Run `tensorboard --logdir=%s --host localhost --port 8088` to see the results.' % LOGDIR)

if __name__ == '__main__':
    main()

还有 MATLAB 代码:

% This is a simple matlab representation of a mnist OCR system.
% Grayscale images are saved in a .csv format (as 2d arrays) in the 
% folder '/slike'
clear

% Import the image
IMAGE = csvread("slike\img0.csv");
% Visualize it
image(IMAGE);

% Import the weights
w1 = csvread("mnist_NOCONV\w1.csv");
b1 = csvread("mnist_NOCONV\b1.csv");
w2 = csvread("mnist_NOCONV\w2.csv");
b2 = csvread("mnist_NOCONV\b2.csv");

% Unroll image to use it as an input vector
x = transpose(IMAGE(:));

% Fully connected layer 1
act1 = x*w1 + transpose(b1);
relu = ReLU(act1);

% Fully connected layer 2
act2 = relu*w2 + transpose(b2);
res  = softmax(act2);
act2
res

matlab中的softmax函数:

function f = softmax(X)
    assert(size(X,1) == 1); % Should be a vector
    exps = exp(X-max(X));   % we add "-max(X)" to prevent overflow
    f = exps/sum(exps); 
end

以及ReLU函数:

% ReLU activivation function. We will need this later.
function f = ReLU(X)
    f = arrayfun(@(x) ReLU0D(x),X);
end

%ReLU activation function for a scalar
function f = ReLU0D(x)
    if x < 0
        f = 0;
    else
        f = x;
    end
end

结果是 Matlab 代码的行为应有尽有(即 res 总和为 1),但大多数时候它只是得到错误的答案。但准确度应该在 95% 左右(以 tensorflow 衡量)。那我做错了什么?

编辑:在 Matlab 中可视化图片效果很好。

编辑:我正在添加我为制作 .csv 图片而编写的代码

import gzip
import numpy as np

f = gzip.open('train-images-idx3-ubyte.gz','r')

image_size = 28
num_images = 20

f.read(16)
buf = f.read(image_size * image_size * num_images)
data = np.frombuffer(buf, dtype=np.uint8).astype(np.float32)
data = data.reshape(num_images, image_size, image_size, 1)


for i in range(num_images):
    string = 'img'
    string +=str(i)
    string +=".csv"
    image = np.asarray(data[i]).squeeze()
    image= np.int32(image)
    np.savetxt(string, image, delimiter=",")

【问题讨论】:

  • 您是否尝试过使用 h5py 或 deepdish 将数据保存为 HDF5?
  • 我没有。我现在就试试这个。
  • 我已经试过了,但结果和 .csv 文件一样。

标签: python matlab tensorflow machine-learning


【解决方案1】:

我发现了我的错误。我以错误的方式注册图像。 我在matlab脚本中添加了一行代码:

% This is a simple matlab representation of a mnist OCR system.
% Grayscale images are saved in a .csv format (as 2d arrays) in the 
% folder '/slike'
clear

% Import the image
IMAGE = csvread("slike\img7.csv");
% Visualize it
image(IMAGE);

IMAGE = transpose(IMAGE); % -- ADDED THIS LINE -- !

% Unroll image to use it as an input vector
x = IMAGE(:);

% Import the weights
w1 = h5read('mnist_NOCONV\coefficients.h5', '/w1');
b1 = h5read('mnist_NOCONV\coefficients.h5', '/b1');
w2 = h5read('mnist_NOCONV\coefficients.h5', '/w2');
b2 = h5read('mnist_NOCONV\coefficients.h5', '/b2');

% Fully connected layer 1
act1 = w1*x + b1;
relu = ReLU(act1);

% Fully connected layer 2
act2 = w2*relu + b2;
res  = softmax(act2);
act2
res

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-10
    • 2017-11-05
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    相关资源
    最近更新 更多