How to display MXNet NDArray as image in Jupyter Notebook? - mxnet

I have an MXNet NDArray that has image data in it. How do I render the NDArray as image in Jupyter Notebook?
type(data)
mxnet.ndarray.ndarray.NDArray
data.shape
(3, 759, 1012)

Here is how to do it:
Convert the MXNet NDArray to numpy array.
Transpose the array to move channel to last dimension.
Convert the array to uint8 (0 to 255).
Render the array using matplotlib.
Here is the code:
import mxnet as mx
import numpy as np
from matplotlib import pyplot as plt
def render_as_image(a):
img = a.asnumpy() # convert to numpy array
img = img.transpose((1, 2, 0)) # Move channel to the last dimension
img = img.astype(np.uint8) # use uint8 (0-255)
plt.imshow(img)
plt.show()
You can then render the array by calling render_as_image.
render_as_image(data)

To display an image or any other plot using Matplotlib first you need to convert MXNet NDArray to NumPy array.
For example, a is your MXNet NDArray to convert it into Numpy we will do this b = a.asnumpy(). Now you can plot/show this b using Matplotlib.

Related

How to access pytorch embeddings lookup table as a tensor

I want to show my embeddings with the tensorboard projector. I would like to access the embeddings matrix (lookup table) of one of my layers so I can write it to the logs.
I instantiate my layer as this:
self.embeddings_user = torch.nn.Embedding(30,300)
And I'm looking for the tensor with shape (30,300) of 30 users with embedding on 300 to dimensions to replace it with the vectors variable in this sample code:
import numpy as np
import tensorflow as tf
import tensorboard as tb
tf.io.gfile = tb.compat.tensorflow_stub.io.gfile
from torch.utils.tensorboard import SummaryWriter
vectors = np.array([[0,0,1], [0,1,0], [1,0,0], [1,1,1]])
metadata = ['001', '010', '100', '111'] # labels
writer = SummaryWriter()
writer.add_embedding(vectors, metadata)
writer.close()
Embeddings layers have weight attributes corresponding to the lookup table. You can access it as follows.
vectors = self.embeddings_user.weight
So now you can visualize it with tensorboard.
import numpy as np
import tensorflow as tf
import tensorboard as tb
tf.io.gfile = tb.compat.tensorflow_stub.io.gfile
from torch.utils.tensorboard import SummaryWriter
vectors = self.embeddings_user.weight
metadata = ['001', '010', '100', '111', ...] # labels
writer = SummaryWriter()
writer.add_embedding(vectors, metadata)
writer.close()

what's the meaning of 'input_length'?

the data have 4 timestamps,but the embedding's input_length=3,so what's the meaning of input_length?
from tensorflow import keras
import numpy as np
data = np.array([[0,0,0,0]])
emb = keras.layers.Embedding(input_dim=2, output_dim=3, input_length=3)
emb(data)
As per the official documentation here,
input_length: Length of input sequences, when it is constant. This
argument is required if you are going to connect Flatten then Dense
layers upstream (without it, the shape of the dense outputs cannot be
computed).
from tensorflow import keras
import numpy as np
model = keras.models.Sequential()
model.add(keras.layers.Embedding(input_dim=2, output_dim=3, input_length=4))
# the model will take as input an integer matrix of size (batch, input_length).
input_array = np.array([[0,0,0,0]])
model.compile('rmsprop', 'mse')
output_array = model.predict(input_array)
print(output_array)
Above works fine, but if you change input_length to 3, then you will get below error:
ValueError: Error when checking input: expected embedding_input to
have shape (3,) but got array with shape (4,)

Tensorflow Tensor out of CSV has no size?

I just can't get any dimensions (size, lenght) out of this damn tensor "datatens". here is the code and the error message:
import tensorflow as tf
import numpy as np
import tflearn
import pandas as pd
from tensorflow import keras
file = 'some.csv'
record_defaults = [tf.float64]*18
from tflearn.data_utils import load_csv
data , label = load_csv(file, target_column=0,has_header=True,
categorical_labels=True, n_classes=50)
datatens = tf.data.Dataset.from_tensor_slices((data,label))
print(datatens.get_shape().as_list())
ERROR:
<TensorSliceDataset shapes: ((17,), (50,)), types: (tf.string, tf.float64)>
Traceback (most recent call last):
File "basic_class.m", line 44, in <module>
print(datatens.get_shape().as_list())
AttributeError: 'TensorSliceDataset' object has no attribute 'get_shape'
FOLLOWUP:
after getting eager execution running im curious, why my tensor is integer instead of float. here is the output of the advised code.
CODE:
print(tf.shape(data))
print(tf.shape(label))
OUTPUT:
Tensor("Shape:0", shape=(2,), dtype=int32)
Tensor("Shape_1:0", shape=(2,), dtype=int32)
When you call tf.data.Dataset.from_tensor_slices, you get a dataset, not a tensor. A dataset is essentially a container of tensors, and you can access its tensors in a few ways.
The simplest way is to call the dataset's make_one_shot_iterator method. This returns an iterator that cycles through the tensors. The best documentation on datasets and iterators is here.
Are you sure you want to call tf.data.Dataset.from_tensor_slices? Aren't data and label already tensors?
EDIT:
If you want to validate the tensor containing labels, try this code:
import tensorflow as tf
import numpy as np
import tflearn
import pandas as pd
from tensorflow import keras
from tflearn.data_utils import load_csv
tf.enable_eager_execution()
file = 'some.csv'
record_defaults = [tf.float64]*18
data, label = load_csv(file, target_column=0,has_header=True,
categorical_labels=True, n_classes=50)
print(tf.shape(label))
Enabling eager execution is important because it lets you access the tensor without having to create and run a session.

How to multiply input images with mask in tensorflow?

I want to multiply every input image with a mask of the same size as the input image. How would I do that in tensorflow?
My image reading function looks like this so far:
img_contents = tf.read_file(input_queue[0])
label_contents = tf.read_file(input_queue[1])
img = tf.image.decode_png(img_contents, channels=3)
label = tf.image.decode_png(label_contents, channels=1)
# Now I want to do something like this?
mask = tf.constant(1.0, dtype=tf.float32, shape=img.shape)
img_masked = tf.multiply(img,mask)
Is that possible?
Not sure if img is already a tensor object and I can use that function here. I'm new to tensorflow...
Here is the code that works well for me. I'm using jupyter notebook to run the code.
%matplotlib inline
import tensorflow as tf
from matplotlib.image import imread
import matplotlib.pyplot as plt
# Loading test image from the local filesystem
x = tf.Variable(imread("test_img.jpg"),dtype='float32')
x_mask = tf.Variable(imread("test_mask.jpg"),dtype='float32')
img_mult = tf.multiply(x,x_mask)
plt.imshow(imread("test_img.jpg"))
plt.show()
plt.imshow(imread("test_mask.jpg"))
plt.show()
sess = tf.Session()
sess.run(tf.global_variables_initializer())
res = sess.run(img_mult)
plt.imshow(res)
plt.show()
Also, Here is a good YouTube tutorial covering image manipulation with TF: https://www.youtube.com/watch?v=bvHgESVuS6Q&t=447s

Convert KMeans Labels into Source data in Sklearn

import numpy as np
from sklearn.cluster import DBSCAN, MiniBatchKMeans
data = np.random.rand(5,5)
print data
km = MiniBatchKMeans(n_clusters=3, n_init=10, max_iter=5)
km.fit(data)
labels = km.labels_
print labels
[1 2 0 2 2]
How can I reconstruct my data using the labels? I mean making my data composed of labels in each pixel.
If you want to do 1d clustering, then reshape your data to a 1d array, cluster the points and then reshape back your labels:
import numpy as np
from sklearn.cluster import MiniBatchKMeans
data = np.random.rand(5, 5)
data_to_cluster = np.reshape(data, (data.size, 1))
km = MiniBatchKMeans(n_clusters=3, n_init=10, max_iter=5)
km.fit(data_to_cluster)
labels = km.labels_
labels = np.reshape(labels, (5, 5))