質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

4093閲覧

Python xml.etree.ElementTree.ParseEroorについて

nigo1973

総合スコア14

Keras

Kerasは、TheanoやTensorFlow/CNTK対応のラッパーライブラリです。DeepLearningの数学的部分を短いコードでネットワークとして表現することが可能。DeepLearningの最新手法を迅速に試すことができます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2018/05/01 13:54

編集2018/05/01 13:57

目的
自前の学習データを用いた物体検出を行うため、以下のサイトを参考にしてKerasでSSDを行っています。
http://qiita.com/slowsingle/items/64cc927bb29a49a7af14
まずは,コードの意味を理解するために,以下のファイルを用いて用意されたアノテーションデータ(VOC2007)から学習用のpklファイルの作成を試みています.
https://github.com/rykov8/ssd_keras/blob/master/PASCAL_VOC/get_data_from_XML.py

問題
自前のデータではなく,VOC2007に入っているアノテーションxmlファイルを用いて,以下のコードを走らせたのですが,etree.ElemetTreeの読み込みエラーと思われるエラーが生じています. コードはデフォルトのままになります.(フォルダの場所は変更しています.)
下記のエラーの対処方法について教えてください.よろしくお願いします.
環境
conda 4.5.1
Python 3.5.2

エラー
Traceback (most recent call last):
File "get_data_from_XML.py", line 89, in <module>
data = XML_preprocessor('VOCdevkit/VOC2007/Annotations/').data
File "get_data_from_XML.py", line 11, in init
self._preprocess_XML()
File "get_data_from_XML.py", line 16, in _preprocess_XML
tree = ElementTree.parse(self.path_prefix + filename)
File "/Users/anaconda/envs/py35/lib/python3.5/xml/etree/ElementTree.py", line 1184, in parse
tree.parse(source, parser)
File "/Users/anaconda/envs/py35/lib/python3.5/xml/etree/ElementTree.py", line 596, in parse
self._root = parser._parse_whole(source)
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 0

コード
import numpy as np
import os
from xml.etree import ElementTree

class XML_preprocessor(object):

def __init__(self, data_path): self.path_prefix = data_path self.num_classes = 20 self.data = dict() self._preprocess_XML() def _preprocess_XML(self): filenames = os.listdir(self.path_prefix) for filename in filenames: tree = ElementTree.parse(self.path_prefix + filename) root = tree.getroot() bounding_boxes = [] one_hot_classes = [] size_tree = root.find('size') width = float(size_tree.find('width').text) height = float(size_tree.find('height').text) for object_tree in root.findall('object'): for bounding_box in object_tree.iter('bndbox'): xmin = float(bounding_box.find('xmin').text)/width ymin = float(bounding_box.find('ymin').text)/height xmax = float(bounding_box.find('xmax').text)/width ymax = float(bounding_box.find('ymax').text)/height bounding_box = [xmin,ymin,xmax,ymax] bounding_boxes.append(bounding_box) class_name = object_tree.find('name').text one_hot_class = self._to_one_hot(class_name) one_hot_classes.append(one_hot_class) image_name = root.find('filename').text bounding_boxes = np.asarray(bounding_boxes) one_hot_classes = np.asarray(one_hot_classes) image_data = np.hstack((bounding_boxes, one_hot_classes)) self.data[image_name] = image_data def _to_one_hot(self,name): one_hot_vector = [0] * self.num_classes if name == 'aeroplane': one_hot_vector[0] = 1 elif name == 'bicycle': one_hot_vector[1] = 1 elif name == 'bird': one_hot_vector[2] = 1 elif name == 'boat': one_hot_vector[3] = 1 elif name == 'bottle': one_hot_vector[4] = 1 elif name == 'bus': one_hot_vector[5] = 1 elif name == 'car': one_hot_vector[6] = 1 elif name == 'cat': one_hot_vector[7] = 1 elif name == 'chair': one_hot_vector[8] = 1 elif name == 'cow': one_hot_vector[9] = 1 elif name == 'diningtable': one_hot_vector[10] = 1 elif name == 'dog': one_hot_vector[11] = 1 elif name == 'horse': one_hot_vector[12] = 1 elif name == 'motorbike': one_hot_vector[13] = 1 elif name == 'person': one_hot_vector[14] = 1 elif name == 'pottedplant': one_hot_vector[15] = 1 elif name == 'sheep': one_hot_vector[16] = 1 elif name == 'sofa': one_hot_vector[17] = 1 elif name == 'train': one_hot_vector[18] = 1 elif name == 'tvmonitor': one_hot_vector[19] = 1 else: print('unknown label: %s' %name) return one_hot_vector

import pickle
data = XML_preprocessor('VOCdevkit/VOC2007/Annotations/').data
pickle.dump(data,open('VOC2007.pkl','wb'))

xmlファイル
<annotation>
<folder>VOC2007</folder>
<filename>000005.jpg</filename>
<source>
<database>The VOC2007 Database</database>
<annotation>PASCAL VOC2007</annotation>
<image>flickr</image>
<flickrid>325991873</flickrid>
</source>
<owner>
<flickrid>archintent louisville</flickrid>
<name>?</name>
</owner>
<size>
<width>500</width>
<height>375</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>chair</name>
<pose>Rear</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>263</xmin>
<ymin>211</ymin>
<xmax>324</xmax>
<ymax>339</ymax>
</bndbox>
</object>
<object>
<name>chair</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>165</xmin>
<ymin>264</ymin>
<xmax>253</xmax>
<ymax>372</ymax>
</bndbox>
</object>
<object>
<name>chair</name>
<pose>Unspecified</pose>
<truncated>1</truncated>
<difficult>1</difficult>
<bndbox>
<xmin>5</xmin>
<ymin>244</ymin>
<xmax>67</xmax>
<ymax>374</ymax>
</bndbox>
</object>
<object>
<name>chair</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>241</xmin>
<ymin>194</ymin>
<xmax>295</xmax>
<ymax>299</ymax>
</bndbox>
</object>
<object>
<name>chair</name>
<pose>Unspecified</pose>
<truncated>1</truncated>
<difficult>1</difficult>
<bndbox>
<xmin>277</xmin>
<ymin>186</ymin>
<xmax>312</xmax>
<ymax>220</ymax>
</bndbox>
</object>
</annotation>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

VOCdevkit/VOC2007/Annotations/以下にあるのは掲載されているxmlのファイルだけですか? そうでなければループの中にprint(filename)などを入れて本当のエラー原因のファイルを特定してください。

本当にそのxmlが原因なら、1行目の0文字目で落ちていますから、疑うべきは文字コードなどです。

投稿2018/05/01 14:14

hayataka2049

総合スコア30933

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

nigo1973

2018/05/02 02:11

macを使用しており,該当するフォルダに”.DS_Store” ファイルが生成されていることが原因でした.xmlしかないと考えていましたが,この隠しファイルを削除し実行するとエラー無く対応できました.ヒントをいただきありがとうございました.
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問