0

I have three learners (three different pre-trained models) participating in an ensemble.

For my train dataset, I get a list of NumPy arrays that state the confidence of each model

# for 3 classes A,B,and C
# prediction array of a given model for 4 different train set images:
[[0.2, 0.2, 0.6],
 [0.1, 0.8, 0.1], 
 [0.4, 0.4, 0.2], 
 [0.3, 0.3, 0.4]] 

I get 3 prediction arrays M1, M2, M3 from 3 different models from which a final prediction array will be generated using np.argmax on the weights

M = w1 * M1 + w2 * M2 + w3 * M3 (w3= 1-w1-w2)

What I planned on, to find these weights was to use another neural network, and compare this with the label array for optimizing loss function (using one hot encoding on it)

[[0.0, 0.0, 1.0],
 [0.0, 1.0, 0.0],
 [1.0, 0.0, 0.0],
 [0.0, 1.0, 0.0]]

The code I presented with uses the 3 prediction lists (with NumPy array confidence per class) in a pandas dataframe. I check it against the list of correct labels (one hot encoded) using an ann

X= pd.DataFrame()
X.insert(0, '1', label1, False)
X.insert(1, '2', label2, False)
X.insert(2, '3', label3, False)
X= X.iloc[1:, :]

The problem is the input shape that I am trying to fit gives the error

Failed to convert a NumPy array to a Tensor (Unsupported object type tensorflow.python.framework.ops.EagerTensor)

ensemble model I created:

model_ensemble  = tf.keras.models.Sequential()
model_ensemble.add(tf.keras.layers.Dense(units=10, activation='relu'))
model_ensemble.add(tf.keras.layers.Dense(units=10, activation='relu'))
model_ensemble.add(tf.keras.layers.Dense(units=3, activation='softmax'))
model_ensemble.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
model_ensemble.fit(X, a, batch_size = 32, epochs = 25)

for creating the correct labels to compare to:

a= pd.DataFrame(x_train['label'] ).astype('int').astype('category')
print(a)
a= tf.keras.utils.to_categorical(a)

I am also attaching the way I generate the prediction labels, which is probably why this stuff isn't working

  label1= [np.asarray([])]
  label2= [np.asarray([])]
  label3= [np.asarray([])]
  for i in x_train['name']:
    Path= path.join('/content/data/train', I)
    img = tf.io.read_file(Path)
    tensor = tf.io.decode_image(img, channels=3, dtype=tf.dtypes.float32)
    tensor = tf.image.resize(tensor, [224, 224])
    input_tensor = tf.expand_dims(tensor, axis=0)
    result1=model1.predict(input_tensor)
    result2=model2.predict(input_tensor)
    result3=model3.predict(input_tensor)
    label1.append(tf.convert_to_tensor(result1))
    label2.append(tf.convert_to_tensor(result2))
    label3.append(tf.convert_to_tensor(result3))
  • It's unclear to me where you're getting an error, but given the error message, but I would guess it's an issue with eager execution returning an EagerTensor and not a numpy array. Can you share the whole traceback? – Lescurel Oct 10 '22 at 08:10
  • 1
    Have you looked a this? https://stackoverflow.com/questions/58636087/tensorflow-valueerror-failed-to-convert-a-numpy-array-to-a-tensor-unsupporte – Pawel Oct 10 '22 at 17:42
  • The error message arises in which line, please share the entire traceback. – Hamzah Oct 11 '22 at 07:42
  • 1
    As a side note, you really don't need a neural network to find the best weights for your use case. If you take the outputs `M1`, `M2` and `M3` as input, and your ground truth as output, you can use a linear regression to find the optimum weights `w1`, `w2` and `w3` that minimises the error between `y_pred = w1*M1 + w2*M2 + w3*M3` and `y_true` – Florent Monin Oct 13 '22 at 11:17
  • Regarding the ensemble of different model's prediction, this might help. https://www.kaggle.com/code/ipythonx/optimizing-metrics-out-of-fold-weights-ensemble – Innat Oct 13 '22 at 11:31

0 Answers0