cytomining / DeepProfiler

Morphological profiling using deep learning
Other
106 stars 39 forks source link

finetune the model you provided error #347

Open shihanyu opened 1 year ago

shihanyu commented 1 year ago

I wanted to finetune the model "Cell_Painting_CNN_v1.hdf5", but error accured. says(1280,490) and (1280,327) does not match.

and I compared "Cell_Painting_CNN_v1.hdf5" and a model I trained from pretrained model in imagenet,their sizes are different.

so is there any way to let me finetune the model based on "Cell_Painting_CNN_v1.hdf5"?

jccaicedo commented 1 year ago

It should be possible to fine-tune it. Are you trying to finetune on a set of Cell Painting images? What is the size of your input images, including height, width and channels?

jccaicedo commented 1 year ago

@Arkkienkeli do you have any suggestion?

Arkkienkeli commented 1 year ago

Hi @shihanyu, it looks like that you load classification head, while in finetuning you need to you use your own classification head (490 is the number of classes we used to train the model, 327 is apparently the number of classes in your dataset).

Here is the short example how you could use the model outside DeepProfiler:

#Input shape
image_shape = (128,128,5)
input_image = tf.compat.v1.keras.layers.Input(image_shape)

# Initilize model object, without weights for now. 
model_conv_cnn = efn.EfficientNetB0(input_tensor=input_image, include_top = False, weights = None)

features = tf.compat.v1.keras.layers.GlobalAveragePooling2D(name="pool5")(model_conv_cnn.layers[-1].output)

# Number of classes from your dataset. 490 - number of classes in Cell Painting dataset. Make an output layer. You can change the number of classes as you wish. 
number_of_classes = 490
class_outputs = []
y = tf.compat.v1.keras.layers.Dense(number_of_classes, activation="softmax", name="ClassProb")(features)
class_outputs.append(y)

# Join new input and output layers with the model and load weights. 
model_conv_cnn = tf.compat.v1.keras.models.Model(inputs=model_conv_cnn.input, outputs=class_outputs)
model_conv_cnn.load_weights('Cell_Painting_CNN_v1.hdf5')

# Compile a model. 
optimizer = tf.compat.v1.keras.optimizers.SGD()
loss = tf.compat.v1.keras.losses.CategoricalCrossentropy()

model_conv_cnn.compile(optimizer, loss)