File size: 3,965 Bytes
3a61b02
 
 
 
 
 
 
 
7817b5d
3a61b02
 
36b8bfd
3a61b02
 
 
 
 
 
 
 
 
 
 
 
 
1d966f6
3a61b02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d966f6
3a61b02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d966f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36b8bfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import os
from box.exceptions import BoxValueError
import yaml
from cnnClassifier import logger
import joblib
import json
from box import ConfigBox
from ensure import ensure_annotations
from pathlib import Path
from typing import Any
import base64
import tensorflow as tf



@ensure_annotations
def read_yaml (path_to_yaml: Path) -> ConfigBox:
    """reads yaml file and returns
    
    Args:
        path_to_yaml (str): path like input
        
    Raises:
        ValueError: if yaml file is empty
        e: empty file
            
    Returns:
        ConfigBox: ConfigBox type
        
    """
    
    try:
        with open(path_to_yaml) as yaml_file:
            content = yaml.safe_load(yaml_file)
            logger.info(f"yaml file: {path_to_yaml} loaded successfully")
            return ConfigBox(content)
        
    except BoxValueError:
        raise ValueError ("yaml file is empty")
    except Exception as e:
        raise e
    
    
@ensure_annotations
def create_directories (path_to_directories: list, verbose=True):
    """create list of directories
    
    Args:
    path_to_directories (list): list of path of directories
    ignore_log (bool, optional): ignore if multiple dirs is to be created. Defaults to False 
    """
        
    for path in path_to_directories:
        os.makedirs(path, exist_ok=True)
        if verbose:
            logger.info(f"created directory at: {path}")
            
            

@ensure_annotations
def save_json(path: Path, data: dict):
    """save json data
    
    Args:
        path (Path): path to json file
        data (dict): data to be saved in json file  
    """
    with open(path, "w") as f:
        json.dump(data, f, indent=4)
        
    logger.info(f"json file saved at: {path}")
    

@ensure_annotations
def load_json(path: Path) -> ConfigBox:
    """load json files data
    
    Args:
        path (Path): path to json file
        
    Returns:
        ConfigBox data as class attribute instead of dict
    """
    
    with open(path) as f:
        content = json.load(f)
        
    logger.info(f"json file loaded successfully from {path}")
    return ConfigBox(content)


@ensure_annotations
def save_bin(data: Any, path: Path):
    """save binary file
    
    Args:
        data (Any): data to be saved as binary
        path (Path): path to binary file
    """
    joblib.dump(value=data, filename=path)
    logger.info(f"binary file saved at {path}")
    
    
@ensure_annotations
def load_bin(path: Path) -> Any:
    """load binary data
    
    Args:
        path (Path): path to binary file
        
    Returns:
        Any: object stored in the file
    """
    data = joblib.load(path)
    logger.info(f"binary file loaded from {path}")
    return data

@ensure_annotations
def get_size(path: Path) -> str:
    """get size in KB
    
    Args:
        path (Path): path of the file
        
    Returns:
        str: size in KB
    """
    
    size_in_kb = round(os.path.getsize(path)/1024)
    return f"~(size_in_kb) KB"


def decodeImage(imgstring, fileName):
    imgdata = base64.b64decode(imgstring)
    with open(fileName, 'wb') as f:
        f.write(imgdata)
        f.close()
        
        
def encodeImageIntoBase64 (croppedImagePath):
    with open(croppedImagePath, "rb") as f:
        return base64.b64encode(f.read())
        
PREPROCESSING_MAP = {
    "VGG16":            None,
    "EfficientNetV2B3": tf.keras.applications.efficientnet_v2.preprocess_input,
    "DenseNet121":      tf.keras.applications.densenet.preprocess_input,
    "ResNet50V2":       tf.keras.applications.resnet_v2.preprocess_input,
}

def build_datagenerator_kwargs(model_name: str) -> dict:
    preprocess_fn = PREPROCESSING_MAP.get(model_name)
    if preprocess_fn:
        return dict(
            preprocessing_function=preprocess_fn,
            validation_split=0.20
        )
    return dict(
        rescale=1./255,
        validation_split=0.20
    )