#!/usr/bin/env python3

import argparse
import numpy
import re
import os
import os.path

parser = argparse.ArgumentParser()
parser.add_argument('-x', '--confusion', type=argparse.FileType('r'), help='read confusion matrix from this file', default='confusion-large.db')
parser.add_argument('-t', '--tagged', help='load tagged data from this directory', default='tagged/full')
parser.add_argument('-d', '--devkit', help='devkit directory containing categories.txt', default='mp-dev_kit')
args = parser.parse_args()

categories = []
for line in open(os.path.join(args.devkit, 'categories.txt')).readlines():
    assert int(line.strip().split()[1]) == len(categories)
    categories.append(re.sub('^/[a-z]/', '', line.strip().split()[0]))
cats = len(categories)

labels = {}
for line in open(os.path.join(args.devkit, 'train.txt')).readlines():
    name, label = line.strip().split()
    labels[name] = int(label)

filenames = [line.strip() for line in
        open(os.path.join(args.tagged, 'train.filenames.txt')).readlines()]
cases = len(filenames)

predictions = numpy.memmap(args.confusion, dtype=numpy.float32, shape=(cases, cats), mode='r')
topindex = numpy.argsort(-predictions, axis=1)

for index in range(cases):
    top = topindex[index]
    topscore = predictions[index][top]
    correct = labels[filenames[index]]
    confusion = numpy.where(top == correct)[0][0]
    print("{} {} should be {}, was {} {}, {} {}, {} {}, {} {}, {} {}".format(
        confusion,
        filenames[index],
        categories[labels[filenames[index]]],
        categories[top[0]],
        topscore[0],
        categories[top[1]],
        topscore[1],
        categories[top[2]],
        topscore[2],
        categories[top[3]],
        topscore[3],
        categories[top[4]],
        topscore[4],
    ))

@@ -143,128 +143,200 @@
 t('-
-x
+t
 ', '--
-confusion', type=argparse.FileType('r'), help='read confusion matrix from this file', default='confusion-large.db
+tagged', help='load tagged data from this directory', default='tagged/full')%0Aparser.add_argument('-d', '--devkit', help='devkit directory containing categories.txt', default='mp-dev_kit
 ')%0Ap
@@ -352,37 +352,37 @@
 _argument('-
-t
+o
 ', '--
-tagged
+outdir
 ', help='loa
@@ -378,37 +378,46 @@
 , help='
-load tagged data from
+store trained network state in
  this di
@@ -430,37 +430,28 @@
 y', default=
-'tagged/full'
+None
 )%0Aparser.add
@@ -458,37 +458,38 @@
 _argument('-
-d
+n
 ', '--
-devkit
+network
 ', help='dev
@@ -489,50 +489,109 @@
 lp='
-devkit directory containing categories.txt
+name of network experiment', default='base')%0Aparser.add_argument('-s', '--subset', help='train or val
 ', d
@@ -598,26 +598,21 @@
 efault='
-mp-dev_kit
+train
 ')%0Aargs 
@@ -634,16 +634,90 @@
 args()%0A%0A
+if args.outdir is None:%0A    args.outdir = %22exp-%7B%7D%22.format(args.network)%0A%0A%0A
 categori
@@ -987,32 +987,41 @@
 en(os.path.join(
+%0A        
 args.devkit, 'tr
@@ -1022,18 +1022,29 @@
 t, '
-train.txt'
+%25s.txt' %25 args.subset
 )).r
@@ -1162,24 +1162,16 @@
  line in
-%0A       
  open(os
@@ -1177,24 +1177,33 @@
 s.path.join(
+%0A        
 args.tagged,
@@ -1204,21 +1204,18 @@
 agged, '
-train
+%25s
 .filenam
@@ -1217,24 +1217,38 @@
 lenames.txt'
+ %25 args.subset
 )).readlines
@@ -1275,16 +1275,107 @@
 names)%0A%0A
+confusion_file = open(os.path.join(args.outdir,%0A    '%25s.confusion.db' %25 args.subset), 'r')%0A
 predicti
@@ -1393,21 +1393,21 @@
 .memmap(
-args.
+%0A    
 confusio
@@ -1407,16 +1407,21 @@
 onfusion
+_file
 , dtype=

Improve command-line handling for print_confusion.