반응형

1. LeNet-5 코드 

1-1 LeNet-5 코드 function api 

from tensorflow.keras.layers import Conv2D,Dense,Flatten,Input,AveragePooling2D
from tensorflow.keras import Model

image_input = Input(shape=(32, 32, 1))
x = Conv2D(6, kernel_size= (5,5), strides=1,  activation = 'relu')(image_input)
x = AveragePooling2D(2)(x)
x = Conv2D(16, kernel_size= (5,5),strides=1, activation = 'relu')(x)
x = AveragePooling2D(2)(x)

x = Flatten()(x)
x= Dense(units = 120, activation = 'relu')(x)
x= Dense(units = 84, activation = 'relu')(x)
output = Dense(10, activation='softmax')(x)
model = Model(image_input, output)
model.summary()

 

1-2 LeNet-5 

import keras 
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense

model = Sequential()

#Layer 1
#Conv Layer 1
model.add(Conv2D(filters = 6, kernel_size = 5, strides = 1, activation = 'relu', input_shape = (32,32,1)))

#Pooling layer 1
model.add(MaxPooling2D(pool_size = 2, strides = 2))

#Layer 2
#Conv Layer 2
model.add(Conv2D(filters = 16, kernel_size = 5,strides = 1,activation = 'relu'))

#Pooling Layer 2
model.add(MaxPooling2D(pool_size = 2, strides = 2))

#Flatten
model.add(Flatten())

#Layer 3
#Fully connected layer 1
model.add(Dense(units = 120, activation = 'relu'))

#Layer 4
#Fully connected layer 2
model.add(Dense(units = 84, activation = 'relu'))

#Layer 5
#Output Layer
model.add(Dense(units = 10, activation = 'softmax'))
model.summary()

 

AlexNet keras 코드

# 5 convolutional laysers, 3 maxpooling , 3 fully connected
import keras 
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D,Dropout
from keras.layers import Flatten
from keras.layers import Dense

model = Sequential()
model.add(Convolution2D(96, (11, 11), strides=(4, 4), input_shape = (224,224,3),padding='SAME',  activation='relu'))
model.add(MaxPooling2D((3,3) , strides = 2))

model.add(Convolution2D(256, (5, 5), padding = 'same', activation='relu'))
model.add(MaxPooling2D((3,3) , strides = 2))

model.add(Convolution2D(384, (3, 3), padding = 'same', activation='relu'))

model.add(Convolution2D(384, (3, 3), padding = 'same', activation='relu'))

model.add(Convolution2D(256, (3, 3), padding = 'same', activation='relu'))
model.add(MaxPooling2D((3,3) , strides = 1))

model.add(Flatten())
model.add(Dense(4096, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(1000, activation = 'softmax'))

model.summary()

 

VGG코드

VGG16 tensorlfow 코드

import tensorflow as tf
model = tf.keras.applications.VGG16(
    include_top=True, weights='imagenet', input_tensor=None,
    input_shape=None, pooling=None, classes=1000,
    classifier_activation='softmax'
)
model.summary()

block 1 2 1

block 2 2 1

block 3 2 1

block 4 2 1

block 5 3 1

flatten

dense 3

 

from tensorflow.keras.layers import Conv2D,Dense,Flatten,Input,MaxPooling2D
from tensorflow.keras import Model

image_input = Input(shape=(224, 224, 3))
x = Conv2D(64, kernel_size= (3,3), padding='same',name='block1_conv1')(image_input)
x = Conv2D(64, kernel_size= (3,3), padding='same',name='block1_conv2')(x)
x = MaxPooling2D(2 ,name ='block1_pool')(x)

x = Conv2D(128, kernel_size= (3,3), padding='same',name='block2_conv1')(x)
x = Conv2D(128, kernel_size= (3,3), padding='same',name='block2_conv2')(x)
x = MaxPooling2D(2 ,name ='block2_pool')(x)


x = Conv2D(256, kernel_size= (3,3), padding='same',name='block3_conv1')(x)
x = Conv2D(256, kernel_size= (3,3), padding='same',name='block3_conv2')(x)
x = MaxPooling2D(2 ,name ='block3_pool')(x)

x = Conv2D(512, kernel_size= (3,3), padding='same',name='block4_conv1')(x)
x = Conv2D(512, kernel_size= (3,3), padding='same',name='block4_conv2')(x)
x = MaxPooling2D(2 ,name ='block4_pool')(x)

x = Conv2D(512, kernel_size= (3,3), padding='same',name='block5_conv1')(x)
x = Conv2D(512, kernel_size= (3,3), padding='same',name='block5_conv2')(x)
x = Conv2D(512, kernel_size= (3,3), padding='same',name='block5_conv3')(x)
x = MaxPooling2D(2 ,name ='block5_pool')(x)

x = Flatten()(x)
x= Dense(units = 4096, activation = 'relu',name='fc1')(x)
x= Dense(units = 4096, activation = 'relu',name='fc2')(x)
output = Dense(1000, activation='softmax' ,name='predictions')(x)
model = Model(image_input, output)
model.summary()

www.tensorflow.org/api_docs/python/tf/keras/applications/VGG16

 

tf.keras.applications.VGG16  |  TensorFlow Core v2.4.1

Instantiates the VGG16 model.

www.tensorflow.org

VGG19 tensorlfow 코드

import tensorflow as tf
model = tf.keras.applications.VGG19(
    include_top=True, weights='imagenet', input_tensor=None,
    input_shape=None, pooling=None, classes=1000,
    classifier_activation='softmax'
)
model.summary()
from tensorflow.keras.layers import Conv2D,Dense,Flatten,Input,MaxPooling2D
from tensorflow.keras import Model

image_input = Input(shape=(224, 224, 3))
x = Conv2D(64, kernel_size= (3,3), padding='same',name='block1_conv1')(image_input)
x = Conv2D(64, kernel_size= (3,3), padding='same',name='block1_conv2')(x)
x = MaxPooling2D(2 ,name ='block1_pool')(x)

x = Conv2D(128, kernel_size= (3,3), padding='same',name='block2_conv1')(x)
x = Conv2D(128, kernel_size= (3,3), padding='same',name='block2_conv2')(x)
x = MaxPooling2D(2 ,name ='block2_pool')(x)


x = Conv2D(256, kernel_size= (3,3), padding='same',name='block3_conv1')(x)
x = Conv2D(256, kernel_size= (3,3), padding='same',name='block3_conv2')(x)
x = Conv2D(256, kernel_size= (3,3), padding='same',name='block3_conv3')(x)
x = Conv2D(256, kernel_size= (3,3), padding='same',name='block3_conv4')(x)
x = MaxPooling2D(2 ,name ='block3_pool')(x)

x = Conv2D(512, kernel_size= (3,3), padding='same',name='block4_conv1')(x)
x = Conv2D(512, kernel_size= (3,3), padding='same',name='block4_conv2')(x)
x = Conv2D(512, kernel_size= (3,3), padding='same',name='block4_conv3')(x)
x = Conv2D(512, kernel_size= (3,3), padding='same',name='block4_conv4')(x)
x = MaxPooling2D(2 ,name ='block4_pool')(x)

x = Conv2D(512, kernel_size= (3,3), padding='same',name='block5_conv1')(x)
x = Conv2D(512, kernel_size= (3,3), padding='same',name='block5_conv2')(x)
x = Conv2D(512, kernel_size= (3,3), padding='same',name='block5_conv3')(x)
x = Conv2D(512, kernel_size= (3,3), padding='same',name='block5_conv4')(x)
x = MaxPooling2D(2 ,name ='block5_pool')(x)

x = Flatten()(x)
x= Dense(units = 4096, activation = 'relu',name='fc1')(x)
x= Dense(units = 4096, activation = 'relu',name='fc2')(x)
output = Dense(1000, activation='softmax' ,name='predictions')(x)
model = Model(image_input, output)
model.summary()

ResNet코드

ResNet50 tensorflow code

model = tf.keras.applications.ResNet50(
    include_top=True, weights='imagenet', input_tensor=None,
    input_shape=None, pooling=None, classes=1000
)
model.summary()

 

ResNet101 tensorflow code

model = tf.keras.applications.ResNet101(
    include_top=True, weights='imagenet', input_tensor=None,
    input_shape=None, pooling=None, classes=1000
)
model.summary()

ResNet152 tensorflow code

model = tf.keras.applications.ResNet152(
    include_top=True, weights='imagenet', input_tensor=None,
    input_shape=None, pooling=None, classes=1000
)
model.summary()

 

keras.io/api/applications/

 

Keras documentation: Keras Applications

Keras Applications Keras Applications are deep learning models that are made available alongside pre-trained weights. These models can be used for prediction, feature extraction, and fine-tuning. Weights are downloaded automatically when instantiating a mo

keras.io

resnet 50 tensorflow 

 

github.com/keras-team/keras-applications/blob/bc89834ed36935ab4a4994446e34ff81c0d8e1b7/keras_applications/resnet50.py#L37

def conv_block(input_tensor,
               kernel_size,
               filters,
               stage,
               block,
               strides=(2, 2)):
    bn_axis = 3
    filters1, filters2, filters3 = filters
    conv_name_base = 'conv'+str(stage)+'_block' + block 

    bn_name_base = 'conv' + str(stage) +'_block'+ block
    relu_name_base = 'conv' + str(stage) +'_block'+ block

    x = layers.Conv2D(filters1, (1, 1), strides=strides,
                      kernel_initializer='he_normal',
                      name=conv_name_base + '_1_conv')(input_tensor)
    x = layers.BatchNormalization(axis=bn_axis, name=bn_name_base + '_1_bn')(x)
    x = layers.Activation('relu',name = relu_name_base+'_1_relu')(x)

    x = layers.Conv2D(filters2, kernel_size, padding='same',
                      kernel_initializer='he_normal',
                      name=conv_name_base + '_2_conv')(x)
    x = layers.BatchNormalization(axis=bn_axis, name=bn_name_base + '_2_bn')(x)
    x = layers.Activation('relu',name = relu_name_base+'_2_relu')(x)

    x = layers.Conv2D(filters3, (1, 1),
                      kernel_initializer='he_normal',
                      name=conv_name_base + '_0_conv')(x)
    x = layers.BatchNormalization(axis=bn_axis, name=bn_name_base + '_0_bn')(x)

    shortcut = layers.Conv2D(filters3, (1, 1), strides=strides,
                             kernel_initializer='he_normal',
                             name=conv_name_base + '_3_conv')(input_tensor)
    shortcut = layers.BatchNormalization(
        axis=bn_axis, name=bn_name_base + '_3_bn')(shortcut)

    x = layers.add([x, shortcut] ,name = 'conv'+str(stage)+'_block'+block+'_add')
    x = layers.Activation('relu',name = 'conv'+str(stage)+'_block'+block+'_out')(x)
    return x
 

def identity_block(input_tensor, kernel_size, filters, stage, block):
    """The identity block is the block that has no conv layer at shortcut.
    # Arguments
        input_tensor: input tensor
        kernel_size: default 3, the kernel size of
            middle conv layer at main path
        filters: list of integers, the filters of 3 conv layer at main path
        stage: integer, current stage label, used for generating layer names
        block: 'a','b'..., current block label, used for generating layer names
    # Returns
        Output tensor for the block.
    """
    filters1, filters2, filters3 = filters
    bn_axis = 3
    conv_name_base = 'conv' + str(stage) +'_block' + '_'+block
    bn_name_base = 'conv' + str(stage) +'_block' + block
    relu_name_base = 'conv' + str(stage) +'_block'+ block

    x = layers.Conv2D(filters1, (1, 1),
                      kernel_initializer='he_normal',
                      name=conv_name_base + '_1_conv')(input_tensor)
    x = layers.BatchNormalization(axis=bn_axis, name=bn_name_base + '_1_bn')(x)
    x = layers.Activation('relu',name = relu_name_base+'_1_relu')(x)

    x = layers.Conv2D(filters2, kernel_size,
                      padding='same',
                      kernel_initializer='he_normal',
                      name=conv_name_base + '_2_conv')(x)
    x = layers.BatchNormalization(axis=bn_axis, name=bn_name_base + '_2_bn')(x)
    x = layers.Activation('relu',name = relu_name_base+'_2_relu')(x)

    x = layers.Conv2D(filters3, (1, 1),
                      kernel_initializer='he_normal',
                      name=conv_name_base + '_3_conv')(x)
    x = layers.BatchNormalization(axis=bn_axis, name=bn_name_base + '_3_bn')(x)

    x = layers.add([x, input_tensor],name = 'conv'+str(stage)+'_block'+block+'_add')
    x = layers.Activation('relu',name = 'conv'+str(stage)+'_block'+block+'_out')(x)
    return x

from tensorflow.keras.layers import Conv2D,Dense,Flatten,Input,MaxPooling2D,ZeroPadding2D,BatchNormalization,Activation
from tensorflow.keras import Model
from tensorflow.keras import layers

image_input = Input(shape=(224, 224, 3))
x = ZeroPadding2D(3,name='conv1_pad')(image_input)
x = Conv2D(64, kernel_size= (7,7), strides=(2, 2),padding='valid',name='conv1_conv')(x) 
x = BatchNormalization(axis=3, name='bn_conv1')(x) 
x = Activation('relu', name='conv1_relu')(x)    
x = ZeroPadding2D(padding=(1, 1),name='pool1_pad')(x)      
x = MaxPooling2D((3, 3), strides=(2, 2),name='pool1_pool')(x) 
               
x = conv_block(x, 3, [64, 64, 256], stage=2, block='1', strides=(1, 1))
x = identity_block(x, 3, [64, 64, 256], stage=2, block='2')
x = identity_block(x, 3, [64, 64, 256], stage=2, block='3')

x = conv_block(x, 3, [128, 128, 512], stage=3, block='1')
x = identity_block(x, 3, [128, 128, 512], stage=3, block='2')
x = identity_block(x, 3, [128, 128, 512], stage=3, block='3')
x = identity_block(x, 3, [128, 128, 512], stage=3, block='4')

x = conv_block(x, 3, [256, 256, 1024], stage=4, block='1')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='2')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='3')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='4')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='5')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='6')

x = conv_block(x, 3, [512, 512, 2048], stage=5, block='1')
x = identity_block(x, 3, [512, 512, 2048], stage=5, block='2')
x = identity_block(x, 3, [512, 512, 2048], stage=5, block='3')

x = layers.GlobalAveragePooling2D(name='avg_pool')(x)

#https://github.com/keras-team/keras-applications/blob/master/keras_applications/resnet50.py

x = Flatten()(x)
x= Dense(units = 4096, activation = 'relu',name='fc1')(x)
x= Dense(units = 4096, activation = 'relu',name='fc2')(x)
output = Dense(1000, activation='softmax' ,name='predictions')(x)
model = Model(image_input, output)
print(model.summary())
반응형

'Deep learning > 소스' 카테고리의 다른 글

yolov5 coco data set training  (0) 2021.09.17
classification mnist-LeNet-5  (0) 2021.04.03
yolov5 모델pt  (0) 2021.03.26
python 한국 시간으로 설정  (0) 2020.11.17
20201110-yolov5로 데이터 학습  (2) 2020.11.04

+ Recent posts