def device(index):
    x_unlabeled = np.load("../datasets/xunlabeled.npy")
    sampling_rate = float(np.load('../outputs/sampling_rate.npy'))
    nr_exp = int(np.load('../outputs/nr_exp.npy'))
    host = 'localhost'
    port = 11111
    s = pickle.dumps(x_unlabeled[0:0 + 1])
    size_of_sample = asizeof.asizeof(s)
    np.save(
        '../outputs/' + str(nr_exp) + '_size_of_sample' +
        str(x_unlabeled.shape[0]) + '_' + str(x_unlabeled.shape[1]) + '.npy',
        np.int(size_of_sample))
    my_socket = utilities.create_socket('normal', host, port)
    buff_size = 4096
    print(multiprocessing.current_process(), "starts sending data")
    for i in range(index[0], index[1]):
        time.sleep(sampling_rate)
        x_sending_sample = x_unlabeled[i:i + 1]
        s = pickle.dumps((i, x_sending_sample))
        try:
            my_socket.send(s)
        except ConnectionResetError:
            break
        try:
            my_socket.recv(buff_size)
        except ConnectionResetError:
            break
        time.sleep(sampling_rate)

    my_socket.close()
def edge_receive():
    global buffer
    port = 11111
    host = 'localhost'
    backlog = 20
    buf_size = 4096

    listening_socket = utilities.create_socket('listen', host, port, backlog)

    while True:
        c, address = listening_socket.accept()
        print("Address connected", address)
        _thread.start_new_thread(on_new_client, (c, buf_size))
def model():
    print("thread for the model is running")
    global clf
    port = 33333
    host = 'localhost'
    backlog = 20
    buf_size = 4096
    listening_socket = utilities.create_socket('listen', host, port, backlog)
    c, address = listening_socket.accept()
    print("Address connected for model transfer", address)
    while True:
        tmp = utilities.receive_sample(c, buf_size)
        if not isinstance(tmp, int):
            clf = tmp
def edge_send(nr_exp, bandwidth=2000, latency=0):
    global buffer
    port = 22222
    host = 'localhost'
    buf_size = 1024

    my_socket = utilities.create_socket('control', host, port,
                                        [latency, bandwidth])
    myfile = "../outputs/" + str(nr_exp) + "_l_" + str(latency) + "_b_" + str(
        bandwidth) + ".txt"
    with open(myfile, "w") as f:
        f.write("len_buffer_edge\n")

    while True:
        active_send(my_socket, buf_size, myfile)
Пример #5
0
def update_model(nr_exp, budget):
    global x_unlabeled, y_unlabeled, x_labeled, y_labeled, x_test, y_test, clf, server_buffer, errorHistoryUS

    if clf is None:
        clf = BayesianRidge()
        clf.fit(x_labeled, y_labeled)
        np.save('../outputs/model_size_' + str(x_unlabeled.shape[1]) + '.npy',
                np.int32(asizeof.asizeof(pickle.dumps(clf))))
        print(np.int32(asizeof.asizeof(pickle.dumps(clf))))
    host = 'localhost'
    port = 33333
    my_socket = -1
    while my_socket == -1:
        my_socket = utilities.create_socket('normal', host, port)

    print("Connection to edge for model sharing established")
    my_socket.send(pickle.dumps(clf))
    myfile = "../outputs/" + str(nr_exp) + "_budget_" + str(budget) + ".txt"
    with open(myfile, "w") as f:
        f.write("len_buffer_server\n")
    while True:
        time.sleep(budget)
        len_buffer = len(server_buffer)
        if len_buffer > 0:
            buffer_data = server_buffer[:len_buffer]
            del server_buffer[:len_buffer]
            x = [bd[1] for bd in buffer_data]
            idx = [bd[0] for bd in buffer_data]
            x = np.array(x)
            x = x.reshape((x.shape[0], x.shape[2]))
            _, std = clf.predict(x, return_std=True)
            most_uncertain_idx = np.argmax(std)
            x_labeled = np.append(x_labeled,
                                  x[most_uncertain_idx:most_uncertain_idx + 1],
                                  axis=0)
            y_labeled = np.append(y_labeled,
                                  y_unlabeled[idx[most_uncertain_idx]])
            clf = BayesianRidge()
            clf.fit(x_labeled, y_labeled)
            p = clf.predict(x_test)
            errorHistoryUS.append(
                np.sqrt(mean_squared_error(y_test.flatten(), p.flatten())))
            with open(myfile, "a") as f:
                f.write(str(len_buffer) + '\n')
        try:
            my_socket.send(pickle.dumps(clf))
        except ConnectionResetError:
            break
def server():
    global server_buffer
    port = 22222
    host = 'localhost'
    backlog = 5
    buf_size = 4096

    listening_socket = utilities.create_socket('listen', host, port, backlog)

    accepted_socket, address = listening_socket.accept()
    print(address, 'Edge is connected!')

    while True:

        data = utilities.receive_sample(accepted_socket, buf_size)
        if not isinstance(data, int):
            server_buffer.append(data)
        try:
            accepted_socket.send('Hello edge, I received the sample.'.encode())
        except ConnectionResetError:
            break