Exemplo n.º 1
0
    if len(checkpoints) > 0:

        checkpoints = natsorted(checkpoints)
        assert len(checkpoints) != 0, 'No checkpoints found.'
        checkpoint_file = checkpoints[-1]
        print('Loading [{}]'.format(checkpoint_file))
        model = load_model(checkpoint_file)

        model.compile(optimizer='adam',
                      loss='categorical_crossentropy',
                      metrics=['accuracy'])

        print(model.summary())

        x_train, y_train, x_test, y_test = get_mnist_data()

        # checking that the accuracy is the same as before 99% at the first epoch.
        test_loss, test_acc = model.evaluate(x_test,
                                             y_test,
                                             verbose=1,
                                             batch_size=128)
        print('')
        assert test_acc > 0.98

        get_activations(model, x_test[0:1],
                        print_shape_only=True)  # with just one sample.

        get_activations(model, x_test[0:200],
                        print_shape_only=True)  # with 200 samples.
Exemplo n.º 2
0
import keras
from keras.layers import Dense
from keras.layers.recurrent import LSTM
from keras.models import Sequential

import utils
from data import get_mnist_data, num_classes
from keract import get_activations

if __name__ == '__main__':
    x_train, _, _, _ = get_mnist_data()

    # (60000, 28, 28, 1) to ((60000, 28, 28)
    # LSTM has (batch, time_steps, input_dim)
    x_train = x_train.squeeze()

    model = Sequential()
    model.add(LSTM(16, input_shape=(28, 28)))
    model.add(Dense(num_classes, activation='softmax'))

    model.compile(loss=keras.losses.categorical_crossentropy,
                  optimizer=keras.optimizers.Adadelta(),
                  metrics=['accuracy'])

    utils.print_names_and_shapes(get_activations(model, x_train[:128]))