Example #1
0
def plot_wrong_classification(x, y_true, y_pred, filename):
    samples = []
    fig_size = (10, 10)
    for i in range(0, x.shape[0]):
        if y_true[i] != y_pred[i]:
            samples.append((y_true[i], y_pred[i], i))

    def plot_sample(x, axis):
        img = x.reshape(x.shape[0], x.shape[1])
        axis.imshow(img, cmap='gray')

    fig = plt.figure(figsize=fig_size)

    for i in range(len(samples)):
        y_t, y_p, index = samples[i]
        ax = fig.add_subplot(*fig_size, i + 1, xticks=[], yticks=[])
        title = map_int_to_letter(y_t) + " -> " + map_int_to_letter(y_p) 
        ax.title.set_text(title)
        ax.title.set_fontsize(10)
        plot_sample(x[index], ax)

    fig.tight_layout()
    plt.savefig(filename)
Example #2
0
        samples.append((y_true[i], y_pred[i], i))


def plot_sample(x, axis):
    img = x.reshape(30, 30)
    axis.imshow(img, cmap='gray')


fig = plt.figure(figsize=(10, 6))

print()

for i in range(len(samples)):
    y_t, y_p, index = samples[i]
    ax = fig.add_subplot(10, 6, i + 1, xticks=[], yticks=[])
    title = cro_mapper.map_int_to_letter(
        y_t) + " -> " + cro_mapper.map_int_to_letter(y_p)
    ax.title.set_text(title)
    ax.title.set_fontsize(10)
    plot_sample(x_test[index], ax)

fig.tight_layout()
# fig.subplots_adjust(top=0.88)
plt.show()

# def twoway_confusion_matrix(cm, i):
#     tp = cm[i, i]
#     fn = np.sum(cm[i,:]) - tp
#     fp = np.sum(cm[:,i]) - tp
#     tn = np.sum(cm) - fp - fn - tp
#     return np.matrix([[tp, fp], [fn, tn]]).astype(float)
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("weights-improvement-645-0.94.hdf5")
print("Loaded model from disk")

image = misc.imread("/Users/filipgulan/Desktop/test.png", mode='F')
print("Image loaded...")
X = []
X.append(image)
X = np.asarray(X)
X = X.reshape(X.shape[0], img_rows, img_cols, 1)
X = X.astype('float32')
X /= 255

prediction = loaded_model.predict(X)[0]

print("Predictions:")
prediction_dict = dict(enumerate(prediction))
prediction_sorted = sorted(prediction_dict.items(),
                           key=lambda x: x[1],
                           reverse=True)

counter = 0
for key, value in prediction_sorted:
    print(cro_mapper.map_int_to_letter(key), value)
    counter += 1
    if counter == 5:
        break

index = np.argmax(prediction)
print("Recognized letter:", cro_mapper.map_int_to_letter(index))
Example #4
0
            if tw[0, 0] != 0:
                precision += tw[0, 0] / (tw[0, 0] + tw[1, 0])
        return precision / size
    else:
        conf = np.zeros((2, 2)).astype(float)
        for tw in tw_ms:
            conf += tw
        conf /= size
        return conf[0, 0] / (conf[0, 0] + conf[1, 0])


def twoway_confusion_matrix(cm, i):
    tp = cm[i, i]
    fn = np.sum(cm[i, :]) - tp
    fp = np.sum(cm[:, i]) - tp
    tn = np.sum(cm) - fp - fn - tp
    return np.matrix([[tp, fp], [fn, tn]]).astype(float)


test_confusion = confusion_matrix(y_true, y_pred)
for i in range(test_confusion.shape[0]):
    print("Matrica zabune za klasu", cro_mapper.map_int_to_letter(i))
    tw = twoway_confusion_matrix(test_confusion, i)
    print("Preciznost", precision_c(tw, averaging="macro"))
    print("Odziv", recall_c(tw, averaging="macro"))
    print("Tocnost", accuracy_c(tw, averaging="macro"))
    print(tw)
np.set_printoptions(threshold=np.inf)

# with open("confmatrix_10000.txt", 'w') as f:
#     f.write(np.array2string(confusion_matrix(y_true, y_pred), separator=', '))
Example #5
0
def save_confusion_matrix_csv(confusion_matrix, filename):
    num_classes = confusion_matrix.shape[0]
    header = [map_int_to_letter(letter_int) for letter_int in range(num_classes)]
    header = ",".join(header)
    np.savetxt(filename, confusion_matrix.astype(int), fmt='%i', delimiter=",")