深度学习相关技术与报错解决

深度学习相关技术与报错解决 一、ONNX的使用转换TensorFlow模型到PyTorch模型涉及使用ONNXOpen Neural Network Exchange作为中间表示。以下是一个简单的步骤使用ONNX将TensorFlow模型转换为PyTorch模型安装ONNX在您的Python环境中安装ONNX。可以使用以下命令pipinstallonnx使用tf2onnx将TensorFlow模型转换为ONNX格式安装tf2onnxpipinstalltf2onnx使用tf2onnx将TensorFlow模型保存为ONNX格式python-mtf2onnx.convert --saved-model /path/to/tf_model--output/path/to/tf_model.onnx--opset12python-mtf2onnx.convert --saved-model saved_models/stegastamp_pretrained--outputsaved_models/tf_model.onnx--opset12请将/path/to/tf_model替换为您的TensorFlow模型保存的路径。使用onnx2pytorch将ONNX模型转换为PyTorch模型安装onnx2pytorchpipinstallonnx2pytorch使用onnx2pytorch将ONNX模型转换为PyTorch模型importtorchimportonnxfromonnx2pytorchimportConvertModel onnx_model_pathsaved_models/tf_model.onnxpytorch_model_pathsaved_models/pytorch_model.pth# Load the ONNX modelonnx_modelonnx.load(onnx_model_path)# Convert the ONNX model to PyTorch modelmodelConvertModel(onnx_model)# Save the PyTorch modeltorch.save(model.state_dict(),pytorch_model_path)请将/path/to/tf_model.onnx替换为使用tf2onnx保存的ONNX模型路径并将/path/to/pytorch_model.pth替换为您希望保存的PyTorch模型的路径。请确保TensorFlow模型兼容ONNX格式因为不是所有的TensorFlow操作都能被成功转换。有时候可能需要一些额外的处理或者手动修改模型。以上步骤是一个简单的示例实际过程可能因模型复杂性和ONNX支持的操作而有所不同。二、对抗样本简述对抗攻击算法主要用于在神经网络深度学习模型上生成对抗样本以欺骗模型。以下是几种常见的对抗攻击算法1. 基本梯度攻击 (Fast Gradient Sign Method, FGSM)原理使用目标模型的梯度信息沿着损失函数梯度的正方向对输入数据施加微小扰动使得模型分类错误。公式x′xϵ⋅sign(∇xJ(x,y)) x x \epsilon \cdot sign(\nabla_x J(x, y))x′xϵ⋅sign(∇x​J(x,y))其中x’ 是对抗样本x 是原始输入J(x, y) 是损失函数\epsilon 是扰动大小sign(\cdot) 表示梯度的符号函数。2. 投影梯度下降 (Projected Gradient Descent, PGD)原理PGD 是 FGSM 的多步版本每次计算梯度后进行小步更新并投影回允许的扰动范围内。PGD 是 FGSM 的多步优化版本可以使用不同优化器如SGD、Momentum、Adam等公式xt1Projϵ(xtα⋅sign(∇xJ(xt,y))) x_{t1} \text{Proj}_\epsilon \left(x_t \alpha \cdot sign(\nabla_x J(x_t, y))\right)xt1​Projϵ​(xt​α⋅sign(∇x​J(xt​,y)))其中α\alpha 是每次更新的步长\text{Proj}_\epsilon(\cdot) 表示投影操作确保扰动不超过最大限制 \epsilon。使用Adam来生成对抗扰动通常属于Carlini WagnerCW攻击或PGDProjected Gradient Descent攻击的一种优化变体。当 PGD 使用 Adam自适应矩估计优化算法进行优化时它会在每一步自适应调整学习率提高扰动的搜索效率。如果使用L2 范数扰动并优化自定义目标函数通常属于CW 攻击。如果是在PGD 过程中用 Adam 代替 SGD 进行优化则属于PGD 的变体。Adam 的使用主要是为了提高优化过程的收敛速度使得攻击更加稳定和有效。3. Carlini Wagner (CW) 攻击原理通过优化一个目标函数寻找最小扰动使模型错误分类。CW 攻击采用Adam 或 L-BFGS作为优化器目标函数min⁡∣∣δ∣∣pc⋅f(xδ) \min ||\delta||_p c \cdot f(x \delta)min∣∣δ∣∣p​c⋅f(xδ)其中|\delta||_p 控制扰动的大小通常使用 L_2 范数。f(x\delta) 是损失函数控制对抗样本是否被错误分类。c 是平衡参数。Adam 作为优化器加速扰动搜索提高对抗样本的有效性。4. 深度伪造攻击 (DeepFool)原理基于几何方法寻找最小的扰动使输入跨越决策边界。步骤计算当前分类边界的法向量。计算最小扰动使得输入跨越边界。迭代调整直到类别发生变化。5. 广义扰动 (Universal Adversarial Perturbations, UAP)原理寻找一个固定的扰动可以欺骗大部分输入样本而不是针对单个样本优化。方法选择一个随机样本集。使用 DeepFool 或 PGD 方法计算扰动。在多个样本上累积扰动直到它对大部分数据都有效。6. 扰动生成网络 (AdvGAN)原理使用生成对抗网络 (GAN) 生成对抗样本以增强攻击能力。方法训练一个生成器 GG 来生成对抗扰动。设计损失函数使生成的样本能有效欺骗目标模型。训练过程中不断优化 GAN以增强泛化能力。7. HopSkipJump 攻击原理是一种基于决策边界的黑盒攻击方法不依赖梯度信息仅使用目标模型的决策结果。方法通过查询模型找到决策边界附近的点。迭代优化最小化扰动生成对抗样本。这些对抗攻击算法被广泛用于研究深度学习模型的鲁棒性同时也推动了对抗防御技术的发展如对抗训练、检测方法和认证方法等。三、解决实战中报错提示github拉下来的项目报错在issue中搜索关键词1. 所有conda环境都使用一个地方的包Conda的虚拟环境应该使用对应虚拟环境中的包通过which pip发现其位置在~/.local/bin/pip而非/usr/anaconda3/envs/py38/bin/pip解决方案在~/.profile或/etc/profile中在PATH~/.local/bin后面设置PATH/usr/anaconda3/bin:$PATH2.tensorboard运行后报错Failed to fetch runs搜了网上包括CSDN很多解决方案都没有用要注意日志报错TypeError: MessageToJson() got an unexpected keyword argument ‘including_default_value_fields’解决方案是在对应环境下pip install protobuf4.25.33.torch.cuda.OutOfMemoryError: CUDA out of memory.一般情况就是爆显存了通过减小batch_size或输入图片尺寸来减小计算图。还有一种情况就是ctrlz关闭的程序其实进程没有结束应当ctrlc。操作如下关闭进程nvidia-smi或watch -n 1 nvidia-smi在看板的下方你会看到正在GPU上通过python指令运行的进程查看它们的PID删除这些进程kill -9 %PID_number%后面我又发现即使kill掉也没完全释放显存只能重启解决4.vscode终端显示两个环境名vscode python插件自动激活环境和服务器终端初始化自动激活base环境之间的不兼容导致的问题解决方法是关闭conda的自动激活环境conda config --set auto_activate_base False5.vscode远程plt.show()无法显示右键在交互窗口中运行或用plt.savefig直接保存6.NVIDIA GeForce RTX 4090 with CUDA capability sm_89 is not compatible with the current PyTorch installation.The current PyTorch install supports CUDA capabilities sm_37 sm_50 sm_60 sm_61 sm_70 sm_75 compute_37.If you want to use the NVIDIA GeForce RTX 4090 GPU with PyTorch, please check the instructions at https://pytorch.org/get-started/locally/显卡架构sm_89较新旧版pytorch库不支持。解决办法conda remove pytorch再输入上方链接的命令。提示环境的pytorch版本不影响代码和程序。7.visdom遇到问题ERROR:tornado.access:500 GET / (::1) 2.10ms解决pip uninstall visdom并用conda下载包命令在下面的网址中https://anaconda.org/conda-forge/visdom8.wandb无法在Media输出键带’id’的图片解决应该是出于安全原因把id替换成di即可9.ImportError: DLL load failed while importing _imaging: 找不到指定的模块。解决不建议使用windows建议使用linux跑程序windows经常会出现此类错误linux不会出现此类错误想在windows解决这个很麻烦10.Collecting package metadata (current_repodata.json): | WARNING conda.models.version:get_matcher(556): Using .* with relational operator is superfluous and deprecated and will be removed in a future version of conda. Your spec was 1.7.1., but conda is ignoring the .and treating it as 1.7.1doneSolving environment: unsuccessful initial attempt using frozen solve. Retrying with flexible solve.Solving environment: unsuccessful attempt using repodata from current_repodata.json, retrying with next repodata source.Collecting package metadata (repodata.json): | WARNING conda.models.version:get_matcher(556): Using .* with relational operator is superfluous and deprecated and will be removed in a future version of conda. Your spec was 1.9.0., but conda is ignoring the .and treating it as 1.9.0WARNING conda.models.version:get_matcher(556): Using .* with relational operator is superfluous and deprecated and will be removed in a future version of conda. Your spec was 1.8.0., but conda is ignoring the .and treating it as 1.8.0WARNING conda.models.version:get_matcher(556): Using .* with relational operator is superfluous and deprecated and will be removed in a future version of conda. Your spec was 1.6.0., but conda is ignoring the .and treating it as 1.6.0conda install pytorch torchvision torchaudio pytorch-cuda -c pytorch -c nvidia运行后报错的解决方法conda config --remove-key channels11.ERROR: Could not find a version that satisfies the requirement tensorflow1.13.1(from versions: 2.2.0, 2.2.1, 2.2.2, 2.2.3, 2.3.0, 2.3.1, 2.3.2, 2.3.3, 2.3.4, 2.4.0, 2.4.1, 2.4.2, 2.4.3, 2.4.4, 2.5.0, 2.5.1, 2.5.2, 2.5.3, 2.6.0rc0, 2.6.0rc1, 2.6.0rc2, 2.6.0, 2.6.1, 2.6.2, 2.6.3, 2.6.4, 2.6.5, 2.7.0rc0, 2.7.0rc1, 2.7.0, 2.7.1, 2.7.2, 2.7.3, 2.7.4, 2.8.0rc0, 2.8.0rc1, 2.8.0, 2.8.1, 2.8.2, 2.8.3, 2.8.4, 2.9.0rc0, 2.9.0rc1, 2.9.0rc2, 2.9.0, 2.9.1, 2.9.2, 2.9.3, 2.10.0rc0, 2.10.0rc1, 2.10.0rc2, 2.10.0rc3, 2.10.0, 2.10.1, 2.11.0rc0, 2.11.0rc1, 2.11.0rc2, 2.11.0, 2.11.1, 2.12.0rc0, 2.12.0rc1, 2.12.0, 2.12.1, 2.13.0rc0, 2.13.0rc1, 2.13.0rc2, 2.13.0, 2.13.1)ERROR: No matching distribution found for tensorflow1.13.1解决使用python3.8创建conda环境不支持1.13.1版本的tensorflow降低python版本即可conda create-ntf37python3.712.ERROR: pip’s dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.tensorflow 1.13.1 requires tensorboard1.14.0,1.13.0, but you have tensorboard 2.11.2 which is incompatible.因为tensorboard1.15.0及以上才能用torch版的writer所以我才更新了tensorboard解决不用管他程序依旧能正常运行这样既能使用tensorflow1.13.1且能正常使用tensorboard13.OSError: CUDA_HOME environment variable is not set. Please set it to your CUDA install root.解决conda环境使用pip下载时会出现使用对应的conda指令即可如conda install mmcv-full。或者运行sudo apt install nvidia-cuda-toolkit下载一个cuda到本地。更新后续发现之前的解决方式会出现ModuleNotFoundError: No module named ‘mmcv._ext’原因是mmcv与torch版本不匹配应运行pip install mmcv-full1.7.0 -f https://download.openmmlab.com/mmcv/dist/cu117/torch1.13/index.html这样可以下载到兼容的版本。14.ImportError: libcudnn.so.7: cannot open shared object file: No such file or directory这个问题出现在pip install tensorflow-gpu1.13.1之后安装了cuda-cudart和其他 CUDA 库但缺少 cuDNNlibcudnn.so.7兼容性要求很高python版本为3.7的情况下1.x我尝试成功的版本conda install -c conda-forge cudatoolkit10.0 cudnn7.6pip install tensorflow-gpu1.13.1。2.x我尝试成功的版本conda install -c conda-forge cudatoolkit11.0 cudnn8.0pip install tensorflow-gpu2.4.0。使用 conda 安装 CUDA 和 cuDNN 的确有很多优势其中一个主要优点就是不需要手动配置环境变量。另外如果代码是v1建议下载v2的tensorflow然后使用import tensorflow.compat.v1 as tf\\ tf.disable_v2_behavior()因为最新版不容易出现包版本冲突问题。failed to run cuBLAS routine cublasSgemm_v2RTX30系显卡会报此错误没有找到解决方案另外conda uninstall tensorflow\\ conda uninstall tensorflow-gpu\\ pip uninstall tensorflow\\ pip uninstall tensorflow-gpu然后conda install --force-reinstall tensorflow就可以下载最新版带GPU的tesnorflow经我验证运行python -c import tensorflow as tf; print(TensorFlow version:, tf.__version__); print(Is GPU available:, tf.config.list_physical_devices(GPU))有返回GPU。2024-10-07 10:47:36.933704539 [E:onnxruntime:Default, provider_bridge_ort.cc:1992 TryGetProviderInfo_CUDA] /onnxruntime_src/onnxruntime/core/session/provider_bridge_ort.cc:1637 onnxruntime::Provider onnxruntime::ProviderLibrary::Get() [ONNXRuntimeError] : 1 : FAIL : Failed to load library libonnxruntime_providers_cuda.so with error: libcudnn.so.9: cannot open shared object file: No such file or directoryconda uninstall onnxruntime\\ conda uninstall onnxruntime-gpu\\ pip uninstall onnxruntime\\ pip uninstall onnxruntime-gpu然后conda install --force-reinstall onnxruntime15.WARNING: No metadata found in /home/user/anaconda3/envs/tf37/lib/python3.7/site-packagesFound existing installation: google-pasta 0.2.0 ERROR: Cannot uninstall google-pasta 0.2.0, RECORD file not found. You might be able to recover from this via: ‘pip install --force-reinstall --no-deps google-pasta0.2.0’.尝试手动删除google-pasta的残留文件。小心执行以下命令rm -rf /home/dongli911/anaconda3/envs/tf37/lib/python3.7/site-packages/google_pasta*