From ae55e5bd73b815dd861588d2a40e0e8634d08751 Mon Sep 17 00:00:00 2001 From: "jiangnana.jnn" Date: Thu, 28 Jul 2022 21:53:34 +0800 Subject: [PATCH] [to #43627720] fix "TypeError: can't convert np.ndarray of type numpy.object_" Link: https://code.alibaba-inc.com/Ali-MaaS/MaaS-lib/codereview/9565199 * fix "TypeError: can't convert np.ndarray of type numpy.object_" --- modelscope/utils/tensor_utils.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/modelscope/utils/tensor_utils.py b/modelscope/utils/tensor_utils.py index 3e6075f0..01b68f78 100644 --- a/modelscope/utils/tensor_utils.py +++ b/modelscope/utils/tensor_utils.py @@ -41,12 +41,16 @@ def torch_default_data_collator(features): first['label'], torch.Tensor) else first['label'] # the msdataset return a 0-dimension np.array with a single value, the following part handle this. if isinstance(label, np.ndarray): + src_dtype = label[()].dtype dtype = torch.long if label[( )].dtype == np.int64 else torch.float else: + src_dtype = type(label) dtype = torch.long if isinstance(label, int) else torch.float + # add dtype to np.array to fix "TypeError: can't convert np.ndarray of type numpy.object_" batch['labels'] = torch.tensor( - np.array([f['label'] for f in features]), dtype=dtype) + np.array([f['label'] for f in features], dtype=src_dtype), + dtype=dtype) elif 'label_ids' in first and first['label_ids'] is not None: if isinstance(first['label_ids'], torch.Tensor): batch['labels'] = torch.stack(