Issue with tensorflow/ keras densenet

Hi everybody,

When I am running the code pasted below, the model is just training for “multiplier” =1 or =4.
Running the same code in google colab → just training for multiplier=1

Is there any mistake in how I am using DenseNet here?

Thanks in advance, appreciate your help!

import numpy as np
import tensorflow as tf
from tensorflow.keras.applications.densenet import DenseNet201
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.losses import BinaryCrossentropy


random_array = np.random.rand(128,128,3)
image = tf.convert_to_tensor(
    random_array
)
label = tf.constant(0)



model = DenseNet201(
    include_top=False, weights='imagenet', input_tensor=None,
    input_shape=(128, 128, 3), pooling=None, classes=2
)
model.compile(
optimizer=Adam(),
loss=BinaryCrossentropy(),
metrics=['accuracy'],
)


for multiplier in range(1,20):

    print(f"Using multiplier {multiplier}")
    x_train = np.array([image]*multiplier)
    y_train = np.array([label]*multiplier)



    try: 
        model.fit(x=x_train,y=y_train, epochs=2)
    except:
        print("Not training...")
        pass

Some advice:

  • Test small pieces before trying to put them together.
  • Read your error messages.

The problem is that you set “include_top=False”:

model = DenseNet201(
  include_top=False, weights='imagenet', input_tensor=None,
  input_shape=(128, 128, 3), pooling=None, classes=2
)

So the output is a batch of feature maps:

result = model(x_train)
result.shape
TensorShape([2, 4, 4, 1920])

And you’ve set the label to [0,0] and the loss function to BinaryCrossentropy.

model.compile(
  optimizer=Adam(),
  loss=BinaryCrossentropy(),
  metrics=['accuracy'],
  run_eagerly=True,
)

It’s failing because bce=BinaryCrossentropy(); bce([0,0], result) doesn’t work.

That’s what the error message was trying to tell you.

Thanks Mark!

From the documentation on the preptrained models, it wasn’t clear to me, that it is necessary to specify your own Classification Layer. I thought by defining the number of classes, it would be added automatically.