网站内容 内链,网站建设电话销售工作总结,网站设计基本流程,如何用云服务器做网站torch.pixel_shuffle()是pytorch里面上采样比较常用的方法#xff0c;但是和tensoflow的depth_to_space不是完全一样的#xff0c;虽然看起来功能很像#xff0c;但是细微是有差异的
def tf_pixelshuffle(input, upscale_factor):temp []depth upscale_factor *upscale_f…torch.pixel_shuffle()是pytorch里面上采样比较常用的方法但是和tensoflow的depth_to_space不是完全一样的虽然看起来功能很像但是细微是有差异的
def tf_pixelshuffle(input, upscale_factor):temp []depth upscale_factor *upscale_factorchannels input.shape.as_list()[-1] // depthfor i in range(channels):out_ tf.nn.depth_to_space(inputinput[:,:, :,i*depth:(i1)*depth], block_sizeupscale_factor)temp.append(out_)out tf.concat(temp, axis-1)return out
因为有人发现在单通道的时候是depth_to_space和pixel_shuffle结果是一样的所以拆分出来计算好在合并就行这样速度基本上没有增加多少亲测速度也是很快的比从头开始实现pixel_shuffle是快非常多的。
如果使用这样的从头开始实现转出来的tflite是没法运行在手机上面的因为tf.transpose的维度太多了tflite在手机上不支持6个维度的transpose的因为超过5个维度就会产生flex层flex层是不被支持的。
def pixel_shuffle(x, upscale_factor):batch_size, height, width, channels x.shapechannel_split channels // (upscale_factor ** 2)# Reshape the input tensor to split channelsx tf.reshape(x, (batch_size, height, width, upscale_factor, upscale_factor, channel_split))# Transpose and reshape to get the pixel shuffled outputx tf.transpose(x, perm[0, 1, 3, 2, 4, 5])x tf.reshape(x, (batch_size, height * upscale_factor, width * upscale_factor, channel_split))return x
下面就测试一下
新建pytorch模型
import torch
import torch.nn as nnclass Net(nn.Module):def __init__(self):super().__init__()self.convnn.Conv2d(in_channels3,out_channels12,kernel_size3,stride2,padding1)def forward(self, input):xself.conv(input)outtorch.pixel_shuffle(x,2)return out
可视化出来 利用tf_pixelshuffle转出来的结果 利用pixel_shuffle转出来的结果