본문 바로가기
Deep Learning

[Keras] VGG-net

by xangmin 2022. 9. 1.
반응형

Keras로 작성된 VGG 종류에 따른 코드이다.

VGG11, VGG11-LRN, VGG13, VGG16, VGG16-conv1 모델들이다. 

VGG11

def create_model(input_shape, label_class_cnt: int):
  """
  :param input_shape: 입력 이미지 형태
  :param label_class_cnt: 출력 class 수
  :return: 생성된 모델
  """
  # weight 초기화
  He_normal = initializers.he_normal(seed=None)

  model = Sequential()

  model.add(Conv2D(64, kernel_size=(3, 3),activation='relu',padding='same', input_shape=input_shape, kernel_initializer=He_normal))
  model.add(MaxPooling2D(pool_size=(2, 2)))

  model.add(Conv2D(128, (3, 3), activation='relu',padding='same', kernel_initializer=He_normal))
  model.add(MaxPooling2D(pool_size=(2, 2)))

  model.add(Conv2D(256, (3, 3), activation='relu',padding='same', kernel_initializer=He_normal))
  model.add(Conv2D(256, (3, 3), activation='relu',padding='same', kernel_initializer=He_normal))
  model.add(MaxPooling2D(pool_size=(2, 2)))

  model.add(Conv2D(512, (3, 3), activation='relu',padding='same', kernel_initializer=He_normal))
  model.add(Conv2D(512, (3, 3), activation='relu',padding='same', kernel_initializer=He_normal))
  model.add(MaxPooling2D(pool_size=(2, 2)))

  model.add(Conv2D(512, (3, 3), activation='relu',padding='same', kernel_initializer=He_normal))
  model.add(Conv2D(512, (3, 3), activation='relu',padding='same', kernel_initializer=He_normal))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  
  model.add(Flatten())
  model.add(Dense(4096,activation='sigmoid' , kernel_initializer=He_normal)) # activation='relu'
  model.add(Dropout(0.5))
  model.add(Dense(4096, activation='sigmoid', kernel_initializer=He_normal)) # activation='relu'
  model.add(Dropout(0.5))
  model.add(Dense(1000,activation='sigmoid', kernel_initializer=He_normal)) #  activation='relu'
  model.add(Dropout(0.5))
  model.add(Dense(label_class_cnt, activation='sigmoid'))

  return model

 

VGG11-LRN

def create_model(input_shape, label_class_cnt: int):
  """
  :param input_shape: 입력 이미지 형태
  :param label_class_cnt: 출력 class 수
  :return: 생성된 모델
  """
  # weight 초기화
  He_normal = initializers.he_normal(seed=None)

  model = Sequential()

  model.add(Conv2D(64, kernel_size=(3, 3),activation='relu',padding='same', input_shape=input_shape, kernel_initializer=He_normal))
  model.add(BatchNormalization())
  model.add(MaxPooling2D(pool_size=(2, 2)))

  model.add(Conv2D(128, (3, 3), activation='relu',padding='same', kernel_initializer=He_normal))
  model.add(MaxPooling2D(pool_size=(2, 2)))

  model.add(Conv2D(256, (3, 3), activation='relu',padding='same', kernel_initializer=He_normal))
  model.add(Conv2D(256, (3, 3), activation='relu',padding='same', kernel_initializer=He_normal))
  model.add(MaxPooling2D(pool_size=(2, 2)))

  model.add(Conv2D(512, (3, 3), activation='relu',padding='same', kernel_initializer=He_normal))
  model.add(Conv2D(512, (3, 3), activation='relu',padding='same', kernel_initializer=He_normal))
  model.add(MaxPooling2D(pool_size=(2, 2)))

  model.add(Conv2D(512, (3, 3), activation='relu',padding='same', kernel_initializer=He_normal))
  model.add(Conv2D(512, (3, 3), activation='relu',padding='same', kernel_initializer=He_normal))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  
  model.add(Flatten())
  model.add(Dense(4096, activation='sigmoid', kernel_initializer=He_normal)) # activation='relu'
  model.add(Dropout(0.5))
  model.add(Dense(4096, activation='sigmoid', kernel_initializer=He_normal)) # activation='relu'
  model.add(Dropout(0.5))
  model.add(Dense(1000, activation='sigmoid', kernel_initializer=He_normal)) # activation='relu'
  model.add(Dropout(0.5))
  model.add(Dense(label_class_cnt, activation='sigmoid'))

  return model

 

VGG13

def create_model(input_shape, label_class_cnt: int):
  """
  :param input_shape: 입력 이미지 형태
  :param label_class_cnt: 출력 class 수
  :return: 생성된 모델
  """
  # weight 초기화
  He_normal = initializers.he_normal(seed=None)

  model = Sequential()

  model.add(Conv2D(64, kernel_size=(3, 3),activation='relu',padding='same', input_shape=input_shape, kernel_initializer=He_normal))
  model.add(Conv2D(64, kernel_size=(3, 3),activation='relu',padding='same', kernel_initializer=He_normal))
  model.add(MaxPooling2D(pool_size=(2, 2)))

  model.add(Conv2D(128, (3, 3), activation='relu',padding='same', kernel_initializer=He_normal))
  model.add(Conv2D(128, (3, 3), activation='relu',padding='same', kernel_initializer=He_normal))
  model.add(MaxPooling2D(pool_size=(2, 2)))

  model.add(Conv2D(256, (3, 3), activation='relu',padding='same', kernel_initializer=He_normal))
  model.add(Conv2D(256, (3, 3), activation='relu',padding='same', kernel_initializer=He_normal))
  model.add(MaxPooling2D(pool_size=(2, 2)))

  model.add(Conv2D(512, (3, 3), activation='relu',padding='same', kernel_initializer=He_normal))
  model.add(Conv2D(512, (3, 3), activation='relu',padding='same', kernel_initializer=He_normal))
  model.add(MaxPooling2D(pool_size=(2, 2)))

  model.add(Conv2D(512, (3, 3), activation='relu',padding='same', kernel_initializer=He_normal))
  model.add(Conv2D(512, (3, 3), activation='relu',padding='same', kernel_initializer=He_normal))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  
  model.add(Flatten())
  model.add(Dense(4096, activation='sigmoid', kernel_initializer=He_normal)) # activation='relu'
  model.add(Dropout(0.5))
  model.add(Dense(4096,activation='sigmoid', kernel_initializer=He_normal)) #  activation='relu',
  model.add(Dropout(0.5))
  model.add(Dense(1000,activation='sigmoid' , kernel_initializer=He_normal)) # activation='relu'
  model.add(Dropout(0.5))
  model.add(Dense(label_class_cnt, activation='sigmoid'))

  return model

 

VGG16

def create_model(input_shape, label_class_cnt: int):
  """
  :param input_shape: 입력 이미지 형태
  :param label_class_cnt: 출력 class 수
  :return: 생성된 모델
  """
  # weight 초기화
  He_normal = initializers.he_normal(seed=None)

  model = Sequential()

  model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', input_shape=input_shape, kernel_initializer=He_normal))
  model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', kernel_initializer=He_normal))
  model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

  model.add(Conv2D(128, kernel_size=(3, 3), activation='relu', kernel_initializer=He_normal))
  model.add(Conv2D(128, kernel_size=(3, 3), activation='relu', kernel_initializer=He_normal))
  model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

  model.add(Conv2D(256, kernel_size=(3, 3), activation='relu', kernel_initializer=He_normal))
  model.add(Conv2D(256, kernel_size=(3, 3), activation='relu', kernel_initializer=He_normal))
  model.add(Conv2D(256, kernel_size=(3, 3), activation='relu', kernel_initializer=He_normal))
  model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
  
  model.add(Conv2D(512, kernel_size=(3, 3), activation='relu', kernel_initializer=He_normal))
  model.add(Conv2D(512, kernel_size=(3, 3), activation='relu', kernel_initializer=He_normal))
  model.add(Conv2D(512, kernel_size=(3, 3), activation='relu', kernel_initializer=He_normal))
  model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

  model.add(Conv2D(512, kernel_size=(3, 3), activation='relu', kernel_initializer=He_normal))
  model.add(Conv2D(512, kernel_size=(3, 3), activation='relu', kernel_initializer=He_normal))
  model.add(Conv2D(512, kernel_size=(3, 3), activation='relu', kernel_initializer=He_normal))
  model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

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

  return model

 

VGG16-conv1

def create_model(input_shape, label_class_cnt: int):
  """
  :param input_shape: 입력 이미지 형태
  :param label_class_cnt: 출력 class 수
  :return: 생성된 모델
  """
  # weight 초기화
  He_normal = initializers.he_normal(seed=None)

  model = Sequential()

  model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', input_shape=input_shape, kernel_initializer=He_normal))
  model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', kernel_initializer=He_normal))
  model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

  model.add(Conv2D(128, kernel_size=(3, 3), activation='relu', kernel_initializer=He_normal))
  model.add(Conv2D(128, kernel_size=(3, 3), activation='relu', kernel_initializer=He_normal))
  model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

  model.add(Conv2D(256, kernel_size=(3, 3), activation='relu', kernel_initializer=He_normal))
  model.add(Conv2D(256, kernel_size=(3, 3), activation='relu', kernel_initializer=He_normal))
  model.add(Conv2D(256, kernel_size=(1, 1), activation='relu', kernel_initializer=He_normal))
  model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
  
  model.add(Conv2D(512, kernel_size=(3, 3), activation='relu', kernel_initializer=He_normal))
  model.add(Conv2D(512, kernel_size=(3, 3), activation='relu', kernel_initializer=He_normal))
  model.add(Conv2D(512, kernel_size=(1, 1), activation='relu', kernel_initializer=He_normal))
  model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

  model.add(Conv2D(512, kernel_size=(3, 3), activation='relu', kernel_initializer=He_normal))
  model.add(Conv2D(512, kernel_size=(3, 3), activation='relu', kernel_initializer=He_normal))
  model.add(Conv2D(512, kernel_size=(1, 1), activation='relu', kernel_initializer=He_normal))
  model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

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

  return model
반응형

댓글