In [1]:
import numpy as np
import json
from keras.models import Model
from keras.layers import Input, Conv2D, Activation, MaxPooling2D, Dropout, Flatten, Dense
from keras import backend as K
from collections import OrderedDict


Using TensorFlow backend.

In [2]:
def format_decimal(arr, places=6):
    return [round(x * 10**places) / 10**places for x in arr]

In [3]:
DATA = OrderedDict()

pipeline 13


In [4]:
random_seed = 10013
data_in_shape = (7, 7, 1)

layers = [
    Conv2D(2, (3,3), padding='valid', strides=(1,1)),
    Activation('relu'),
    Conv2D(2, (3,3), padding='valid', strides=(1,1)),
    Activation('relu'),
    MaxPooling2D(pool_size=(2,2), strides=(1,1)),
    Dropout(0.25),
    Flatten(),
    Dense(3),
    Activation('relu'),
    Dropout(0.5),
    Dense(3),
    Activation('softmax')
]

input_layer = Input(shape=data_in_shape)
x = layers[0](input_layer)
for layer in layers[1:-1]:
    x = layer(x)
output_layer = layers[-1](x)
model = Model(inputs=input_layer, outputs=output_layer)

np.random.seed(random_seed)
data_in = 2 * np.random.random(data_in_shape) - 1

# set weights to random (use seed for reproducibility)
weights = []
for i, w in enumerate(model.get_weights()):
    np.random.seed(random_seed + i)
    weights.append(2 * np.random.random(w.shape) - 1)
model.set_weights(weights)

result = model.predict(np.array([data_in]))
data_out_shape = result[0].shape
data_in_formatted = format_decimal(data_in.ravel().tolist())
data_out_formatted = format_decimal(result[0].ravel().tolist())

DATA['pipeline_13'] = {
    'input': {'data': data_in_formatted, 'shape': data_in_shape},
    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in weights],
    'expected': {'data': data_out_formatted, 'shape': data_out_shape}
}

export for Keras.js tests


In [5]:
import os

filename = '../../test/data/pipeline/13.json'
if not os.path.exists(os.path.dirname(filename)):
    os.makedirs(os.path.dirname(filename))
with open(filename, 'w') as f:
    json.dump(DATA, f)

In [6]:
print(json.dumps(DATA))


{"pipeline_13": {"input": {"data": [0.314304, 0.087114, -0.192081, 0.687827, -0.05382, -0.449211, 0.152559, 0.729694, 0.99547, -0.487042, -0.486432, 0.714711, -0.292248, -0.199289, -0.242229, -0.439609, -0.575396, -0.053792, 0.860916, -0.065459, 0.353475, 0.680997, 0.830208, 0.469092, -0.265621, -0.687023, -0.45321, 0.530606, -0.319466, 0.898567, -0.021989, -0.726166, 0.350933, 0.471236, 0.214698, -0.218634, -0.656787, 0.487585, -0.45693, 0.095657, 0.951011, -0.969214, -0.540415, 0.42117, 0.268511, -0.931772, -0.026435, -0.416304, -0.229962], "shape": [7, 7, 1]}, "weights": [{"data": [0.314304, 0.087114, -0.192081, 0.687827, -0.05382, -0.449211, 0.152559, 0.729694, 0.99547, -0.487042, -0.486432, 0.714711, -0.292248, -0.199289, -0.242229, -0.439609, -0.575396, -0.053792], "shape": [3, 3, 1, 2]}, {"data": [0.488004, 0.911709], "shape": [2]}, {"data": [0.263717, -0.720003, 0.8268, -0.92253, 0.462425, 0.244446, -0.124898, -0.811324, 0.354873, 0.801179, 0.564291, -0.355745, 0.644611, 0.859794, -0.560491, -0.788843, -0.963575, -0.638994, 0.405345, 0.176622, -0.153501, 0.998311, 0.944831, 0.536878, 0.033269, -0.435141, -0.309305, 0.602218, 0.537614, 0.254239, 0.37362, -0.505583, -0.948316, 0.939032, 0.149339, -0.197973], "shape": [3, 3, 2, 2]}, {"data": [-0.296519, 0.572188], "shape": [2]}, {"data": [-0.983721, 0.040472, -0.976147, -0.023768, -0.481133, -0.192315, 0.766864, -0.700711, -0.549223, 0.449282, 0.713811, 0.290114, -0.96995, 0.09209, -0.529109, -0.916857, 0.914016, -0.884145, 0.24725, -0.24954, -0.752434, 0.479158, -0.887148, 0.116841], "shape": [8, 3]}, {"data": [0.524698, 0.474407, 0.471901], "shape": [3]}, {"data": [-0.040091, -0.549528, 0.347624, 0.057999, -0.900791, -0.547796, -0.152768, -0.651661, -0.474991], "shape": [3, 3]}, {"data": [-0.70434, -0.602297, -0.208904], "shape": [3]}], "expected": {"data": [0.266763, 0.295422, 0.437815], "shape": [3]}}}

In [ ]: