训练模型杂货铺

训练过程中用到的代码与问题记事本

  1. 数据增强
from PIL import Image, ImageEnhance
import os

# 定义四种增强方式:亮度,对比度,色彩,锐度
enhancers = [ImageEnhance.Brightness, ImageEnhance.Contrast, ImageEnhance.Color, ImageEnhance.Sharpness]
enhancer_names = ['brightness', 'contrast', 'color', 'sharpness']

# 指定原始图片文件夹和目标图片文件夹
input_folder = 'E:\\code\\deep-learning-for-image-processing-master\\data_set\\eyes\\train\\Pathological Myopia (M)'
output_folder = 'E:\\code\\deep-learning-for-image-processing-master\\data_set\\eyes\\train\\Pathological Myopia (M)(1)'
# 如果输出文件夹不存在,则创建一个新的文件夹
if not os.path.exists(output_folder):
    os.makedirs(output_folder)
# 获取原始文件夹中所有图片文件
file_names = os.listdir(input_folder)
image_files = [f for f in file_names if f.endswith('.jpg') or f.endswith('.jpeg') or f.endswith('.png')]

# 遍历所有图片文件并进行四种增强方式的处理
for f in image_files:
    # 打开原始图片
    input_path = os.path.join(input_folder, f)
    input_image = Image.open(input_path)

    # 处理四种增强方式
    for enhancer, enhancer_name in zip(enhancers, enhancer_names):
        # 构建输出文件名
        output_file_name = f.split('.')[0] + '_' + enhancer_name + '.' + f.split('.')[1]
        output_path = os.path.join(output_folder, output_file_name)

        # 应用增强方式并保存
        enhanced_image = enhancer(input_image).enhance(1.5)  # 这里将增强系数设为1.5,可以根据需要自行调整
        enhanced_image.save(output_path)
  1. 数据集划分split data
import os
from shutil import copy, rmtree
import random


def mk_file(file_path: str):
    if os.path.exists(file_path):
        # 如果文件夹存在,则先删除原文件夹在重新创建
        rmtree(file_path)
    os.makedirs(file_path)


def main():
    # 保证随机可复现
    random.seed(0)

    # 将数据集中10%的数据划分到验证集中
    split_rate = 0.1

    # 指向你解压后的数据集文件夹
    cwd = os.getcwd()
    data_root = os.path.join(cwd, "eyes")
    origin_flower_path = os.path.join(data_root, "train2.0")
    assert os.path.exists(origin_flower_path), "path '{}' does not exist.".format(origin_flower_path)

    flower_class = [cla for cla in os.listdir(origin_flower_path)
                    if os.path.isdir(os.path.join(origin_flower_path, cla))]

    # 建立保存训练集的文件夹
    train_root = os.path.join(data_root, "train")
    mk_file(train_root)
    for cla in flower_class:
        # 建立每个类别对应的文件夹
        mk_file(os.path.join(train_root, cla))

    # 建立保存验证集的文件夹
    val_root = os.path.join(data_root, "val")
    mk_file(val_root)
    for cla in flower_class:
        # 建立每个类别对应的文件夹
        mk_file(os.path.join(val_root, cla))

    for cla in flower_class:
        cla_path = os.path.join(origin_flower_path, cla)
        images = os.listdir(cla_path)
        num = len(images)
        # 随机采样验证集的索引
        eval_index = random.sample(images, k=int(num*split_rate))
        for index, image in enumerate(images):
            if image in eval_index:
                # 将分配至验证集中的文件复制到相应目录
                image_path = os.path.join(cla_path, image)
                new_path = os.path.join(val_root, cla)
                copy(image_path, new_path)
            else:
                # 将分配至训练集中的文件复制到相应目录
                image_path = os.path.join(cla_path, image)
                new_path = os.path.join(train_root, cla)
                copy(image_path, new_path)
            print("\r[{}] processing [{}/{}]".format(cla, index+1, num), end="")  # processing bar
        print()

    print("processing done!")


if __name__ == '__main__':
    main()

3.xml2txt

  • xml中无图像width和height信息 (图像名与标签名对应)
  • 将name标签根据你的xml修改,此处我是label
import xml.etree.ElementTree as ET
import os
import cv2
from tqdm import tqdm

classes = ["holothurian", "echinus", "scallop", "starfish"]  # 类别
xml_path = "xml标签文件夹路径"
txt_path = "txt标签存储路径"
image_path = "图像文件夹路径"


# 将原有的xmax,xmin,ymax,ymin换为x,y,w,h
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)


# 输入时图像和图像的宽高
def convert_annotation(image_id, width, hight):
    in_file = open(xml_path + '\\{}.xml'.format(image_id), encoding='UTF-8')
    out_file = open(txt_path + '\\{}.txt'.format(image_id), 'w')  # 生成同名的txt格式文件
    tree = ET.parse(in_file)
    root = tree.getroot()
    size = root.find('size')	# 此处是获取原图的宽高,便于后续的归一化操作
    if size is not None:
        w = int(size.find('width').text)
        h = int(size.find('height').text)
    else:
        w = width
        h = hight
        
    for obj in root.iter('object'):
        cls = obj.find('name').text
        # print(cls)
        if cls not in classes:	# 此处会将cls里没有的类别打印,以便后续添加
            print(cls)
            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')


# 此处获取图像宽高的数组,tqdm为进度条库,将处理可视化
def image_size(path):
    image = os.listdir(path)
    w_l, h_l = [], []
    for i in tqdm(image):
        if i.endswith('jpg'):
            h_l.append(cv2.imread(os.path.join(path, i)).shape[0])
            w_l.append(cv2.imread(os.path.join(path, i)).shape[1])
    return w_l, h_l


# 遍历xml文件,将对应的宽高输入convert_annotation方法
if __name__  == "__main__":
    img_xmls = os.listdir(xml_path)
    w, h = image_size(image_path)
    i = 0
    for img_xml in img_xmls:
        label_name = img_xml.split('.')[0]
        print(label_name)
        convert_annotation(label_name, w[i], h[i])
        i += 1

  • xml中有图像width和height信息
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join


sets = ['train', 'test', 'val']
classes = ['classes1']
# 进行归一化操作
def convert(size, box): # size:(原图w,原图h) , box:(xmin,xmax,ymin,ymax)
    dw = 1./size[0]     # 1/w
    dh = 1./size[1]     # 1/h
    x = (box[0] + box[1])/2.0   # 物体在图中的中心点x坐标
    y = (box[2] + box[3])/2.0   # 物体在图中的中心点y坐标
    w = box[1] - box[0]         # 物体实际像素宽度
    h = box[3] - box[2]         # 物体实际像素高度
    x = x*dw    # 物体中心点x的坐标比(相当于 x/原图w)
    w = w*dw    # 物体宽度的宽度比(相当于 w/原图w)
    y = y*dh    # 物体中心点y的坐标比(相当于 y/原图h)
    h = h*dh    # 物体宽度的宽度比(相当于 h/原图h)
    return (x, y, w, h)    # 返回 相对于原图的物体中心点的x坐标比,y坐标比,宽度比,高度比,取值范围[0-1]
# year ='2012', 对应图片的id(文件名)
def convert_annotation(image_id):
    '''
    将对应文件名的xml文件转化为label文件,xml文件包含了对应的bunding框以及图片长款大小等信息,
    通过对其解析,然后进行归一化最终读到label文件中去,也就是说
    一张图片文件对应一个xml文件,然后通过解析和归一化,能够将对应的信息保存到唯一一个label文件中去
    labal文件中的格式:calss x y w h  同时,一张图片对应的类别有多个,所以对应的bunding的信息也有多个
    '''
    # 对应的通过year 找到相应的文件夹,并且打开相应image_id的xml文件,其对应bund文件
    in_file = open('data/Annotations/%s.xml' % (image_id), encoding='utf-8')
    # 准备在对应的image_id 中写入对应的label,分别为
    # <object-class> <x> <y> <width> <height>
    out_file = open('data/labels/%s.txt' % (image_id), 'w', encoding='utf-8')
    # 解析xml文件
    tree = ET.parse(in_file)
    # 获得对应的键值对
    root = tree.getroot()
    # 获得图片的尺寸大小
    size = root.find('size')
    # 如果xml内的标记为空,增加判断条件
    if size != None:
        # 获得宽
        w = int(size.find('width').text)
        # 获得高
        h = int(size.find('height').text)
        # 遍历目标obj
        for obj in root.iter('object'):
            # 获得difficult ??
            difficult = obj.find('difficult').text
            # 获得类别 =string 类型
            cls = obj.find('name').text
            # 如果类别不是对应在我们预定好的class文件中,或difficult==1则跳过
            if cls not in classes or int(difficult) == 1:
                continue
            # 通过类别名称找到id
            cls_id = classes.index(cls)
            # 找到bndbox 对象
            xmlbox = obj.find('bndbox')
            # 获取对应的bndbox的数组 = ['xmin','xmax','ymin','ymax']
            b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
                 float(xmlbox.find('ymax').text))
            print(image_id, cls, b)
            # 带入进行归一化操作
            # w = 宽, h = 高, b= bndbox的数组 = ['xmin','xmax','ymin','ymax']
            bb = convert((w, h), b)
            # bb 对应的是归一化后的(x,y,w,h)
            # 生成 calss x y w h 在label文件中
            out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')

# 返回当前工作目录
wd = getcwd()
print(wd)
for image_set in sets:
    '''
    对所有的文件数据集进行遍历
    做了两个工作:
    1.将所有图片文件都遍历一遍,并且将其所有的全路径都写在对应的txt文件中去,方便定位
    2.同时对所有的图片文件进行解析和转化,将其对应的bundingbox 以及类别的信息全部解析写到label 文件中去
         最后再通过直接读取文件,就能找到对应的label 信息
    '''
    # 先找labels文件夹如果不存在则创建
    if not os.path.exists('../data/labels/'):
        os.makedirs('../data/labels/')
    # 读取在ImageSets/Main 中的train、test..等文件的内容
    # 包含对应的文件名称
    image_ids = open('data/ImageSets/%s.txt' % (image_set)).read().strip().split()
    # 打开对应的2012_train.txt 文件对其进行写入准备
    list_file = open('data/%s.txt' % (image_set), 'w')
    # 将对应的文件_id以及全路径写进去并换行
    for image_id in image_ids:
        list_file.write('data/images/%s.jpg\n' % (image_id))
        # 调用  year = 年份  image_id = 对应的文件名_id
        convert_annotation(image_id)
    # 关闭文件
    list_file.close()

xml2json
首先需要安装一下这个库:

pip install xmltodict

import os
import json
import xmltodict

def xml_to_JSON(xml):
    # 格式转换
    try:
        convertJson = xmltodict.parse(xml, encoding = 'utf-8')
        jsonStr = json.dumps(convertJson, indent=1)
        return jsonStr
    except Exception:
        print('something has occurred')
        pass

def find_read_list(path):
    # 获取该文件夹下所有以.xml为后缀的文件
    file_list = os.listdir(path)
    read_list = []
    for i in file_list:
        a, b = os.path.splitext(i)
        if b == '.xml':
            read_list.append(i)
        else:
            continue
    return read_list

def batch_convert(path):
    # 主函数
    in_list = find_read_list(path)
    print(in_list)
    for item in in_list:
        with open(path+'\\'+item, encoding = 'utf-8') as f:
            xml = f.read()
            converted_doc = xml_to_JSON(xml)
        new_name = item.rsplit('.xml')[0] + '.json'
        with open(path+'\\'+new_name, 'w+',encoding = 'utf-8') as f:
            f.write(converted_doc)
            print('{} has finished'.format(new_name))

# 在这边输入文件夹路径,接下来就会把这个文件夹下所有以.xml为后缀的文件转换为.json文件
# 注意Python文件路径的输入格式 \\
batch_convert('E:\\code\\detr-main\\to\\coco\\annotations')

#将生成的json文件转存到另外一个文件夹中
# import os
# import shutil
#
# # 源文件夹路径
# src_folder = "E:\\code\\detr-main\\to\\coco\\annotations"
#
# # 目标文件夹路径
# dst_folder = "E:\\code\\detr-main\\to\\coco\\json"
#
# # 遍历源文件夹中的所有文件
# for filename in os.listdir(src_folder):
#     # 判断文件后缀是否为 json
#     if filename.endswith(".json"):
#         # 构造源文件路径和目标文件路径
#         src_file = os.path.join(src_folder, filename)
#         dst_file = os.path.join(dst_folder, filename)
#         # 使用 shutil.copy() 函数复制文件到目标文件夹
#         shutil.move(src_file, dst_file)
#         print(f"已转存文件 {src_file} 到 {dst_file}")
  1. 分类个数
import os
from unicodedata import name
import xml.etree.ElementTree as ET
import glob


def count_num(indir):
    label_list = []
    # 提取xml文件列表
    os.chdir(indir)
    annotations = os.listdir('..')
    annotations = glob.glob(str(annotations) + '*.xml')

    dict = {}  # 新建字典,用于存放各类标签名及其对应的数目
    for i, file in enumerate(annotations):  # 遍历xml文件

        # actual parsing
        in_file = open(file, encoding='utf-8')
        tree = ET.parse(in_file)
        root = tree.getroot()

        # 遍历文件的所有标签
        for obj in root.iter('object'):
            label = obj.find('label').text
            if (label in dict.keys()):
                dict[label] += 1  # 如果标签不是第一次出现,则+1
            else:
                dict[label] = 1  # 如果标签是第一次出现,则将该标签名对应的value初始化为1

    # 打印结果
    print("各类标签的数量分别为:")
    for key in dict.keys():
        print(key + ': ' + str(dict[key]))
        label_list.append(key)
    print("标签类别如下:")
    print(label_list)


if __name__ == '__main__':
    # xml文件所在的目录,修改此处
    indir = 'data/Annotations'
    count_num(indir)  # 调用函数统计各类标签数目

5自动标注数据集数字

import json
import os

import base64
import urllib
import requests
import json

API_KEY = "UGQwds8RCP6ZbPA7WaPioNxi"
SECRET_KEY = "7NVYmCEA2NDfaLG7qnbemEutzzX17PgM"

def doFacePoint(imgName):
    url = "https://aip.baidubce.com/rest/2.0/face/v1/landmark?access_token=" + get_access_token()

    # image 可以通过 get_file_content_as_base64("C:\fakepath\004753.jpg",False) 方法获取
    image = get_file_content_as_base64("D:/DeepLearningDataSets/口罩和人脸关键点标注/5_4750_5711/5_4750_5711/JPEGImages/"+imgName,False)
    payload = json.dumps({
        "image": image,
        "image_type": "BASE64",
        "max_face_num": "10",
        "face_field": "landmark72"
    })
    headers = {
        'Content-Type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload)

    print(response.text)
    return response.text

def get_file_content_as_base64(path, urlencoded=False):
    """
    获取文件base64编码
    :param path: 文件路径
    :param urlencoded: 是否对结果进行urlencoded
    :return: base64编码信息
    """
    with open(path, "rb") as f:
        content = base64.b64encode(f.read()).decode("utf8")
        if urlencoded:
            content = urllib.parse.quote_plus(content)
    return content


def get_access_token():
    """
    使用 AK,SK 生成鉴权签名(Access Token)
    :return: access_token,或是None(如果错误)
    """
    url = "https://aip.baidubce.com/oauth/2.0/token"
    params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
    return str(requests.post(url, params=params).json().get("access_token"))



def labelMehh(imgName):
    path = "D:/DeepLearningDataSets/口罩和人脸关键点标注/5_4750_5711/5_4750_5711/Annotations/"+str(imgName)
    result = open(path, "r", encoding="utf-8")
    data = json.load(result)

    objects = data["shapes"]
    imgPath = data["imagePath"]

    # print(imgPath)
    # 先遍历所有的目标,把是人的框先拿出来
    person_id = 0
    persons = []  # 人的字典
    others = []  # 除了person的其他
    add_face_points = []  # 新增人脸关键点的内容
    for one_object in objects:
        if one_object["label"] == "person":
            one_object["group_id"] = person_id
            person_id = person_id + 1
            persons.append(one_object)
        else:
            others.append(one_object)

    # print("------------")
    # print(persons)
    # print("------------")
    # print(others)
    # print("------------")
    imgNameToFaceDetect = imgName.split('.')[0] + '.jpg'
    faces = doFacePoint(imgNameToFaceDetect)
    facesJson = json.loads(faces)
    if facesJson["error_msg"] == "SUCCESS":
        landmark72Datas = facesJson["result"]["face_list"]
        for person in landmark72Datas:
            # {'description': '', 'label': 'left_eye', 'points': [[643.0, 99.8]], 'group_id': 0, 'shape_type': 'point', 'flags': {}}
            # {'description': '', 'label': 'right_eye', 'points': [[662.5, 101.5]], 'group_id': 0, 'shape_type': 'point', 'flags': {}}
            # {'description': '', 'label': 'nose', 'points': [[652.8, 113.3]], 'group_id': 0, 'shape_type': 'point', 'flags': {}}
            # {'description': '', 'label': 'left_mouth', 'points': [[646.6, 118.2]], 'group_id': 0, 'shape_type': 'point',
            # {'description': '', 'label': 'right_mouth', 'points': [[597.1, 82.7]], 'group_id': 2, 'shape_type': 'point', 'flags': {}}
            # if other["label"] == "head":

            # head_point_x0 = int(other["points"][0][0])
            # head_point_x1 = int(other["points"][1][0])
            # head_point_y0 = int(other["points"][0][1])
            # head_point_y1 = int(other["points"][1][1])
            # x_center = (head_point_x0 + head_point_x1) / 2
            # y_center = (head_point_y0 + head_point_y1) / 2
            # width = head_point_x1 - head_point_x0
            # height = head_point_y1 - head_point_y0
            # 标注五个关键点(根据head的相对位置来标注)
            lfe_x = person["landmark72"][21]['x']  # 左眼
            lfe_y = person["landmark72"][21]['y']  # 左眼

            re_x = person["landmark72"][38]['x']  # 右眼
            re_y = person["landmark72"][38]['y']  # 右眼

            nose_x = person["landmark72"][57]['x']  # 鼻尖
            nose_y = person["landmark72"][57]['y']  # 鼻尖

            lfm_x = person["landmark72"][58]['x']  # 左嘴
            lfm_y = person["landmark72"][58]['y']  # 左嘴

            rfm_x = person["landmark72"][62]['x']  # 右嘴
            rfm_y = person["landmark72"][62]['y']  # 右嘴
            left_eye = {'description': '', 'label': 'left_eye',
                        'points': [[int(lfe_x), int(lfe_y)]], 'shape_type': 'point', 'flags': {}}
            right_eye = {'description': '', 'label': 'right_eye',
                         'points': [[int(re_x), int(re_y)]], 'shape_type': 'point', 'flags': {}}
            nose = {'description': '', 'label': 'nose', 'points': [[int(nose_x), int(nose_y)]], 'shape_type': 'point',
                    'flags': {}}
            left_mouth = {'description': '', 'label': 'left_mouth',
                          'points': [[int(lfm_x), int(lfm_y)]], 'shape_type': 'point', 'flags': {}}
            right_mouth = {'description': '', 'label': 'right_mouth',
                           'points': [[int(rfm_x), int(rfm_y)]], 'shape_type': 'point',
                           'flags': {}}
            others.append(left_eye)
            others.append(right_eye)
            others.append(nose)
            others.append(left_mouth)
            others.append(right_mouth)
    # 标注ID
    for other in others:
        for person in persons:
            person_point_x0 = int(person["points"][0][0])
            person_point_x1 = int(person["points"][1][0])
            person_point_y0 = int(person["points"][0][1])
            person_point_y1 = int(person["points"][1][1])
            # 方框的东西
            if len(other["points"]) == 2:
                other_point_x = (int(other["points"][0][0]) + int(other["points"][1][0])) / 2
                other_point_y = (int(other["points"][0][1]) + int(other["points"][1][1])) / 2
                if other_point_x >= person_point_x0 and other_point_x <= person_point_x1 and other_point_y >= person_point_y0 and other_point_y <= person_point_y1:
                    other["group_id"] = person["group_id"]
            # 关键点的东西
            else:
                other_point_x = int(other["points"][0][0])
                other_point_y = int(other["points"][0][1])
                if other_point_x >= person_point_x0 and other_point_x <= person_point_x1 and other_point_y >= person_point_y0 and other_point_y <= person_point_y1:
                    other["group_id"] = person["group_id"]

    objects = persons + others
    # print(objects)
    data["shapes"] = objects
    vert_json = json.dumps(data)
    # print(vert_json)

    with open("D:/DeepLearningDataSets/口罩和人脸关键点标注/5_4750_5711/5_4750_5711/newBaiDuAnnotations/"+imgPath.split(".")[0] + ".json", 'w', encoding='utf8') as f2:
        # ensure_ascii=False才能输入中文,否则是Unicode字符
        # indent=2 JSON数据的缩进,美观
        json.dump(data, f2, ensure_ascii=False, indent=2)

        # print(str(len(other["points"]))+str(other["label"]))
if __name__ == '__main__':
    COOKED_FOLDER = 'D:/DeepLearningDataSets/口罩和人脸关键点标注/5_4750_5711/5_4750_5711/Annotations/'  # 文件夹的地址
    dirs = os.listdir(COOKED_FOLDER)
    for dir in dirs:
        labelMehh(dir)
        print(dir)
    print("完成")


6根据xml文件统计目标的平均长度、宽度、面积以及每一个目标在原图中的占比

# -*- coding:utf-8 -*-
#统计
# 计算每一个目标在原图中的占比
# 计算目标的平均长度、
# 计算平均宽度,
# 计算平均面积、
# 计算目标平均占比
 
import os
import xml.etree.ElementTree as ET
import numpy as np
 
#np.set_printoptions(suppress=True, threshold=np.nan)  #10,000,000
np.set_printoptions(suppress=True, threshold=10000000)  #10,000,000
import matplotlib
from PIL import Image
 
 
def parse_obj(xml_path, filename):
    tree = ET.parse(xml_path + filename)
    objects = []
    for obj in tree.findall('object'):
        obj_struct = {}
        obj_struct['name'] = obj.find('name').text
        bbox = obj.find('bndbox')
        obj_struct['bbox'] = [int(bbox.find('xmin').text),
                              int(bbox.find('ymin').text),
                              int(bbox.find('xmax').text),
                              int(bbox.find('ymax').text)]
        objects.append(obj_struct)
    return objects
 
 
def read_image(image_path, filename):
    im = Image.open(image_path + filename)
    W = im.size[0]
    H = im.size[1]
    area = W * H
    im_info = [W, H, area]
    return im_info
 
 
if __name__ == '__main__':
    image_path = 'C:/Users/YANG/Music/VOC2007/JPEGImages/'
    xml_path = 'C:/Users/YANG/Music/VOC2007/Annotations/'
    filenamess = os.listdir(xml_path)
    filenames = []
    for name in filenamess:
        name = name.replace('.xml', '')
        filenames.append(name)
    print(filenames)
    recs = {}
    ims_info = {}
    obs_shape = {}
    classnames = []
    num_objs={}
    obj_avg = {}
    for i, name in enumerate(filenames):
        print('正在处理 {}.xml '.format(name))
        recs[name] = parse_obj(xml_path, name + '.xml')
        print('正在处理 {}.jpg '.format(name))
        ims_info[name] = read_image(image_path, name + '.jpg')
    print('所有信息收集完毕。')
    print('正在处理信息......')
    for name in filenames:
        im_w = ims_info[name][0]
        im_h = ims_info[name][1]
        im_area = ims_info[name][2]
        for object in recs[name]:
            if object['name'] not in num_objs.keys():
                num_objs[object['name']] = 1
            else:
                num_objs[object['name']] += 1
            #num_objs += 1
            ob_w = object['bbox'][2] - object['bbox'][0]
            ob_h = object['bbox'][3] - object['bbox'][1]
            ob_area = ob_w * ob_h
            w_rate = ob_w / im_w
            h_rate = ob_h / im_h
            area_rate = ob_area / im_area
            if not object['name'] in obs_shape.keys():
                obs_shape[object['name']] = ([[ob_w,
                                               ob_h,
                                               ob_area,
                                               w_rate,
                                               h_rate,
                                               area_rate]])
            else:
                obs_shape[object['name']].append([ob_w,
                                                  ob_h,
                                                  ob_area,
                                                  w_rate,
                                                  h_rate,
                                                  area_rate])
        if object['name'] not in classnames:
            classnames.append(object['name'])  # 求平均
 
    for name in classnames:
        obj_avg[name] = (np.array(obs_shape[name]).sum(axis=0)) / num_objs[name]
        print('{}的情况如下:*******\n'.format(name))
        print('  目标平均W={}'.format(obj_avg[name][0]))
        print('  目标平均H={}'.format(obj_avg[name][1]))
        print('  目标平均area={}'.format(obj_avg[name][2]))
        print('  目标平均与原图的W比例={}'.format(obj_avg[name][3]))
        print('  目标平均与原图的H比例={}'.format(obj_avg[name][4]))
        print('  目标平均原图面积占比={}\n'.format(obj_avg[name][5]))
    print('信息统计计算完毕。')

*出现nan
*可能是因为设置了半精度
在trainer中找到train方法,点击_ontrain中找到amp进入
在这前面加入self.amp=false关闭半精度看看是否解决

# Check AMP
self.amp = torch.tensor(self.args.amp).to(self.device)  # True or False
if self.amp and RANK in (-1, 0):  # Single-GPU and DDP
    callbacks_backup = callbacks.default_callbacks.copy()  # backup callbacks as check_amp() resets them
    self**斜体样式**.amp = torch.tensor(check_amp(self.model), device=self.device)
    callbacks.default_callbacks = callbacks_backup  # restore callbacks
if RANK > -1:  # DDP
    dist.broadcast(self.amp, src=0)  # broadcast the tensor from rank 0 to all other ranks (returns None)
self.amp = bool(self.amp)  # as boolean
self.scaler = amp.GradScaler(enabled=self.amp)

或直接在default中将amp参数改为false

Pip不成功
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by ‘SSLError(SSLError(1, ‘[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1123)’))’: /simple/pip/Could not fetch URL https://pypi.org/simple/pip/:
解决方案:在C:/Users/Administrator/路径下,新建pip文件夹,并新建pip.ini文件,文件内容如下:

[global]
index-url=http://pypi.douban.com/simple/
[install]
trusted-host=pypi.douban.com

保存。并在环境变量中新增“C:/Users/Administrator/pip”即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值