Ejemplo n.º 1
0
def compress_activations(activations, out, compression_method,
                         compression_latch, thresholding_method):
    #separate the activations into spatial subdivisions
    activation_divisions = []

    if (compression_method["type"] == "max"):
        activations = activations.reshape((activations.shape[0], -1))
        activation_divisions = [np.max(activations, axis=1)]

    elif (compression_method["type"] == "even"):
        step = activations.shape[1] / compression_method["value"]

        for n in range(0, activations.shape[1], step):
            for m in range(0, activations.shape[2], step):
                restruct = activations[:, n:n + step, m:m + step].reshape(
                    (activations.shape[0], -1))
                activation_divisions.append(np.max(restruct, axis=1))

    elif (compression_method["type"] == "peaks"):
        #print("start_gen")
        activation_divisions = peak_finding.peak_finding_fast(
            activations, n=compression_method["value"])
        #print("stop_gen")
    #print("activation_divisions: ", len(activation_divisions))
    #perform thresholding on each of the spatial subdivisions
    thresholded_activations = []
    for i in range(len(activation_divisions)):
        thresholded_activations.append([])

    activation_latch = CountDownLatch(len(activation_divisions))
    list_of_threads = []
    for i in range(len(activation_divisions)):
        #print("start")
        if (len(activation_divisions[i]) > 0):
            t = threading.Thread(target=threshold_activations,
                                 args=(
                                     activation_divisions[i],
                                     thresholded_activations[i],
                                     activation_latch,
                                     thresholding_method,
                                 ))
            # threshold_activations(activation_divisions[i], thresholded_activations[i], None, thresholding_method)
            list_of_threads.append(t)
            t.start()
        else:
            #print("err: ", i, activation_divisions[i])
            activation_latch.count_down()

    # wait for all threads to finish
    activation_latch. await ()
    #print("finished")
    #for i in activation_divisions:
    #	print(i)
    out.append(np.array(thresholded_activations).squeeze())
    #print("out_shape:", np.array(thresholded_activations).squeeze().shape)
    compression_latch.count_down()
Ejemplo n.º 2
0
def compress_activations(activations,
                         out,
                         compression_method,
                         compression_latch,
                         thresholding_method,
                         index=None,
                         max_val=None,
                         min_val=None):
    #separate the activations into spatial subdivisions
    activation_divisions = []

    if (compression_method["type"] == "max"):
        activations = activations.reshape((activations.shape[0], -1))
        activation_divisions = [np.max(activations, axis=1)]

    elif (compression_method["type"] == "even"):
        step = activations.shape[1] / compression_method["value"]

        for n in range(0, activations.shape[1], step):
            for m in range(0, activations.shape[2], step):
                restruct = activations[:, n:n + step, m:m + step].reshape(
                    (activations.shape[0], -1))
                activation_divisions.append(np.max(restruct, axis=1))

    elif (compression_method["type"] == "peaks"):
        activation_divisions = peak_finding.peak_finding_fast(
            activations, n=compression_method["value"])

    #perform thresholding on each of the spatial subdivisions
    thresholded_activations = []
    if (thresholding_method != "none"):

        for i in range(len(activation_divisions)):
            thresholded_activations.append([])

        activation_latch = CountDownLatch(len(activation_divisions))
        list_of_threads = []
        for i in range(len(activation_divisions)):
            if (len(activation_divisions[i]) > 0):
                t = None
                if (thresholding_method == "min_max_norm"):
                    assert max_val != None, "max_val variable must be set"
                    assert min_val != None, "max_val variable must be set"
                    t = threading.Thread(target=threshold_activations,
                                         args=(
                                             activation_divisions[i],
                                             thresholded_activations[i],
                                             activation_latch,
                                             thresholding_method,
                                             index + i,
                                             max_val,
                                             min_val,
                                         ))
                else:
                    t = threading.Thread(target=threshold_activations,
                                         args=(
                                             activation_divisions[i],
                                             thresholded_activations[i],
                                             activation_latch,
                                             thresholding_method,
                                         ))
                list_of_threads.append(t)
                t.start()
            else:
                activation_latch.count_down()

        # wait for all threads to finish
        activation_latch. await ()

    else:
        thresholded_activations = activation_divisions

    out.append(np.array(thresholded_activations).squeeze())
    compression_latch.count_down()