xgboost model 处理 MNIST handwritten digits

博主在优快云上分享了其运行特定代码或实验的结果,详细记录了过程与发现,为读者提供了实践参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<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? 按照这个分析来
最新发布
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值