Exemplo n.º 1
0
def main(cm_file, perm_file, steps, labels_file):
    """Orchestrate."""
    # Load confusion matrix
    with open(cm_file) as f:
        cm = json.load(f)
        cm = np.array(cm)

    # Load permutation
    if os.path.isfile(perm_file):
        print("loaded %s" % perm_file)
        with open(perm_file) as data_file:
            perm = json.load(data_file)
    else:
        perm = random.shuffle(list(range(len(cm))))

    print("Score without perm: {:0.2f}%".format(
        calculate_split_error(cm) * 100))
    result = simulated_annealing(cm,
                                 perm,
                                 score=calculate_split_error,
                                 deterministic=True,
                                 steps=steps)
    # First recursive step
    # split_i = int(len(cm) / 2)
    # cm = result['cm'][:split_i, :split_i]
    # perm = list(range(split_i))
    # result = simulated_annealing(cm, perm,
    #                              score=calculate_split_error,
    #                              deterministic=True,
    #                              steps=steps)

    print("Score: {}".format(calculate_split_error(result['cm'])))
    print("Perm: {}".format(list(result['perm'])))
    # Load labels
    if os.path.isfile(labels_file):
        with open(labels_file) as f:
            symbols = json.load(f)
    else:
        symbols = read_symbols()
    print("Symbols: {}".format([symbols[i] for i in result['perm']]))
    plot_cm(result['cm'], zero_diagonal=True)
Exemplo n.º 2
0
def main(cm_file, perm_file, steps, labels_file):
    """Orchestrate."""
    # Load confusion matrix
    with open(cm_file) as f:
        cm = json.load(f)
        cm = np.array(cm)

    # Load permutation
    if os.path.isfile(perm_file):
        print("loaded %s" % perm_file)
        with open(perm_file) as data_file:
            perm = json.load(data_file)
    else:
        perm = random.shuffle(list(range(len(cm))))

    print("Score without perm: {:0.2f}%".format(calculate_split_error(cm) * 100))
    result = simulated_annealing(cm, perm,
                                 score=calculate_split_error,
                                 deterministic=True,
                                 steps=steps)
    # First recursive step
    # split_i = int(len(cm) / 2)
    # cm = result['cm'][:split_i, :split_i]
    # perm = list(range(split_i))
    # result = simulated_annealing(cm, perm,
    #                              score=calculate_split_error,
    #                              deterministic=True,
    #                              steps=steps)

    print("Score: {}".format(calculate_split_error(result['cm'])))
    print("Perm: {}".format(list(result['perm'])))
    # Load labels
    if os.path.isfile(labels_file):
        with open(labels_file, "r") as f:
            symbols = json.load(f)
    else:
        symbols = read_symbols()
    print("Symbols: {}".format([symbols[i] for i in result['perm']]))
    plot_cm(result['cm'], zero_diagonal=True)
Exemplo n.º 3
0
X_train = X_train.astype('float32')
X_val = X_val.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255.0
X_val /= 255.0
X_test /= 255.0

# Calculate confusion matrix
y_val_i = y_test.flatten()
y_val_pred = model.predict(X_test)
y_val_pred_i = y_val_pred.argmax(1)
cm = np.zeros((n_classes, n_classes), dtype=np.int)
for i, j in zip(y_val_i, y_val_pred_i):
    cm[i][j] += 1

acc = get_accuracy(cm)
print("Accuracy: {:0.2f}".format(acc * 100))

# Create plot
plot_cm(cm)

# Serialize confusion matrix
with open('cm.json', 'w', encoding='utf8') as outfile:
    str_ = json.dumps(cm.tolist(),
                      indent=4,
                      sort_keys=True,
                      separators=(',', ':'),
                      ensure_ascii=False)
    outfile.write(to_unicode(str_))
Exemplo n.º 4
0
            continue

    return grouping


with open('confusion-matrix.json') as f:
    cm = json.load(f)
    cm = np.array(cm)

# Visulize
print("Score: {}".format(calculate_score(cm)))

# Load permutation
perm_file = 'permutations/hasy-58626.json'
if os.path.isfile(perm_file):
    with open(perm_file) as data_file:
        perm = json.load(data_file)
else:
    perm = list(range(len(cm)))

result = simulated_annealing(cm, perm, deterministic=True, steps=1)
print("Score: {}".format(calculate_score(result['cm'])))
print("Accuracy: {}".format(get_accuracy(result['cm'])))
print("Perm: {}".format(list(result['perm'])))
symbols = read_symbols()
grouping = get_group(result['cm'])
for group in grouping:
    if len(group) > 1:
        print("Symbols: {}".format([symbols[i] for i in group]))
plot_cm(result['cm'], zero_diagonal=True)
            continue

    return grouping


with open('confusion-matrix.json') as f:
    cm = json.load(f)
    cm = np.array(cm)

# Visulize
print("Score: {}".format(calculate_score(cm)))

# Load permutation
perm_file = 'permutations/hasy-58626.json'
if os.path.isfile(perm_file):
    with open(perm_file) as data_file:
        perm = json.load(data_file)
else:
    perm = list(range(len(cm)))

result = simulated_annealing(cm, perm, deterministic=True, steps=1)
print("Score: {}".format(calculate_score(result['cm'])))
print("Accuracy: {}".format(get_accuracy(result['cm'])))
print("Perm: {}".format(list(result['perm'])))
symbols = read_symbols()
grouping = get_group(result['cm'])
for group in grouping:
    if len(group) > 1:
        print("Symbols: {}".format([symbols[i] for i in group]))
plot_cm(result['cm'], zero_diagonal=True)