Getting Error after merging two Deep Learning models VGG16 and ResNet50

File “C:\Users\User.conda\envs\Sum\lib\site-packages\numpy\lib\npyio.py”, line 1382, in savetxt
raise ValueError(

ValueError: Expected 1D or 2D array, got 4D array instead

This kind of error i am getting…please give me solution.code has been attached below

from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
inputs_2 = keras.Input(shape=(224, 224, 3), name=“img”)
vgg = VGG16(input_tensor=inputs_2, weights=‘imagenet’, include_top=False)
for layer in vgg.layers:
layer.trainable = False

resnet = ResNet50(input_tensor=inputs_2, weights=‘imagenet’, include_top=False)
for layer in resnet.layers:
layer.trainable = False

mergedOutput = tf.keras.layers.concatenate([vgg.output, resnet.output])

#merged = keras.layers.GlobalAveragePooling2D()(mergedOutput) # This line

#x = keras.layers.Dense(256, activation=“relu”)(merged)

#prediction = Dense(3, activation=‘softmax’)(x)
model = Model(inputs=inputs_2, outputs=mergedOutput)

print(model.summary())
img_path = ‘img-1.png’
img = image.load_img(img_path, target_size=(224, 224))
x2 = image.img_to_array(img)
x2 = np.expand_dims(x2, axis=0)
x2 = preprocess_input(x2)

features = model.predict(x2)

np.savetxt(“feat-c.csv”,features,delimiter=“,”)

@Sumana_Naskar,

Welcome to the Tensorflow Forum!

You need to reshape your array using reshape() method as shown below

features = model.predict(x2.reshape(1, 224 * 224 * 3))

Thank you!

Thank you sir but i am getting same error after putting this line.what i will do?

Could you please share img-1.png ?

Thank you!

Here media is not supported?how i will send you?any mail id can i get to send image?

@Sumana_Naskar,

You can store the image in google drive and share the link.

Thank you!

above link is for img-1.png

This is actually video frame of nail image. That’s why this is quite reddish -black and hazy.

Thank you Sir

@Sumana_Naskar,

I request you please follow the steps to share the link

  1. Select the file you want to share.
  2. Click Share or Share .
  3. Under “General access” click the Down arrow .
  4. Choose Anyone with the link.
  5. To decide what role people will have, select Viewer, Commenter, or Editor.
  6. Click Copy link.
  7. Click Done.
  8. Paste the link here.

Thank you

i am following above process

@Sumana_Naskar,

ValueError: Expected 1D or 2D array, got 4D array instead

The function np.savetxt() expects a 1D or 2D array, but the array you are trying to save is a 4D array.

If you want to save in .txt by using:

np.savetxt("feat-c.csv", features.reshape(features.shape[0], -1), delimiter=",")

Otherwise you can try to save in .npy (which is recommend) by using:

np.save("feat-c.npy", features)

Thank you!

Thank you sir…i will try putting your solution…