To visualize TensorType(float64, matrix) as an image using imshow, How could I that?I cannot directly use imshow on Tensor since it gives me this error
mat is not a numpy array, neither a scalar
When I try to convert datatype to array using numpy.asarray I get
mat data type = 17 is not supported
Is there any way to convert to uint8 datatype?
Theano tensors are symbolic variables, with no value attached to them. If you want to plot something it is probably a shared variable (the weights of your network), in which case you can grab the values with my_shared.get_value(), or the output of the network when you pass an input, in which case I believe it should already be a numpy array.
Related
I am trying to convert an array of 3D tensors (images) to a single 4D one, so I can pass them as values to the model.fit which does not seem to accept Tensor3D arrays.
The idea would be
4dTensor = tf.tensor4d(batch)
I am actually using javascript, but either a python or js solution would probably work as the Tensorflow API is similar.
The error of this procedure is:
Argument of type 'Tensor4D' is not assignable to parameter of type 'Tensor3D[]'.
Type 'Tensor<Rank.R4>' is missing the following properties from type 'Tensor3D[]': length, pop, push, join, and 26 more.ts(2345)
You may want to use tf.stack():
4dTensor = tf.stack(batch)
I have a 3d image stored in fet_img np array. The size is (400,400,74).
I want to access the 74 2D images seperately, each of size (400,400).
I would expect that this would do the trick:
fet_img[:][:][0]
However, when I print the shape of this, I get (400,74)
I tried
fet_img[0][:][:]
and
fet_img[:][0][:]
but the size of all three of these are (400,74)...
I'm overlooking something but I can't quite figure out what?
Note: I'm runnning this from a local jupyter notebook and all values are dtype('float64') if that matters at all.
You should use fet_img[:, :, 0] instead.
I'm actually trying to program a Keras Model. In my point of view, a keras Model needs a list of np.arrays as x (or a Numpy Array). In my case x is looking like this:
print(training.dtype)
object
print(training.shape)
(406,)
print(training[0].dtype)
float64
print(training[0].shape)
(5140, 5)
This is the size of my Train data (x). If I want to train the model I get this error:
return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.
That's why I think, I prepared the data wrong. If I want to convert them with .astype to float32, I get the same error.
Thanks for your help!
If the entries in train2 do not all have the same size, you will need to pad them. As this is something that needs to be done quite regularly, Keras offers a function for this: pad_sequences
Once they all are the same size, np.array(train2) will create one single numpy array that you can pass to model.fit().
Depending on your model, the extra data you are adding this way may or may not be an issue. A common way to deal with this is Masking. Use this to generate a mask that will automatically be passed down the model so that certain values (the values you added via padding) are ignored. Note however, that not all layers support masking, so maybe this is not an option for you.
The issue is not changing the type. The issue is in the batch samples not being of the same size, so no np array could be created. You can solve this by using padding as mentioned in the comments. Have a look at keras pad_sequences What does Keras.io.preprocessing.sequence.pad_sequences do?
I have a 100x200 input, and a 1x100 target matrix that I am using to run a gridsearch and create a classifier in python. However, I get errors that my training set of target data is not an array. I've tried:
target=np.asarray(matTarget)
Where the matTarget is just my target imported from Matlab using scipy.io.loadmat.
My exact error is
len() of unsized object
When I try target.size I get a blank size as well.
If I do not do the array conversion, then I get
Expected array-like (array or non string sequence) got {'_header_': b'Matlab matfile ... Array([[1],[1]...)}
I still have the original matrix in Matlab and have also tried using np.array instead of asarray.
If I do print(matTarget.keys()) then I get ('header`,'version','globals','y_train'])
y_train is the name of the mat file itself
According to the documentation of scipy.io.loadmat it returns a dictionary where the values are the contained matrices.
Returns: mat_dict : dict
dictionary with variable names as keys, and loaded matrices as values.
So you need to select your matrix by its name before using it with numpy:
matrix = matTarget['name of matrix']
I am using OpenCV to read and display an image. I am trying to do a scalar multiplication but it is being displayed very differently for two similar approaches:
img = cv2.imread('C:/Python27/user_scripts/images/g1.jpg', -1)
cv2.imshow('img_scaled1', 0.5*img)
cv2.waitKey(0)
cv2.imshow('img_scaled2', img/2)
cv2.waitKey(0)
In the 1st case, hardly anything is displayed. 2nd case works fine.
It seems to me that imshow() does not support numpy array of floats.
I want to use the first method. Can somebody help?
There are lot of pitfall when using images. This one seems like a type issue.
imshowaccept uint8 arrays in range(0,256) (256 excluded), and float arrays in range(0.0,1.0). When doing a=a*.5, you have a float array out of range, so no warranty on the result.
A solution is to cast the array in the uint8 type by:
imshow((a*.5).astype(np.uint8))
or
imshow((a*.5).astype('uint8'))