训练平台:colab
防断连代码:
function ClickConnect(){
console.log("Working");
document.querySelector("colab-toolbar-button#connect").click()
}
setInterval(ClickConnect,60000)
YOLO v4 Tiny convert to .tfile
Linux代码学习:
%cp -r /content/dataset/. /content/darknet/(将A复制到B,没有.的话就是默认当前文件夹)
.代表的是当前目录,可加可不加
with open('data/obj.data', 'w') as out:
out.write('classes = 3\n')
:打开文件data/obj.data写入classes=3换行
Write Custom Training Config for YOLOv4
Infer Custom Objects with Saved YOLOv4 Weights
刚刚colab又断连了,看了下这个技巧,应该有点用,先收藏吃灰吧。
4. Convert the TensorFlow weights to TensorFlow Lite
遇到问题:loc("batch_normalization/moving_mean"): is not immutable, try running tf-saved-model-optimize-global-tensors to prove tensors are immutable.(变量没有冻结,无法转换成.tfile文件)
tensorflow版本过高(colab直接都是安装最新的版本,所运行以前代码毛病多)
解决方法:!pip install tensorflow==2.3.0
链接:https://github.com/tensorflow/tensorflow/issues/44790
问题:cv2.error: OpenCV(4.1.2) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'
解决办法:不知道细心的小伙伴看到没,文件名称不匹配
BloodImage_00386_jpg
对应的的是.rf.c708422b2d9c642f200a853e70012850.jpg而执行代码的是rf.1de8e2e0e94f942d7a1523852d7fb146.jpg
替换掉就行了。
VOC 转换成 YOLO格式
打了六个小时标签,没有自动保存,全给清空了,我疯了呀!
自己打的标签还不能训练,不知道哪出了问题,还是用打好标签的voc数据集转换成yolodarknet格式:https://xugaoxiang.com/2021/06/22/voc-yolo-format-exchange/
脚本下载: https://pjreddie.com/media/files/voc_label.py
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
# 根据自己情况修改
classes = ["label"]
def convert(size, box):
dw = 1. / size[0]
dh = 1. / size[1]
x = (box[0] + box[1]) / 2.0
y = (box[2] + box[3]) / 2.0
w = box[1] - box[0]
h = box[3] - box[2]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return (x, y, w, h)
root_path = 'd:/Bubblling/Mobilenet-SSD-Essay/VOC to YOLO/'
def convert_annotation(image_id):
if not os.path.exists(os.path.join(root_path, 'Annotations/')+'%s.xml' % (image_id)):
print("Path err")
return
in_file = open(os.path.join(root_path, 'Annotations/') +
'%s.xml' % (image_id))
out_file = open("D:\Bubblling\Mobilenet-SSD-Essay\VOC to YOLO\labels\%s.txt" % (image_id), 'w')
tree = ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
print("path已读取")
for obj in root.iter('object'):
cls = obj.find('name').text
if cls not in classes:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
float(xmlbox.find('ymax').text))
bb = convert((w, h), b)
out_file.write(str(cls_id) + " " +
" ".join([str(a) for a in bb]) + '\n')
for image in os.listdir(root_path+'images'):
# 这里需要根据你的图片情况进行对应修改。比如图片名称是123.456.jpg,这里就会出错了。一般来讲,如果图片格式固定,如全都是jpg,那就image_id=image[:-4]处理就好了。总之,情况比较多,自己看着办,哈哈!
image_id = image.split('.')[0]
convert_annotation(image_id)