def _get_layer_detail(self, layer): variables = [v for v in self.var_list if layer.group_name == v.name.split('/')[self._level + 1]] num, dense_num = stark.get_params_num(variables, consider_prune=True) # Generate a row row = [self._get_layer_string(layer, True, True), layer.output_shape_str, stark.get_num_string(num, dense_num)] return row, num, dense_num
def structure_detail(self): # TODO: res_block structure_detail from tframe import hub from tframe.utils import stark import tframe.utils.format_string as fs widths = [33, 24, 20] rows = [] add_to_rows = lambda cols: rows.append(fs.table_row(cols, widths)) total_params, dense_total = 0, 0 def get_num_string(num, dense_num): if num == 0: num_str = '' elif hub.prune_on or hub.etch_on: num_str = '{} ({:.1f}%)'.format(num, 100.0 * num / dense_num) else: num_str = str(num) return num_str for child in self.children + [self._transform_layer ] + self._post_processes: if child is None: continue if isinstance(child, Layer): # Try to find variable in child # TODO: to be fixed # variables = [v for v in self.var_list if child.group_name in v.name] variables = [ v for v in self.var_list if child.group_name == v.name.split('/')[self._level + 1] ] num, dense_num = stark.get_params_num(variables, consider_prune=True) # Generate a row cols = [ self._get_layer_string(child, True, True), child.output_shape_str, get_num_string(num, dense_num) ] add_to_rows(cols) else: raise TypeError('!! unknown child type {}'.format(type(child))) # Accumulate total_params and dense_total_params total_params += num dense_total += dense_num return rows, total_params, dense_total
def params_num(self): return stark.get_params_num(self.var_list, consider_prune=True)
def structure_detail(self): """A list of structure strings with format Layer (type) Output Shape Params # Currently only work for sequential model """ from tframe.nets.rnet import RNet widths = [33, 24, 20] indent = 3 rows = [] add_to_rows = lambda cols: rows.append(fs.table_row(cols, widths)) # Dense total will be used when model weights are pruned total_params, dense_total = 0, 0 if self.is_root: add_to_rows(['input', shape_string(self.input_.sample_shape), '']) def get_num_string(num, dense_num): if num == 0: num_str = '' elif hub.prune_on or hub.etch_on: num_str = '{} ({:.1f}%)'.format(num, 100.0 * num / dense_num) else: num_str = str(num) return num_str for child in self.children: if isinstance(child, Layer): # Try to find variable in child variables = [v for v in self.var_list if child.group_name in v.name] num, dense_num = stark.get_params_num(variables, consider_prune=True) # Generate a row cols = [self._get_layer_string(child, True, True), child.output_shape_str, get_num_string(num, dense_num)] add_to_rows(cols) elif isinstance(child, RNet): num, dense_num = child.params_num cols = [child.structure_string(), child.output_shape_str, get_num_string(num, dense_num)] add_to_rows(cols) elif isinstance(child, Net): _rows, num, dense_num = child.structure_detail rows += _rows else: raise TypeError('!! unknown child type {}'.format(type(child))) # Accumulate total_params and dense_total_params total_params += num dense_total += dense_num if self.is_root: # Head detail = '' add_with_indent = lambda d, c: d + ' ' * indent + c + '\n' width = sum(widths) detail = add_with_indent(detail, '-' * width) detail = add_with_indent( detail, fs.table_row(['Layers', 'Output Shape', 'Params #'], widths)) detail = add_with_indent(detail, '=' * width) # Content for i, row in enumerate(rows): if i > 0: detail = add_with_indent(detail, '-' * width) detail = add_with_indent(detail, row) # Summary detail = add_with_indent(detail, '=' * width) detail = add_with_indent( detail, 'Total params: {}'.format( get_num_string(total_params, dense_total))) detail += ' ' * indent + '-' * width return detail, total_params, dense_total else: return rows, total_params, dense_total