Ejemplo n.º 1
0
Archivo: main.py Proyecto: CNMAdmin/CNM
def cnn_train():
    train, test = trainer.read_total_module()
    #train[0] = np_transform_error_deal(train[0])
    model = trainer.create_model(64)
    print(model.summary())
    model.fit(np.array(train[0]), np.array(train[1]), epochs=10)
    model.save('net/CNN/total_CNN_ABP10.net')
Ejemplo n.º 2
0
def greedy_solver(depth=3):
    '''Runs the greedy solver.

    Results (2020/05/20):
        3.4 cubes solved / second at depth 3.
    '''
    model = trainer.create_model()
    num_starting_cubes = 20
    num_runs = 3

    num_runs_done = 0
    time_elapsed = 0
    for _ in range(num_starting_cubes):
        cube = cube_lib.get_scrambled_cube(depth)
        begin_time = time.monotonic()

        for _ in range(num_runs):
            solver = solver_lib.GreedySolver(cube, model, depth)
            num_rotations = 0
            while not solver.cube.is_solved():
                if num_rotations == depth:
                    raise Exception(
                        'Done {} rotations, cube still not solved'.format(
                            depth))
                solver.apply_next_rotation()
                num_rotations += 1

        end_time = time.monotonic()
        time_elapsed += (end_time - begin_time)
        num_runs_done += num_runs

    print('{} cubes solved per second at depth {}'.format(
        num_runs_done / time_elapsed, depth))
Ejemplo n.º 3
0
def generate_td_value_examples():
    '''Loops through training examples.

    Results (2020/05/20): 660 examples/s on my machine.
    '''
    model = trainer.create_model()
    examples = trainer.get_td_value_examples(model)
    examples_it = iter(examples)
    begin_time = time.monotonic()
    for _ in range(NUM_EXAMPLES):
        next(examples_it)
    end_time = time.monotonic()
    print('{:.1f} examples/s'.format(NUM_EXAMPLES / (end_time - begin_time)))
Ejemplo n.º 4
0
def test(isABP):
    signal_tag = 'ABP' if isABP else 'ICP'
    int_dat = os.listdir('10 ' + signal_tag + ' int')
    file_names = []
    setting = "10 fold nonaug " + signal_tag
    print(setting)
    for int_d in int_dat:
        file_names.append(int_d)
    for i in range(0, 10):
        train_ori = np.load('npy10/' + signal_tag + '/' + file_names[i] +
                            '.train.ori.npy')
        train_rep = np.load('npy10/' + signal_tag + '/' + file_names[i] +
                            '.train.rep.npy')
        train_etc = np.load('npy10/' + signal_tag + '/' + file_names[i] +
                            '.train.etc.npy')
        test_ori = np.load('npy10/' + signal_tag + '/' + file_names[i] +
                           '.test.ori.npy')
        test_rep = np.load('npy10/' + signal_tag + '/' + file_names[i] +
                           '.test.rep.npy')
        test_etc = np.load('npy10/' + signal_tag + '/' + file_names[i] +
                           '.test.etc.npy')
        model = trainer.create_model(64)
        print(model.summary())
        new_train = n_augmentation(train_rep, label_gen(train_etc[0]),
                                   train_etc[1])
        model.fit(np.array(new_train[0]),
                  np.array(new_train[1]),
                  validation_data=(np.array(test_rep),
                                   np.array(label_gen(test_etc[0]))),
                  epochs=10,
                  shuffle=True)
        model.save('net/CNN/' + str(i) + '_' + setting + '_CNN10_' +
                   signal_tag + '.net')
        pred = model.predict(np.array(test_rep))
        pen = open(
            'test/' + signal_tag + '/' + str(file_names[i]) + '_' + setting +
            '.csv', 'w')
        pen.write('idx,real,pred\n')
        for j in range(len(label_gen(test_etc[0]))):
            pen.write(
                str(j) + ',' + str(test_etc[0][j]) + ',' + str(pred[j][0]) +
                '\n')
        pen.close()
        sentence = trainer.get_pred_perfomance(label_gen(test_etc[0]), pred,
                                               test_etc[1], test_rep)
        pen = open('CNN_result.csv', 'a')
        pen.write('\n' + str(file_names[i]) + '_' + setting + ',' + sentence)
        pen.close()
Ejemplo n.º 5
0
def _model_inference_inner_loop(batch_size):
    cube = cube_lib.Cube()
    features = cube.as_numpy_array()

    features_batched = np.tile(features, [batch_size, 1, 1])
    assert (features_batched.shape == (batch_size, 20, 24))

    model = trainer.create_model()

    begin_time = time.monotonic()
    num_runs = 1000
    for _ in range(num_runs):
        model.predict(features_batched, batch_size=batch_size)
    end_time = time.monotonic()
    print('{:.1f} examples/s (batch_size = {})'.format(
        num_runs * batch_size / (end_time - begin_time), batch_size))
Ejemplo n.º 6
0
def gen_full_train_network(isABP):
    signal_tag = 'ABP' if isABP else 'ICP'
    int_dat = os.listdir('10 ' + signal_tag + ' int')
    train_x = []
    train_y = []
    train_t = []
    for int_d in int_dat:
        sig, lab, tim = ae.load_one_data('10 ' + signal_tag + ' int/' + int_d)
        train_x.extend(sig)
        train_y.extend(lab)
        train_t.extend(tim)
    train_x = ae.signal_to_img(train_x)
    train_rep = ae.autoencoding_cnn(train_x, train_x,
                                    'net/FINAL_' + signal_tag + '.net')
    new_train = n_augmentation(train_rep, label_gen(train_y), train_t)
    model = trainer.create_model(64)
    print(model.summary())
    model.fit(np.array(new_train[0]),
              np.array(new_train[1]),
              epochs=10,
              shuffle=True)
    model.save('net/CNN/FINAL_' + signal_tag + '.net')
Ejemplo n.º 7
0
def learnmain():
    fs: str = ""
    #finds the netCDF file in directory
    for root, dirs, files in os.walk(os.getcwd()):
        files.sort()
        for f in files:
            if f.endswith(".nc"):
                fs = f

    # opens the netCDF file in "read" state
    ds = netCDF4.Dataset(fs, "r")
    # gets the number of bands in the netcdf file
    input_features: int = ds.dimensions["Bands"].size
    # creates a model based on the number of bands
    model = create_model(input_features)

    model.load_weights("weights")

    #gets the array from netCDF file and rotates 180, and flips
    arrtemp = ds.variables["Data"][:]
    arrmain = np.zeros(ds.variables["Data"].shape)
    # Apply transformation to the arrays so it's right orientation
    for c in range(0, input_features):
        arrmain[c] = np.fliplr(np.rot90(arrtemp[c, :, :], 2))

    # Create new plot and insert data
    fig, ax = plt.subplots(figsize=(6, 6))

    # Uses the RGB bands, 29, 21, 16
    ep.plot_rgb(arrmain, rgb=(29, 21, 16), ax=ax, title="HyperSpectral Image")
    fig.tight_layout()

    # Function event listenter, once detected right click, will generate new graph
    def onclick(event):
        if event.xdata is not None and event.ydata is not None and event.button == 3:
            # (x,y) from click
            y = int(event.xdata)
            x = int(event.ydata)
            #init array
            arr2 = []
            #Grabs array for (x,y) from the main array
            dataPred = arrmain[:, x, y]
            #
            dataPred = np.array([dataPred])
            pred = model.predict(dataPred)
            #print(pred)
            if pred[0][0] > .5:
                print("Rooftop")
                print(int(pred[0][0] * 100))
            elif pred[0][1] > .5:
                print("Trees")
                print(int(pred[0][1] * 100))
            elif pred[0][2] > .5:
                print("Parking Lot")
                print(int(pred[0][2] * 100))
            elif pred[0][3] > .5:
                print("Shallow Water")
                print(int(pred[0][3] * 100))
            elif pred[0][4] > .5:
                print("Field")
                print(int(pred[0][4] * 100))
            elif pred[0][5] > .5:
                print("Deep Water")
                print(int(pred[0][5] * 100))
            elif pred[0][6] > .5:
                print("Road")
                print(int(pred[0][6] * 100))
            elif pred[0][7] > .5:
                print("No Data")
                print(int(pred[0][7] * 100))
            else:
                print(pred)
            #Gets data from the click location (x,y) for all bands
            for b in range(1, input_features):
                data = arrmain[b][x][y]
                arr2.append(data)

            #Creates new graph
            fig2 = go.Figure()
            xaxis = list(range(0, input_features))
            fig2.add_trace(
                go.Scatter(x=xaxis,
                           y=arr2,
                           name="(" + str(x) + "," + str(y) + ")",
                           line=dict(color='firebrick', width=2)))
            fig2.update_layout(title="Band Information for " + "(" + str(x) +
                               "," + str(y) + ")",
                               xaxis_title='Band Number',
                               yaxis_title='Value')
            #displays graph onto web browser
            #fig2.show()

    # Adds event listener for right clicks to main graph
    cid = fig.canvas.mpl_connect('button_press_event', onclick)
    plt.show()