Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	Upload 5 files
#5
by
						
kadirnar
	
							
						- opened
							
					
- app.py +11 -6
- data/Inria.jpg +0 -0
- data/Istanbul.jpg +0 -0
- requirements.txt +5 -3
- utils/dataloader.py +55 -0
- utils/download.py +17 -0
- utils/istanbul_unet.py +21 -0
    	
        app.py
    CHANGED
    
    | @@ -3,13 +3,10 @@ from PIL import Image | |
| 3 | 
             
            import gradio as gr
         | 
| 4 | 
             
            import numpy
         | 
| 5 | 
             
            import torch
         | 
| 6 | 
            -
            import os
         | 
| 7 |  | 
| 8 | 
            -
             | 
| 9 | 
            -
             | 
| 10 | 
            -
            model_id_list = ['deprem-ml/Binafarktespit-yolo5x-v1-xview', 'SerdarHelli/deprem_satellite_labeled_yolov8', 'kadirnar/yolov7-v0.1']
         | 
| 11 | 
             
            current_device = "cuda" if torch.cuda.is_available() else "cpu"
         | 
| 12 | 
            -
            model_types = ["YOLOv5", "YOLOv5 + SAHI", "YOLOv8", "YOLOv7"]
         | 
| 13 |  | 
| 14 | 
             
            def sahi_yolov5_inference(
         | 
| 15 | 
             
                image,
         | 
| @@ -25,7 +22,7 @@ def sahi_yolov5_inference( | |
| 25 | 
             
                postprocess_match_threshold=0.25,
         | 
| 26 | 
             
                postprocess_class_agnostic=False,
         | 
| 27 | 
             
            ):
         | 
| 28 | 
            -
             | 
| 29 | 
             
                rect_th = None or max(round(sum(image.size) / 2 * 0.0001), 1)
         | 
| 30 | 
             
                text_th = None or max(rect_th - 2, 1)
         | 
| 31 |  | 
| @@ -100,6 +97,12 @@ def sahi_yolov5_inference( | |
| 100 | 
             
                    results = model([image], size=image_size)
         | 
| 101 | 
             
                    return results.render()[0]
         | 
| 102 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 103 | 
             
            inputs = [
         | 
| 104 | 
             
                gr.Image(type="pil", label="Original Image"),
         | 
| 105 | 
             
                gr.Dropdown(choices=model_id_list,label="Choose Model",value=model_id_list[0]),
         | 
| @@ -125,8 +128,10 @@ examples = [ | |
| 125 | 
             
                ["data/27.jpg", 'deprem-ml/Binafarktespit-yolo5x-v1-xview', "YOLOv5 + SAHI", 640, 512, 512, 0.1, 0.1, "NMS", "IOU", 0.25, False],
         | 
| 126 | 
             
                ["data/28.jpg", 'deprem-ml/Binafarktespit-yolo5x-v1-xview', "YOLOv5 + SAHI", 640, 512, 512, 0.1, 0.1, "NMS", "IOU", 0.25, False],
         | 
| 127 | 
             
                ["data/31.jpg", 'deprem-ml/SerdarHelli-yolov8-v1-xview', "YOLOv8", 640, 512, 512, 0.1, 0.1, "NMS", "IOU", 0.25, False],
         | 
|  | |
| 128 | 
             
            ]
         | 
| 129 |  | 
|  | |
| 130 | 
             
            demo = gr.Interface(
         | 
| 131 | 
             
                sahi_yolov5_inference,
         | 
| 132 | 
             
                inputs,
         | 
|  | |
| 3 | 
             
            import gradio as gr
         | 
| 4 | 
             
            import numpy
         | 
| 5 | 
             
            import torch
         | 
|  | |
| 6 |  | 
| 7 | 
            +
            model_id_list = ['deprem-ml/Binafarktespit-yolo5x-v1-xview', 'SerdarHelli/deprem_satellite_labeled_yolov8', 'kadirnar/yolov7-v0.1', 'kadirnar/UNet-EfficientNet-b6-Istanbul']
         | 
|  | |
|  | |
| 8 | 
             
            current_device = "cuda" if torch.cuda.is_available() else "cpu"
         | 
| 9 | 
            +
            model_types = ["YOLOv5", "YOLOv5 + SAHI", "YOLOv8", "YOLOv7", "Unet-Istanbul"]
         | 
| 10 |  | 
| 11 | 
             
            def sahi_yolov5_inference(
         | 
| 12 | 
             
                image,
         | 
|  | |
| 22 | 
             
                postprocess_match_threshold=0.25,
         | 
| 23 | 
             
                postprocess_class_agnostic=False,
         | 
| 24 | 
             
            ):
         | 
| 25 | 
            +
                
         | 
| 26 | 
             
                rect_th = None or max(round(sum(image.size) / 2 * 0.0001), 1)
         | 
| 27 | 
             
                text_th = None or max(rect_th - 2, 1)
         | 
| 28 |  | 
|  | |
| 97 | 
             
                    results = model([image], size=image_size)
         | 
| 98 | 
             
                    return results.render()[0]
         | 
| 99 |  | 
| 100 | 
            +
                elif model_type == "Unet-Istanbul":
         | 
| 101 | 
            +
                    from utils.istanbul_unet import unet_prediction
         | 
| 102 | 
            +
                    
         | 
| 103 | 
            +
                    output = unet_prediction(input_path=image, model_path=model_id)
         | 
| 104 | 
            +
                    return output
         | 
| 105 | 
            +
                
         | 
| 106 | 
             
            inputs = [
         | 
| 107 | 
             
                gr.Image(type="pil", label="Original Image"),
         | 
| 108 | 
             
                gr.Dropdown(choices=model_id_list,label="Choose Model",value=model_id_list[0]),
         | 
|  | |
| 128 | 
             
                ["data/27.jpg", 'deprem-ml/Binafarktespit-yolo5x-v1-xview', "YOLOv5 + SAHI", 640, 512, 512, 0.1, 0.1, "NMS", "IOU", 0.25, False],
         | 
| 129 | 
             
                ["data/28.jpg", 'deprem-ml/Binafarktespit-yolo5x-v1-xview', "YOLOv5 + SAHI", 640, 512, 512, 0.1, 0.1, "NMS", "IOU", 0.25, False],
         | 
| 130 | 
             
                ["data/31.jpg", 'deprem-ml/SerdarHelli-yolov8-v1-xview', "YOLOv8", 640, 512, 512, 0.1, 0.1, "NMS", "IOU", 0.25, False],
         | 
| 131 | 
            +
                ["data/Istanbul.jpg", 'kadirnar/UNet-EfficientNet-b6-Istanbul', "Unet-Istanbul", 512, 512, 512, 0.1, 0.1, "NMS", "IOU", 0.25, False],
         | 
| 132 | 
             
            ]
         | 
| 133 |  | 
| 134 | 
            +
             | 
| 135 | 
             
            demo = gr.Interface(
         | 
| 136 | 
             
                sahi_yolov5_inference,
         | 
| 137 | 
             
                inputs,
         | 
    	
        data/Inria.jpg
    ADDED
    
    |   | 
    	
        data/Istanbul.jpg
    ADDED
    
    |   | 
    	
        requirements.txt
    CHANGED
    
    | @@ -1,5 +1,7 @@ | |
| 1 | 
            -
            torch==1. | 
| 2 | 
            -
            torchvision==0.11.3
         | 
| 3 | 
             
            yolov5==7.0.8
         | 
| 4 | 
             
            sahi==0.11.11
         | 
| 5 | 
            -
            yolov7detect==1.0.1
         | 
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            torch==1.7.1
         | 
|  | |
| 2 | 
             
            yolov5==7.0.8
         | 
| 3 | 
             
            sahi==0.11.11
         | 
| 4 | 
            +
            yolov7detect==1.0.1
         | 
| 5 | 
            +
            ultralyticsplus==0.0.26
         | 
| 6 | 
            +
            segmentation-models-pytorch==0.1.3
         | 
| 7 | 
            +
            albumentations==1.3.0
         | 
    	
        utils/dataloader.py
    ADDED
    
    | @@ -0,0 +1,55 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import albumentations as albu
         | 
| 2 | 
            +
            import numpy as np
         | 
| 3 | 
            +
            import cv2
         | 
| 4 | 
            +
            import os
         | 
| 5 | 
            +
            os.environ['CUDA_VISIBLE_DEVICES'] = '0'
         | 
| 6 | 
            +
             | 
| 7 | 
            +
             | 
| 8 | 
            +
            class Dataset:
         | 
| 9 | 
            +
                def __init__(
         | 
| 10 | 
            +
                        self, 
         | 
| 11 | 
            +
                        image_path, 
         | 
| 12 | 
            +
                        augmentation=None, 
         | 
| 13 | 
            +
                        preprocessing=None,
         | 
| 14 | 
            +
                ):
         | 
| 15 | 
            +
                    self.pil_image = image_path
         | 
| 16 | 
            +
                    self.augmentation = augmentation
         | 
| 17 | 
            +
                    self.preprocessing = preprocessing
         | 
| 18 | 
            +
                
         | 
| 19 | 
            +
                def get(self):
         | 
| 20 | 
            +
                    # pil image > numpy array
         | 
| 21 | 
            +
                    image = np.array(self.pil_image)
         | 
| 22 | 
            +
                    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                    # apply augmentations
         | 
| 25 | 
            +
                    if self.augmentation:
         | 
| 26 | 
            +
                        sample = self.augmentation(image=image)
         | 
| 27 | 
            +
                        image = sample['image']
         | 
| 28 | 
            +
                    
         | 
| 29 | 
            +
                    # apply preprocessing
         | 
| 30 | 
            +
                    if self.preprocessing:
         | 
| 31 | 
            +
                        sample = self.preprocessing(image=image)
         | 
| 32 | 
            +
                        image = sample['image']
         | 
| 33 | 
            +
                        
         | 
| 34 | 
            +
                    return image
         | 
| 35 | 
            +
             | 
| 36 | 
            +
             | 
| 37 | 
            +
            def get_validation_augmentation():
         | 
| 38 | 
            +
                """Add paddings to make image shape divisible by 32"""
         | 
| 39 | 
            +
                test_transform = [
         | 
| 40 | 
            +
                    albu.PadIfNeeded(384, 480)
         | 
| 41 | 
            +
                ]
         | 
| 42 | 
            +
                return albu.Compose(test_transform)
         | 
| 43 | 
            +
             | 
| 44 | 
            +
             | 
| 45 | 
            +
            def to_tensor(x, **kwargs):
         | 
| 46 | 
            +
                return x.transpose(2, 0, 1).astype('float32')
         | 
| 47 | 
            +
             | 
| 48 | 
            +
             | 
| 49 | 
            +
            def get_preprocessing(preprocessing_fn):
         | 
| 50 | 
            +
                
         | 
| 51 | 
            +
                _transform = [
         | 
| 52 | 
            +
                    albu.Lambda(image=preprocessing_fn),
         | 
| 53 | 
            +
                    albu.Lambda(image=to_tensor),
         | 
| 54 | 
            +
                ]
         | 
| 55 | 
            +
                return albu.Compose(_transform)    
         | 
    	
        utils/download.py
    ADDED
    
    | @@ -0,0 +1,17 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            def attempt_download_from_hub(repo_id, hf_token=None):
         | 
| 2 | 
            +
                # https://github.com/fcakyon/yolov5-pip/blob/main/yolov5/utils/downloads.py
         | 
| 3 | 
            +
                from huggingface_hub import hf_hub_download, list_repo_files
         | 
| 4 | 
            +
                from huggingface_hub.utils._errors import RepositoryNotFoundError
         | 
| 5 | 
            +
                from huggingface_hub.utils._validators import HFValidationError
         | 
| 6 | 
            +
                try:
         | 
| 7 | 
            +
                    repo_files = list_repo_files(repo_id=repo_id, repo_type='model', token=hf_token)
         | 
| 8 | 
            +
                    model_file = [f for f in repo_files if f.endswith('.pth')][0]
         | 
| 9 | 
            +
                    file = hf_hub_download(
         | 
| 10 | 
            +
                        repo_id=repo_id,
         | 
| 11 | 
            +
                        filename=model_file,
         | 
| 12 | 
            +
                        repo_type='model',
         | 
| 13 | 
            +
                        token=hf_token,
         | 
| 14 | 
            +
                    )
         | 
| 15 | 
            +
                    return file
         | 
| 16 | 
            +
                except (RepositoryNotFoundError, HFValidationError):
         | 
| 17 | 
            +
                    return None
         | 
    	
        utils/istanbul_unet.py
    ADDED
    
    | @@ -0,0 +1,21 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            from utils.download import attempt_download_from_hub
         | 
| 2 | 
            +
            import segmentation_models_pytorch as smp
         | 
| 3 | 
            +
            from utils.dataloader import *
         | 
| 4 | 
            +
            import torch
         | 
| 5 | 
            +
             | 
| 6 | 
            +
             | 
| 7 | 
            +
            def unet_prediction(input_path, model_path):
         | 
| 8 | 
            +
                model_path = attempt_download_from_hub(model_path)
         | 
| 9 | 
            +
                best_model = torch.load(model_path)
         | 
| 10 | 
            +
                preprocessing_fn = smp.encoders.get_preprocessing_fn('efficientnet-b6', 'imagenet')
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                test_dataset = Dataset(input_path, augmentation=get_validation_augmentation(), preprocessing=get_preprocessing(preprocessing_fn))
         | 
| 13 | 
            +
                image = test_dataset.get()
         | 
| 14 | 
            +
                
         | 
| 15 | 
            +
                x_tensor = torch.from_numpy(image).to("cuda").unsqueeze(0)
         | 
| 16 | 
            +
                pr_mask = best_model.predict(x_tensor)
         | 
| 17 | 
            +
                pr_mask = (pr_mask.squeeze().cpu().numpy().round())*255
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                # Save the predicted mask
         | 
| 20 | 
            +
                cv2.imwrite("output.png", pr_mask)
         | 
| 21 | 
            +
                return 'output.png'
         | 
 
			
