def getResidualBlock(I, mode, filter_size, featmaps, activation, dropout, shortcut, convArgs, bnArgs): """Get residual block.""" if mode == "real": O = BatchNormalization(**bnArgs)(I) elif mode == "complex": O = ComplexBatchNormalization(**bnArgs)(I) elif mode == "quaternion": O = QuaternionBatchNormalization(**bnArgs)(I) O = Activation(activation)(O) if shortcut == 'regular': if mode == "real": O = Conv2D(featmaps, filter_size, **convArgs)(O) elif mode == "complex": O = ComplexConv2D(featmaps, filter_size, **convArgs)(O) elif mode == "quaternion": O = QuaternionConv2D(featmaps, filter_size, **convArgs)(O) elif shortcut == 'projection': if mode == "real": O = Conv2D(featmaps, filter_size, **convArgs)(O) elif mode == "complex": O = ComplexConv2D(featmaps, filter_size, **convArgs)(O) elif mode == "quaternion": O = QuaternionConv2D(featmaps, filter_size, **convArgs)(O) if mode == "real": O = BatchNormalization(**bnArgs)(O) O = Activation(activation)(O) O = Conv2D(featmaps, filter_size, **convArgs)(O) elif mode == "complex": O = ComplexBatchNormalization(**bnArgs)(O) O = Activation(activation)(O) O = ComplexConv2D(featmaps, filter_size, **convArgs)(O) elif mode == "quaternion": O = QuaternionBatchNormalization(**bnArgs)(O) O = Activation(activation)(O) O = QuaternionConv2D(featmaps, filter_size, **convArgs)(O) if shortcut == 'regular': O = Add()([O, I]) elif shortcut == 'projection': if mode == "real": X = Conv2D(featmaps, (1, 1), **convArgs)(I) O = Concatenate(1)([X, O]) elif mode == "complex": X = ComplexConv2D(featmaps, (1, 1), **convArgs)(I) O_real = Concatenate(1)([GetReal()(X), GetReal()(O)]) O_imag = Concatenate(1)([GetImag()(X), GetImag()(O)]) O = Concatenate(1)([O_real, O_imag]) elif mode == "quaternion": X = QuaternionConv2D(featmaps, (1, 1), **convArgs)(I) O_r = Concatenate(1)([GetR()(X), GetR()(O)]) O_i = Concatenate(1)([GetI()(X), GetI()(O)]) O_j = Concatenate(1)([GetJ()(X), GetJ()(O)]) O_k = Concatenate(1)([GetK()(X), GetK()(O)]) O = Concatenate(1)([O_r, O_i, O_j, O_k]) return O
def getModel(params): mode = params.mode n = params.num_blocks sf = params.start_filter activation = params.act dropout = params.dropout inputShape = (3, 93, 310) channelAxis = 1 filsize = (3, 3) convArgs = { "padding": "same", "use_bias": False, "kernel_regularizer": l2(0.0001), } bnArgs = { "axis": channelAxis, "momentum": 0.9, "epsilon": 1e-04, "scale": False } convArgs.update({"kernel_initializer": params.init}) # Create the vector channels R = Input(shape=inputShape) if mode != "quaternion": I = learnVectorBlock(R, 3, filsize, 'relu', bnArgs) O = concatenate([R, I], axis=channelAxis) else: I = learnVectorBlock(R, 3, filsize, 'relu', bnArgs) J = learnVectorBlock(R, 3, filsize, 'relu', bnArgs) K = learnVectorBlock(R, 3, filsize, 'relu', bnArgs) O = concatenate([R, I, J, K], axis=channelAxis) if mode == "real": O = Conv2D(sf, filsize, **convArgs)(O) O = BatchNormalization(**bnArgs)(O) elif mode == "complex": O = ComplexConv2D(sf, filsize, **convArgs)(O) O = ComplexBatchNormalization(**bnArgs)(O) else: O = QuaternionConv2D(sf, filsize, **convArgs)(O) O = QuaternionBatchNormalization(**bnArgs)(O) O = Activation(activation)(O) for i in range(n): O = getResidualBlock(O, mode, filsize, sf, activation, dropout, 'regular', convArgs, bnArgs) O = getResidualBlock(O, mode, filsize, sf, activation, dropout, 'projection', convArgs, bnArgs) for i in range(n-1): O = getResidualBlock(O, mode, filsize, sf*2, activation, dropout, 'regular', convArgs, bnArgs) O = getResidualBlock(O, mode, filsize, sf*2, activation, dropout, 'projection', convArgs, bnArgs) for i in range(n-1): O = getResidualBlock(O, mode, filsize, sf*4, activation, dropout, 'regular', convArgs, bnArgs) # heatmap output O = Convolution2D(1, 1, activation='sigmoid')(O) model = Model(R, O) opt = SGD (lr = params.lr, momentum = params.momentum, decay = params.decay, nesterov = True, clipnorm = params.clipnorm) model.compile(opt, dice_coef_loss) return model
datagen.fit(x_train) # convert class vectors to binary class matrices y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) #将灰度图片转为四自由度图片 R = Input(shape=input_shape) I = learnVectorBlock(R) J = learnVectorBlock(R) K = learnVectorBlock(R) O = concatenate([R, I, J, K], axis=-1) O = QuaternionConv2D(64, (5, 5), activation='relu', padding="same", kernel_initializer='quaternion')(O) O = QuaternionConv2D(64, (5, 5), activation='relu', padding="same", kernel_initializer='quaternion')(O) O = QuaternionConv2D(32, (5, 5), activation='relu', padding="same", kernel_initializer='quaternion')(O) # O = Convolution2D(256, (5, 5), activation='relu', padding="same")(R) # O = Convolution2D(256, (5, 5), activation='relu', padding="same")(O) # O = Convolution2D(128, (5, 5), activation='relu', padding="same")(O) O = Flatten()(O)
def getModel(params): mode = params.mode n = params.num_blocks sf = params.start_filter dataset = params.dataset activation = params.act inputShape = (3, 512, 512) channelAxis = 1 filsize = (3, 3) convArgs = { "padding": "same", "use_bias": False, "kernel_regularizer": l2(0.0001), } bnArgs = {"axis": channelAxis, "momentum": 0.9, "epsilon": 1e-04} convArgs.update({"kernel_initializer": params.init}) # Create the vector channels R = Input(shape=inputShape) if mode != "quaternion": I = learnVectorBlock(R, 3, filsize, 'relu', bnArgs) O = concatenate([R, I], axis=channelAxis) else: I = learnVectorBlock(R, 3, filsize, 'relu', bnArgs) J = learnVectorBlock(R, 3, filsize, 'relu', bnArgs) K = learnVectorBlock(R, 3, filsize, 'relu', bnArgs) O = concatenate([R, I, J, K], axis=channelAxis) if mode == "real": O = Conv2D(sf, filsize, **convArgs)(O) O = BatchNormalization(**bnArgs)(O) elif mode == "complex": O = ComplexConv2D(sf, filsize, **convArgs)(O) O = ComplexBatchNormalization(**bnArgs)(O) else: O = QuaternionConv2D(64, (5, 5), **convArgs)(O) O = QuaternionBatchNormalization(**bnArgs)(O) O = Activation(activation)(O) O = MaxPooling2D(pool_size=(3, 3)) # conv2 O = QuaternionConv2D(128, (5, 5), **convArgs) O = Activation(activation)(O) O = MaxPooling2D(pool_size=(3, 3)) #conv3 O = QuaternionConv2D(256, (3, 3), **convArgs) O = Activation(activation)(O) O = MaxPooling2D(pool_size=(3, 3)) #conv4 O = QuaternionConv2D(512, (3, 3), **convArgs) O = Activation(activation)(O) O = MaxPooling2D(pool_size=(3, 3)) #conv5 O = QuaternionConv2D(16, (3, 3), **convArgs) O = Activation(activation)(O) O = MaxPooling2D(pool_size=(3, 3)) # for i in range(n): # O = getResidualBlock(O, mode, filsize, sf, activation, 'regular', convArgs, bnArgs) # # O = getResidualBlock(O, mode, filsize, sf, activation, 'projection', convArgs, bnArgs) # # for i in range(n - 1): # O = getResidualBlock(O, mode, filsize, sf * 2, activation, 'regular', convArgs, bnArgs) # # O = getResidualBlock(O, mode, filsize, sf * 2, activation, 'projection', convArgs, bnArgs) # # for i in range(n - 1): # O = getResidualBlock(O, mode, filsize, sf * 4, activation, 'regular', convArgs, bnArgs) # O = AveragePooling2D(pool_size=(8, 8))(O) # Flatten O = Flatten()(O) # Dense O = Dense(1000, activation='relu', kernel_regularizer=l2(0.0001))(O) model = Model(R, O) return model
def getModel(params): mode = params.mode n = params.num_blocks sf = params.start_filter dataset = params.dataset activation = params.act inputShape = (3, 32, 32) channelAxis = 1 filsize = (3, 3) convArgs = { "padding": "same", "use_bias": False, "kernel_regularizer": l2(0.0001), } bnArgs = { "axis": channelAxis, "momentum": 0.9, "epsilon": 1e-04 } convArgs.update({"kernel_initializer": params.init}) # Create the vector channels R = Input(shape=inputShape) if mode != "quaternion": I = learnVectorBlock(R, 3, filsize, 'relu', bnArgs) O = concatenate([R, I], axis=channelAxis) else: I = learnVectorBlock(R, 3, filsize, 'relu', bnArgs) J = learnVectorBlock(R, 3, filsize, 'relu', bnArgs) K = learnVectorBlock(R, 3, filsize, 'relu', bnArgs) O = concatenate([R, I, J, K], axis=channelAxis) if mode == "real": O = Conv2D(sf, filsize, **convArgs)(O) O = BatchNormalization(**bnArgs)(O) elif mode == "complex": O = ComplexConv2D(sf, filsize, **convArgs)(O) O = ComplexBatchNormalization(**bnArgs)(O) else: O = QuaternionConv2D(sf, filsize, **convArgs)(O) O = QuaternionBatchNormalization(**bnArgs)(O) O = Activation(activation)(O) for i in range(n): O = getResidualBlock(O, mode, filsize, sf, activation, 'regular', convArgs, bnArgs) O = getResidualBlock(O, mode, filsize, sf, activation, 'projection', convArgs, bnArgs) for i in range(n-1): O = getResidualBlock(O, mode, filsize, sf*2, activation, 'regular', convArgs, bnArgs) O = getResidualBlock(O, mode, filsize, sf*2, activation, 'projection', convArgs, bnArgs) for i in range(n-1): O = getResidualBlock(O, mode, filsize, sf*4, activation, 'regular', convArgs, bnArgs) O = AveragePooling2D(pool_size=(8, 8))(O) # Flatten O = Flatten()(O) # Dense if dataset == 'cifar10': O = Dense(10, activation='softmax', kernel_regularizer=l2(0.0001))(O) elif dataset == 'cifar100': O = Dense(100, activation='softmax', kernel_regularizer=l2(0.0001))(O) model = Model(R, O) opt = SGD (lr = params.lr, momentum = params.momentum, decay = params.decay, nesterov = True, clipnorm = params.clipnorm) model.compile(opt, 'categorical_crossentropy', metrics=['accuracy']) return model