首页 > 其他分享 >实验15-使用RESNET完成图像分类

实验15-使用RESNET完成图像分类

时间:2024-06-05 21:22:14浏览次数:17  
标签:kernel 15 RESNET strides filters 图像 input block size

model.py

# -*-coding:utf-8-*-
"""author: Zhou Chen
   datetime: 2019/6/25 9:10
   desc: 实现模型
"""
from keras.models import Model
from keras.layers import Conv2D, MaxPooling2D, BatchNormalization, Flatten, Input, ZeroPadding2D, AveragePooling2D, Dense
from keras.layers import add


def Conv2D_BN(x, filters, kernel_size, strides=(1, 1), padding='same', name=None):
    if name:
        bn_name = name + '_bn'
        conv_name = name + '_conv'
    else:
        bn_name = None
        conv_name = None
    x = Conv2D(filters, kernel_size, strides=strides, padding=padding, activation='relu', name=conv_name)(x)
    x = BatchNormalization(name=bn_name)(x)
    return x


def identity_block(input_tensor, filters, kernel_size, strides=(1, 1), is_conv_shortcuts=False):
    """

    :param input_tensor:
    :param filters:
    :param kernel_size:
    :param strides:
    :param is_conv_shortcuts: 直接连接或者投影连接
    :return:
    """
    x = Conv2D_BN(input_tensor, filters, kernel_size, strides=strides, padding='same')
    x = Conv2D_BN(x, filters, kernel_size, padding='same')
    if is_conv_shortcuts:
        shortcut = Conv2D_BN(input_tensor, filters, kernel_size, strides=strides, padding='same')
        x = add([x, shortcut])
    else:
        x = add([x, input_tensor])
    return x


def bottleneck_block(input_tensor, filters=(64, 64, 256), strides=(1, 1), is_conv_shortcuts=False):
    """

    :param input_tensor:
    :param filters:
    :param strides:
    :param is_conv_shortcuts: 直接连接或者投影连接
    :return:
    """
    filters_1, filters_2, filters_3 = filters
    x = Conv2D_BN(input_tensor, filters=filters_1, kernel_size=(1, 1), strides=strides, padding='same')
    x = Conv2D_BN(x, filters=filters_2, kernel_size=(3, 3))
    x = Conv2D_BN(x, filters=filters_3, kernel_size=(1, 1))
    if is_conv_shortcuts:
        short_cut = Conv2D_BN(input_tensor, filters=filters_3, kernel_size=(1, 1), strides=strides)
        x = add([x, short_cut])
    else:
        x = add([x, input_tensor])
    return x


def ResNet34(input_shape=(224, 224, 3), n_classes=1000):
    """

    :param input_shape:
    :param n_classes:
    :return:
    """

    input_layer = Input(shape=input_shape)
    x = ZeroPadding2D((3, 3))(input_layer)
    # block1
    x = Conv2D_BN(x, filters=64, kernel_size=(7, 7), strides=(2, 2), padding='valid')
    x = MaxPooling2D(pool_size=(3, 3), strides=2, padding='same')(x)
    # block2
    x = identity_block(x, filters=64, kernel_size=(3, 3))
    x = identity_block(x, filters=64, kernel_size=(3, 3))
    x = identity_block(x, filters=64, kernel_size=(3, 3))
    # block3
    x = identity_block(x, filters=128, kernel_size=(3, 3), strides=(2, 2), is_conv_shortcuts=True)
    x = identity_block(x, filters=128, kernel_size=(3, 3))
    x = identity_block(x, filters=128, kernel_size=(3, 3))
    x = identity_block(x, filters=128, kernel_size=(3, 3))
    # block4
    x = identity_block(x, filters=256, kernel_size=(3, 3), strides=(2, 2), is_conv_shortcuts=True)
    x = identity_block(x, filters=256, kernel_size=(3, 3))
    x = identity_block(x, filters=256, kernel_size=(3, 3))
    x = identity_block(x, filters=256, kernel_size=(3, 3))
    x = identity_block(x, filters=256, kernel_size=(3, 3))
    x = identity_block(x, filters=256, kernel_size=(3, 3))
    # block5
    x = identity_block(x, filters=512, kernel_size=(3, 3), strides=(2, 2), is_conv_shortcuts=True)
    x = identity_block(x, filters=512, kernel_size=(3, 3))
    x = identity_block(x, filters=512, kernel_size=(3, 3))
    x = AveragePooling2D(pool_size=(7, 7))(x)
    x = Flatten()(x)
    x = Dense(n_classes, activation='softmax')(x)

    model = Model(inputs=input_layer, outputs=x)
    return model


def ResNet50(input_shape=(224, 224, 3), n_classes=1000):
    """

    :param input_shape:
    :param n_classes:
    :return:
    """
    input_layer = Input(shape=input_shape)
    x = ZeroPadding2D((3, 3))(input_layer)
    # block1
    x = Conv2D_BN(x, filters=64, kernel_size=(7, 7), strides=(2, 2), padding='valid')
    x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')(x)

    # block2
    x = bottleneck_block(x, filters=(64, 64, 256), strides=(1, 1), is_conv_shortcuts=True)
    x = bottleneck_block(x, filters=(64, 64, 256))
    x = bottleneck_block(x, filters=(64, 64, 256))
    # block3
    x = bottleneck_block(x, filters=(128, 128, 512), strides=(2, 2), is_conv_shortcuts=True)
    x = bottleneck_block(x, filters=(128, 128, 512))
    x = bottleneck_block(x, filters=(128, 128, 512))
    x = bottleneck_block(x, filters=(128, 128, 512))
    # block4
    x = bottleneck_block(x, filters=(256, 256, 1024), strides=(2, 2), is_conv_shortcuts=True)
    x = bottleneck_block(x, filters=(256, 256, 1024))
    x = bottleneck_block(x, filters=(256, 256, 1024))
    x = bottleneck_block(x, filters=(256, 256, 1024))
    x = bottleneck_block(x, filters=(256, 256, 1024))
    x = bottleneck_block(x, filters=(256, 256, 1024))
    # block5
    x = bottleneck_block(x, filters=(512, 512, 2048), strides=(2, 2), is_conv_shortcuts=True)
    x = bottleneck_block(x, filters=(512, 512, 2048))
    x = bottleneck_block(x, filters=(512, 512, 2048))
    x = AveragePooling2D(pool_size=(7, 7))(x)
    x = Flatten()(x)
    x = Dense(n_classes, activation='softmax')(x)

    model = Model(inputs=input_layer, outputs=x)
    return model


if __name__ == '__main__':
    resnet34 = ResNet34((224, 224, 3), n_classes=101)
    resnet50 = ResNet50((224, 224, 3), n_classes=101)
    print(resnet34.summary())
    print(resnet50.summary())

 

标签:kernel,15,RESNET,strides,filters,图像,input,block,size
From: https://www.cnblogs.com/liucaizhi/p/18233826

相关文章

  • CSP历年复赛题-P2671 [NOIP2015 普及组] 求和
    原题链接:https://www.luogu.com.cn/problem/P2671题意解读:找到所有符合条件的三元组,累加三元组的分数,结果对10007取模。解题思路:仔细读题,并分析数据规模,1~4个数据点可以通过O(n^2)复杂度解决,也就是枚举法。1、枚举法要求x<y<z,y−x=z−y,移项可得x+z=2*y,并且c......
  • P7860 [COCI2015-2016#2] ARTUR
    原题链接教训1.计算几何,能用乘法就不用除法2.计算几何,开longlong3.计算几何,注意直线的特殊性code#include<bits/stdc++.h>#definelllonglongusingnamespacestd;structnode{llx1,y1,x2,y2;}sk[5005];intcheck(nodea,nodeb){if(a.x2<b.x1||a.x1>b.......
  • Affinity Photo for Mac(专业级图像处理软件)v2.5.2版
    AffinityPhotoforMac是一款专为Mac用户打造的专业级图像处理软件,具备强大的编辑功能,包括图层管理、滤镜效果和色彩调整等,能够满足从摄影师到设计师以及绘画爱好者的各种图像处理需求。它不仅保持了高性能,还注重用户体验和界面设计,使得操作更加直观流畅。此外,AffinityPhoto......
  • 图像检索实践
    1、Related_functions.pyimporttorchfromtorchvisionimportmodels,transformsfromPILimportImageimportosimportnumpyasnpimportwarningswarnings.filterwarnings("ignore",category=Warning)defget_feature(image_dir):vgg_model=......
  • 怎样将图像左右两边对调?5个非常好用的方法分享给你
    今天要给大家介绍的是两种非常高效的对图片进行水平翻转的方法,能轻轻松松、一次性实现将大量的图片进行水平翻转。接下来我们就一起来看下它是如何做到批量将多张图片进行水平翻转的吧。方法一:使用【汇帮图片处理器】里的批量旋转进行操作具体步骤:1,将汇帮图片处理器安装在电......
  • GPEN——使用GANs恢复对人脸图像进行修复
    1.简介盲目的面部修复(BlindFaceRestoration,BFR)是一个活跃的研究领域,它涉及到在没有任何先验信息的情况下改善低质量(LowQuality,LQ)图像的质量。这确实是一个具有挑战性的问题,因为模型需要能够处理多种未知的退化,例如模糊、噪声、压缩伪影等,这些退化可能在训练数据中......
  • 基于Python混沌系统和DNA编码的图像加密算法
    欢迎大家点赞、收藏、关注、评论啦,由于篇幅有限,只展示了部分核心代码。文章目录一项目简介二、功能三、系统四.总结一项目简介  一、项目背景随着互联网和多媒体技术的快速发展,数字图像作为信息传递的重要媒介,在各个领域得到广泛应用。然而,图像信息的传输......
  • 基于Python+OpenCV使用DNA编码和混沌图创建图像加密算法
    欢迎大家点赞、收藏、关注、评论啦,由于篇幅有限,只展示了部分核心代码。文章目录一项目简介二、功能三、系统四.总结一项目简介  一、项目背景与意义在数字信息时代,图像作为信息的重要载体,其安全性尤为重要。传统的图像加密方法往往存在安全性不足、加密效......
  • 【图像处理】交通标志检测(Matlab代码实现)
    ......
  • 5.15
    【附实验报告格式】Pyhton环境与基础训练班级:信2205-1 学号:20224074 姓名:王晨宇一实验目的l 使学生熟悉Python环境的安装与配置,熟悉Python解释器的使用。l 使学生掌握Python控制语句、函数结构等的基本语法知识和使用。l 使学生掌握Python的基本数据类型、列表......