[MPSGraph coordinateAlongAxisTensor:withShapeTensor:name:]: unrecognized selector sent to instance 0x2bf029030

I am running keros_ocr with tensorflow-macos 2.11.0 and tensorflow-metal 0.7.1 on Monterey 12.2 on an MacBook Pro 2021 with Apple M1 Pro. The environment I built according to this website https://developer.apple.com/metal/tensorflow-plugin/ Then I got this issue

Looking for /Users/macbookpro/.keras-ocr/craft_mlt_25k.h5
Metal device set to: Apple M1 Pro

systemMemory: 16.00 GB
maxCacheSize: 5.33 GB

2023-02-13 09:42:08.663860: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:306] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.
2023-02-13 09:42:08.664267: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:272] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)
Looking for /Users/macbookpro/.keras-ocr/crnn_kurapan.h5
2023-02-13 09:42:11.437347: W tensorflow/tsl/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz
2023-02-13 09:42:11.595474: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:114] Plugin optimizer for device_type GPU is enabled.
1/1 [==============================] - 2s 2s/step
2023-02-13 09:42:15.158322: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:114] Plugin optimizer for device_type GPU is enabled.
2023-02-13 09:42:15.438 python[2073:25218] -[MPSGraph coordinateAlongAxisTensor:withShapeTensor:name:]: unrecognized selector sent to instance 0x2bf029030
2023-02-13 09:42:15.487 python[2073:25218] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MPSGraph coordinateAlongAxisTensor:withShapeTensor:name:]: unrecognized selector sent to instance 0x2bf029030'
*** First throw call stack:
(
        0   CoreFoundation                      0x00000001ac40c1cc __exceptionPreprocess + 240
        1   libobjc.A.dylib                     0x00000001ac15d7b8 objc_exception_throw + 60
        2   CoreFoundation                      0x00000001ac49f1a0 -[NSObject(NSObject) __retain_OA] + 0
        3   CoreFoundation                      0x00000001ac36c360 ___forwarding___ + 1728
        4   CoreFoundation                      0x00000001ac36bbe0 _CF_forwarding_prep_0 + 96
        5   libmetal_plugin.dylib               0x000000017795e25c ___ZN12metal_plugin17MPSSequenceOpBaseIiE15ProduceSequenceEPNS_15OpKernelContextEPNS_6TensorEiiix_block_invoke + 200
        6   libmetal_plugin.dylib               0x000000017795c81c ___ZN12metal_plugin13MPSGraphCache17CreateCachedGraphERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEU13block_pointerFPNS_14MPSCachedGraphEvE_block_invoke + 492
        7   libdispatch.dylib                   0x00000001ac102bac _dispatch_client_callout + 20
        8   libdispatch.dylib                   0x00000001ac111e00 _dispatch_lane_barrier_sync_invoke_and_complete + 56
        9   libmetal_plugin.dylib               0x000000017795decc _ZN12metal_plugin17MPSSequenceOpBaseIiE15ProduceSequenceEPNS_15OpKernelContextEPNS_6TensorEiiix + 828
        10  libmetal_plugin.dylib               0x000000017795d468 _ZN12metal_plugin10MPSRangeOpIiE7ComputeEPNS_15OpKernelContextE + 2076
        11  libmetal_plugin.dylib               0x000000017795cb78 _ZN12metal_pluginL15ComputeOpKernelINS_10MPSRangeOpIiEEEEvPvP18TF_OpKernelContext + 44
        12  libtensorflow_framework.2.dylib     0x0000000156240028 _ZN10tensorflow15PluggableDevice7ComputeEPNS_8OpKernelEPNS_15OpKernelContextE + 148
        13  libtensorflow_framework.2.dylib     0x00000001561bd510 _ZN10tensorflow12_GLOBAL__N_113ExecutorStateINS_15PropagatorStateEE7ProcessENS2_10TaggedNodeEx + 3784
        14  libtensorflow_framework.2.dylib     0x00000001561bf170 _ZNSt3__110__function6__funcIZN10tensorflow12_GLOBAL__N_113ExecutorStateINS2_15PropagatorStateEE7RunTaskIZNS6_13ScheduleReadyEPN4absl12lts_2022062313InlinedVectorINS5_10TaggedNodeELm8ENS_9allocatorISB_EEEEPNS5_20TaggedNodeReadyQueueEEUlvE1_EEvOT_iEUlvE_NSC_ISL_EEFvvEEclEv + 56
        15  _pywrap_tensorflow_internal.so      0x00000002a7faab78 _ZN5Eigen15ThreadPoolTemplIN3tsl6thread16EigenEnvironmentEE10WorkerLoopEi + 1496
        16  _pywrap_tensorflow_internal.so      0x00000002a7faa474 _ZZN3tsl6thread16EigenEnvironment12CreateThreadENSt3__18functionIFvvEEEENKUlvE_clEv + 80
        17  libtensorflow_framework.2.dylib     0x00000001560de878 _ZN3tsl12_GLOBAL__N_17PThread8ThreadFnEPv + 120
        18  libsystem_pthread.dylib             0x00000001ac2c1240 _pthread_start + 148
        19  libsystem_pthread.dylib             0x00000001ac2bc024 thread_start + 8
)
libc++abi: terminating with uncaught exception of type NSException

Her is my test code

import matplotlib.pyplot as plt

import keras_ocr

# keras-ocr will automatically download pretrained
# weights for the detector and recognizer.
pipeline = keras_ocr.pipeline.Pipeline()

# Get a set of three example images
images = [
    keras_ocr.tools.read(url) for url in [
        'https://pic4.zhimg.com/v2-97af8540d2b25dd0f01f45af74b65f2b_r.jpg',
        'https://pic4.zhimg.com/v2-283a715d0c4628fb1e2020a2228e252b_r.jpg'
    ]
]

# Each list of predictions in prediction_groups is a list of
# (word, box) tuples.
prediction_groups = pipeline.recognize(images)

# Plot the predictions
fig, axs = plt.subplots(nrows=len(images), figsize=(20, 20))
for ax, image, predictions in zip(axs, images, prediction_groups):
    keras_ocr.tools.drawAnnotations(image=image, predictions=predictions, ax=ax)

And libs version

keras           2.11.0          pypi_0  pypi
keras-applications    1.0.8          pypi_0  pypi
keras-ocr         0.0.0          pypi_0  pypi
numpy           1.24.2          pypi_0  pypi
opencv-python       4.7.0.68         pypi_0  pypi
openssl          1.1.1s        h1a28f6b_0  
pip            23.0           pypi_0  pypi
python          3.9.16        hc0d8a6c_0  
python-dateutil      2.8.2          pypi_0  pypi
tensorboard        2.11.2          pypi_0  pypi
tensorboard-data-server  0.6.1          pypi_0  pypi
tensorboard-plugin-wit  1.8.1          pypi_0  pypi
tensorflow-deps      2.9.0             0  apple
tensorflow-estimator   2.11.0          pypi_0  pypi
tensorflow-macos     2.11.0          pypi_0  pypi
tensorflow-metal     0.7.1          pypi_0  pypi
Answered by OliverPatt in 744744022

I can running this code successfully

import tensorflow as tf
from tensorflow.keras.optimizers.legacy import Adam

cifar = tf.keras.datasets.cifar100
(x_train, y_train), (x_test, y_test) = cifar.load_data()
model = tf.keras.applications.ResNet50(
    include_top=True,
    weights=None,
    input_shape=(32, 32, 3),
    classes=100,)

loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
# Instantiate legacy Adam optimizer
model.compile(optimizer=Adam(), loss=loss_fn, metrics=["accuracy"])
model.fit(x_train, y_train, epochs=5, batch_size=64)
Accepted Answer

I can running this code successfully

import tensorflow as tf
from tensorflow.keras.optimizers.legacy import Adam

cifar = tf.keras.datasets.cifar100
(x_train, y_train), (x_test, y_test) = cifar.load_data()
model = tf.keras.applications.ResNet50(
    include_top=True,
    weights=None,
    input_shape=(32, 32, 3),
    classes=100,)

loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
# Instantiate legacy Adam optimizer
model.compile(optimizer=Adam(), loss=loss_fn, metrics=["accuracy"])
model.fit(x_train, y_train, epochs=5, batch_size=64)

For the issue with unrecognized selector, could you please try upgrading to latest macOS Ventura?

[MPSGraph coordinateAlongAxisTensor:withShapeTensor:name:]: unrecognized selector sent to instance 0x2bf029030
 
 
Q