日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ValueError: With n_samples=0, test_size=0.2 and train_size=None, the resulting train set will be emp

發(fā)布時間:2023/12/8 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ValueError: With n_samples=0, test_size=0.2 and train_size=None, the resulting train set will be emp 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

今天寫代碼labelmetovoc,即將labelme標(biāo)注的轉(zhuǎn)化為voc標(biāo)準(zhǔn)格式參考的這篇文章時遇到了如下問題:
ValueError: With n_samples=0, test_size=0.2 and train_size=None, the resulting train set will be empty. Adjust any of the aforementioned parameters.
在網(wǎng)上查了一下,大部分博客都認(rèn)為是scikit-learn版本較高時出現(xiàn)的問題,需要換到0.20.0以下版本,但是換的話numpy,scipy等庫均要重新下載兼容的版本,所以建議不要輕易嘗試,我請教了一下師兄,他說是因?yàn)閰?shù)上面參數(shù)設(shè)置出現(xiàn)了問題,train或test的數(shù)量必須為整數(shù),改好的代碼如下所示:

# -*- coding: utf-8 -*- """ Created on Fri Apr 10 13:39:17 2020@author: nihao """ import os import numpy as np import codecs import json import glob import cv2 import shutil from sklearn.model_selection import train_test_split# 1.標(biāo)簽路徑 labelme_path = "D:\\PinInspection\\jpgretanglelabel" # 原始labelme標(biāo)注數(shù)據(jù)路徑 saved_path = "D:\\PinInspection\\improveto300\\VOC2007" # 保存路徑# 2.創(chuàng)建要求文件夾 dst_annotation_dir = os.path.join(saved_path, 'Annotations') if not os.path.exists(dst_annotation_dir):os.makedirs(dst_annotation_dir) dst_image_dir = os.path.join(saved_path, "JPEGImages") if not os.path.exists(dst_image_dir):os.makedirs(dst_image_dir) dst_main_dir = os.path.join(saved_path, "ImageSets", "Main") if not os.path.exists(dst_main_dir):os.makedirs(dst_main_dir)# 3.獲取待處理文件 org_json_files = sorted(glob.glob(os.path.join(labelme_path, '*.json'))) org_json_file_names = [i.split("\\")[-1].split(".json")[0] for i in org_json_files] org_img_files = sorted(glob.glob(os.path.join(labelme_path, '*.jpg'))) org_img_file_names = [i.split("\\")[-1].split(".jpg")[0] for i in org_img_files]# 4.labelme file to voc dataset for i, json_file_ in enumerate(org_json_files):json_file = json.load(open(json_file_, "r", encoding="utf-8"))image_path = os.path.join(labelme_path, org_json_file_names[i]+'.jpg')img = cv2.imread(image_path)height, width, channels = img.shapedst_image_path = os.path.join(dst_image_dir, "{:06d}.jpg".format(i))cv2.imwrite(dst_image_path, img)dst_annotation_path = os.path.join(dst_annotation_dir, '{:06d}.xml'.format(i))with codecs.open(dst_annotation_path, "w", "utf-8") as xml:xml.write('<annotation>\n')xml.write('\t<folder>' + 'Pin_detection' + '</folder>\n')xml.write('\t<filename>' + "{:06d}.jpg".format(i) + '</filename>\n')# xml.write('\t<source>\n')# xml.write('\t\t<database>The UAV autolanding</database>\n')# xml.write('\t\t<annotation>UAV AutoLanding</annotation>\n')# xml.write('\t\t<image>flickr</image>\n')# xml.write('\t\t<flickrid>NULL</flickrid>\n')# xml.write('\t</source>\n')# xml.write('\t<owner>\n')# xml.write('\t\t<flickrid>NULL</flickrid>\n')# xml.write('\t\t<name>ChaojieZhu</name>\n')# xml.write('\t</owner>\n')xml.write('\t<size>\n')xml.write('\t\t<width>' + str(width) + '</width>\n')xml.write('\t\t<height>' + str(height) + '</height>\n')xml.write('\t\t<depth>' + str(channels) + '</depth>\n')xml.write('\t</size>\n')xml.write('\t\t<segmented>0</segmented>\n')for multi in json_file["shapes"]:points = np.array(multi["points"])xmin = min(points[:, 0])xmax = max(points[:, 0])ymin = min(points[:, 1])ymax = max(points[:, 1])label = multi["label"]if xmax <= xmin:passelif ymax <= ymin:passelse:xml.write('\t<object>\n')xml.write('\t\t<name>' + label + '</name>\n')xml.write('\t\t<pose>Unspecified</pose>\n')xml.write('\t\t<truncated>1</truncated>\n')xml.write('\t\t<difficult>0</difficult>\n')xml.write('\t\t<bndbox>\n')xml.write('\t\t\t<xmin>' + str(xmin) + '</xmin>\n')xml.write('\t\t\t<ymin>' + str(ymin) + '</ymin>\n')xml.write('\t\t\t<xmax>' + str(xmax) + '</xmax>\n')xml.write('\t\t\t<ymax>' + str(ymax) + '</ymax>\n')xml.write('\t\t</bndbox>\n')xml.write('\t</object>\n')print(json_file_, xmin, ymin, xmax, ymax, label)xml.write('</annotation>')# 5.split files for txt train_file = os.path.join(dst_main_dir, 'train.txt') trainval_file = os.path.join(dst_main_dir, 'trainval.txt') val_file = os.path.join(dst_main_dir, 'val.txt') test_file = os.path.join(dst_main_dir, 'test.txt')ftrain = open(train_file, 'w') ftrainval = open(trainval_file, 'w') fval = open(val_file, 'w') ftest = open(test_file, 'w')total_annotation_files = glob.glob(os.path.join(dst_annotation_dir, "*.xml")) total_annotation_names = [i.split("\\")[-1].split(".xml")[0] for i in total_annotation_files]# test_filepath = "" for file in total_annotation_names:ftrainval.writelines(file + '\n') # test # for file in os.listdir(test_filepath): # ftest.write(file.split(".jpg")[0] + "\n") # split train_files, val_files = train_test_split(total_annotation_names, test_size=0.2) # train for file in train_files:ftrain.write(file + '\n') # val for file in val_files:fval.write(file + '\n')ftrainval.close() ftrain.close() fval.close() # ftest.close()

只需要
1.將自己的輸入地址寫入labelme_path = “D:\PinInspection\jpgretanglelabel” # 原始labelme標(biāo)注數(shù)據(jù)路徑
里,jpgretanglelabel文件夾里需要同時存放原圖與標(biāo)注的json文件,文件命名對應(yīng)(一張標(biāo)注的json文件對應(yīng)一張?jiān)瓐D)。
2.將自己及的輸出地址寫入saved_path = “D:\PinInspection\improveto300\VOC2007” # 保存路徑
這樣問題就解決了~

總結(jié)

以上是生活随笔為你收集整理的ValueError: With n_samples=0, test_size=0.2 and train_size=None, the resulting train set will be emp的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。