• 카테고리

    질문 & 답변
  • 세부 분야

    컴퓨터 비전

  • 해결 여부

    해결됨

테스트 데이터셋 predict의 'NoneType' object has no attribute 'shape' 오류

24.03.14 17:48 작성 조회수 118

0

안녕하세요.

테스트 데이터셋을 predict하는 부분에서 오류가 나서 질문드립니다.

 

test_path = test_df['path'].values

test_ds = Plant_Dataset(image_filenames=test_path, labels=None, image_size=IMAGE_SIZE, batch_size=BATCH_SIZE, augmentor=None, shuffle=False, pre_func=xcp_preprocess_input)

preds = xcp_model_01.predict(test_ds)

 

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[40], line 7
      3 test_path = test_df['path'].values
      4 test_ds = Plant_Dataset(image_filenames=test_path, labels=None, image_size=IMAGE_SIZE, batch_size=BATCH_SIZE, 
      5                         augmentor=None, shuffle=False, pre_func=xcp_preprocess_input)
----> 7 preds = xcp_model_01.predict(test_ds)

File /opt/conda/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py:123, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    120     filtered_tb = _process_traceback_frames(e.__traceback__)
    121     # To get the full stack trace, call:
    122     # `keras.config.disable_traceback_filtering()`
--> 123     raise e.with_traceback(filtered_tb) from None
    124 finally:
    125     del filtered_tb

File /opt/conda/lib/python3.10/site-packages/tree/__init__.py:435, in map_structure(func, *structures, **kwargs)
    432 for other in structures[1:]:
    433   assert_same_structure(structures[0], other, check_types=check_types)
    434 return unflatten_as(structures[0],
--> 435                     [func(*args) for args in zip(*map(flatten, structures))])

File /opt/conda/lib/python3.10/site-packages/tree/__init__.py:435, in <listcomp>(.0)
    432 for other in structures[1:]:
    433   assert_same_structure(structures[0], other, check_types=check_types)
    434 return unflatten_as(structures[0],
--> 435                     [func(*args) for args in zip(*map(flatten, structures))])

AttributeError: 'NoneType' object has no attribute 'shape'

 


test_image_batch, test_label_batch = next(iter(test_ds))

print(test_image_batch.shape, test_label_batch)
의 출력이
(32, 224, 224, 3) None

history.history['val_auc']
의 출력이
[0.9417113065719604, 0.9647012948989868, 0.9738287925720215, 0.9816075563430786, 0.9799161553382874, 0.9804703593254089, 0.9877450466156006, 0.9854006767272949, 0.9803326725959778, 0.9843235611915588]

학습도 완료됐고 Plant_Dataset도 제대로 작동하고 있습니다.

AttributeError:'NoneType' object has no attribute 'shape'으로 어느 부분이 문제가 되는지 질문드립니다.

 

답변 1

답변을 작성해보세요.

1

안녕하십니까,

tensorflow가 버전업이 되면서 predict() 내에 들어가는 Dataset Sequence Class 로직이 달라져야 되는군요.

class Plant_Dataset(Sequence) 에서

def getitem(self, index): 함수에서 맨마지막의

return image_batch, label_batch 와 같이 적용하면 labels 인자값이 None이 들어오는 Test 데이터 세트의 경우 (image_batch, None) 이 tuple 형태로 반환되었는데, 이게 아니라 image_batch 만 명확하게 반환이 되어야 되는 것로 Tensorflow가 버전업이 되었습니다.

그래서 위 부분은 아래와 같이 변경 부탁드립니다.

if self.labels is None:

return image_batch

else:

return image_batch, label_batch

 

좋은 정보 감사합니다. 곧 전체 공지 하겠습니다.