当前位置:网站首页>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
])
边栏推荐
- 阿里前辈给我推荐的软件测试人员必读书籍(附电子书),让我受益匪浅...
- Illustrator tutorial, how to recolor artwork in illustrator?
- A Zhu and Xu Baobao's high-rise game - difference
- Junda technology is applicable to "kestar" intelligent precision air conditioning network monitoring
- How to determine the sample size of an inspection lot in SAP QM's initial sampling strategy?
- Detect current system language
- Data visualization - broken line area chart
- [tutorial] detailed explanation of the page rules parameter settings of cloudflare CDN
- Introduction to system mode development of rouya wechat mall
- Solve the cvxpy error the solver GLPK_ MI is not installed
猜你喜欢

Why my order by create_ Time ASC becomes order by ASC

Data visualization diagram microblog forwarding diagram

leetcode:210. 课程表 II

Access control system based on RFID

It has been engaged in the functional test of 10K to the test development of 40W annual salary for 5 years, and spent 7 days sorting out the super comprehensive learning route

Design and practice of Hudi bucket index in byte skipping

At the same time, do the test. Others have been paid 20W a year. Why are you still working hard to reach 10K a month?

Is it really possible to find a testing job with a monthly income of more than 10000 without a degree and self-study software testing?

半路自学测试成功转行,第一份测试工作就拿10k

竣达技术丨适用于“科士达”智能精密空调网络监控
随机推荐
多机房动环状态网络触摸屏监控解决方案
机器学习资料汇总
循环插入excel某一列,以及多列之和
torch. Finfo function
EditText control starts from the upper left corner
作用域和作用域链
初步了解认识正则表达式(Regex)
Technology to understand
go --- 监控文件变化
China hydraulic press market trend report, technical innovation and market forecast
Market trend report, technical innovation and market forecast of hydraulic torque wrench in China
Successful transition from self-study test halfway, 10K for the first test
半路自学测试成功转行,第一份测试工作就拿10k
lintcode:127 · 拓扑排序
Research Report on market supply and demand and strategy of hydraulic operating table industry in China
JS deep and shallow copy
Lua pattern matching
QT pro file configuration ffmpeg macro
Transaction code qs28 of SAP QM preliminary level
设计规则检查约束(set_max_transition、set_max_capacitance)