几个常用的python脚本

一、移动多文件夹里的文件

import sys
import os
lines = [line.strip() for line in open("list_1013.txt", 'r').readlines()]
for line in lines:
    imgpath = line
    outpath = "newbg/"+line.split("/")[0]
    if not os.path.exists(outpath):
        os.makedirs(outpath)
    os.system("cp "+ imgpath +"  "+outpath)
    #print(imgpath, line,outpath, line.split("/")[0]+"/"+line.split("/")[1])
    print(line,outpath)

二、修改tensoflow的pth模型权重

import torch
import cv2
import numpy as np
from collections import OrderedDict
new_state_dict = OrderedDict()
state_dict = torch.load('model_0235000.pth')
for k, v in state_dict.items():
    if k == "model":
        for ik in v:
            key = ik#.replace('backbone.body.', '')
            new_state_dict[key] = v[str(key)]
torch.save(new_state_dict,  "yolotiny.pth")

三、修改pytorch的pth模型权重

    state_dict = torch.load("/home/zhao/baidu/yolov5_new/best.pt_tmp")
    state_old = torch.load("weights/yolov5s.pt")
    i = 0
    j = 0
    for k, v in state_dict.items():     
        if k == "model":
            nlist = list(state_old.items())[j][1]
            for name, param in v.named_parameters():
                if name == "model.0.conv.conv.weight":      
                    param.data = torch.cat((list(nlist.parameters())[i], list(nlist.parameters())[i]), 1)
                else:
                    param.data = list(nlist.parameters())[i]#param.data
                print(i,len(list(nlist.parameters())), len(list(v.named_parameters())), list(nlist.parameters())[i].shape, param.data)
                i = i + 1   
        j = j + 1
    #print(state_dict)
    '''i = 0
    j = 0
    for k, v in state_dict.items():     
        if k == "model":
            nlist = list(state_old.items())[j][1]
            for name, param in v.named_parameters():
                print(i,len(list(nlist.parameters())), len(list(v.named_parameters())), list(nlist.parameters())[i].shape, param.data.shape)
                i = i + 1  
       # if k == "optimizer": 
            # print(v) 
        j = j + 1'''
    torch.save(state_dict,  "newmodel6.pt")

四、给图像roi区域涂黑

h0, w0 = img.shape[:2]
bg_img = img.copy()
for ibox in self.labels[index]:
    ix1 = int(ibox[1] * w0 - ibox[3] * w0 * 0.5)
    iy1 = int(ibox[2] * h0 - ibox[4] * h0 * 0.5)
    ix2 = int(ix1 + ibox[3] * w0)
    iy2 = int(iy1 + ibox[4] * h0)
    ix1 = ix1 if ix1 < w0 else w0 - 1
    iy1 = iy1 if iy1 < h0 else h0 - 1
    ix2 = ix2 if ix2 < w0 else w0 - 1
    iy2 = iy2 if iy2 < h0 else h0 - 1
    iw = ix2 - ix1
    ih = iy2 - iy1
    #print(bg_img.shape, iy1,iy2,ix1,ix2, ibox)
    bg_img[iy1:iy2, ix1:ix2] = np.zeros((ih, iw, 3), np.uint8)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值