当前位置: 首页 > news >正文

pytorch的pixel_shuffle转tflite文件

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(input=input[:,:, :,i*depth:(i+1)*depth], block_size=upscale_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.conv=nn.Conv2d(in_channels=3,out_channels=12,kernel_size=3,stride=2,padding=1)def forward(self, input):x=self.conv(input)out=torch.pixel_shuffle(x,2)return out

可视化出来

利用tf_pixelshuffle转出来的结果:

利用pixel_shuffle转出来的结果:

http://www.lryc.cn/news/177827.html

相关文章:

  • sentinel-dashboard-1.8.0.jar开机自启动脚本
  • c++堆排序-建堆-插入-删除-排序
  • 使用代理后pip install 出现ssl错误
  • 护眼灯什么价位的好?最具性价比的护眼台灯推荐
  • vue event bus 事件总线
  • 深信服云桌面用户忘记密码后的处理
  • Cocos Creator3.8 实战问题(一)cocos creator prefab 无法显示内容
  • 朴素贝叶斯深度解码:从原理到深度学习应用
  • RUST 每日一省:闭包
  • Ubuntu下文件的解压缩操作:常用zip和unzip
  • Linux学习第22天:Linux中断驱动开发(一): 突如其来
  • IDEA 2019 Springboot 3.1.3 运行异常
  • 【JAVA】飞机大战
  • Midjourney 生成油画技巧
  • 26559-2021 机械式停车设备 分类
  • xxe攻击(XML外部实体)
  • 大数据-hadoop
  • 容器启动报错
  • 求生之路2服务器搭建插件安装及详细的游戏参数配置教程linux
  • IntelliJ IDEA 左侧Commit栏不见了
  • 使用自功率谱、互功率谱估计滤波器幅频特性
  • 51单片机光照强度检测自动路灯开关仿真( proteus仿真+程序+报告+讲解视频)
  • socat管理haproxy配置
  • Linux发行版X华为鲲鹏openEuler
  • 计算机网络相关知识点
  • Jmeter+Ant+Git+Jenkins持续集成介绍
  • Spring Cloud Gateway实战WebFlux解析请求体及抛出指定错误代码和信息
  • Servlet开发-通过代码案例熟悉HttpServletRequest类
  • 离线环境harbor 搭建及使用
  • 华为杯数学建模比赛经验分享