def _search(self, u, start_dim, total_dim, n_add): """Search the graph for widening the layers. Args: u: The starting node identifier. start_dim: The position to insert the additional dimensions. total_dim: The total number of dimensions the layer has before widening. n_add: The number of dimensions to add. """ if (u, start_dim, total_dim, n_add) in self.vis: return self.vis[(u, start_dim, total_dim, n_add)] = True for v, layer_id in self.adj_list[u]: layer = self.layer_list[layer_id] if is_layer(layer, 'Conv'): new_layer = wider_next_conv(layer, start_dim, total_dim, n_add, self.weighted) self._replace_layer(layer_id, new_layer) elif is_layer(layer, 'Dense'): new_layer = wider_next_dense(layer, start_dim, total_dim, n_add, self.weighted) self._replace_layer(layer_id, new_layer) elif is_layer(layer, 'BatchNormalization'): new_layer = wider_bn(layer, start_dim, total_dim, n_add, self.weighted) self._replace_layer(layer_id, new_layer) self._search(v, start_dim, total_dim, n_add) elif is_layer(layer, 'Concatenate'): if self.layer_id_to_input_node_ids[layer_id][1] == u: # u is on the right of the concat # next_start_dim += next_total_dim - total_dim left_dim = self._upper_layer_width( self.layer_id_to_input_node_ids[layer_id][0]) next_start_dim = start_dim + left_dim next_total_dim = total_dim + left_dim else: next_start_dim = start_dim next_total_dim = total_dim + self._upper_layer_width( self.layer_id_to_input_node_ids[layer_id][1]) self._search(v, next_start_dim, next_total_dim, n_add) else: self._search(v, start_dim, total_dim, n_add) for v, layer_id in self.reverse_adj_list[u]: layer = self.layer_list[layer_id] if is_layer(layer, 'Conv'): new_layer = wider_pre_conv(layer, n_add, self.weighted) self._replace_layer(layer_id, new_layer) elif is_layer(layer, 'Dense'): new_layer = wider_pre_dense(layer, n_add, self.weighted) self._replace_layer(layer_id, new_layer) elif is_layer(layer, 'Concatenate'): continue else: self._search(v, start_dim, total_dim, n_add)
def _search(self, u, start_dim, total_dim, n_add): """Search the graph for widening the layers. Args: u: The starting node identifier. start_dim: The position to insert the additional dimensions. total_dim: The total number of dimensions the layer has before widening. n_add: The number of dimensions to add. """ if (u, start_dim, total_dim, n_add) in self.vis: return self.vis[(u, start_dim, total_dim, n_add)] = True for v, layer_id in self.adj_list[u]: layer = self.layer_list[layer_id] if is_layer(layer, 'Conv'): new_layer = wider_next_conv(layer, start_dim, total_dim, n_add, self.weighted) self._replace_layer(layer_id, new_layer) elif is_layer(layer, 'Dense'): new_layer = wider_next_dense(layer, start_dim, total_dim, n_add, self.weighted) self._replace_layer(layer_id, new_layer) elif is_layer(layer, 'BatchNormalization'): new_layer = wider_bn(layer, start_dim, total_dim, n_add, self.weighted) self._replace_layer(layer_id, new_layer) self._search(v, start_dim, total_dim, n_add) elif is_layer(layer, 'Concatenate'): if self.layer_id_to_input_node_ids[layer_id][1] == u: # u is on the right of the concat # next_start_dim += next_total_dim - total_dim left_dim = self._upper_layer_width(self.layer_id_to_input_node_ids[layer_id][0]) next_start_dim = start_dim + left_dim next_total_dim = total_dim + left_dim else: next_start_dim = start_dim next_total_dim = total_dim + self._upper_layer_width(self.layer_id_to_input_node_ids[layer_id][1]) self._search(v, next_start_dim, next_total_dim, n_add) else: self._search(v, start_dim, total_dim, n_add) for v, layer_id in self.reverse_adj_list[u]: layer = self.layer_list[layer_id] if is_layer(layer, 'Conv'): new_layer = wider_pre_conv(layer, n_add, self.weighted) self._replace_layer(layer_id, new_layer) elif is_layer(layer, 'Dense'): new_layer = wider_pre_dense(layer, n_add, self.weighted) self._replace_layer(layer_id, new_layer) elif is_layer(layer, 'Concatenate'): continue else: self._search(v, start_dim, total_dim, n_add)
def to_wider_model(self, pre_layer_id, n_add): """Widen the last dimension of the output of the pre_layer. Args: pre_layer_id: A convolutional layer or dense layer. n_add: The number of dimensions to add. Returns: A new Keras model with the widened layers. """ self.operation_history.append(('to_wider_model', pre_layer_id, n_add)) pre_layer = self.layer_list[pre_layer_id] output_id = self.layer_id_to_output_node_ids[pre_layer_id][0] dim = layer_width(pre_layer) if is_layer(pre_layer, 'Conv'): new_layer = wider_pre_conv(pre_layer, n_add, self.weighted) self._replace_layer(pre_layer_id, new_layer) else: new_layer = wider_pre_dense(pre_layer, n_add, self.weighted) self._replace_layer(pre_layer_id, new_layer) self._search_next(output_id, dim, dim, n_add)
def _search_pre(self, u, start_dim, total_dim, n_add): """Search upward the graph for widening the layers. Args: u: The starting node identifier. start_dim: The dimension to insert the additional dimensions. total_dim: The total number of dimensions the layer has before widening. n_add: The number of dimensions to add. """ if self.pre_vis[u]: return self.pre_vis[u] = True self._search_next(u, start_dim, total_dim, n_add) for v, layer_id in self.reverse_adj_list[u]: layer = self.layer_list[layer_id] if is_layer(layer, 'Conv'): new_layer = wider_pre_conv(layer, n_add, self.weighted) self._replace_layer(layer_id, new_layer) elif is_layer(layer, 'Dense'): new_layer = wider_pre_dense(layer, n_add, self.weighted) self._replace_layer(layer_id, new_layer) elif is_layer(layer, 'BatchNormalization'): self._search_pre(v, start_dim, total_dim, n_add) elif is_layer(layer, 'Concatenate'): if self.layer_id_to_input_node_ids[layer_id][1] == v: # v is on the right other_branch_v = self.layer_id_to_input_node_ids[layer_id][ 0] if self.pre_vis[other_branch_v]: # The other branch is already been widen, which means the widen for upper part of this concat # layer is done. continue pre_total_dim = self._upper_layer_width(v) pre_start_dim = start_dim - (total_dim - pre_total_dim) self._search_pre(v, pre_start_dim, pre_total_dim, n_add) else: self._search_pre(v, start_dim, total_dim, n_add)
def _wider_pre_conv(self, layer, n_add): return wider_pre_conv(layer, n_add)