当前位置:网站首页>pytorch transforms. Use of lambda
pytorch transforms. Use of lambda
2022-06-12 20:57:00 【Human high quality Algorithm Engineer】
When you want to set the image transforms strategy , Such as :
from torchvision import transforms as T
normalize = T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
data_transforms = {
'train': T.Compose([
T.RandomResizedCrop(224), # Capture from picture center
T.RandomHorizontalFlip(), # Randomly flip a given... Horizontally PIL.Image, The turnover probability is 0.5
T.ToTensor(), # Turn into Tensor Format , The size range is [0,1]
normalize
]),
'val': T.Compose([
T.Resize(256), # Resize
T.CenterCrop(224),
T.ToTensor(),
normalize
]),
}
But sometimes official methods don't meet your needs , At this time, you need to customize your own transform Strategy
The way is to use transforms.Lambda
Illustrate with examples :
For example, when we want to capture an image , But I don't want to intercept at random , Instead, you want to intercept at a designated location
Then you need to customize an interception function , And then use transforms.Lambda Just encapsulate it , Such as :
# coding:utf-8
from torchvision import transforms as T
def __crop(img, pos, size):
""" :param img: Input image :param pos: Location of image capture , The type is tuple , contain (x, y) :param size: Size of image capture :return: Returns the captured image """
ow, oh = img.size
x1, y1 = pos
tw = th = size
# There is enough size to intercept
# img.crop Coordinate representation (left, upper, right, lower)
if (ow > tw or oh > th):
return img.crop((x1, y1, x1+tw, y1+th))
return img
# And then use transforms.Lambda Encapsulate it as transforms Strategy
# Then define new transforms by
normalize = T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
data_transforms = T.Compose([
T.Lambda(lambda img: __crop(img, (5,5), 224)),
T.RandomHorizontalFlip(), # Randomly flip a given... Horizontally PIL.Image, The turnover probability is 0.5
T.ToTensor(), # Turn into Tensor Format , The size range is [0,1]
normalize
])
边栏推荐
- New product release Junda intelligent integrated environmental monitoring terminal
- InRelease: 由于没有公钥,无法验证下列签名: NO_PUBKEY EB3E94ADBE1229CF
- Research Report on market supply and demand and strategy of China's hydraulic hammer industry
- Minio client (MC command) implements data migration
- How do testers plan for their future? To achieve 25K in 2 years?
- How to improve communication efficiency during home office | community essay solicitation
- Simplest ALV template
- How to download putty using alicloud image?
- 机器学习资料汇总
- EditText control starts from the upper left corner
猜你喜欢

Lightroom 大使系列:用 Meg Loeks 捕捉怀旧之情

What's a good gift for the goddess Festival? Gift recommendation for the goddess Festival on March 8

Scala基础语法入门(三)Scala中的各种运算符

Algorinote_ 2_ Main theorem and Akra bazzi theorem

Data visualization diagram microblog forwarding diagram

阿里前辈给我推荐的软件测试人员必读书籍(附电子书),让我受益匪浅...

解决cvxpy报错The solver GLPK_MI is not installed

Listener in JSP

(11) Image frequency domain filtering with OpenCV

leetcode:207. 课程表
随机推荐
跳槽前恶补面试题,金三成功上岸腾讯,拿到30k的测开offer
Why my order by create_ Time ASC becomes order by ASC
Junda technology is applicable to "kestar" intelligent precision air conditioning network monitoring
函数的了解
InRelease: 由于没有公钥,无法验证下列签名: NO_PUBKEY EB3E94ADBE1229CF
Composer version degradation
A Zhu and Xu Baobao's high-rise game - difference
How do testers plan for their future? To achieve 25K in 2 years?
Allegro Xile technology, a developer of distributed cloud services, received millions of dollars of angel round financing and was independently invested by Yaotu capital
Analysis of test questions in Chapter 13 of PMP preparation
大小端转换
做自媒体视频,友好的新媒体运营必备app分享
Deploy etcd cluster in static pod mode
阿里前辈给我推荐的软件测试人员必读书籍(附电子书),让我受益匪浅...
SAP QM preliminary - cannot assign a sampling policy to an inspection characteristic when maintaining an inspection plan by executing transaction code qp02?
JS中如何实现重载
检测当前系统语言
字符串基础知识
解决cvxpy报错The solver GLPK_MI is not installed
逐向双碳:东数西算中的绿色需求与竞争焦点