Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions onnx2kerastl/reshape_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def optimize_constant_array_for_serialization(input_0: tf.Tensor, params, indice
input_0 = first_row
return input_0, indices


def convert_gather(node, params, layers, lambda_func, node_name, keras_name):
"""
Convert gather.
Expand Down Expand Up @@ -175,14 +176,26 @@ def convert_gather(node, params, layers, lambda_func, node_name, keras_name):
else:
if tf.is_tensor(indices) and indices.dtype not in [tf.int16, tf.int32, tf.int64]:
indices = tf_cast(indices, tf.int32, tf_name=f"{params['cleaned_name']}_gather_cast_indices")
if isinstance(indices, list):
indices = np.array(indices)

if type(indices) == int:
out_type = tf.int32
else:
out_type = indices.dtype
if isinstance(indices, list):
out_type = np.array(indices).dtype
else:
out_type = indices.dtype

dim_len = tf_shape(input_0, out_type=out_type,
tf_name=f"{params['cleaned_name']}_gather_input_shape")[axis] # support None
if isinstance(indices, list):
for i in range(len(indices)):
try:
if indices[i] < 0:
indices[i] = int(indices[i]) + dim_len
except TypeError:
pass


if isinstance(indices, (int, np.integer)) and indices < 0:
indices += dim_len
if tf.is_tensor(indices):
Expand Down Expand Up @@ -636,7 +649,7 @@ def convert_resize(node, params, layers, lambda_func, node_name, keras_name):
resize_size = tf_stack(tf_resize_shapes,
axis=0,
tf_name=f"{params['cleaned_name']}_resize_stack_1")

if (
resize_method == tf.image.ResizeMethod.NEAREST_NEIGHBOR
and isinstance(resize_size, keras.engine.keras_tensor.KerasTensor)
Expand All @@ -650,7 +663,7 @@ def convert_resize(node, params, layers, lambda_func, node_name, keras_name):

def target_layer(x, resize_size=resize_size):
from tensorflow.python.ops.image_ops import resize_nearest_neighbor

return resize_nearest_neighbor(x, resize_size, half_pixel_centers=False)
lambda_layer = keras.layers.Lambda(target_layer, name=f"{params['cleaned_name']}_resize_lambda")
resized = lambda_layer(to_channel_last)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "onnx2kerastl"
version = "0.0.175"
version = "0.0.176"
description = ""
authors = ["dorhar <doron.harnoy@tensorleap.ai>"]
license = "MIT"
Expand Down
19 changes: 19 additions & 0 deletions test/models/test_openclip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# code to proprely load data here: https://pytorch.org/hub/facebookresearch_pytorchvideo_x3d/
import onnx
import numpy as np
from onnx2kerastl import onnx_to_keras
from keras_data_format_converter import convert_channels_first_to_last
import urllib


def test_openclip():
urllib.request.urlretrieve(
"https://storage.googleapis.com/example-datasets-47ml982d/openclip/openclip.onnx",
"openclip.onnx")
onnx_model = onnx.load('openclip.onnx')
keras_model = onnx_to_keras(onnx_model, ["pixel_values"], name_policy='attach_weights_name',
allow_partial_compilation=False)
keras_model = keras_model.converted_model
final_model = convert_channels_first_to_last(keras_model, should_transform_inputs_and_outputs=True)
tf_preds = final_model(np.random.random((1, 224, 224, 3)))

1 change: 1 addition & 0 deletions test/models/test_raft_ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ def test_raft_ci():
first_im = np.random.random((440, 1024, 3))[None, ...]
second_im = np.random.random((440, 1024, 3))[None, ...]
tf_preds = final_model([first_im, second_im])