In [1]:
import numpy as np
from keras.models import Model
from keras.layers import Input
from keras.layers.convolutional import Cropping1D
from keras import backend as K
import json
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()

Cropping1D

[convolutional.Cropping1D.0] cropping (1,1) on 6x4 input


In [4]:
data_in_shape = (6, 4)
L = Cropping1D(cropping=(1,1))

layer_0 = Input(shape=data_in_shape)
layer_1 = L(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

# set weights to random (use seed for reproducibility)
np.random.seed(240)
data_in = 2 * np.random.random(data_in_shape) - 1
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())
print('')
print('in shape:', data_in_shape)
print('in:', data_in_formatted)
print('out shape:', data_out_shape)
print('out:', data_out_formatted)

DATA['convolutional.Cropping1D.0'] = {
    'input': {'data': data_in_formatted, 'shape': data_in_shape},
    'expected': {'data': data_out_formatted, 'shape': data_out_shape}
}


in shape: (6, 4)
in: [-0.412987, -0.139246, 0.567466, -0.512174, -0.33603, 0.585584, 0.453472, 0.942013, -0.609538, -0.894302, 0.711927, -0.126057, 0.677549, 0.676991, 0.471487, 0.752844, 0.218213, 0.37408, 0.09404, -0.786108, -0.1961, -0.031679, -0.188902, 0.846634]
out shape: (4, 4)
out: [-0.33603, 0.585584, 0.453472, 0.942013, -0.609538, -0.894302, 0.711927, -0.126057, 0.677549, 0.676991, 0.471487, 0.752844, 0.218213, 0.37408, 0.09404, -0.786108]

[convolutional.Cropping1D.1] cropping (2,3) on 6x4 input


In [5]:
data_in_shape = (6, 4)
L = Cropping1D(cropping=(2,3))

layer_0 = Input(shape=data_in_shape)
layer_1 = L(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

# set weights to random (use seed for reproducibility)
np.random.seed(241)
data_in = 2 * np.random.random(data_in_shape) - 1
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())
print('')
print('in shape:', data_in_shape)
print('in:', data_in_formatted)
print('out shape:', data_out_shape)
print('out:', data_out_formatted)

DATA['convolutional.Cropping1D.1'] = {
    'input': {'data': data_in_formatted, 'shape': data_in_shape},
    'expected': {'data': data_out_formatted, 'shape': data_out_shape}
}


in shape: (6, 4)
in: [0.229895, 0.156613, -0.66952, -0.996975, 0.45773, 0.684298, -0.999213, 0.276751, -0.484373, -0.506163, 0.353904, 0.513668, -0.981594, 0.78914, 0.603978, 0.204066, 0.532874, 0.866766, -0.544852, 0.876693, -0.523052, -0.716556, 0.875262, 0.544879]
out shape: (1, 4)
out: [-0.484373, -0.506163, 0.353904, 0.513668]

[convolutional.Cropping1D.2] cropping 2 on 6x4 input


In [6]:
data_in_shape = (6, 4)
L = Cropping1D(cropping=2)

layer_0 = Input(shape=data_in_shape)
layer_1 = L(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

# set weights to random (use seed for reproducibility)
np.random.seed(242)
data_in = 2 * np.random.random(data_in_shape) - 1
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())
print('')
print('in shape:', data_in_shape)
print('in:', data_in_formatted)
print('out shape:', data_out_shape)
print('out:', data_out_formatted)

DATA['convolutional.Cropping1D.2'] = {
    'input': {'data': data_in_formatted, 'shape': data_in_shape},
    'expected': {'data': data_out_formatted, 'shape': data_out_shape}
}


in shape: (6, 4)
in: [0.369373, -0.889592, 0.295148, 0.159458, -0.326385, -0.785026, 0.251471, 0.679684, -0.679005, -0.873982, -0.727913, 0.894448, -0.433901, -0.714725, 0.053319, -0.87505, 0.112552, 0.058438, -0.942547, 0.91998, -0.967637, 0.722331, -0.892625, 0.714282]
out shape: (2, 4)
out: [-0.679005, -0.873982, -0.727912, 0.894448, -0.433901, -0.714725, 0.053319, -0.87505]

export for Keras.js tests


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


{"convolutional.Cropping1D.0": {"input": {"shape": [6, 4], "data": [-0.412987, -0.139246, 0.567466, -0.512174, -0.33603, 0.585584, 0.453472, 0.942013, -0.609538, -0.894302, 0.711927, -0.126057, 0.677549, 0.676991, 0.471487, 0.752844, 0.218213, 0.37408, 0.09404, -0.786108, -0.1961, -0.031679, -0.188902, 0.846634]}, "expected": {"shape": [4, 4], "data": [-0.33603, 0.585584, 0.453472, 0.942013, -0.609538, -0.894302, 0.711927, -0.126057, 0.677549, 0.676991, 0.471487, 0.752844, 0.218213, 0.37408, 0.09404, -0.786108]}}, "convolutional.Cropping1D.1": {"input": {"shape": [6, 4], "data": [0.229895, 0.156613, -0.66952, -0.996975, 0.45773, 0.684298, -0.999213, 0.276751, -0.484373, -0.506163, 0.353904, 0.513668, -0.981594, 0.78914, 0.603978, 0.204066, 0.532874, 0.866766, -0.544852, 0.876693, -0.523052, -0.716556, 0.875262, 0.544879]}, "expected": {"shape": [1, 4], "data": [-0.484373, -0.506163, 0.353904, 0.513668]}}, "convolutional.Cropping1D.2": {"input": {"shape": [6, 4], "data": [0.369373, -0.889592, 0.295148, 0.159458, -0.326385, -0.785026, 0.251471, 0.679684, -0.679005, -0.873982, -0.727913, 0.894448, -0.433901, -0.714725, 0.053319, -0.87505, 0.112552, 0.058438, -0.942547, 0.91998, -0.967637, 0.722331, -0.892625, 0.714282]}, "expected": {"shape": [2, 4], "data": [-0.679005, -0.873982, -0.727912, 0.894448, -0.433901, -0.714725, 0.053319, -0.87505]}}}

In [ ]: