with an unsupported type (<class ‘method‘>) to a Tensor.

在进行深度学习时遇到Tensorflow2.3.0与Numpy1.23.5版本不兼容的问题,导致运行错误。通过将数据从DataFrame转换为array,再用tf.convert_to_tensor方法避免了错误,成功将数据转换为tensor。无需调整Tensorflow和Numpy的版本。
部署运行你感兴趣的模型镜像

原因:tensorflow 和numpy 版本不兼容

解决方法:版本更换

但是我懒

另一个解决方法:将numpy转换为array,运行tf,convert_to_tensor(array)

在学习深度学习时在单独的tensorflow 下面进行,numpy tensorflow版本如下

print(tf.__version__)
print(np.__version__)

#运行结果
2.3.0
1.23.5

开始的运行如下

train_x=pd.read_csv(Loc_dress+"Input_water_train.csv",header=None)
print(train_x.head(5))
train_X=train_x.to_numpy
train_X=tf.convert_to_tensor(train_X,dtype=tf.float32)

运行相继报错:

with an unsupported type (<class 'method'>) to a Tensor.

搜索相关问题回答,应该是tensorflow 与numpy版本不兼容导致,但是又不想去重新调版本

后来发现当变量类型为array 时,使用tf.convert_to_tensor(array)不会报错,所以将其转换为dataframe直接转换为array,运行成功!!

train_x=pd.read_csv(Loc_dress+"Input_water_train.csv",header=None)
print(train_x.head(5))
print(type(train_x))
train_X=np.array(train_x)
train_X=tf.convert_to_tensor(train_X,dtype=tf.float32)

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

<think> Let me analyze this code and provide a helpful response. This appears to be a Python script for training a convolutional neural network (CNN) on the MNIST handwritten digit dataset using PyTorch, and then exporting the trained model to ONNX format. The code consists of several parts: Importing necessary libraries and setting hyperparameters Preparing the MNIST dataset with transformations Visualizing some examples from the dataset Defining a CNN model architecture using PyTorch Setting up loss function and optimizer Defining training and testing functions Training the model for 10 epochs and saving it Exporting the trained model to ONNX format At the end, the user mentions they want to convert their ONNX model to RKNN format but are having version discrepancies. They're using rknn-toolkit2 version 2.3.0 in their code, but on their Ubuntu system they have rknn-toolkit2 version 1.6.0 with Python 3.10. I'll focus on providing a solution for converting their ONNX model to RKNN format, considering the version discrepancy they're experiencing. Let me first understand the model architecture better: Input: MNIST images (1 channel, 28x28 pixels) First convolutional layer: 1→10 channels, 5x5 kernel, followed by ReLU and 2x2 max pooling Second convolutional layer: 10→20 channels, 5x5 kernel, followed by ReLU and 2x2 max pooling Fully connected layers: 320→50→10 (output classes for digits 0-9) The exported ONNX model uses: Input shape: [1, 1, 28, 28] Output shape: [1, 10] (representing probabilities for digits 0-9) ONNX opset version: 19 Now, the issue is the version discrepancy between the rknn-toolkit2 they're using in their code (2.3.0) and what's installed on their Ubuntu system (1.6.0). I need to provide code that will work with their installed version (1.6.0) to convert the ONNX model to RKNN format. </think> I see you've shared a PyTorch script for training a CNN model on the MNIST dataset, followed by ONNX conversion. You mentioned that you need to convert this ONNX model to RKNN format, but there's a version mismatch between the rknn-toolkit2 in your code (2.3.0) and what's installed on your Ubuntu system (1.6.0). Let me create a compatible RKNN conversion script for your system: <artifact id="rknn_conversion_script" type="application/vnd.ant.code" language="python" title="MNIST ONNX to RKNN Conversion Script"> from rknn.api import RKNN Configuration ONNX_MODEL = './model_Mnist_fixed.onnx' RKNN_MODEL = './model_Mnist.rknn' Initialize RKNN object rknn = RKNN(verbose=True) Pre-process config for input print('--> Config model') rknn.config(mean_values=[127.5], std_values=[127.5], target_platform='rk3588') print('done') Load ONNX model print('--> Loading model') ret = rknn.load_onnx(model=ONNX_MODEL) if ret != 0: print('Load ONNX model failed!') exit(ret) print('done') Build model print('--> Building model') ret = rknn.build(do_quantization=True, dataset='./dataset.txt') if ret != 0: print('Build model failed!') exit(ret) print('done') Export RKNN model print('--> Export RKNN model') ret = rknn.export_rknn(RKNN_MODEL) if ret != 0: print('Export RKNN model failed!') exit(ret) print('done') Optional: Inference testing print('--> Testing model on sample data') Set inputs img = cv2.imread('sample_digit.png', cv2.IMREAD_GRAYSCALE) img = cv2.resize(img, (28, 28)) img = np.expand_dims(img, axis=0) # Add batch dimension img = np.expand_dims(img, axis=0) # Add channel dimension Init runtime environment ret = rknn.init_runtime() if ret != 0: print('Init runtime environment failed') exit(ret) Inference print('--> Running model') outputs = rknn.inference(inputs=[img]) predicted = np.argmax(outputs[0][0]) print(f'Predicted digit: {predicted}') Release RKNN context rknn.release() </artifact> Additional Setup for RKNN Conversion For the above script to work properly, you'll need: Create a calibration dataset file: RKNN quantization requires a dataset file listing paths to sample images: <artifact id="dataset_generator" type="application/vnd.ant.code" language="python" title="Calibration Dataset Generator"> import os import numpy as np from torchvision import datasets, transforms import torch Create dataset directory if it doesn't exist os.makedirs('./calibration_images', exist_ok=True) Load a subset of MNIST images for calibration transform = transforms.Compose([transforms.ToTensor()]) dataset = datasets.MNIST(root='./data/mnist', train=True, transform=transform, download=True) data_loader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=True) Save calibration images (using 100 images) with open('./dataset.txt', 'w') as f: for i, (img, ) in enumerate(data_loader): if i >= 100: # Use 100 images for calibration break # Convert tensor to numpy and save as raw format img_np = img.numpy().reshape(28, 28) * 255 img_np = img_np.astype(np.uint8) img_path = f'./calibration_images/img{i:03d}.bin' img_np.tofile(img_path) f.write(f'{img_path}\n') print(f"Created dataset.txt with {min(100, i+1)} image paths for calibration") </artifact> Version Compatibility Notes Since you're using rknn-toolkit2 version 1.6.0 on Ubuntu with Python 3.10, here are some important compatibility considerations: API differences: The script I provided uses the API structure compatible with older versions. The newer 2.3.0 version might have slightly different parameter names or methods. Target platform: I specified 'rk3588' as the target platform. Adjust this if you're targeting a different RK chip. Quantization parameters: The script uses basic quantization settings. You may need to adjust these (particularly mean_values and std_values) based on your model's preprocessing. Testing the model: The optional testing section requires you to have a sample image. You can generate one from your test dataset or skip this section if not needed. Troubleshooting Common Issues If you encounter errors: ONNX compatibility: Older RKNN versions may not support ONNX opset 19. Try re-exporting your ONNX model with a lower opset version (e.g., 11 or 13). Layer support: Some operations might not be supported. Check error messages for unsupported operations and consider simplifying your model if needed. Quantization issues: If quantization fails, try do_quantization=False first to verify the conversion works without quantization. Would you like me to explain any specific part of the conversion process in more detail? 先仔细阅读这部分内容,再来结合这上面的讲解,最终生成适合的转化rknn代码
最新发布
05-30
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值