几个常用的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)

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页