I have a (hopefully) quick Numpy question, and I hope you can help me.
I want to use numpy.reshape to convert (5000, 32, 32, 3) into (5000, 3072), and the only clue I got for the assignment is this:
# Reshape each image data into a 1-dim array
print (X_train.shape, X_test.shape) # Should be: (5000, 32, 32, 3) (500, 32, 32, 3)
#####################################################################
# TODO (2): #
# Reshape the image data to one dimension. #
# #
# Hint: Look at the numpy reshape function and have a look at -1 #
# option #
#####################################################################
X_train =
X_test =
#####################################################################
# END OF YOUR CODE #
#####################################################################
print (X_train.shape, X_test.shape) # Should be: (5000, 3072) (500, 3072)
I've been spending the last day scouring Google for examples, but apparently this is too trivial to warrant an ask. Help?
You can simply do:
X_train = np.reshape(X_train, (5000, -1))
X_test = np.reshape(X_test, (500, -1))
Working example:
import numpy as np
a = np.zeros((5000,32,32,3))
b = np.reshape(a, (5000, -1))
print(a.shape)
print(b.shape)
# Output
# (5000, 32, 32, 3)
# (5000, 3072)
numpy.reshape will try to fit the source array a into an array with first dimension of length 5000. The -1 tells reshape to adjust the length of the second dimension depending on the total length of the source array a.
Related
I am trying to reshape an activity recognition dataset into the 3D form to be parsed in 2D CNN. I tried multiple times, but I couldn't figure out how it can be done.
My Current shape of X_train is (1418, 80, 6), and X_Test is (355, 80, 6).
I am trying to do as follows.
X_train = X_train.reshape(1418, 20, 2, 1)
And I got the following error:
cannot reshape array of size 680640 into shape (1418,20,2,1)
Any advice on how I can reshape the data into 3d so I can pass it in a 2D CNN algorithm?
Thank you
By doing:
X_train = X_train.reshape(1418, 20, 2, 1) on some data that originally has shape (1418, 80, 6) python will output the error:
cannot reshape array of size 680640 into shape (1418,20,2,1)
This is happening because you are trying to reshape (80,6) to (20,2,1)
80 * 6 is not equal 20 * 2 * 1.
try changing it to something that would result into the same quantity of 80 * 6 such as X_train = X_train.reshape(1418, 40, 12, 1) or X_train = X_train.reshape(1418, 20, 24, 1)
I have a problem about reshaping dataframe for implementing CNN.
My dataframe shape : train.shape -> (230, 67502).
Then I wrote a code shown below.
Y_train = train["Label"]
X_train = train.drop(labels = ["Label"],axis = 1)
When I run this code below for plotting some images by iloc, It throws an error
img = X_train.iloc[0].to_numpy()
img = np.pad(img, (0, (67600-img.shape[0])), 'constant').reshape((260, 260))
plt.imshow(img,cmap='gray')
plt.title(train.iloc[0,0])
plt.axis("off")
plt.show()
Then I normalize X_train
X_train = X_train / 255.0
print("x_train shape: ",X_train.shape)
When I reshape X_train , it throws an error
X_train = X_train.values.reshape(-1, 260, 260)
print("x_train shape: ",X_train.shape)
ValueError: cannot reshape array of size 15525000 into shape (260,260)
How can I fix the issue?
Are you absolutely sure you need a 260 * 260 image? As 67500 == 270 * 250, you can try that and see how it looks!
Otherwise you would need to PAD - how exactly would depend upon your image.
But, one of the simplest might be to add 100 more 0's at the end to make it 67600 - hence 260 * 260
Well, 260*260 is 67600 and not 67500. So you can't cast your array into those dimensions.
To actually cast it into those dimensions you would need to pad or normalize the source image arrays. For example, check the Keras pad_sequences functionality documentation on dealing with this kind of issues.
Solution :
X_train = np.pad(X_train, ((0,0), (0, (67600-X_train.shape[1]))), 'constant').reshape(-1, 260, 260)
Here I have one tensor with (1084, 1625, 3) shape.
And I need to reshape it to (none,none,none ,3).
how can i do that?
I used this code but it does not work.
image = tf.cast(img, tf.float32)
image = (image / 127.5) - 1
I don't think you can do that. I think what you're trying to do is turn a 3D tensor into a 4D tensor. I'm guessing this is the origin of your problem. You can do this to add a 4th dimension, because Tensorflow needs it:
import tensorflow as tf
tensor = tf.random.uniform((100, 100, 3), 0, 256, dtype=tf.int32)
new = tf.expand_dims(tensor, axis=0)
print(new.shape)
Out[14]: TensorShape([1, 100, 100, 3])
But then I could be wrong. If this is the case you can provide your error traceback and code.
I have a network in which the data comes from a stack of images and a vector of numbers.
I begin with two "branches": The images go through several convolutions and produce an output of shape (50, 50, 64). In the other branch I import the number and go:
x = Input(shape = (13)) # data vector is of length 13
x = Dense(50*50)(x)
x = Reshape((50,50))(x)
I now have 2 outputs from the branches - one is of shape (50, 50, 64) and the other of shape (50, 50, 1). How can I "stick" them together to get a collective (50, 50, 65) which I'd then Deconv2D?
You could use the keras Concatenate() layer as in follows:
import numpy as np
from keras import backend as K
from keras import layers
# create some dummy tensors with numpy and the keras backend
arr1 = K.constant(np.zeros((50, 50, 1)))
arr2 = K.constant(np.zeros((50, 50, 64)))
# and this is how you call the concatenate layer
combined = layers.Concatenate()([arr1, arr2])
# it should print this:
# Tensor("concatenate_1/concat:0", shape=(50, 50, 65), dtype=float32)
print(combined)
you can use numpy function : np.c_
try:
>>> x.shape
(50, 50, 64)
>>> y.shape
(50, 50, 1)
>>> z = np.c_[x,y]
>>> z.shape
(50, 50, 65)
There is a 25*15 image, and i want to identify what it is by using CNN.
When training my CNN, I input a numpy named 'img' as datasets which shape is (200, 375):
sess.run(train, feed_dict={X: imgs, Y: labels}
This numpy contains 200 sample ,each of them have 375 features.
But when i reshape this numpy to a (-1, 25, 15, 1) Tensor:
X = tf.placeholder(tf.float32, [None, 375])
X = tf.reshape(X,[-1,25,15,1])
Something wrong happened:
Cannot feed value of shape (200, 375) for Tensor 'Reshape:0', which has shape '(?, 25, 15, 1)'
I don't know why it can't work, 25*15 is indeed 375.
Thank you!
You don't seem to reshape the dict variable you are feeding to the placeholder. You have to reshape your img variable as well into shape [-1, 25, 15, 1]