pytorch

Custom weight initialization in PyTorch

本秂侑毒 提交于 2021-02-18 06:34:08
问题 What would be the right way to implement a custom weight initialization method in PyTorch ? I believe I can't directly add any method to 'torch.nn.init` but wish to initialize my model's weights with my own proprietary method. 回答1: You can define a method to initialize the weights according to each layer: def weights_init(m): classname = m.__class__.__name__ if classname.find('Conv2d') != -1: m.weight.data.normal_(0.0, 0.02) elif classname.find('BatchNorm') != -1: m.weight.data.normal_(1.0, 0

使用PyTorch对音频进行分类

百般思念 提交于 2021-02-18 05:18:21
作者 | Aakash 来源 | Medium 编辑 | 代码医生团队 什么是分类问题? 对对象进行分类就是将其分配给特定的类别。这本质上是一个分类问题是什么,即将输入数据从一组这样的类别,也称为类分配到预定义的类别。 机器学习中的分类问题示例包括:识别手写数字,区分垃圾邮件和非垃圾邮件或 识别核中的不同蛋白质 。 https://www.kaggle.com/c/jovian-pytorch-z2g 使用的数据集 为了演示分类问题的工作原理,将使用 UrbanSound8K数据集 。该数据集包括 10 种类别的城市声音:空调,汽车喇叭,儿童游戏,狗吠,钻探, enginge_idling , gun_shot ,手提钻,警笛和 street_music 。 https://urbansounddataset.weebly.com/urbansound8k.html 目的是将数据提供给模型(目前可以将其视为黑匣子),并确定模型预测的准确性。 数据集的结构 该数据集可以作为压缩包使用,大小约为 5.6GB 。与某些机器学习数据集不同,此特定数据集中的音频数据与元数据文件夹一起存在于 10 个不同的文件夹中,元数据文件夹包含名为“ UrbanSound8K.csv ”的文件。 D:\DL\ZEROTOGANS\06-URBAN8K-CLASSIFICATION\DATA

RuntimeError: Expected 4-dimensional input for 4-dimensional weight [1024, 64, 3, 3], but got input of size [32, 10] instead

不想你离开。 提交于 2021-02-17 05:48:52
问题 This line works fine self.conv = nn.Conv2d(3, 64, kernel_size=3, stride=2, padding=1, bias=False) I introduced ResNet18 self.conv = ResNet18() **ResNet Class** '''ResNet in PyTorch. For Pre-activation ResNet, see 'preact_resnet.py'. Reference: [1] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun Deep Residual Learning for Image Recognition. arXiv:1512.03385 ''' import torch import torch.nn as nn import torch.nn.functional as F class BasicBlock(nn.Module): expansion = 1 def __init__(self, in

RuntimeError: Expected 4-dimensional input for 4-dimensional weight [1024, 64, 3, 3], but got input of size [32, 10] instead

情到浓时终转凉″ 提交于 2021-02-17 05:48:07
问题 This line works fine self.conv = nn.Conv2d(3, 64, kernel_size=3, stride=2, padding=1, bias=False) I introduced ResNet18 self.conv = ResNet18() **ResNet Class** '''ResNet in PyTorch. For Pre-activation ResNet, see 'preact_resnet.py'. Reference: [1] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun Deep Residual Learning for Image Recognition. arXiv:1512.03385 ''' import torch import torch.nn as nn import torch.nn.functional as F class BasicBlock(nn.Module): expansion = 1 def __init__(self, in

保姆教程 | YOLOv5在建筑工地中安全帽佩戴检测的应用

孤街浪徒 提交于 2021-02-17 04:31:21
点击上方 “ 机器学习与生成对抗网络 ”,关注"星标" 获取有趣、好玩的前沿干货! 教程来自: https://github.com/PeterH0323/Smart_Construction 编辑:AI深度前沿视线 一、YOLO v5训练自己数据集教程 1.1 创建自己的数据集配置文件 1.2 创建每个图片对应的标签文件 1.3 文件放置规范 1.4 聚类得出先验框(可选) 1.5 选择一个你需要的模型 1.6 开始训练 1.7 看训练之后的结果 二、侦测 三、检测危险区域内是否有人 3.1 危险区域标注方式 3.2 执行侦测 3.3 效果:在危险区域里面的人体会被 红色框 选出来 四、生成 ONNX 五、增加数据集的分类 该项目是使用 YOLOv5 v2.x 来训练在智能工地安全领域中头盔目标检测的应用,先来一波演示! 指标 yolov5s 为基础训练, epoch = 50 分类 P R mAP0.5 总体 0.884 0.899 0.888 人体 0.846 0.893 0.877 头 0.889 0.883 0.871 安全帽 0.917 0.921 0.917 对应的 权重文件 :https://pan.baidu.com/share/init?surl=ELPhtW-Q4G8UqEr4YrV_5A,提取码: b981 yolov5m 为基础训练, epoch =

Unexpected error when loading the model: problem in predictor - ModuleNotFoundError: No module named 'torchvision'

一世执手 提交于 2021-02-16 21:30:45
问题 I've been trying to deploy my model to the AI platform for Prediction through the console on my vm instance, but I've gotten the error "(gcloud.beta.ai-platform.versions.create) Create Version failed. Bad model detected with error: "Failed to load model: Unexpected error when loading the model: problem in predictor - ModuleNotFoundError: No module named 'torchvision' (Error code: 0)" I need to include both torch and torchvision . I followed the steps in this question Cannot deploy trained

How two rows can be swapped in a torch tensor?

南笙酒味 提交于 2021-02-16 20:22:07
问题 var = [[0, 1, -4, 8], [2, -3, 2, 1], [5, -8, 7, 1]] var = torch.Tensor(var) Here, var is a 3 x 4 (2d) tensor. How the first and second row can be swapped to get the following 2d tensor? 2, -3, 2, 1 0, 1, -4, 8 5, -8, 7, 1 回答1: The other answer does not work, as some dimensions get overwritten before they are copied: >>> var = [[0, 1, -4, 8], [2, -3, 2, 1], [5, -8, 7, 1]] >>> x = torch.tensor(var) >>> index = torch.LongTensor([1, 0, 2]) >>> x[index] = x >>> x tensor([[ 0, 1, -4, 8], [ 0, 1, -4

pytorch的显存释放机制torch.cuda.empty_cache()

老子叫甜甜 提交于 2021-02-16 09:46:35
Pytorch已经可以自动回收我们不用的显存,类似于python的引用机制,当某一内存内的数据不再有任何变量引用时,这部分的内存便会被释放。但有一点需要注意,当我们有一部分显存不再使用的时候,这部分释放的显存通过Nvidia-smi命令是看不到的,举个例子: device = torch.device('cuda:0') # 定义两个tensor dummy_tensor_4 = torch.randn(120, 3, 512, 512).float().to(device) # 120*3*512*512*4/1000/1000 = 377.48M dummy_tensor_5 = torch.randn(80, 3, 512, 512).float().to(device) # 80*3*512*512*4/1000/1000 = 251.64M # 然后释放 dummy_tensor_4 = dummy_tensor_4.cpu() dummy_tensor_2 = dummy_tensor_2.cpu() # 这里虽然将上面的显存释放了,但是我们通过Nvidia-smi命令看到显存依然在占用 torch.cuda.empty_cache() # 只有执行完上面这句,显存才会在Nvidia-smi中释放 Pytorch的开发者也对此进行说明了,这部分释放后的显存可以用

PyTorch: Dataloader for time series task

馋奶兔 提交于 2021-02-16 08:35:42
问题 I have a Pandas dataframe with n rows and k columns loaded into memory. I would like to get batches for a forecasting task where the first training example of a batch should have shape (q, k) with q referring to the number of rows from the original dataframe (e.g. 0:128). The next example should be (128:256, k) and so on. So, ultimately, one batch should have the shape (32, q, k) with 32 corresponding to the batch size. Since TensorDataset from data_utils does not work here, I am wondering

对抗训练和对抗样本

十年热恋 提交于 2021-02-15 11:08:10
对抗训练 分类模型:对已有图片训练模型并分类 生成模型:分类模型的反向过程生成一张图片, 从一个类别向量反向生成一张图片 对抗样本 对图片通过算法增加一些噪声,变成新的图片,新的图片人眼无法分辨出变化,但是机器能分辨出来而且还会判错类别。 目前一些对抗样本生成算法: FGSM、FGMT:基于攻击者设置的对抗目标函数,利用梯度来优化原数据样本feature的值。 JSMA:基于模型输入输出之间的雅可比矩阵(Jacobian)来决定对输出影响最大的输入feature,进而改变这些feature来生成对抗样本。 DeepFool: 目的是寻找可以使分类器产生误判的最小扰动。简单理解就是点到分界面的各个最小距离。 开源项目 cleverhans Tensorflow下的子项目,完成了FGSM、FGMT、deepfool、CW2、JSMA等对抗样本攻击算法,提供tensorflow,keras,pytorch接口支持,可加载预训练模型。 https://github.com/tensorflow/cleverhans.git Foolbox 用来产生对抗样本的python工具箱,支持Pytorch、TensorFlow、Keras https://github.com/bethgelab/foolbox.git AdvBox 百度提供的AI模型安全工具箱,可产生对抗样本 https:/