Spaces:
Running
Running
| import os | |
| from box.exceptions import BoxValueError | |
| import yaml | |
| from EmotionRecognition import logger | |
| import json | |
| from ensure import ensure_annotations | |
| from box import ConfigBox | |
| from pathlib import Path | |
| from typing import Any | |
| import json | |
| import tensorflow as tf | |
| 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 | |
| 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}") | |
| def save_json(path: Path, data: dict): | |
| with open(path, "w") as f: | |
| json.dump(data, f, indent=4) | |
| logger.info(f"json file saved at: {path}") | |
| def create_mobilenetv2_model(input_shape, num_classes, dropout_rate, is_training=True): # <--- ADD ARGUMENT | |
| """ | |
| Builds the MobileNetV2 model with our custom head. | |
| This centralized function ensures consistency. | |
| """ | |
| base_model = tf.keras.applications.MobileNetV2( | |
| input_shape=input_shape, include_top=False, weights='imagenet' | |
| ) | |
| inputs = tf.keras.Input(shape=input_shape) | |
| # --- CRITICAL CHANGE --- | |
| # Pass the is_training flag to the base model call | |
| x = base_model(inputs, training=is_training) | |
| # --- END CHANGE --- | |
| x = tf.keras.layers.GlobalAveragePooling2D()(x) | |
| x = tf.keras.layers.Dense(128, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.01))(x) | |
| x = tf.keras.layers.Dropout(dropout_rate)(x) | |
| outputs = tf.keras.layers.Dense(num_classes, activation='softmax')(x) | |
| model = tf.keras.Model(inputs, outputs) | |
| return model |