原生版python安装tensorflow以及object

it2022-05-05  154

安装参考:https://blog.csdn.net/sarsscofy/article/details/81111815

objectdetectionAPI参考:https://blog.csdn.net/dy_guox/article/details/79111949

https://github.com/EdjeElectronics/TensorFlow-Object-Detection-API-Tutorial-Train-Multiple-Objects-Windows-10

补充:

protoc下载:https://repo1.maven.org/maven2/com/google/protobuf/protoc/

labelImg下载:https://www.dropbox.com/speedbump/tq7zfrcwl44vxan/?content_link=https://uc63f15e1ad73b0025f5c8649014.dl.dropboxusercontent.com/cd/0/get/Ak62zFsNYqty7yrnxuVR0tyN71-1khkbXw6Btc6vY2o2GEI0gziQ_9P2K0xC3j6NjEHiz7KSB8ZY9d0PCgY23y9eUaH_2OSRrz_aXa3SpXStFw/file?dl=1#&hmac=ADqbaSs5GmO5x9GU7isX4SR6dgpJtBokotvz1g2o5RrICw

https://github.com/tzutalin/labelImg

安装基础步骤:

protobuf安装配置:将proto文件转化为py文件---相关模块包安装:pillow、lxml、pandas(用于后续生成TFRecord文件)、opencv---环境变量设置---编译protobufs并运行setup.py---安装成功测试一下demo收集图片,分为train与test(两成)labelImgxml_to_csv.py和generate_tfrecord.py(需修改路径及自己做的标签) python generate_tfrecord.py --csv_input=images\train_labels.csv --image_dir=images\train --output_path=train.record python generate_tfrecord.py --csv_input=images\test_labels.csv --image_dir=images\test --output_path=test.record 创建文件并保存于research \ object_detection \ training文件夹中的labelmap.pbtxt,键入自己的标签对应信息导航到research \ object_detection \ samples \ configs,并将faster_rcnn_inception_v2_pets.config文件复制到\ object_detection \ training目录中。然后,使用文本编辑器打开文件。要对.config文件进行一些更改,主要是更改类和示例的数量,以及将文件路径添加到训练数据:第9行:num_classes、第106行:将fine_tune_checkpoint更改fine_tune_checkpoint:“research/object_detection/faster_rcnn_inception_v2_coco_2018_01_28/model.ckpt”、第123和125行:在train_input_reader,nput_path和label_map_path更改、第130行:将num_examples更改为\ images \ test目录中的图像数、第135行和第137行:在eval_input_reader中,将input_path和label_map_path更改开始运行模型训练: python model_main.py --pipeline_config_path=training/ssd_mobilenet_v1_coco.config --model_dir=training --num_train_steps=50000 --num_eval_step=2000 --alsologtostderr

或者找到legacy文件夹中找到train.py并键入

python train.py --logtostderr --train_dir=training/ --pipeline_config_path=training/ssd_mobilenet_v1_coco.config

则:

 cpu版本速度太慢,后面用用可怜的MX150试试搭gpu加速吧!

在此过程中的发现:

proto一次性全部编译protoc object_detection/protos/*.proto --python_out=.时如出错,可将proto文件一条一条编译,将*.proto改为文件夹下每一条具体文件名即可测试demo时记得修改路径~labelImg.exe文件在做标签生成xml文件时会在其中记录当前文件路径、矩形框坐标、标签等信息,所以需先将图片切割resize并放在正确路径后再做标签,否则xml信息没法更新,再转化为csv文件和TFRecoard时将出错,若不对,可重新将图片打开,按空格键对每一张图片重新保存即可。参考:https://blog.csdn.net/qq_30129009/article/details/89513845如果在xml转化为csv文件时报错某图片的某个classes输入为空,代表在images/train或者test文件夹下存在未能与xml对应的图片,需删除或者使用labelImg添加xml

csv文件转换为tfrecord文件出错

几点需要确认一下:

1.  在object_detection.py 程序里,确认 NUM_CLASSES = 你自己的类别数;

2.  确认一下运行时候的路径有没有错(python generate_tfrecord.py --csv_input=data/tv_vehicle_labels.csv  --output_path=train.record)

3.  config 文件里 num_classes: 你自己的类别数

4.  pbtxt 文件下 item序号与名称与generate_tfrecord.py 里的序号名称必须一一对应。

将文件夹内所有图片按行列切割数切割: # -*- coding: utf-8 -*- import os from PIL import Image # 切割图片 def splitimage(src, rownum, colnum, dstpath): img = Image.open(src) w, h = img.size if rownum <= h and colnum <= w: print('Original image info: %sx%s, %s, %s' % (w, h, img.format, img.mode)) print('图片切割') s = os.path.split(src) if dstpath == '': dstpath = s[0] fn = s[1].split('.') basename = fn[0] ext = fn[-1] num = 0 rowheight = h // rownum colwidth = w // colnum for r in range(rownum): for c in range(colnum): box = (c * colwidth, r * rowheight, (c + 1) * colwidth, (r + 1) * rowheight) img.crop(box).save(os.path.join(dstpath, basename + '_' + str(num) + '.' + ext), ext) num = num + 1 print('共生成 %s 张小图片。' % num) else: print('error') # 创建文件夹 def mkdir(path): # 去除首位空格 path = path.strip() # 去除尾部 \ 符号 path = path.rstrip("\\") # 判断路径是否存在 # 存在 True # 不存在 False isExists = os.path.exists(path) # 判断结果 if not isExists: os.makedirs(path) print (path + ' 创建成功') return True else: print (path + ' 目录已存在') return False folder = r'E:/Desktop/AOI-PCB板短路检测/debug' # 存放图片的文件夹 path = os.listdir(folder) # print(path) for each_bmp in path: # 批量操作 first_name, second_name = os.path.splitext(each_bmp) each_bmp = os.path.join(folder, each_bmp) src = each_bmp print(src) print(first_name) # 定义要创建的目录 mkpath = "E:/Desktop/AOI-PCB板短路检测/splitdebug/" + first_name # 调用函数 mkdir(mkpath) if os.path.isfile(src): dstpath = mkpath if (dstpath == '') or os.path.exists(dstpath): row = int(8) # 切割行数 col = int(23) # 切割列数 if row > 0 and col > 0: splitimage(src, row, col, dstpath) else: print('无效的') else: print('图片保存目录 %s 不存在!' % dstpath) else: print('图片文件 %s 不存在!' % src)

 

文件统一转换格式:

import os from PIL import Image dirname_read="E:/Desktop/windows_v1.6.0/train_test/" #注意后面的斜杠 dirname_write="E:/Desktop/windows_v1.6.0/123/" names=os.listdir(dirname_read) count=0 for name in names: img=Image.open(dirname_read+name) name=name.split(".") if name[-1] == "bmp": name[-1] = "jpg" name = str.join(".", name) #r,g,b,a=img.split() #img=Image.merge("RGB",(r,g,b)) to_save_path = dirname_write + name img.save(to_save_path) count+=1 print(to_save_path, "------conut:",count) else: continue

 

图片训练好像单通道的没发跑,可手动改为三通道。

from PIL import Image import os path = '/Users/lyz/Desktop/dataset/images/' #图片目录 for file in os.listdir(path): extension = file.split('.')[-1] if extension == 'jpg': fileLoc = path+file img = Image.open(fileLoc) if img.mode != 'RGB': print(file+', '+img.mode)

 


最新回复(0)