In [1]:
import numpy as np
import json
from keras.models import Model
from keras.layers import Input, SeparableConv2D, Activation, BatchNormalization
from keras import backend as K
from collections import OrderedDict


Using TensorFlow backend.
/home/leon/miniconda3/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6
  return f(*args, **kwds)

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

In [3]:
DATA = OrderedDict()

pipeline 14


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

layers = [
    SeparableConv2D(2, (3,3), padding='valid', strides=(1,1)),
    SeparableConv2D(2, (3,3), padding='valid', strides=(1,1))
]

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_14'] = {
    '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/14.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_14": {"input": {"data": [0.488004, 0.911709, 0.385817, 0.735948, 0.007869, -0.35278, 0.875938, -0.016655, 0.5483, -0.074508, 0.164805, 0.364344, 0.113199, -0.963444, 0.249173, 0.518158, -0.131317, 0.700476, 0.406088, -0.846826, 0.29394, 0.082756, -0.416403, 0.499608, -0.800435, 0.920522, 0.310248, -0.303414, -0.908233, 0.203626, -0.241686, -0.086586, -0.594866, -0.396726, -0.326546, -0.303103, 0.464722, -0.565768, -0.125654, 0.077641, 0.633304, 0.211824, -0.108683, -0.613915, 0.907336, -0.708153, 0.447691, 0.77094, 0.796293], "shape": [7, 7, 1]}, "weights": [{"data": [0.488004, 0.911709, 0.385817, 0.735948, 0.007869, -0.35278, 0.875938, -0.016655, 0.5483], "shape": [3, 3, 1, 1]}, {"data": [0.263717, -0.720003], "shape": [1, 1, 1, 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], "shape": [3, 3, 2, 1]}, {"data": [0.524698, 0.474407, 0.471901, 0.222861], "shape": [1, 1, 2, 2]}, {"data": [-0.040091, -0.549528], "shape": [2]}], "expected": {"data": [0.055576, -0.469849, -1.299474, -1.284736, -0.554166, -0.719936, -0.517287, -0.698947, 0.015128, -0.407083, 0.259625, -0.344306, -0.861422, -0.872735, -0.40709, -0.565243, -0.880509, -0.777895], "shape": [3, 3, 2]}}}

In [ ]: