| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -127,7 +127,7 @@ def choose_color_by_layertype(layertype): | |||
| 127 | 127 | return color | |
| 128 | 128 | ||
| 129 | 129 | ||
| 130 | - def get_pydot_graph(caffe_net, rankdir, label_edges=True): | ||
| 130 | + def get_pydot_graph(caffe_net, rankdir, label_edges=True, phase=None): | ||
| 131 | 131 | """Create a data structure which represents the `caffe_net`. | |
| 132 | 132 | ||
| 133 | 133 | Parameters | |
@@ -137,6 +137,9 @@ def get_pydot_graph(caffe_net, rankdir, label_edges=True): | |||
| 137 | 137 | Direction of graph layout. | |
| 138 | 138 | label_edges : boolean, optional | |
| 139 | 139 | Label the edges (default is True). | |
| 140 | + phase : {caffe_pb2.Phase.TRAIN, caffe_pb2.Phase.TEST, None} optional | ||
| 141 | + Include layers from this network phase. If None, include all layers. | ||
| 142 | + (the default is None) | ||
| 140 | 143 | ||
| 141 | 144 | Returns | |
| 142 | 145 | ------- | |
@@ -148,6 +151,19 @@ def get_pydot_graph(caffe_net, rankdir, label_edges=True): | |||
| 148 | 151 | pydot_nodes = {} | |
| 149 | 152 | pydot_edges = [] | |
| 150 | 153 | for layer in caffe_net.layer: | |
| 154 | + if phase is not None: | ||
| 155 | + included = False | ||
| 156 | + if len(layer.include) == 0: | ||
| 157 | + included = True | ||
| 158 | + if len(layer.include) > 0 and len(layer.exclude) > 0: | ||
| 159 | + raise ValueError('layer ' + layer.name + ' has both include ' | ||
| 160 | + 'and exclude specified.') | ||
| 161 | + for layer_phase in layer.include: | ||
| 162 | + included = included or layer_phase.phase == phase | ||
| 163 | + for layer_phase in layer.exclude: | ||
| 164 | + included = included and not layer_phase.phase == phase | ||
| 165 | + if not included: | ||
| 166 | + continue | ||
| 151 | 167 | node_label = get_layer_label(layer, rankdir) | |
| 152 | 168 | node_name = "%s_%s" % (layer.name, layer.type) | |
| 153 | 169 | if (len(layer.bottom) == 1 and len(layer.top) == 1 and | |
@@ -186,7 +202,7 @@ def get_pydot_graph(caffe_net, rankdir, label_edges=True): | |||
| 186 | 202 | return pydot_graph | |
| 187 | 203 | ||
| 188 | 204 | ||
| 189 | - def draw_net(caffe_net, rankdir, ext='png'): | ||
| 205 | + def draw_net(caffe_net, rankdir, ext='png', phase=None): | ||
| 190 | 206 | """Draws a caffe net and returns the image string encoded using the given | |
| 191 | 207 | extension. | |
| 192 | 208 | ||
@@ -195,16 +211,19 @@ def draw_net(caffe_net, rankdir, ext='png'): | |||
| 195 | 211 | caffe_net : a caffe.proto.caffe_pb2.NetParameter protocol buffer. | |
| 196 | 212 | ext : string, optional | |
| 197 | 213 | The image extension (the default is 'png'). | |
| 214 | + phase : {caffe_pb2.Phase.TRAIN, caffe_pb2.Phase.TEST, None} optional | ||
| 215 | + Include layers from this network phase. If None, include all layers. | ||
| 216 | + (the default is None) | ||
| 198 | 217 | ||
| 199 | 218 | Returns | |
| 200 | 219 | ------- | |
| 201 | 220 | string : | |
| 202 | 221 | Postscript representation of the graph. | |
| 203 | 222 | """ | |
| 204 | - return get_pydot_graph(caffe_net, rankdir).create(format=ext) | ||
| 223 | + return get_pydot_graph(caffe_net, rankdir, phase=phase).create(format=ext) | ||
| 205 | 224 | ||
| 206 | 225 | ||
| 207 | - def draw_net_to_file(caffe_net, filename, rankdir='LR'): | ||
| 226 | + def draw_net_to_file(caffe_net, filename, rankdir='LR', phase=None): | ||
| 208 | 227 | """Draws a caffe net, and saves it to file using the format given as the | |
| 209 | 228 | file extension. Use '.raw' to output raw text that you can manually feed | |
| 210 | 229 | to graphviz to draw graphs. | |
@@ -216,7 +235,10 @@ def draw_net_to_file(caffe_net, filename, rankdir='LR'): | |||
| 216 | 235 | The path to a file where the networks visualization will be stored. | |
| 217 | 236 | rankdir : {'LR', 'TB', 'BT'} | |
| 218 | 237 | Direction of graph layout. | |
| 238 | + phase : {caffe_pb2.Phase.TRAIN, caffe_pb2.Phase.TEST, None} optional | ||
| 239 | + Include layers from this network phase. If None, include all layers. | ||
| 240 | + (the default is None) | ||
| 219 | 241 | """ | |
| 220 | 242 | ext = filename[filename.rfind('.')+1:] | |
| 221 | 243 | with open(filename, 'wb') as fid: | |
| 222 | - fid.write(draw_net(caffe_net, rankdir, ext)) | ||
| 244 | + fid.write(draw_net(caffe_net, rankdir, ext, phase)) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,6 +28,11 @@ def parse_args(): | |||
| 28 | 28 | 'http://www.graphviz.org/doc/info/' | |
| 29 | 29 | 'attrs.html#k:rankdir'), | |
| 30 | 30 | default='LR') | |
| 31 | + parser.add_argument('--phase', | ||
| 32 | + help=('Which network phase to draw: can be TRAIN, ' | ||
| 33 | + 'TEST, or ALL. If ALL, then all layers are drawn ' | ||
| 34 | + 'regardless of phase.'), | ||
| 35 | + default="ALL") | ||
| 31 | 36 | ||
| 32 | 37 | args = parser.parse_args() | |
| 33 | 38 | return args | |
@@ -38,7 +43,15 @@ def main(): | |||
| 38 | 43 | net = caffe_pb2.NetParameter() | |
| 39 | 44 | text_format.Merge(open(args.input_net_proto_file).read(), net) | |
| 40 | 45 | print('Drawing net to %s' % args.output_image_file) | |
| 41 | - caffe.draw.draw_net_to_file(net, args.output_image_file, args.rankdir) | ||
| 46 | + phase=None; | ||
| 47 | + if args.phase == "TRAIN": | ||
| 48 | + phase = caffe.TRAIN | ||
| 49 | + elif args.phase == "TEST": | ||
| 50 | + phase = caffe.TEST | ||
| 51 | + elif args.phase != "ALL": | ||
| 52 | + raise ValueError("Unknown phase: " + args.phase) | ||
| 53 | + caffe.draw.draw_net_to_file(net, args.output_image_file, args.rankdir, | ||
| 54 | + phase) | ||
| 42 | 55 | ||
| 43 | 56 | ||
| 44 | 57 | if __name__ == '__main__': | |
| Back | FazBrowse Home | New Git URL |
0 commit comments