我有以下连体网络(基于 Xception),其组成如下:
from tensorflow.keras.models import Model, model_from_json, Sequential
from tensorflow.keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from tensorflow.keras.callbacks import TensorBoard
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D, SeparableConv2D, UpSampling2D, BatchNormalization, Input, GlobalAveragePooling2D
from tensorflow.keras.regularizers import l2
from tensorflow.keras.optimizers import SGD, RMSprop
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.utils import plot_model
def entry_flow(inputs) :
x = Conv2D(32, 3, strides = 2, padding='same')(inputs)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(64,3,padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
previous_block_activation = x
for size in [128, 256, 728] :
x = Activation('relu')(x)
x = SeparableConv2D(size, 3, padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = SeparableConv2D(size, 3, padding='same')(x)
x = BatchNormalization()(x)
x = MaxPooling2D(3, strides=2, padding='same')(x)
residual = Conv2D(size, 1, strides=2, padding='same')(previous_block_activation)
x = tensorflow.keras.layers.Add()([x, residual])
previous_block_activation = x
return x
def middle_flow(x, num_blocks=1) :
previous_block_activation = x
for _ in range(num_blocks) :
x = Activation('relu')(x)
x = SeparableConv2D(728, 3, padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = SeparableConv2D(728, 3, padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = SeparableConv2D(728, 3, padding='same')(x)
x = BatchNormalization()(x)
x = tensorflow.keras.layers.Add()([x, previous_block_activation])
previous_block_activation = x
return x
def exit_flow(x) :
previous_block_activation = x
x = Activation('relu')(x)
x = SeparableConv2D(728, 3, padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = SeparableConv2D(1024, 3, padding='same')(x)
x = BatchNormalization()(x)
x = MaxPooling2D(3, strides=2, padding='same')(x)
residual = Conv2D(1024, 1, strides=2, padding='same')(previous_block_activation)
x = tensorflow.keras.layers.Add()([x, residual])
x = Activation('relu')(x)
x = SeparableConv2D(728, 3, padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = SeparableConv2D(1024, 3, padding='same')(x)
x = BatchNormalization()(x)
x = GlobalAveragePooling2D()(x)
x = Dense(48, activation='linear')(x)
return x
要构建连体网络,我只需要调用以下代码:
def build_siamese_model(inputShape):
# specify the inputs for the feature extractor network
inputs = Input(shape=(inputShape))
outputs = exit_flow(middle_flow(entry_flow(inputs)))
xception = Model(inputs, outputs)
# return the model to the calling function
return exception
我已经在给定的数据集中训练了这个模型,我现在要做的就是再次构建模型,加载以前的权重,冻结层(除了具有 48 个神经元的 Dense 层)并训练 Dense(全连接) 具有新数据集的图层。我如何使用 Siamese Networks 做到这一点?我只能使用 Imagenet CNN 来做到这一点,但我不知道如何使用自定义连体网络来做到这一点。