深度学习制作数据集的部分代码实现(解压zip、生成json文件)
1、当数据集是压缩包时,需要解压成图片文件import zipfiledef unzip_data(src_path,target_path):'''解压原始数据集,将src_path路径下的zip包解压至data/dataset目录下'''if(not os.path.isdir(target_path)):z = zipfile.ZipFile(src_path, 'r')z.extracta
·
1、当数据集是压缩包时,需要解压成图片文件
import zipfile
def unzip_data(src_path,target_path):
'''
解压原始数据集,将src_path路径下的zip包解压至data/dataset目录下
'''
if(not os.path.isdir(target_path)):
z = zipfile.ZipFile(src_path, 'r')
z.extractall(path=target_path)
z.close()
else:
print("文件已解压")
2、生成json文件
# 说明的json文件信息
readjson = {}
readjson['all_class_name'] = data_list_path #文件父目录
readjson['all_class_images'] = all_class_images
readjson['class_detail'] = class_detail
jsons = json.dumps(readjson, sort_keys=True, indent=4, separators=(',', ': '))
with open(train_parameters['readme_path'],'w') as f:
f.write(jsons)
print ('生成数据列表完成!')
3、自定义data_reader
def data_reader(file_list):
'''
自定义data_reader
'''
def reader():
with open(file_list, 'r') as f:
lines = [line.strip() for line in f]
for line in lines:
img_path, lab = line.strip().split('\t')
img = cv2.imread(img_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = np.array(img).astype('float32')
img = img/255.0
yield img, int(lab)
return reader
更多推荐
所有评论(0)