Skip to content
Advertisement

How to convert Tensorflow dataset to 2D numpy array

I have a TensorFlow dataset which contains nearly 15000 multicolored images with 168*84 resolution and a label for each image. Its type and shape are like this:

< ConcatenateDataset shapes: ((168, 84, 3), ()), types: (tf.float32, tf.int32)>

I need to use it to train my network. That’s why I need to pass it as a parameter to this function that I built my layers in:

def cnn_model_fn(features, labels, mode):

  input_layer = tf.reshape(features["x"], [-1, 168, 84, 3])
  # Convolutional Layer #1
  conv1 = tf.layers.conv2d(
     inputs=input_layer,
     filters=32,
     kernel_size=[5, 5],
     padding="same",
     activation=tf.nn.relu)
.
.
.

I tried to convert each tensor into np.array(which is the proper type for the function above, i guess) by using tf.eval() and np.ravel(). But I failed.

So, how can I convert this dataset into the proper type to pass it to the function?

Plus

I am new to python and tensorflow and I don’t think I understand why there are datasets if we can not use them directly to build layers (I am following the tutorial in TensorFlow’s website btw).

Thanks.

Advertisement

Answer

It doesn’t sound like you set up things using the Tensorflow Dataset pipeline, here is the guide for doing so:

https://www.tensorflow.org/programmers_guide/datasets

You can either follow that (it’s the right approach, but there’s a small learning curve to get used to it), or you can just pass in the numpy array to sess.run as part of the feed_dict parameter. If you go this way then you should just create a tf.placeholder which will be populated by the value in feed_dict. Many of the basic tutorial examples here follow this approach:

https://github.com/aymericdamien/TensorFlow-Examples

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement