5

I am analyzing some image represented datasets using keras. I am stuck that I have two different dimensions of images. Please see the snapshot. Features has 14637 images having dimension (10,10,3) and features2 has dimension (10,10,100)

enter image description here

Is there any way that I can merge/concatenate these two data together.?

5
  • what would you expect the new array to be after concatenation? Commented Sep 21, 2018 at 19:02
  • Are you looking for an answer in Keras or in Numpy? Do you want a layer of a neural network that merges the two? If so, then you can use Keras concatenate. Commented Sep 21, 2018 at 19:11
  • @caseWestern that concatenate isn't only for merging layers...?? I am doing early fusion kind thing. neb's answer perfectly works to me.thanks Commented Sep 24, 2018 at 8:59
  • concatenate in Keras does the same as concatenate in Numpy except with Keras tensors instead of Numpy arrays. So if you need to merge two tensors in a neural network you would use the Keras version. I'm glad the solution worked for you though. Commented Sep 24, 2018 at 13:11
  • oh ok, I didn't know about that. Thank you for the information :) Commented Sep 26, 2018 at 8:05

3 Answers 3

3

If features and features2 contain the features of the same batch of images, that is features[i] is the same image of features2[i] for each i, then it would make sense to group the features in a single array using the numpy function concatenate():

newArray = np.concatenate((features, features2), axis=3)

Where 3 is the axis along which the arrays will be concatenated. In this case, you'll end up with a new array having dimension (14637, 10, 10, 103).

However, if they refer to completely different batches of images and you would like to merge them on the first axis such that the 14637 images of features2 are placed after the first 14637 image, then, there no way you can end up with an array, since numpy array are structured as matrix, non as a list of objects.

For instance, if you try to execute:

> a = np.array([[0, 1, 2]]) // shape = (1, 3)
> b = np.array([[0, 1]]) // shape = (1, 2)
> c = np.concatenate((a, b), axis=0)

Then, you'll get:

ValueError: all the input array dimensions except for the concatenation axis must match exactly

since you are concatenating along axis = 0 but axis 1's dimensions differ.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @neb it worked for me. Yeah features are from the same batch.. I exactly wanted that result (14637, 10, 10, 103). :) Thanks
@FarzanM.Ñóòrî my pleasure :)
2

If dealing with numpy arrays, you should be able to use concatenate method and specify the axis, along which the data should be merged. Basically: np.concatenate((array_a, array_b), axis=2)

Comments

0

I think it would be better if you use class.

class your_class:
     array_1 = []
     array_2 = []

 final_array = []

 for x in range(len(your_previous_one_array)):
     temp_class = your_class
     temp_class.array_1 = your_previous_one_array
     temp_class.array_2 = your_previous_two_array
     final_array.append(temp_class)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.