获取文件夹下所有文件:

def get_all(cwd, result):get_dir = os.listdir(cwd)for i in get_dir:sub_dir = os.path.join(cwd,i)if os.path.isdir(sub_dir):get_all(sub_dir, result)else:result.append(cwd+"\\"+i)return result

保存数据为二进制:

data = []for name, param in net.named_parameters():print("name:", name, " param:", param.size())pa = param.reshape(-1).to(device_cpu)numpy_param = pa.detach().numpy()print("param:", len(numpy_param))data += list(numpy_param)
print("len data:", len(data))
f = open('net.bin','wb+')
data_struct = struct.pack(('%df' % len(data)), *data)
f.write(data_struct)
f.close()

list 数据写入文件  注:line结尾加 "\n"

def write_line(file_name, lines):if file_name == "":returnwith open(file_name, "a+") as fs:for line in lines:fs.writelines(line)fs.close()

读取数据到list

def read_file(data_file, mode='more'):try:with open(data_file, 'r') as f:if mode == 'one':output = f.read()return outputelif mode == 'more':output = f.readlines()# return map(str.strip, output)return outputelse:return list()except IOError:return list()

rect 缩放:

def get_zoom_rect(rect, width, height, zoom):x, y = rect[0], rect[1]w = rect[2] - rect[0]h = rect[3] - rect[1]w1 = min(w*zoom, width-x)h1 = min(h*zoom, height-y)x1 = min(max(x - (w1 - w)/2, 0), width-w1)y1 = min(max(y - (h1 - h)/2, 0), height-h1)#print("rect:", rect, " new:", [x1, y1, x1+w1, y1+h1])return [x1, y1, x1+w1, y1+h1]

fuse BatchNorm 网络

import torch
import torch.nn as nn
import torchvision as tvclass DummyModule(nn.Module):def __init__(self):super(DummyModule, self).__init__()def forward(self, x):# print("Dummy, Dummy.")return xdef fuse(conv, bn):w = conv.weightmean = bn.running_meanvar_sqrt = torch.sqrt(bn.running_var + bn.eps)beta = bn.weightgamma = bn.biasif conv.bias is not None:b = conv.biaselse:b = mean.new_zeros(mean.shape)w = w * (beta / var_sqrt).reshape([conv.out_channels, 1, 1, 1])b = (b - mean)/var_sqrt * beta + gammafused_conv = nn.Conv2d(conv.in_channels,conv.out_channels,conv.kernel_size,conv.stride,conv.padding,bias=True)fused_conv.weight = nn.Parameter(w)fused_conv.bias = nn.Parameter(b)return fused_convdef fuse_module(m):children = list(m.named_children())c = Nonecn = Nonefor name, child in children:if isinstance(child, nn.BatchNorm2d):bc = fuse(c, child)m._modules[cn] = bcm._modules[name] = DummyModule()c = Noneelif isinstance(child, nn.Conv2d):c = childcn = nameelse:fuse_module(child)def test_net(m, input):import times = time.time()o_output = m(input)print("Original time: ", time.time() - s)fuse_module(m)s = time.time()f_output = m(input)print("Fused time: ", time.time() - s)print("Max abs diff: ", (o_output - f_output).abs().max().item())assert(o_output.argmax() == f_output.argmax())# print(o_output[0][0].item(), f_output[0][0].item())print("MSE diff: ", nn.MSELoss()(o_output, f_output).item())

使用方法:

use_cuda = True
device_cpu = torch.device('cpu')
net = FCNETCTC(len(CHARS)).to(device_cpu)
net.load_state_dict(torch.load("weights/best_net_ctc_params_opt1.pkl", map_location=device_cpu))
net.eval()example = torch.rand(1, 3, 24, 94)
test_net(net, example)

均值归一化:

img_tensor = img_tensor.permute({0, 3, 1, 2});   //翻转让通道是第二个维度//均值归一化img_tensor[0][0] = img_tensor[0][0].sub_(0.485).div_(0.229);img_tensor[0][1] = img_tensor[0][1].sub_(0.456).div_(0.224);img_tensor[0][2] = img_tensor[0][2].sub_(0.406).div_(0.225);

python util相关推荐

  1. python util_Python util.util方法代码示例

    本文整理汇总了Python中util.util.util方法的典型用法代码示例.如果您正苦于以下问题:Python util.util方法的具体用法?Python util.util怎么用?Pytho ...

  2. java可以调用python程序吗_我们可以从java调用python方法吗?

    是的,那可以做到.通常,这将通过创建PythonInterpreter对象然后使用它来调用python类来完成. 请考虑以下示例: Java: import org.python.core.PyIns ...

  3. java调用python的函数_java如何调用python的.py文件,以及如何执行里面的函数,和创建...

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 package com.hmammon.service; import java.io.FileInputStream; import java.io.F ...

  4. java和python可以在一个项目中同时使用么-java调用python的几种用法(看这篇就够了)...

    java调用python的几种用法如下: 在java类中直接执行python语句 在java类中直接调用本地python脚本 使用Runtime.getRuntime()执行python脚本文件(推荐 ...

  5. 【Java】使用Java调用Python的四种方法

    写在前面 为啥一个好好的岗位是Java开发工程师要去做写python呢?因为产品经理安排的(突然多少有点明白为啥程序员和产品经理会一直开撕).由于在选择企业的时候没看清企业性质,看了要求以为就是互联网 ...

  6. java调用python项目实战_Java调用Python

    今天遇到Java调用一个Python脚本的问题,纠结了大半天,遇到各种问题.网上搜索的大部分都是用jython,但是我想要调用的python脚本里有import urllib,这个urllib也不是什 ...

  7. Debug Tensorflow: tensorflow.python.framework.errors_impl.InvalidArgumentError: OpKernel ‘ConcatV2‘

    环境 Ubuntu 18.04 Tensorflow-gpu 2.4 报错 Traceback (most recent call last):File "nn/cnn.py", ...

  8. java 调用python脚本过程_通过Java调用Python脚本

    在进行开发的过程中,偶尔会遇到需要使用Java调用Python脚本的时候,毕竟Python在诸如爬虫,以及科学计算等方面具有天然的优势.最近在工作中遇到需要在Java程序中调用已经写好的Python程 ...

  9. 怎么把模组直接装在Java里面_如何使用jythonj将python模块添加到java中

    以下代码在我的Ubuntu机器上运行得很好,jython2.7和java1.6(在Eclipse和终端上进行了测试):package myjythonproject; import org.pytho ...

最新文章

  1. 设计模式(三)外观模式
  2. Delphi中文件名函数-路径、名称、子目录、驱动器、扩展名
  3. perl脚本的默认参数
  4. 【代码】synchronized是可重入锁并且多个sync代码块顺序执行
  5. 怎么求平均数_EXCEL怎么求企业连续几年业绩的平均增长率
  6. python函数 global_**Python的函数参数传递 和 global
  7. VMware ubuntu 上网
  8. hbase动态更改行键设计_Hadoop HBase概念学习系列之优秀行键设计(十六)
  9. 性价比不高却出到8?华为存在感最低的一个系列再发新机
  10. [Java] 蓝桥杯ALGO-151 算法训练 6-2递归求二进制表示位数
  11. Vmware中linux端口映射,让外网/ssh访问虚拟机Linux
  12. [Matlab]维纳滤波器设计
  13. 模电摸索日记之《集成运算放大器》
  14. 更新macOS Monterey后,Flutter页面崩溃、白屏
  15. python批量处理图片属性_python PIL 批量处理处理图片
  16. 应用MATLAB建模与仿真
  17. 小程序 Serverless: 解放生产力,驱动研发效能提升 1
  18. 衡水年内计划发放万张农民工“一卡通” 防止拖欠工资
  19. ajax请求失败readyState为0
  20. CF1037E. Trips

热门文章

  1. 可信平台模块TPM(Trusted Platform Module)介绍及tpm-tools安装使用
  2. pycharm中新建项目出现的问题及过程(小白自学)
  3. 华为手机输入键盘声音_华为键盘声音怎么设置
  4. Azure Queues and Service Bus Queues - Compared and Contrasted
  5. chrome模拟手机浏览器方法
  6. spring cache ttl 过期
  7. 基于arduino 开发 esp32 点亮ili9341屏幕
  8. java.lang.IllegalArgumentException: Name for argument type [java.lang.Integer] not available异常
  9. java.lang.IllegalArgumentException: Illegal URL:
  10. Update From 用法