Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -11,8 +11,8 @@ from io import BytesIO | |
| 11 | 
             
            import numpy as np
         | 
| 12 | 
             
            from scipy.spatial.distance import mahalanobis
         | 
| 13 |  | 
| 14 | 
            -
            # Setup logging | 
| 15 | 
            -
            logging.basicConfig(level=logging. | 
| 16 | 
             
            logger = logging.getLogger(__name__)
         | 
| 17 |  | 
| 18 | 
             
            # Define the number of classes
         | 
| @@ -92,7 +92,7 @@ def is_in_distribution(features): | |
| 92 | 
             
                    distance = compute_mahalanobis_distance(features, mean, cov)
         | 
| 93 | 
             
                    distances.append(distance)
         | 
| 94 | 
             
                min_distance = min(distances)
         | 
| 95 | 
            -
                logger.info(f"Minimum Mahalanobis distance: {min_distance:.4f}") | 
| 96 | 
             
                return min_distance < MAHALANOBIS_THRESHOLD
         | 
| 97 |  | 
| 98 | 
             
            # Prediction function for an uploaded image
         | 
| @@ -105,6 +105,7 @@ def predict_from_image_url(image_url): | |
| 105 |  | 
| 106 | 
             
                    # Apply transformations
         | 
| 107 | 
             
                    image_tensor = transform(image).unsqueeze(0)  # Shape: [1, 3, 224, 224]
         | 
|  | |
| 108 |  | 
| 109 | 
             
                    # Extract features from the penultimate layer
         | 
| 110 | 
             
                    with torch.no_grad():
         | 
| @@ -128,7 +129,10 @@ def predict_from_image_url(image_url): | |
| 128 | 
             
                    # Stage 2: Main Model Prediction
         | 
| 129 | 
             
                    with torch.no_grad():
         | 
| 130 | 
             
                        outputs = main_model(image_tensor)  # Shape: [1, 3]
         | 
|  | |
|  | |
| 131 | 
             
                        probabilities = torch.softmax(outputs, dim=1)[0]  # Convert to probabilities
         | 
|  | |
| 132 | 
             
                        predicted_class = torch.argmax(outputs, dim=1).item()
         | 
| 133 |  | 
| 134 | 
             
                    # Define class information
         | 
| @@ -194,4 +198,4 @@ demo = gr.Interface( | |
| 194 | 
             
            )
         | 
| 195 |  | 
| 196 | 
             
            if __name__ == "__main__":
         | 
| 197 | 
            -
                demo.launch() | 
|  | |
| 11 | 
             
            import numpy as np
         | 
| 12 | 
             
            from scipy.spatial.distance import mahalanobis
         | 
| 13 |  | 
| 14 | 
            +
            # Setup logging
         | 
| 15 | 
            +
            logging.basicConfig(level=logging.INFO)
         | 
| 16 | 
             
            logger = logging.getLogger(__name__)
         | 
| 17 |  | 
| 18 | 
             
            # Define the number of classes
         | 
|  | |
| 92 | 
             
                    distance = compute_mahalanobis_distance(features, mean, cov)
         | 
| 93 | 
             
                    distances.append(distance)
         | 
| 94 | 
             
                min_distance = min(distances)
         | 
| 95 | 
            +
                logger.info(f"Minimum Mahalanobis distance: {min_distance:.4f}")
         | 
| 96 | 
             
                return min_distance < MAHALANOBIS_THRESHOLD
         | 
| 97 |  | 
| 98 | 
             
            # Prediction function for an uploaded image
         | 
|  | |
| 105 |  | 
| 106 | 
             
                    # Apply transformations
         | 
| 107 | 
             
                    image_tensor = transform(image).unsqueeze(0)  # Shape: [1, 3, 224, 224]
         | 
| 108 | 
            +
                    logger.info(f"Input image tensor shape: {image_tensor.shape}")
         | 
| 109 |  | 
| 110 | 
             
                    # Extract features from the penultimate layer
         | 
| 111 | 
             
                    with torch.no_grad():
         | 
|  | |
| 129 | 
             
                    # Stage 2: Main Model Prediction
         | 
| 130 | 
             
                    with torch.no_grad():
         | 
| 131 | 
             
                        outputs = main_model(image_tensor)  # Shape: [1, 3]
         | 
| 132 | 
            +
                        logger.info(f"Model output shape: {outputs.shape}")
         | 
| 133 | 
            +
                        logger.info(f"Raw logits: {outputs[0].numpy()}")
         | 
| 134 | 
             
                        probabilities = torch.softmax(outputs, dim=1)[0]  # Convert to probabilities
         | 
| 135 | 
            +
                        logger.info(f"Softmax probabilities: {probabilities.numpy()}")
         | 
| 136 | 
             
                        predicted_class = torch.argmax(outputs, dim=1).item()
         | 
| 137 |  | 
| 138 | 
             
                    # Define class information
         | 
|  | |
| 198 | 
             
            )
         | 
| 199 |  | 
| 200 | 
             
            if __name__ == "__main__":
         | 
| 201 | 
            +
                demo.launch()
         |