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 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)) filters_end = self.layer_list[end_id].output.shape[-1] filters_start = self.layer_list[start_id].output.shape[-1] start_node_id = self.layer_id_to_output_node_ids[start_id][0] pre_end_node_id = self.layer_id_to_input_node_ids[end_id][0] end_node_id = self.layer_id_to_output_node_ids[end_id][0] skip_output_id = self._insert_pooling_layer_chain( start_node_id, end_node_id) concat_input_node_id = self._add_node( deepcopy(self.node_list[end_node_id])) self._redirect_edge(pre_end_node_id, end_node_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_conv_layer = get_conv_class(self.n_dim)( filters_start + filters_end, filters_end, 1) self._add_edge(new_conv_layer, concat_output_node_id, end_node_id) new_conv_layer.input = self.node_list[concat_output_node_id] new_conv_layer.output = self.node_list[end_node_id] self.node_list[end_node_id].shape = new_conv_layer.output_shape if self.weighted: filter_shape = (1, ) * self.n_dim 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]))))
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 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)