简体   繁体   中英

How to use pre-trained models without classes in Tensorflow?

I'm trying to use a pretrained network such as tf.keras.applications.ResNet50 but I have two problems:

I just want to obtain the top embedding layers at the end of the network, because I don't want to do any image classification. So due to this there is no need for a classes number I think.

  • tf.keras.applications.ResNet50 takes a default parameter 'classes=1000'

    • Is there a way how I can omit this parameter?
  • My input pictures are 128*128*1 pixels and not 224*224*3

    • What is the best way to fix my input data shape?

My goal is to make a triplet loss network with the output of a resnet network.

Thanks a lot!

  • ResNet50 has a parameter include_top exactly for that purpose -- set it to False to skip the last fully connected layer. (It then outputs a feature vector of length 2048).
  • The best way to reduce your image size is to resample the images, eg using the dedicated function tf.image.resample_images .

    • Also, I did not notice at first that your input images have only three channels, thx @Daniel. I suggest you build your 3-channel grayscale image on the GPU (not on the host using numpy) to avoid tripling your data transfer to GPU memory, using tf.tile :

       im3 = tf.tile(im, (1, 1, 1, 3)) 

As a complement to the other answer. You will also need to make your images have three channels, although technically not the best input for Resnet, it is the easiest solution (changing the Resnet model is an option too, if you visit the source code and change the input shape yourself).

Use numpy to pack images in three channels:

images3ch = np.concatenate([images,images,images], axis=-1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM