PyTorch自定义Transform模块(torchvision)-深度学习

torchvision的使用(transforms用法介绍) 介绍 torchvision是pytorch的一个图形库,它服务于PyTorch深度学习框架的,主要用来构建计算机视觉模型。torchvision.transforms主要是用于常见的一些图形变换。以下是torchvision的构成: torchvision.datasets: 一些加载数据的函数及常用的数据集接口; torchvision.models: 包含常用的模型结构(含预训练模型),例如AlexNet、VGG、ResNet等; torchvision.transforms: 常用的图片变换,例如 阅读详情

版权归属:

更多关注:

1 问题

PyTorch实现image-to-image时,需要对image pair进行相同参数的transform(i.e. sync),torchvision已经实现了多种针对single image的transform,但是在使用时input image与target image的transform不一致,上网查阅没有发现好的方法,因此只能自己实现。查看了TorchVision的Transform代码,自己也是可以仿照实现。

2 Random Scale and Crop原理

在这里插入图片描述
将原始图片放大scale=r倍,为了满足crop后的图片中间的蓝色区域没有被分割遗漏,就需要满足crop size > min crop size,进而得到r > 2n/(m+n)。
如果将n=256, m=172带入,r大概1.2左右。反过来如果令r在[1.0, 1.2]之间那么m=170.6,实际中取m=170 or 172。

3 实现代码

Talk is cheap, show me the code.

#coding=utf-8
import torchvision.transforms as transforms
import torchvision.transforms.functional as F
import random
import numpy as np
from PIL import Image
import numbers
import collections
import sys

if sys.version_info < (3, 3):
    Sequence = collections.Sequence
    Iterable = collections.Iterable
else:
    Sequence = collections.abc.Sequence
    Iterable = collections.abc.Iterable



def get_sync_transform(opt):
    transform_list = []

    osize = [opt.loadSize, opt.loadSize]
    transform_list.append(Resize(osize, Image.BILINEAR))
    transform_list.append(RandomScaleCrop())

    if opt.isTrain and not opt.no_flip:
        transform_list.append(transforms.RandomHorizontalFlip())

    transform_list += [ToTensor(),
                       Normalize(<
torchvision中Transform的normalize参数含义 比如原来的tensor是三个维度的,值在0到1之间,经过以下变换之后就到了-1到1区间。 transforms.Normalize((.5,.5,.5),(.5,.5,.5)) transforms的源码中解释: input[channel] = (input[channel] - mean[channel]) / std[channel] 假设你数据的范围是图片的数据范围四[0,1],那么如果... 阅读详情

相关推荐

pytorch中的torchvision.transforms模块详解

torchvision.transforms torchvision.transforms是pytorch中的图像预处理包,包含了很多种对图像数据进行变换的函数,这些都是在我们进行图像数据读入步骤中必不可少的。 data_transforms = transforms.Compose([ transforms.RandomResizedCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTens

qq_28505705的博客 2108

pytorch中torchvision数据集的使用——CIFAR-10dataset数据集

pytorch中torchvision数据集的使用

m0_54291751的博客 5377

PyTorch与torchvision版本匹配:原理、查询与安装全指南

在深度学习与计算机视觉开发中,环境配置的稳定性是项目成功的基石。其核心原理在于确保核心框架与扩展工具包之间的二进制兼容性和API一致性。PyTorch作为主流深度学习框架,其官方视觉库torchvision高度依赖特定版本的PyTorch底层接口。版本不匹配会直接导致二进制扩展模块链接错误、API调用失败或CUDA运行时冲突,严重影响开发效率。从工程实践角度看,这涉及ABI兼容性、Python API演变及CUDA工具链绑定等关键技术点。尤其在模型训练、部署及复现等应用场景中,精确的版本对应是保证代码可复现

weixin_30457551的博客 517

【Python深度学习】零基础掌握Pytorch转换Transforms

通过这篇教程学习了如何使用ToTensor()和来处理的图像数据。就像学习一门新语言一样,一开始可能会觉得有点复杂,但是一旦掌握了基本的转换技巧,就能更轻松地与深度学习模型进行交流了。ToTensor()帮把图像转换成了计算机更容易理解的张量格式,就好比把文字从书本上转录到电脑上。而则更像是一个多功能的翻译器,无论有多奇怪的要求,它都能帮实现,比如把彩色图变成黑白图,或者进行其他更复杂的操作。掌握这些数据转换的技巧,对于学习和应用深度学习非常重要。

Mr数据杨 752

pytorch中的transforms模块实例详解

今天小编就为大家分享一篇pytorch中的transforms模块实例详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

手把手教你搭建自己的 API 中转站

本文介绍了如何通过开源工具CLIProxyAPI(CPA)搭建本地AI模型代理服务,解决多工具切换、海外API访问困难等问题。CPA可将OpenAI、Gemini等不同服务的API统一封装,用户只需配置一次账号信息,所有客户端即可通过标准接口访问。文章详细讲解了CPA的下载安装、配置修改、账号导入及客户端连接方法,并强调其经济性、可控性和集中管理优势。通过自建中转站,用户可避免第三方服务的质量风险,实现多账号负载均衡,构建个人AI资源池。最后提供了网络配置等实用建议,帮助开发者高效使用AI编程工具。

DongJiYong_20的博客 1830

深入解析XTEA加密算法的核心机制与优化策略

本文深入解析XTEA加密算法的核心机制与优化策略,详细介绍了XTEA在嵌入式系统和资源受限环境中的优势,包括其高效的加解密速度和低资源消耗。文章还探讨了XTEA的轮函数设计、关键参数优化策略以及实战应用中的性能对比,为开发者提供了安全增强方案和优化技巧。

k8l9m0n的博客 243

Jetson平台XDMA PCIe驱动安装与测试全指南

本文提供了一份在NVIDIA Jetson平台上安装与测试Xilinx FPGA XDMA PCIe驱动的完整指南。详细阐述了驱动作为高速DMA引擎的核心作用,并逐步讲解了从环境准备、驱动编译、内核模块加载到实际数据传输测试的全过程。内容涵盖常见问题排查与性能优化建议,帮助开发者实现Jetson与FPGA间的高速数据通信,适用于自动驾驶、工业视觉等需要异构计算的应用场景。

weixin_29196315的博客 527

使用Docker搭建MaxKB智能体平台

【代码】使用Docker搭建MaxKB智能体平台。

janthinasnail的博客 711

瑞芯微RK3506开发板DSM音频开发避坑指南:从硬件改接到内核配置全流程

本文详细解析了在瑞芯微RK3506开发板上进行DSM音频开发的完整流程与关键避坑点。内容涵盖从硬件改造(如电阻焊接)、内核驱动配置(DTS参数、补丁应用)到系统调试与波形分析的全过程,旨在帮助嵌入式开发者高效实现高信噪比、低成本的音频方案,避免常见错误。

weixin_29164091的博客 408

AutoSAR实验视频讲解:相关资料下载

AutoSAR实验视频讲解:闪烁一颗LED实验【相关资料下载】 总目录 为了方便大家自己的实验顺利进行,这里我们准备了一些实验中可能用到的资料供大家使用 介绍 这里主要是三个文件: MyApp_Core.0是我们的用户代码,在工程集成那集将会讲解 McalConfig.arxml就是我们的Mcal导出文件,用于导入到DaVinci中生成代码 CAN.dbc文件是我们的通信矩阵文件 以上几个文件都是简单的配置,不具有实际项目的实用性,大家仅供参考 // 顺便吐槽一句:百度网盘的吃相真的难看 下载连接

雪云飞星的博客 6668

网络流量异常信息分析方法研究

本课题针对网络安全中网络流量异常的分析方法进行研究,提出一种基于机器学习和深度学习的异常检测算法。该算法采用卷积神经网络(CNN)和长短时记忆神经网络(LSTM)结合的方式,通过训练得到二分类模型,从而实现对网络流量异常的检测。随着互联网的飞速发展,网络攻击事件日益增多,网络安全问题成为了亟待解决的问题之一。常见的网络攻击手段包括 DDoS 攻击、SQL 注入、XSS 攻击等,这些攻击手段可能导致网络系统发生异常,给网络运维、数据安全带来极大的挑战。

肥晨开发的学习之路 1037

JAX/JAXlib GPU 加速版本安装教程(CUDA)

本文介绍了在Windows WSL2+Ubuntu22.04环境下配置JAX/JAXlib GPU加速版本的详细步骤。使用RTX 4060显卡,安装CUDA 12.4和cuDNN 8.9.7驱动,通过Miniconda创建Python 3.9虚拟环境。具体步骤包括:安装Miniconda、配置CUDA环境变量、安装cuDNN驱动、下载并安装JAX/JAXlib。最后通过测试代码验证GPU加速功能,成功检测到GPU并完成矩阵运算。整个过程配有详细命令和截图说明,适合需要搭建JAX GPU开发环境的用户参考。

猿小白的博客 627

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

上一篇: 一个RandomCrop方案-深度学习
下一篇: 一个神奇的优快云繁体版盗版网站 - 台部落(https://www.twblogs.net/u/5b8f102c2b71771883499d6b)
chanhal
博客等级 码龄9年 30粉丝 28原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值