修改Resnet模型中的图层 [英] Modify layers in resnet model

查看:417
本文介绍了修改Resnet模型中的图层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试针对图像分类问题训练resnet50模型.在对我拥有的数据集进行模型训练之前,我已经加载了预训练的"imagenet"权重.我想在输入层和第一个卷积层之间插入一个层(均值减法层).

I am trying to train resnet50 model for image classification problem. I have loaded the pretrained 'imagenet' weights before training the model on the dataset I have. I want to insert a layer (mean subtraction layer) in-between the input layer and the first convolutiuon layer.

model = ResNet50(weights='imagenet')
def mean_subtract(img):
    img = T.set_subtensor(img[:,0,:,:],img[:,0,:,:] - 123.68)
    img = T.set_subtensor(img[:,1,:,:],img[:,1,:,:] - 116.779)
    img = T.set_subtensor(img[:,2,:,:],img[:,2,:,:] - 103.939)

    return img / 255.0

我想在输入层旁边插入inputs = Lambda(mean_subtract, name='mean_subtraction')(inputs)并将其连接到resnet模型的第一个卷积层,而不会丢失已保存的权重.

I want to insert inputs = Lambda(mean_subtract, name='mean_subtraction')(inputs) next to the input layer and connect this to the first convolution layer of resnet model without losing the weights saved.

我该怎么做?

谢谢!

推荐答案

快速解答(似乎比将函数添加到模型更好)

Quick answer (Seems better than adding the function to the model)

按如下所述使用预处理功能:使用keras函数ImageDataGenerator()生成的图像进行预处理以训练resnet50模型

Use the preprocessing function as described here: preprocessing images generated using keras function ImageDataGenerator() to train resnet50 model

好答案

由于您的函数不会改变形状,因此您可以将其放在外部模型中而无需更改Resnet模型(更改模型可能不是那么简单,如果需要,我总是尝试将新模型与其他模型的零件一起安装).

Since your function doesn't change shapes, you can put it in an outer model without changing the Resnet model (changing models may not be so simple, I always try to mount new models with parts from other models if needed).

resnet_model = ResNet50(weights='imagenet')

inputs = Input((None,None,3)) 
    #it seems you're using (3,None,None) instead.    
    #choose based on your "data_format", which by default is channels_last 

outputs = Lambda(mean_subtract,output_shape=not_necessary_with_tensorflow)(inputs)
outputs = resnet_model(outputs)

model = Model(inputs, outputs)

这篇关于修改Resnet模型中的图层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆