Skip to content

🔧 Add Apple Silicon (M1) Compatibility — Dependency & API Updates #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 9 additions & 5 deletions common/utils/models_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import os
from tabulate import tabulate
from keras.utils.layer_utils import count_params
from typing import Dict, Optional, Tuple, List
import tensorflow as tf
from tensorflow.keras.models import Model
Expand All @@ -20,6 +19,7 @@
import onnxruntime
import mlflow
from common.utils import log_to_file
from tensorflow.keras import backend as K


def ai_interp_input_quant(ai_interp, data: np.array, data_scale: float, data_offset: float, file_extension: str):
Expand Down Expand Up @@ -286,7 +286,11 @@ def model_summary(model):
if layer_type == "InputLayer":
layer_shape = model.input.shape
else:
layer_shape = layer.output_shape
if hasattr(layer, 'output') and hasattr(layer.output, 'shape'):
layer_shape = layer.output.shape
else:
layer_shape = 'Not available'

is_trainable = True if layer.trainable else False
num_params = layer.count_params()
if layer.trainable:
Expand All @@ -300,8 +304,8 @@ def model_summary(model):
print(tabulate(table, headers=["Layer index", "Trainable", "Name", "Type", "Params#", "Output shape"]))
print(108 * '-')
print("Total params:", model.count_params())
print("Trainable params: ", count_params(model.trainable_weights))
print("Non-trainable params: ", count_params(model.non_trainable_weights))
print("Trainable params: ", sum([K.count_params(w) for w in model.trainable_weights]))
print("Non-trainable params: ", sum([K.count_params(w) for w in model.non_trainable_weights]))
print(108 * '-')
print("Total layers:", num_layers)
print("Trainable layers:", trainable_layers)
Expand Down Expand Up @@ -443,4 +447,4 @@ def compute_confusion_matrix(test_set: tf.data.Dataset = None, model: Model = No

# Calculate the confusion matrix.
cm = sklearn.metrics.confusion_matrix(labels, logits)
return cm, test_accuracy
return cm, test_accuracy
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,18 @@
import tensorflow as tf
from tensorflow import keras
from keras import backend
from keras.engine import base_layer
from keras.engine import base_preprocessing_layer
from keras.utils import control_flow_util
from keras.layers import Layer
from src.data_augmentation import data_augmentation


class DataAugmentationLayer(base_layer.Layer):
class DataAugmentationLayer(Layer):

def __init__(self,
data_augmentation_fn,
config=None,
pixels_range=None,
batches_per_epoch=None,
**kwargs):
base_preprocessing_layer.keras_kpl_gauge.get_cell('DataAugmentationLayer').set(True)
super(DataAugmentationLayer, self).__init__(**kwargs)
self.data_augmentation_fn = data_augmentation_fn
self.config_dict = config
Expand Down Expand Up @@ -70,7 +67,11 @@ def transform_input_data():

return outputs

return control_flow_util.smart_cond(training, transform_input_data, lambda: inputs)
if training:
return transform_input_data()
else:
return inputs



def get_config(self):
Expand Down
24 changes: 21 additions & 3 deletions image_classification/src/models/mobilenetv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,26 @@

import tensorflow as tf
from tensorflow import keras
from keras.applications import imagenet_utils
from tensorflow.keras import layers
from tensorflow.keras import backend as K

def correct_pad(inputs, kernel_size):
"""Returns a tuple for zero-padding for 2D conv with 'same' padding."""
img_dim = 1 if K.image_data_format() == 'channels_first' else 2
input_size = K.int_shape(inputs)[img_dim:img_dim + 2]
if isinstance(kernel_size, int):
kernel_size = (kernel_size, kernel_size)
adjust = (1, 1)
correct = ((kernel_size[0] // 2, kernel_size[0] // 2),
(kernel_size[1] // 2, kernel_size[1] // 2))
if input_size[0] is not None and input_size[0] % 2 == 0:
correct = ((kernel_size[0] // 2 - 1, kernel_size[0] // 2),
correct[1])
if input_size[1] is not None and input_size[1] % 2 == 0:
correct = (correct[0],
(kernel_size[1] // 2 - 1, kernel_size[1] // 2))
return correct



def _make_divisible(v, divisor, min_value=None):
Expand Down Expand Up @@ -60,7 +78,7 @@ def _inverted_res_block(inputs, expansion, stride, alpha, filters, block_id):

"""
# Get the shape of the input tensor.
shape = inputs.get_shape().as_list()
shape = inputs.shape
# Get the number of channels in the input tensor.
in_channels = shape[-1]
# Calculate the number of filters for the pointwise 1x1 convolution.
Expand All @@ -80,7 +98,7 @@ def _inverted_res_block(inputs, expansion, stride, alpha, filters, block_id):
# Depthwise 3x3 convolution.
if stride == 2:
# If the stride is 2, pad the input tensor to maintain the same output size.
x = layers.ZeroPadding2D(padding=imagenet_utils.correct_pad(x, 3))(x)
x = layers.ZeroPadding2D(padding=correct_pad(x, 3))(x)
# Perform the depthwise 3x3 convolution.
x = layers.DepthwiseConv2D(kernel_size=3, strides=stride, use_bias=False,
padding='same' if stride == 1 else 'valid')(x)
Expand Down
49 changes: 26 additions & 23 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
tensorflow==2.8.3
mlflow==2.3.0
tensorflow==2.16.2; sys_platform == "linux"
tensorflow-macos==2.16.2; sys_platform == "darwin" and platform_machine == "arm64"
tensorflow-metal==1.2.0; sys_platform == "darwin" and platform_machine == "arm64"
mlflow==2.22.0
hydra-core==1.3.2
omegaconf==2.3.0
numpy==1.23.4
pandas==1.5.3
numpy==1.26.4
pandas==2.2.3
wget==3.2
munch==2.5.0
seaborn==0.12.2
scikit-learn==1.2.2
tqdm==4.65.0
skl2onnx==1.14.0
onnx==1.12.0
munch==4.0.0
seaborn==0.13.2
scikit-learn==1.6.1
tqdm==4.67.1
skl2onnx==1.18.0
onnx==1.17.0
onnxconverter-common==1.13.0
onnxruntime==1.15.1
optuna==3.1.1
onnxruntime==1.22.0
optuna==4.3.0
imgaug==0.4.0
matplotlib==3.6.2
matplotlib==3.10.3
opencv_python==4.6.0.66
joblib==1.2.0
librosa==0.10.0.post2
mako==1.2.4
joblib==1.5.0
librosa==0.11.0
mako==1.3.10
pyserial==3.5
colorama==0.4.5
Requests==2.28.2
urllib3==1.26.13
colorama==0.4.6
requests==2.32.3
urllib3==2.4.0
xmlrunner==1.7.7
marshmallow==3.20.1
tf2onnx==1.14.0
clearml==1.16.5
pip-system-certs==4.0
marshmallow==4.0.0
tf2onnx==1.16.1
clearml==1.18.0
pip-system-certs==4.0
tabulate==0.9.0