记录一下小样本目标检测点滴,还有mmrotate的环境配置,数据增强

编程入门 行业动态 更新时间:2024-10-27 05:32:30

记录一下小<a href=https://www.elefans.com/category/jswz/34/1766853.html style=样本目标检测点滴,还有mmrotate的环境配置,数据增强"/>

记录一下小样本目标检测点滴,还有mmrotate的环境配置,数据增强

毕业设计做的题目是小样本目标检测,用到的是mmrotate。

话说mmrotate是真的好用 

哇,但是这个装环境就装了一万年,前前后后实验环境用到的版本换了一遍又一遍,又由于学校实验室的系统和自己电脑的系统不一样(学校的是linux,自己的是windows),所以前后一共被折磨了两次┭┮﹏┭┮,不过好在最后还是成功的跑了起来。最后实验中用到的环境配置如下:

windows:cuda10.2+pytorch1.6.0+torchvision0.7.0+mmrotate0.1.0+mmcv-full1.4.6
linux:cuda11.0+pytorch1.7.0+torchversion0.8.0+mmrotate0.2.0+mmcv-full1.4.6

以下的环境安装都是以我在windows系统下的配置,实际上在linux终端下是一样的方法。话不多说,直接开始咱们的mmrotate环境安装流程。


官网给出的要求如下:

1.首先创建一个mmrotate环境并激活

创建一个新的环境是一个很好的习惯,不然会像我一开始一样老是路径有问题,我用的python版本是3.7的,可以根据自己的需要去选则python的版本。

conda create -n mmrotate python=3.7
conda activate mmrotate

2.安装pytorch和对应版本的torchversion

看第2点前建议先看看3,不然很有可能版本有问题。

直接去pytorch官网就可以复制代码pip命令生成安装链接,但是官网打开比较慢

Start Locally | PyTorch

所以可以直接按照我下面的格式去pip安装就行了

conda install pytorch==1.6.0 torchvision==0.7.0 cudatoolkit=10.2 -c pytorch

3.安装mmrotate

mmrotate官网上提供了两种安装mmrotate的方法(不过我比较菜只有第二种成功过😂)

(1)直接掉下面两行命令

pip install openmim
mim install mmrotate

但是好像这个mim install的mmcv不是mmcv-full版本的,可以先uninstall mmcv再下一个full版本的具体pip下载的格式可以看看第二种方法。

(2)第二种方法比较麻烦,但是几乎不会出现什么错误

1)首先,安装mmcv-full

pip install mmcv-full -f /{cu_version}/{torch_version}/index.html

替换其中的cu_version和torch_version为你自己的版本。

这里需要注意的一点是,也是我在环境安装中反复踩的一个坑,mmcv-full的安装对pytorch的版本要求很奇怪(不知到是不是我的水平太低了😂),好像torch的版本只能是x.x.0的,比如1.7.0还有1.6.0,用1.7.1和1.6.1就没有mmcv-full的对应版本,这也就导致当时pip install mmcv-full的时候老是找不到版本(用pip安装mmcv-full的时候一定要用官网的代码!!!直接pip install是安装最新版本,可能又会有问题报错),像我在windows下的就这样子安装:

pip install mmcv-full==1.4.6 -f .6.0/index.html

2)接着,安装mmdetection

mmrotate是基于mmdetection开发的,所以还需要安装一下mmdetection。

pip install mmdet

3)最后,安装mmrotate

mmrotate一直都在更新,上次我用还在等0.1.1的版本,结果过了一段时间都0.2.0了

pip install mmrotate

或者你也可以git安装

git clone .git
cd mmrotate
pip install -r requirements/build.txt
pip install -v -e .  # or "python setup.py develop"

这样子就大功告成啦!

 

带标签的dota数据集格式数据增强

from PIL import Image
from PIL import ImageChops
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
Image.MAX_IMAGE_PIXELS = None
from PIL import ImageFilter
from PIL import ImageEnhance
import os
import cv2
import numpy as np
import math#仿射变换-平移
def move(imageDir,img_name,x,y): #平移,平移尺度为offimg = Image.open(os.path.join(imageDir, img_name))offset = ImageChops.offset(img,x,y)return offset
#仿射变换-平移 txt
def move_txt(imageDir,txtDir,savetxtDir,img_name,txt_name,txt_savename,x,y):img = Image.open(os.path.join(imageDir, img_name))with open(os.path.join(savetxtDir, txt_savename), 'w') as fp:fp.write("imagesource:GoogleEarth")fp.write('\n')fp.write("gsd:0.146343590398")fp.write('\n')with open(os.path.join(txtDir,txt_name)) as f:i=0for line in f.readlines():i=i+1temp_list=[]if i>2:   #从第3行开始读temp_list = line.split(' ')temp_list[-1] = temp_list[-1].replace('\n', ',')x1 = float(temp_list[0]) + xy1 = float(temp_list[1]) + yx2 = float(temp_list[2]) + xy2 = float(temp_list[3]) + yx3 = float(temp_list[4]) + xy3 = float(temp_list[5]) + yx4 = float(temp_list[6]) + xy4 = float(temp_list[7]) + yfor x in [x1,x2,x3,x4]:if x>img.width:x-=img.widthif x<0:x+=img.widthfor y in [y1,y2,y3,y4]:if y>img.height:y-=img.heightif y<0:y-=img.heightput_str = ' '.join([str(x1), str(y1), str(x2), str(y2), str(x3), str(y3), str(x4), str(y4), str('bigship'), str(0)])with open(os.path.join(savetxtDir, txt_savename), 'a') as fp:fp.write(put_str)fp.write('\n')return 0#仿射变换-翻转
def flip(imageDir,img_name):   #翻转图像img = Image.open(os.path.join(imageDir, img_name))filp_img = img.transpose(Image.FLIP_LEFT_RIGHT)# filp_img.save(os.path.join(root_path,img_name.split('.')[0] + '_flip.jpg'))return filp_img
#仿射变换-翻转txt
def flip_txt(imageDir,txtDir,savetxtDir,img_name,txt_name,txt_savename,x,y):img = Image.open(os.path.join(imageDir, img_name))with open(os.path.join(savetxtDir, txt_savename), 'w') as fp:fp.write("imagesource:GoogleEarth")fp.write('\n')fp.write("gsd:0.146343590398")fp.write('\n')with open(os.path.join(txtDir,txt_name)) as f:i=0for line in f.readlines():i=i+1temp_list=[]if i>2:   #从第3行开始读temp_list = line.split(' ')temp_list[-1] = temp_list[-1].replace('\n', ',')x1 = img.width-float(temp_list[0])y1 = float(temp_list[1])x2 = img.width-float(temp_list[2])y2 = float(temp_list[3])x3 = img.width-float(temp_list[4])y3 = float(temp_list[5])x4 = img.width-float(temp_list[6])y4 = float(temp_list[7])put_str = ' '.join([str(x1), str(y1), str(x2), str(y2), str(x3), str(y3), str(x4), str(y4), str('bigship'), str(0)])with open(os.path.join(savetxtDir, txt_savename), 'a') as fp:fp.write(put_str)fp.write('\n')return 0#旋转角度
def rotation(imageDir, img_name, sita):img = Image.open(os.path.join(imageDir, img_name))rotation_img = img.rotate(sita,expand=True) #旋转角度# rotation_img.save(os.path.join(root_path,img_name.split('.')[0] + '_rotation.jpg'))return rotation_img
#仿射变换-旋转txt
def rotation_txt(imageDir,txtDir,savetxtDir,img_name,txt_name,txt_savename,sita):img = Image.open(os.path.join(imageDir, img_name))w = img.widthh = img.heightprint(w,h)sita = sita/180*3.1415926with open(os.path.join(savetxtDir, txt_savename), 'w') as fp:fp.write("imagesource:GoogleEarth")fp.write('\n')fp.write("gsd:0.146343590398")fp.write('\n')with open(os.path.join(txtDir, txt_name)) as f:i = 0for line in f.readlines():i = i + 1temp_list = []if i > 2:  # 从第3行开始读temp_list = line.split(' ')temp_list[-1] = temp_list[-1].replace('\n', ',')x1 = float(temp_list[0])y1 = float(temp_list[1])l1 = math.sqrt(x1 ** 2 + y1 ** 2)a1 = math.atan(y1 / x1)X1 = l1 * math.cos(sita-a1)Y1 = w * math.sin(sita) - l1 * math.sin(sita-a1)x2 = float(temp_list[2])y2 = float(temp_list[3])l2 = math.sqrt(x2 ** 2 + y2 ** 2)a2 = math.atan(y2 / x2)X2 = l2 * math.cos(sita - a2)Y2 = w * math.sin(sita) - l2 * math.sin(sita - a2)x3 = float(temp_list[4])y3 = float(temp_list[5])l3 = math.sqrt(x3 ** 2 + y3 ** 2)a3 = math.atan(y3 / x3)X3 = l3 * math.cos(sita - a3)Y3 = w * math.sin(sita) - l3 * math.sin(sita - a3)x4 = float(temp_list[6])y4 = float(temp_list[7])l4 = math.sqrt(x4 ** 2 + y4 ** 2)a4 = math.atan(y4 / x4)X4 = l4 * math.cos(sita - a4)Y4 = w * math.sin(sita) - l4 * math.sin(sita - a4)put_str = ' '.join([str(X1), str(Y1), str(X2), str(Y2), str(X3), str(Y3), str(X4), str(Y4), str('bigship'), str(0)])print(put_str)with open(os.path.join(savetxtDir, txt_savename), 'a') as fp:fp.write(put_str)fp.write('\n')return 0#随机颜色
def randomColor(imageDir, img_name):"""对图像进行颜色抖动:param image: PIL的图像image:return: 有颜色色差的图像image"""image = Image.open(os.path.join(imageDir, img_name))random_factor = np.random.randint(0, 31) / 10.  # 随机因子color_image = ImageEnhance.Color(image).enhance(random_factor)  # 调整图像的饱和度random_factor = np.random.randint(10, 21) / 10.  # 随机因子brightness_image = ImageEnhance.Brightness(color_image).enhance(random_factor)  # 调整图像的亮度random_factor = np.random.randint(10, 21) / 10.  # 随机因子contrast_image = ImageEnhance.Contrast(brightness_image).enhance(random_factor)  # 调整图像对比度random_factor = np.random.randint(0, 31) / 10.  # 随机因子return ImageEnhance.Sharpness(contrast_image).enhance(random_factor)  # 调整图像锐度#对比度增强
def contrastEnhancement(imageDir, img_name):  # 对比度增强image = Image.open(os.path.join(imageDir, img_name))enh_con = ImageEnhance.Contrast(image)contrast = 1.5image_contrasted = enh_con.enhance(contrast)return image_contrasted#亮度增强
def brightnessEnhancement(imageDir,img_name):#亮度增强image = Image.open(os.path.join(imageDir, img_name))enh_bri = ImageEnhance.Brightness(image)brightness = 1.5image_brightened = enh_bri.enhance(brightness)return image_brightened#颜色增强
def colorEnhancement(imageDir,img_name):#颜色增强image = Image.open(os.path.join(imageDir, img_name))enh_col = ImageEnhance.Color(image)color = 1.5image_colored = enh_col.enhance(color)return image_coloreddef  only_change_name(txtDir,savetxtDir,txt_name,txt_savename):with open(os.path.join(savetxtDir, txt_savename), 'w') as fp:fp.write("imagesource:GoogleEarth")fp.write('\n')fp.write("gsd:0.146343590398")fp.write('\n')with open(os.path.join(txtDir, txt_name)) as f:for line in f.readlines():with open(os.path.join(savetxtDir, txt_savename), 'a') as fp:fp.write(line)return 0#通过在 hsv 色彩空间中,对 h、s、v三个通道增加扰动,来进行色调增强变换
def augment_hsv(imageDir,image_name, hgain, sgain, vgain):"""HSV color-space augmentation:param image:       待增强的图片:param hgain:       HSV 中的 h 扰动系数,yolov5:0.015:param sgain:       HSV 中的 s 扰动系数,yolov5:0.7:param vgain:       HSV 中的 v 扰动系数,yolov5:0.4:return:"""image=Image.open(os.path.join(imageDir,image_name))if hgain or sgain or vgain:# 随机取-1到1三个实数,乘以 hsv 三通道扰动系数# r:[1-gain,1+gain]r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1  # random gainsimage=cv2.cvtColor(image,cv2.COLOR_RGB2BGR)image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)# cv2.split:通道拆分# h:[0~180], s:[0~255], v:[0~255]hue, sat, val = cv2.split(image_hsv)dtype = image.dtype  # uint8x = np.arange(0, 256, dtype=r.dtype)lut_hue = ((x * r[0]) % 180).astype(dtype)lut_sat = np.clip(x * r[1], 0, 255).astype(dtype)lut_val = np.clip(x * r[2], 0, 255).astype(dtype)# cv2.LUT:dst(I) = lut(src(I) + d),d为常数0 / 128hue = cv2.LUT(hue, lut_hue)sat = cv2.LUT(sat, lut_sat)val = cv2.LUT(val, lut_val)# 通道合并image_hsv = cv2.merge((hue, sat, val)).astype(dtype)# 将hsv格式转为RGB格式image_dst = cv2.cvtColor(image_hsv, cv2.COLOR_HSV2BGR)image=Image.formarry(cv2.cvtColor(image_dst, cv2.COLOR_BGR2RGB))return imageelse:return image
# 图像的缩放
def change_scale(imageDir,img_name,h,w,jitter=0.5):image = Image.open(os.path.join(imageDir, img_name))iw, ih = image.size# 对图像进行缩放并且进行长和宽的扭曲new_ar = w/h * rand(1-jitter,1+jitter)/rand(1-jitter,1+jitter)scale = rand(.15,2.5)if new_ar < 1:nh = int(scale*h)nw = int(nh*new_ar)else:nw = int(scale*w)nh = int(nw/new_ar)image = image.resize((nw,nh), Image.BICUBIC)return imageimageDir="D:\Bishe\mmrotate-main\\fangshesjzq\img" #要改变的图片的路径文件夹
txtDir="D:\Bishe\mmrotate-main\\fangshesjzq\\txt"#要改变的txt的路径文件夹
saveimgDir="D:\Bishe\mmrotate-main\\fangshesjzq\saveimg"   #要保存的图片的路径文件夹
savetxtDir="D:\Bishe\mmrotate-main\\fangshesjzq\savetxt"#平移参数
x=0
y=5000i=0
for i in range(0,1):i=i+1txt_name=str(i)+'.txt'img_name=str(i)+'.png'save_i=i+0img_savename=str(save_i)+'.png'txt_savename=str(save_i)+'.txt'# saveImage=move(imageDir,img_name,x,y)# move_txt(imageDir,txtDir,savetxtDir,img_name,txt_name,txt_savename,x,y)# saveImage=flip(imageDir,img_name)# flip_txt(imageDir, txtDir, savetxtDir, img_name, txt_name, txt_savename, x, y)# saveImage=rotation(imageDir,img_name,30)# rotation_txt(imageDir, txtDir, savetxtDir, img_name, txt_name, txt_savename, 30)# saveImage=randomColor(imageDir, img_name)# saveImage=contrastEnhancement(imageDir, img_name)# saveImage=brightnessEnhancement(imageDir, img_name)saveImage=colorEnhancement(imageDir, img_name)# saveImage=augment_hsv(imageDir,img_name, 0.5, 0.5, 0.5)# only_change_name(txtDir, savetxtDir, txt_name, txt_savename)# saveImage=change_scale(imageDir, img_name, 1000,1000, jitter=0.5)saveImage.save(os.path.join(saveimgDir, img_savename))print("finish"+"  "+str(img_name))

今天就更新到这,q我催我更新(1609373452)

更多推荐

记录一下小样本目标检测点滴,还有mmrotate的环境配置,数据增强

本文发布于:2024-02-08 21:54:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1675483.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:样本   点滴   目标   环境   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!