Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
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, LayerType.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, LayerType.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, LayerType.BATCH_NORM):
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, LayerType.CONCAT):
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)
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]
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):
def _get_pooling_layers(self, start_node_id, end_node_id):
"""Given two node IDs, return all the pooling layers between them."""
layer_list = []
node_list = [start_node_id]
if not self._depth_first_search(end_node_id, layer_list, node_list):
raise AssertionError("Error: node %d not found among all the layers." % end_node_id)
ret = []
for layer_id in layer_list:
layer = self.layer_list[layer_id]
if is_layer(layer, LayerType.POOL):
ret.append(layer)
elif is_layer(layer, LayerType.CONV) and (layer.stride != 1 or layer.padding != int(layer.kernel_size / 2)):
ret.append(layer)
return ret
def _get_pooling_layers(self, start_node_id, end_node_id):
"""Given two node IDs, return all the pooling layers between them."""
layer_list = []
node_list = [start_node_id]
if not self._depth_first_search(end_node_id, layer_list, node_list):
raise AssertionError("Error: node %d not found among all the layers." % end_node_id)
ret = []
for layer_id in layer_list:
layer = self.layer_list[layer_id]
if is_layer(layer, LayerType.POOL):
ret.append(layer)
elif is_layer(layer, LayerType.CONV) and (layer.stride != 1 or layer.padding != int(layer.kernel_size / 2)):
ret.append(layer)
return ret
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]
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)
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]
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])
def deep_layer_ids(self):
ret = []
for layer_id in self.get_main_chain_layers():
layer = self.layer_list[layer_id]
if is_layer(layer, LayerType.GLOBAL_POOL):
break
if is_layer(layer, LayerType.ADD) or is_layer(layer, LayerType.CONCAT):
continue
ret.append(layer_id)
return ret
def to_wider_graph(graph):
weighted_layer_ids = graph.wide_layer_ids()
weighted_layer_ids = list(filter(lambda x: graph.layer_list[x].output.shape[-1], weighted_layer_ids))
wider_layers = sample(weighted_layer_ids, 1)
for layer_id in wider_layers:
layer = graph.layer_list[layer_id]
if is_layer(layer, LayerType.CONV):
n_add = layer.filters
else:
n_add = layer.units
graph.to_wider_model(layer_id, n_add)
return graph