@@ -492,6 +492,21 @@ def __mod__(self, other):
492
492
)
493
493
return OpenVINOKerasTensor (ov_opset .mod (first , other ).output (0 ))
494
494
495
+ def __array__ (self , dtype = None ):
496
+ try :
497
+ tensor = cast (self , dtype = dtype ) if dtype is not None else self
498
+ return convert_to_numpy (tensor )
499
+ except Exception as e :
500
+ raise RuntimeError (
501
+ "An OpenVINOKerasTensor is symbolic: it's a placeholder "
502
+ "for a shape and a dtype.\n "
503
+ "It doesn't have any actual numerical value.\n "
504
+ "You cannot convert it to a NumPy array."
505
+ ) from e
506
+
507
+ def numpy (self ):
508
+ return self .__array__ ()
509
+
495
510
496
511
def ov_to_keras_type (ov_type ):
497
512
for _keras_type , _ov_type in OPENVINO_DTYPES .items ():
@@ -672,8 +687,10 @@ def convert_to_numpy(x):
672
687
ov_model = Model (results = [ov_result ], parameters = [])
673
688
ov_compiled_model = compile_model (ov_model , get_device ())
674
689
result = ov_compiled_model ({})[0 ]
675
- except :
676
- raise "`convert_to_numpy` cannot convert to numpy"
690
+ except Exception as inner_exception :
691
+ raise RuntimeError (
692
+ "`convert_to_numpy` failed to convert the tensor."
693
+ ) from inner_exception
677
694
return result
678
695
679
696
@@ -690,6 +707,7 @@ def shape(x):
690
707
691
708
692
709
def cast (x , dtype ):
710
+ dtype = standardize_dtype (dtype )
693
711
ov_type = OPENVINO_DTYPES [dtype ]
694
712
x = get_ov_output (x )
695
713
return OpenVINOKerasTensor (ov_opset .convert (x , ov_type ).output (0 ))
0 commit comments