Example #1
0
def train(epochs, prev_data):
    if prev_data is None:
        data_saves = sorted(glob.glob('fine_tune_data/data*.npz'))
    else:
        data_saves = sorted(glob.glob('fine_tune_data/data*.npz'))[-prev_data:]
    x = list()
    y = list()
    for data_save in data_saves:
        data = np.load(data_save)
        x.append(data['x'])
        y.append(data['y'])
    x = np.concatenate(x, axis=0)
    y = np.concatenate(y, axis=0)

    single_frame_encoder = model.single_frame_model()
    multi_frame_encoder = model.multi_frame_model(num_frames=None)
    full_model = model.full_model(single_frame_encoder,
                                  multi_frame_encoder,
                                  num_frames=None)
    training_run = 'run12'
    single_frame_encoder_loc = os.path.join('./inference', training_run,
                                            'single_frame.h5')
    multi_frame_encoder_loc = os.path.join('./inference', training_run,
                                           'multi_frame.h5')
    single_frame_encoder.load_weights(single_frame_encoder_loc, by_name=True)
    multi_frame_encoder.load_weights(multi_frame_encoder_loc, by_name=True)

    for layer in single_frame_encoder.layers:
        if layer.name in {
                'separable_conv2d_15', 'separable_conv2d_16',
                'separable_conv2d_18'
        }:
            layer.trainable = True
        else:
            layer.trainable = False
    for layer in multi_frame_encoder.layers:
        if layer.name in {'dense_1'}:
            layer.trainable = True
        else:
            layer.trainable = False

    full_model.compile(optimizer=SGD(learning_rate=1e-3,
                                     momentum=0.9,
                                     nesterov=True),
                       loss=temporal_crossentropy,
                       metrics=[temporal_accuracy, temporal_top_k_accuracy])
    full_model.fit(x, y, batch_size=BATCH_SIZE, epochs=epochs, shuffle=True)

    single_frame_encoder.save(single_frame_encoder_loc)
    multi_frame_encoder.save(multi_frame_encoder_loc)
Example #2
0
def evaluate():
    data_saves = sorted(glob.glob('fine_tune_data/data*.npz'))
    x = list()
    y = list()
    for data_save in data_saves:
        data = np.load(data_save)
        x.append(data['x'])
        y.append(data['y'])
    x = np.concatenate(x, axis=0)
    y = np.concatenate(y, axis=0)
    total_samples = y.sum(axis=0).astype(np.int)
    for gesture, num_samples in zip(GESTURES, total_samples):
        print('{} : {}'.format(gesture, num_samples))

    single_frame_encoder = model.single_frame_model()
    multi_frame_encoder = model.multi_frame_model(num_frames=None)
    full_model = model.full_model(single_frame_encoder,
                                  multi_frame_encoder,
                                  num_frames=None)
    training_run = 'run12'
    single_frame_encoder_loc = os.path.join('./inference', training_run,
                                            'single_frame.h5')
    multi_frame_encoder_loc = os.path.join('./inference', training_run,
                                           'multi_frame.h5')
    single_frame_encoder.load_weights(single_frame_encoder_loc, by_name=True)
    multi_frame_encoder.load_weights(multi_frame_encoder_loc, by_name=True)

    y_pred = full_model.predict(x)
    y_pred = np.average(y_pred, axis=1)
    conf_matrix = tf.math.confusion_matrix(y.argmax(axis=1),
                                           y_pred.argmax(axis=1),
                                           num_classes=27).numpy()
    conf_matrix = conf_matrix.astype('float') / conf_matrix.sum(
        axis=1)[:, np.newaxis]

    plt.figure(figsize=(14, 10))
    sns.heatmap(conf_matrix, annot=True)
    plt.title('Confusion Matrix')
    plt.yticks(np.arange(len(GESTURES)), GESTURES, rotation='horizontal')
    plt.xticks(np.arange(len(GESTURES)), GESTURES, rotation='vertical')
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    plt.tight_layout()
    plt.show()
Example #3
0
def plot_model(this, C_mix, C_dyn, cascade=False):
    v = this['viscosity']
    c = this['conductivity']

    times, heights, mix = filter_trajectory(this['time'], this['height2'],
                                            this['mixed_mass'],
                                            this['extent_mesh'][2])
    mix = 2 * (64 - mix)
    #times = this['time']; heights = this['height2']

    if len(times) < len(this['time']):
        print("TRUNC: {} {} stopped at {}".format(v, c, times[-1]))

    if heights.size < 4:
        return

    L = np.sqrt(2) / this["kmin"]
    Atwood = this['atwood'] * this['g']
    y0 = [this["amp0"] / this["kmin"], 0.]

    Gr = Atwood * L**3 / (v * v)
    Ra = Gr * v / c
    Sc = v / c

    gamma = np.sqrt(Atwood * 2. * np.pi / L)

    offset = this['delta']**2. / c

    h_spline = UnivariateSpline(times, heights, k=3, s=0.00000001)
    v_spline = h_spline.derivative()
    m_spline = UnivariateSpline(times, mix, k=3, s=0.00000001)

    #T, H, V = dyn_model(exp_dyn(C_dyn), Atwood, v, L, m_spline, y0, times)

    T, HB, VB, MB = full_model(exp_dyn(C_dyn), exp_mix(C_mix), Atwood, v, L, c,
                               this['delta'], y0, times)

    dyn_error, mix_error = both_error(exp_dyn(C_dyn), exp_mix(C_mix), Atwood,
                                      v, L, c, this['delta'], y0, times,
                                      heights, mix)

    fig, axs = plt.subplots(3, 1, figsize=(8, 12), sharex=True)
    fig.subplots_adjust(hspace=0)
    axs[0].plot(times * gamma, heights / L, label="Simulation")
    axs[0].plot(times * gamma, HB / L, label="Model")
    axs[0].set_ylabel("Bubble Height ($h / \\lambda$)")
    axs[0].set_ylim(0.0, 1.2 * np.max(heights) / L)
    axs[0].legend(loc=2)
    axs[0].grid()

    axs[1].plot(times * gamma,
                v_spline(times) / np.sqrt(Atwood * L),
                label="Simulation")
    axs[1].plot(times * gamma, VB / np.sqrt(Atwood * L), label="Model")
    #axs[1].legend()
    axs[1].axhline(1. / np.sqrt(np.pi), color='black', linestyle='dashed')
    #axs[1].axhline(L * np.sqrt(Atwood * L) / (results[v,c]['C_dyn'][1] * v))
    axs[1].set_ylim(0.0, 1.2 * np.max(v_spline(times)) / np.sqrt(Atwood * L))
    axs[1].set_ylabel("Froude number")
    axs[1].grid()

    axs[2].plot(times * gamma, mix / (2 * L * L * L), label="Simulation")
    axs[2].plot(times * gamma, MB / (2 * L * L * L), label="Model")
    axs[2].set_ylim(0.0, 1.2 * np.max(mix / (2 * L * L * L)))
    axs[2].set_ylabel("Mixing height ($M / \lambda^3$) ")
    axs[2].set_xlabel("Time ($\\gamma_0 T$)")
    axs[2].grid()
    #axs[2].legend()

    if title:
        axs[1].set_title(r"Fit of $\nu=${} and D={}".format(v, c))
    plt.savefig('H-{:d}-{:d}.{}'.format(int(v * 10000), int(c * 10000),
                                        img_format))
    plt.close()

    res = results[v, c]
    #print("V={}, D={}, C=[{:8.3f}], Err={err:8.3f}".format(v, c, *(C_mix), err=res['E_mix']))
    #print("V={}, D={}, T={}, Err={}\n >> C=".format(v, c, data_table[v,c,'time'][-1], res['E_',0]) + str(res['coef',:].values()))
    if np.max(heights) < np.sqrt(2):
        return
    print(
        "LW: V={}, D={}, C_dyn=[{:6.3f}, {:8.3f}, {:6.3f}, {:6.3f}], C_mix=[{C5:6.3f}]: [{derr:6.3f}, {merr:6.3f}]"
        .format(v,
                c,
                *(C_dyn),
                C5=C_mix[0],
                derr=dyn_error / np.max(heights),
                merr=mix_error / np.max(mix)))
    #print(" >> S=" + str(res['std',:].values()))
    print(
        "T: {Grashof} {Rayleigh} {Schmidt} {C1:6.3f} {C2:8.3f} {C3:6.3f} {C7:6.3f} {C5:6.3f} {E_dyn} {E_mix}"
        .format(Grashof=Gr,
                Rayleigh=Ra,
                Schmidt=Sc,
                C1=C_dyn[0],
                C2=C_dyn[1],
                C3=C_dyn[2],
                C7=C_dyn[3],
                C5=C_mix[0],
                E_dyn=dyn_error / np.max(heights),
                E_mix=mix_error / np.max(mix)))

    if not cascade:
        return

    fig, axs = plt.subplots(1, 1)

    axs.plot(heights / L,
             v_spline(times) / np.sqrt(Atwood * L),
             label="Simulation",
             color='black')
    axs.set_ylim(0.0, 2 * np.max(v_spline(times)) / np.sqrt(Atwood * L))
    #axs.set_xlim(0.0, np.max(heights)/L)
    axs.set_xlabel("Bubble Height ($h/\lambda$)")
    axs.set_ylabel("Froude number")

    C_mix_tmp = [0., 0.]
    C_dyn_tmp = [0., 0., 0., 0.]
    T, HB, VB, MB = full_model(exp_dyn(C_dyn_tmp), exp_mix(C_mix_tmp), Atwood,
                               v, L, c, this['delta'], y0, times)
    axs.plot(HB / L, VB / np.sqrt(Atwood * L), label='$C_4,C_6,C_8 > 0$')

    C_mix_tmp = [0., 0.]
    C_dyn_tmp = [0., 0., C_dyn[2], 0.]
    T, HB, VB, MB = full_model(exp_dyn(C_dyn_tmp), exp_mix(C_mix_tmp), Atwood,
                               v, L, c, this['delta'], y0, times)
    axs.plot(HB / L, VB / np.sqrt(Atwood * L), label='$C_3 > 0$')

    C_mix_tmp = [0., 0.]
    C_dyn_tmp = [C_dyn[0], 0., C_dyn[2], 0.]
    T, HB, VB, MB = full_model(exp_dyn(C_dyn_tmp), exp_mix(C_mix_tmp), Atwood,
                               v, L, c, this['delta'], y0, times)
    axs.plot(HB / L, VB / np.sqrt(Atwood * L), label='$C_1 > 0$')

    C_mix_tmp = [0., 0.]
    C_dyn_tmp = [C_dyn[0], C_dyn[1], C_dyn[2], 0.]
    T, HB, VB, MB = full_model(exp_dyn(C_dyn_tmp), exp_mix(C_mix_tmp), Atwood,
                               v, L, c, this['delta'], y0, times)
    axs.plot(HB / L, VB / np.sqrt(Atwood * L), label='$C_2 > 0$')

    C_mix_tmp = [C_mix[0], 0.]
    C_dyn_tmp = [C_dyn[0], C_dyn[1], C_dyn[2], C_dyn[3]]
    T, HB, VB, MB = full_model(exp_dyn(C_dyn_tmp), exp_mix(C_mix_tmp), Atwood,
                               v, L, c, this['delta'], y0, times)
    axs.plot(HB / L, VB / np.sqrt(Atwood * L), label='$C_5,C_7 > 0$')

    axs.axvline(0.05, color='black', linestyle='dashed')
    axs.axvline(np.pi * Gr / (128.**2.) / 4.,
                color='black',
                linestyle='dashed')
    axs.axvline(Ra / (128. * 64) / 2., color='black', linestyle='dashed')
    #axs.axvline(1.5, color='black', linestyle='dashed')

    axs.grid()
    axs.set_xlim(0.0, 2)
    if v < 0.0008:
        axs.legend(loc=2, ncol=1)
    else:
        axs.legend(loc=4, ncol=2)
    plt.savefig('Cascade-short-{:d}-{:d}.{}'.format(int(v * 10000),
                                                    int(c * 10000),
                                                    img_format))
    axs.set_xlim(0.0, np.max(heights) / L)
    axs.legend(loc=8, ncol=2)
    plt.savefig('Cascade-{:d}-{:d}.{}'.format(int(v * 10000), int(c * 10000),
                                              img_format))
    plt.close()

    return
Example #4
0
                                      min_lr=1e-6),
    keras.callbacks.TensorBoard(log_dir='./logs',
                                histogram_freq=0,
                                batch_size=32,
                                write_graph=True,
                                write_grads=False,
                                write_images=False,
                                embeddings_freq=0,
                                embeddings_layer_names=None,
                                embeddings_metadata=None,
                                embeddings_data=None,
                                update_freq='epoch')
    #NotifyCB
]

model = full_model(period)
model.compile(optimizer='adam', loss='mse')
model.fit(x_train,
          y_train,
          batch_size=16,
          validation_data=(x_test, y_test),
          epochs=100,
          verbose=1,
          callbacks=callbacks)
model.load_weights("Resnet_50_100.hdf5")
y_pred = []
test = x_test[0]
print(test.shape)
# for i in range(len(x_test)):
#     test_new=np.reshape(test,(1,period))
#     val=model.predict(test_new, verbose=0)
Example #5
0
    except Exception as e:
        print(f'Unable to save: {e}')
    else:
        print('Saved tf lite')


if __name__ == '__main__':
    tf.keras.backend.set_learning_phase(0)
    training_run = sys.argv[1]
    model_loc = sys.argv[2]

    os.makedirs(os.path.join('./inference', training_run), exist_ok=True)

    single_frame_encoder = model.single_frame_model()
    multi_frame_encoder = model.multi_frame_model(num_frames=12)
    full_model = model.full_model(single_frame_encoder, multi_frame_encoder, num_frames=12)
    full_model.load_weights(model_loc, by_name=True)

    print('Loaded model')
    single_frame_encoder.summary()
    multi_frame_encoder.summary()

    if 'tflite' in sys.argv:
        convert_to_tflite()
    if 'coreml' in sys.argv:
        convert_to_coreml()
    else:
        single_frame_encoder.save(os.path.join('./inference', training_run, 'single_frame.h5'))
        multi_frame_encoder.save(os.path.join('./inference', training_run, 'multi_frame.h5'))
        single_frame_encoder.save(os.path.join('./inference', training_run, 'single_frame.tf'))
        multi_frame_encoder.save(os.path.join('./inference', training_run, 'multi_frame.tf'))
Example #6
0
def main(model_loc):
    fig, ax = plt.subplots()
    ax.set_ylim(0, 1)
    ax.figure.set_size_inches(10, 10)
    bars = ax.bar(data.labels, np.zeros(NUM_CLASSES, dtype=np.float32))
    plt.xticks(rotation=90)
    plt.tight_layout()

    num_frames = 7
    frame_time = 1 / MIN_FPS * 1000

    single_frame_model = model.single_frame_model()
    multi_frame_model = model.multi_frame_model(num_frames=num_frames)
    full_model = model.full_model(single_frame_model,
                                  multi_frame_model,
                                  num_frames=num_frames + 1)
    full_model.load_weights(model_loc)

    cap = cv2.VideoCapture(0)

    image_size = (IMAGE_WIDTH, IMAGE_HEIGHT)
    prev = np.zeros((4 * 6 * 2048), dtype=np.float32)
    model_input = np.zeros((1, num_frames, 4 * 6 * 2048), dtype=np.float32)

    def animate(i):
        start = time.time()
        ret, frame = cap.read()

        if not ret:
            print("Couldn't read input")
            return

        frame = cv2.resize(frame, image_size)
        frame = frame.astype(np.float32) / 255.0

        frame_encoded = single_frame_model.predict(frame[None, ...])[0]
        frame_encoded = frame_encoded.reshape(4 * 6 * 2048)
        frame_diff = frame_encoded - prev
        prev[:] = frame_encoded

        model_input[0, :-1] = model_input[0, 1:]
        model_input[0, -1] = frame_diff

        pred = multi_frame_model.predict(model_input)[0]

        pred = np.max(pred, axis=0)
        pred = softmax(pred)

        for bar, p in zip(bars, pred):
            bar.set_height(p)

        # Display the resulting frame
        cv2.imshow('frame', frame)
        if cv2.waitKey(max(1, int((time.time() - start) * 1000 -
                                  frame_time))) & 0xFF == ord('q'):
            cap.release()
            cv2.destroyAllWindows()
            exit(0)

        if pred.max() > 0.5:
            predictions = pred.argsort()
            print(data.labels[predictions[-1]])
            print(data.labels[predictions[-2]])
        return bars

    animation.FuncAnimation(fig, animate, frames=None, interval=1, blit=True)
    plt.show()