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

六眼飞鱼酱① 提交于 2019-12-08 11:06:16

问题


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!


回答1:


  • 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, e.g. 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))
      



回答2:


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)


来源:https://stackoverflow.com/questions/50646426/how-to-use-pre-trained-models-without-classes-in-tensorflow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!