简体   繁体   English

如何在Tensorflow中使用不带类的预训练模型?

[英]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: 我正在尝试使用预先训练的网络,例如tf.keras.applications.ResNet50但是我有两个问题:

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' tf.keras.applications.ResNet50采用默认参数'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 我输入的图片是128*128*1像素,而不是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. 我的目标是使用resnet网络的输出resnet三重损耗网络。

Thanks a lot! 非常感谢!

  • ResNet50 has a parameter include_top exactly for that purpose -- set it to False to skip the last fully connected layer. ResNet50正是出于这个目的ResNet50具有一个参数include_top将其设置为False可以跳过最后一个完全连接的层。 (It then outputs a feature vector of length 2048). (然后输出长度为2048的特征向量)。
  • The best way to reduce your image size is to resample the images, eg using the dedicated function tf.image.resample_images . 减小图像尺寸的最佳方法是对图像重新采样,例如使用专用功能tf.image.resample_images

    • Also, I did not notice at first that your input images have only three channels, thx @Daniel. 另外,起初我没有注意到您的输入图像只有三个通道,即@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 : 我建议您在GPU上构建3通道灰度图像(而不是在使用numpy的主机上),以避免使用tf.tile将数据传输到GPU内存的过程增加三倍:

       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). 您还需要使图像具有三个通道,尽管从技术上讲,这不是Resnet的最佳输入,但这是最简单的解决方案(如果您自己访问源代码并更改输入形状,则更改Resnet模型也是一种选择)。

Use numpy to pack images in three channels: 使用numpy将图像打包到三个通道中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 OpenVINO 预训练模型? - How to use OpenVINO pre-trained models? 如何在TensorFlow中使用预训练模型 - How to use pre-trained model in TensorFlow 如何在 Python 中使用预训练的 CNN 模型 - How to Use Pre-Trained CNN models in Python 如何在Keras损失函数中使用预训练的TensorFlow网络 - How to use a pre-trained TensorFlow net in Keras loss function 如何使用预训练的权重在 tensorflow 中训练卷积神经网络? - How to use pre-trained weight for training convolutional NN in tensorflow? 如何使用预训练模型进行文本分类?比较经过微调的 model 与未经微调的预训练 model - How to use pre-trained models for text classification?Comparing a fine-tuned model with a pre-trained model without fine-tuning 如何在 tensorflow 中使用预训练的 model 进行预测? - how to predict with pre-trained model in tensorflow? 连接来自两个预训练的 Tensorflow 模型的预测 - Concatenate predictions from two pre-trained Tensorflow models 在应用程序中使用预训练模型的硬件要求是什么? - What are the hardware requirements for the use of pre-trained models in applications? 使用fasttext预训练的单词向量作为在tensorflow脚本中的嵌入 - Use of fasttext Pre-trained word vector as embedding in tensorflow script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM