Tensorflow + ResNet101 + fasterRcnn 训练自己的模型 数据(一)

2024-02-13 08:18

本文主要是介绍Tensorflow + ResNet101 + fasterRcnn 训练自己的模型 数据(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、数据准备:

1、PASCAL VOC数据集格式

2、数据扩充:做了旋转【0, 90,180,270】(备注:这里可以不做那么多许旋转,fasterrcnn在训练的时候要做图片的镜像变换)、降采样


降采样:

import os
import cv2
import numpy as np
try:import xml.etree.cElementTree as ET
except ImportEroor:import xml.etree.ElementTree as ET
import copy,randomdef _image_downsampling(folder,Savefolder,scale=0.5):   JPEGImages = os.path.join(folder, 'JPEGImages')saveJPEGImages = os.path.join(Savefolder, 'JPEGImages')if not os.path.exists(saveJPEGImages):os.makedirs(saveJPEGImages)for imgfile in os.listdir(JPEGImages):print(imgfile)img = cv2.imread(os.path.join(JPEGImages,imgfile))img_downsampled = cv2.resize(img,(int(scale*img.shape[1]),int(scale*img.shape[0])))new_name = imgfile[:-4] + '_downsampled' + str(scale) + '.jpg'cv2.imwrite(os.path.join(saveJPEGImages,new_name),img_downsampled)# cv2.imshow('name',downsampled_img)# key = cv2.waitKey(5000)# if key == 27:#     quit()# cv2.destroyWindow('name')def _rewrite_txt(folder,Savefolder,scale):Annotations = os.path.join(folder, 'Annotations/labels')saveAnnotations = os.path.join(Savefolder, 'Annotations/labels')if not os.path.exists(saveAnnotations):os.makedirs(saveAnnotations)for txtfile in os.listdir(Annotations):print(txtfile)ff = open(os.path.join(Annotations,txtfile),'r')f = open(os.path.join(saveAnnotations,txtfile[:-4] + '_downsampled'  + str(scale) + '.txt'),'at')for line in ff.readlines():[filename, cls, x1, y1, x2, y2] = line.strip().split(' ')x1 = int(int(x1) * scale)y1 = int(int(y1) * scale)x2 = int(int(x2) * scale)y2 = int(int(y2) * scale)new_name = filename[:-4] + '_downsampled' + str(scale) + '.jpg'nline = new_name + ' ' + cls + ' ' + str(x1) + ' ' + str(y1) + ' ' + str(x2) + ' ' + str(y2) + '\n'f.write(nline)f.close()ff.close()def _rewrite_xml(folder,Savefolder,scale):Annotations = os.path.join(folder, 'Annotations')saveAnnotations = os.path.join(Savefolder, 'Annotations')if not os.path.exists(saveAnnotations):os.makedirs(saveAnnotations)for xmlfile in os.listdir(Annotations):print(xmlfile)# if xmlfile[:-4] != '.xml':#     continuenew_name = xmlfile[:-4] + '_downsampled' + str(scale) + '.jpg'tree = ET.parse(os.path.join(Annotations,xmlfile))root = tree.getroot()for obj in root.findall('object'):bndbox = obj.find('bndbox')bndbox[0].text = str(int(float(bndbox[0].text)*scale))bndbox[1].text = str(int(float(bndbox[1].text)*scale))bndbox[2].text = str(int(float(bndbox[2].text)*scale))bndbox[3].text = str(int(float(bndbox[3].text)*scale))size = tree.find('size')height = size.find('height')width = size.find('width')height.text = str(int(float(height.text)*scale))width.text = str(int(float(width.text)*scale))filename = tree.find('filename')filename.text = new_nameprint(new_name)tree.write(os.path.join(saveAnnotations,new_name[:-4]+'.xml'))def _rewrite_trainval_and_test(folder,Savefolder,scales):if not os.path.exists(os.path.join(Savefolder,'ImageSets', 'Main')):os.makedirs(os.path.join(Savefolder,'ImageSets', 'Main'))# folder = '../enhancement/' + cate + '/'# Savefolder = '../downsampled/'+ cate + '_downsampled'trainval = open(folder + 'ImageSets/Main/train.txt','r')print 'trainval:', trainvaltest = open(folder + 'ImageSets/Main/val.txt','r')print 'test:',testsave_trainval = open(Savefolder + '/ImageSets/Main/train.txt','a')print 'save_trainval:', save_trainvalsave_test = open(Savefolder + '/ImageSets/Main/val.txt','a')print 'save_test:', save_testfor line in trainval.readlines():for i in range(len(scales)):nline = line.strip() + '_downsampled' + str(i)save_trainval.write(nline + '\n')save_trainval.close()for line in test.readlines():for i in range(len(scales)):nline = line.strip() + '_downsampled' + str(i)save_test.write(nline + '\n')save_test.close()def main(folder,scale = 0.5):#images_image_downsampling(folder,Savefolder,scale)#annotationsif annotations_file is 'txt':_rewrite_txt(folder,Savefolder,scale)elif annotations_file is 'xml':_rewrite_xml(folder,Savefolder,scale) #trainval,test_rewrite_trainval_and_test(folder,Savefolder,[scale])def main2():JPEGImages = os.path.join(folder, 'JPEGImages')saveJPEGImages = os.path.join(Savefolder, 'JPEGImages')if not os.path.exists(saveJPEGImages):os.makedirs(saveJPEGImages)Annotations = os.path.join(folder, 'Annotations')saveAnnotations = os.path.join(Savefolder, 'Annotations')if not os.path.exists(saveAnnotations):os.makedirs(saveAnnotations)scales = Nonefor imgfile in os.listdir(JPEGImages):#imagesimg = cv2.imread(os.path.join(JPEGImages,imgfile))scales = [random.uniform(2,15)*0.1 for _ in range(2)]# scales = [random.uniform(5,10)*0.1,random.uniform(10,15)]for i,scale in enumerate(scales):img_downsampled = cv2.resize(img,(int(scale*img.shape[1]),int(scale*img.shape[0])))new_name = imgfile[:-4] + '_downsampled' + str(i)+'.jpg'print(new_name)cv2.imwrite(os.path.join(saveJPEGImages,new_name),img_downsampled)#annotationsxmlfile = imgfile[:-4] + '.xml'tree = ET.parse(os.path.join(Annotations,xmlfile))root = tree.getroot()for obj in root.findall('object'):bndbox = obj.find('bndbox')xmin = bndbox.find('xmin').textymin = bndbox.find('ymin').textxmax = bndbox.find('xmax').textymax = bndbox.find('ymax').textbndbox.find('xmin').text = str(int(int(xmin)*scale))bndbox.find('ymin').text = str(int(int(ymin)*scale))bndbox.find('xmax').text = str(int(int(xmax)*scale))bndbox.find('ymax').text = str(int(int(ymax)*scale))assert(((int(xmax) - int(xmin))*scale)>0)assert(((int(ymax) - int(ymin))*scale)>0)filename = tree.find('filename')filename.text = new_namesize = tree.find('size')height = size.find('height')width = size.find('width')height.text = str(int(float(height.text)*scale))width.text = str(int(float(width.text)*scale))tree.write(os.path.join(saveAnnotations,new_name[:-4]+'.xml'))#trainval,test_rewrite_trainval_and_test(folder,Savefolder,scales)if __name__=='__main__': global annotations_file,folder, Savefolderannotations_file = 'xml'#xmlcatelist = ['hydropower', 'thermalpower', 'tower', 'windpower']for cate in catelist:folder = '../enhancement/' + cate + '/'Savefolder = '../downsampled/'+ cate + '_downsampled'main2()# _rewrite_trainval_and_test(folder,Savefolder,[0,0])

旋转:

import cv2
import os
import numpy as np
import math
import copy
try:import xml.etree.cElementTree as ET
except ImportError:import xml.etree.ElementTree as ETdef _write_(f,ff):for lines in ff.readlines():for angle in rotation_angle:line1 = lines.strip()+'_rotated_' + str(angle)# line2 = lines.strip()+'_rotated_' + str(angle) + '_mirror'f.write(line1+'\n')# f.write(line2+'\n')f.close()ff.close()def rotate_about_center(src, angle, scale=1.):w = src.shape[1]h = src.shape[0]rangle = np.deg2rad(angle) #angle in radiansnw = (abs(np.sin(rangle)*h)+abs(np.cos(rangle)*w))*scalenh = (abs(np.cos(rangle)*h)+abs(np.sin(rangle)*w))*scalerot_mat = cv2.getRotationMatrix2D((nw*0.5,nh*0.5), angle, scale)# rotate with centerrot_move = np.dot(rot_mat,np.array([(nw-w)*0.5,(nh-h)*0.5,0]))rot_mat[0,2] += rot_move[0]rot_mat[1,2] += rot_move[1]return cv2.warpAffine(src,rot_mat,(int(math.ceil(nw)),int(math.ceil(nh))),flags = cv2.INTER_LANCZOS4)def enhancement_using_rotation(ImagePath,AnnotationsPath):print '============'print ImagePathfor imgfile in os.listdir(ImagePath):print imgfile[:-4]if not os.path.isfile(os.path.join(AnnotationsPath,imgfile[:-4]+'.xml')):continuesimg = cv2.imread(os.path.join(ImagePath,imgfile))#rotationfor angle in rotation_angle:new_name = imgfile[:-4] + '_rotated_'+ str(angle) + '.jpg'# print '\nnew JPEGImage:', new_namerotate_img = rotate_about_center(img,angle)cv2.imwrite(os.path.join(ImageSavePath,new_name),rotate_img)center_x = img.shape[1]/2center_y = img.shape[0]/2new_center_x = rotate_img.shape[1]/2new_center_y = rotate_img.shape[0]/2if annotation_file is 'txt':            ff = open(os.path.join(AnnotationsPath,'labels',imgfile[:-4]+'.txt'),'r')f = open(os.path.join(AnnotationsSavePath,'labels',new_name[:-4] +'.txt'),'a')for line in ff.readlines():[filename, cls, x1,y1,x2,y2,t] = line.split(' ')final_x1, final_y1, final_x2, final_y2 = \_rotated_location(x1,y1,x2,y2,center_x,center_y,new_center_x,new_center_y)assert(final_y2-final_y1>0) & (final_x2-final_x1>0)# print 'rotated:',final_x1, final_y1, final_x2, final_y2if ifshow == 1:cv2.rectangle(rotate_img,(final_x1,final_y1),(final_x2,final_y2),(0,0,255),2)cv2.putText(rotate_img, cls, (int(final_x1),int(final_y1)),0,1.2,(0,0,255),2)nline = new_name + ' ' + cls + ' ' + str(final_x1) + ' ' + str(final_y1) + ' ' + str(final_x2) + ' ' + str(final_y2) + '\n'f.write(nline)f.close()ff.close()elif annotation_file is 'xml':Annotations = os.path.join(AnnotationsPath,imgfile[:-4]+'.xml')saveAnnotations = os.path.join(AnnotationsSavePath,new_name[:-4]+'.xml')tree = ET.parse(Annotations)filename = tree.find('filename')filename.text = new_namesize = tree.find('size')height = size.find('height')height.text = str(rotate_img.shape[0])width = size.find('width')width.text = str(rotate_img.shape[1])root = tree.getroot()for obj in tree.findall('object'):class_node = obj.find('name')bndbox = obj.find('bndbox')x1 = bndbox.find('xmin').texty1 = bndbox.find('ymin').textx2 = bndbox.find('xmax').texty2 = bndbox.find('ymax').textif not(int(y2)-int(y1)>0) & (int(x2)-int(x1)>0):print(Annotations)root.remove(obj)continueassert(int(y2)-int(y1)>0) & (int(x2)-int(x1)>0)# cls = class_node.text# if cls == 'airplane': # 	class_node.text = 'aircraft'# 	print(class_node.text,cls,'11')# if cls == 'car': #     class_node.text = 'vehicle'#     print(class_node.text,cls,'22')#     print(imgfile)#     root.remove(obj)#     continue# if cls == 'copy of helicopter' or cls =='Copy of helicopter': #     class_node.text = 'helicopter'#     print(class_node.text,cls,'33')#     print(imgfile)final_x1, final_y1, final_x2, final_y2 = \_rotated_location(x1,y1,x2,y2,center_x,center_y,new_center_x,new_center_y,angle)assert(final_y2-final_y1>0)assert(final_x2-final_x1>0)assert(final_x1>=0)assert(final_y1>=0)assert(final_x2<=width.text)assert(final_y2<=height.text)# print 'rotated:',final_x1, final_y1, final_x2, final_y2if ifshow == 1:cv2.rectangle(rotate_img,(final_x1,final_y1),(final_x2,final_y2),(0,0,255),2)cv2.putText(rotate_img, cls, (int(final_x1),int(final_y1)),0,1.2,(0,0,255),2)bndbox.find('xmin').text = str(final_x1)bndbox.find('ymin').text = str(final_y1)bndbox.find('xmax').text = str(final_x2)bndbox.find('ymax').text = str(final_y2)tree.write(saveAnnotations)# tree.clear()if ifshow == 1:cv2.imshow(new_name,rotate_img)key = cv2.waitKey(500)if key ==27:quit()cv2.destroyWindow(new_name)def _rotated_location(x1,y1,x2,y2,center_x,center_y,new_center_x,new_center_y,angle):x1 = float(x1) - center_xy1 = -(float(y1) - center_y)x2 = float(x2) - center_xy2 = -(float(y2) - center_y)rangle = np.deg2rad(angle)rotated_x1 = np.cos(rangle)*x1 - np.sin(rangle)*y1rotated_y1 = np.cos(rangle)*y1 + np.sin(rangle)*x1rotated_x2 = np.cos(rangle)*x2 - np.sin(rangle)*y2rotated_y2 = np.cos(rangle)*y2 + np.sin(rangle)*x2rotated_x1 = int(rotated_x1 + new_center_x)rotated_y1 = int(-rotated_y1 + new_center_y)rotated_x2 = int(rotated_x2 + new_center_x)rotated_y2 = int(-rotated_y2 + new_center_y)final_x1 = int(min(rotated_x1,rotated_x2))final_y1 = int(min(rotated_y1,rotated_y2))final_x2 = int(max(rotated_x1,rotated_x2))final_y2 = int(max(rotated_y1,rotated_y2))return final_x1, final_y1, final_x2, final_y2def enhancement_using_mirror(ImagePath,AnnotationsPath):#mirrorfor imgfile in os.listdir(ImagePath):img = cv2.imread(os.path.join(ImagePath,imgfile))new_name = imgfile[:-4] + '_mirror' + '.jpg'# print(new_name)mirror_img  = mirroir_hierachically(img)cv2.imwrite(os.path.join(ImageSavePath,new_name),mirror_img)w = img.shape[1]if annotation_file is 'txt':ff = open(os.path.join(txtpath,imgfile[:-4]+'.txt'),'r')f = open(os.path.join(txtpath,new_name[:-4]+'.txt'),'a')for line in ff.readlines():[filename,cls,x1,y1,x2,y2] = line.strip().split(' ')x2 = abs(int(x1)-w)x1 = abs(int(x2)-w)final_x1 = min(x1,x2)final_x2 = max(x1,x2)assert(final_x2-final_x1>0)nline = new_name + ' ' + cls + ' ' + str(final_x1) + ' ' + y1 + ' ' + str(final_x2) +  ' ' + y2 + '\n'f.write(nline)if ifshow == 1:cv2.rectangle(mirror_img,(final_x1,int(y1)),(final_x2,int(y2)),(0,0,255),2)cv2.putText(mirror_img, cls, (final_x1,int(y1)),0,1.2,(0,0,255),2)           f.close()ff.close()elif annotation_file is 'xml':Annotations = os.path.join(AnnotationsPath, imgfile[:-4]+'.xml')saveAnnotations = os.path.join(AnnotationsSavePath,new_name[:-4]+'.xml')tree = ET.parse(Annotations)filename = tree.find('filename')filename.text = new_namefor obj in tree.findall('object'):bndbox = obj.find('bndbox')x1 = bndbox.find('xmin').texty1 = bndbox.find('ymin').textx2 = bndbox.find('xmax').texty2 = bndbox.find('ymax').textnew_x1 = abs(int(x2)-w)new_x2 = abs(int(x1)-w)final_x1 = int(min(new_x1,new_x2))final_x2 = int(max(new_x1,new_x2))assert(final_x2-final_x1>0)cls = obj.find('name').text if ifshow == 1:cv2.rectangle(mirror_img,(final_x1,int(y1)),(final_x2,int(y2)),(0,0,255),2)cv2.putText(mirror_img, cls, (int(final_x1),int(y1)),0,1.2,(0,0,255),2)bndbox.find('xmin').text = str(final_x1)bndbox.find('xmax').text = str(final_x2)tree.write(saveAnnotations)# tree.clear()if ifshow == 1:cv2.imshow(new_name,mirror_img)key = cv2.waitKey(500)if key ==27:quit()cv2.destroyWindow(new_name)def mirroir_hierachically(src):w = src.shape[1]h = src.shape[0]ll = src.shape[2]mirror_img = copy.deepcopy(src)for wi in xrange(w):mirror_img[:,w-wi-1] = src[:,wi]return mirror_imgif __name__ == '__main__':global annotation_file, rotation_angle, ifshow, ImageSavePath,AnnotationsSavePath,AnnotationsSavePathannotation_file = 'xml'#'txt',xml'rotation_angle=[0,90,180,270]ifshow = 0catelist = ['hydropower', 'thermalpower', 'tower', 'windpower']# ##################################### PART 1 ######################################################## for cate in catelist:savepath =  '../enhancement/' + cateImageSavePath = savepath + '/JPEGImages/'AnnotationsSavePath = savepath + '/Annotations/'ImageSetsSavePath = savepath + '/ImageSets/Main/'if not os.path.exists(ImageSavePath):os.makedirs(ImageSavePath)    if not os.path.exists(AnnotationsSavePath):os.makedirs(AnnotationsSavePath)if not os.path.exists(ImageSetsSavePath):os.makedirs(ImageSetsSavePath)# ##################################### PART 2 #########################################################augument trainning dataprint('rotating...')# path =   '../TrainData/' + cate  path =   '../TrainData/' + cate ImagePath = path + '/JPEG/'AnnotationsPath = path + '/XML/'enhancement_using_rotation(ImagePath,AnnotationsPath)##################################### PART 3 ######################################################### trainval.txt/test.txtprint('writing trainval / test txt...')path =  '../TrainData/' + cate + '/ImageSets/Main/'f1 = open(path + 'train.txt','r')f2 = open(path + 'val.txt','r')trainval_txt = open('../enhancement/'+cate +'/ImageSets/Main/train.txt','a')test_txt = open( '../enhancement/'+cate +'/ImageSets/Main/val.txt','a')_write_(trainval_txt,f1)_write_(test_txt,f2)


3、训练中数据出现的问题:

(1)此次训练了4类目标,但是数据里面实际存在了其他类的数据,

(2)图片边界问题:x,y不能为负数; anchor一致

(3)一些参数问题要好好看下论文

(4)cache文件删除(数据初始化将train_val和test写入,因为没有删除 导致在test是出现keyerror。训练的时候是先初始化,数据放在cache文件pkl文件中)



这篇关于Tensorflow + ResNet101 + fasterRcnn 训练自己的模型 数据(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/704993

相关文章

0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型的操作流程

《0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeekR1模型的操作流程》DeepSeekR1模型凭借其强大的自然语言处理能力,在未来具有广阔的应用前景,有望在多个领域发... 目录0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型,3步搞定一个应

Deepseek R1模型本地化部署+API接口调用详细教程(释放AI生产力)

《DeepseekR1模型本地化部署+API接口调用详细教程(释放AI生产力)》本文介绍了本地部署DeepSeekR1模型和通过API调用将其集成到VSCode中的过程,作者详细步骤展示了如何下载和... 目录前言一、deepseek R1模型与chatGPT o1系列模型对比二、本地部署步骤1.安装oll

Spring AI Alibaba接入大模型时的依赖问题小结

《SpringAIAlibaba接入大模型时的依赖问题小结》文章介绍了如何在pom.xml文件中配置SpringAIAlibaba依赖,并提供了一个示例pom.xml文件,同时,建议将Maven仓... 目录(一)pom.XML文件:(二)application.yml配置文件(一)pom.xml文件:首

Redis的数据过期策略和数据淘汰策略

《Redis的数据过期策略和数据淘汰策略》本文主要介绍了Redis的数据过期策略和数据淘汰策略,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录一、数据过期策略1、惰性删除2、定期删除二、数据淘汰策略1、数据淘汰策略概念2、8种数据淘汰策略

轻松上手MYSQL之JSON函数实现高效数据查询与操作

《轻松上手MYSQL之JSON函数实现高效数据查询与操作》:本文主要介绍轻松上手MYSQL之JSON函数实现高效数据查询与操作的相关资料,MySQL提供了多个JSON函数,用于处理和查询JSON数... 目录一、jsON_EXTRACT 提取指定数据二、JSON_UNQUOTE 取消双引号三、JSON_KE

Python给Excel写入数据的四种方法小结

《Python给Excel写入数据的四种方法小结》本文主要介绍了Python给Excel写入数据的四种方法小结,包含openpyxl库、xlsxwriter库、pandas库和win32com库,具有... 目录1. 使用 openpyxl 库2. 使用 xlsxwriter 库3. 使用 pandas 库

SpringBoot定制JSON响应数据的实现

《SpringBoot定制JSON响应数据的实现》本文主要介绍了SpringBoot定制JSON响应数据的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录前言一、如何使用@jsonView这个注解?二、应用场景三、实战案例注解方式编程方式总结 前言

如何在本地部署 DeepSeek Janus Pro 文生图大模型

《如何在本地部署DeepSeekJanusPro文生图大模型》DeepSeekJanusPro模型在本地成功部署,支持图片理解和文生图功能,通过Gradio界面进行交互,展示了其强大的多模态处... 目录什么是 Janus Pro1. 安装 conda2. 创建 python 虚拟环境3. 克隆 janus

使用Python在Excel中创建和取消数据分组

《使用Python在Excel中创建和取消数据分组》Excel中的分组是一种通过添加层级结构将相邻行或列组织在一起的功能,当分组完成后,用户可以通过折叠或展开数据组来简化数据视图,这篇博客将介绍如何使... 目录引言使用工具python在Excel中创建行和列分组Python在Excel中创建嵌套分组Pyt

本地私有化部署DeepSeek模型的详细教程

《本地私有化部署DeepSeek模型的详细教程》DeepSeek模型是一种强大的语言模型,本地私有化部署可以让用户在自己的环境中安全、高效地使用该模型,避免数据传输到外部带来的安全风险,同时也能根据自... 目录一、引言二、环境准备(一)硬件要求(二)软件要求(三)创建虚拟环境三、安装依赖库四、获取 Dee