python对PASCAL VOC标注数据进行统计

用于统计训练数据中的类别,以及所有目标的个数:

# coding:utf-8
import xml.etree.cElementTree as ET
import os
from collections import Counter
import shutil

# Counter({'towCounter({'tower': 3074, 'windpower': 2014, 'thermalpower': 689, 'hydropower': 261, 'transformer': 225})
# total_num: 6263

def count(pathdir,despath):
	category = []
	path = pathdir + '/XML/'
	for index,xml in enumerate(os.listdir(path)):
		# print(str(index) + ' xml: '+ xml)
		root = ET.parse(os.path.join(path, xml))
		objects = root.findall('object')
		
		# ==================select images which has a special object=============
		for obj in objects:
			obj_label = obj.find('name').text
			if obj_label == 'transformer':
				print(xml)
				imgfile = pathdir + 'JPEG/' + xml.replace('xml', 'jpg')
				img_despath = despath + xml.replace('xml', 'jpg')
				# if not os.path.exists(img_despath):
				shutil.copyfile(imgfile, img_despath)

		# ==================select images which has a special object=============

		category += [ob.find('name').text for ob in objects]
	print(Counter(category))
	total_num = sum([value for key, value in Counter(category).items()])
	print('total_num:',total_num)

if __name__ == '__main__':
	# pathdirs = list(set(os.listdir('./')) ^ set(['tools','count.py']))
	# print(pathdirs)
	# for pathdir in pathdirs:
	pathdir = '/summer/Desktop/power_traindata/'
	despath = '/transformer/'
	count(pathdir,despath)

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要将Pascal VOC标注格式的数据转换成TensorFlow支持的格式,可以使用一些开源工具来完成这个转换,例如`tfrecord-generator`。接着,可以使用TensorFlow的API来加载和训练数据集。 以下是一个简单的示例代码,用于加载和训练Pascal VOC数据集: ```python import tensorflow as tf # 加载tfrecord数据集 def load_dataset(tfrecord_path): dataset = tf.data.TFRecordDataset(tfrecord_path) # 解析tfrecord文件数据 feature_description = { 'image': tf.io.FixedLenFeature([], tf.string), 'label': tf.io.FixedLenFeature([], tf.string), 'width': tf.io.FixedLenFeature([], tf.int64), 'height': tf.io.FixedLenFeature([], tf.int64) } def _parse_function(example_proto): parsed_features = tf.io.parse_single_example(example_proto, feature_description) image = tf.io.decode_jpeg(parsed_features['image'], channels=3) label = tf.io.decode_raw(parsed_features['label'], tf.uint8) width = parsed_features['width'] height = parsed_features['height'] return (image, label), (width, height) return dataset.map(_parse_function) # 训练模型 def train_model(dataset): model = tf.keras.Sequential([ tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(None, None, 3)), tf.keras.layers.MaxPooling2D((2, 2)), tf.keras.layers.Flatten(), tf.keras.layers.Dense(10, activation='softmax') ]) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(dataset, epochs=10) # 加载数据集 train_dataset = load_dataset('/path/to/train.tfrecord') test_dataset = load_dataset('/path/to/test.tfrecord') # 训练模型 train_model(train_dataset) ``` 需要注意的是,以上示例代码只是一个简单的例子,需要根据具体的任务和数据集进行修改和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值