def generate(self, model_len=Constant.MODEL_LEN, model_width=Constant.MODEL_WIDTH): """Generates a CNN. Args: model_len: An integer. Number of convolutional layers. model_width: An integer. Number of filters for the convolutional layers. Returns: An instance of the class Graph. Represents the neural architecture graph of the generated model. """ pooling_len = int(model_len / 4) graph = Graph(self.input_shape, False) temp_input_channel = self.input_shape[-1] output_node_id = 0 stride = 1 for i in range(model_len): output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(self.batch_norm(graph.node_list[output_node_id].shape[-1]), output_node_id) output_node_id = graph.add_layer(self.conv(temp_input_channel, model_width, kernel_size=3, stride=stride), output_node_id) # if stride == 1: # stride = 2 temp_input_channel = model_width if pooling_len == 0 or ((i + 1) % pooling_len == 0 and i != model_len - 1): output_node_id = graph.add_layer(self.pooling(), output_node_id) output_node_id = graph.add_layer(self.global_avg_pooling(), output_node_id) output_node_id = graph.add_layer(self.dropout(Constant.CONV_DROPOUT_RATE), output_node_id) output_node_id = graph.add_layer(StubDense(graph.node_list[output_node_id].shape[0], model_width), output_node_id) output_node_id = graph.add_layer(StubReLU(), output_node_id) graph.add_layer(StubDense(model_width, self.n_output_node), output_node_id) return graph
def generate(self, model_len=None, model_width=None): graph = Graph(self.input_shape, False) temp_input_channel = self.input_shape[-1] output_node_id = 0 #out_planes = self.in_planes*self.n_output_node out_planes = 24 output_node_id = graph.add_layer(self.conv(temp_input_channel, self.in_planes, kernel_size=3, stride=1, padding=1), output_node_id) output_node_id = graph.add_layer(self.batch_norm(graph.node_list[output_node_id].shape[-1]), output_node_id) output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = self._make_layer(graph, output_node_id) output_node_id = graph.add_layer(self.conv(out_planes, out_planes*4, kernel_size=1, stride=1, padding=0), output_node_id) output_node_id = graph.add_layer(self.batch_norm(graph.node_list[output_node_id].shape[-1]), output_node_id) output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(self.global_avg_pooling(), output_node_id) graph.add_layer(StubDense(out_planes*4, self.n_output_node), output_node_id) return graph
def generate(self, model_len=None, model_width=None): if model_len is None: model_len = Constant.MODEL_LEN if model_width is None: model_width = Constant.MODEL_WIDTH graph = Graph(self.input_shape, False) temp_input_channel = self.input_shape[-1] # First convolution output_node_id = 0 output_node_id = graph.add_layer(self.conv(temp_input_channel, model_width, kernel_size=7), output_node_id) output_node_id = graph.add_layer(self.batch_norm(num_features=self.num_init_features), output_node_id) output_node_id = graph.add_layer(StubReLU(), output_node_id) db_input_node_id = graph.add_layer(self.max_pooling(kernel_size=3, stride=2, padding=1), output_node_id) # Each DensebLock num_features = self.num_init_features for i, num_layers in enumerate(self.block_config): db_input_node_id = self._dense_block(num_layers=num_layers, num_input_features=num_features, bn_size=self.bn_size, growth_rate=self.growth_rate, drop_rate=self.drop_rate, graph=graph, input_node_id=db_input_node_id) num_features = num_features + num_layers * self.growth_rate if i != len(self.block_config) - 1: db_input_node_id = self._transition(num_input_features=num_features, num_output_features=num_features // 2, graph=graph, input_node_id=db_input_node_id) num_features = num_features // 2 # Final batch norm out = graph.add_layer(self.batch_norm(num_features), db_input_node_id) out = graph.add_layer(StubReLU(), out) out = graph.add_layer(self.adaptive_avg_pooling(), out) # Linear layer graph.add_layer(StubDense(num_features, self.n_output_node), out) return graph
def generate(self, model_len=Constant.MODEL_LEN, model_width=Constant.MODEL_WIDTH): pooling_len = int(model_len / 4) graph = Graph(self.input_shape, False) temp_input_channel = self.input_shape[-1] output_node_id = 0 for i in range(model_len): output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer( StubConv(temp_input_channel, model_width, kernel_size=3), output_node_id) output_node_id = graph.add_layer( StubBatchNormalization(model_width), output_node_id) temp_input_channel = model_width if pooling_len == 0 or ((i + 1) % pooling_len == 0 and i != model_len - 1): output_node_id = graph.add_layer(StubPooling(), output_node_id) output_node_id = graph.add_layer(StubGlobalPooling(), output_node_id) output_node_id = graph.add_layer( StubDropout(Constant.CONV_DROPOUT_RATE), output_node_id) output_node_id = graph.add_layer( StubDense(graph.node_list[output_node_id].shape[0], model_width), output_node_id) output_node_id = graph.add_layer(StubReLU(), output_node_id) graph.add_layer(StubDense(model_width, self.n_output_node), output_node_id) return graph
def _dense_layer(self, num_input_features, growth_rate, bn_size, drop_rate, graph, input_node_id): out = graph.add_layer(self.batch_norm(num_features=num_input_features), input_node_id) out = graph.add_layer(StubReLU(), out) out = graph.add_layer(self.conv(num_input_features, bn_size * growth_rate, kernel_size=1, stride=1), out) out = graph.add_layer(self.batch_norm(bn_size * growth_rate), out) out = graph.add_layer(StubReLU(), out) out = graph.add_layer(self.conv(bn_size * growth_rate, growth_rate, kernel_size=3, stride=1, padding=1), out) out = graph.add_layer(self.dropout(rate=drop_rate), out) out = graph.add_layer(StubConcatenate(), (input_node_id, out)) return out
def _make_block(self, graph, inplanes, planes, node_id, downsample=None): residual_node_id = node_id out = graph.add_layer(StubReLU(), node_id) out = graph.add_layer(self.conv(inplanes, planes, kernel_size=1), out) out = graph.add_layer(self.batch_norm(planes), out) out = graph.add_layer(StubReLU(), out) out = graph.add_layer(self.conv(planes, planes, kernel_size=3), out) out = graph.add_layer(self.batch_norm(planes), out) if downsample is not None: downsample_out = graph.add_layer(StubReLU(), node_id) downsample_out = graph.add_layer(downsample[0], downsample_out) residual_node_id = graph.add_layer(downsample[1], downsample_out) out = graph.add_layer(StubAdd(), (out, residual_node_id)) return out
def get_concat_skip_model(): graph = Graph((32, 32, 3), False) output_node_id = 0 output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(StubConv(3, 3, 3), output_node_id) output_node_id = graph.add_layer(StubBatchNormalization(3), output_node_id) output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(StubConv(3, 3, 3), output_node_id) output_node_id = graph.add_layer(StubBatchNormalization(3), output_node_id) temp_node_id = output_node_id output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(StubConv(3, 3, 3), output_node_id) output_node_id = graph.add_layer(StubBatchNormalization(3), output_node_id) output_node_id = graph.add_layer(StubConcatenate(), [output_node_id, temp_node_id]) output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(StubConv(6, 3, 1), output_node_id) output_node_id = graph.add_layer(StubBatchNormalization(3), output_node_id) temp_node_id = output_node_id output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(StubConv(3, 3, 3), output_node_id) output_node_id = graph.add_layer(StubBatchNormalization(3), output_node_id) output_node_id = graph.add_layer(StubConcatenate(), [output_node_id, temp_node_id]) output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(StubConv(6, 3, 1), output_node_id) output_node_id = graph.add_layer(StubBatchNormalization(3), output_node_id) output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(StubConv(3, 3, 3), output_node_id) output_node_id = graph.add_layer(StubBatchNormalization(3), output_node_id) output_node_id = graph.add_layer(StubFlatten(), output_node_id) output_node_id = graph.add_layer(StubDropout(Constant.CONV_DROPOUT_RATE), output_node_id) output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer( StubDense(graph.node_list[output_node_id].shape[0], 5), output_node_id) output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer(StubDense(5, 5), output_node_id) graph.add_layer(StubSoftmax(), output_node_id) graph.produce_model().set_weight_to_graph() return graph
def _make_block(self, graph, in_planes, planes, node_id, stride=1): out = graph.add_layer(self.batch_norm(in_planes), node_id) out = graph.add_layer(StubReLU(), out) residual_node_id = out out = graph.add_layer(self.conv(in_planes, planes, kernel_size=3, stride=stride), out) out = graph.add_layer(self.batch_norm(planes), out) out = graph.add_layer(StubReLU(), out) out = graph.add_layer(self.conv(planes, planes, kernel_size=3), out) residual_node_id = graph.add_layer(StubReLU(), residual_node_id) residual_node_id = graph.add_layer(self.conv(in_planes, planes * self.block_expansion, kernel_size=1, stride=stride), residual_node_id) out = graph.add_layer(StubAdd(), (out, residual_node_id)) return out
def deeper_conv_block(conv_layer, kernel_size, weighted=True): 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 = StubConv(conv_layer.filters, n_filters, kernel_size=kernel_size) bn = StubBatchNormalization(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 dense_to_deeper_block(dense_layer, weighted=True): units = dense_layer.units weight = np.eye(units) bias = np.zeros(units) new_dense_layer = StubDense(units, units) if weighted: new_dense_layer.set_weights((add_noise(weight, np.array([0, 1])), add_noise(bias, np.array([0, 1])))) return [StubReLU(), new_dense_layer]
def _make_block(self, graph, in_planes, out_planes, expansion, node_id, stride): planes = expansion * in_planes output_node_id = graph.add_layer( self.conv(in_planes, planes, kernel_size=1, stride=1, padding=0), node_id) output_node_id = graph.add_layer( self.batch_norm(graph.node_list[output_node_id].shape[-1]), output_node_id) output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer( self.conv(planes, planes, kernel_size=3, stride=stride, padding=1, groups=planes), output_node_id) output_node_id = graph.add_layer( self.batch_norm(graph.node_list[output_node_id].shape[-1]), output_node_id) output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer( self.conv(planes, out_planes, kernel_size=1, stride=1, padding=0), output_node_id) output_node_id = graph.add_layer( self.batch_norm(graph.node_list[output_node_id].shape[-1]), output_node_id) #if stride == 1 and in_planes != out_planes: # shortcut_node_id = node_id # shortcut_node_id = graph.add_layer(self.conv(in_planes, # out_planes, # kernel_size=1, # stride=1, # padding=0), shortcut_node_id) # shortcut_node_id = graph.add_layer(self.batch_norm(graph.node_list[shortcut_node_id].shape[-1]), shortcut_node_id) # output_node_id = graph.add_layer(StubAdd(), (output_node_id, shortcut_node_id)) return output_node_id
def to_add_skip_model(self, start_id, end_id): """Add a weighted add skip-connection from after start node to end node. Args: start_id: The convolutional layer ID, after which to start the skip-connection. end_id: The convolutional layer ID, after which to end the skip-connection. """ self.operation_history.append(('to_add_skip_model', start_id, end_id)) conv_block_input_id = self._conv_block_end_node(start_id) conv_block_input_id = self.adj_list[conv_block_input_id][0][0] block_last_layer_input_id = self._conv_block_end_node(end_id) # Add the pooling layer chain. layer_list = self._get_pooling_layers(conv_block_input_id, block_last_layer_input_id) skip_output_id = conv_block_input_id for index, layer_id in enumerate(layer_list): skip_output_id = self.add_layer(deepcopy(self.layer_list[layer_id]), skip_output_id) # Add the conv layer new_relu_layer = StubReLU() skip_output_id = self.add_layer(new_relu_layer, skip_output_id) new_conv_layer = self.conv(self.layer_list[start_id].filters, self.layer_list[end_id].filters, 1) skip_output_id = self.add_layer(new_conv_layer, skip_output_id) new_bn_layer = self.batch_norm(self.layer_list[end_id].filters) skip_output_id = self.add_layer(new_bn_layer, skip_output_id) # Add the add layer. block_last_layer_output_id = self.adj_list[block_last_layer_input_id][0][0] add_input_node_id = self._add_node(deepcopy(self.node_list[block_last_layer_output_id])) add_layer = StubAdd() self._redirect_edge(block_last_layer_input_id, block_last_layer_output_id, add_input_node_id) self._add_edge(add_layer, add_input_node_id, block_last_layer_output_id) self._add_edge(add_layer, skip_output_id, block_last_layer_output_id) add_layer.input = [self.node_list[add_input_node_id], self.node_list[skip_output_id]] add_layer.output = self.node_list[block_last_layer_output_id] self.node_list[block_last_layer_output_id].shape = add_layer.output_shape # Set weights to the additional conv layer. if self.weighted: filters_end = self.layer_list[end_id].filters filters_start = self.layer_list[start_id].filters filter_shape = (1,) * (len(self.layer_list[end_id].get_weights()[0].shape) - 2) weights = np.zeros((filters_end, filters_start) + filter_shape) bias = np.zeros(filters_end) new_conv_layer.set_weights((add_noise(weights, np.array([0, 1])), add_noise(bias, np.array([0, 1])))) n_filters = filters_end 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]))] new_bn_layer.set_weights(new_weights)
def _insert_pooling_layer_chain(self, start_node_id, end_node_id): skip_output_id = start_node_id for layer in self._get_pooling_layers(start_node_id, end_node_id): new_layer = deepcopy(layer) if is_layer(new_layer, 'Conv'): filters = self.node_list[start_node_id].shape[-1] new_layer = get_conv_class(self.n_dim)(filters, filters, 1, layer.stride) else: new_layer = deepcopy(layer) skip_output_id = self.add_layer(new_layer, skip_output_id) skip_output_id = self.add_layer(StubReLU(), skip_output_id) return skip_output_id
def _transition(self, num_input_features, num_output_features, graph, input_node_id): out = graph.add_layer(self.batch_norm(num_features=num_input_features), input_node_id) out = graph.add_layer(StubReLU(), out) out = graph.add_layer( self.conv(num_input_features, num_output_features, kernel_size=1, stride=1), out) out = graph.add_layer(self.avg_pooling(kernel_size=2, stride=2), out) return out
def _insert_pooling_layer_chain(self, start_node_id, end_node_id): skip_output_id = start_node_id for layer in self._get_pooling_layers(start_node_id, end_node_id): new_layer = deepcopy(layer) if is_layer(new_layer, 'Conv'): filters = self.node_list[start_node_id].shape[-1] kernel_size = layer.kernel_size if layer.padding != int( layer.kernel_size / 2) or layer.stride != 1 else 1 new_layer = get_conv_class(self.n_dim)(filters, filters, kernel_size, layer.stride, padding=layer.padding) if self.weighted: init_conv_weight(new_layer) else: new_layer = deepcopy(layer) skip_output_id = self.add_layer(new_layer, skip_output_id) skip_output_id = self.add_layer(StubReLU(), skip_output_id) return skip_output_id
def generate(self, model_len=Constant.MLP_MODEL_LEN, model_width=Constant.MLP_MODEL_WIDTH): if type(model_width) is list and not len(model_width) == model_len: raise ValueError('The length of \'model_width\' does not match \'model_len\'') elif type(model_width) is int: model_width = [model_width] * model_len graph = Graph(self.input_shape, False) output_node_id = 0 n_nodes_prev_layer = self.input_shape[0] for width in model_width: output_node_id = graph.add_layer(StubDense(n_nodes_prev_layer, width), output_node_id) output_node_id = graph.add_layer(StubDropout1d(Constant.MLP_DROPOUT_RATE), output_node_id) output_node_id = graph.add_layer(StubReLU(), output_node_id) n_nodes_prev_layer = width graph.add_layer(StubDense(n_nodes_prev_layer, self.n_output_node), output_node_id) return graph
def generate(self, model_len=None, model_width=None): if model_width is None: model_width = Constant.MODEL_WIDTH graph = Graph(self.input_shape, False) temp_input_channel = self.input_shape[-1] output_node_id = 0 output_node_id = graph.add_layer(self.conv(temp_input_channel, model_width, kernel_size=3), output_node_id) output_node_id = graph.add_layer(self.batch_norm(model_width), output_node_id) output_node_id = graph.add_layer(StubReLU(), output_node_id) # output_node_id = graph.add_layer(self.pooling(kernel_size=3, stride=2, padding=1), output_node_id) output_node_id = self._make_layer(graph, model_width, self.layers[0], output_node_id, 1) model_width *= 2 output_node_id = self._make_layer(graph, model_width, self.layers[1], output_node_id, 2) model_width *= 2 output_node_id = self._make_layer(graph, model_width, self.layers[2], output_node_id, 2) model_width *= 2 output_node_id = self._make_layer(graph, model_width, self.layers[3], output_node_id, 2) output_node_id = graph.add_layer(self.global_avg_pooling(), output_node_id) graph.add_layer(StubDense(model_width * self.block_expansion, self.n_output_node), output_node_id) return graph
def generate(self, model_len, model_width): graph = Graph(self.input_shape, False) temp_input_channel = self.input_shape[-1] output_node_id = 0 output_node_id = graph.add_layer(StubReLU(), output_node_id) output_node_id = graph.add_layer( self.conv(temp_input_channel, model_width, kernel_size=7), output_node_id) output_node_id = graph.add_layer(self.batch_norm(model_width), output_node_id) output_node_id = graph.add_layer( self.pooling(kernel_size=3, stride=2, padding=1), output_node_id) for layer in self.layers: output_node_id = self._make_layer(graph, model_width, layer, output_node_id) model_width *= 2 output_node_id = graph.add_layer(self.global_avg_pooling(), output_node_id) graph.add_layer( StubDense( int(model_width / 2) * self.block_expansion, self.n_output_node), output_node_id) return graph
def create_new_layer(layer, n_dim): 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, LayerType.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, LayerType.DROPOUT): dense_deeper_classes = [StubDense, StubReLU] elif is_layer(layer, LayerType.BATCH_NORM): conv_deeper_classes = [get_conv_class(n_dim)] #, StubReLU] new_layers = [] 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]) new_layers.append(new_layer) elif layer_class == get_dropout_class(n_dim): new_layer = layer_class(Constant.DENSE_DROPOUT_RATE) new_layers.append(new_layer) elif layer_class == get_conv_class(n_dim): # add conv layer # new_layer = layer_class(input_shape[-1],, input_shape[-1], sample((1, 3, 5), 1)[0], stride=1) # add mobilenet block in_planes = input_shape[-1] expansion = sample((1, 6), 1)[0] stride = sample((1, 2), 1)[0] planes = expansion * in_planes new_layer = layer_class(in_planes, planes, 1, stride=1, padding=0) new_layers.append(new_layer) new_layer = get_batch_norm_class(n_dim)(planes) new_layers.append(new_layer) new_layer = StubReLU() new_layers.append(new_layer) new_layer = layer_class(planes, planes, 3, stride=stride, padding=1, groups=planes) new_layers.append(new_layer) new_layer = get_batch_norm_class(n_dim)(planes) new_layers.append(new_layer) new_layer = StubReLU() new_layers.append(new_layer) new_layer = layer_class(planes, in_planes, 1, stride=1, padding=0) new_layers.append(new_layer) new_layer = get_batch_norm_class(n_dim)(in_planes) new_layers.append(new_layer) elif layer_class == get_batch_norm_class(n_dim): new_layer = layer_class(input_shape[-1]) new_layers.append(new_layer) elif layer_class == get_pooling_class(n_dim): new_layer = layer_class(sample((1, 3, 5), 1)[0]) new_layers.append(new_layer) else: new_layer = layer_class() new_layers.append(new_layer) return new_layers
def to_concat_skip_model(self, start_id, end_id): """Add a weighted add concatenate connection from after start node to end node. Args: start_id: The convolutional layer ID, after which to start the skip-connection. end_id: The convolutional layer ID, after which to end the skip-connection. """ self.operation_history.append(('to_concat_skip_model', start_id, end_id)) conv_block_input_id = self._conv_block_end_node(start_id) conv_block_input_id = self.adj_list[conv_block_input_id][0][0] block_last_layer_input_id = self._conv_block_end_node(end_id) # Add the pooling layer chain. pooling_layer_list = self._get_pooling_layers(conv_block_input_id, block_last_layer_input_id) skip_output_id = conv_block_input_id for index, layer_id in enumerate(pooling_layer_list): skip_output_id = self.add_layer(deepcopy(self.layer_list[layer_id]), skip_output_id) block_last_layer_output_id = self.adj_list[block_last_layer_input_id][0][0] concat_input_node_id = self._add_node(deepcopy(self.node_list[block_last_layer_output_id])) self._redirect_edge(block_last_layer_input_id, block_last_layer_output_id, concat_input_node_id) concat_layer = StubConcatenate() concat_layer.input = [self.node_list[concat_input_node_id], self.node_list[skip_output_id]] concat_output_node_id = self._add_node(Node(concat_layer.output_shape)) self._add_edge(concat_layer, concat_input_node_id, concat_output_node_id) self._add_edge(concat_layer, skip_output_id, concat_output_node_id) concat_layer.output = self.node_list[concat_output_node_id] self.node_list[concat_output_node_id].shape = concat_layer.output_shape # Add the concatenate layer. new_relu_layer = StubReLU() concat_output_node_id = self.add_layer(new_relu_layer, concat_output_node_id) new_conv_layer = self.conv(self.layer_list[start_id].filters + self.layer_list[end_id].filters, self.layer_list[end_id].filters, 1) concat_output_node_id = self.add_layer(new_conv_layer, concat_output_node_id) new_bn_layer = self.batch_norm(self.layer_list[end_id].filters) self._add_edge(new_bn_layer, concat_output_node_id, block_last_layer_output_id) new_bn_layer.input = self.node_list[concat_output_node_id] new_bn_layer.output = self.node_list[block_last_layer_output_id] self.node_list[block_last_layer_output_id].shape = new_bn_layer.output_shape if self.weighted: filters_end = self.layer_list[end_id].filters filters_start = self.layer_list[start_id].filters filter_shape = (1,) * (len(self.layer_list[end_id].get_weights()[0].shape) - 2) weights = np.zeros((filters_end, filters_end) + filter_shape) for i in range(filters_end): filter_weight = np.zeros((filters_end,) + filter_shape) center_index = (i,) + (0,) * self.n_dim filter_weight[center_index] = 1 weights[i, ...] = filter_weight weights = np.concatenate((weights, np.zeros((filters_end, filters_start) + filter_shape)), axis=1) bias = np.zeros(filters_end) new_conv_layer.set_weights((add_noise(weights, np.array([0, 1])), add_noise(bias, np.array([0, 1])))) n_filters = filters_end 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]))] new_bn_layer.set_weights(new_weights)