Spaces:
Sleeping
Sleeping
Fix: Runtime error
Browse files- app.py +1 -1
- inference.py +4 -16
app.py
CHANGED
|
@@ -67,7 +67,7 @@ def inference_wrapper(image, alpha, top_k, target_layer):
|
|
| 67 |
)
|
| 68 |
except Exception as e:
|
| 69 |
print(f"Error in inference: {str(e)}")
|
| 70 |
-
return
|
| 71 |
|
| 72 |
|
| 73 |
def main():
|
|
|
|
| 67 |
)
|
| 68 |
except Exception as e:
|
| 69 |
print(f"Error in inference: {str(e)}")
|
| 70 |
+
return None, None
|
| 71 |
|
| 72 |
|
| 73 |
def main():
|
inference.py
CHANGED
|
@@ -28,35 +28,23 @@ def inference(image, alpha, top_k, target_layer, model=None, classes=None):
|
|
| 28 |
model = model.to(device)
|
| 29 |
model.eval()
|
| 30 |
|
| 31 |
-
# Convert input to tensor and move to GPU
|
| 32 |
-
if isinstance(image, np.ndarray):
|
| 33 |
-
image_tensor = torch.from_numpy(image).to(device)
|
| 34 |
-
if image_tensor.ndim == 3:
|
| 35 |
-
image_tensor = image_tensor.unsqueeze(0)
|
| 36 |
-
else:
|
| 37 |
-
image_tensor = image.to(device)
|
| 38 |
-
|
| 39 |
with torch.cuda.amp.autocast(): # Enable automatic mixed precision
|
| 40 |
with torch.no_grad():
|
| 41 |
# Save a copy of input img
|
| 42 |
org_img = image.copy()
|
| 43 |
|
| 44 |
-
# Calculate mean over each channel
|
| 45 |
mean_r, mean_g, mean_b = np.mean(image[:, :, 0]/255.), np.mean(image[:, :, 1]/255.), np.mean(image[:, :, 2]/255.)
|
| 46 |
-
|
| 47 |
-
# Calculate Standard deviation over each channel
|
| 48 |
std_r, std_g, std_b = np.std(image[:, :, 0]/255.), np.std(image[:, :, 1]/255.), np.std(image[:, :, 2]/255.)
|
| 49 |
|
| 50 |
# Convert img to tensor and normalize it
|
| 51 |
_transform = transforms.Compose([
|
| 52 |
transforms.ToTensor(),
|
| 53 |
transforms.Normalize((mean_r, mean_g, mean_b), (std_r, std_g, std_b))
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
# Preprocess the input image
|
| 57 |
-
input_tensor = _transform(image)
|
| 58 |
|
| 59 |
-
#
|
|
|
|
| 60 |
input_tensor = input_tensor.unsqueeze(0)
|
| 61 |
|
| 62 |
# Get Model Predictions
|
|
|
|
| 28 |
model = model.to(device)
|
| 29 |
model.eval()
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
with torch.cuda.amp.autocast(): # Enable automatic mixed precision
|
| 32 |
with torch.no_grad():
|
| 33 |
# Save a copy of input img
|
| 34 |
org_img = image.copy()
|
| 35 |
|
| 36 |
+
# Calculate mean and std over each channel
|
| 37 |
mean_r, mean_g, mean_b = np.mean(image[:, :, 0]/255.), np.mean(image[:, :, 1]/255.), np.mean(image[:, :, 2]/255.)
|
|
|
|
|
|
|
| 38 |
std_r, std_g, std_b = np.std(image[:, :, 0]/255.), np.std(image[:, :, 1]/255.), np.std(image[:, :, 2]/255.)
|
| 39 |
|
| 40 |
# Convert img to tensor and normalize it
|
| 41 |
_transform = transforms.Compose([
|
| 42 |
transforms.ToTensor(),
|
| 43 |
transforms.Normalize((mean_r, mean_g, mean_b), (std_r, std_g, std_b))
|
| 44 |
+
])
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
# Preprocess the input image and move to device
|
| 47 |
+
input_tensor = _transform(image).to(device)
|
| 48 |
input_tensor = input_tensor.unsqueeze(0)
|
| 49 |
|
| 50 |
# Get Model Predictions
|