def __init__(self, num_classes, wd, name='AuxiliaryHeadCIFAR', **kwargs): super(AuxiliaryHeadCIFAR, self).__init__(name=name, **kwargs) self.features = Sequential([ ReLU(), AveragePooling2D(5, strides=3, padding='valid'), Conv2D(filters=128, kernel_size=1, strides=1, padding='valid', kernel_initializer=kernel_init(), kernel_regularizer=regularizer(wd), use_bias=False), BatchNormalization(affine=True), ReLU(), Conv2D(filters=768, kernel_size=2, strides=1, padding='valid', kernel_initializer=kernel_init(), kernel_regularizer=regularizer(wd), use_bias=False), BatchNormalization(affine=True), ReLU() ]) self.classifier = Dense(num_classes, kernel_initializer=kernel_init(), kernel_regularizer=regularizer(wd))
def CifarModel(cfg, training=True, stem_multiplier=3, name='CifarModel'): """Cifar Model""" logging.info(f"buliding {name}...") input_size = cfg['input_size'] ch_init = cfg['init_channels'] layers = cfg['layers'] num_cls = cfg['num_classes'] wd = cfg['weights_decay'] genotype = eval("genotypes.%s" % cfg['arch']) # define model inputs = Input([input_size, input_size, 3], name='input_image') if training: drop_path_prob = Input([], name='drop_prob') else: drop_path_prob = None ch_curr = stem_multiplier * ch_init s0 = s1 = Sequential([ Conv2D(filters=ch_curr, kernel_size=3, strides=1, padding='same', kernel_initializer=kernel_init(), kernel_regularizer=regularizer(wd), use_bias=False), BatchNormalization(affine=True)], name='stem')(inputs) ch_curr = ch_init reduction_prev = False logits_aux = None for layer_index in range(layers): if layer_index in [layers // 3, 2 * layers // 3]: ch_curr *= 2 reduction = True else: reduction = False cell = Cell(genotype, ch_curr, reduction, reduction_prev, wd, name=f'Cell_{layer_index}') s0, s1 = s1, cell(s0, s1, drop_path_prob) reduction_prev = reduction if layer_index == 2 * layers // 3 and training: logits_aux = AuxiliaryHeadCIFAR(num_cls, wd=wd)(s1) fea = GlobalAveragePooling2D()(s1) logits = Dense(num_cls, kernel_initializer=kernel_init(), kernel_regularizer=regularizer(wd))(Flatten()(fea)) if training: return Model((inputs, drop_path_prob), (logits, logits_aux), name=name) else: return Model(inputs, logits, name=name)
def _build_model(self): """Model""" logging.info(f"buliding {self.name}...") input_size = self.cfg['input_size'] ch_init = self.cfg['init_channels'] layers = self.cfg['layers'] num_cls = self.cfg['num_classes'] wd = self.cfg['weights_decay'] # define model inputs = Input([input_size, input_size, 3], name='input_image') alphas_normal = Input([None], name='alphas_normal') alphas_reduce = Input([None], name='alphas_reduce') betas_normal = Input([], name='betas_normal') betas_reduce = Input([], name='betas_reduce') alphas_reduce_weights = Softmax( name='AlphasReduceSoftmax')(alphas_reduce) alphas_normal_weights = Softmax( name='AlphasNormalSoftmax')(alphas_normal) betas_reduce_weights = SplitSoftmax( range(2, 2 + self.steps), name='BetasReduceSoftmax')(betas_reduce) betas_normal_weights = SplitSoftmax( range(2, 2 + self.steps), name='BetasNormalSoftmax')(betas_normal) ch_curr = self.stem_multiplier * ch_init s0 = s1 = Sequential([ Conv2D(filters=ch_curr, kernel_size=3, strides=1, padding='same', kernel_initializer=kernel_init(), kernel_regularizer=regularizer(wd), use_bias=False), BatchNormalization(affine=True) ], name='stem')(inputs) ch_curr = ch_init reduction_prev = False for layer_index in range(layers): if layer_index in [layers // 3, 2 * layers // 3]: ch_curr *= 2 reduction = True weights = alphas_reduce_weights edge_weights = betas_reduce_weights else: reduction = False weights = alphas_normal_weights edge_weights = betas_normal_weights cell = Cell(self.steps, self.multiplier, ch_curr, reduction, reduction_prev, wd, name=f'Cell_{layer_index}') s0, s1 = s1, cell(s0, s1, weights, edge_weights) reduction_prev = reduction fea = GlobalAveragePooling2D()(s1) logits = Dense(num_cls, kernel_initializer=kernel_init(), kernel_regularizer=regularizer(wd))(Flatten()(fea)) return Model( (inputs, alphas_normal, alphas_reduce, betas_normal, betas_reduce), logits, name=self.name)