Reshaping a dataframe into 3D - python

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)

Related

Error when checking input: expected input_6 to have shape (80, 80, 1) but got array with shape (80, 80, 2400) in image segmentation

I have a medical imaging dataset with a dimension of (80,80,2900), each image is 80*80. First I loaded the mat file of the data as follow:
data = loadmat('cardiac-dig.mat')
images_LV = np.array (data['images_LV'])
val_data_size = 500
valid_images = images_LV[:,:,:val_data_size]
train_images = images_LV[:,:,val_data_size:]
valid_masks = masks[:,:,:val_data_size]
train_masks = masks[:,:,val_data_size:]
when I tried to fit the model using this:
model.fit(train_images , train_masks, epochs=2, batch_size=8)
I end up this error which says the input should be 4 dimensions:
Error when checking input: expected input_6 to have 4 dimensions, but got array with shape (80, 80, 2400)
I tried to reshape the input to 4 dimensions using:
images_LV = np.reshape(images_LV, (-1, 80,80,2900))
but I got other error:
Error when checking input: expected input_6 to have shape (80, 80, 1) but got array with shape (80, 80, 2400)
I think that the input should be like (2900,80,80,1)?
if you need more information I can share.
I found the solution. I wanted to post the answer to those may have the same issue:
based on the shape of my dataset, (80,80,2900), I needed to change the dimension from 3 to 4. Also, it was necessary to reshape the dataset as follow:
***images = np.swapaxes(images, 0, 2)
images = np.swapaxes(images, 1, 2)
images = np.reshape(images, (-1, 80,80,1))
print(images.shape)
\\(2900, 80, 80, 1)***
After above change, I simply pass the data as the input of the model:
***train_images = images[val_data_size:,:,:,:]
model.fit(train_images , train_masks, epochs=5, batch_size=8)***

ValueError: cannot reshape array of size 23760 into shape (240,1,28,28)

# Reshape and normalize training data
trainX = train[:, 1:].reshape(train.shape[0],1,28, 28).astype( 'float32' )
x_train = trainX / 255.0
y_train = train[:,98]
# Reshape and normalize test data
testX = test[:,1:].reshape(test.shape[0],1, 28, 28).astype( 'float32' )
x_test = testX / 255.0
y_test = test[:,98]
I try to reshape my csv train_data and test_data into 3-D Matrix but I get an error:
ValueError Traceback (most recent call last)
<ipython-input-57-268af51a6b14> in <module>()
----> 1 trainX = train[:, 1:].reshape(train.shape[0],1,28, 28).astype( 'float32' )
2 x_train = trainX / 255.0
3
4 y_train = train[:,98]
5
ValueError: cannot reshape array of size 23760 into shape (240,1,28,28)
Error Report Screenshoot
As already mentioned in the comments, 23760 != 240*1*28*28, so reshaping into that particular array is impossible. In fact, 28*28 doesn't even divide 23760, so even if train.shape[0] were replaced with something else, this would never work.
Assuming that train.shape[0] and 1 are what you want to use for the first two dimensions, the final dimensions need to have a product of 23760/240 = 99. As this isn't a square number, the two numbers will have to differ. The prime factorization of 99 is 99 = 3*3*11, so the only possible options are
(240, 1, 1, 99), (240, 1, 3, 33), (240, 1, 9, 11),
and their permutations.

Creating batches within an array containing images

I have an array X_train containing 9957 images. I am making a Convolutional network.The desired shape of the array for feeding into the model is (batchsize, channel, height, width)
X_train.shape #gives (9957, 60, 80, 3)
X_train[1].shape #gives (60, 80, 3)
If we use
np.reshape(X_train,(-1, 3, 60, 80)) #it gives (9957, 3, 60, 80)
How can I get each array with shape (batchsize, 3, 60, 80) and the final image array for training with shape(9957, batchsize, 3, 60, 80)?
You can get from i-th image until i + batchsize image as follows.
batchsize = 16
i = 0
X_batch = X_train[i: i+batchsize]
print('X_batch.shape: ', X_batch.shape) # it should be (16, 3, 60, 80)
Please change i with for loop to get each image. For example,
for i in range(0, len(X_train), batchsize):
X_batch = X_train[i: i+batchsize]
# --- Do something with X_batch ---

How to reshape image array?

currently I'm working on a neural network that can classify the numbers in the Street View House Number dataset (http://ufldl.stanford.edu/housenumbers/). For now, I'm just trying to do it on the second format, the one similar to the MNIST dataset.
The problem I've encountered is that the shapes of the train and test arrays of examples are (HEIGHT, WIDTH, CHANNELS, EXAMPLES) rather than (EXAMPLES, HEIGHT, WIDTH, CHANNELS).
Is there a simple way to reshape the array to what I want without using many nested loops?
I'm not sure if the object you are trying to reshape is a Tensor or numpy.ndarray.
If it is a numpy.ndarray, you can use np.transpose. For example:
import numpy as np
a = np.zeros((299, 299, 3, 50))
print(a.shape) # (299, 299, 3, 50) H x W x C x M
b = np.transpose(a, [3, 0, 1, 2])
print(b.shape) # (50, 299, 299, 3)
If it is a Tensor, You can use tf.transpose to change the order of the dimension in exactly the same way as np.transpose. For example:
import tensorflow as tf
a = tf.zeros((299, 299, 3, 50), dtype=tf.int32)
print(a.shape.as_list()) # [299, 299, 3, 50]
b = tf.transpose(a, [3, 0, 1, 2])
print(b.shape.as_list()) # [50, 299, 299, 3]

Figuring out the proper numpy.reshape for -1 option

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.

Categories