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

相关文章

大模型研发全揭秘:客服工单数据标注的完整攻略

在人工智能(AI)领域,数据标注是模型训练过程中至关重要的一步。无论你是新手还是有经验的从业者,掌握数据标注的技术细节和常见问题的解决方案都能为你的AI项目增添不少价值。在电信运营商的客服系统中,工单数据是客户问题和解决方案的重要记录。通过对这些工单数据进行有效标注,不仅能够帮助提升客服自动化系统的智能化水平,还能优化客户服务流程,提高客户满意度。本文将详细介绍如何在电信运营商客服工单的背景下进行

基于MySQL Binlog的Elasticsearch数据同步实践

一、为什么要做 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品、订单等数据的多维度检索。 使用 Elasticsearch 存储业务数据可以很好的解决我们业务中的搜索需求。而数据进行异构存储后,随之而来的就是数据同步的问题。 二、现有方法及问题 对于数据同步,我们目前的解决方案是建立数据中间表。把需要检索的业务数据,统一放到一张M

关于数据埋点,你需要了解这些基本知识

产品汪每天都在和数据打交道,你知道数据来自哪里吗? 移动app端内的用户行为数据大多来自埋点,了解一些埋点知识,能和数据分析师、技术侃大山,参与到前期的数据采集,更重要是让最终的埋点数据能为我所用,否则可怜巴巴等上几个月是常有的事。   埋点类型 根据埋点方式,可以区分为: 手动埋点半自动埋点全自动埋点 秉承“任何事物都有两面性”的道理:自动程度高的,能解决通用统计,便于统一化管理,但个性化定

使用SecondaryNameNode恢复NameNode的数据

1)需求: NameNode进程挂了并且存储的数据也丢失了,如何恢复NameNode 此种方式恢复的数据可能存在小部分数据的丢失。 2)故障模拟 (1)kill -9 NameNode进程 [lytfly@hadoop102 current]$ kill -9 19886 (2)删除NameNode存储的数据(/opt/module/hadoop-3.1.4/data/tmp/dfs/na

异构存储(冷热数据分离)

异构存储主要解决不同的数据,存储在不同类型的硬盘中,达到最佳性能的问题。 异构存储Shell操作 (1)查看当前有哪些存储策略可以用 [lytfly@hadoop102 hadoop-3.1.4]$ hdfs storagepolicies -listPolicies (2)为指定路径(数据存储目录)设置指定的存储策略 hdfs storagepolicies -setStoragePo

Hadoop集群数据均衡之磁盘间数据均衡

生产环境,由于硬盘空间不足,往往需要增加一块硬盘。刚加载的硬盘没有数据时,可以执行磁盘数据均衡命令。(Hadoop3.x新特性) plan后面带的节点的名字必须是已经存在的,并且是需要均衡的节点。 如果节点不存在,会报如下错误: 如果节点只有一个硬盘的话,不会创建均衡计划: (1)生成均衡计划 hdfs diskbalancer -plan hadoop102 (2)执行均衡计划 hd

Andrej Karpathy最新采访:认知核心模型10亿参数就够了,AI会打破教育不公的僵局

夕小瑶科技说 原创  作者 | 海野 AI圈子的红人,AI大神Andrej Karpathy,曾是OpenAI联合创始人之一,特斯拉AI总监。上一次的动态是官宣创办一家名为 Eureka Labs 的人工智能+教育公司 ,宣布将长期致力于AI原生教育。 近日,Andrej Karpathy接受了No Priors(投资博客)的采访,与硅谷知名投资人 Sara Guo 和 Elad G

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

Retrieval-based-Voice-Conversion-WebUI模型构建指南

一、模型介绍 Retrieval-based-Voice-Conversion-WebUI(简称 RVC)模型是一个基于 VITS(Variational Inference with adversarial learning for end-to-end Text-to-Speech)的简单易用的语音转换框架。 具有以下特点 简单易用:RVC 模型通过简单易用的网页界面,使得用户无需深入了

透彻!驯服大型语言模型(LLMs)的五种方法,及具体方法选择思路

引言 随着时间的发展,大型语言模型不再停留在演示阶段而是逐步面向生产系统的应用,随着人们期望的不断增加,目标也发生了巨大的变化。在短短的几个月的时间里,人们对大模型的认识已经从对其zero-shot能力感到惊讶,转变为考虑改进模型质量、提高模型可用性。 「大语言模型(LLMs)其实就是利用高容量的模型架构(例如Transformer)对海量的、多种多样的数据分布进行建模得到,它包含了大量的先验