Sammo1
(Sammo)
May 22, 2022, 4:04am
#1
I need to extract features from medical images using Pytorch but the features I need are before the final layer for the classification … i used like this
model = VGG16()
model = models.Model(inputs=model.inputs, outputs=model.layers[-2].output)
does that right or the medical needs another way, please ?
kaizhao
(KAI ZHAO)
May 22, 2022, 7:17am
#2
PyTorch does not identify specific image modalities, eg medical image or natural images.
Basically, all you need is a 3-channel tensor, no matter what it contains.
But you may finetune on your own data before extracting features I guess.
1 Like
Hello,
I believe what you wanted is just the convolutional part of VGG16.
This is the base of style-transfer problem in ML.
https://pytorch.org/tutorials/advanced/neural_style_tutorial.html#:~:text=%23%20desired%20depth%20layers,style_losses%2C%20content_losses
Under this link you will find code which extracts tensors of features on different lvls of the network. So effectively you just remove the part of the network you don’t need.
Sammo1
(Sammo)
May 23, 2022, 7:28am
#5
Thanks a lot, i will see it now but excuse me can you help in this
I need to write a code using PyTorch to extract features like this one
def extract_features(image):
feature_extractor = models[config.base_model_name]["model"](
include_top=False, weights="imagenet"
)
print(feature_extractor.summary())
if config.pooling != "avg":
feature_extractor = tf.keras.Model(
feature_extractor.input,
tf.keras.layers.AveragePooling2D(int(config.pooling[0]))(
feature_extractor.output
),
…