aymericdamien / TensorFlow-Examples

TensorFlow Tutorial and Examples for Beginners (support TF v1 & v2)
Other
43.44k stars 14.94k forks source link

convert tensor object to numpy #363

Closed shoriuchi1970 closed 4 years ago

shoriuchi1970 commented 4 years ago

Hi. everyone

Please tell me about How to convert tensor object to numpy array in keras programing for deep-learning.

Now I make the following programing code of semating segmentation with python on keras-tensorflow platform. Running the model fit and get the model output, I want to convert tensor object corresponding to model output to numpy array. But there is the following error.

Error:

Epoch 1/5 1/10 [==>...........................] - ETA: 11s - loss: 2.7155 - 5/10 [==============>...............] - ETA: 1s - loss: 2.8642 - a 9/10 [==========================>...] - ETA: 0s - loss: 2.7524 - a10/10 [==============================] - 2s 172ms/step - loss: 2.7148 - accuracy: 0.0419 - val_loss: 1.7582 - val_accuracy: 0.1053 Epoch 2/5 1/10 [==>...........................] - ETA: 0s - loss: 2.8022 - a 5/10 [==============>...............] - ETA: 0s - loss: 2.6346 - a 9/10 [==========================>...] - ETA: 0s - loss: 2.5909 - a10/10 [==============================] - 0s 19ms/step - loss: 2.6262 - accuracy: 0.0519 - val_loss: 2.4169 - val_accuracy: 0.1439 Epoch 3/5 1/10 [==>...........................] - ETA: 0s - loss: 2.5856 - a 4/10 [===========>..................] - ETA: 0s - loss: 2.6392 - a 5/10 [==============>...............] - ETA: 0s - loss: 2.6724 - a 7/10 [====================>.........] - ETA: 0s - loss: 2.7038 - a 8/10 [=======================>......] - ETA: 0s - loss: 2.7045 - a 9/10 [==========================>...] - ETA: 0s - loss: 2.6891 - a10/10 [==============================] - 0s 41ms/step - loss: 2.6768 - accuracy: 0.0811 - val_loss: 2.1290 - val_accuracy: 0.3055 Epoch 4/5 1/10 [==>...........................] - ETA: 0s - loss: 2.7163 - a 2/10 [=====>........................] - ETA: 0s - loss: 2.6762 - a 4/10 [===========>..................] - ETA: 0s - loss: 2.6826 - a 5/10 [==============>...............] - ETA: 0s - loss: 2.6591 - a 7/10 [====================>.........] - ETA: 0s - loss: 2.5913 - a 8/10 [=======================>......] - ETA: 0s - loss: 2.5808 - a 9/10 [==========================>...] - ETA: 0s - loss: 2.6193 - a10/10 [==============================] - 1s 52ms/step - loss: 2.6087 - accuracy: 0.0908 - val_loss: 2.1647 - val_accuracy: 0.5914 Epoch 5/5 1/10 [==>...........................] - ETA: 0s - loss: 2.5523 - a 2/10 [=====>........................] - ETA: 0s - loss: 2.5476 - a 3/10 [========>.....................] - ETA: 0s - loss: 2.4632 - a 4/10 [===========>..................] - ETA: 0s - loss: 2.5363 - a 6/10 [=================>............] - ETA: 0s - loss: 2.4754 - a 8/10 [=======================>......] - ETA: 0s - loss: 2.5135 - a10/10 [==============================] - 1s 51ms/step - loss: 2.5008 - accuracy: 0.1407 - val_loss: 2.2194 - val_accuracy: 0.6505 Traceback (most recent call last): File "/home/shoriuchi/.local/lib/python3.6/site-packages/tensorflow_core/python/client/session.py", line 1365, in _do_call return fn(*args) File "/home/shoriuchi/.local/lib/python3.6/site-packages/tensorflow_core/python/client/session.py", line 1350, in _run_fn target_list, run_metadata) File "/home/shoriuchi/.local/lib/python3.6/site-packages/tensorflow_core/python/client/session.py", line 1443, in _call_tf_sessionrun run_metadata) tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'input_1' with dtype float and shape [?,32,32,3] [[{{node input_1}}]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "train.py", line 49, in numout = K.get_value(output) File "/home/shoriuchi/.local/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2925, in get_value return x.eval(session=get_session()) File "/home/shoriuchi/.local/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 798, in eval return _eval_using_default_session(self, feed_dict, self.graph, session) File "/home/shoriuchi/.local/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 5407, in _eval_using_default_session return session.run(tensors, feed_dict) File "/home/shoriuchi/.local/lib/python3.6/site-packages/tensorflow_core/python/client/session.py", line 956, in run run_metadata_ptr) File "/home/shoriuchi/.local/lib/python3.6/site-packages/tensorflow_core/python/client/session.py", line 1180, in _run feed_dict_tensor, options, run_metadata) File "/home/shoriuchi/.local/lib/python3.6/site-packages/tensorflow_core/python/client/session.py", line 1359, in _do_run run_metadata) File "/home/shoriuchi/.local/lib/python3.6/site-packages/tensorflow_core/python/client/session.py", line 1384, in _do_call raise type(e)(node_def, op, message) tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'input_1' with dtype float and shape [?,32,32,3] [[node input_1 (defined at /home/shoriuchi/.local/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py:1748) ]]

Original stack trace for 'input_1': File "train.py", line 10, in model = model.unet() File "/home/shoriuchi/VOC2012/model.py", line 33, in unet input = Input((32, 32, 3)) File "/home/shoriuchi/.local/lib/python3.6/site-packages/keras/engine/input_layer.py", line 178, in Input input_tensor=tensor) File "/home/shoriuchi/.local/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper return func(*args, *kwargs) File "/home/shoriuchi/.local/lib/python3.6/site-packages/keras/engine/input_layer.py", line 87, in init name=self.name) File "/home/shoriuchi/.local/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 736, in placeholder shape=shape, ndim=ndim, dtype=dtype, sparse=sparse, name=name) File "/home/shoriuchi/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/backend.py", line 1051, in placeholder x = array_ops.placeholder(dtype, shape=shape, name=name) File "/home/shoriuchi/.local/lib/python3.6/site-packages/tensorflow_core/python/ops/array_ops.py", line 2619, in placeholder return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name) File "/home/shoriuchi/.local/lib/python3.6/site-packages/tensorflow_core/python/ops/gen_array_ops.py", line 6669, in placeholder "Placeholder", dtype=dtype, shape=shape, name=name) File "/home/shoriuchi/.local/lib/python3.6/site-packages/tensorflow_core/python/framework/op_def_library.py", line 794, in _apply_op_helper op_def=op_def) File "/home/shoriuchi/.local/lib/python3.6/site-packages/tensorflow_core/python/util/deprecation.py", line 507, in new_func return func(args, **kwargs) File "/home/shoriuchi/.local/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 3357, in create_op attrs, op_def, compute_device) File "/home/shoriuchi/.local/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 3426, in _create_op_internal op_def=op_def) File "/home/shoriuchi/.local/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 1748, in init self._traceback = tf_stack.extract_stack()

Programing code is written as follows:

import model import data_gen import callbacks import matplotlib.pyplot as plt import sys import tensorflow as tf import numpy as np from keras import backend as K

model = model.unet() model.summary()

train_gen = data_gen.trainGenerator('./VOC-train', batch_size=5) validation_gen = data_gen.trainGenerator('./VOC-test', batch_size=5)

callbacks_list = callbacks.callbacks_01('./cp')

history = model.fit_generator( generator=train_gen, steps_per_epoch=10, epochs=5,

callbacks=callbacks_list,

validation_data=validation_gen,
validation_steps=5

)

output = model.output

with tf.Session() as sess:

sess.run(tf.global_variables_initializer())

print(type(output.eval()))

my_session = K.get_session()

A = my_session.run(output)

A = output.eval(session=my_session)

A = K.eval(output)

print(A)

print(type(output.eval()))

numout = K.get_value(output) print(numout)

epochs=5

val_acc = history.history['val_accuracy'] val_loss = history.history['val_loss']

plt.rc('font',family='serif') fig = plt.figure() plt.plot(range(epochs), val_acc, label='acc', color='black') plt.xlabel('epochs') plt.savefig('semanticsegmantation.png')

Please tell me how to convert tensor object to numpy array.

I am sured that the basic program of converting tensor object to numpy array work well.

Basic program:

import tensorflow as tf import numpy as np from keras import backend as K

c = tf.constant([[1.0,2.0],[3.0,4.0]]) print(c) A = K.eval(c) print(A) print(A[1,1])

Dear, shun.

shoriuchi1970 commented 4 years ago

I miss the detail about the program, so I retry to make issues again.