Spaces:
Sleeping
Sleeping
Update app.py
Browse files3 unmodified lines4567891 unmodified line1112131415161718192021222324252627282930316 unmodified lines383940414243443 unmodified linesfrom tensorflow.keras.applications.efficientnet import preprocess_inputfrom PIL import Imagefrom tensorflow.keras.layers import InputLayer# 1. Định nghĩa hằng sốMODEL_PATH = "best_model.h5" # Tên file mô hình đã upload (ĐÃ SỬA: Đồng bộ với tên file trên GitHub)1 unmodified lineCLASS_NAMES = ['bad', 'good', 'very_good']# =================================================================# KHẮC PHỤC LỖI DESERIALIZATION (batch_shape)# Nếu mô hình được lưu bằng Keras cũ, ta cần định nghĩa lại InputLayer# để chấp nhận tham số batch_shape khi tải.class FixedInputLayer(InputLayer):"""Tạo InputLayer cố định để xử lý lỗi "Unrecognized keyword arguments: ['batch_shape']"khi tải mô hình đã lưu bằng TF/Keras cũ hơn."""def __init__(self, **kwargs):if 'batch_shape' in kwargs:# Chuyển batch_shape thành input_shapekwargs['input_shape'] = kwargs['batch_shape'][1:]del kwargs['batch_shape']super().__init__(**kwargs)# =================================================================# 2. Tải mô hình6 unmodified linesmodel = tf.keras.models.load_model(MODEL_PATH,custom_objects={'InputLayer': FixedInputLayer # Sử dụng lớp FixedInputLayer để thay thế})print("✅ Mô hình đã được tải thành công.")3 unmodified lines45678910111 unmodified line13141516171819202122232425262728293031323334353637383940414243446 unmodified lines5152535455565758596061623 unmodified linesfrom tensorflow.keras.applications.efficientnet import preprocess_inputfrom PIL import Imagefrom tensorflow.keras.layers import InputLayer# Import các lớp Augmentation mà mô hình có thể đã lưufrom tensorflow.keras.layers import RandomFlip, RandomRotation, RandomZoom, RandomContrast# 1. Định nghĩa hằng sốMODEL_PATH = "best_model.h5" # Tên file mô hình đã upload (ĐÃ SỬA: Đồng bộ với tên file trên GitHub)1 unmodified lineCLASS_NAMES = ['bad', 'good', 'very_good']# =================================================================# KHẮC PHỤC LỖI DESERIALIZATION CHO CÁC LỚP AUGMENTATION VÀ INPUT LAYER# Mục đích: Bỏ qua tham số 'batch_shape' và 'data_format' không tương thích.class FixedInputLayer(InputLayer):"""Xử lý lỗi batch_shape."""def __init__(self, **kwargs):if 'batch_shape' in kwargs:kwargs['input_shape'] = kwargs['batch_shape'][1:]del kwargs['batch_shape']super().__init__(**kwargs)def fix_augmentation_layer(LayerClass):"""Tạo lớp cố định để loại bỏ tham số 'data_format'."""class FixedLayer(LayerClass):def __init__(self, **kwargs):if 'data_format' in kwargs:del kwargs['data_format']super().__init__(**kwargs)return FixedLayer# Tạo các lớp cố định cho AugmentationFixedRandomFlip = fix_augmentation_layer(RandomFlip)FixedRandomRotation = fix_augmentation_layer(RandomRotation)FixedRandomZoom = fix_augmentation_layer(RandomZoom)FixedRandomContrast = fix_augmentation_layer(RandomContrast)# =================================================================# 2. Tải mô hình6 unmodified linesmodel = tf.keras.models.load_model(MODEL_PATH,custom_objects={'InputLayer': FixedInputLayer, # Fix InputLayer'RandomFlip': FixedRandomFlip, # Fix RandomFlip'RandomRotation': FixedRandomRotation, # Fix RandomRotation'RandomZoom': FixedRandomZoom, # Fix RandomZoom'RandomContrast': FixedRandomContrast # Fix RandomContrast# Bổ sung các lớp Augmentation khác nếu cần})print("✅ Mô hình đã được tải thành công.")
app.py+27-9