[英]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'
My input pictures are 128*128*1
pixels and not 224*224*3
我输入的图片是
128*128*1
像素,而不是224*224*3
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). 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.