In [1]:
import numpy as np
import json
from keras.models import Model
from keras.layers import Input
from keras.layers.convolutional import Conv2D
from keras.layers.pooling import MaxPooling2D, AveragePooling2D
from keras.layers.normalization import BatchNormalization
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 8


In [4]:
random_seed = 1008
data_in_shape = (8, 8, 2)

layers = [
    Conv2D(4, (3,3), activation='relu', padding='same', strides=(1,1), data_format='channels_last', use_bias=True),
    Conv2D(4, (3,3), activation='relu', padding='valid', strides=(1,1), data_format='channels_last', use_bias=True),
    MaxPooling2D(pool_size=(2,2), strides=(1,1), padding='same', data_format='channels_last')
]

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_08'] = {
    '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/08.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_08": {"input": {"data": [0.228005, 0.859479, -0.49018, 0.232871, -0.303968, -0.799141, 0.621228, 0.850429, 0.029476, -0.583346, 0.889636, -0.128896, 0.067108, -0.1059, -0.788773, -0.559347, 0.674802, 0.513275, -0.95495, -0.230976, -0.430566, 0.607782, -0.292593, -0.362274, -0.825576, 0.904458, 0.531651, 0.139053, -0.797761, 0.905804, -0.875903, 0.04377, -0.704592, 0.203555, -0.083031, 0.321316, 0.334565, 0.965166, 0.31912, 0.987618, 0.11275, 0.755438, 0.133156, -0.271605, -0.739053, 0.930942, 0.723852, -0.399546, 0.365907, 0.680404, 0.302211, 0.481088, -0.254831, -0.719056, -0.13153, 0.195489, -0.457555, -0.825431, -0.866045, -0.010333, -0.168299, 0.986557, -0.410139, 0.607414, 0.897455, -0.503628, -0.125592, -0.216501, 0.854905, 0.862831, 0.911679, 0.451035, -0.208763, -0.0935, -0.482974, -0.552539, -0.348091, -0.720601, -0.234177, -0.522557, -0.128204, 0.992609, -0.112888, -0.806388, 0.223532, -0.205692, -0.772152, 0.989159, -0.277926, -0.544355, -0.707034, -0.598198, 0.555265, -0.382095, -0.372491, 0.631353, 0.784895, -0.373885, -0.368449, 0.898173, 0.252309, 0.912245, -0.033641, -0.896843, 0.914818, -0.598634, -0.389127, 0.116656, -0.476051, -0.595, 0.076412, -0.998532, -0.469193, -0.993491, 0.503043, 0.698487, 0.538773, -0.763721, 0.048814, -0.996688, -0.13339, 0.032822, 0.777114, -0.102801, -0.866702, -0.544717, -0.870035, -0.645622], "shape": [8, 8, 2]}, "weights": [{"data": [0.228005, 0.859479, -0.49018, 0.232871, -0.303968, -0.799141, 0.621228, 0.850429, 0.029476, -0.583346, 0.889636, -0.128896, 0.067108, -0.1059, -0.788773, -0.559347, 0.674802, 0.513275, -0.95495, -0.230976, -0.430566, 0.607782, -0.292593, -0.362274, -0.825576, 0.904458, 0.531651, 0.139053, -0.797761, 0.905804, -0.875903, 0.04377, -0.704592, 0.203555, -0.083031, 0.321316, 0.334565, 0.965166, 0.31912, 0.987618, 0.11275, 0.755438, 0.133156, -0.271605, -0.739053, 0.930942, 0.723852, -0.399546, 0.365907, 0.680404, 0.302211, 0.481088, -0.254831, -0.719056, -0.13153, 0.195489, -0.457555, -0.825431, -0.866045, -0.010333, -0.168299, 0.986557, -0.410139, 0.607414, 0.897455, -0.503628, -0.125592, -0.216501, 0.854905, 0.862831, 0.911679, 0.451035], "shape": [3, 3, 2, 4]}, {"data": [0.229761, -0.670851, -0.398017, -0.453102], "shape": [4]}, {"data": [-0.211487, -0.648815, -0.854588, -0.616238, -0.200391, -0.163753, 0.525164, 0.04282, -0.178234, 0.074889, -0.458875, -0.133347, 0.654533, -0.456294, 0.454776, -0.799519, -0.004428, 0.160632, 0.153349, -0.585922, -0.407693, 0.794725, -0.535387, 0.408942, -0.182012, 0.741361, 0.045939, -0.156736, -0.156846, -0.357358, 0.539258, 0.948017, -0.307682, -0.715505, 0.740323, 0.616044, -0.79421, 0.478351, -0.40107, 0.597915, -0.251741, -0.56835, -0.30559, 0.943538, -0.121007, -0.5726, 0.63163, -0.4633, -0.466873, -0.269675, 0.939682, -0.088032, -0.686666, -0.763883, 0.327973, -0.116397, -0.369221, 0.023515, 0.355153, 0.071139, -0.748422, -0.178133, -0.416396, -0.96893, -0.992102, 0.872025, -0.453677, -0.696693, -0.847087, 0.968047, 0.350708, 0.123802, -0.658602, -0.017016, 0.567433, 0.69692, -0.125943, -0.93719, -0.331865, -0.819113, -0.307996, 0.12548, 0.781431, -0.837784, 0.606706, 0.184851, 0.896077, -0.706952, -0.444589, 0.806915, 0.575949, -0.599633, 0.588646, 0.162818, -0.951798, 0.227047, 0.153506, -0.817576, -0.671, -0.849184, -0.748259, 0.650901, 0.286647, -0.577941, -0.254328, -0.412536, -0.566946, -0.983986, -0.293822, -0.807716, 0.396576, -0.017533, 0.374002, -0.940263, -0.28683, -0.032966, 0.704066, -0.668463, -0.867457, 0.563842, 0.711939, 0.36657, 0.439858, -0.256388, 0.700991, 0.353122, 0.627541, -0.018557, -0.550522, 0.034842, -0.861297, 0.697198, -0.717249, -0.338883, 0.574879, 0.201778, -0.773585, -0.907219, -0.541137, -0.983263, -0.073794, -0.063249, -0.304643, -0.542468], "shape": [3, 3, 4, 4]}, {"data": [0.311362, -0.228519, 0.253024, -0.775634], "shape": [4]}], "expected": {"data": [0.0, 0.0, 3.763104, 0.0, 0.0, 0.0, 3.984484, 0.0, 0.0, 0.0, 3.984484, 0.0, 0.0, 0.642166, 5.072556, 0.0, 0.0, 0.642166, 5.072556, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 3.763104, 0.0, 0.0, 0.116547, 3.984484, 0.0, 0.0, 1.627302, 3.984484, 0.0, 0.0, 1.627302, 3.953471, 0.781317, 0.0, 1.275233, 3.953471, 0.781317, 0.0, 1.275233, 0.0, 0.0, 3.186446, 0.511314, 3.056352, 0.0, 0.0, 0.511314, 3.056352, 0.0, 0.0, 1.627302, 2.379764, 0.0, 0.0, 1.627302, 2.57343, 0.781317, 0.0, 1.275233, 2.57343, 0.781317, 0.0, 1.275233, 0.0, 0.0, 3.186446, 4.710953, 3.056352, 0.0, 0.0, 0.511314, 3.056352, 0.0, 0.0, 0.0, 6.52666, 0.0, 0.0, 0.336272, 6.52666, 0.0, 0.0, 0.336272, 1.224865, 0.0, 0.0, 0.0, 1.224865, 0.0, 0.0, 4.710953, 1.770351, 0.0, 0.0, 0.354758, 1.770351, 0.0, 0.0, 0.354758, 6.52666, 0.0, 0.0, 0.0, 6.52666, 0.0, 0.0, 0.0, 1.224865, 0.0, 0.0, 0.0, 1.224865, 0.0, 0.0, 0.0, 0.818353, 0.0, 0.0, 0.354758, 0.818353, 0.0, 0.0, 0.354758, 0.262927, 0.0, 0.0, 0.0, 0.262927, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], "shape": [6, 6, 4]}}}

In [ ]: