当前位置:网站首页>Pytoch implements tf Functions of the gather() function
Pytoch implements tf Functions of the gather() function
2022-07-05 12:26:00 【Dongcheng West que】
Purpose :
Large size medical image [3, 897, 1196] In a batch in
Divide by loop [12, 3, 299, 299], After superposition and segmentation, it is [24, 3, 299, 299]
There is a problem with the order on the left side of the above figure , Now we need some operations to convert it to the above figure , Write code .
for i, (inputs, labels,img_path) in enumerate(train_loader):
D_L = len(inputs)
# labels = labels.to(device)
img_tensor = Variable(inputs.to(device), volatile=True)
crop_img = torch.zeros((4, 3))
print(" The first ",i," individual ")
# Big picture [3, 897, 1196] Divided into [12, 3, 299, 299]
for j in range(3):
for ii in range(4):
if ii == 0 and j == 0:
crop_img = img_tensor[:, :, 299 * j:299 * (j + 1), 299 * ii:299 * (ii + 1)]
else:
crop_img = torch.cat(
(crop_img, img_tensor[:, :, 299 * j:299 * (j + 1), 299 * ii:299 * (ii + 1)]), 0)
print("crop_img:", crop_img.shape)
# Rearrange by index , take [24*batch_size,3,299,299], Arrange the pictures in order of large size
trans_image = torch.zeros([12, 3, 299, 299]).to(device)
for ind in range(D_L):
index = np.arange(ind, 12 * D_L, D_L)
index = torch.from_numpy(index).long().to(device)
index = index.unsqueeze(1).unsqueeze(1).unsqueeze(1)
index = index.expand(12, 3, 299, 299)
t = torch.gather(crop_img, 0, index)
if ind == 0:
trans_image = t
else:
trans_image = torch.cat([trans_image, t], 0)
print("233", trans_image.shape)
Tensorflow Medium gather And pytorch Medium gather The functions of are different , If pytorch There are and tf. Gather Functions with the same function can be used directly .
tf.gather()
A one-dimensional
Multi dimension by row
Multidimensional by column
tf.gather_nd()
torch.gather(input, dim, index, out=None)
effect : Collect the value of the specified position of the specific dimension entered
input(tensor): Number to be manipulated . Let's set its dimension as (x1, x2, …, xn)
dim(int): Dimensions to be operated .
index(LongTensor): How to input To operate . Its dimensions are limited , For example, when dim=i when ,index The dimensions are (x1, x2, …y, …,xn), As a general input Of the i The size of the dimension is changed to y, And to satisfy y>=1( Except for i Dimensions other than dimensions , Size and input bring into correspondence with ).
out: Pay attention to the output and index The dimensions of are consistent
Use torch.gather() Construction and tf.gather() Functions with the same function
According to the first 0 dimension
According to the first 1 dimension
According to the first 2 dimension
torch.gather() Realization tf.gather()
边栏推荐
- Third party payment interface design
- Anaconda creates a virtual environment and installs pytorch
- Seven ways to achieve vertical centering
- ZABBIX agent2 monitors mongodb nodes, clusters and templates (official blog)
- ABAP table lookup program
- Course design of compilation principle --- formula calculator (a simple calculator with interface developed based on QT)
- Acid transaction theory
- 7月华清学习-1
- 只是巧合?苹果 iOS16 的神秘技术竟然与中国企业 5 年前产品一致!
- 嵌入式软件架构设计-消息交互
猜你喜欢
MySQL storage engine
[email protected] (using password"/>
Solve the error 1045 of Navicat creating local connection -access denied for user [email protected] (using password
Take you hand in hand to develop a service monitoring component
MySQL transaction
Master-slave mode of redis cluster
Knowledge representation (KR)
Interviewer: is acid fully guaranteed for redis transactions?
ZABBIX ODBC database monitoring
Select drop-down box realizes three-level linkage of provinces and cities in China
July Huaqing learning-1
随机推荐
Learn the memory management of JVM 02 - memory allocation of JVM
MySQL regular expression
Understanding the architecture type of mobile CPU
Video networkstate property
Implementing Yang Hui triangle with cyclic queue C language
Understand kotlin from the perspective of an architect
MySQL storage engine
ZABBIX 5.0 - LNMP environment compilation and installation
Intern position selection and simplified career development planning in Internet companies
C language structure is initialized as a function parameter
语义分割实验:Unet网络/MSRC2数据集
Constructing expression binary tree with prefix expression
GPS數據格式轉換[通俗易懂]
PXE startup configuration and principle
Tabbar configuration at the bottom of wechat applet
How can beginners learn flutter efficiently?
Yum only downloads the RPM package of the software to the specified directory without installing it
Four operations and derivative operations of MATLAB polynomials
Check the debug port information in rancher and do idea remote JVM debug
Matlab imoverlay function (burn binary mask into two-dimensional image)