主頁 > 後端開發 > 利用COCO API測驗自己資料集訓練的YOLOv3模型的mAP(VOC格式資料集)

利用COCO API測驗自己資料集訓練的YOLOv3模型的mAP(VOC格式資料集)

2021-01-13 10:56:22 後端開發

目錄

  • 工具
  • 前言
  • 生成標注集的json檔案
    • 資料集準備
    • 將voc注解格式資料集的注解轉換成txt注解格式
    • 自定義資料集的注解轉換成coco的注解格式
  • 生成結果集的json檔案
    • 安裝darknet
    • 獲取自己模型的.weight檔案(將.h5檔案轉換成.weight檔案)
    • 將影像以coco格式重命名
    • 修改coco.data中的路徑
    • 修改yolov3.cfg檔案
    • 進行檢測并生成json檔案
  • 測驗mAP步驟
  • 錯誤問題解決
  • 參考博文及Github專案(十分感謝!)

工具

1.git:去git官網下載:https://git-scm.com/downloads/,下載自己需要的版本,下載完成后按照默認步驟安裝即可

2.pycocotools:測驗mAP時需要用到,參照https://blog.csdn.net/SyliaJason/article/details/103066638 進行安裝(Win10系統)

3.Advanced Renamer:批量重命名工具——https://www.advancedrenamer.com/ ,批量更改資料集名稱時可能會用到,

前言

對于不同資料集mAP值的計算方法不同,VOC2007提出了利用11個recall值來計算AP,而在2010之后使用了所有資料點來計算AP,COCO資料集采用的計算方式更加嚴格,它計算了不同IOU閾值和物體大小下的AP值,再取平均值,

本文參考了利用COCO API評估YOLOv3模型mAP的相關文章,這里總結了如何評估自己訓練出的yolov3模型的mAP,其中自制資料集參考了VOC資料集的格式存放,

測驗mAP需要兩個json檔案:cocoGt_file 和 cocoDt_file,一個是經過正確標注的標注集的json檔案,一個是通過自己訓練的YOLOv3模型進行檢測而生成的結果集的json檔案,這可以通過mAP的定義來理解,

下面我將分別介紹如何生成所需要的這兩個json檔案,進行mAP測驗,
【文章默認已經準備好了帶有xml標注的資料集,并且訓練好了自己的yolo.h5模型】

生成標注集的json檔案

資料集準備

我使用的是VOC格式的自制資料集,要生成COCO資料集需要的json檔案,需要對資料集進行處理,
在這里插入圖片個描述 在這里插入圖片描述

我這里需要使用的僅僅是測驗集,所以只需要用到test.txt,該檔案保存的是
測驗集的影像名稱,

將voc注解格式資料集的注解轉換成txt注解格式

在自己的專案檔案夾下新建1_voc2txt.py檔案,輸入如下代碼,注意根據自己的實際情況更改資料集的路徑,并且在VOCdevkit/VOC2007/Annotations檔案夾下需要存放標注的.xml檔案,

import os
import shutil



'''
將 dataset_dir 改為你的資料集的路徑,
生成的txt注解檔案格式為:
圖片名 物體1左上角x坐標,物體1左上角y坐標,物體1右下角x坐標,物體1右下角y坐標,物體1類別id 物體2左上角x坐標,物體2左上角y坐標,物體2右下角x坐標,物體2右下角y坐標,物體2類別id ...

train_difficult控制是否訓練難例,use_default_label控制是否使用默認的類別檔案,
'''


# 是否訓練難例,
train_difficult = True
# train_difficult = False


# 是否使用默認的類別檔案,
use_default_label = True
# use_default_label = False


dataset_dir = 'VOCdevkit/VOC2007/'
train_path = dataset_dir + 'ImageSets/Main/train.txt'
val_path = dataset_dir + 'ImageSets/Main/val.txt'
test_path = dataset_dir + 'ImageSets/Main/test.txt'
#test_path = None
annos_dir = dataset_dir + 'Annotations/'


# 保存的txt注解檔案的檔案名
train_txt_name = 'voc2007_train.txt'
val_txt_name = 'voc2007_val.txt'
test_txt_name = 'voc2007_test.txt'



class_names = []
class_names_ids = {}
cid_index = 0


if use_default_label:
    # class_txt_name指向已有的類別檔案,一行一個類別名,類別id根據這個類別檔案中類別名在第幾行確定,
    # 如果只訓練該資料集的部分類別,那么編輯該類別檔案,只留下所需類別的類別名即可,
    class_txt_name = 'model_data/voc_classes.txt'
    if not os.path.exists(class_txt_name):
        raise FileNotFoundError("%s does not exist!" % class_txt_name)
    with open(class_txt_name, 'r', encoding='utf-8') as f:
        for line in f:
            cname = line.strip()
            class_names.append(cname)
            class_names_ids[cname] = cid_index
            cid_index += 1
else:   # 如果不使用默認的類別檔案,則會分析出有幾個類別,生成一個類別檔案,
    # 保存的類別檔案名
    class_txt_name = 'data/class_names.txt'



train_names = []
val_names = []
test_names = []

with open(train_path, 'r', encoding='utf-8') as f:
    for line in f:
        line = line.strip()
        train_names.append(line)
with open(val_path, 'r', encoding='utf-8') as f:
    for line in f:
        line = line.strip()
        val_names.append(line)
if test_path is not None:
    with open(test_path, 'r', encoding='utf-8') as f:
        for line in f:
            line = line.strip()
            test_names.append(line)




# 創建txt注解目錄
if os.path.exists('annotation/'): shutil.rmtree('annotation/')
os.mkdir('annotation/')


def write_txt(xml_names, annos_dir, txt_name, use_default_label, train_difficult, class_names, class_names_ids, cid_index):
    content = ''
    for xml_name in xml_names:
        xml_file = '%s%s.xml'%(annos_dir, xml_name)
        enter_gt = False
        enter_part = False
        x0, y0, x1, y1, cid = '', '', '', '', -10
        difficult = 0
        img_name = ''
        bboxes = ''
        with open(xml_file, 'r', encoding='utf-8') as f:
            for line in f:
                line = line.strip()
                if '<filename>' in line:
                    if '</filename>' in line:
                        ss = line.split('name>')
                        sss = ss[1].split('</file')
                        img_name = sss[0]
                    else:
                        print('Error 1.')
                if '<object>' in line:
                    if '</object>' in line:
                        print('Error 2.')
                    else:
                        enter_gt = True
                if '</object>' in line:
                    if cid > -5:
                        if train_difficult:
                            bboxes += ' %s,%s,%s,%s,%d'%(x0, y0, x1, y1, cid)
                        else:
                            if difficult == 0:
                                bboxes += ' %s,%s,%s,%s,%d'%(x0, y0, x1, y1, cid)
                    x0, y0, x1, y1, cid = '', '', '', '', -10
                    difficult = 0
                    enter_gt = False
                    enter_part = False
                if enter_gt:
                    if '<part>' in line:   # <object>里會有<part>節點,我們要忽略<part>節點,
                        if '</part>' in line:
                            print('Error part.')
                        else:
                            enter_part = True
                    if '</part>' in line:
                        enter_part = False
                    if not enter_part:
                        if '<name>' in line:
                            if '</name>' in line:
                                ss = line.split('name>')
                                sss = ss[1].split('</')
                                cname = sss[0]
                                if use_default_label:
                                    if cname not in class_names:
                                        cid = -10
                                    else:
                                        cid = class_names_ids[cname]
                                else:
                                    if cname not in class_names:
                                        class_names.append(cname)
                                        class_names_ids[cname] = cid_index
                                        cid_index += 1
                                    cid = class_names_ids[cname]
                            else:
                                print('Error 3.')
                        if '<xmin>' in line:
                            if '</xmin>' in line:
                                ss = line.split('xmin>')
                                sss = ss[1].split('</')
                                x0 = sss[0]
                            else:
                                print('Error 4.')
                        if '<ymin>' in line:
                            if '</ymin>' in line:
                                ss = line.split('ymin>')
                                sss = ss[1].split('</')
                                y0 = sss[0]
                            else:
                                print('Error 5.')
                        if '<xmax>' in line:
                            if '</xmax>' in line:
                                ss = line.split('xmax>')
                                sss = ss[1].split('</')
                                x1 = sss[0]
                            else:
                                print('Error 6.')
                        if '<ymax>' in line:
                            if '</ymax>' in line:
                                ss = line.split('ymax>')
                                sss = ss[1].split('</')
                                y1 = sss[0]
                            else:
                                print('Error 7.')
                        if '<difficult>' in line:
                            if '</difficult>' in line:
                                ss = line.split('difficult>')
                                sss = ss[1].split('</')
                                difficult = int(sss[0])
                            else:
                                print('Error 8.')
        content += img_name + bboxes + '\n'
    with open('annotation/%s' % txt_name, 'w', encoding='utf-8') as f:
        f.write(content)
        f.close()
    return class_names, class_names_ids, cid_index


# train set
class_names, class_names_ids, cid_index = write_txt(train_names, annos_dir, train_txt_name,
                                                    use_default_label, train_difficult, class_names, class_names_ids, cid_index)

# val set
class_names, class_names_ids, cid_index = write_txt(val_names, annos_dir, val_txt_name,
                                                    use_default_label, train_difficult, class_names, class_names_ids, cid_index)

# test set
if test_path is not None:
    class_names, class_names_ids, cid_index = write_txt(test_names, annos_dir, test_txt_name,
                                                        use_default_label, train_difficult, class_names, class_names_ids, cid_index)


if not use_default_label:
    num_classes = len(class_names)
    content = ''
    for cid in range(num_classes):
        for cname in class_names_ids.keys():
            if cid == class_names_ids[cname]:
                content += cname + '\n'
                break

    if not os.path.exists('data/'): os.mkdir('data/')
    with open(class_txt_name, 'w', encoding='utf-8') as f:
        f.write(content)
        f.close()

print('Done.')

運行后生成一個annotation檔案夾,保存txt注解格式,如下圖,

在這里插入圖片描述

自定義資料集的注解轉換成coco的注解格式

同樣新建一個1_txt2json.py檔案,輸入如下代碼,這段代碼參考了Github上的專案:https://github.com/miemie2013/Keras-YOLOv4,我在im_id處做了修改,以便于匹配訓練生成的image_id的格式,

#! /usr/bin/env python
# coding=utf-8
# ================================================================
#
#   Author      : miemie2013
#   Created date: 2020-05-20 15:35:27
#   Description : Convert annotation files (txt format) into coco json format.
#                 自定義資料集的注解轉換成coco的注解格式,生成的json注解檔案在annotation_json目錄下,
#
# ================================================================
import os
import cv2
import json
import copy
import shutil

def get_classes(classes_path):
    with open(classes_path) as f:
        class_names = f.readlines()
    class_names = [c.strip() for c in class_names]
    return class_names

def write_json(txt_path, img_path, base_json, anno_name, im_id, anno_id):
    target_json = copy.deepcopy(base_json)
    with open(txt_path) as f:
        txt_lines = f.readlines()
    images = []
    annos = []
    for line in txt_lines:
        anno_list = line.split()
        ndarr = cv2.imread(img_path + anno_list[0])
        img_h, img_w, _ = ndarr.shape
        im_id=int((((line.split())[0]).split("."))[0])
        image = {
            'license': 1,
            'file_name': anno_list[0],
            'coco_url': 'a',
            'height': img_h,
            'width': img_w,
            'date_captured': 'a',
            'flickr_url': 'a',
            'id': im_id,
        }
        images.append(image)
        for p in range(1, len(anno_list), 1):
            bbox = anno_list[p].split(',')
            x1 = float(bbox[0])
            y1 = float(bbox[1])
            x2 = float(bbox[2])
            y2 = float(bbox[3])
            cid = int(bbox[4])
            w = x2 - x1
            h = y2 - y1
            anno = {
                'segmentation': [[x2, y2, x2, y1, x1, y1, x1, y2, x2, y2]],
                'area': w*h,
                'iscrowd': 0,
                'image_id': im_id,
                'bbox': [x1, y1, w, h],
                'category_id': cid,
                'id': anno_id,
            }
            annos.append(anno)
            anno_id += 1
        #im_id += 1
    target_json['annotations'] = annos
    target_json['images'] = images
    filename = anno_name[0] #這里我根據自己存放測驗集的txt格式做了修改
    if '/' in anno_name[0]:
        filename = anno_name[0].split('/')[-1]
    with open('annotation_json/%s.json' % filename, 'w') as f2:
        json.dump(target_json, f2)
    return im_id, anno_id


if __name__ == '__main__':
    # 自定義資料集的注解轉換成coco的注解格式,只需改下面7個即可,檔案夾下的子目錄(子檔案)用/隔開,而不能用\或\\,
    train_path = 'annotation/voc2007_train.txt'
    val_path = 'annotation/voc2007_val.txt'
    test_path = 'annotation/voc2007_test.txt'   # 如果沒有測驗集,填None;如果有測驗集,填txt注解檔案的路徑,
    classes_path = 'model_data/voc_classes.txt'
    train_pre_path = 'VOCdevkit/VOC2007/JPEGImages/'   # 訓練集圖片相對路徑
    val_pre_path = 'VOCdevkit/VOC2007/JPEGImages/'     # 驗證集圖片相對路徑
    test_pre_path = 'VOCdevkit/VOC2007/JPEGImages/'    # 測驗集圖片相對路徑


    # 創建json注解目錄
    if os.path.exists('annotation_json/'): shutil.rmtree('annotation_json/')
    os.mkdir('annotation_json/')

    train_anno_name = train_path.split('.')
    val_anno_name = val_path.split('.')
    print('Convert annotation files (txt format) into coco json format...')
    info = {
        'description': 'My dataset',
        'url': 'https://github.com/miemie2013',
        'version': '1.0',
        'year': '2020',
        'contributor': 'miemie2013',
        'date_created': '2020/06/01',
    }
    licenses_0 = {
        'url': 'https://github.com/miemie2013',
        'id': 1,
        'name': 'miemie2013 license',
    }
    licenses = [licenses_0]
    categories = []
    class_names = get_classes(classes_path)
    num_classes = len(class_names)
    for cid in range(num_classes):
        cate = {
            'supercategory': 'object',
            'id': cid,
            'name': class_names[cid],
        }
        categories.append(cate)
    base_json = {
        'info': info,
        'licenses': licenses,
        'categories': categories,
    }
    im_id = 0
    anno_id = 0

    # train set
    im_id, anno_id = write_json(train_path, train_pre_path, base_json, train_anno_name, im_id, anno_id)

    # val set
    im_id, anno_id = write_json(val_path, val_pre_path, base_json, val_anno_name, im_id, anno_id)

    # test set
    if test_path is not None:
        test_anno_name = test_path.split('.')
        im_id, anno_id = write_json(test_path, test_pre_path, base_json, test_anno_name, im_id, anno_id)

    print('Done.')

運行后生成的json注解檔案在專案檔案夾的annotation_json目錄下,格式如下,該檔案就是標注集的json檔案,記住它的路徑,
在這里插入圖片描述

生成結果集的json檔案

安裝darknet

在終端輸入命令從github上clone原始碼,或者從該鏈接直接下載.zip檔案,

git clone https://github.com/pjreddie/darknet.git

darknet檔案格式如下:
在這里插入圖片描述

想要用GPU進行檢測的可以將darknet-master/Makefile檔案中第一行的GPU=0改為GPU=1,我這里沒有進行修改,進入到darknet檔案中

cd darknet

編譯(windows系統需要自行下載Cygwin,參考https://blog.csdn.net/chunleixiahe/article/details/55666792來安裝,使得darknet可以編譯)

make

編譯結束后,會產生darknet.exe、libdarknet.a、libdarknet.so檔案,將darknet.exe所在檔案夾添加到環境變數當中去,即可使用darknet命令,

獲取自己模型的.weight檔案(將.h5檔案轉換成.weight檔案)

通常情況下,我們訓練好的YOLOv3模型都是.h5檔案,而后續生成COCO資料集需要的json檔案則需要用到.weight檔案,所以需要進行轉換,

這里參考文章https://blog.csdn.net/qinchang1/article/details/105776132,將自己訓練好的.h5檔案轉換成.weight檔案,(注意修改model_path為自己的.h5檔案名稱)

轉換完會生成自己的.weight檔案,復制到darknet-master/backup當中去,
在這里插入圖片描述 在這里插入圖片描述

將影像以coco格式重命名

作者在檢測時發現按照原來的000001.jpg格式命名,在識別image_id時會出錯,所以要更改命名方式,將自己需要測驗的資料集批量重命名為COCO_VOC2007_000001.jpg這種格式,

創建一個convert.py將上面的VOC2007/ImageSets/Main/test.txt轉換成保存影像路徑的txt,注意根據自己的實際情況修改路徑,運行該腳本會生成ntest.txt檔案,

ftest = open('VOC2007/test.txt', 'r')
lines = ftest.readlines()
ftest.close()
ftest = open('VOC2007/test.txt', 'w')
for line in lines:
    line_new="VOC2007/JPEGImages/COCO_VOC2007_"+line
    ftest.write(line_new)

ff = open('VOC2007/ntest.txt','w')  #打開一個檔案,可寫模式
with open('VOC2007/test.txt','r') as f:  #打開一個檔案只讀模式
    line = f.readlines()
    for line_list in line:
        line_new =line_list.replace('\n','')
        line_new=line_new+r'.jpg'+'\n'
        ff.write(line_new)
        

ntest.txt檔案格式如下:
在這里插入圖片描述

修改coco.data中的路徑

打開darknet/cfg/coco.data,這里只需要用到valid,所以把valid的值改為保存影像路徑的txt的路徑,把classes改為你的資料集包含的物體類別數,

在這里插入圖片描述

打開data/coco.names檔案,將內容修改為自己模型的物體類別名稱,

修改yolov3.cfg檔案

打開darknet/cfg/yolov3.cfg檔案,搜索yolo,一共搜索到三處,每一處都做如下修改:
1.filters =3*(5+classes) (注意這里要寫計算出來的具體數字,例如classes是2,這里就改為21,否則后面會報錯)
2.classes=2(你訓練的模型的類別個數)
在這里插入圖片描述

進行檢測并生成json檔案

在終端運行(作者嘗試了在Win10系統運行,但記憶體始終報錯,于是轉到Linux系統):

./darknet detector valid cfg/coco.data cfg/yolov3.cfg backup/yolov3.weights

在這里插入圖片描述
運行完成后結果保存在results/coco_results.json檔案中,即結果集的json檔案,將該檔案復制到自己的專案檔案夾下,并記住該路徑,

測驗mAP步驟

得到兩個json檔案后,在自己的專案檔案夾下創建一個coco_compute_mAP.py檔案,根據自己存放的路徑對cocoGt_file和cocoDt_file進行修改,

#-*- coding:utf-8 -*-
import matplotlib.pyplot as plt
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
import numpy as np
import skimage.io as io
import pylab,json
pylab.rcParams['figure.figsize'] = (10.0, 8.0)
def get_img_id(file_name):
    ls = []
    myset = []
    annos = json.load(open(file_name, 'r'))
    for anno in annos:
      ls.append(anno['image_id'])
    myset = {}.fromkeys(ls).keys()
    return myset
if __name__ == '__main__':
    annType = ['segm', 'bbox', 'keypoints']#set iouType to 'segm', 'bbox' or 'keypoints'
    annType = annType[1] # specify type here
    cocoGt_file = 'annotation_json/voc2007_test.json' #需要根據自己的實際情況配置該路徑
    cocoGt = COCO(cocoGt_file)#取得標注集中coco json物件
    cocoDt_file = 'results/coco_results.json' #需要根據自己的實際情況配置該路徑
    imgIds = get_img_id(cocoDt_file)
    print(len(imgIds))
    cocoDt = cocoGt.loadRes(cocoDt_file)#取得結果集中image json物件
    imgIds = sorted(imgIds)#按順序排列coco標注集image_id
    imgIds = imgIds[0:5000]#標注集中的image資料
    cocoEval = COCOeval(cocoGt, cocoDt, annType)
    cocoEval.params.imgIds = imgIds#引數設定
    cocoEval.evaluate()#評價
    cocoEval.accumulate()#積累
    cocoEval.summarize()#總結

運行該腳本得到mAP的計算結果:
在這里插入圖片描述

錯誤問題解決

1.【pycocotools報TypeError: object of type class numpy.float64 cannot be safely interpreted as an int】

https://blog.csdn.net/sinat_29957455/article/details/106481297

將507行和508行都做上述修改

2.【檢測時報"cannot load image "./JPEGImages/000001.jpg
STB Reason: unknown image type images】

https://blog.csdn.net/weixin_30840573/article/details/94896855

圖片打開出現錯誤,視情況將后綴改為.png / 將webp格式轉換為.jpg格式

3.【檢測時報:STB Reason: can‘t fopen】
https://blog.csdn.net/pursuit_zhangyu/article/details/107604731

【如有其他錯誤歡迎留言討論,但是我也不一定會…?▽?】

參考博文及Github專案(十分感謝!)

1.COCOAPI評估Yolov3,計算mAP
https://blog.csdn.net/SongJ12345666/article/details/108452730

2.計算YOLOv3在COCO資料集上的mAP值
https://blog.csdn.net/huangxiang360729/article/details/105853200/

3.利用COCOAPI計算Yolov3訓練出的模型的MAP值,復現ap
https://blog.csdn.net/xidaoliang/article/details/88397280

4.【YOLO】如何將Keras訓練的模型用于OpenCV中(.h5檔案轉換成.weights檔案)
https://blog.csdn.net/qinchang1/article/details/105776132

5.Github:Keras-YOLOv4
https://github.com/miemie2013/Keras-YOLOv4

6.目標檢測模型的評估指標mAP詳解(附代碼)
https://zhuanlan.zhihu.com/p/37910324

作者第一次發布文章,在這個方面還屬于小白,以上內容難免會有錯誤,歡迎在評論區指正(??????)??

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/248101.html

標籤:python

上一篇:Python 環境安裝

下一篇:tornado框架5分鐘快速上手、tornado基礎

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more