1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
""" # @File : model_vggnet.py # @Time : # @Author :0399 # @version :python 3.9 # @Software : PyCharm # @Description: """
import tensorflow as tf from tensorflow.keras import layers, Model, Sequential
CONV_KERNEL_INITIALIZER = { 'class_name': 'VarianceScaling', 'config': { 'scale': 2.0, 'mode': 'fan_out', 'distribution': 'truncated_normal' } }
DENSE_KERNEL_INITIALIZER = { 'class_name': 'VarianceScaling', 'config': { 'scale': 1. / 3., 'mode': 'fan_out', 'distribution': 'uniform' } }
def VGG(feature, im_height=224, im_width=224, num_classes=1000): input_image = layers.Input(shape=(im_height, im_width, 3), dtype="float32") x = feature(input_image) x = layers.Flatten()(x) x = layers.Dropout(rate=0.5)(x) x = layers.Dense(2048, activation='relu', kernel_initializer=DENSE_KERNEL_INITIALIZER)(x) x = layers.Dropout(rate=0.5)(x) x = layers.Dense(2048, activation='relu', kernel_initializer=DENSE_KERNEL_INITIALIZER)(x) x = layers.Dropout(rate=0.5)(x) x = layers.Dense(num_classes, activation='relu', kernel_initializer=DENSE_KERNEL_INITIALIZER)(x) output = layers.Softmax()(x)
model = Model(inputs=input_image, outputs=output) return model
def make_feature(cfg): feature_layers = [] for v in cfg: if v == 'M': feature_layers.append(layers.MaxPool2D(pool_size=2, strides=2)) else: feature_layers.append(layers.Conv2D(v, kernel_size=3, padding="same", activation="relu", kernel_initializer=CONV_KERNEL_INITIALIZER)) return Sequential(feature_layers, name="feature")
cfgs = { 'vgg11': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'], 'vgg13': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'], 'vgg16': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'], 'vgg19': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'], }
def vgg(model_name="vgg16", im_height=224, im_width=224, num_classes=1000): cfg = cfgs[model_name] model = VGG(make_feature(cfg), im_height=im_height, im_width=im_width, num_classes=num_classes) return model
|