Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hhh #3

Open
meteorshowers opened this issue Sep 23, 2021 · 1 comment
Open

hhh #3

meteorshowers opened this issue Sep 23, 2021 · 1 comment

Comments

@meteorshowers
Copy link

meteorshowers commented Sep 23, 2021

`def conv_down(inp,oup):
return nn.Sequential(
nn.Conv2d(inp,oup,4,stride=2,padding=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(oup,oup,3,stride=1,padding=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True)
)
def conv_up(inp,oup):
return nn.Sequential(
nn.ConvTranspose2d(inp,oup,2,stride=2,padding=0),
nn.LeakyReLU(negative_slope=0.2, inplace=True)
)
def conv_merge(inp,oup):
return nn.Sequential(
nn.Conv2d(inp, oup, 1, stride=1, padding=0),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(oup, oup, 3, stride=1, padding=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(oup, oup, 3, stride=1, padding=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True)
)
def conv(inp,oup):
return nn.Sequential(
nn.Conv2d(inp, oup, 3, stride=1, padding=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
)

def convbn(in_channel, out_channel, kernel_size, stride, pad, dilation):
#no bn
return nn.Sequential(
nn.Conv2d(
in_channel,
out_channel,
kernel_size=kernel_size,
stride=stride,
padding=dilation if dilation > 1 else pad,
dilation=dilation))

class BasicBlock(nn.Module):
"""ResNet BasicBlock"""
expansion = 1

def __init__(self, c1, c2, s, downsample, p, d):
    super(BasicBlock, self).__init__()
    self.conv1 = nn.Sequential(convbn(c1, c2, 3, s, p, d), nn.LeakyReLU(negative_slope=0.2, inplace=True))
    self.conv2 = convbn(c2, c2, 3, 1, p, d)
    self.stride = s

def forward(self, x):
    out = self.conv1(x)
    out = self.conv2(out)
    out += x
    return out

class Unet(nn.Module):
def init(self):
super().init()
#16,16,24,24,32
self.conv1 = conv(3, 16)
self.down1 = conv_down(16,16)
self.down2 = conv_down(16,24)
self.down3 = conv_down(24,24)
self.down4 = nn.Sequential(conv_down(24,32),
nn.Conv2d(32, 32, 3, stride=1, padding=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(32, 32, 3, stride=1, padding=1),
nn.LeakyReLU(negative_slope=0.2, inplace=True)
)

    self.up4 = conv_up(32,24)
    self.up3 = conv_up(24,24)
    self.up2 = conv_up(24,16)
    self.up1 = conv_up(16,16)

    self.merge4 = conv_merge(24+24,24)
    self.merge3 = conv_merge(24+24,24)
    self.merge2 = conv_merge(16+16,16)
    self.merge1 = conv_merge(16+16,16)




def forward(self, x):
    x_down = self.conv1(x)               #16*320*960
    x_down1 = self.down1(x_down)         #16*160*480
    x_down2 = self.down2(x_down1)   #24*96*320
    x_down3 = self.down3(x_down2)   #24*48*160
    x_down4 = self.down4(x_down3)   #32*24*80

    x_up4 = self.up4(x_down4)
    
    if x_up4.size() != x_down3.size():
        x_up4 = x_up4[:,:,:-1,:]
    x_up4 = self.merge4(torch.cat((x_down3,x_up4),dim=1)) #24*48*160

    x_up3 = self.up3(x_up4)
    if x_up3.size() != x_down2.size():
        x_up3 = x_up3[:,:,:-1,:]

    x_up3 = self.merge3(torch.cat((x_down2,x_up3),dim=1)) #24*96*320


    x_up2 = self.up2(x_up3)

    if x_up2.size() != x_down1.size():
        x_up2 = x_up2[:,:,:-1,:]

    x_up2 = self.merge2(torch.cat((x_down1,x_up2),dim=1)) #16*192*640


    x_up1 = self.up1(x_up2)
    if x_up1.size() != x_down.size():
        x_up1 = x_up1[:,:,:-1,:]


    x_up1 = self.merge1(torch.cat((x_down,x_up1),dim=1)) #16*384*1280


    return [x_down4,x_up4,x_up3,x_up2,x_up1]

class Initial(nn.Module):
#share the 4*4 conv
def init(self,inp,maxdisp=256,scale=8):
super().init()
self.maxdisp = maxdisp
self.scale = scale
# self.pad = torch.nn.ZeroPad2d(padding=(0, 3, 0, 0))
self.pad = torch.nn.ZeroPad2d(padding=(1, 2, 0, 0))
self.conv4_4 = nn.Conv2d(inp,16,4,stride=4)
self.conv4_4.weight = nn.Parameter(torch.randn(16,inp,4,4).cuda())
self.conv4_4.bias = nn.Parameter(torch.randn(16).cuda())
self.post = nn.Sequential(
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(16,16,1,stride=1,padding=0),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
)

    # self.post = nn.Sequential(
    #     nn.LeakyReLU(negative_slope=0.2, inplace=True),
    #     nn.Conv2d(16,16,1,stride=1,padding=0),
    #     nn.LeakyReLU(negative_slope=0.2, inplace=True),
    #     nn.Conv2d(16,16,1,stride=1,padding=0),
    #     nn.LeakyReLU(negative_slope=0.2, inplace=True),
    # )
    if scale == 16 or scale==8:
        self.descriptor = nn.Sequential(
            nn.Conv2d(17,13,1),
            nn.LeakyReLU(negative_slope=0.2, inplace=True)
        )
    elif scale == 4:
        self.descriptor = nn.Sequential(
            nn.Conv2d(33, 13, 1),
            nn.LeakyReLU(negative_slope=0.2, inplace=True)
        )
    elif scale == 2 or scale==1:
        self.descriptor = nn.Sequential(
            nn.Conv2d(25, 13, 1),
            nn.LeakyReLU(negative_slope=0.2, inplace=True)
        )




def forward(self, fea_l,fea_r,fea_cat=None):
    # print(fea_l.size(), fea_r.size())

    fea_l = self.post(self.conv4_4(fea_l))
    self.conv4_4.stride=[4,1]
    # fea_r = self.post(self.conv4_4(self.pad(fea_r)))
    fea_r = self.post(self.conv4_4(fea_r))
    # print(fea_l.size(), fea_r.size())
    #还原,不然下个数据会有问题
    self.conv4_4.stride=[4,4]

    maxdisp = self.maxdisp //(self.scale)
    #看下数值是不是都是正数:
    #print(fea_l.shape,(fea_l<0).sum(),(fea_r<0).sum(),fea_l.mean(),fea_r.mean()) 
    
    cost,d_init ,cost_d_init = calc_init_disp(fea_l,fea_r,maxdisp)
    d_init = d_init.squeeze(1)
    if self.scale==16 or self.scale==8:
        p_init = self.descriptor(torch.cat((cost_d_init,fea_l),1))
    else:
        # print(cost_d_init.size(), fea_cat.size())
        p_init = self.descriptor(torch.cat((cost_d_init, fea_cat), 1))


            #1*24*12*40  1*24*12*160  1*13*12*40  1*d*12*40
    return [fea_l,fea_r,p_init,d_init,cost,cost_d_init]`
@meteorshowers
Copy link
Author

meteorshowers commented Dec 4, 2021

`#coding:utf-8
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
#matplot模仿matlab,两者绘图函数大致相同.numpy用于创建数组,且内置计算函数很强
from mpl_toolkits.mplot3d import Axes3D
mycolormap = plt.get_cmap('plasma')

xyzvalues = np.random.choice(range(2,100), size=(10,10,10))

xyzvalues = np.zeros((10,10,20))
xyzvalues[1,0:,0] = 1

xyzvalues[0,0:,0] = 0.1
#数组大小:101010, 数值范围0~9.

xyzminvalue=xyzvalues.min()
xyzmaxvalue=xyzvalues.max()
#根据三维数组中的最大和最小值来定义每个数值的相对强度,范围0~1.0

relativevalue=np.zeros((10,10,20))
#色温强度矩阵大小与xyz测试数组大小一致

for i in range(0,relativevalue.shape[0]):
for j in range(0,relativevalue.shape[1]):
for k in range(0,relativevalue.shape[2]):
relativevalue[i][j][k]=round(xyzvalues[i][j][k]/xyzmaxvalue,1)
#round函数取小数点后1位

colorsvalues = np.empty(xyzvalues.shape, dtype=object)
alpha=0.2
#透明度,视显示效果决定

for i in range(0,relativevalue.shape[0]):
for j in range(0,relativevalue.shape[1]):
for k in range(0,relativevalue.shape[2]):
tempc=mycolormap(relativevalue[i][j][k])
#tempc为tuple变量,存储当前数值的颜色值(R,G,B,Alpha)

        colorreal=(tempc[0],tempc[1],tempc[2],alpha)

#tuple为不可变数据类型,所以替换自定义alpha值时需要重新定义

        colorsvalues[i][j][k]=colorreal

#最终每个数值所对应的颜色

fig = plt.figure(figsize=(7, 4.5))

Make a figure and axes with dimensions as desired.

#需要注意的是,3Dplot不支持设置xyz的比例尺相同,这就带来了一些麻烦:
#保存图片时长宽比例受限,这个问题以后再做说明解决

ax = fig.gca(projection='3d')

#ax.voxels(xyzvalues, facecolors=colorsvalues, edgecolor='k',shade=False,)
ax.voxels(xyzvalues, facecolors=colorsvalues, edgecolor=None)
#关键函数voxels:用以无缝绘制每个像素格

ax.set_xlabel('X');ax.set_ylabel('Y');ax.set_zlabel('Z')

ax.set_title('3D Voxel Map')

#新建区域ax1,用以额外绘制colorbar
#ref:https://matplotlib.org/examples/api/colorbar_only.html
#位置为figure的百分比,从figure 0%的位置开始绘制, 高是figure的80%
left, bottom, width, height = 0.1, 0.1, 0.05, 0.8

#获得绘制的句柄

ax1 = fig.add_axes([left, bottom, width, height])

# Set the colormap and norm to correspond to the data for which

# the colorbar will be used.

cmap = mpl.cm.plasma

#colormap与绘制voxel图保持一致

norm = mpl.colors.Normalize(vmin=xyzminvalue, vmax=xyzmaxvalue)

#色温colorbar的数值范围可选择实际xyz数组中的数值范围(其实不应该从0开始)

cb1 = mpl.colorbar.ColorbarBase(ax1, cmap=cmap,

norm=norm,

orientation='vertical')

cb1.set_label('Units')

plt.axis('off')
plt.show()
print('end')`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant