def wider_bn(layer, start_dim, total_dim, n_add, weighted=True): '''wider batch norm layer. ''' n_dim = get_n_dim(layer) if not weighted: return get_batch_norm_class(n_dim)(layer.num_features + n_add) weights = layer.get_weights() new_weights = [ add_noise(np.ones(n_add, dtype=np.float32), np.array([0, 1])), add_noise(np.zeros(n_add, dtype=np.float32), np.array([0, 1])), add_noise(np.zeros(n_add, dtype=np.float32), np.array([0, 1])), add_noise(np.ones(n_add, dtype=np.float32), np.array([0, 1])), ] student_w = tuple() for weight, new_weight in zip(weights, new_weights): temp_w = weight.copy() temp_w = np.concatenate( (temp_w[:start_dim], new_weight, temp_w[start_dim:total_dim])) student_w += (temp_w, ) new_layer = get_batch_norm_class(n_dim)(layer.num_features + n_add) new_layer.set_weights(student_w) return new_layer
def create_new_layer(layer, n_dim): ''' create new layer for the graph ''' input_shape = layer.output.shape dense_deeper_classes = [StubDense, get_dropout_class(n_dim), StubReLU] conv_deeper_classes = [ get_conv_class(n_dim), get_batch_norm_class(n_dim), StubReLU ] if is_layer(layer, "ReLU"): conv_deeper_classes = [ get_conv_class(n_dim), get_batch_norm_class(n_dim) ] dense_deeper_classes = [StubDense, get_dropout_class(n_dim)] elif is_layer(layer, "Dropout"): dense_deeper_classes = [StubDense, StubReLU] elif is_layer(layer, "BatchNormalization"): conv_deeper_classes = [get_conv_class(n_dim), StubReLU] layer_class = None if len(input_shape) == 1: # It is in the dense layer part. layer_class = sample(dense_deeper_classes, 1)[0] else: # It is in the conv layer part. layer_class = sample(conv_deeper_classes, 1)[0] if layer_class == StubDense: new_layer = StubDense(input_shape[0], input_shape[0]) elif layer_class == get_dropout_class(n_dim): new_layer = layer_class(Constant.DENSE_DROPOUT_RATE) elif layer_class == get_conv_class(n_dim): new_layer = layer_class(input_shape[-1], input_shape[-1], sample((1, 3, 5), 1)[0], stride=1) elif layer_class == get_batch_norm_class(n_dim): new_layer = layer_class(input_shape[-1]) elif layer_class == get_pooling_class(n_dim): new_layer = layer_class(sample((1, 3, 5), 1)[0]) else: new_layer = layer_class() return new_layer
def deeper_conv_block(conv_layer, kernel_size, weighted=True): '''deeper conv layer. ''' n_dim = get_n_dim(conv_layer) filter_shape = (kernel_size, ) * 2 n_filters = conv_layer.filters weight = np.zeros((n_filters, n_filters) + filter_shape) center = tuple(map(lambda x: int((x - 1) / 2), filter_shape)) for i in range(n_filters): filter_weight = np.zeros((n_filters, ) + filter_shape) index = (i, ) + center filter_weight[index] = 1 weight[i, ...] = filter_weight bias = np.zeros(n_filters) new_conv_layer = get_conv_class(n_dim)(conv_layer.filters, n_filters, kernel_size=kernel_size) bn = get_batch_norm_class(n_dim)(n_filters) if weighted: new_conv_layer.set_weights( (add_noise(weight, np.array([0, 1])), add_noise(bias, np.array([0, 1])))) new_weights = [ add_noise(np.ones(n_filters, dtype=np.float32), np.array([0, 1])), add_noise(np.zeros(n_filters, dtype=np.float32), np.array([0, 1])), add_noise(np.zeros(n_filters, dtype=np.float32), np.array([0, 1])), add_noise(np.ones(n_filters, dtype=np.float32), np.array([0, 1])), ] bn.set_weights(new_weights) return [StubReLU(), new_conv_layer, bn]
def to_deeper_graph2(graph): ''' deeper graph ''' weighted_layer_ids = graph.deep_layer_ids2() if len(weighted_layer_ids) >= Constant.MAX_LAYERS: return None deeper_layer_ids = sample(weighted_layer_ids, 1)#选一层 for layer_id in deeper_layer_ids: layer = graph.layer_list[layer_id] input_shape = layer.output.shape layer_class = get_conv_class(graph.n_dim) new_layer = layer_class(input_shape[-1], input_shape[-1], 3, stride=1) output_id = graph.to_deeper_model(layer_id, new_layer) layer_id2 = graph.get_layers_id(output_id) layer2 = graph.layer_list[layer_id2] input_shape2 = layer2.output.shape layer_class = get_batch_norm_class(graph.n_dim) new_layer2 = layer_class(input_shape2[-1]) output_id2=graph.to_deeper_model(layer_id2, new_layer2) layer_id3 = graph.get_layers_id(output_id2) graph.to_deeper_model(layer_id3, StubReLU()) return graph
def __init__(self, n_output_node, input_shape): super(CnnGenerator, self).__init__(n_output_node, input_shape) self.n_dim = len(self.input_shape) - 1 if len(self.input_shape) > 4: raise ValueError("The input dimension is too high.") if len(self.input_shape) < 2: raise ValueError("The input dimension is too low.") self.conv = get_conv_class(self.n_dim) self.dropout = get_dropout_class(self.n_dim) self.global_avg_pooling = get_global_avg_pooling_class(self.n_dim) self.pooling = get_pooling_class(self.n_dim) self.batch_norm = get_batch_norm_class(self.n_dim)
def __init__(self, input_shape, weighted=True): """Initializer for Graph. """ self.input_shape = input_shape self.weighted = weighted self.node_list = [] self.layer_list = [] # node id start with 0 self.node_to_id = {} self.layer_to_id = {} self.layer_id_to_input_node_ids = {} self.layer_id_to_output_node_ids = {} self.adj_list = {} self.reverse_adj_list = {} self.operation_history = [] self.n_dim = len(input_shape) - 1 self.conv = get_conv_class(self.n_dim) self.batch_norm = get_batch_norm_class(self.n_dim) self.vis = None self._add_node(Node(input_shape))