YutaroOgawa / pytorch_advanced

書籍「つくりながら学ぶ! PyTorchによる発展ディープラーニング」の実装コードを配置したリポジトリです
MIT License
837 stars 336 forks source link

2-2-3のDataTransformの動作確認時のエラーについて #219

Open 1sa014kawa opened 10 months ago

1sa014kawa commented 10 months ago

2-2-3においてDataTransformの動作確認時に下に示すようなエラーが発生するようになりました。 utilsにあるdata-augumentation.pyにおいてnumpyのrandom.choiceに型の異なる値の入ったタプルを代入しているのが原因のようです。 このあたりはpythonが適切に判断して動作してくれていたのだと思うのですが、仕様が少し変わったのかもしれません(私には詳しいことはわかりませんでした)。 ひとまず、data-augumentation.pyの最初にfrom random import choice as random_choiceを書き加え、RandomSampleCrop.__call__(self, image, boxes, labels)内のmode=random.choice(self.sample_option)mode=random_choice(self.sample_option)と変えることで対処しています。

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[8], line 23
     21 # 5. train画像の表示
     22 phase = "train"
---> 23 img_transformed, boxes, labels = transform(
     24     img, phase, anno_list[:, :4], anno_list[:, 4])
     25 plt.imshow(cv2.cvtColor(img_transformed, cv2.COLOR_BGR2RGB))
     26 plt.show()

Cell In[7], line 48, in DataTransform.__call__(self, img, phase, boxes, labels)
     41 def __call__(self, img, phase, boxes, labels):
     42     """
     43     Parameters
     44     ----------
     45     phase : 'train' or 'val'
     46         前処理のモードを指定。
     47     """
---> 48     return self.data_transform[phase](img, boxes, labels)

File ~/python_local/pytorch_advanced-master/2_objectdetection/utils/data_augumentation.py:62, in Compose.__call__(self, img, boxes, labels)
     60 def __call__(self, img, boxes=None, labels=None):
     61     for t in self.transforms:
---> 62         img, boxes, labels = t(img, boxes, labels)
     63     return img, boxes, labels

File ~/python_local/pytorch_advanced-master/2_objectdetection/utils/data_augumentation.py:248, in RandomSampleCrop.__call__(self, image, boxes, labels)
    245 height, width, _ = image.shape
    246 while True:
    247     # randomly choose a mode
--> 248     mode = random.choice(self.sample_options)
    249     if mode is None:
    250         return image, boxes, labels

File mtrand.pyx:920, in numpy.random.mtrand.RandomState.choice()

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (6,) + inhomogeneous part.
Manami-GIWA commented 2 weeks ago

同様のエラーが出た者です. from numpy import randomより,random.choicenumpy.random.choiceですので,random.choiceの引数self.sample_optionsをNumPy配列に変換したらうまく行きました. (公式ドキュメントを読みましたが,このあたりの細かい理由については分かりませんでした)

つまり,data-augumentation.pyclass RandomSampleCrop(object): def __init__(self):以下を

        # 変更前
        self.sample_options = (
            # using entire original input image
            None,
            # sample a patch s.t. MIN jaccard w/ obj in .1,.3,.4,.7,.9
            (0.1, None),
            (0.3, None),
            (0.7, None),
            (0.9, None),
            # randomly sample a patch
            (None, None),
        )
        # 変更後
        self.sample_options = np.array([
            # using entire original input image
            None,
            # sample a patch s.t. MIN jaccard w/ obj in .1,.3,.4,.7,.9
            (0.1, None),
            (0.3, None),
            (0.7, None),
            (0.9, None),
            # randomly sample a patch
            (None, None)
        ], dtype=object)

とすることで解決しました.

@1sa014kawa 様が原因箇所を特定してくださっていたおかげで解決に至りました.ありがとうございます.

使用バージョン Python 3.8.10 Numpy 1.24.4