4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PyTorch でのトレーニング時に UnidentifiedImageError 出たときの対処法の一例

Last updated at Posted at 2023-06-02

はじめに

PyTorch で機械学習モデルのトレーニングを実行した際、 UnidentifiedImageError というエラーが発生しました

このエラーはトレーニングに用いる画像ファイルが破損しているときなどに発生するもののようです

ただし、今回私が遭遇したケースの場合、トレーニング用の画像ファイルは全て問題なくプレビューで開くことができ、破損しているわけではなさそうでした

エラーメッセージには ._sample.jpg でエラーが発生したように書いていますが、 sample.jpg はトレーニングデータにあるものの、 ._sample.jpg なんていうファイルは見当たりません

非表示ファイルを表示してみても存在しません

というわけで、このケースでの対処法をメモとして残しておきます

原因

エラーの原因となっていた(と思われる)ファイル sample.jpgls -l で見てみると、以下のような表示になっていました

$ ls -l ./sample.jpg                                
-rw-r--r--@ 1 oec  staff  129616  5  9 15:19 ./sample.jpg

パーミッション表示(-rw-r--r--)の後に @ が付いています

この @ は macOS の拡張属性 (EA = Extented Attribute) が付いていることを表します

そして、この拡張属性を持つファイルを圧縮すると、拡張属性が別ファイルとして追加されてしまうようです

今回、トレーニングデータをローカルで圧縮し、それをサーバに送って展開していました

その結果、 ._sample.jpg という画像ではないのに拡張子 .jpg という不正なファイルができてしまい、それがエラーの原因になっていたのでした

参考記事:

対処法

ls -l@ とすると、拡張属性の内容が確認できます

$ls -l@ ./sample.jpg
-rw-r--r--@ 1 oec  staff  129616  5  9 15:19 ./sample.jpg
        com.apple.lastuseddate#PS            2 
        com.apple.quarantine         2

前述の参考記事にある通り、 xattr -c で拡張属性を全て削除できます

xattr -c ./sample.jpg

改めて ls -l@ とすると、拡張属性が消えています

$ ls -l@ ./sample.jpg  
-rw-r--r--  1 oec  staff  129616  5  9 15:19 ./sample.jpg

この状態で圧縮してサーバに送り、トレーニングを実行することで、エラーが解消しました

まとめ

とりあえず ls -l をしてみて @ という謎の記号を見つけたので対処できましたが、まだまだ知らないことが多いです

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?