Steffen
Steffen

Reputation: 9

Is my configuration for Densenet in tensorflow wrong?

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

if the training does not start, the output is:

2021-12-01 11:48:40.372387: W tensorflow/core/framework/op_kernel.cc:1680] Invalid argument: required broadcastable shapes
2021-12-01 11:48:40.372660: W tensorflow/core/framework/op_kernel.cc:1680] Invalid argument: required broadcastable shapes
2021-12-01 11:48:40.372734: W tensorflow/core/framework/op_kernel.cc:1680] Invalid argument: required broadcastable shapes

Upvotes: 0

Views: 157

Answers (1)

Steffen
Steffen

Reputation: 9

apparently, it is necessary to add a custom GlobalAveragePooling and Dense Layer if a custom input_shape (not the standard 224x224x3 of ImageNet) and include_top = False is used:

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

x= base_model.output
x = GlobalAveragePooling2D(name = "avg_pool")(x)
outputs = Dense(2, activation=tf.nn.softmax, name="predictions")(x)

model = Model(base_model.input, outputs)

Upvotes: 0

Related Questions