Upload model.py
Browse files
model.py
CHANGED
|
@@ -65,7 +65,7 @@ class TritonPythonModel:
|
|
| 65 |
print(f"Method 3 (tiff fallback) failed: {e3}")
|
| 66 |
# Method 4: Final fallback - try to interpret as raw numpy array
|
| 67 |
try:
|
| 68 |
-
# This assumes the
|
| 69 |
data_array = np.frombuffer(jp2_bytes, dtype=np.float32)
|
| 70 |
|
| 71 |
# Try to guess square dimensions
|
|
@@ -163,11 +163,39 @@ class TritonPythonModel:
|
|
| 163 |
responses.append(response)
|
| 164 |
continue
|
| 165 |
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
|
|
|
| 169 |
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
# Read red band data (use as reference for dimensions)
|
| 173 |
red_data, target_height, target_width, red_profile = self.safe_read_jp2_bytes(red_bytes)
|
|
@@ -192,6 +220,14 @@ class TritonPythonModel:
|
|
| 192 |
responses.append(response)
|
| 193 |
continue
|
| 194 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
# Stack bands in CHW format for prediction (channels, height, width)
|
| 196 |
prediction_array = np.stack([red_data, green_data, nir_data], axis=0)
|
| 197 |
print(f"Final prediction array shape: {prediction_array.shape}")
|
|
|
|
| 65 |
print(f"Method 3 (tiff fallback) failed: {e3}")
|
| 66 |
# Method 4: Final fallback - try to interpret as raw numpy array
|
| 67 |
try:
|
| 68 |
+
# This assumes the data might be raw numpy bytes as fallback
|
| 69 |
data_array = np.frombuffer(jp2_bytes, dtype=np.float32)
|
| 70 |
|
| 71 |
# Try to guess square dimensions
|
|
|
|
| 163 |
responses.append(response)
|
| 164 |
continue
|
| 165 |
|
| 166 |
+
# The input might be hex strings, decode them to bytes first
|
| 167 |
+
red_hex = jp2_bytes_list[0]
|
| 168 |
+
green_hex = jp2_bytes_list[1]
|
| 169 |
+
nir_hex = jp2_bytes_list[2]
|
| 170 |
|
| 171 |
+
# Convert hex strings to bytes
|
| 172 |
+
try:
|
| 173 |
+
if isinstance(red_hex, str):
|
| 174 |
+
red_bytes = bytes.fromhex(red_hex)
|
| 175 |
+
green_bytes = bytes.fromhex(green_hex)
|
| 176 |
+
nir_bytes = bytes.fromhex(nir_hex)
|
| 177 |
+
print(f"Decoded hex strings to bytes")
|
| 178 |
+
elif isinstance(red_hex, (bytes, np.bytes_)):
|
| 179 |
+
# Already bytes, use directly
|
| 180 |
+
red_bytes = bytes(red_hex)
|
| 181 |
+
green_bytes = bytes(green_hex)
|
| 182 |
+
nir_bytes = bytes(nir_hex)
|
| 183 |
+
print(f"Input already in bytes format")
|
| 184 |
+
else:
|
| 185 |
+
# Might be numpy string object
|
| 186 |
+
red_bytes = bytes.fromhex(str(red_hex))
|
| 187 |
+
green_bytes = bytes.fromhex(str(green_hex))
|
| 188 |
+
nir_bytes = bytes.fromhex(str(nir_hex))
|
| 189 |
+
print(f"Converted numpy strings to bytes")
|
| 190 |
+
except Exception as e:
|
| 191 |
+
error_msg = f"Failed to decode input data: {str(e)}"
|
| 192 |
+
print(f"Decode error: {error_msg}")
|
| 193 |
+
error = pb_utils.TritonError(error_msg)
|
| 194 |
+
response = pb_utils.InferenceResponse(output_tensors=[], error=error)
|
| 195 |
+
responses.append(response)
|
| 196 |
+
continue
|
| 197 |
+
|
| 198 |
+
print(f"Processing JP2 data - decoded sizes: Red={len(red_bytes)}, Green={len(green_bytes)}, NIR={len(nir_bytes)}")
|
| 199 |
|
| 200 |
# Read red band data (use as reference for dimensions)
|
| 201 |
red_data, target_height, target_width, red_profile = self.safe_read_jp2_bytes(red_bytes)
|
|
|
|
| 220 |
responses.append(response)
|
| 221 |
continue
|
| 222 |
|
| 223 |
+
# Check for valid dimensions
|
| 224 |
+
if red_data.shape[0] == 0 or red_data.shape[1] == 0:
|
| 225 |
+
error_msg = f"Invalid band dimensions: {red_data.shape}"
|
| 226 |
+
error = pb_utils.TritonError(error_msg)
|
| 227 |
+
response = pb_utils.InferenceResponse(output_tensors=[], error=error)
|
| 228 |
+
responses.append(response)
|
| 229 |
+
continue
|
| 230 |
+
|
| 231 |
# Stack bands in CHW format for prediction (channels, height, width)
|
| 232 |
prediction_array = np.stack([red_data, green_data, nir_data], axis=0)
|
| 233 |
print(f"Final prediction array shape: {prediction_array.shape}")
|