Traceback (most recent call last):
File "/opt/anaconda3/envs/sam3/lib/python3.9/site-packages/torch/utils/data/_utils/worker.py", line 309, in _worker_loop
data = fetcher.fetch(index) # type: ignore[possibly-undefined]
File "/opt/anaconda3/envs/sam3/lib/python3.9/site-packages/torch/utils/data/_utils/fetch.py", line 52, in fetch
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/opt/anaconda3/envs/sam3/lib/python3.9/site-packages/torch/utils/data/_utils/fetch.py", line 52, in
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/Users/samerkharboush/Downloads/SAR-MPS-OptunaNasFbnYoloTransformers/xview3/dataset/random_crop_dataset.py", line 191, in getitem
return self.build_sample(
File "/Users/samerkharboush/Downloads/SAR-MPS-OptunaNasFbnYoloTransformers/xview3/centernet/multilabel_dataset.py", line 361, in build_sample
return self.build_centernet_sample(
File "/Users/samerkharboush/Downloads/SAR-MPS-OptunaNasFbnYoloTransformers/xview3/centernet/multilabel_dataset.py", line 143, in build_centernet_sample
raise ValueError("Confidences array contains non-numeric or inconsistent data.") from e
ValueError: Confidences array contains non-numeric or inconsistent data.
DEBUG:rasterio._io:all_valid: False
[DEBUG] Channel 0 shape: (512, 512)
[DEBUG] Channel 1 shape: (512, 512)
DEBUG:rasterio._io:mask_flags: ([<MaskFlags.nodata: 8>],)
DEBUG:rasterio._io:Jump straight to _read()
DEBUG:rasterio._io:Window: Window(col_off=19067, row_off=19797, width=512, height=512)
DEBUG:rasterio.env:Exiting env context: <rasterio.env.Env object at 0x34572ed30>
DEBUG:rasterio._io:IO window xoff=19067.0 yoff=19797.0 width=512.0 height=512.0
DEBUG:rasterio.env:Cleared existing <rasterio._env.GDALEnv object at 0x34572eca0> options
DEBUG:rasterio._base:Nodata success: 1, Nodata value: -32768.000000
DEBUG:rasterio._env:Stopped GDALEnv <rasterio._env.GDALEnv object at 0x34572eca0>.
DEBUG:rasterio.env:Exiting outermost env
DEBUG:rasterio.env:Exited env context: <rasterio.env.Env object at 0x34572ed30>
DEBUG:rasterio._base:Dataset is started.
DEBUG:rasterio.env:Exiting env context: <rasterio.env.Env object at 0x33ddc0a00>
DEBUG:rasterio.env:Cleared existing <rasterio._env.GDALEnv object at 0x33ddc02b0> options
DEBUG:rasterio._env:Stopped GDALEnv <rasterio._env.GDALEnv object at 0x33ddc02b0>.
DEBUG:rasterio.env:Exiting outermost env
DEBUG:rasterio.env:Exited env context: <rasterio.env.Env object at 0x33ddc0a00>
(168.0, 45.0, 0.0) [<class 'float'>, <class 'float'>, <class 'float'>]
(229.0, 264.0, 0.0) [<class 'float'>, <class 'float'>, <class 'float'>]
(238.0, 285.0, 0.0) [<class 'float'>, <class 'float'>, <class 'float'>]
(236.0, 271.0, 0.0) [<class 'float'>, <class 'float'>, <class 'float'>]
DEBUG:rasterio.env:Entering env context: <rasterio.env.Env object at 0x33ddc07f0>
(244.0, 276.0, 0.0) [<class 'float'>, <class 'float'>, <class 'float'>]
(252.0, 281.0, 0.0) [<class 'float'>, <class 'float'>, <class 'float'>]
(175.0, 49.0, 1.8786169290542603) [<class 'float'>, <class 'float'>, <class 'float'>]
DEBUG:rasterio.env:Starting outermost env
(183.0, 53.0, 1.8199727535247803) [<class 'float'>, <class 'float'>, <class 'float'>]
(190.0, 54.0, 2.3164262771606445) [<class 'float'>, <class 'float'>, <class 'float'>]
(182.0, 36.0, 1.0649999380111694) [<class 'float'>, <class 'float'>, <class 'float'>]
DEBUG:rasterio.env:No GDAL environment exists
DEBUG:rasterio.env:New GDAL environment <rasterio._env.GDALEnv object at 0x33ddc02b0> created
[DEBUG] Channel 0 shape: (512, 512)
[DEBUG] Channel 1 shape: (512, 512)
DEBUG:rasterio._env:GDAL_DATA found in environment.
DEBUG:rasterio._env:PROJ_DATA found in environment.
DEBUG:rasterio._env:Started GDALEnv: self=<rasterio._env.GDALEnv object at 0x33ddc02b0>.
DEBUG:rasterio.env:Entered env context: <rasterio.env.Env object at 0x33ddc07f0>
DEBUG:rasterio._io:Output nodata value read from file: -32768.0
(45.0, 333.0, 0.0) [<class 'float'>, <class 'float'>, <class 'float'>]
(267.0, 241.0, 0.0) [<class 'float'>, <class 'float'>, <class 'float'>]
DEBUG:rasterio._io:Output nodata values: [-32768.0]
DEBUG:rasterio._io:all_valid: False
DEBUG:rasterio._io:mask_flags: ([<MaskFlags.nodata: 8>],)
DEBUG:rasterio._io:Jump straight to _read()
DEBUG:rasterio._io:Window: Window(col_off=22205, row_off=10815, width=512, height=512)
DEBUG:rasterio._io:IO window xoff=22205.0 yoff=10815.0 width=512.0 height=512.0
[DEBUG] Build Sample - Confidences Type: <class 'numpy.ndarray'>
[DEBUG] Confidences Dtype: object
[DEBUG] Confidences Shape: (2,)
[DEBUG] Build Sample - Confidences Type: <class 'numpy.ndarray'>
[DEBUG] Confidences Dtype: object
[DEBUG] Confidences Shape: (10,)
(sam3) samerkharboush@Samers-MBP SAR-MPS-OptunaNasFbnYoloTransformers %
The error you're encountering arises from attempting to convert a string value 'M' to a float within your data processing pipeline. Let's break down the error and explore potential solutions to resolve it.
Understanding the Error
-
Traceback Summary:
-
Specific Code Causing the Issue:
confidences = np.array([list(conf) for conf in confidences], dtype=np.float32)
- Here,
confidences is expected to be an iterable of iterables containing numeric values. However, it's encountering a string 'M' instead.
Potential Causes
-
Data Integrity Issues:
- The
confidences data might contain non-numeric placeholders such as 'M' to denote missing or special cases.
- If
'M' is intended to represent missing data, it should be handled appropriately before conversion.
-
Data Loading or Parsing Errors:
- The source from which
confidences are being loaded might inadvertently include 'M' due to formatting issues or incorrect parsing.
-
Incorrect Data Preprocessing:
- Prior preprocessing steps might not be adequately sanitizing or validating the data, allowing non-numeric values to slip through.
Steps to Resolve
-
Inspect the confidences Data:
- Before the conversion, print out or log the contents of
confidences to identify where and why 'M' is present.
print("Confidences before conversion:", confidences)
-
Handle Non-Numeric Values:
- Option 1: Filter Out Non-Numeric Entries
confidences = [conf for conf in confidences if isinstance(conf, (int, float))]
confidences = np.array([list(conf) for conf in confidences], dtype=np.float32)
- Option 2: Replace Non-Numeric Entries with NaN
confidences = [[float(c) if c != 'M' else np.nan for c in conf] for conf in confidences]
confidences = np.array(confidences, dtype=np.float32)
- Note: Ensure downstream processes can handle
NaN values appropriately.
- Option 3: Use Pandas for Robust Conversion
import pandas as pd
confidences_df = pd.DataFrame(confidences)
confidences_df = confidences_df.apply(pd.to_numeric, errors='coerce')
confidences = confidences_df.to_numpy(dtype=np.float32)
- This approach converts non-convertible values to
NaN, which can then be handled as needed.
-
Validate Data Source:
- Ensure that the source of
confidences is correctly formatted and does not include unexpected string values.
- If
'M' has a specific meaning (e.g., "Missing"), document and handle it accordingly in your data processing pipeline.
-
Add Error Handling:
- Implement try-except blocks to catch and handle such conversion errors gracefully.
try:
confidences = np.array([list(conf) for conf in confidences], dtype=np.float32)
except ValueError as e:
print("Error converting confidences:", e)
# Handle the error (e.g., skip this sample, replace 'M' with a default value, etc.)
-
Unit Testing:
- Write tests to ensure that
confidences only contains numeric data before conversion.
- This can help catch data issues early in the development process.
-
Review Data Generation Pipeline:
- If
confidences are generated or transformed by earlier steps in your pipeline, ensure that those steps maintain data integrity and enforce data types.
Example Modification in build_centernet_sample
Here's how you might modify the build_centernet_sample function to handle non-numeric values gracefully:
import numpy as np
import pandas as pd
def build_centernet_sample(...):
# Existing code to generate confidences
confidences = ... # your existing code
# Log the problematic confidences for debugging
print("Confidences before conversion:", confidences)
# Option 1: Replace 'M' with np.nan
try:
confidences = [[float(c) if c != 'M' else np.nan for c in conf] for conf in confidences]
confidences = np.array(confidences, dtype=np.float32)
except ValueError as e:
print("Error converting confidences:", e)
raise ValueError("Confidences array contains non-numeric or inconsistent data.") from e
# Continue with the rest of your function
...
Final Recommendations
-
Data Validation: Always validate and sanitize your data at the earliest point in your pipeline to prevent such issues downstream.
-
Consistent Data Formats: Ensure that all parts of your data processing pipeline agree on the expected data formats and types.
-
Detailed Logging: Incorporate detailed logging around data transformations to make debugging easier when issues arise.
By implementing these strategies, you should be able to identify the root cause of the 'M' values in your confidences data and handle them appropriately, thereby resolving the ValueError you're encountering.