Reputation: 711
I would like to write a Resnet 18 model, so I found this code, knowing that my dataset is an image dataset and my labels are 2 (num_classes=2), I find this error that I can't understand it. Here is my model :
def create_compiled_keras_model():
inputs = tf.keras.Input((224, 224, 3))
regularizer = None
x = tf.keras.layers.ZeroPadding2D(padding=(3,3), name='pad')(inputs)
x = tf.keras.layers.Conv2D(filters=64, kernel_size=7, strides=2, padding='valid', activation='linear',
use_bias=False, kernel_initializer='he_normal', kernel_regularizer=regularizer, name='conv1')(x)
x = tf.keras.layers.BatchNormalization(momentum=0.1, epsilon=1e-5, name='bn1')(x)
x = tf.keras.layers.Activation('relu', name='relu')(x)
x = tf.keras.layers.ZeroPadding2D(padding=(1,1), name='pad1')(x)
x = tf.keras.layers.MaxPool2D(pool_size=3, strides=2, padding='valid', name='maxpool')(x)
x = BasicBlock(x, num_channels=64, kernel_size=3, num_blocks=2, skip_blocks=[], regularizer=regularizer, name='layer1')
x = BasicBlockDown(x, num_channels=128, kernel_size=3, regularizer=regularizer, name='layer2')
x = BasicBlock(x, num_channels=128, kernel_size=3, num_blocks=2, skip_blocks=[0], regularizer=regularizer, name='layer2')
x = BasicBlockDown(x, num_channels=256, kernel_size=3, regularizer=regularizer, name='layer3')
x = BasicBlock(x, num_channels=256, kernel_size=3, num_blocks=2, skip_blocks=[0], regularizer=regularizer, name='layer3')
x = BasicBlockDown(x, num_channels=512, kernel_size=3, regularizer=regularizer, name='layer4')
x = BasicBlock(x, num_channels=512, kernel_size=3, num_blocks=2, skip_blocks=[0], regularizer=regularizer, name='layer4')
x = tf.keras.layers.GlobalAveragePooling2D(name='avgpool')(x)
x = tf.keras.layers.Dense(units=1000, use_bias=True, activation='linear', kernel_regularizer=regularizer, name='fc')(x)
#x = tf.keras.layers.GlobalAveragePooling2D()(x)
#x = tf.keras.layers.Dense(units=2, use_bias=False, name='output', activation='relu')(x)
model_output = tf.keras.layers.Dense(units=2,use_bias=False, name='output', activation='relu')(x)
model = tf.keras.Model(inputs, model_output)
model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.001),
loss=tf.keras.losses.CategoricalCrossentropy(),
metrics=[tf.keras.metrics.CategoricalAccuracy()])
return x
Error: TypeError: Expected tensorflow.python.keras.engine.training.Model, found tensorflow.python.framework.ops.Tensor.
Upvotes: 0
Views: 451
Reputation: 2782
You are returning the wrong variable. I think you should return model
instead of x
.
Upvotes: 2