将张量转换为keras中的numpy数组 [英] Convert a tensor to numpy array in keras

查看:819
本文介绍了将张量转换为keras中的numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将形状(?,224,224,3)的张量转换为keras中的一个numpy数组.

I want to convert the tensor of shape (?,224,224,3) to a numpy array in keras.

Tensor("add_1/add_3:0", shape=(?, 224, 224, 3), dtype=float32)

我的基本CNN代码如下:

My basic CNN code is as below:

final_model = Model(input=[img_input], output=[act1,act2,act3,act4,act5])
addops(final_model)    

def addops(model):
        output = model.output
        factor = 2
        for i in range(len(output)):
                output[i] = UpSampling2D(size = (factor**i,factor**i),interpolation='bilinear')(output[i])
                output[i] = (ZeroPadding2D((1,1)))(output[i])
                output[i] = (Conv2D(3,(3,3)))(output[i])
        out = Add()([output[0],output[1],output[2],output[3],output[4]])
        print(out)
        sess = tf.InteractiveSession()
        outarray = tf.Variable(out).eval()
        print(outarray)

基本上,我正在采用预先训练的模型,并捕获一些中间层的特征图,并对它们进行转换以将其重塑为输入图像的大小.稍后,我将这些重塑后的特征图添加到单个张量中.我想将上述程序中名为 'out' 的张量转换为numpy数组.这是我正在尝试实施的项目的一部分,但我对此感到震惊.

Basically, I am taking a pretrained model and capturing the feature maps of a few intermediate layers and doing those transformations on them to reshape them to the size of the input image. Later, i am adding those reshaped feature maps to a single tensor. I want to convert this tensor which is named as 'out' in the above program to a numpy array. This is part of the project I am trying to implement but I am struck here.

运行此代码时,出现以下错误

When I run this code, I get the below error

ValueError: initial_value must have a shape specified: Tensor("add_1/add_3:0", shape=(?, 224, 224, 3), dtype=float32)

如果我不初始化会话并仅将张量 'out' 作为输出,则会编译程序.我尝试了其他几种方法,并提到了有关Stack Overflow的各种问题,但是没有一个问题给了我所需的结果.在上面的代码中将此张量 'out' 转换为numpy数组的最佳方法是什么.

The program gets compiled if I don't initialize the session and take just the tensor 'out' as output. I tried several other ways and have referred to various questions on Stack Overflow, but none of them gave me the required results. What is the best way to convert this tensor 'out' in the above code to a numpy array.

注意: :我想稍后将此numpy数组转换为图像.我很高兴知道是否有任何方法也可以将该张量直接转换为图像.

Note: I want to convert this numpy array to an image later. I am happy to know if there are any methods to directly convert this tensor to an image as well.

EDIT1 :我传递了一张图像以填充形状的initial_value,但是现在我看到了另一个错误,这基本上是将张量转换为numpy的问题数组.

EDIT1: I passed an image to fill in the initial_value in the shape, but now I see another error which is basically an issue with converting this tensor to a numpy array.

    def addops(model,image):
        image = load_img(image,target_size=(224,224))
        image = img_to_array(image)
        image = np.expand_dims(image,axis=0)
        val =model.predict(image)
#       print(val)
        output = val
        factor = 2
        for i in range(len(output)):
                output[i] = tf.convert_to_tensor(output[i],np.float32)
                output[i] = UpSampling2D(size = (factor**i,factor**i),interpolation='bilinear')(output[i])
                output[i] = (ZeroPadding2D((1,1)))(output[i])
                output[i] = (Conv2D(3,(3,3)))(output[i])
        out = Add()([output[0],output[1],output[2],output[3],output[4]])
        print(out)
        sess = tf.InteractiveSession()
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        outarray = tf.Variable(out).eval()
        print(outarray)

错误消息是:

    Using TensorFlow backend.
WARNING:tensorflow:From /home/ssindhu/deeplab_env/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
2019-09-23 02:39:52.684145: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-09-23 02:39:52.690019: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 3493110000 Hz
2019-09-23 02:39:52.692198: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x51c36f0 executing computations on platform Host. Devices:
2019-09-23 02:39:52.692254: I tensorflow/compiler/xla/service/service.cc:158]   StreamExecutor device (0): <undefined>, <undefined>
WARNING:tensorflow:From /home/ssindhu/deeplab_env/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.
/home/ssindhu/hypercolumns.py:107: UserWarning: Update your `Model` call to the Keras 2 API: `Model(inputs=[<tf.Tenso..., outputs=[<tf.Tenso...)`
  final_model = Model(input=[img_input], output=[act1,act2,act3,act4,act5])
Frontend weights loaded.
Tensor("add_1/add_3:0", shape=(1, 224, 224, 3), dtype=float32)
Traceback (most recent call last):
  File "/home/ssindhu/deeplab_env/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1334, in _do_call
    return fn(*args)
  File "/home/ssindhu/deeplab_env/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1319, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "/home/ssindhu/deeplab_env/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1407, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value Variable
         [[{{node _retval_Variable_0_0}}]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "training.py", line 18, in <module>
    gethypercols(model,'JPEGImages/2007_000033.jpg')
  File "/home/ssindhu/hypercolumns.py", line 34, in gethypercols
    outarray = tf.Variable(out).eval()
  File "/home/ssindhu/deeplab_env/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 1695, in eval
    return self._variable.eval(session=session)
  File "/home/ssindhu/deeplab_env/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 695, in eval
    return _eval_using_default_session(self, feed_dict, self.graph, session)
  File "/home/ssindhu/deeplab_env/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 5181, in _eval_using_default_session
    return session.run(tensors, feed_dict)
  File "/home/ssindhu/deeplab_env/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 929, in run
    run_metadata_ptr)
  File "/home/ssindhu/deeplab_env/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1152, in _run
    feed_dict_tensor, options, run_metadata)
  File "/home/ssindhu/deeplab_env/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1328, in _do_run
    run_metadata)
  File "/home/ssindhu/deeplab_env/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1348, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value Variable
         [[{{node _retval_Variable_0_0}}]]

它表示我正在尝试使用未初始化的值变量.我已经看到我们必须在会话中为tensorflow初始化变量,所以我也包括了这些行,但是仍然看到相同的错误.我该怎么解决.

It says that I am trying to use an uninitialized value variable. I have seen that we have to initialize the variables in the session for tensorflow and so I have included those lines as well.But still I see the same error. How could I resolve this.

推荐答案

如果要计算张量的值(此处out是张量),则只需在会话中运行张量,就不需要从中创建一个变量.您只能在会话中运行out.

If you want to compute the value of a tensor (here out is a tensor), you just run the tensor in a session, there is no need to create a variable out of it. You can just run out in a session.

变量用于存储模型的可训练(和其他)参数.您要做的是创建一个新变量,应使用out对其进行初始化,然后在不初始化的情况下运行该变量.

Variables are for storing trainable (and other) parameters of your model. What you do is that you create a new variable that should be initialized with out and run it without initialization.

这篇关于将张量转换为keras中的numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆