Converting tensorflow dataset to numpy array - tensorflow

I have an autoencoder defined using tf.keras in tensorflow 1.15. I cannot upgrade to tensorflow to 2.0 for some specific reasons.
This particular autoencoder is used for anomaly detection. I currently compute the AUC score of the autoencoder as follows:
All anomalous inputs are labelled 1 and all normal inputs are labelled 0. This is y_true
I feed the autoencoder with unseen inputs and then measure the reconstruction error, like so: errors = np.mean(np.square(data - model.predict(data)), axis=-1)
The mean of this array is then said to the predicted label, y_pred.
I then compute the AUC using auc = metrics.roc_auc_score(y_true, y_pred).
This approach works well. I now need to move towards using tf.data.dataset to feed in my data. Previously, it was numpy arrays. The issue is, I am unable to convert tf.data.dataset to a numpy array and hence unable to compute the mean squared error as seen in 2.
Once I have a tf.data.Dataset, I feed it for prediction like so: results = model.predict(x_test)
This yields a numpy array, results. I want to compute the mean square error of results with x_test. However, x_test is of type tf.data.Dataset. So the question is, how can I convert a tf.data.dataset to a numpy array in tensorflow 1.15 or what is an alternative method to do this?

Related

Convert Tensorflow Tensor to Numpyarray

I have a class 'tensorflow.python.framework.ops.Tensor as output and need to convert this to a numpy array.
.numpy() doesn't work because it isn't a eagerTensor.
.eval doesn't work as well, because i'm using tensorflow >2.0
Is there any other way to fix this?
img_height=330
img_width=600
img_depth=23
save_model="saved_Models/wheatModel"
prediction_data_path=["data/stacked/MOD13Q1.A2017.2738.tif","data/stacked/MOD13Q1.A2017.889.tif","data/stacked/MOD13Q1.A2017.923.tif"]
prediction_data=dataConv.preparePredictionData(prediction_data_path)
prediction_reshaped=dataConv.reshapeFiles(prediction_data,img_width,img_height,img_depth)
x_ds =tf.stack(prediction_reshaped)
model = tf.keras.models.load_model(save_model)
model.predict(x_ds)
image=model.get_layer(name='prediction_image').output
n,output_width,output_height,output_depth,output_channels=image.shape
print(type(image))
image=tf.reshape(image,(output_width,output_height,output_depth))
print(type(image))
image.numpy()
So in the code above.
I load my trained model
predict the given images
get the output from the next to last layer
reshape this data
Now i want to convert this tensor to an numpyarray

How to convert y_true and y_pred from tensorflow 2 into numpy array?

I am trying to write a custom loss function to use in a Tensorflow 2 model.
def loss(y_true, y_pred):
1. wish to convert y_true and y_pred to numpy array (these are images)
2. After that I wish to carry out some operations (such as binarization of images, some pixel wise AND
or OR operation etc.)
3. Finally get a single floating number as a loss, and feed it to the model and minimize the loss.
can any one please suggest me how to do it? I already tried many of the options given on the internet but I am not able to convert y_true and y_pred into a numpy array.

Using kohannenkappa loss function in keras models

Trying to use non keras backend functions for custom loss calculation in keras models.
I am trying to make my keras cnn model use a custom loss function ( KAppa score). However since kappas is not defined in Keras backend , i need to used scikit-learn based kappa implementation. This sklearn function takes array of labels as the argument unlike keras backend functions which take tensors. The loss function call within keras mostly sends tensors Y_pred and Y_true. I did the implementation below using some quide i found online but I get errors .
import keras.backend as K
def cohen_kappa_score_func(y_true, y_pred):
sess = tf.Session()
with sess.as_default():
score = cohen_kappa_score(type(y_true.eval()),type(y_pred.eval()), weights='linear')#idea is to convert the tensor to array
sess.close()
return score
#use this later to compile the keras model with custom loss function as
model.compile(optimizer=optimizers.SGD(lr=0.001, momentum=0.9),
loss=cohen_kappa_score_func,
metrics=['categorical_crossentropy', 'mae','categorical_accuracy'])
This doesnt work and i get the following error
"InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'dense_15_target' with dtype float and shape [?,?]
[[node dense_15_target "
Please give me suggestios to solve this.

Tensorflow taking input in the same order as input

I am using tensorflow to test my trained model on test images. I am feeding the images to tensorflow as below:
image_ab, image_aba = sess.run(fetches, feed_dict={self.image_a: image_a,
self.is_train: False})
I printed the image_a and image_ab and observed that image_a is not in the same order as the input images i give.
For some reasons i want the output also to be in the same order as input images.
Does tensorflow usually takes input in the same order as the input given?
I assume you mean image_ab is not in the same order. Because image_a is the input that you feed to tensorflow. If this input is not ordered correctly, it will be your preprocessing, not tensorflow.
Tensorflow usually works on batches of data. For images, the convention for batch dimensions is:
[batch, x, y, colors]
The operations that tensorflow performs are parallelized along the batch. If you simply plug convolutional layers together, the order of the batch should be preserved.
However, it is surely possible to reorder things in tensorflow:
import numpy as np
import tensorflow as tf
x = tf.placeholder(shape=(2,1), dtype="float32")
y = tf.concat([x[1], x[0]], axis=0)
sess = tf.Session()
sess.run([x,y], feed_dict={x:np.random.rand(2,1)})
This code will read in x, change the order of its entries and produce y.
So tensorflow can reorder your images. You could search your code for a pattern like the one in my example.

Why does Keras to_categorical method not return 3-D tensor when inputting 2-D tensor?

I was trying to build a LSTM neural net with Keras to predict tags for words in a set of sentences.
The implementation is all pretty straight forward, but the surprising thing was that
given the exactly same and otherwise correctly implemented code and
using Tensorflow 1.4.0 with Keras running on Tensorflow Backend,
on some people's computers, it returned tensors with wrong dimensions, while for others it worked perfectly.
The problem occured in the following context:
First, we turned the list of training sentences (sentences as a list of word indeces) into a 2-D matrix using the pad_sequences method from Keras (https://keras.io/preprocessing/sequence/):
def do_padding(sequences, length, padding_value):
return pad_sequences(sequences, maxlen=length, padding='post',
truncating='post', value=padding_value)
train_sents_padded = do_padding(train_sents, MAX_LENGTH,
word_to_id[PAD_TOKEN])
Next, we used our do_padding method on the corresponding training labels to turn them into a padded matrix. At the same time, we used the Keras to_categorical method (https://keras.io/utils/#to_categorical) to add a one-hot encoded vector to the created label matrix (one one-hot vector for each cell in the matrix, that means for word in each training sentence):
train_labels_padded = to_categorical(do_padding(train_labels, MAX_LENGTH,
label_to_id["O"]), NUM_LABELS)
We expected the resulting shape to be 3-D: (len(train_labels), MAX_LENGTH, NUM_LABELS). Yet, we found that the resulting shape was 2-D and basically looked like this: ((len(train_labels) x MAX_LENGTH), NUM_LABELS), meaning the numbers on the two expected dimensions len(train_labels) and MAX_LENGTH were multiplied and flattened into one dimension.
Interestingly, this problem as said before only occured for about 50% of the people, using Tensorflow 1.4.0 and Keras running on Tensorflow Backend.
We managed to solve the problem by reshaping the label matrix manually:
train_labels_padded = np.reshape(train_labels_padded, (len(train_labels),
MAX_LENGTH, NUM_LABELS))
I was just wondering if any of you have experienced a similar problem and have figured out the reason why this happens.