Beispiel #1
0
def thickness_sim(model, data, labels, ref_thickness):
    opt_r = 0
    diff = 2
    current_acc = 0
    current_tau = 0
    acc = []
    tau = []
    r = 1
    curr_data = data
    while abs(ref_thickness - current_tau) > diff:

        downscaled = np.array(scale_down(curr_data))
        current_acc = test_model(model, downscaled, labels)
        current_tau = utils.calc_linewidth(curr_data)
        tau.append(current_tau)
        acc.append(current_acc)
        print("{Current acc: %s, Current tau: %s}" %
              (current_acc, current_tau))
        r = int(np.sign(ref_thickness - current_tau))

        if abs(ref_thickness - current_tau) > diff:
            opt_r += r
            curr_data = change_thickness(curr_data, r)

    return opt_r, acc, tau, curr_data
Beispiel #2
0
def thickness_sim(model, data, labels, ref_thickness):
    opt_r = 0
    epsilon = 2
    current_acc = test_out_digits(model, data, labels)
    current_tau = utils.calc_linewidth(data)
    metrics = [current_acc]
    tau = [current_tau]
    curr_data = data
    r = int(np.sign(ref_thickness - current_tau))
    new_r = r
    while abs(ref_thickness - current_tau) > epsilon:
        opt_r += r
        pre_acc = current_acc
        new_data = change_thickness(curr_data, r)
        current_acc = test_out_digits(model, new_data, labels)
        current_tau = utils.calc_linewidth(new_data)
        metrics.append(current_acc)
        tau.append(current_tau)
        curr_data = new_data
        new_r = int(np.sign(ref_thickness - current_tau))
        print(current_acc)
    return opt_r, metrics, tau, curr_data
def normalize_digit_thickness(digits):
    global pwa
    tau = utils.calc_linewidth(digits)

    datapoints = digits.shape[0]
    pb = ProgressBar(total=datapoints,
                     prefix="processing digits:",
                     length=20,
                     fill="=",
                     zfill="_")

    pwa.reset(tau, pb)
    print("Normalizing %s digits to tau = %s" % (datapoints, tau))
    with Pool(6) as p:
        d_normalized = p.map(wrapper_pwa_linewidth, digits)
    return np.array(d_normalized)
def change_thickness(data, ref_thickness):
    print("Adjusting the thicknesses to %s" % ref_thickness)
    diff = 0.5
    curr_data = data
    while abs(ref_thickness - tau) > diff:

        tau = utils.calc_linewidth(curr_data)
        print("current linethickness : %s" % tau)
        dt = ref_thickness - tau
        r = int(round(dt))
        r = r if r != 0 else int(
            np.sign(dt))  # Ensures that r is at least 1 or -1
        if abs(ref_thickness - tau) > diff:
            print("Adjusting Linewidth with r = %s" % r)
            curr_data = change_thickness_helper(curr_data, r)
    return curr_data
Beispiel #5
0
        },
        {
            "type" : "Dense",
            "units" : 10,
            "activation" : "softmax"
        }
    ]
}
"""

def resize_helper(img):
    sides = 280
    return cv2.resize(img, (sides,sides))

xtrain, ytrain, xtest, ytest = utils.load_mnist()
chunksize = 1000
mnist_total_size = xtest.shape[0]
partitions = int(mnist_total_size/chunksize)
taus = []

for i in range(partitions):
    print("Starting Loop")
    with Pool(6) as p:
        mnist_batch = xtest[i*chunksize:(i+1)*chunksize]
        mnist_batch_big = np.array(p.map(resize_helper, mnist_batch))

    taus.append(utils.calc_linewidth(mnist_batch_big))
    print("Calculated Line width of %s Digits of %s" % ((i+1) * chunksize, mnist_total_size))

print(np.mean(taus))