def _pyramid_ensemble_adaptation(teacher: Model) -> Tuple[Model, int]: """ Adapt pyramid ensemble by changing its output to contain each of its submodels outputs. :param teacher: the pyramid ensemble. :return: the selective_learning pyramid ensemble and the submodels number. """ # Get each submodel's outputs. output1 = teacher.get_layer('submodel_strong_output').output weak_1_output = teacher.get_layer('submodel_weak_1_output').output weak_2_output = teacher.get_layer('submodel_weak_2_output').output # Create zeros. weak_1_zeros = ZerosLike(name='weak_1_zeros')(weak_1_output) weak_2_zeros = ZerosLike(name='weak_2_zeros')(weak_2_output) # Append zeros to the model outputs which do not predict all the classes. output2 = Concatenate(name='submodel_weak_1_output_fixed')( [weak_1_output, weak_1_zeros]) output3 = Concatenate(name='submodel_weak_2_output_fixed')( [weak_2_zeros, weak_2_output]) # Add activations to the outputs. output1 = Activation('softmax', name='softmax1')(output1) output2 = Activation('softmax', name='softmax2')(output2) output3 = Activation('softmax', name='softmax3')(output3) # Stack submodels outputs. outputs = Stack( axis=1, name='stacked_submodels_outputs')([output1, output2, output3]) # Create selective_learning teacher. selective_learning_teacher = Model(teacher.input, outputs, name='selective_learning_' + teacher.name) return selective_learning_teacher, 3
def _load_saved_instance(cls, config_dict: Dict, model_path: str, tf_model: keras.Model): from kashgari import utils embeddings = [] for embed_info in config_dict['embeddings']: embed_class = pydoc.locate( f"{embed_info['module']}.{embed_info['class_name']}") embedding: Embedding = embed_class._load_saved_instance( embed_info, model_path, tf_model) embeddings.append(embedding) instance = cls(embeddings=embeddings, from_saved_model=True) embed_model_json_str = json.dumps(config_dict['embed_model']) instance.embed_model = utils._custom_load_keras_model_from_json( embed_model_json_str) # Load Weights from model for layer in instance.embed_model.layers: layer.set_weights(tf_model.get_layer(layer.name).get_weights()) return instance
class SSD(BaseSSD): def __init__(self, aspect_ratios=None, image_size=None): self.model = None if aspect_ratios is None: aspect_ratios = [1.] self.aspect_ratios = aspect_ratios self.num_boxes = len(aspect_ratios) + 1 if 1. in aspect_ratios else 0 self.create_head_layers() self.build(input_shape=(image_size, image_size, 3)) def build(self, input_shape): input_tensor = Input(shape=input_shape) # vgg block 1 x = Conv2D(64, 3, padding='same', activation='relu', name='block1_conv1')(input_tensor) x = Conv2D(64, 3, padding='same', activation='relu', name='block1_conv2')(x) x = MaxPooling2D(2, 2, padding='same', name='block1_pool')(x) # vgg block 2 x = Conv2D(128, 3, padding='same', activation='relu', name='block2_conv1')(x) x = Conv2D(128, 3, padding='same', activation='relu', name='block2_conv2')(x) x = MaxPooling2D(2, 2, padding='same', name='block2_pool')(x) # vgg block 3 x = Conv2D(256, 3, padding='same', activation='relu', name='block3_conv1')(x) x = Conv2D(256, 3, padding='same', activation='relu', name='block3_conv2')(x) x = Conv2D(256, 3, padding='same', activation='relu', name='block3_conv3')(x) x = MaxPooling2D(2, 2, padding='same', name='block3_pool')(x) # vgg block 4 x = Conv2D(512, 3, padding='same', activation='relu', name='block4_conv1')(x) x = Conv2D(512, 3, padding='same', activation='relu', name='block4_conv2')(x) conv43 = Conv2D(512, 3, padding='same', activation='relu', name='block4_conv3')(x) x = MaxPooling2D(2, 2, padding='same', name='block4_pool')(conv43) # vgg block 5 x = Conv2D(512, 3, padding='same', activation='relu', name='block5_conv1')(x) x = Conv2D(512, 3, padding='same', activation='relu', name='block5_conv2')(x) x = Conv2D(512, 3, padding='same', activation='relu', name='block5_conv3')(x) x = MaxPooling2D(3, 1, padding='same', name='block5_pool')(x) # vgg fc modified as conv conv6 = Conv2D(1024, 3, dilation_rate=6, activation='relu', padding='same', name='conv6')(x) conv7 = Conv2D(1024, 1, activation='relu', padding='same', name='conv7')(conv6) # extra feature map layers extra1 = ConvBn(256, 1, name='extra1_shrink')(conv7) extra1 = ConvBn(512, 3, strides=2, padding='same', name='extra1')(extra1) extra2 = ConvBn(128, 1, name='extra2_shrink')(extra1) extra2 = ConvBn(256, 3, strides=2, padding='same', name='extra2')(extra2) extra3 = ConvBn(128, 1, name='extra3_shrink')(extra2) extra3 = ConvBn(256, 3, name='extra3')(extra3) extra4 = ConvBn(128, 1, name='extra4_shrink')(extra3) extra4 = ConvBn(256, 3, name='extra4')(extra4) extra5 = ConvBn(128, 1, name='extra5_shrink')(extra4) extra5 = ConvBn(256, 4, name='extra5')(extra5) # heads feature_maps = [conv43, conv7, extra1, extra2, extra3, extra4, extra5] confs, locs, anchors = [], [], [] for i in range(len(feature_maps)): map = feature_maps[i] conf = self.conf_layers[i](map) loc = self.loc_layers[i](map) anchor = self.anchor_layers[i](map) confs.append(conf) locs.append(loc) anchors.append(anchor) confs_reshaped = [Reshape((-1, 1))(conf) for conf in confs] locs_reshaped = [Reshape((-1, 4))(loc) for loc in locs] anchors_reshaped = [Reshape((-1, 4))(db) for db in anchors] conf_concat = Concatenate(axis=1, name='scores')(confs_reshaped) loc_concat = Concatenate(axis=1, name='offsets')(locs_reshaped) anchor_concat = Concatenate(axis=1, name='default_boxes')(anchors_reshaped) self.model = Model(input_tensor, [conf_concat, loc_concat, anchor_concat], name='ssd_vgg16') def init_vgg16(self): vgg16 = VGG16(weights='imagenet', include_top=True) for i, layer in enumerate(self.model.layers): layer_name = layer.name if layer_name.startswith('block'): layer_weights = vgg16.get_layer(name=layer_name).get_weights() layer.set_weights(layer_weights) fc1_w, fc1_b = vgg16.get_layer(name='fc1').get_weights() fc2_w, fc2_b = vgg16.get_layer(name='fc2').get_weights() conv6_w = np.random.choice(np.reshape(fc1_w, (-1, )), (3, 3, 512, 1024)) conv6_b = np.random.choice(fc1_b, (1024, )) conv7_w = np.random.choice(np.reshape(fc2_w, (-1, )), (1, 1, 1024, 1024)) conv7_b = np.random.choice(fc2_b, (1024, )) self.model.get_layer(name='conv6').set_weights([conv6_w, conv6_b]) self.model.get_layer(name='conv7').set_weights([conv7_w, conv7_b])