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 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| from tensorflow.keras import layers, models, Input from tensorflow.keras.models import Model from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout
def VGG16(nb_classes, input_shape): input_tensor = Input(shape=input_shape) x = Conv2D(64, (3,3), activation='relu', padding='same',name='block1_conv1')(input_tensor) x = Conv2D(64, (3,3), activation='relu', padding='same',name='block1_conv2')(x) x = MaxPooling2D((2,2), strides=(2,2), name = 'block1_pool')(x) x = Conv2D(128, (3,3), activation='relu', padding='same',name='block2_conv1')(x) x = Conv2D(128, (3,3), activation='relu', padding='same',name='block2_conv2')(x) x = MaxPooling2D((2,2), strides=(2,2), name = 'block2_pool')(x) x = Conv2D(256, (3,3), activation='relu', padding='same',name='block3_conv1')(x) x = Conv2D(256, (3,3), activation='relu', padding='same',name='block3_conv2')(x) x = Conv2D(256, (3,3), activation='relu', padding='same',name='block3_conv3')(x) x = MaxPooling2D((2,2), strides=(2,2), name = 'block3_pool')(x) x = Conv2D(512, (3,3), activation='relu', padding='same',name='block4_conv1')(x) x = Conv2D(512, (3,3), activation='relu', padding='same',name='block4_conv2')(x) x = Conv2D(512, (3,3), activation='relu', padding='same',name='block4_conv3')(x) x = MaxPooling2D((2,2), strides=(2,2), name = 'block4_pool')(x) x = Conv2D(512, (3,3), activation='relu', padding='same',name='block5_conv1')(x) x = Conv2D(512, (3,3), activation='relu', padding='same',name='block5_conv2')(x) x = Conv2D(512, (3,3), activation='relu', padding='same',name='block5_conv3')(x) x = MaxPooling2D((2,2), strides=(2,2), name = 'block5_pool')(x) x = Flatten()(x) x = Dense(4096, activation='relu', name='fc1')(x) x = Dense(4096, activation='relu', name='fc2')(x) output_tensor = Dense(nb_classes, activation='softmax', name='predictions')(x)
model = Model(input_tensor, output_tensor) return model
model=VGG16(1000, (img_width, img_height, 3)) model.summary() """ Model: "model" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= input_1 (InputLayer) [(None, 224, 224, 3)] 0 _________________________________________________________________ block1_conv1 (Conv2D) (None, 224, 224, 64) 1792 _________________________________________________________________ block1_conv2 (Conv2D) (None, 224, 224, 64) 36928 _________________________________________________________________ block1_pool (MaxPooling2D) (None, 112, 112, 64) 0 _________________________________________________________________ block2_conv1 (Conv2D) (None, 112, 112, 128) 73856 _________________________________________________________________ block2_conv2 (Conv2D) (None, 112, 112, 128) 147584 _________________________________________________________________ block2_pool (MaxPooling2D) (None, 56, 56, 128) 0 _________________________________________________________________ block3_conv1 (Conv2D) (None, 56, 56, 256) 295168 _________________________________________________________________ block3_conv2 (Conv2D) (None, 56, 56, 256) 590080 _________________________________________________________________ block3_conv3 (Conv2D) (None, 56, 56, 256) 590080 _________________________________________________________________ block3_pool (MaxPooling2D) (None, 28, 28, 256) 0 _________________________________________________________________ block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160 _________________________________________________________________ block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808 _________________________________________________________________ block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808 _________________________________________________________________ block4_pool (MaxPooling2D) (None, 14, 14, 512) 0 _________________________________________________________________ block5_conv1 (Conv2D) (None, 14, 14, 512) 2359808 _________________________________________________________________ block5_conv2 (Conv2D) (None, 14, 14, 512) 2359808 _________________________________________________________________ block5_conv3 (Conv2D) (None, 14, 14, 512) 2359808 _________________________________________________________________ block5_pool (MaxPooling2D) (None, 7, 7, 512) 0 _________________________________________________________________ flatten (Flatten) (None, 25088) 0 _________________________________________________________________ fc1 (Dense) (None, 4096) 102764544 _________________________________________________________________ fc2 (Dense) (None, 4096) 16781312 _________________________________________________________________ predictions (Dense) (None, 1000) 4097000 ================================================================= Total params: 138,357,544 Trainable params: 138,357,544 Non-trainable params: 0 _________________________________________________________________ """
|