-1

I have a folder of 2890 tiff lzw compression images as labels for use in deep learning( I have 2 classes, 1 and 2). I want to prepare them as an input of the model that needs a 2D array. so I use imread() of skimage.io library to read them as a list of 2890 numpy arrays(Y), then convert the list into nd.array (Y_train). now I have Y_train by 3 dimensions (2890, 224, 224) that 224 ,224 are input images size. Now I need to convert this 3D array to 2D array as (2890,2) that 2 shows the class number.

  1. I used Y_train.reshape(2890,2) and got this error : ValueError: cannot reshape array of size 145008640 into shape (2890,2).

  2. also I used to_categorical from keras.utils and got 4D arry as (2890, 224, 224, 3).

How can I convert 3D array (2890, 224, 224) to 2D array (2890,2)?

Razneesh
  • 1,147
  • 3
  • 13
  • 29
Somaye K.
  • 13
  • 5
  • This isn't quite a duplicate but there's already an answer on here for what you're trying to do [by slicing the numpy array](https://stackoverflow.com/a/37152121/9005776) – Phles Mar 13 '20 at 19:13
  • Please provide the entire error message, as well as a [mcve]. – AMC Mar 14 '20 at 02:25

2 Answers2

0

assuming you mean to reshape it to a (2890, 224*224), you can do this by putting a -1 in the dimension you want to replace the old dimensions. e.g. np.reshape(2890, -1)

MaMaG
  • 359
  • 1
  • 9
0

I think you have a very wrong idea about the reshape operation. The reshape operation makes sure the number of elements in a matrix/vector will be the same no matter what.

So, you can only reshape a matrix of shape (A, B, C, D) to (A, E) iff B x C x D = E.

You have completely two different arrays, X with shape (-1, 224, 224) and Y with shape (-1, 2). You can't just reshape them, you have to read the images as numpy array and for each image, append the label to another numpy array, after which you can apply to_categorical (Keras) or LabelEncoder (scikit-learn).

Follow some online resources to get an idea about reading labels: Tensorflow read images with labels

Zabir Al Nazi
  • 10,298
  • 4
  • 33
  • 60
  • It is the code I use : `labels_path = "D:/01_thesis/data/qgis/01_classification/LABEL/"` `labels = glob.glob(labels_path + "*.tif") ` `labels.sort()` `Y = []` `for mask in labels: label = imread(mask) Y.append(label) ` `Y_train = np.asarray(Y)` `Y_train_categorical = keras.utils.to_categorical(Y_train)` – Somaye K. Mar 19 '20 at 12:02
  • This seems okay, even though hard to read the code this way. Is it working? – Zabir Al Nazi Mar 19 '20 at 12:03
  • it is working but finally I have a array with 4D that is (2890, 224, 224, 3). – Somaye K. Mar 19 '20 at 12:07
  • This is the right dimension. 2890 is the number of images you have or samples. 224 = image width, 224 = image height, 3 = image channel (red, green, blue) – Zabir Al Nazi Mar 19 '20 at 12:08
  • I got this error : `ValueError: Error when checking target: expected fc1000 to have 2 dimensions, but got array with shape (2312, 224, 224, 3)` that 2312 is the result of split 2890 into train and test. – Somaye K. Mar 19 '20 at 12:17