0

I am working on OVA (one vs all) classification problem. For that, I have trained Keras binary classifiers with sigmoid function and binary_crossentropy. I need to ensemble them to multiclass model similar to here When I am trying to do that I get the following error

 tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'sequential_input' with dtype float and shape [?,224,224,3]
         [[{{node sequential_input}}]]

Program code

for i in os.listdir(model_root): //loading all the models
print(i)
filename = model_root + "/" + i
# load model
model = load_model(filename, custom_objects={'KerasLayer': hub.KerasLayer})
models.append(model)
print(len(models))  //3

#Merge layer to fit a model

inputs = tf.keras.Input(shape=(224,224,3))
outputs = [m(inputs) for m in models]
outputs = tf.keras.layers.concatenate(outputs, axis=-1)
ensemble = tf.keras.models.Model(inputs, outputs)
ensemble.compile(optimizer=tf.keras.optimizers.Adam(), loss='categorical_crossentropy', metrics=['accuracy'])

#To fit the loaded models to the data

steps_per_epoch = image_data.samples // image_data.batch_size
validation_steps = image_data_val.samples / image_data_val.batch_size
ensemble.fit((item for item in image_data), epochs=2,
          steps_per_epoch=steps_per_epoch,
          validation_data=(item for item in image_data_val), validation_steps=validation_steps, verbose=2)

I am getting this error on the fit function. Here is a traceback

Epoch 1/2
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2019.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/Pawandeep/Desktop/Python projects/ensemble_image.py", line 85, in <module>
    validation_data=(item for item in image_data_val), validation_steps=validation_steps, verbose=2)
  File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\training.py", line 673, in fit
    initial_epoch=initial_epoch)
  File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1433, in fit_generator
    steps_name='steps_per_epoch')
  File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\training_generator.py", line 264, in model_iteration
    batch_outs = batch_function(*batch_data)
  File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1175, in train_on_batch
    outputs = self.train_function(ins)  # pylint: disable=not-callable
  File "C:\Python\lib\site-packages\tensorflow\python\keras\backend.py", line 3292, in __call__
    run_metadata=self.run_metadata)
  File "C:\Python\lib\site-packages\tensorflow\python\client\session.py", line 1458, in __call__
    run_metadata_ptr)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'dense_1_target' with dtype float and shape [?,?]
     [[{{node dense_1_target}}]]

My model looks like

        Model: "model"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            [(None, 224, 224, 3) 0                                            
__________________________________________________________________________________________________
sequential_4 (Sequential)       (None, 1)            3541267     input_1[0][0]                    
__________________________________________________________________________________________________
sequential_8 (Sequential)       (None, 1)            3541267     input_1[0][0]                    
__________________________________________________________________________________________________
sequential_2 (Sequential)       (None, 1)            3541267     input_1[0][0]                    
__________________________________________________________________________________________________
concatenate (Concatenate)       (None, 3)            0           sequential_4[1][0]               
                                                                 sequential_8[1][0]               
                                                                 sequential_2[1][0]               
==================================================================================================
Total params: 10,623,801
Trainable params: 3,006
Non-trainable params: 10,620,795
__________________________________________________________________________________________________

I can't find tensor dense_1_target. I don't understand which one it is referring to.

My data looks like this:

image_data = image_generator.flow_from_directory(str(data_root), target_size=IMAGE_SIZE, subset='training')
for image_batch, label_batch in image_data:
print("Image batch shape: ", image_batch.shape) // (32, 224, 224, 3)
print("Label batch shape: ", label_batch.shape) // (32, 3)

Now where can I put the placeholders and how can I relate it to my input data.

user3050590
  • 1,656
  • 4
  • 21
  • 40
  • maybe can help you https://stackoverflow.com/questions/49756909/invalidargumenterror-you-must-feed-a-value-for-placeholder-tensor-placeholder – Zrufy Aug 20 '19 at 09:36
  • I don't know what is tf.placeholder. What is it in my code? – user3050590 Aug 20 '19 at 09:45
  • A placeholder is TF (1.x)'s way to represent input tensors. It would seem that you're not passing in all the necessary input data when you call `fit`. Check that `item` from `item for item in image_data` is what `fit` expects (i.e., an array of shape `[?,224,224,3]` of image samples to feed to the network) – GPhilo Aug 20 '19 at 10:05
  • define placeholder for your training e val set and pass these value in fit. For Example `x = tf.placeholder(tf.float32,shape=[None,224,224,3]) y_true = tf.placeholder(tf.float32,shape=[None,output_classes]) ` – Zrufy Aug 20 '19 at 10:21
  • @Zrufy I have updated the post with information about data, how can I put the placeholder here. could you please help – user3050590 Aug 20 '19 at 14:29
  • @GPhilo, yes I checked it is generating the same shape i.e `(32, 224, 224, 3)´ where 32 is the batch size – user3050590 Aug 21 '19 at 09:12
  • fit doesn't expect the data to be pre-batched, it batches it internally (one of the parameters of fit is batch size). – GPhilo Aug 21 '19 at 09:17
  • @GPhilo I am doing batches manually. 32 is the default batch size that it gives. I am reading the data from image_generator. but image_data, and image_data_val(validation data) is loaded by generator and tensorflow is batching it to 32 – user3050590 Aug 21 '19 at 09:21
  • sorry, I meant I am not* doing batches manually – user3050590 Aug 21 '19 at 09:26

0 Answers0