0
from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np

model = ResNet50(weights='imagenet')

In this code, there is the "wrapper" (that's what it's referred to) ResNet50. What are the other types of weights I can use for this? I tried looking around but I don't even understand the source code; there is nothing conclusive there either

Jerome Ariola
  • 135
  • 1
  • 11

1 Answers1

0

You can find it on the keras doc Or github code

There is only two options, either None if you just want the architecture without the weights, or imagenet to load imagenet weights.

Edit : how to use our own weights :

# Take a DenseNET201
backbone = tf.keras.applications.DenseNet201(input_shape=input_shape, weights=None, include_top=False)

# Change the model a little bit, because why not
input_image = tf.keras.layers.Input(shape=input_shape)
x = backcone(input_image)
x = tf.keras.layers.Conv2D(classes, (3, 3), padding='same', name='final_conv')(input)
x = tf.keras.layers.Activation(activation, name=activation)(x)
model = tf.keras.Model(input, x)

#... some additional code
# training part
optimizer = tf.keras.optimizers.Adam(lr=FLAGS.learning_rate)
model.compile(loss=loss,
              optimizer=optimizer,
              metrics=['accuracy', f1_m, recall_m, precision_m])

callbacks = [tf.keras.callbacks.ModelCheckpoint(filepath=ckpt_name)]

model.fit(train_generator, validation_data=validation_generator, validation_freq=1, epochs=10, callbacks=callbacks)

# using the callback there will weights saved in cktp_name each epoch
# Inference part, just need to reinstance the model (lines after #Change part comment)

model.load_weights(ckpt_name)

results = model.predict(test_generator, verbose=1)

You don't need to change the model obviously, you could have used x = backbone(x) and then model = tf.keras.Model(input, x)

Orphee Faucoz
  • 1,220
  • 5
  • 9
  • Is it possible to import my own weights? I'm looking into a project using ResNet. – Jerome Ariola Jan 28 '20 at 09:18
  • Yeah sure, let's say that you have made a training using keras, at the end of your training you can save the weights. If you want to use them afterwards, you just need to reinstance your model (with same layers as before) and then load the weights, take a look at [this stack answer](https://stackoverflow.com/questions/47266383/save-and-load-weights-in-keras) – Orphee Faucoz Jan 28 '20 at 09:22
  • I'm adding on the post on how I infer using my own weights – Orphee Faucoz Jan 28 '20 at 09:22