当前位置:网站首页>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()

边栏推荐
- Linux Installation and deployment lamp (apache+mysql+php)
- How to clear floating?
- Open3d mesh (surface) coloring
- 语义分割实验:Unet网络/MSRC2数据集
- Automated test lifecycle
- Codeforces Round #804 (Div. 2)
- 什么是数字化存在?数字化转型要先从数字化存在开始
- PXE启动配置及原理
- Redis highly available sentinel mechanism
- Course design of compilation principle --- formula calculator (a simple calculator with interface developed based on QT)
猜你喜欢

Master-slave mode of redis cluster

Master the new features of fluent 2.10
![[superhard core] is the core technology of redis](/img/5e/d6438f09031c2acbea17441c316a2b.jpg)
[superhard core] is the core technology of redis

7月华清学习-1

MySQL index (1)
The survey shows that traditional data security tools cannot resist blackmail software attacks in 60% of cases

Hexadecimal conversion summary

What is digital existence? Digital transformation starts with digital existence

Detailed steps for upgrading window mysql5.5 to 5.7.36

Tabbar configuration at the bottom of wechat applet
随机推荐
[pytorch pre training model modification, addition and deletion of specific layers]
Recyclerview paging slide
Reinforcement learning - learning notes 3 | strategic learning
The solution of outputting 64 bits from printf format%lld of cross platform (32bit and 64bit)
Time tools
Redis clean cache
Summary of C language learning problems (VS)
Learning JVM garbage collection 06 - memory set and card table (hotspot)
Anaconda creates a virtual environment and installs pytorch
The evolution of mobile cross platform technology
How to design an interface?
MySQL basic operation -dql
Cypher syntax of neo4j graph database
Handwriting blocking queue: condition + lock
Array cyclic shift problem
GPS數據格式轉換[通俗易懂]
End to end neural network
ZABBIX monitors mongodb templates and configuration operations
Course design of compilation principle --- formula calculator (a simple calculator with interface developed based on QT)
图像超分实验:SRCNN/FSRCNN