tensorflow object detection api进行模型训练,用的模型是自带的,已经到了开始训练 文件设置路径都没问题
python C:\Users\Administrator\models\research\object_detection\model_main_tf2.py ^
--model_dir=C:/Users/Administrator/models/research/object_detection/training ^
--pipeline_config_path=C:/Users/Administrator/models/research/object_detection/training/ssd_efficientdet_d1_0x0_coco17_tpu-8.config ^
--num_train_steps=20000 ^
--sample_1_of_n_eval_examples=9
结果报错
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd5 in position 158: invalid continuation byte
请问如何能解决
import os
pic_path = “/Users/cathy/Desktop/models/research/object_detection/test_images/test”
def rename():
piclist = os.listdir(pic_path)
total_num = len(piclist)
i = 1
for pic in piclist:
if pic.endswith(".jpg"):
old_path = os.path.join(os.path.abspath(pic_path), pic) # os.path.abspath获得绝对路径
new_path = os.path.join(os.path.abspath(pic_path), '000' + format(str(i), '0>3') + '.jpg')
os.renames(old_path, new_path)
print
u"把原图片命名格式:" + old_path + u"转换为新图片命名格式:" + new_path
# print "把原图片路径:%s,转换为新图片路径:%s" %(old_path,new_path)
i = i + 1
print("总共" + str(total_num) + "张图片被重命名为:" "00001.jpg~" + '000' + format(str(i - 1), '0>3') + ".jpg形式")
rename()
3.在训练模型的过程中,图片必须是RGB通道,否则也会出错。接下来我写了两个py文件进行图片处理。
第一个py文件:判定文件夹中的图片是否为RGB,检查图片是否RGB.py
from PIL import Image
import os
path = ‘/Users/cathy/Desktop/models/research/object_detection/test_images/train/’ # 图片目录
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)
第二个文件:将不是RGB的图片变为RGB,图片变为RGB.py
from PIL import Image
import os
path = ‘/Users/cathy/Desktop/models/research/object_detection/test_images/train/’ # 图片目录
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)
img_rgb = img.convert("RGB")#转化成RGB模式
os.remove(path + file)
img_rgb.save(path + file)
4.上面对于图片的处理告一段落,接下来对图片进行标注,使用的是labellmg,地址在这里https://github.com/tzutalin/labelImg#macos
我用的是mac,因此用的mac底下的那些命令进行安装
环境:python3.7 Qt5
brew install qt # Install qt-5.x.x by Homebrew
brew install libxml2
pyrcc5-o/Users/cathy/Desktop/labelImg-master/resources.py /Users/cathy/Desktop/labelImg-master/resources.qrc#根据自己的地址进行相应的修改
python3 /Users/cathy/Desktop/labelImg-master/labelImg.py
做完以上工作,会跑出来一个小程序,接下来我指出较为重要的功能:
选择目录和存放目录后,点击w,然后选择物体,然后标注名称,接着点击保存。点击d下一张继续以上的工作。
我将xml文件分别存放在了一下文件夹中:
/Users/cathy/Desktop/models/research/object_detection/test_images/train_xml
/Users/cathy/Desktop/models/research/object_detection/test_images/test_xml
5.生成了xml文件之后,还要将他们转为csv文件,xml_to_csv.py文件如下,运行即可: