基于tensorflow实现Inception_RestNet_v2

1.Inception-ResNet-V2结构:

2.代码实现

# 构建卷积块
def conv_fn(x,filters,kernel_size,strides,padding="same",activation="relu"):
    x = keras.layers.Conv2D(filters,kernel_size=kernel_size,strides=strides,padding=padding)(x)
    x = keras.layers.BatchNormalization()(x)
    if activation is not None:
        x = keras.activations.get(activation)(x)
    return x
# 构建inception_resnet模块
k = keras.backend
def inception_resnet(x,scale,block_name,activation="relu"):
    if block_name == 'Inception_Resnet_A':
        # 路径1 1*1 32
        branch1 = conv_fn(x,32,1,1)
        # 路径2 1*1 3*3
        branch2 = conv_fn(x,32,1,1)
        branch2 = conv_fn(x,32,32,1)
        # 路径3 1*1—3*3
        branch3 = conv_fn(x,32,1,1)
        branch3 = conv_fn(x,48,3,1)
        branch3 = conv_fn(x,64,32,1)
        
        branch = keras.layers.Concatenate(axis=3)([branch1,branch2,branch3])
        mixed = conv_fn(branch,384,1,1)
        
    elif block_name == 'Inception_Resnet_B':
        # 路径1 1*1 192
        branch1 = conv_fn(x,192,1,1)
        # 路径2 1*1-1*7-7*1 
        branch2 = conv_fn(x,128,1,1)
        branch2 = conv_fn(x,160,(1,7),1)
        branch2 = conv_fn(x,192,(7,1),1)
        
        branch = keras.layers.Concatenate(axis=3)([branch1,branch2])
        mixed = conv_fn(branch,1154,1,1)
        
    elif block_name == 'Inception_Resnet_C':
        # 路径1 1*1 192
        branch1 = conv_fn(x,192,1,1)
        # 路径2 1*1-1*3-3*1
        branch2 = conv_fn(x,192,1,1)
        branch2 = conv_fn(x,224,(1,3),1)
        branch2 = conv_fn(x,224,(3,192),1)
        
        branch = keras.layers.Concatenate(axis=3)([branch1,branch2])
        mixed = mixed = conv_fn(branch,2048,1,1)
        
    up = conv_fn(x,k.int_shape(mixed)[3],1,1,activation=None)
    x = keras.layers.Lambda(lambda inputs:inputs[0]+scale*inputs[1])([up,mixed])
    if activation is not None:
        x = keras.activations.get(activation)(x)
    return x
# 构建Inception_Resnet_v2
def inception_resnet_v2(input_shape,n_classes=1000):
    ##stem
    x_input = keras.layers.Input(shape=input_shape)
    # 149*149*3
    x = conv_fn(x_input,32,3,2,padding="valid")
    # 147*147*32
    x = conv_fn(x,32,3,1,padding="valid")
    # 147*147*64
    x = conv_fn(x,64,3,1)
    
    # 73*73*160
    branch1 = keras.layers.MaxPooling2D(pool_size=3,strides=2,padding="valid")(x)
    branch2 = conv_fn(x,32,3,2,padding="valid")
    x = keras.layers.Concatenate(axis=3)([branch1,branch2])
    
    # 71*71*192
    # 路径1 1*1-3*3 1v
    branch1 = conv_fn(x,64,3,1)
    branch1 = conv_fn(branch1,96,3,1,padding="valid")
    #路径2 1*1-1*7-7*1-3*3 1v
    branch2 = conv_fn(x,64,3,1)
    branch2 = conv_fn(branch2,64,(1,7),1)
    branch2 = conv_fn(branch2,64,(7,1),1)
    branch2 = conv_fn(branch2,96,3,1,padding="valid")
    x = keras.layers.Concatenate(axis=3)([branch1,branch2])
    
    # 35*35*384
    branch1 = keras.layers.MaxPooling2D(pool_size=3,strides=2,padding="valid")(x)
    branch2 = conv_fn(x,192,3,2,padding="valid")
    x = keras.layers.Concatenate(axis=3)([branch1,branch2])
    
    ## 5*inception_resnet_A
    # 35*35*384
    for i in range(5):
        x = inception_resnet(x,0.2,'Inception_Resnet_A')
    
    ## reduction_A
    #17*17*1152
    branch1 = keras.layers.MaxPooling2D(pool_size=3,strides=2,padding="valid")(x)
    branch2 = conv_fn(x,384,3,2,padding="valid")
    branch3 = conv_fn(x,256,1,1)
    branch3 = conv_fn(branch3,256,3,1)
    branch3 = conv_fn(branch3,384,3,2,padding="valid")
    x = keras.layers.Concatenate(axis=3)([branch1,branch2,branch3])
    
    ## 10*inception_resnet_B
    # 17*17*1154
    for i in range(10):
        x = inception_resnet(x,0.2,'Inception_Resnet_B')
    
    ## reduction_B
    # 8*8*2146
    branch1 = keras.layers.MaxPooling2D(pool_size=3,strides=2,padding="valid")(x)
    branch2 = conv_fn(x,256,1,1)
    branch2 = conv_fn(branch2,384,3,2,padding="valid")
    branch3 = conv_fn(x,256,1,1)
    branch3 = conv_fn(branch3,288,3,2,padding="valid")
    branch4 = conv_fn(x,256,1,1)
    branch4 = conv_fn(branch4,288,3,1)
    branch4 = conv_fn(branch4,320,3,2,padding="valid")
    x = keras.layers.Concatenate(axis=3)([branch1,branch2,branch3,branch4])
    
    ## 5*inception_resnet_C
    # 8*8*2048
    for i in range(5):
        x = inception_resnet(x,0.2,'Inception_Resnet_C')
    x = keras.layers.GlobalAveragePooling2D()(x)
    x = keras.layers.Dropout(0.8)(x)
    outputs = keras.layers.Dense(n_classes,activation="softmax")(x)
    model = keras.models.Model(inputs=[x_input],outputs=[outputs])
    return model

3.训练模型(使用指数调度) 

model = inception_resnet_v2([299,299,3],12)
n_epochs = 100
s = 20*len(train_images)//16
learning_rate = keras.optimizers.schedules.ExponentialDecay(0.01,s,0.1)
optimizer = keras.optimizers.SGD(learning_rate,momentum=0.9,nesterov=True)
model.compile(loss="categorical_crossentropy",optimizer=optimizer,metrics=["accuracy"])
history = model.fit(train_images,y_train,epochs=n_epochs,validation_split=0.2,batch_size=16)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在TensorFlow 2.3.0中,tf_slim库已被弃用,因此无法直接使用`from tf_slim.nets import inception_resnet_v2`来引用inception_resnet_v2。但是,您可以使用TensorFlow官方的模型库(tensorflow/models)中的相应模型来代替。 首先,您需要从GitHub上克隆tensorflow/models仓库到本地: ``` git clone https://github.com/tensorflow/models.git ``` 然后,将models/research/slim目录添加到您的Python路径中。您可以通过以下方式实现: ```python import sys sys.path.append('/path/to/models/research/slim') ``` 现在,您可以使用官方模型库中的inception_resnet_v2模型了。示例代码如下: ```python import tensorflow as tf from official.vision.image_classification import imagenet_preprocessing from official.vision.image_classification import resnet_preprocessing # 导入inception_resnet_v2模型 from official.vision.image_classification.resnet import inception_resnet_v2 # 创建模型实例 model = inception_resnet_v2.InceptionResNetV2(weights=None) # 加载预训练权重(如果有的话) model.load_weights('path/to/pretrained/weights.h5') # 预处理输入图像 image_path = 'path/to/image.jpg' image = tf.io.read_file(image_path) image = tf.image.decode_jpeg(image, channels=3) image = resnet_preprocessing.preprocess_image(image, model.input_shape[1], model.input_shape[2]) image = tf.expand_dims(image, axis=0) # 进行推理 predictions = model.predict(image) # 打印预测结果 print(predictions) ``` 请确保您已经安装了所需的依赖项,并将路径替换为适当的路径。这样,您就可以在TensorFlow 2.3.0中使用inception_resnet_v2模型了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

樱花的浪漫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值