diff --git a/Archive/API/Python/Example.py b/Archive/API/Python/Example.py deleted file mode 100644 index a98f089..0000000 --- a/Archive/API/Python/Example.py +++ /dev/null @@ -1,30 +0,0 @@ -# Import necessary libraries -import pprint -import numpy as np -from pdai import * -from PIL import Image - -pp = pprint.PrettyPrinter(indent=4, width=10) - -# Instantiate the PneumoniaModel class -pdai_model = PneumoniaModel("models\Ready\V1\PAI_model.h5", verbose=0) - -# Load the model -pdai_model.load_model() - -# Load an image for prediction -img_path = "API\\Python\\test sampels\\PNEUMONIA\\person1947_bacteria_4876.jpeg" -img = Image.open(img_path) -img = img.convert("RGB") # Convert grayscale to RGB -img = img.resize((280, 300)) -x = np.array(img) -x = np.expand_dims(x, axis=0) - -print("without CLAHE>>>") -# Make a prediction without CLAHE -result = pdai_model.predict(x) -pp.pprint(result) -print("with CLAHE>>>") -# Make a prediction with CLAHE -result = pdai_model.predict(x, clahe=True) -pp.pprint(result) diff --git a/Archive/API/Python/pdai.py b/Archive/API/Python/pdai.py deleted file mode 100644 index be4aa96..0000000 --- a/Archive/API/Python/pdai.py +++ /dev/null @@ -1,118 +0,0 @@ -import os - -os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3" -from keras.models import load_model -from typing import Union, Dict -import numpy as np -import cv2 - - -class PneumoniaModel: - def __init__(self, model_path: str, verbose: int = 0): - """ - Initializes the PneumoniaModel with the given model path and verbosity level. - - Args: - model_path (str): Path to the saved model. - verbose (int, optional): Verbosity level. If 1, prints status messages during operations. Defaults to 0. - """ - self.model_path = model_path - self.model = None - self.verbose = verbose - - def load_model(self) -> Dict[str, Union[str, None]]: - """ - Loads the model from the path specified during initialization. - - Returns: - dict: A dictionary with a "status" key. If the model is loaded successfully, "status" is "success". - If an error occurs, "status" is "error" and an additional "message" key contains the error message. - """ - try: - self.model = None - self.model = load_model(self.model_path) - if self.verbose == 1: - print("Model loaded successfully.") - except Exception as e: - if self.verbose == 1: - print(f"Error loading model: {str(e)}") - return {"status": "error", "message": str(e)} - - return {"status": "success"} - - def predict(self, image: np.ndarray, clahe: bool = False) -> Dict[str, Union[str, float, None]]: - """ - Makes a prediction using the loaded model on the given image. - - Args: - image (np.ndarray): The image to make a prediction on. - clahe (bool, optional): Whether to apply CLAHE to the image before making a prediction. Defaults to False. - - Returns: - dict: A dictionary with a "status" key. If the prediction is made successfully, "status" is "success", - and additional "prediction" and "confidence" keys contain the prediction and confidence level. - If an error occurs, "status" is "error" and an additional "message" key contains the error message. - """ - if self.model is None: - if self.verbose == 1: - print("Model not loaded. Call load_model() first.") - return { - "status": "error", - "message": "Model not loaded. Call load_model() first.", - } - - if image.ndim != 4 or image.shape[3] != 3: - return { - "status": "error", - "message": f"Invalid image format. The image should have three color channels (RGB). Img shape = {image.shape}.", - } - - try: - if clahe: - # Create a CLAHE object - clahe = cv2.createCLAHE(clipLimit=2, tileGridSize=(8, 8)) - - b, g, r = cv2.split(image[0]) - - # Convert the channels to the appropriate format - b = cv2.convertScaleAbs(b) - g = cv2.convertScaleAbs(g) - r = cv2.convertScaleAbs(r) - - # Apply adaptive histogram equalization to each channel - equalized_b = clahe.apply(b) - equalized_g = clahe.apply(g) - equalized_r = clahe.apply(r) - - # Merge the equalized channels back into an image - equalized_image = cv2.merge((equalized_b, equalized_g, equalized_r)) - - # Replace the original image with the equalized image in the array - image = equalized_image - - # Normalize the image - image = image / 255.0 - - if self.verbose == 1: - print("Making prediction...") - prediction = self.model.predict(image) - if np.argmax(prediction) == 0: - if self.verbose == 1: - print("Prediction: Normal") - return { - "status": "success", - "prediction": "Normal", - "confidence": np.max(prediction), - } - else: - if self.verbose == 1: - print("Prediction: Pneumonia") - return { - "status": "success", - "prediction": "Pneumonia", - "confidence": np.max(prediction), - } - except IndexError as e: - if self.verbose == 1: - print(f"Error making prediction: {str(e)}") - return {"status": "error", "message": str(e)} diff --git a/Archive/API/Python/test sampels/NORMAL/NORMAL2-IM-1427-0001.jpeg b/Archive/API/Python/test sampels/NORMAL/NORMAL2-IM-1427-0001.jpeg deleted file mode 100644 index aba8d7f..0000000 Binary files a/Archive/API/Python/test sampels/NORMAL/NORMAL2-IM-1427-0001.jpeg and /dev/null differ diff --git a/Archive/API/Python/test sampels/NORMAL/NORMAL2-IM-1430-0001.jpeg b/Archive/API/Python/test sampels/NORMAL/NORMAL2-IM-1430-0001.jpeg deleted file mode 100644 index 5839aa5..0000000 Binary files a/Archive/API/Python/test sampels/NORMAL/NORMAL2-IM-1430-0001.jpeg and /dev/null differ diff --git a/Archive/API/Python/test sampels/NORMAL/NORMAL2-IM-1431-0001.jpeg b/Archive/API/Python/test sampels/NORMAL/NORMAL2-IM-1431-0001.jpeg deleted file mode 100644 index 4dfd4c5..0000000 Binary files a/Archive/API/Python/test sampels/NORMAL/NORMAL2-IM-1431-0001.jpeg and /dev/null differ diff --git a/Archive/API/Python/test sampels/NORMAL/NORMAL2-IM-1436-0001.jpeg b/Archive/API/Python/test sampels/NORMAL/NORMAL2-IM-1436-0001.jpeg deleted file mode 100644 index fb9473d..0000000 Binary files a/Archive/API/Python/test sampels/NORMAL/NORMAL2-IM-1436-0001.jpeg and /dev/null differ diff --git a/Archive/API/Python/test sampels/NORMAL/NORMAL2-IM-1437-0001.jpeg b/Archive/API/Python/test sampels/NORMAL/NORMAL2-IM-1437-0001.jpeg deleted file mode 100644 index f976342..0000000 Binary files a/Archive/API/Python/test sampels/NORMAL/NORMAL2-IM-1437-0001.jpeg and /dev/null differ diff --git a/Archive/API/Python/test sampels/NORMAL/NORMAL2-IM-1438-0001.jpeg b/Archive/API/Python/test sampels/NORMAL/NORMAL2-IM-1438-0001.jpeg deleted file mode 100644 index b87d5bf..0000000 Binary files a/Archive/API/Python/test sampels/NORMAL/NORMAL2-IM-1438-0001.jpeg and /dev/null differ diff --git a/Archive/API/Python/test sampels/NORMAL/NORMAL2-IM-1440-0001.jpeg b/Archive/API/Python/test sampels/NORMAL/NORMAL2-IM-1440-0001.jpeg deleted file mode 100644 index 4711d6a..0000000 Binary files a/Archive/API/Python/test sampels/NORMAL/NORMAL2-IM-1440-0001.jpeg and /dev/null differ diff --git a/Archive/API/Python/test sampels/NORMAL/NORMAL2-IM-1442-0001.jpeg b/Archive/API/Python/test sampels/NORMAL/NORMAL2-IM-1442-0001.jpeg deleted file mode 100644 index dfe506a..0000000 Binary files a/Archive/API/Python/test sampels/NORMAL/NORMAL2-IM-1442-0001.jpeg and /dev/null differ diff --git a/Archive/API/Python/test sampels/PNEUMONIA/person1946_bacteria_4874.jpeg b/Archive/API/Python/test sampels/PNEUMONIA/person1946_bacteria_4874.jpeg deleted file mode 100644 index 6237f88..0000000 Binary files a/Archive/API/Python/test sampels/PNEUMONIA/person1946_bacteria_4874.jpeg and /dev/null differ diff --git a/Archive/API/Python/test sampels/PNEUMONIA/person1946_bacteria_4875.jpeg b/Archive/API/Python/test sampels/PNEUMONIA/person1946_bacteria_4875.jpeg deleted file mode 100644 index 1524420..0000000 Binary files a/Archive/API/Python/test sampels/PNEUMONIA/person1946_bacteria_4875.jpeg and /dev/null differ diff --git a/Archive/API/Python/test sampels/PNEUMONIA/person1947_bacteria_4876.jpeg b/Archive/API/Python/test sampels/PNEUMONIA/person1947_bacteria_4876.jpeg deleted file mode 100644 index 1b795a2..0000000 Binary files a/Archive/API/Python/test sampels/PNEUMONIA/person1947_bacteria_4876.jpeg and /dev/null differ diff --git a/Archive/API/Python/test sampels/PNEUMONIA/person1949_bacteria_4880.jpeg b/Archive/API/Python/test sampels/PNEUMONIA/person1949_bacteria_4880.jpeg deleted file mode 100644 index 94644db..0000000 Binary files a/Archive/API/Python/test sampels/PNEUMONIA/person1949_bacteria_4880.jpeg and /dev/null differ diff --git a/Archive/API/Python/test sampels/PNEUMONIA/person1950_bacteria_4881.jpeg b/Archive/API/Python/test sampels/PNEUMONIA/person1950_bacteria_4881.jpeg deleted file mode 100644 index cfb813a..0000000 Binary files a/Archive/API/Python/test sampels/PNEUMONIA/person1950_bacteria_4881.jpeg and /dev/null differ diff --git a/Archive/API/Python/test sampels/PNEUMONIA/person1951_bacteria_4882.jpeg b/Archive/API/Python/test sampels/PNEUMONIA/person1951_bacteria_4882.jpeg deleted file mode 100644 index 7087e47..0000000 Binary files a/Archive/API/Python/test sampels/PNEUMONIA/person1951_bacteria_4882.jpeg and /dev/null differ diff --git a/Archive/API/Python/test sampels/PNEUMONIA/person1952_bacteria_4883.jpeg b/Archive/API/Python/test sampels/PNEUMONIA/person1952_bacteria_4883.jpeg deleted file mode 100644 index cb28074..0000000 Binary files a/Archive/API/Python/test sampels/PNEUMONIA/person1952_bacteria_4883.jpeg and /dev/null differ diff --git a/Archive/API/Python/test sampels/PNEUMONIA/person1954_bacteria_4886.jpeg b/Archive/API/Python/test sampels/PNEUMONIA/person1954_bacteria_4886.jpeg deleted file mode 100644 index 44a761d..0000000 Binary files a/Archive/API/Python/test sampels/PNEUMONIA/person1954_bacteria_4886.jpeg and /dev/null differ diff --git a/Archive/keras_applications_mod/README.md b/Archive/keras_applications_mod/README.md deleted file mode 100644 index 43230c4..0000000 --- a/Archive/keras_applications_mod/README.md +++ /dev/null @@ -1,7 +0,0 @@ -# This is a modified clone of `keras-applications` -## The repo part https://github.com/keras-team/keras/tree/master/keras/applications link: -### https://github.com/keras-team/keras-applications/tree/master -## Changed: -- efficientnet.py - - Added `EfficientNet_CXL` model. - \ No newline at end of file diff --git a/Archive/keras_applications_mod/models/__init__.py b/Archive/keras_applications_mod/models/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/Archive/keras_applications_mod/models/applications_load_weight_test.py b/Archive/keras_applications_mod/models/applications_load_weight_test.py deleted file mode 100644 index 639c46d..0000000 --- a/Archive/keras_applications_mod/models/applications_load_weight_test.py +++ /dev/null @@ -1,193 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Integration tests for Keras applications.""" - -import numpy as np -import tensorflow.compat.v2 as tf -from absl import flags -from absl.testing import parameterized - -from keras.applications import convnext -from keras.applications import densenet -from keras.applications import efficientnet -from keras.applications import efficientnet_v2 -from keras.applications import inception_resnet_v2 -from keras.applications import inception_v3 -from keras.applications import mobilenet -from keras.applications import mobilenet_v2 -from keras.applications import mobilenet_v3 -from keras.applications import nasnet -from keras.applications import regnet -from keras.applications import resnet -from keras.applications import resnet_rs -from keras.applications import resnet_v2 -from keras.applications import vgg16 -from keras.applications import vgg19 -from keras.applications import xception -from keras.utils import data_utils -from keras.utils import image_utils - -ARG_TO_MODEL = { - "resnet": (resnet, [resnet.ResNet50, resnet.ResNet101, resnet.ResNet152]), - "resnet_v2": ( - resnet_v2, - [resnet_v2.ResNet50V2, resnet_v2.ResNet101V2, resnet_v2.ResNet152V2], - ), - "vgg16": (vgg16, [vgg16.VGG16]), - "vgg19": (vgg19, [vgg19.VGG19]), - "xception": (xception, [xception.Xception]), - "inception_v3": (inception_v3, [inception_v3.InceptionV3]), - "inception_resnet_v2": ( - inception_resnet_v2, - [inception_resnet_v2.InceptionResNetV2], - ), - "mobilenet": (mobilenet, [mobilenet.MobileNet]), - "mobilenet_v2": (mobilenet_v2, [mobilenet_v2.MobileNetV2]), - "mobilenet_v3_small": (mobilenet_v3, [mobilenet_v3.MobileNetV3Small]), - "mobilenet_v3_large": (mobilenet_v3, [mobilenet_v3.MobileNetV3Large]), - "convnext": ( - convnext, - [ - convnext.ConvNeXtTiny, - convnext.ConvNeXtSmall, - convnext.ConvNeXtBase, - convnext.ConvNeXtLarge, - convnext.ConvNeXtXLarge, - ], - ), - "densenet": ( - densenet, - [densenet.DenseNet121, densenet.DenseNet169, densenet.DenseNet201], - ), - "nasnet_mobile": (nasnet, [nasnet.NASNetMobile]), - "nasnet_large": (nasnet, [nasnet.NASNetLarge]), - "efficientnet": ( - efficientnet, - [ - efficientnet.EfficientNetB0, - efficientnet.EfficientNetB1, - efficientnet.EfficientNetB2, - efficientnet.EfficientNetB3, - efficientnet.EfficientNetB4, - efficientnet.EfficientNetB5, - efficientnet.EfficientNetB6, - efficientnet.EfficientNetB7, - ], - ), - "efficientnet_v2": ( - efficientnet_v2, - [ - efficientnet_v2.EfficientNetV2B0, - efficientnet_v2.EfficientNetV2B1, - efficientnet_v2.EfficientNetV2B2, - efficientnet_v2.EfficientNetV2B3, - efficientnet_v2.EfficientNetV2S, - efficientnet_v2.EfficientNetV2M, - efficientnet_v2.EfficientNetV2L, - ], - ), - "resnet_rs": ( - resnet_rs, - [ - resnet_rs.ResNetRS50, - resnet_rs.ResNetRS101, - resnet_rs.ResNetRS152, - resnet_rs.ResNetRS200, - resnet_rs.ResNetRS270, - resnet_rs.ResNetRS350, - resnet_rs.ResNetRS420, - ], - ), - "regnet": ( - regnet, - [ - regnet.RegNetX002, - regnet.RegNetX004, - regnet.RegNetX006, - regnet.RegNetX008, - regnet.RegNetX016, - regnet.RegNetX032, - regnet.RegNetX040, - regnet.RegNetX064, - regnet.RegNetX080, - regnet.RegNetX120, - regnet.RegNetX160, - regnet.RegNetX320, - regnet.RegNetY002, - regnet.RegNetY004, - regnet.RegNetY006, - regnet.RegNetY008, - regnet.RegNetY016, - regnet.RegNetY032, - regnet.RegNetY040, - regnet.RegNetY064, - regnet.RegNetY080, - regnet.RegNetY120, - regnet.RegNetY160, - regnet.RegNetY320, - ], - ), -} - -TEST_IMAGE_PATH = "https://storage.googleapis.com/tensorflow/" "keras-applications/tests/elephant.jpg" -_IMAGENET_CLASSES = 1000 - -# Add a flag to define which application module file is tested. -# This is set as an 'arg' in the build target to guarantee that -# it only triggers the tests of the application models in the module -# if that module file has been modified. -FLAGS = flags.FLAGS -flags.DEFINE_string("module", None, "Application module used in this test.") - - -def _get_elephant(target_size): - # For models that don't include a Flatten step, - # the default is to accept variable-size inputs - # even when loading ImageNet weights (since it is possible). - # In this case, default to 299x299. - if target_size[0] is None: - target_size = (299, 299) - test_image = data_utils.get_file("elephant.jpg", TEST_IMAGE_PATH) - img = image_utils.load_img(test_image, target_size=tuple(target_size)) - x = image_utils.img_to_array(img) - return np.expand_dims(x, axis=0) - - -class ApplicationsLoadWeightTest(tf.test.TestCase, parameterized.TestCase): - def assertShapeEqual(self, shape1, shape2): - if len(shape1) != len(shape2): - raise AssertionError(f"Shapes are different rank: {shape1} vs {shape2}") - if shape1 != shape2: - raise AssertionError(f"Shapes differ: {shape1} vs {shape2}") - - def test_application_pretrained_weights_loading(self): - app_module = ARG_TO_MODEL[FLAGS.module][0] - apps = ARG_TO_MODEL[FLAGS.module][1] - for app in apps: - try: - model = app(weights="imagenet") - except Exception: - self.skipTest("TODO(b/227700184): Re-enable.") - self.assertShapeEqual(model.output_shape, (None, _IMAGENET_CLASSES)) - x = _get_elephant(model.input_shape[1:3]) - x = app_module.preprocess_input(x) - preds = model.predict(x) - names = [p[1] for p in app_module.decode_predictions(preds)[0]] - # Test correct label is in top 3 (weak correctness test). - self.assertIn("African_elephant", names[:3]) - - -if __name__ == "__main__": - tf.test.main() diff --git a/Archive/keras_applications_mod/models/applications_test.py b/Archive/keras_applications_mod/models/applications_test.py deleted file mode 100644 index 95a2339..0000000 --- a/Archive/keras_applications_mod/models/applications_test.py +++ /dev/null @@ -1,243 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Integration tests for Keras applications.""" - -import os - -import tensorflow.compat.v2 as tf -from absl.testing import parameterized - -from keras import backend -from keras import utils -from keras.applications import convnext -from keras.applications import densenet -from keras.applications import efficientnet -from keras.applications import efficientnet_v2 -from keras.applications import inception_resnet_v2 -from keras.applications import inception_v3 -from keras.applications import mobilenet -from keras.applications import mobilenet_v2 -from keras.applications import mobilenet_v3 -from keras.applications import nasnet -from keras.applications import regnet -from keras.applications import resnet -from keras.applications import resnet_rs -from keras.applications import resnet_v2 -from keras.applications import vgg16 -from keras.applications import vgg19 -from keras.applications import xception -from keras.testing_infra import test_utils - -MODEL_LIST_NO_NASNET = [ - (resnet.ResNet50, 2048), - (resnet.ResNet101, 2048), - (resnet.ResNet152, 2048), - (resnet_v2.ResNet50V2, 2048), - (resnet_v2.ResNet101V2, 2048), - (resnet_v2.ResNet152V2, 2048), - (vgg16.VGG16, 512), - (vgg19.VGG19, 512), - (xception.Xception, 2048), - (inception_v3.InceptionV3, 2048), - (inception_resnet_v2.InceptionResNetV2, 1536), - (mobilenet.MobileNet, 1024), - (mobilenet_v2.MobileNetV2, 1280), - (mobilenet_v3.MobileNetV3Small, 576), - (mobilenet_v3.MobileNetV3Large, 960), - (convnext.ConvNeXtTiny, 768), - (convnext.ConvNeXtSmall, 768), - (convnext.ConvNeXtBase, 1024), - (convnext.ConvNeXtLarge, 1536), - (convnext.ConvNeXtXLarge, 2048), - (densenet.DenseNet121, 1024), - (densenet.DenseNet169, 1664), - (densenet.DenseNet201, 1920), - (efficientnet.EfficientNetB0, 1280), - (efficientnet.EfficientNetB1, 1280), - (efficientnet.EfficientNetB2, 1408), - (efficientnet.EfficientNetB3, 1536), - (efficientnet.EfficientNetB4, 1792), - (efficientnet.EfficientNetB5, 2048), - (efficientnet.EfficientNetB6, 2304), - (efficientnet.EfficientNetB7, 2560), - (efficientnet_v2.EfficientNetV2B0, 1280), - (efficientnet_v2.EfficientNetV2B1, 1280), - (efficientnet_v2.EfficientNetV2B2, 1408), - (efficientnet_v2.EfficientNetV2B3, 1536), - (efficientnet_v2.EfficientNetV2S, 1280), - (efficientnet_v2.EfficientNetV2M, 1280), - (efficientnet_v2.EfficientNetV2L, 1280), - (regnet.RegNetX002, 368), - (regnet.RegNetX004, 384), - (regnet.RegNetX006, 528), - (regnet.RegNetX008, 672), - (regnet.RegNetX016, 912), - (regnet.RegNetX032, 1008), - (regnet.RegNetX040, 1360), - (regnet.RegNetX064, 1624), - (regnet.RegNetX080, 1920), - (regnet.RegNetX120, 2240), - (regnet.RegNetX160, 2048), - (regnet.RegNetX320, 2520), - (regnet.RegNetY002, 368), - (regnet.RegNetY004, 440), - (regnet.RegNetY006, 608), - (regnet.RegNetY008, 768), - (regnet.RegNetY016, 888), - (regnet.RegNetY032, 1512), - (regnet.RegNetY040, 1088), - (regnet.RegNetY064, 1296), - (regnet.RegNetY080, 2016), - (regnet.RegNetY120, 2240), - (regnet.RegNetY160, 3024), - (regnet.RegNetY320, 3712), - (resnet_rs.ResNetRS50, 2048), - (resnet_rs.ResNetRS101, 2048), - (resnet_rs.ResNetRS152, 2048), - (resnet_rs.ResNetRS200, 2048), - (resnet_rs.ResNetRS270, 2048), - (resnet_rs.ResNetRS350, 2048), - (resnet_rs.ResNetRS420, 2048), -] - -NASNET_LIST = [ - (nasnet.NASNetMobile, 1056), - (nasnet.NASNetLarge, 4032), -] - -MODEL_LIST = MODEL_LIST_NO_NASNET + NASNET_LIST - -# Parameters for loading weights for MobileNetV3. -# (class, alpha, minimalistic, include_top) -MOBILENET_V3_FOR_WEIGHTS = [ - (mobilenet_v3.MobileNetV3Large, 0.75, False, False), - (mobilenet_v3.MobileNetV3Large, 1.0, False, False), - (mobilenet_v3.MobileNetV3Large, 1.0, True, False), - (mobilenet_v3.MobileNetV3Large, 0.75, False, True), - (mobilenet_v3.MobileNetV3Large, 1.0, False, True), - (mobilenet_v3.MobileNetV3Large, 1.0, True, True), - (mobilenet_v3.MobileNetV3Small, 0.75, False, False), - (mobilenet_v3.MobileNetV3Small, 1.0, False, False), - (mobilenet_v3.MobileNetV3Small, 1.0, True, False), - (mobilenet_v3.MobileNetV3Small, 0.75, False, True), - (mobilenet_v3.MobileNetV3Small, 1.0, False, True), - (mobilenet_v3.MobileNetV3Small, 1.0, True, True), -] - - -class ApplicationsTest(tf.test.TestCase, parameterized.TestCase): - def assertShapeEqual(self, shape1, shape2): - if len(shape1) != len(shape2): - raise AssertionError(f"Shapes are different rank: {shape1} vs {shape2}") - for v1, v2 in zip(shape1, shape2): - if v1 != v2: - raise AssertionError(f"Shapes differ: {shape1} vs {shape2}") - - @parameterized.parameters(*MODEL_LIST) - def test_application_base(self, app, _): - # Can be instantiated with default arguments - model = app(weights=None) - # Can be serialized and deserialized - config = model.get_config() - if "ConvNeXt" in app.__name__: - custom_objects = {"LayerScale": convnext.LayerScale} - with utils.custom_object_scope(custom_objects): - reconstructed_model = model.__class__.from_config(config) - else: - reconstructed_model = model.__class__.from_config(config) - self.assertEqual(len(model.weights), len(reconstructed_model.weights)) - backend.clear_session() - - @parameterized.parameters(*MODEL_LIST) - def test_application_notop(self, app, last_dim): - if "NASNet" in app.__name__: - only_check_last_dim = True - else: - only_check_last_dim = False - output_shape = _get_output_shape(lambda: app(weights=None, include_top=False)) - if only_check_last_dim: - self.assertEqual(output_shape[-1], last_dim) - else: - self.assertShapeEqual(output_shape, (None, None, None, last_dim)) - backend.clear_session() - - @parameterized.parameters(*MODEL_LIST) - def test_application_notop_custom_input_shape(self, app, last_dim): - output_shape = _get_output_shape(lambda: app(weights="imagenet", include_top=False, input_shape=(224, 224, 3))) - - self.assertEqual(output_shape[-1], last_dim) - - @parameterized.parameters(MODEL_LIST) - def test_application_pooling(self, app, last_dim): - output_shape = _get_output_shape(lambda: app(weights=None, include_top=False, pooling="avg")) - self.assertShapeEqual(output_shape, (None, last_dim)) - - @parameterized.parameters(MODEL_LIST) - def test_application_classifier_activation(self, app, _): - if "RegNet" in app.__name__: - self.skipTest("RegNet models do not support classifier activation") - model = app(weights=None, include_top=True, classifier_activation="softmax") - last_layer_act = model.layers[-1].activation.__name__ - self.assertEqual(last_layer_act, "softmax") - - @parameterized.parameters(*MODEL_LIST_NO_NASNET) - def test_application_variable_input_channels(self, app, last_dim): - if backend.image_data_format() == "channels_first": - input_shape = (1, None, None) - else: - input_shape = (None, None, 1) - output_shape = _get_output_shape(lambda: app(weights=None, include_top=False, input_shape=input_shape)) - self.assertShapeEqual(output_shape, (None, None, None, last_dim)) - backend.clear_session() - - if backend.image_data_format() == "channels_first": - input_shape = (4, None, None) - else: - input_shape = (None, None, 4) - output_shape = _get_output_shape(lambda: app(weights=None, include_top=False, input_shape=input_shape)) - self.assertShapeEqual(output_shape, (None, None, None, last_dim)) - backend.clear_session() - - @parameterized.parameters(*MOBILENET_V3_FOR_WEIGHTS) - def test_mobilenet_v3_load_weights(self, mobilenet_class, alpha, minimalistic, include_top): - mobilenet_class( - input_shape=(224, 224, 3), - weights="imagenet", - alpha=alpha, - minimalistic=minimalistic, - include_top=include_top, - ) - - @parameterized.parameters(MODEL_LIST) - @test_utils.run_v2_only - def test_model_checkpoint(self, app, _): - model = app(weights=None) - - checkpoint = tf.train.Checkpoint(model=model) - checkpoint_manager = tf.train.CheckpointManager( - checkpoint, - directory=os.path.join(self.get_temp_dir(), model.name), - max_to_keep=1, - ) - checkpoint_manager.save(checkpoint_number=1) - - -def _get_output_shape(model_fn): - model = model_fn() - return model.output_shape - - -if __name__ == "__main__": - tf.test.main() diff --git a/Archive/keras_applications_mod/models/convnext.py b/Archive/keras_applications_mod/models/convnext.py deleted file mode 100644 index e0d3814..0000000 --- a/Archive/keras_applications_mod/models/convnext.py +++ /dev/null @@ -1,755 +0,0 @@ -# Copyright 2022 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - - -"""ConvNeXt models for Keras. - -References: - -- [A ConvNet for the 2020s](https://arxiv.org/abs/2201.03545) - (CVPR 2022) -""" - -import numpy as np -import tensorflow.compat.v2 as tf - -from keras import backend -from keras import initializers -from keras import layers -from keras import utils -from keras.applications import imagenet_utils -from keras.engine import sequential -from keras.engine import training as training_lib - -# isort: off -from tensorflow.python.util.tf_export import keras_export - -BASE_WEIGHTS_PATH = "https://storage.googleapis.com/tensorflow/keras-applications/convnext/" - -WEIGHTS_HASHES = { - "convnext_tiny": ( - "8ae6e78ce2933352b1ef4008e6dd2f17bc40771563877d156bc6426c7cf503ff", - "d547c096cabd03329d7be5562c5e14798aa39ed24b474157cef5e85ab9e49ef1", - ), - "convnext_small": ( - "ce1277d8f1ee5a0ef0e171469089c18f5233860ceaf9b168049cb9263fd7483c", - "6fc8009faa2f00c1c1dfce59feea9b0745eb260a7dd11bee65c8e20843da6eab", - ), - "convnext_base": ( - "52cbb006d3dadd03f6e095a8ca1aca47aecdd75acb4bc74bce1f5c695d0086e6", - "40a20c5548a5e9202f69735ecc06c990e6b7c9d2de39f0361e27baeb24cb7c45", - ), - "convnext_large": ( - "070c5ed9ed289581e477741d3b34beffa920db8cf590899d6d2c67fba2a198a6", - "96f02b6f0753d4f543261bc9d09bed650f24dd6bc02ddde3066135b63d23a1cd", - ), - "convnext_xlarge": ( - "c1f5ccab661354fc3a79a10fa99af82f0fbf10ec65cb894a3ae0815f17a889ee", - "de3f8a54174130e0cecdc71583354753d557fcf1f4487331558e2a16ba0cfe05", - ), -} - - -MODEL_CONFIGS = { - "tiny": { - "depths": [3, 3, 9, 3], - "projection_dims": [96, 192, 384, 768], - "default_size": 224, - }, - "small": { - "depths": [3, 3, 27, 3], - "projection_dims": [96, 192, 384, 768], - "default_size": 224, - }, - "base": { - "depths": [3, 3, 27, 3], - "projection_dims": [128, 256, 512, 1024], - "default_size": 224, - }, - "large": { - "depths": [3, 3, 27, 3], - "projection_dims": [192, 384, 768, 1536], - "default_size": 224, - }, - "xlarge": { - "depths": [3, 3, 27, 3], - "projection_dims": [256, 512, 1024, 2048], - "default_size": 224, - }, -} - -BASE_DOCSTRING = """Instantiates the {name} architecture. - - References: - - [A ConvNet for the 2020s](https://arxiv.org/abs/2201.03545) - (CVPR 2022) - - For image classification use cases, see - [this page for detailed examples]( - https://keras.io/api/applications/#usage-examples-for-image-classification-models). - For transfer learning use cases, make sure to read the - [guide to transfer learning & fine-tuning]( - https://keras.io/guides/transfer_learning/). - - The `base`, `large`, and `xlarge` models were first pre-trained on the - ImageNet-21k dataset and then fine-tuned on the ImageNet-1k dataset. The - pre-trained parameters of the models were assembled from the - [official repository](https://github.com/facebookresearch/ConvNeXt). To get a - sense of how these parameters were converted to Keras compatible parameters, - please refer to - [this repository](https://github.com/sayakpaul/keras-convnext-conversion). - - Note: Each Keras Application expects a specific kind of input preprocessing. - For ConvNeXt, preprocessing is included in the model using a `Normalization` - layer. ConvNeXt models expect their inputs to be float or uint8 tensors of - pixels with values in the [0-255] range. - - When calling the `summary()` method after instantiating a ConvNeXt model, - prefer setting the `expand_nested` argument `summary()` to `True` to better - investigate the instantiated model. - - Args: - include_top: Whether to include the fully-connected - layer at the top of the network. Defaults to `True`. - weights: One of `None` (random initialization), - `"imagenet"` (pre-training on ImageNet-1k), or the path to the weights - file to be loaded. Defaults to `"imagenet"`. - input_tensor: Optional Keras tensor - (i.e. output of `layers.Input()`) - to use as image input for the model. - input_shape: Optional shape tuple, only to be specified - if `include_top` is False. - It should have exactly 3 inputs channels. - pooling: Optional pooling mode for feature extraction - when `include_top` is `False`. - - `None` means that the output of the model will be - the 4D tensor output of the last convolutional layer. - - `avg` means that global average pooling - will be applied to the output of the - last convolutional layer, and thus - the output of the model will be a 2D tensor. - - `max` means that global max pooling will - be applied. - Defaults to `None`. - classes: Optional number of classes to classify images - into, only to be specified if `include_top` is True, and - if no `weights` argument is specified. 1000 is how many - ImageNet classes there are. Defaults to `1000`. - classifier_activation: A `str` or callable. The activation function to use - on the "top" layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" layer. - When loading pretrained weights, `classifier_activation` can only - be `None` or `"softmax"`. Defaults to `"softmax"`. - - Returns: - A `keras.Model` instance. -""" - - -class StochasticDepth(layers.Layer): - """Stochastic Depth module. - - It performs batch-wise dropping rather than sample-wise. In libraries like - `timm`, it's similar to `DropPath` layers that drops residual paths - sample-wise. - - References: - - https://github.com/rwightman/pytorch-image-models - - Args: - drop_path_rate (float): Probability of dropping paths. Should be within - [0, 1]. - - Returns: - Tensor either with the residual path dropped or kept. - """ - - def __init__(self, drop_path_rate, **kwargs): - super().__init__(**kwargs) - self.drop_path_rate = drop_path_rate - - def call(self, x, training=None): - if training: - keep_prob = 1 - self.drop_path_rate - shape = (tf.shape(x)[0],) + (1,) * (len(tf.shape(x)) - 1) - random_tensor = keep_prob + tf.random.uniform(shape, 0, 1) - random_tensor = tf.floor(random_tensor) - return (x / keep_prob) * random_tensor - return x - - def get_config(self): - config = super().get_config() - config.update({"drop_path_rate": self.drop_path_rate}) - return config - - -class LayerScale(layers.Layer): - """Layer scale module. - - References: - - https://arxiv.org/abs/2103.17239 - - Args: - init_values (float): Initial value for layer scale. Should be within - [0, 1]. - projection_dim (int): Projection dimensionality. - - Returns: - Tensor multiplied to the scale. - """ - - def __init__(self, init_values, projection_dim, **kwargs): - super().__init__(**kwargs) - self.init_values = init_values - self.projection_dim = projection_dim - - def build(self, input_shape): - self.gamma = self.add_weight( - name="gamma", - shape=(self.projection_dim,), - initializer=initializers.Constant(self.init_values), - trainable=True, - ) - - def call(self, x): - return x * self.gamma - - def get_config(self): - config = super().get_config() - config.update({ - "init_values": self.init_values, - "projection_dim": self.projection_dim, - }) - return config - - -def ConvNeXtBlock(projection_dim, drop_path_rate=0.0, layer_scale_init_value=1e-6, name=None): - """ConvNeXt block. - - References: - - https://arxiv.org/abs/2201.03545 - - https://github.com/facebookresearch/ConvNeXt/blob/main/models/convnext.py - - Notes: - In the original ConvNeXt implementation (linked above), the authors use - `Dense` layers for pointwise convolutions for increased efficiency. - Following that, this implementation also uses the same. - - Args: - projection_dim (int): Number of filters for convolution layers. In the - ConvNeXt paper, this is referred to as projection dimension. - drop_path_rate (float): Probability of dropping paths. Should be within - [0, 1]. - layer_scale_init_value (float): Layer scale value. Should be a small float - number. - name: name to path to the keras layer. - - Returns: - A function representing a ConvNeXtBlock block. - """ - if name is None: - name = "prestem" + str(backend.get_uid("prestem")) - - def apply(inputs): - x = inputs - - x = layers.Conv2D( - filters=projection_dim, - kernel_size=7, - padding="same", - groups=projection_dim, - name=name + "_depthwise_conv", - )(x) - x = layers.LayerNormalization(epsilon=1e-6, name=name + "_layernorm")(x) - x = layers.Dense(4 * projection_dim, name=name + "_pointwise_conv_1")(x) - x = layers.Activation("gelu", name=name + "_gelu")(x) - x = layers.Dense(projection_dim, name=name + "_pointwise_conv_2")(x) - - if layer_scale_init_value is not None: - x = LayerScale( - layer_scale_init_value, - projection_dim, - name=name + "_layer_scale", - )(x) - if drop_path_rate: - layer = StochasticDepth(drop_path_rate, name=name + "_stochastic_depth") - else: - layer = layers.Activation("linear", name=name + "_identity") - - return inputs + layer(x) - - return apply - - -def PreStem(name=None): - """Normalizes inputs with ImageNet-1k mean and std. - - Args: - name (str): Name prefix. - - Returns: - A presemt function. - """ - if name is None: - name = "prestem" + str(backend.get_uid("prestem")) - - def apply(x): - x = layers.Normalization( - mean=[0.485 * 255, 0.456 * 255, 0.406 * 255], - variance=[ - (0.229 * 255) ** 2, - (0.224 * 255) ** 2, - (0.225 * 255) ** 2, - ], - name=name + "_prestem_normalization", - )(x) - return x - - return apply - - -def Head(num_classes=1000, classifier_activation=None, name=None): - """Implementation of classification head of ConvNeXt. - - Args: - num_classes: number of classes for Dense layer - classifier_activation: activation function for the Dense layer - name: name prefix - - Returns: - Classification head function. - """ - if name is None: - name = str(backend.get_uid("head")) - - def apply(x): - x = layers.GlobalAveragePooling2D(name=name + "_head_gap")(x) - x = layers.LayerNormalization(epsilon=1e-6, name=name + "_head_layernorm")(x) - x = layers.Dense( - num_classes, - activation=classifier_activation, - name=name + "_head_dense", - )(x) - return x - - return apply - - -def ConvNeXt( - depths, - projection_dims, - drop_path_rate=0.0, - layer_scale_init_value=1e-6, - default_size=224, - model_name="convnext", - include_preprocessing=True, - include_top=True, - weights=None, - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - """Instantiates ConvNeXt architecture given specific configuration. - - Args: - depths: An iterable containing depths for each individual stages. - projection_dims: An iterable containing output number of channels of - each individual stages. - drop_path_rate: Stochastic depth probability. If 0.0, then stochastic - depth won't be used. - layer_scale_init_value: Layer scale coefficient. If 0.0, layer scaling - won't be used. - default_size: Default input image size. - model_name: An optional name for the model. - include_preprocessing: boolean denoting whther to include preprocessing in - the model. When `weights="imagenet"` this should be always set to True. - But for other models (e.g., randomly initialized) users should set it - to False and apply preprocessing to data accordingly. - include_top: Boolean denoting whether to include classification head to - the model. - weights: one of `None` (random initialization), `"imagenet"` (pre-training - on ImageNet-1k), or the path to the weights file to be loaded. - input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to - use as image input for the model. - input_shape: optional shape tuple, only to be specified if `include_top` - is False. It should have exactly 3 inputs channels. - pooling: optional pooling mode for feature extraction when `include_top` - is `False`. - - `None` means that the output of the model will be the 4D tensor output - of the last convolutional layer. - - `avg` means that global average pooling will be applied to the output - of the last convolutional layer, and thus the output of the model will - be a 2D tensor. - - `max` means that global max pooling will be applied. - classes: optional number of classes to classify images into, only to be - specified if `include_top` is True, and if no `weights` argument is - specified. - classifier_activation: A `str` or callable. The activation function to use - on the "top" layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" layer. - - Returns: - A `keras.Model` instance. - - Raises: - ValueError: in case of invalid argument for `weights`, - or invalid input shape. - ValueError: if `classifier_activation` is not `softmax`, or `None` - when using a pretrained top layer. - ValueError: if `include_top` is True but `num_classes` is not 1000 - when using ImageNet. - """ - if not (weights in {"imagenet", None} or tf.io.gfile.exists(weights)): - raise ValueError( - "The `weights` argument should be either " - "`None` (random initialization), `imagenet` " - "(pre-training on ImageNet), " - "or the path to the weights file to be loaded." - ) - - if weights == "imagenet" and include_top and classes != 1000: - raise ValueError("If using `weights` as `'imagenet'` with `include_top`" " as true, `classes` should be 1000") - - # Determine proper input shape. - input_shape = imagenet_utils.obtain_input_shape( - input_shape, - default_size=default_size, - min_size=32, - data_format=backend.image_data_format(), - require_flatten=include_top, - weights=weights, - ) - - if input_tensor is None: - img_input = layers.Input(shape=input_shape) - else: - if not backend.is_keras_tensor(input_tensor): - img_input = layers.Input(tensor=input_tensor, shape=input_shape) - else: - img_input = input_tensor - - if input_tensor is not None: - inputs = utils.layer_utils.get_source_inputs(input_tensor)[0] - else: - inputs = img_input - - x = inputs - if include_preprocessing: - channel_axis = 3 if backend.image_data_format() == "channels_last" else 1 - num_channels = input_shape[channel_axis - 1] - if num_channels == 3: - x = PreStem(name=model_name)(x) - - # Stem block. - stem = sequential.Sequential( - [ - layers.Conv2D( - projection_dims[0], - kernel_size=4, - strides=4, - name=model_name + "_stem_conv", - ), - layers.LayerNormalization(epsilon=1e-6, name=model_name + "_stem_layernorm"), - ], - name=model_name + "_stem", - ) - - # Downsampling blocks. - downsample_layers = [] - downsample_layers.append(stem) - - num_downsample_layers = 3 - for i in range(num_downsample_layers): - downsample_layer = sequential.Sequential( - [ - layers.LayerNormalization( - epsilon=1e-6, - name=model_name + "_downsampling_layernorm_" + str(i), - ), - layers.Conv2D( - projection_dims[i + 1], - kernel_size=2, - strides=2, - name=model_name + "_downsampling_conv_" + str(i), - ), - ], - name=model_name + "_downsampling_block_" + str(i), - ) - downsample_layers.append(downsample_layer) - - # Stochastic depth schedule. - # This is referred from the original ConvNeXt codebase: - # https://github.com/facebookresearch/ConvNeXt/blob/main/models/convnext.py#L86 - depth_drop_rates = [float(x) for x in np.linspace(0.0, drop_path_rate, sum(depths))] - - # First apply downsampling blocks and then apply ConvNeXt stages. - cur = 0 - - num_convnext_blocks = 4 - for i in range(num_convnext_blocks): - x = downsample_layers[i](x) - for j in range(depths[i]): - x = ConvNeXtBlock( - projection_dim=projection_dims[i], - drop_path_rate=depth_drop_rates[cur + j], - layer_scale_init_value=layer_scale_init_value, - name=model_name + f"_stage_{i}_block_{j}", - )(x) - cur += depths[i] - - if include_top: - imagenet_utils.validate_activation(classifier_activation, weights) - x = Head( - num_classes=classes, - classifier_activation=classifier_activation, - name=model_name, - )(x) - - else: - if pooling == "avg": - x = layers.GlobalAveragePooling2D()(x) - elif pooling == "max": - x = layers.GlobalMaxPooling2D()(x) - x = layers.LayerNormalization(epsilon=1e-6)(x) - - model = training_lib.Model(inputs=inputs, outputs=x, name=model_name) - - # Load weights. - if weights == "imagenet": - if include_top: - file_suffix = ".h5" - file_hash = WEIGHTS_HASHES[model_name][0] - else: - file_suffix = "_notop.h5" - file_hash = WEIGHTS_HASHES[model_name][1] - file_name = model_name + file_suffix - weights_path = utils.data_utils.get_file( - file_name, - BASE_WEIGHTS_PATH + file_name, - cache_subdir="models", - file_hash=file_hash, - ) - model.load_weights(weights_path) - elif weights is not None: - model.load_weights(weights) - - return model - - -## Instantiating variants ## - - -@keras_export( - "keras.applications.convnext.ConvNeXtTiny", - "keras.applications.ConvNeXtTiny", -) -def ConvNeXtTiny( - model_name="convnext_tiny", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return ConvNeXt( - depths=MODEL_CONFIGS["tiny"]["depths"], - projection_dims=MODEL_CONFIGS["tiny"]["projection_dims"], - drop_path_rate=0.0, - layer_scale_init_value=1e-6, - default_size=MODEL_CONFIGS["tiny"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export( - "keras.applications.convnext.ConvNeXtSmall", - "keras.applications.ConvNeXtSmall", -) -def ConvNeXtSmall( - model_name="convnext_small", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return ConvNeXt( - depths=MODEL_CONFIGS["small"]["depths"], - projection_dims=MODEL_CONFIGS["small"]["projection_dims"], - drop_path_rate=0.0, - layer_scale_init_value=1e-6, - default_size=MODEL_CONFIGS["small"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export( - "keras.applications.convnext.ConvNeXtBase", - "keras.applications.ConvNeXtBase", -) -def ConvNeXtBase( - model_name="convnext_base", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return ConvNeXt( - depths=MODEL_CONFIGS["base"]["depths"], - projection_dims=MODEL_CONFIGS["base"]["projection_dims"], - drop_path_rate=0.0, - layer_scale_init_value=1e-6, - default_size=MODEL_CONFIGS["base"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export( - "keras.applications.convnext.ConvNeXtLarge", - "keras.applications.ConvNeXtLarge", -) -def ConvNeXtLarge( - model_name="convnext_large", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return ConvNeXt( - depths=MODEL_CONFIGS["large"]["depths"], - projection_dims=MODEL_CONFIGS["large"]["projection_dims"], - drop_path_rate=0.0, - layer_scale_init_value=1e-6, - default_size=MODEL_CONFIGS["large"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export( - "keras.applications.convnext.ConvNeXtXLarge", - "keras.applications.ConvNeXtXLarge", -) -def ConvNeXtXLarge( - model_name="convnext_xlarge", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return ConvNeXt( - depths=MODEL_CONFIGS["xlarge"]["depths"], - projection_dims=MODEL_CONFIGS["xlarge"]["projection_dims"], - drop_path_rate=0.0, - layer_scale_init_value=1e-6, - default_size=MODEL_CONFIGS["xlarge"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -ConvNeXtTiny.__doc__ = BASE_DOCSTRING.format(name="ConvNeXtTiny") -ConvNeXtSmall.__doc__ = BASE_DOCSTRING.format(name="ConvNeXtSmall") -ConvNeXtBase.__doc__ = BASE_DOCSTRING.format(name="ConvNeXtBase") -ConvNeXtLarge.__doc__ = BASE_DOCSTRING.format(name="ConvNeXtLarge") -ConvNeXtXLarge.__doc__ = BASE_DOCSTRING.format(name="ConvNeXtXLarge") - - -@keras_export("keras.applications.convnext.preprocess_input") -def preprocess_input(x, data_format=None): - """A placeholder method for backward compatibility. - - The preprocessing logic has been included in the convnext model - implementation. Users are no longer required to call this method to - normalize the input data. This method does nothing and only kept as a - placeholder to align the API surface between old and new version of model. - - Args: - x: A floating point `numpy.array` or a `tf.Tensor`. - data_format: Optional data format of the image tensor/array. `None` means - the global setting `tf.keras.backend.image_data_format()` is used - (unless you changed it, it uses "channels_last"). - Defaults to `None`. - - Returns: - Unchanged `numpy.array` or `tf.Tensor`. - """ - return x - - -@keras_export("keras.applications.convnext.decode_predictions") -def decode_predictions(preds, top=5): - return imagenet_utils.decode_predictions(preds, top=top) - - -decode_predictions.__doc__ = imagenet_utils.decode_predictions.__doc__ diff --git a/Archive/keras_applications_mod/models/densenet.py b/Archive/keras_applications_mod/models/densenet.py deleted file mode 100644 index fc84a12..0000000 --- a/Archive/keras_applications_mod/models/densenet.py +++ /dev/null @@ -1,452 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""DenseNet models for Keras. - -Reference: - - [Densely Connected Convolutional Networks]( - https://arxiv.org/abs/1608.06993) (CVPR 2017) -""" - -import tensorflow.compat.v2 as tf - -from keras import backend -from keras.applications import imagenet_utils -from keras.engine import training -from keras.layers import VersionAwareLayers -from keras.utils import data_utils -from keras.utils import layer_utils - -# isort: off -from tensorflow.python.util.tf_export import keras_export - -BASE_WEIGHTS_PATH = "https://storage.googleapis.com/tensorflow/keras-applications/densenet/" -DENSENET121_WEIGHT_PATH = BASE_WEIGHTS_PATH + "densenet121_weights_tf_dim_ordering_tf_kernels.h5" -DENSENET121_WEIGHT_PATH_NO_TOP = BASE_WEIGHTS_PATH + "densenet121_weights_tf_dim_ordering_tf_kernels_notop.h5" -DENSENET169_WEIGHT_PATH = BASE_WEIGHTS_PATH + "densenet169_weights_tf_dim_ordering_tf_kernels.h5" -DENSENET169_WEIGHT_PATH_NO_TOP = BASE_WEIGHTS_PATH + "densenet169_weights_tf_dim_ordering_tf_kernels_notop.h5" -DENSENET201_WEIGHT_PATH = BASE_WEIGHTS_PATH + "densenet201_weights_tf_dim_ordering_tf_kernels.h5" -DENSENET201_WEIGHT_PATH_NO_TOP = BASE_WEIGHTS_PATH + "densenet201_weights_tf_dim_ordering_tf_kernels_notop.h5" - -layers = VersionAwareLayers() - - -def dense_block(x, blocks, name): - """A dense block. - - Args: - x: input tensor. - blocks: integer, the number of building blocks. - name: string, block label. - - Returns: - Output tensor for the block. - """ - for i in range(blocks): - x = conv_block(x, 32, name=name + "_block" + str(i + 1)) - return x - - -def transition_block(x, reduction, name): - """A transition block. - - Args: - x: input tensor. - reduction: float, compression rate at transition layers. - name: string, block label. - - Returns: - output tensor for the block. - """ - bn_axis = 3 if backend.image_data_format() == "channels_last" else 1 - x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + "_bn")(x) - x = layers.Activation("relu", name=name + "_relu")(x) - x = layers.Conv2D( - int(backend.int_shape(x)[bn_axis] * reduction), - 1, - use_bias=False, - name=name + "_conv", - )(x) - x = layers.AveragePooling2D(2, strides=2, name=name + "_pool")(x) - return x - - -def conv_block(x, growth_rate, name): - """A building block for a dense block. - - Args: - x: input tensor. - growth_rate: float, growth rate at dense layers. - name: string, block label. - - Returns: - Output tensor for the block. - """ - bn_axis = 3 if backend.image_data_format() == "channels_last" else 1 - x1 = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + "_0_bn")(x) - x1 = layers.Activation("relu", name=name + "_0_relu")(x1) - x1 = layers.Conv2D(4 * growth_rate, 1, use_bias=False, name=name + "_1_conv")(x1) - x1 = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + "_1_bn")(x1) - x1 = layers.Activation("relu", name=name + "_1_relu")(x1) - x1 = layers.Conv2D(growth_rate, 3, padding="same", use_bias=False, name=name + "_2_conv")(x1) - x = layers.Concatenate(axis=bn_axis, name=name + "_concat")([x, x1]) - return x - - -def DenseNet( - blocks, - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - """Instantiates the DenseNet architecture. - - Reference: - - [Densely Connected Convolutional Networks]( - https://arxiv.org/abs/1608.06993) (CVPR 2017) - - This function returns a Keras image classification model, - optionally loaded with weights pre-trained on ImageNet. - - For image classification use cases, see - [this page for detailed examples]( - https://keras.io/api/applications/#usage-examples-for-image-classification-models). - - For transfer learning use cases, make sure to read the - [guide to transfer learning & fine-tuning]( - https://keras.io/guides/transfer_learning/). - - Note: each Keras Application expects a specific kind of input preprocessing. - For DenseNet, call `tf.keras.applications.densenet.preprocess_input` on your - inputs before passing them to the model. - `densenet.preprocess_input` will scale pixels between 0 and 1 and then - will normalize each channel with respect to the ImageNet dataset statistics. - - Args: - blocks: numbers of building blocks for the four dense layers. - include_top: whether to include the fully-connected - layer at the top of the network. - weights: one of `None` (random initialization), - 'imagenet' (pre-training on ImageNet), - or the path to the weights file to be loaded. - input_tensor: optional Keras tensor - (i.e. output of `layers.Input()`) - to use as image input for the model. - input_shape: optional shape tuple, only to be specified - if `include_top` is False (otherwise the input shape - has to be `(224, 224, 3)` (with `'channels_last'` data format) - or `(3, 224, 224)` (with `'channels_first'` data format). - It should have exactly 3 inputs channels, - and width and height should be no smaller than 32. - E.g. `(200, 200, 3)` would be one valid value. - pooling: optional pooling mode for feature extraction - when `include_top` is `False`. - - `None` means that the output of the model will be - the 4D tensor output of the - last convolutional block. - - `avg` means that global average pooling - will be applied to the output of the - last convolutional block, and thus - the output of the model will be a 2D tensor. - - `max` means that global max pooling will - be applied. - classes: optional number of classes to classify images - into, only to be specified if `include_top` is True, and - if no `weights` argument is specified. - classifier_activation: A `str` or callable. The activation function to use - on the "top" layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" layer. - When loading pretrained weights, `classifier_activation` can only - be `None` or `"softmax"`. - - Returns: - A `keras.Model` instance. - """ - if not (weights in {"imagenet", None} or tf.io.gfile.exists(weights)): - raise ValueError( - "The `weights` argument should be either " - "`None` (random initialization), `imagenet` " - "(pre-training on ImageNet), " - "or the path to the weights file to be loaded." - ) - - if weights == "imagenet" and include_top and classes != 1000: - raise ValueError('If using `weights` as `"imagenet"` with `include_top`' " as true, `classes` should be 1000") - - # Determine proper input shape - input_shape = imagenet_utils.obtain_input_shape( - input_shape, - default_size=224, - min_size=32, - data_format=backend.image_data_format(), - require_flatten=include_top, - weights=weights, - ) - - if input_tensor is None: - img_input = layers.Input(shape=input_shape) - else: - if not backend.is_keras_tensor(input_tensor): - img_input = layers.Input(tensor=input_tensor, shape=input_shape) - else: - img_input = input_tensor - - bn_axis = 3 if backend.image_data_format() == "channels_last" else 1 - - x = layers.ZeroPadding2D(padding=((3, 3), (3, 3)))(img_input) - x = layers.Conv2D(64, 7, strides=2, use_bias=False, name="conv1/conv")(x) - x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name="conv1/bn")(x) - x = layers.Activation("relu", name="conv1/relu")(x) - x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)))(x) - x = layers.MaxPooling2D(3, strides=2, name="pool1")(x) - - x = dense_block(x, blocks[0], name="conv2") - x = transition_block(x, 0.5, name="pool2") - x = dense_block(x, blocks[1], name="conv3") - x = transition_block(x, 0.5, name="pool3") - x = dense_block(x, blocks[2], name="conv4") - x = transition_block(x, 0.5, name="pool4") - x = dense_block(x, blocks[3], name="conv5") - - x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name="bn")(x) - x = layers.Activation("relu", name="relu")(x) - - if include_top: - x = layers.GlobalAveragePooling2D(name="avg_pool")(x) - - imagenet_utils.validate_activation(classifier_activation, weights) - x = layers.Dense(classes, activation=classifier_activation, name="predictions")(x) - else: - if pooling == "avg": - x = layers.GlobalAveragePooling2D(name="avg_pool")(x) - elif pooling == "max": - x = layers.GlobalMaxPooling2D(name="max_pool")(x) - - # Ensure that the model takes into account - # any potential predecessors of `input_tensor`. - if input_tensor is not None: - inputs = layer_utils.get_source_inputs(input_tensor) - else: - inputs = img_input - - # Create model. - if blocks == [6, 12, 24, 16]: - model = training.Model(inputs, x, name="densenet121") - elif blocks == [6, 12, 32, 32]: - model = training.Model(inputs, x, name="densenet169") - elif blocks == [6, 12, 48, 32]: - model = training.Model(inputs, x, name="densenet201") - else: - model = training.Model(inputs, x, name="densenet") - - # Load weights. - if weights == "imagenet": - if include_top: - if blocks == [6, 12, 24, 16]: - weights_path = data_utils.get_file( - "densenet121_weights_tf_dim_ordering_tf_kernels.h5", - DENSENET121_WEIGHT_PATH, - cache_subdir="models", - file_hash="9d60b8095a5708f2dcce2bca79d332c7", - ) - elif blocks == [6, 12, 32, 32]: - weights_path = data_utils.get_file( - "densenet169_weights_tf_dim_ordering_tf_kernels.h5", - DENSENET169_WEIGHT_PATH, - cache_subdir="models", - file_hash="d699b8f76981ab1b30698df4c175e90b", - ) - elif blocks == [6, 12, 48, 32]: - weights_path = data_utils.get_file( - "densenet201_weights_tf_dim_ordering_tf_kernels.h5", - DENSENET201_WEIGHT_PATH, - cache_subdir="models", - file_hash="1ceb130c1ea1b78c3bf6114dbdfd8807", - ) - else: - if blocks == [6, 12, 24, 16]: - weights_path = data_utils.get_file( - "densenet121_weights_tf_dim_ordering_tf_kernels_notop.h5", - DENSENET121_WEIGHT_PATH_NO_TOP, - cache_subdir="models", - file_hash="30ee3e1110167f948a6b9946edeeb738", - ) - elif blocks == [6, 12, 32, 32]: - weights_path = data_utils.get_file( - "densenet169_weights_tf_dim_ordering_tf_kernels_notop.h5", - DENSENET169_WEIGHT_PATH_NO_TOP, - cache_subdir="models", - file_hash="b8c4d4c20dd625c148057b9ff1c1176b", - ) - elif blocks == [6, 12, 48, 32]: - weights_path = data_utils.get_file( - "densenet201_weights_tf_dim_ordering_tf_kernels_notop.h5", - DENSENET201_WEIGHT_PATH_NO_TOP, - cache_subdir="models", - file_hash="c13680b51ded0fb44dff2d8f86ac8bb1", - ) - model.load_weights(weights_path) - elif weights is not None: - model.load_weights(weights) - - return model - - -@keras_export("keras.applications.densenet.DenseNet121", "keras.applications.DenseNet121") -def DenseNet121( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - """Instantiates the Densenet121 architecture.""" - return DenseNet( - [6, 12, 24, 16], - include_top, - weights, - input_tensor, - input_shape, - pooling, - classes, - classifier_activation, - ) - - -@keras_export("keras.applications.densenet.DenseNet169", "keras.applications.DenseNet169") -def DenseNet169( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - """Instantiates the Densenet169 architecture.""" - return DenseNet( - [6, 12, 32, 32], - include_top, - weights, - input_tensor, - input_shape, - pooling, - classes, - classifier_activation, - ) - - -@keras_export("keras.applications.densenet.DenseNet201", "keras.applications.DenseNet201") -def DenseNet201( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - """Instantiates the Densenet201 architecture.""" - return DenseNet( - [6, 12, 48, 32], - include_top, - weights, - input_tensor, - input_shape, - pooling, - classes, - classifier_activation, - ) - - -@keras_export("keras.applications.densenet.preprocess_input") -def preprocess_input(x, data_format=None): - return imagenet_utils.preprocess_input(x, data_format=data_format, mode="torch") - - -@keras_export("keras.applications.densenet.decode_predictions") -def decode_predictions(preds, top=5): - return imagenet_utils.decode_predictions(preds, top=top) - - -preprocess_input.__doc__ = imagenet_utils.PREPROCESS_INPUT_DOC.format( - mode="", - ret=imagenet_utils.PREPROCESS_INPUT_RET_DOC_TORCH, - error=imagenet_utils.PREPROCESS_INPUT_ERROR_DOC, -) -decode_predictions.__doc__ = imagenet_utils.decode_predictions.__doc__ - -DOC = """ - - Reference: - - [Densely Connected Convolutional Networks]( - https://arxiv.org/abs/1608.06993) (CVPR 2017) - - Optionally loads weights pre-trained on ImageNet. - Note that the data format convention used by the model is - the one specified in your Keras config at `~/.keras/keras.json`. - - Note: each Keras Application expects a specific kind of input preprocessing. - For DenseNet, call `tf.keras.applications.densenet.preprocess_input` on your - inputs before passing them to the model. - - Args: - include_top: whether to include the fully-connected - layer at the top of the network. - weights: one of `None` (random initialization), - 'imagenet' (pre-training on ImageNet), - or the path to the weights file to be loaded. - input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) - to use as image input for the model. - input_shape: optional shape tuple, only to be specified - if `include_top` is False (otherwise the input shape - has to be `(224, 224, 3)` (with `'channels_last'` data format) - or `(3, 224, 224)` (with `'channels_first'` data format). - It should have exactly 3 inputs channels, - and width and height should be no smaller than 32. - E.g. `(200, 200, 3)` would be one valid value. - pooling: Optional pooling mode for feature extraction - when `include_top` is `False`. - - `None` means that the output of the model will be - the 4D tensor output of the - last convolutional block. - - `avg` means that global average pooling - will be applied to the output of the - last convolutional block, and thus - the output of the model will be a 2D tensor. - - `max` means that global max pooling will - be applied. - classes: optional number of classes to classify images - into, only to be specified if `include_top` is True, and - if no `weights` argument is specified. - classifier_activation: A `str` or callable. The activation function to use - on the "top" layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" layer. - When loading pretrained weights, `classifier_activation` can only - be `None` or `"softmax"`. - - Returns: - A Keras model instance. -""" - -setattr(DenseNet121, "__doc__", DenseNet121.__doc__ + DOC) -setattr(DenseNet169, "__doc__", DenseNet169.__doc__ + DOC) -setattr(DenseNet201, "__doc__", DenseNet201.__doc__ + DOC) diff --git a/Archive/keras_applications_mod/models/efficientnet.py b/Archive/keras_applications_mod/models/efficientnet.py deleted file mode 100644 index 41b3138..0000000 --- a/Archive/keras_applications_mod/models/efficientnet.py +++ /dev/null @@ -1,888 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - - -"""EfficientNet models for Keras. - -Reference: - - [EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks]( - https://arxiv.org/abs/1905.11946) (ICML 2019) -""" - -import copy -import math - -import tensorflow.compat.v2 as tf - -from keras import backend -from keras.applications import imagenet_utils -from keras.engine import training -from keras.layers import VersionAwareLayers -from keras.utils import data_utils -from keras.utils import layer_utils - -# isort: off -from tensorflow.python.util.tf_export import keras_export - -BASE_WEIGHTS_PATH = "https://storage.googleapis.com/keras-applications/" - -WEIGHTS_HASHES = { - "b0": ( - "902e53a9f72be733fc0bcb005b3ebbac", - "50bc09e76180e00e4465e1a485ddc09d", - ), - "b1": ( - "1d254153d4ab51201f1646940f018540", - "74c4e6b3e1f6a1eea24c589628592432", - ), - "b2": ( - "b15cce36ff4dcbd00b6dd88e7857a6ad", - "111f8e2ac8aa800a7a99e3239f7bfb39", - ), - "b3": ( - "ffd1fdc53d0ce67064dc6a9c7960ede0", - "af6d107764bb5b1abb91932881670226", - ), - "b4": ( - "18c95ad55216b8f92d7e70b3a046e2fc", - "ebc24e6d6c33eaebbd558eafbeedf1ba", - ), - "b5": ( - "ace28f2a6363774853a83a0b21b9421a", - "38879255a25d3c92d5e44e04ae6cec6f", - ), - "b6": ( - "165f6e37dce68623721b423839de8be5", - "9ecce42647a20130c1f39a5d4cb75743", - ), - "b7": ( - "8c03f828fec3ef71311cd463b6759d99", - "cbcfe4450ddf6f3ad90b1b398090fe4a", - ), -} - -DEFAULT_BLOCKS_ARGS = [ - { - "kernel_size": 3, - "repeats": 1, - "filters_in": 32, - "filters_out": 16, - "expand_ratio": 1, - "id_skip": True, - "strides": 1, - "se_ratio": 0.25, - }, - { - "kernel_size": 3, - "repeats": 2, - "filters_in": 16, - "filters_out": 24, - "expand_ratio": 6, - "id_skip": True, - "strides": 2, - "se_ratio": 0.25, - }, - { - "kernel_size": 5, - "repeats": 2, - "filters_in": 24, - "filters_out": 40, - "expand_ratio": 6, - "id_skip": True, - "strides": 2, - "se_ratio": 0.25, - }, - { - "kernel_size": 3, - "repeats": 3, - "filters_in": 40, - "filters_out": 80, - "expand_ratio": 6, - "id_skip": True, - "strides": 2, - "se_ratio": 0.25, - }, - { - "kernel_size": 5, - "repeats": 3, - "filters_in": 80, - "filters_out": 112, - "expand_ratio": 6, - "id_skip": True, - "strides": 1, - "se_ratio": 0.25, - }, - { - "kernel_size": 5, - "repeats": 4, - "filters_in": 112, - "filters_out": 192, - "expand_ratio": 6, - "id_skip": True, - "strides": 2, - "se_ratio": 0.25, - }, - { - "kernel_size": 3, - "repeats": 1, - "filters_in": 192, - "filters_out": 320, - "expand_ratio": 6, - "id_skip": True, - "strides": 1, - "se_ratio": 0.25, - }, -] - -CONV_KERNEL_INITIALIZER = { - "class_name": "VarianceScaling", - "config": { - "scale": 2.0, - "mode": "fan_out", - "distribution": "truncated_normal", - }, -} - -DENSE_KERNEL_INITIALIZER = { - "class_name": "VarianceScaling", - "config": { - "scale": 1.0 / 3.0, - "mode": "fan_out", - "distribution": "uniform", - }, -} - -layers = VersionAwareLayers() - -BASE_DOCSTRING = """Instantiates the {name} architecture. - - Reference: - - [EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks]( - https://arxiv.org/abs/1905.11946) (ICML 2019) - - This function returns a Keras image classification model, - optionally loaded with weights pre-trained on ImageNet. - - For image classification use cases, see - [this page for detailed examples]( - https://keras.io/api/applications/#usage-examples-for-image-classification-models). - - For transfer learning use cases, make sure to read the - [guide to transfer learning & fine-tuning]( - https://keras.io/guides/transfer_learning/). - - Note: each Keras Application expects a specific kind of input preprocessing. - For EfficientNet, input preprocessing is included as part of the model - (as a `Rescaling` layer), and thus - `tf.keras.applications.efficientnet.preprocess_input` is actually a - pass-through function. EfficientNet models expect their inputs to be float - tensors of pixels with values in the [0-255] range. - - Args: - include_top: Whether to include the fully-connected - layer at the top of the network. Defaults to `True`. - weights: One of `None` (random initialization), - 'imagenet' (pre-training on ImageNet), - or the path to the weights file to be loaded. Defaults to 'imagenet'. - input_tensor: Optional Keras tensor - (i.e. output of `layers.Input()`) - to use as image input for the model. - input_shape: Optional shape tuple, only to be specified - if `include_top` is False. - It should have exactly 3 inputs channels. - pooling: Optional pooling mode for feature extraction - when `include_top` is `False`. Defaults to `None`. - - `None` means that the output of the model will be - the 4D tensor output of the - last convolutional layer. - - `avg` means that global average pooling - will be applied to the output of the - last convolutional layer, and thus - the output of the model will be a 2D tensor. - - `max` means that global max pooling will - be applied. - classes: Optional number of classes to classify images - into, only to be specified if `include_top` is True, and - if no `weights` argument is specified. 1000 is how many - ImageNet classes there are. Defaults to `1000`. - classifier_activation: A `str` or callable. The activation function to use - on the "top" layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" layer. - Defaults to 'softmax'. - When loading pretrained weights, `classifier_activation` can only - be `None` or `"softmax"`. - - Returns: - A `keras.Model` instance. -""" - - -IMAGENET_STDDEV_RGB = [0.229, 0.224, 0.225] - - -def EfficientNet( - width_coefficient, - depth_coefficient, - default_size, - dropout_rate=0.2, - drop_connect_rate=0.2, - depth_divisor=8, - activation="swish", - blocks_args="default", - model_name="efficientnet", - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - """Instantiates the EfficientNet architecture. - - Args: - width_coefficient: float, scaling coefficient for network width. - depth_coefficient: float, scaling coefficient for network depth. - default_size: integer, default input image size. - dropout_rate: float, dropout rate before final classifier layer. - drop_connect_rate: float, dropout rate at skip connections. - depth_divisor: integer, a unit of network width. - activation: activation function. - blocks_args: list of dicts, parameters to construct block modules. - model_name: string, model name. - include_top: whether to include the fully-connected - layer at the top of the network. - weights: one of `None` (random initialization), - 'imagenet' (pre-training on ImageNet), - or the path to the weights file to be loaded. - input_tensor: optional Keras tensor - (i.e. output of `layers.Input()`) - to use as image input for the model. - input_shape: optional shape tuple, only to be specified - if `include_top` is False. - It should have exactly 3 inputs channels. - pooling: optional pooling mode for feature extraction - when `include_top` is `False`. - - `None` means that the output of the model will be - the 4D tensor output of the - last convolutional layer. - - `avg` means that global average pooling - will be applied to the output of the - last convolutional layer, and thus - the output of the model will be a 2D tensor. - - `max` means that global max pooling will - be applied. - classes: optional number of classes to classify images - into, only to be specified if `include_top` is True, and - if no `weights` argument is specified. - classifier_activation: A `str` or callable. The activation function to use - on the "top" layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" layer. - - Returns: - A `keras.Model` instance. - - Raises: - ValueError: in case of invalid argument for `weights`, - or invalid input shape. - ValueError: if `classifier_activation` is not `softmax` or `None` when - using a pretrained top layer. - """ - if blocks_args == "default": - blocks_args = DEFAULT_BLOCKS_ARGS - - if not (weights in {"imagenet", None} or tf.io.gfile.exists(weights)): - raise ValueError( - "The `weights` argument should be either " - "`None` (random initialization), `imagenet` " - "(pre-training on ImageNet), " - "or the path to the weights file to be loaded." - ) - - if weights == "imagenet" and include_top and classes != 1000: - raise ValueError('If using `weights` as `"imagenet"` with `include_top`' " as true, `classes` should be 1000") - - # Determine proper input shape - input_shape = imagenet_utils.obtain_input_shape( - input_shape, - default_size=default_size, - min_size=32, - data_format=backend.image_data_format(), - require_flatten=include_top, - weights=weights, - ) - - if input_tensor is None: - img_input = layers.Input(shape=input_shape) - else: - if not backend.is_keras_tensor(input_tensor): - img_input = layers.Input(tensor=input_tensor, shape=input_shape) - else: - img_input = input_tensor - - bn_axis = 3 if backend.image_data_format() == "channels_last" else 1 - - def round_filters(filters, divisor=depth_divisor): - """Round number of filters based on depth multiplier.""" - filters *= width_coefficient - new_filters = max(divisor, int(filters + divisor / 2) // divisor * divisor) - # Make sure that round down does not go down by more than 10%. - if new_filters < 0.9 * filters: - new_filters += divisor - return int(new_filters) - - def round_repeats(repeats): - """Round number of repeats based on depth multiplier.""" - return int(math.ceil(depth_coefficient * repeats)) - - # Build stem - x = img_input - x = layers.Rescaling(1.0 / 255.0)(x) - x = layers.Normalization(axis=bn_axis)(x) - if weights == "imagenet": - # Note that the normaliztion layer uses square value of STDDEV as the - # variance for the layer: result = (input - mean) / sqrt(var) - # However, the original implemenetation uses (input - mean) / var to - # normalize the input, we need to divide another sqrt(var) to match the - # original implementation. - # See https://github.com/tensorflow/tensorflow/issues/49930 for more - # details - x = layers.Rescaling([1.0 / math.sqrt(stddev) for stddev in IMAGENET_STDDEV_RGB])(x) - - x = layers.ZeroPadding2D(padding=imagenet_utils.correct_pad(x, 3), name="stem_conv_pad")(x) - x = layers.Conv2D( - round_filters(32), - 3, - strides=2, - padding="valid", - use_bias=False, - kernel_initializer=CONV_KERNEL_INITIALIZER, - name="stem_conv", - )(x) - x = layers.BatchNormalization(axis=bn_axis, name="stem_bn")(x) - x = layers.Activation(activation, name="stem_activation")(x) - - # Build blocks - blocks_args = copy.deepcopy(blocks_args) - - b = 0 - blocks = float(sum(round_repeats(args["repeats"]) for args in blocks_args)) - for i, args in enumerate(blocks_args): - assert args["repeats"] > 0 - # Update block input and output filters based on depth multiplier. - args["filters_in"] = round_filters(args["filters_in"]) - args["filters_out"] = round_filters(args["filters_out"]) - - for j in range(round_repeats(args.pop("repeats"))): - # The first block needs to take care of stride and filter size - # increase. - if j > 0: - args["strides"] = 1 - args["filters_in"] = args["filters_out"] - x = block( - x, - activation, - drop_connect_rate * b / blocks, - name=f"block{i + 1}{chr(j + 97)}_", - **args, - ) - b += 1 - - # Build top - x = layers.Conv2D( - round_filters(1280), - 1, - padding="same", - use_bias=False, - kernel_initializer=CONV_KERNEL_INITIALIZER, - name="top_conv", - )(x) - x = layers.BatchNormalization(axis=bn_axis, name="top_bn")(x) - x = layers.Activation(activation, name="top_activation")(x) - if include_top: - x = layers.GlobalAveragePooling2D(name="avg_pool")(x) - if dropout_rate > 0: - x = layers.Dropout(dropout_rate, name="top_dropout")(x) - imagenet_utils.validate_activation(classifier_activation, weights) - x = layers.Dense( - classes, - activation=classifier_activation, - kernel_initializer=DENSE_KERNEL_INITIALIZER, - name="predictions", - )(x) - else: - if pooling == "avg": - x = layers.GlobalAveragePooling2D(name="avg_pool")(x) - elif pooling == "max": - x = layers.GlobalMaxPooling2D(name="max_pool")(x) - - # Ensure that the model takes into account - # any potential predecessors of `input_tensor`. - if input_tensor is not None: - inputs = layer_utils.get_source_inputs(input_tensor) - else: - inputs = img_input - - # Create model. - model = training.Model(inputs, x, name=model_name) - - # Load weights. - if weights == "imagenet": - if include_top: - file_suffix = ".h5" - file_hash = WEIGHTS_HASHES[model_name[-2:]][0] - else: - file_suffix = "_notop.h5" - file_hash = WEIGHTS_HASHES[model_name[-2:]][1] - file_name = model_name + file_suffix - weights_path = data_utils.get_file( - file_name, - BASE_WEIGHTS_PATH + file_name, - cache_subdir="models", - file_hash=file_hash, - ) - model.load_weights(weights_path) - elif weights is not None: - model.load_weights(weights) - return model - - -def block( - inputs, - activation="swish", - drop_rate=0.0, - name="", - filters_in=32, - filters_out=16, - kernel_size=3, - strides=1, - expand_ratio=1, - se_ratio=0.0, - id_skip=True, -): - """An inverted residual block. - - Args: - inputs: input tensor. - activation: activation function. - drop_rate: float between 0 and 1, fraction of the input units to drop. - name: string, block label. - filters_in: integer, the number of input filters. - filters_out: integer, the number of output filters. - kernel_size: integer, the dimension of the convolution window. - strides: integer, the stride of the convolution. - expand_ratio: integer, scaling coefficient for the input filters. - se_ratio: float between 0 and 1, fraction to squeeze the input filters. - id_skip: boolean. - - Returns: - output tensor for the block. - """ - bn_axis = 3 if backend.image_data_format() == "channels_last" else 1 - - # Expansion phase - filters = filters_in * expand_ratio - if expand_ratio != 1: - x = layers.Conv2D( - filters, - 1, - padding="same", - use_bias=False, - kernel_initializer=CONV_KERNEL_INITIALIZER, - name=name + "expand_conv", - )(inputs) - x = layers.BatchNormalization(axis=bn_axis, name=name + "expand_bn")(x) - x = layers.Activation(activation, name=name + "expand_activation")(x) - else: - x = inputs - - # Depthwise Convolution - if strides == 2: - x = layers.ZeroPadding2D( - padding=imagenet_utils.correct_pad(x, kernel_size), - name=name + "dwconv_pad", - )(x) - conv_pad = "valid" - else: - conv_pad = "same" - x = layers.DepthwiseConv2D( - kernel_size, - strides=strides, - padding=conv_pad, - use_bias=False, - depthwise_initializer=CONV_KERNEL_INITIALIZER, - name=name + "dwconv", - )(x) - x = layers.BatchNormalization(axis=bn_axis, name=name + "bn")(x) - x = layers.Activation(activation, name=name + "activation")(x) - - # Squeeze and Excitation phase - if 0 < se_ratio <= 1: - filters_se = max(1, int(filters_in * se_ratio)) - se = layers.GlobalAveragePooling2D(name=name + "se_squeeze")(x) - if bn_axis == 1: - se_shape = (filters, 1, 1) - else: - se_shape = (1, 1, filters) - se = layers.Reshape(se_shape, name=name + "se_reshape")(se) - se = layers.Conv2D( - filters_se, - 1, - padding="same", - activation=activation, - kernel_initializer=CONV_KERNEL_INITIALIZER, - name=name + "se_reduce", - )(se) - se = layers.Conv2D( - filters, - 1, - padding="same", - activation="sigmoid", - kernel_initializer=CONV_KERNEL_INITIALIZER, - name=name + "se_expand", - )(se) - x = layers.multiply([x, se], name=name + "se_excite") - - # Output phase - x = layers.Conv2D( - filters_out, - 1, - padding="same", - use_bias=False, - kernel_initializer=CONV_KERNEL_INITIALIZER, - name=name + "project_conv", - )(x) - x = layers.BatchNormalization(axis=bn_axis, name=name + "project_bn")(x) - if id_skip and strides == 1 and filters_in == filters_out: - if drop_rate > 0: - x = layers.Dropout(drop_rate, noise_shape=(None, 1, 1, 1), name=name + "drop")(x) - x = layers.add([x, inputs], name=name + "add") - return x - - -@keras_export( - "keras.applications.efficientnet.EfficientNetB0", - "keras.applications.EfficientNetB0", -) -def EfficientNetB0( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", - **kwargs, -): - return EfficientNet( - 1.0, - 1.0, - 224, - 0.2, - model_name="efficientnetb0", - include_top=include_top, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - **kwargs, - ) - - -@keras_export( - "keras.applications.efficientnet.EfficientNetB1", - "keras.applications.EfficientNetB1", -) -def EfficientNetB1( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", - **kwargs, -): - return EfficientNet( - 1.0, - 1.1, - 240, - 0.2, - model_name="efficientnetb1", - include_top=include_top, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - **kwargs, - ) - - -@keras_export( - "keras.applications.efficientnet.EfficientNetB2", - "keras.applications.EfficientNetB2", -) -def EfficientNetB2( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", - **kwargs, -): - return EfficientNet( - 1.1, - 1.2, - 260, - 0.3, - model_name="efficientnetb2", - include_top=include_top, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - **kwargs, - ) - - -@keras_export( - "keras.applications.efficientnet.EfficientNetB3", - "keras.applications.EfficientNetB3", -) -def EfficientNetB3( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", - **kwargs, -): - return EfficientNet( - 1.2, - 1.4, - 300, - 0.3, - model_name="efficientnetb3", - include_top=include_top, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - **kwargs, - ) - - -@keras_export( - "keras.applications.efficientnet.EfficientNetB4", - "keras.applications.EfficientNetB4", -) -def EfficientNetB4( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", - **kwargs, -): - return EfficientNet( - 1.4, - 1.8, - 380, - 0.4, - model_name="efficientnetb4", - include_top=include_top, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - **kwargs, - ) - - -@keras_export( - "keras.applications.efficientnet.EfficientNetB5", - "keras.applications.EfficientNetB5", -) -def EfficientNetB5( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", - **kwargs, -): - return EfficientNet( - 1.6, - 2.2, - 456, - 0.4, - model_name="efficientnetb5", - include_top=include_top, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - **kwargs, - ) - - -@keras_export( - "keras.applications.efficientnet.EfficientNetB6", - "keras.applications.EfficientNetB6", -) -def EfficientNetB6( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", - **kwargs, -): - return EfficientNet( - 1.8, - 2.6, - 528, - 0.5, - model_name="efficientnetb6", - include_top=include_top, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - **kwargs, - ) - - -@keras_export( - "keras.applications.efficientnet.EfficientNetB7", - "keras.applications.EfficientNetB7", -) -def EfficientNetB7( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", - **kwargs, -): - return EfficientNet( - 2.0, - 3.1, - 600, - 0.5, - model_name="efficientnetb7", - include_top=include_top, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - **kwargs, - ) - - -def EfficientNet_CXL( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", - **kwargs, -): - return EfficientNet( - 3.0, - 4.8, - 384, - 0.5, - model_name="efficientnet-CXL", - include_top=include_top, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - **kwargs, - ) - - -EfficientNetB0.__doc__ = BASE_DOCSTRING.format(name="EfficientNetB0") -EfficientNetB1.__doc__ = BASE_DOCSTRING.format(name="EfficientNetB1") -EfficientNetB2.__doc__ = BASE_DOCSTRING.format(name="EfficientNetB2") -EfficientNetB3.__doc__ = BASE_DOCSTRING.format(name="EfficientNetB3") -EfficientNetB4.__doc__ = BASE_DOCSTRING.format(name="EfficientNetB4") -EfficientNetB5.__doc__ = BASE_DOCSTRING.format(name="EfficientNetB5") -EfficientNetB6.__doc__ = BASE_DOCSTRING.format(name="EfficientNetB6") -EfficientNetB7.__doc__ = BASE_DOCSTRING.format(name="EfficientNetB7") -EfficientNet_CXL.__doc__ = BASE_DOCSTRING.format(name="efficientnet-CXL") - - -@keras_export("keras.applications.efficientnet.preprocess_input") -def preprocess_input(x, data_format=None): - """A placeholder method for backward compatibility. - - The preprocessing logic has been included in the efficientnet model - implementation. Users are no longer required to call this method to - normalize the input data. This method does nothing and only kept as a - placeholder to align the API surface between old and new version of model. - - Args: - x: A floating point `numpy.array` or a `tf.Tensor`. - data_format: Optional data format of the image tensor/array. `None` means - the global setting `tf.keras.backend.image_data_format()` is used - (unless you changed it, it uses "channels_last"). - Defaults to `None`. - - Returns: - Unchanged `numpy.array` or `tf.Tensor`. - """ - return x - - -@keras_export("keras.applications.efficientnet.decode_predictions") -def decode_predictions(preds, top=5): - return imagenet_utils.decode_predictions(preds, top=top) - - -decode_predictions.__doc__ = imagenet_utils.decode_predictions.__doc__ diff --git a/Archive/keras_applications_mod/models/efficientnet_v2.py b/Archive/keras_applications_mod/models/efficientnet_v2.py deleted file mode 100644 index 3e44f24..0000000 --- a/Archive/keras_applications_mod/models/efficientnet_v2.py +++ /dev/null @@ -1,1344 +0,0 @@ -# Copyright 2021 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - - -"""EfficientNet V2 models for Keras. - -Reference: -- [EfficientNetV2: Smaller Models and Faster Training]( - https://arxiv.org/abs/2104.00298) (ICML 2021) -""" - -import copy -import math - -import tensorflow.compat.v2 as tf - -from keras import backend -from keras import layers -from keras.applications import imagenet_utils -from keras.engine import training -from keras.utils import data_utils -from keras.utils import layer_utils - -# isort: off -from tensorflow.python.util.tf_export import keras_export - -BASE_WEIGHTS_PATH = "https://storage.googleapis.com/tensorflow/keras-applications/efficientnet_v2/" # noqa: E501 - -WEIGHTS_HASHES = { - "b0": ( - "21ecbf6da12460d5c40bb2f29ceb2188", - "893217f2bb855e2983157299931e43ff", - ), - "b1": ( - "069f0534ff22adf035c89e2d9547a9dc", - "0e80663031ca32d657f9caa404b6ec37", - ), - "b2": ( - "424e49f28180edbde1e94797771950a7", - "1dfe2e7a5d45b6632553a8961ea609eb", - ), - "b3": ( - "1f1fc43bd98a6e4fd8fdfd551e02c7a0", - "f6abf7b5849ac99a89b50dd3fd532856", - ), - "-s": ( - "e1d88a8495beba45748fedd0cecbe016", - "af0682fb74e8c54910f2d4393339c070", - ), - "-m": ( - "a3bf6aa3276309f4fc6a34aa114c95cd", - "1b8dc055df72dde80d614482840fe342", - ), - "-l": ( - "27e6d408b53c7ebc868fefa357689935", - "b0b66b5c863aef5b46e8608fe1711615", - ), -} - -DEFAULT_BLOCKS_ARGS = { - "efficientnetv2-s": [ - { - "kernel_size": 3, - "num_repeat": 2, - "input_filters": 24, - "output_filters": 24, - "expand_ratio": 1, - "se_ratio": 0.0, - "strides": 1, - "conv_type": 1, - }, - { - "kernel_size": 3, - "num_repeat": 4, - "input_filters": 24, - "output_filters": 48, - "expand_ratio": 4, - "se_ratio": 0.0, - "strides": 2, - "conv_type": 1, - }, - { - "conv_type": 1, - "expand_ratio": 4, - "input_filters": 48, - "kernel_size": 3, - "num_repeat": 4, - "output_filters": 64, - "se_ratio": 0, - "strides": 2, - }, - { - "conv_type": 0, - "expand_ratio": 4, - "input_filters": 64, - "kernel_size": 3, - "num_repeat": 6, - "output_filters": 128, - "se_ratio": 0.25, - "strides": 2, - }, - { - "conv_type": 0, - "expand_ratio": 6, - "input_filters": 128, - "kernel_size": 3, - "num_repeat": 9, - "output_filters": 160, - "se_ratio": 0.25, - "strides": 1, - }, - { - "conv_type": 0, - "expand_ratio": 6, - "input_filters": 160, - "kernel_size": 3, - "num_repeat": 15, - "output_filters": 256, - "se_ratio": 0.25, - "strides": 2, - }, - ], - "efficientnetv2-m": [ - { - "kernel_size": 3, - "num_repeat": 3, - "input_filters": 24, - "output_filters": 24, - "expand_ratio": 1, - "se_ratio": 0, - "strides": 1, - "conv_type": 1, - }, - { - "kernel_size": 3, - "num_repeat": 5, - "input_filters": 24, - "output_filters": 48, - "expand_ratio": 4, - "se_ratio": 0, - "strides": 2, - "conv_type": 1, - }, - { - "kernel_size": 3, - "num_repeat": 5, - "input_filters": 48, - "output_filters": 80, - "expand_ratio": 4, - "se_ratio": 0, - "strides": 2, - "conv_type": 1, - }, - { - "kernel_size": 3, - "num_repeat": 7, - "input_filters": 80, - "output_filters": 160, - "expand_ratio": 4, - "se_ratio": 0.25, - "strides": 2, - "conv_type": 0, - }, - { - "kernel_size": 3, - "num_repeat": 14, - "input_filters": 160, - "output_filters": 176, - "expand_ratio": 6, - "se_ratio": 0.25, - "strides": 1, - "conv_type": 0, - }, - { - "kernel_size": 3, - "num_repeat": 18, - "input_filters": 176, - "output_filters": 304, - "expand_ratio": 6, - "se_ratio": 0.25, - "strides": 2, - "conv_type": 0, - }, - { - "kernel_size": 3, - "num_repeat": 5, - "input_filters": 304, - "output_filters": 512, - "expand_ratio": 6, - "se_ratio": 0.25, - "strides": 1, - "conv_type": 0, - }, - ], - "efficientnetv2-l": [ - { - "kernel_size": 3, - "num_repeat": 4, - "input_filters": 32, - "output_filters": 32, - "expand_ratio": 1, - "se_ratio": 0, - "strides": 1, - "conv_type": 1, - }, - { - "kernel_size": 3, - "num_repeat": 7, - "input_filters": 32, - "output_filters": 64, - "expand_ratio": 4, - "se_ratio": 0, - "strides": 2, - "conv_type": 1, - }, - { - "kernel_size": 3, - "num_repeat": 7, - "input_filters": 64, - "output_filters": 96, - "expand_ratio": 4, - "se_ratio": 0, - "strides": 2, - "conv_type": 1, - }, - { - "kernel_size": 3, - "num_repeat": 10, - "input_filters": 96, - "output_filters": 192, - "expand_ratio": 4, - "se_ratio": 0.25, - "strides": 2, - "conv_type": 0, - }, - { - "kernel_size": 3, - "num_repeat": 19, - "input_filters": 192, - "output_filters": 224, - "expand_ratio": 6, - "se_ratio": 0.25, - "strides": 1, - "conv_type": 0, - }, - { - "kernel_size": 3, - "num_repeat": 25, - "input_filters": 224, - "output_filters": 384, - "expand_ratio": 6, - "se_ratio": 0.25, - "strides": 2, - "conv_type": 0, - }, - { - "kernel_size": 3, - "num_repeat": 7, - "input_filters": 384, - "output_filters": 640, - "expand_ratio": 6, - "se_ratio": 0.25, - "strides": 1, - "conv_type": 0, - }, - ], - "efficientnetv2-b0": [ - { - "kernel_size": 3, - "num_repeat": 1, - "input_filters": 32, - "output_filters": 16, - "expand_ratio": 1, - "se_ratio": 0, - "strides": 1, - "conv_type": 1, - }, - { - "kernel_size": 3, - "num_repeat": 2, - "input_filters": 16, - "output_filters": 32, - "expand_ratio": 4, - "se_ratio": 0, - "strides": 2, - "conv_type": 1, - }, - { - "kernel_size": 3, - "num_repeat": 2, - "input_filters": 32, - "output_filters": 48, - "expand_ratio": 4, - "se_ratio": 0, - "strides": 2, - "conv_type": 1, - }, - { - "kernel_size": 3, - "num_repeat": 3, - "input_filters": 48, - "output_filters": 96, - "expand_ratio": 4, - "se_ratio": 0.25, - "strides": 2, - "conv_type": 0, - }, - { - "kernel_size": 3, - "num_repeat": 5, - "input_filters": 96, - "output_filters": 112, - "expand_ratio": 6, - "se_ratio": 0.25, - "strides": 1, - "conv_type": 0, - }, - { - "kernel_size": 3, - "num_repeat": 8, - "input_filters": 112, - "output_filters": 192, - "expand_ratio": 6, - "se_ratio": 0.25, - "strides": 2, - "conv_type": 0, - }, - ], - "efficientnetv2-b1": [ - { - "kernel_size": 3, - "num_repeat": 1, - "input_filters": 32, - "output_filters": 16, - "expand_ratio": 1, - "se_ratio": 0, - "strides": 1, - "conv_type": 1, - }, - { - "kernel_size": 3, - "num_repeat": 2, - "input_filters": 16, - "output_filters": 32, - "expand_ratio": 4, - "se_ratio": 0, - "strides": 2, - "conv_type": 1, - }, - { - "kernel_size": 3, - "num_repeat": 2, - "input_filters": 32, - "output_filters": 48, - "expand_ratio": 4, - "se_ratio": 0, - "strides": 2, - "conv_type": 1, - }, - { - "kernel_size": 3, - "num_repeat": 3, - "input_filters": 48, - "output_filters": 96, - "expand_ratio": 4, - "se_ratio": 0.25, - "strides": 2, - "conv_type": 0, - }, - { - "kernel_size": 3, - "num_repeat": 5, - "input_filters": 96, - "output_filters": 112, - "expand_ratio": 6, - "se_ratio": 0.25, - "strides": 1, - "conv_type": 0, - }, - { - "kernel_size": 3, - "num_repeat": 8, - "input_filters": 112, - "output_filters": 192, - "expand_ratio": 6, - "se_ratio": 0.25, - "strides": 2, - "conv_type": 0, - }, - ], - "efficientnetv2-b2": [ - { - "kernel_size": 3, - "num_repeat": 1, - "input_filters": 32, - "output_filters": 16, - "expand_ratio": 1, - "se_ratio": 0, - "strides": 1, - "conv_type": 1, - }, - { - "kernel_size": 3, - "num_repeat": 2, - "input_filters": 16, - "output_filters": 32, - "expand_ratio": 4, - "se_ratio": 0, - "strides": 2, - "conv_type": 1, - }, - { - "kernel_size": 3, - "num_repeat": 2, - "input_filters": 32, - "output_filters": 48, - "expand_ratio": 4, - "se_ratio": 0, - "strides": 2, - "conv_type": 1, - }, - { - "kernel_size": 3, - "num_repeat": 3, - "input_filters": 48, - "output_filters": 96, - "expand_ratio": 4, - "se_ratio": 0.25, - "strides": 2, - "conv_type": 0, - }, - { - "kernel_size": 3, - "num_repeat": 5, - "input_filters": 96, - "output_filters": 112, - "expand_ratio": 6, - "se_ratio": 0.25, - "strides": 1, - "conv_type": 0, - }, - { - "kernel_size": 3, - "num_repeat": 8, - "input_filters": 112, - "output_filters": 192, - "expand_ratio": 6, - "se_ratio": 0.25, - "strides": 2, - "conv_type": 0, - }, - ], - "efficientnetv2-b3": [ - { - "kernel_size": 3, - "num_repeat": 1, - "input_filters": 32, - "output_filters": 16, - "expand_ratio": 1, - "se_ratio": 0, - "strides": 1, - "conv_type": 1, - }, - { - "kernel_size": 3, - "num_repeat": 2, - "input_filters": 16, - "output_filters": 32, - "expand_ratio": 4, - "se_ratio": 0, - "strides": 2, - "conv_type": 1, - }, - { - "kernel_size": 3, - "num_repeat": 2, - "input_filters": 32, - "output_filters": 48, - "expand_ratio": 4, - "se_ratio": 0, - "strides": 2, - "conv_type": 1, - }, - { - "kernel_size": 3, - "num_repeat": 3, - "input_filters": 48, - "output_filters": 96, - "expand_ratio": 4, - "se_ratio": 0.25, - "strides": 2, - "conv_type": 0, - }, - { - "kernel_size": 3, - "num_repeat": 5, - "input_filters": 96, - "output_filters": 112, - "expand_ratio": 6, - "se_ratio": 0.25, - "strides": 1, - "conv_type": 0, - }, - { - "kernel_size": 3, - "num_repeat": 8, - "input_filters": 112, - "output_filters": 192, - "expand_ratio": 6, - "se_ratio": 0.25, - "strides": 2, - "conv_type": 0, - }, - ], -} - -CONV_KERNEL_INITIALIZER = { - "class_name": "VarianceScaling", - "config": { - "scale": 2.0, - "mode": "fan_out", - "distribution": "truncated_normal", - }, -} - -DENSE_KERNEL_INITIALIZER = { - "class_name": "VarianceScaling", - "config": { - "scale": 1.0 / 3.0, - "mode": "fan_out", - "distribution": "uniform", - }, -} - -BASE_DOCSTRING = """Instantiates the {name} architecture. - - Reference: - - [EfficientNetV2: Smaller Models and Faster Training]( - https://arxiv.org/abs/2104.00298) (ICML 2021) - - This function returns a Keras image classification model, - optionally loaded with weights pre-trained on ImageNet. - - For image classification use cases, see - [this page for detailed examples]( - https://keras.io/api/applications/#usage-examples-for-image-classification-models). - - For transfer learning use cases, make sure to read the - [guide to transfer learning & fine-tuning]( - https://keras.io/guides/transfer_learning/). - - Note: each Keras Application expects a specific kind of input preprocessing. - For EfficientNetV2, by default input preprocessing is included as a part of - the model (as a `Rescaling` layer), and thus - `tf.keras.applications.efficientnet_v2.preprocess_input` is actually a - pass-through function. In this use case, EfficientNetV2 models expect their - inputs to be float tensors of pixels with values in the [0-255] range. - At the same time, preprocessing as a part of the model (i.e. `Rescaling` - layer) can be disabled by setting `include_preprocessing` argument to False. - With preprocessing disabled EfficientNetV2 models expect their inputs to be - float tensors of pixels with values in the [-1, 1] range. - - Args: - include_top: Boolean, whether to include the fully-connected - layer at the top of the network. Defaults to `True`. - weights: One of `None` (random initialization), - `"imagenet"` (pre-training on ImageNet), - or the path to the weights file to be loaded. Defaults to `"imagenet"`. - input_tensor: Optional Keras tensor - (i.e. output of `layers.Input()`) - to use as image input for the model. - input_shape: Optional shape tuple, only to be specified - if `include_top` is False. - It should have exactly 3 inputs channels. - pooling: Optional pooling mode for feature extraction - when `include_top` is `False`. - - `None` means that the output of the model will be - the 4D tensor output of the - last convolutional layer. - - `"avg"` means that global average pooling - will be applied to the output of the - last convolutional layer, and thus - the output of the model will be a 2D tensor. - - `"max"` means that global max pooling will - be applied. - Defaults to `None`. - classes: Optional number of classes to classify images - into, only to be specified if `include_top` is True, and - if no `weights` argument is specified. 1000 is how many - ImageNet classes there are. Defaults to `1000`. - classifier_activation: A string or callable. The activation function to use - on the `"top"` layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" layer. - When loading pretrained weights, `classifier_activation` can only - be `None` or `"softmax"`. - Defaults to `"softmax"`. - - Returns: - A `keras.Model` instance. -""" - - -def round_filters(filters, width_coefficient, min_depth, depth_divisor): - """Round number of filters based on depth multiplier.""" - filters *= width_coefficient - minimum_depth = min_depth or depth_divisor - new_filters = max( - minimum_depth, - int(filters + depth_divisor / 2) // depth_divisor * depth_divisor, - ) - return int(new_filters) - - -def round_repeats(repeats, depth_coefficient): - """Round number of repeats based on depth multiplier.""" - return int(math.ceil(depth_coefficient * repeats)) - - -def MBConvBlock( - input_filters: int, - output_filters: int, - expand_ratio=1, - kernel_size=3, - strides=1, - se_ratio=0.0, - bn_momentum=0.9, - activation="swish", - survival_probability: float = 0.8, - name=None, -): - """MBConv block: Mobile Inverted Residual Bottleneck.""" - bn_axis = 3 if backend.image_data_format() == "channels_last" else 1 - - if name is None: - name = backend.get_uid("block0") - - def apply(inputs): - # Expansion phase - filters = input_filters * expand_ratio - if expand_ratio != 1: - x = layers.Conv2D( - filters=filters, - kernel_size=1, - strides=1, - kernel_initializer=CONV_KERNEL_INITIALIZER, - padding="same", - data_format="channels_last", - use_bias=False, - name=name + "expand_conv", - )(inputs) - x = layers.BatchNormalization( - axis=bn_axis, - momentum=bn_momentum, - name=name + "expand_bn", - )(x) - x = layers.Activation(activation, name=name + "expand_activation")(x) - else: - x = inputs - - # Depthwise conv - x = layers.DepthwiseConv2D( - kernel_size=kernel_size, - strides=strides, - depthwise_initializer=CONV_KERNEL_INITIALIZER, - padding="same", - data_format="channels_last", - use_bias=False, - name=name + "dwconv2", - )(x) - x = layers.BatchNormalization(axis=bn_axis, momentum=bn_momentum, name=name + "bn")(x) - x = layers.Activation(activation, name=name + "activation")(x) - - # Squeeze and excite - if 0 < se_ratio <= 1: - filters_se = max(1, int(input_filters * se_ratio)) - se = layers.GlobalAveragePooling2D(name=name + "se_squeeze")(x) - if bn_axis == 1: - se_shape = (filters, 1, 1) - else: - se_shape = (1, 1, filters) - se = layers.Reshape(se_shape, name=name + "se_reshape")(se) - - se = layers.Conv2D( - filters_se, - 1, - padding="same", - activation=activation, - kernel_initializer=CONV_KERNEL_INITIALIZER, - name=name + "se_reduce", - )(se) - se = layers.Conv2D( - filters, - 1, - padding="same", - activation="sigmoid", - kernel_initializer=CONV_KERNEL_INITIALIZER, - name=name + "se_expand", - )(se) - - x = layers.multiply([x, se], name=name + "se_excite") - - # Output phase - x = layers.Conv2D( - filters=output_filters, - kernel_size=1, - strides=1, - kernel_initializer=CONV_KERNEL_INITIALIZER, - padding="same", - data_format="channels_last", - use_bias=False, - name=name + "project_conv", - )(x) - x = layers.BatchNormalization(axis=bn_axis, momentum=bn_momentum, name=name + "project_bn")(x) - - if strides == 1 and input_filters == output_filters: - if survival_probability: - x = layers.Dropout( - survival_probability, - noise_shape=(None, 1, 1, 1), - name=name + "drop", - )(x) - x = layers.add([x, inputs], name=name + "add") - - return x - - return apply - - -def FusedMBConvBlock( - input_filters: int, - output_filters: int, - expand_ratio=1, - kernel_size=3, - strides=1, - se_ratio=0.0, - bn_momentum=0.9, - activation="swish", - survival_probability: float = 0.8, - name=None, -): - """Fused MBConv Block: Fusing the proj conv1x1 and depthwise_conv into a - conv2d.""" - bn_axis = 3 if backend.image_data_format() == "channels_last" else 1 - - if name is None: - name = backend.get_uid("block0") - - def apply(inputs): - filters = input_filters * expand_ratio - if expand_ratio != 1: - x = layers.Conv2D( - filters, - kernel_size=kernel_size, - strides=strides, - kernel_initializer=CONV_KERNEL_INITIALIZER, - data_format="channels_last", - padding="same", - use_bias=False, - name=name + "expand_conv", - )(inputs) - x = layers.BatchNormalization(axis=bn_axis, momentum=bn_momentum, name=name + "expand_bn")(x) - x = layers.Activation(activation=activation, name=name + "expand_activation")(x) - else: - x = inputs - - # Squeeze and excite - if 0 < se_ratio <= 1: - filters_se = max(1, int(input_filters * se_ratio)) - se = layers.GlobalAveragePooling2D(name=name + "se_squeeze")(x) - if bn_axis == 1: - se_shape = (filters, 1, 1) - else: - se_shape = (1, 1, filters) - - se = layers.Reshape(se_shape, name=name + "se_reshape")(se) - - se = layers.Conv2D( - filters_se, - 1, - padding="same", - activation=activation, - kernel_initializer=CONV_KERNEL_INITIALIZER, - name=name + "se_reduce", - )(se) - se = layers.Conv2D( - filters, - 1, - padding="same", - activation="sigmoid", - kernel_initializer=CONV_KERNEL_INITIALIZER, - name=name + "se_expand", - )(se) - - x = layers.multiply([x, se], name=name + "se_excite") - - # Output phase: - x = layers.Conv2D( - output_filters, - kernel_size=1 if expand_ratio != 1 else kernel_size, - strides=1 if expand_ratio != 1 else strides, - kernel_initializer=CONV_KERNEL_INITIALIZER, - padding="same", - use_bias=False, - name=name + "project_conv", - )(x) - x = layers.BatchNormalization(axis=bn_axis, momentum=bn_momentum, name=name + "project_bn")(x) - if expand_ratio == 1: - x = layers.Activation(activation=activation, name=name + "project_activation")(x) - - # Residual: - if strides == 1 and input_filters == output_filters: - if survival_probability: - x = layers.Dropout( - survival_probability, - noise_shape=(None, 1, 1, 1), - name=name + "drop", - )(x) - x = layers.add([x, inputs], name=name + "add") - return x - - return apply - - -def EfficientNetV2( - width_coefficient, - depth_coefficient, - default_size, - dropout_rate=0.2, - drop_connect_rate=0.2, - depth_divisor=8, - min_depth=8, - bn_momentum=0.9, - activation="swish", - blocks_args="default", - model_name="efficientnetv2", - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", - include_preprocessing=True, -): - """Instantiates the EfficientNetV2 architecture using given scaling - coefficients. - - Args: - width_coefficient: float, scaling coefficient for network width. - depth_coefficient: float, scaling coefficient for network depth. - default_size: integer, default input image size. - dropout_rate: float, dropout rate before final classifier layer. - drop_connect_rate: float, dropout rate at skip connections. - depth_divisor: integer, a unit of network width. - min_depth: integer, minimum number of filters. - bn_momentum: float. Momentum parameter for Batch Normalization layers. - activation: activation function. - blocks_args: list of dicts, parameters to construct block modules. - model_name: string, model name. - include_top: whether to include the fully-connected layer at the top of - the network. - weights: one of `None` (random initialization), `"imagenet"` (pre-training - on ImageNet), or the path to the weights file to be loaded. - input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) or - numpy array to use as image input for the model. - input_shape: optional shape tuple, only to be specified if `include_top` - is False. It should have exactly 3 inputs channels. - pooling: optional pooling mode for feature extraction when `include_top` - is `False`. - - `None` means that the output of the model will be the 4D tensor output - of the last convolutional layer. - - "avg" means that global average pooling will be applied to the output - of the last convolutional layer, and thus the output of the model will - be a 2D tensor. - - `"max"` means that global max pooling will be applied. - classes: optional number of classes to classify images into, only to be - specified if `include_top` is True, and if no `weights` argument is - specified. - classifier_activation: A string or callable. The activation function to - use on the `"top"` layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the `"top"` layer. - include_preprocessing: Boolean, whether to include the preprocessing layer - (`Rescaling`) at the bottom of the network. Defaults to `True`. - - Returns: - A `keras.Model` instance. - - Raises: - ValueError: in case of invalid argument for `weights`, - or invalid input shape. - ValueError: if `classifier_activation` is not `"softmax"` or `None` when - using a pretrained top layer. - """ - - if blocks_args == "default": - blocks_args = DEFAULT_BLOCKS_ARGS[model_name] - - if not (weights in {"imagenet", None} or tf.io.gfile.exists(weights)): - raise ValueError( - "The `weights` argument should be either " - "`None` (random initialization), `imagenet` " - "(pre-training on ImageNet), " - "or the path to the weights file to be loaded." - f"Received: weights={weights}" - ) - - if weights == "imagenet" and include_top and classes != 1000: - raise ValueError( - "If using `weights` as `'imagenet'` with `include_top`" " as true, `classes` should be 1000" f"Received: classes={classes}" - ) - - # Determine proper input shape - input_shape = imagenet_utils.obtain_input_shape( - input_shape, - default_size=default_size, - min_size=32, - data_format=backend.image_data_format(), - require_flatten=include_top, - weights=weights, - ) - - if input_tensor is None: - img_input = layers.Input(shape=input_shape) - else: - if not backend.is_keras_tensor(input_tensor): - img_input = layers.Input(tensor=input_tensor, shape=input_shape) - else: - img_input = input_tensor - - bn_axis = 3 if backend.image_data_format() == "channels_last" else 1 - - x = img_input - - if include_preprocessing: - # Apply original V1 preprocessing for Bx variants - # if number of channels allows it - num_channels = input_shape[bn_axis - 1] - if model_name.split("-")[-1].startswith("b") and num_channels == 3: - x = layers.Rescaling(scale=1.0 / 255)(x) - x = layers.Normalization( - mean=[0.485, 0.456, 0.406], - variance=[0.229**2, 0.224**2, 0.225**2], - axis=bn_axis, - )(x) - else: - x = layers.Rescaling(scale=1.0 / 128.0, offset=-1)(x) - - # Build stem - stem_filters = round_filters( - filters=blocks_args[0]["input_filters"], - width_coefficient=width_coefficient, - min_depth=min_depth, - depth_divisor=depth_divisor, - ) - x = layers.Conv2D( - filters=stem_filters, - kernel_size=3, - strides=2, - kernel_initializer=CONV_KERNEL_INITIALIZER, - padding="same", - use_bias=False, - name="stem_conv", - )(x) - x = layers.BatchNormalization( - axis=bn_axis, - momentum=bn_momentum, - name="stem_bn", - )(x) - x = layers.Activation(activation, name="stem_activation")(x) - - # Build blocks - blocks_args = copy.deepcopy(blocks_args) - b = 0 - blocks = float(sum(args["num_repeat"] for args in blocks_args)) - - for i, args in enumerate(blocks_args): - assert args["num_repeat"] > 0 - - # Update block input and output filters based on depth multiplier. - args["input_filters"] = round_filters( - filters=args["input_filters"], - width_coefficient=width_coefficient, - min_depth=min_depth, - depth_divisor=depth_divisor, - ) - args["output_filters"] = round_filters( - filters=args["output_filters"], - width_coefficient=width_coefficient, - min_depth=min_depth, - depth_divisor=depth_divisor, - ) - - # Determine which conv type to use: - block = {0: MBConvBlock, 1: FusedMBConvBlock}[args.pop("conv_type")] - repeats = round_repeats(repeats=args.pop("num_repeat"), depth_coefficient=depth_coefficient) - for j in range(repeats): - # The first block needs to take care of stride and filter size - # increase. - if j > 0: - args["strides"] = 1 - args["input_filters"] = args["output_filters"] - - x = block( - activation=activation, - bn_momentum=bn_momentum, - survival_probability=drop_connect_rate * b / blocks, - name=f"block{i + 1}{chr(j + 97)}_", - **args, - )(x) - b += 1 - - # Build top - top_filters = round_filters( - filters=1280, - width_coefficient=width_coefficient, - min_depth=min_depth, - depth_divisor=depth_divisor, - ) - x = layers.Conv2D( - filters=top_filters, - kernel_size=1, - strides=1, - kernel_initializer=CONV_KERNEL_INITIALIZER, - padding="same", - data_format="channels_last", - use_bias=False, - name="top_conv", - )(x) - x = layers.BatchNormalization( - axis=bn_axis, - momentum=bn_momentum, - name="top_bn", - )(x) - x = layers.Activation(activation=activation, name="top_activation")(x) - - if include_top: - x = layers.GlobalAveragePooling2D(name="avg_pool")(x) - if dropout_rate > 0: - x = layers.Dropout(dropout_rate, name="top_dropout")(x) - imagenet_utils.validate_activation(classifier_activation, weights) - x = layers.Dense( - classes, - activation=classifier_activation, - kernel_initializer=DENSE_KERNEL_INITIALIZER, - bias_initializer=tf.constant_initializer(0), - name="predictions", - )(x) - else: - if pooling == "avg": - x = layers.GlobalAveragePooling2D(name="avg_pool")(x) - elif pooling == "max": - x = layers.GlobalMaxPooling2D(name="max_pool")(x) - - # Ensure that the model takes into account - # any potential predecessors of `input_tensor`. - if input_tensor is not None: - inputs = layer_utils.get_source_inputs(input_tensor) - else: - inputs = img_input - - # Create model. - model = training.Model(inputs, x, name=model_name) - - # Load weights. - if weights == "imagenet": - if include_top: - file_suffix = ".h5" - file_hash = WEIGHTS_HASHES[model_name[-2:]][0] - else: - file_suffix = "_notop.h5" - file_hash = WEIGHTS_HASHES[model_name[-2:]][1] - file_name = model_name + file_suffix - weights_path = data_utils.get_file( - file_name, - BASE_WEIGHTS_PATH + file_name, - cache_subdir="models", - file_hash=file_hash, - ) - model.load_weights(weights_path) - elif weights is not None: - model.load_weights(weights) - - return model - - -@keras_export( - "keras.applications.efficientnet_v2.EfficientNetV2B0", - "keras.applications.EfficientNetV2B0", -) -def EfficientNetV2B0( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", - include_preprocessing=True, -): - return EfficientNetV2( - width_coefficient=1.0, - depth_coefficient=1.0, - default_size=224, - model_name="efficientnetv2-b0", - include_top=include_top, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - include_preprocessing=include_preprocessing, - ) - - -@keras_export( - "keras.applications.efficientnet_v2.EfficientNetV2B1", - "keras.applications.EfficientNetV2B1", -) -def EfficientNetV2B1( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", - include_preprocessing=True, -): - return EfficientNetV2( - width_coefficient=1.0, - depth_coefficient=1.1, - default_size=240, - model_name="efficientnetv2-b1", - include_top=include_top, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - include_preprocessing=include_preprocessing, - ) - - -@keras_export( - "keras.applications.efficientnet_v2.EfficientNetV2B2", - "keras.applications.EfficientNetV2B2", -) -def EfficientNetV2B2( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", - include_preprocessing=True, -): - return EfficientNetV2( - width_coefficient=1.1, - depth_coefficient=1.2, - default_size=260, - model_name="efficientnetv2-b2", - include_top=include_top, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - include_preprocessing=include_preprocessing, - ) - - -@keras_export( - "keras.applications.efficientnet_v2.EfficientNetV2B3", - "keras.applications.EfficientNetV2B3", -) -def EfficientNetV2B3( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", - include_preprocessing=True, -): - return EfficientNetV2( - width_coefficient=1.2, - depth_coefficient=1.4, - default_size=300, - model_name="efficientnetv2-b3", - include_top=include_top, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - include_preprocessing=include_preprocessing, - ) - - -@keras_export( - "keras.applications.efficientnet_v2.EfficientNetV2S", - "keras.applications.EfficientNetV2S", -) -def EfficientNetV2S( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", - include_preprocessing=True, -): - return EfficientNetV2( - width_coefficient=1.0, - depth_coefficient=1.0, - default_size=384, - model_name="efficientnetv2-s", - include_top=include_top, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - include_preprocessing=include_preprocessing, - ) - - -@keras_export( - "keras.applications.efficientnet_v2.EfficientNetV2M", - "keras.applications.EfficientNetV2M", -) -def EfficientNetV2M( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", - include_preprocessing=True, -): - return EfficientNetV2( - width_coefficient=1.0, - depth_coefficient=1.0, - default_size=480, - model_name="efficientnetv2-m", - include_top=include_top, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - include_preprocessing=include_preprocessing, - ) - - -@keras_export( - "keras.applications.efficientnet_v2.EfficientNetV2L", - "keras.applications.EfficientNetV2L", -) -def EfficientNetV2L( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", - include_preprocessing=True, -): - return EfficientNetV2( - width_coefficient=1.0, - depth_coefficient=1.0, - default_size=480, - model_name="efficientnetv2-l", - include_top=include_top, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - include_preprocessing=include_preprocessing, - ) - - -EfficientNetV2B0.__doc__ = BASE_DOCSTRING.format(name="EfficientNetV2B0") -EfficientNetV2B1.__doc__ = BASE_DOCSTRING.format(name="EfficientNetV2B1") -EfficientNetV2B2.__doc__ = BASE_DOCSTRING.format(name="EfficientNetV2B2") -EfficientNetV2B3.__doc__ = BASE_DOCSTRING.format(name="EfficientNetV2B3") -EfficientNetV2S.__doc__ = BASE_DOCSTRING.format(name="EfficientNetV2S") -EfficientNetV2M.__doc__ = BASE_DOCSTRING.format(name="EfficientNetV2M") -EfficientNetV2L.__doc__ = BASE_DOCSTRING.format(name="EfficientNetV2L") - - -@keras_export("keras.applications.efficientnet_v2.preprocess_input") -def preprocess_input(x, data_format=None): - """A placeholder method for backward compatibility. - - The preprocessing logic has been included in the EfficientNetV2 model - implementation. Users are no longer required to call this method to - normalize the input data. This method does nothing and only kept as a - placeholder to align the API surface between old and new version of model. - - Args: - x: A floating point `numpy.array` or a `tf.Tensor`. - data_format: Optional data format of the image tensor/array. `None` means - the global setting `tf.keras.backend.image_data_format()` is used - (unless you changed it, it uses "channels_last"). - Defaults to `None`. - - Returns: - Unchanged `numpy.array` or `tf.Tensor`. - """ - return x - - -@keras_export("keras.applications.efficientnet_v2.decode_predictions") -def decode_predictions(preds, top=5): - return imagenet_utils.decode_predictions(preds, top=top) - - -decode_predictions.__doc__ = imagenet_utils.decode_predictions.__doc__ diff --git a/Archive/keras_applications_mod/models/imagenet_utils.py b/Archive/keras_applications_mod/models/imagenet_utils.py deleted file mode 100644 index 3084d07..0000000 --- a/Archive/keras_applications_mod/models/imagenet_utils.py +++ /dev/null @@ -1,444 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Utilities for ImageNet data preprocessing & prediction decoding.""" - -import json -import warnings - -import numpy as np - -from keras import activations -from keras import backend -from keras.utils import data_utils - -# isort: off -from tensorflow.python.util.tf_export import keras_export - -CLASS_INDEX = None -CLASS_INDEX_PATH = "https://storage.googleapis.com/download.tensorflow.org/" "data/imagenet_class_index.json" - - -PREPROCESS_INPUT_DOC = """ - Preprocesses a tensor or Numpy array encoding a batch of images. - - Usage example with `applications.MobileNet`: - - ```python - i = tf.keras.layers.Input([None, None, 3], dtype = tf.uint8) - x = tf.cast(i, tf.float32) - x = tf.keras.applications.mobilenet.preprocess_input(x) - core = tf.keras.applications.MobileNet() - x = core(x) - model = tf.keras.Model(inputs=[i], outputs=[x]) - - image = tf.image.decode_png(tf.io.read_file('file.png')) - result = model(image) - ``` - - Args: - x: A floating point `numpy.array` or a `tf.Tensor`, 3D or 4D with 3 color - channels, with values in the range [0, 255]. - The preprocessed data are written over the input data - if the data types are compatible. To avoid this - behaviour, `numpy.copy(x)` can be used. - data_format: Optional data format of the image tensor/array. None, means - the global setting `tf.keras.backend.image_data_format()` is used - (unless you changed it, it uses "channels_last").{mode} - Defaults to `None`. - - Returns: - Preprocessed `numpy.array` or a `tf.Tensor` with type `float32`. - {ret} - - Raises: - {error} - """ - -PREPROCESS_INPUT_MODE_DOC = """ - mode: One of "caffe", "tf" or "torch". - - caffe: will convert the images from RGB to BGR, - then will zero-center each color channel with - respect to the ImageNet dataset, - without scaling. - - tf: will scale pixels between -1 and 1, - sample-wise. - - torch: will scale pixels between 0 and 1 and then - will normalize each channel with respect to the - ImageNet dataset. - Defaults to "caffe". - """ - -PREPROCESS_INPUT_DEFAULT_ERROR_DOC = """ - ValueError: In case of unknown `mode` or `data_format` argument.""" - -PREPROCESS_INPUT_ERROR_DOC = """ - ValueError: In case of unknown `data_format` argument.""" - -PREPROCESS_INPUT_RET_DOC_TF = """ - The inputs pixel values are scaled between -1 and 1, sample-wise.""" - -PREPROCESS_INPUT_RET_DOC_TORCH = """ - The input pixels values are scaled between 0 and 1 and each channel is - normalized with respect to the ImageNet dataset.""" - -PREPROCESS_INPUT_RET_DOC_CAFFE = """ - The images are converted from RGB to BGR, then each color channel is - zero-centered with respect to the ImageNet dataset, without scaling.""" - - -@keras_export("keras.applications.imagenet_utils.preprocess_input") -def preprocess_input(x, data_format=None, mode="caffe"): - """Preprocesses a tensor or Numpy array encoding a batch of images.""" - if mode not in {"caffe", "tf", "torch"}: - raise ValueError("Expected mode to be one of `caffe`, `tf` or `torch`. " f"Received: mode={mode}") - - if data_format is None: - data_format = backend.image_data_format() - elif data_format not in {"channels_first", "channels_last"}: - raise ValueError("Expected data_format to be one of `channels_first` or " f"`channels_last`. Received: data_format={data_format}") - - if isinstance(x, np.ndarray): - return _preprocess_numpy_input(x, data_format=data_format, mode=mode) - else: - return _preprocess_symbolic_input(x, data_format=data_format, mode=mode) - - -preprocess_input.__doc__ = PREPROCESS_INPUT_DOC.format( - mode=PREPROCESS_INPUT_MODE_DOC, - ret="", - error=PREPROCESS_INPUT_DEFAULT_ERROR_DOC, -) - - -@keras_export("keras.applications.imagenet_utils.decode_predictions") -def decode_predictions(preds, top=5): - """Decodes the prediction of an ImageNet model. - - Args: - preds: Numpy array encoding a batch of predictions. - top: Integer, how many top-guesses to return. Defaults to 5. - - Returns: - A list of lists of top class prediction tuples - `(class_name, class_description, score)`. - One list of tuples per sample in batch input. - - Raises: - ValueError: In case of invalid shape of the `pred` array - (must be 2D). - """ - global CLASS_INDEX - - if len(preds.shape) != 2 or preds.shape[1] != 1000: - raise ValueError( - "`decode_predictions` expects " - "a batch of predictions " - "(i.e. a 2D array of shape (samples, 1000)). " - "Found array with shape: " + str(preds.shape) - ) - if CLASS_INDEX is None: - fpath = data_utils.get_file( - "imagenet_class_index.json", - CLASS_INDEX_PATH, - cache_subdir="models", - file_hash="c2c37ea517e94d9795004a39431a14cb", - ) - with open(fpath) as f: - CLASS_INDEX = json.load(f) - results = [] - for pred in preds: - top_indices = pred.argsort()[-top:][::-1] - result = [tuple(CLASS_INDEX[str(i)]) + (pred[i],) for i in top_indices] - result.sort(key=lambda x: x[2], reverse=True) - results.append(result) - return results - - -def _preprocess_numpy_input(x, data_format, mode): - """Preprocesses a Numpy array encoding a batch of images. - - Args: - x: Input array, 3D or 4D. - data_format: Data format of the image array. - mode: One of "caffe", "tf" or "torch". - - caffe: will convert the images from RGB to BGR, - then will zero-center each color channel with - respect to the ImageNet dataset, - without scaling. - - tf: will scale pixels between -1 and 1, - sample-wise. - - torch: will scale pixels between 0 and 1 and then - will normalize each channel with respect to the - ImageNet dataset. - - Returns: - Preprocessed Numpy array. - """ - if not issubclass(x.dtype.type, np.floating): - x = x.astype(backend.floatx(), copy=False) - - if mode == "tf": - x /= 127.5 - x -= 1.0 - return x - elif mode == "torch": - x /= 255.0 - mean = [0.485, 0.456, 0.406] - std = [0.229, 0.224, 0.225] - else: - if data_format == "channels_first": - # 'RGB'->'BGR' - if x.ndim == 3: - x = x[::-1, ...] - else: - x = x[:, ::-1, ...] - else: - # 'RGB'->'BGR' - x = x[..., ::-1] - mean = [103.939, 116.779, 123.68] - std = None - - # Zero-center by mean pixel - if data_format == "channels_first": - if x.ndim == 3: - x[0, :, :] -= mean[0] - x[1, :, :] -= mean[1] - x[2, :, :] -= mean[2] - if std is not None: - x[0, :, :] /= std[0] - x[1, :, :] /= std[1] - x[2, :, :] /= std[2] - else: - x[:, 0, :, :] -= mean[0] - x[:, 1, :, :] -= mean[1] - x[:, 2, :, :] -= mean[2] - if std is not None: - x[:, 0, :, :] /= std[0] - x[:, 1, :, :] /= std[1] - x[:, 2, :, :] /= std[2] - else: - x[..., 0] -= mean[0] - x[..., 1] -= mean[1] - x[..., 2] -= mean[2] - if std is not None: - x[..., 0] /= std[0] - x[..., 1] /= std[1] - x[..., 2] /= std[2] - return x - - -def _preprocess_symbolic_input(x, data_format, mode): - """Preprocesses a tensor encoding a batch of images. - - Args: - x: Input tensor, 3D or 4D. - data_format: Data format of the image tensor. - mode: One of "caffe", "tf" or "torch". - - caffe: will convert the images from RGB to BGR, - then will zero-center each color channel with - respect to the ImageNet dataset, - without scaling. - - tf: will scale pixels between -1 and 1, - sample-wise. - - torch: will scale pixels between 0 and 1 and then - will normalize each channel with respect to the - ImageNet dataset. - - Returns: - Preprocessed tensor. - """ - if mode == "tf": - x /= 127.5 - x -= 1.0 - return x - elif mode == "torch": - x /= 255.0 - mean = [0.485, 0.456, 0.406] - std = [0.229, 0.224, 0.225] - else: - if data_format == "channels_first": - # 'RGB'->'BGR' - if backend.ndim(x) == 3: - x = x[::-1, ...] - else: - x = x[:, ::-1, ...] - else: - # 'RGB'->'BGR' - x = x[..., ::-1] - mean = [103.939, 116.779, 123.68] - std = None - - mean_tensor = backend.constant(-np.array(mean)) - - # Zero-center by mean pixel - if backend.dtype(x) != backend.dtype(mean_tensor): - x = backend.bias_add( - x, - backend.cast(mean_tensor, backend.dtype(x)), - data_format=data_format, - ) - else: - x = backend.bias_add(x, mean_tensor, data_format) - if std is not None: - std_tensor = backend.constant(np.array(std), dtype=backend.dtype(x)) - if data_format == "channels_first": - std_tensor = backend.reshape(std_tensor, (-1, 1, 1)) - x /= std_tensor - return x - - -def obtain_input_shape( - input_shape, - default_size, - min_size, - data_format, - require_flatten, - weights=None, -): - """Internal utility to compute/validate a model's input shape. - - Args: - input_shape: Either None (will return the default network input shape), - or a user-provided shape to be validated. - default_size: Default input width/height for the model. - min_size: Minimum input width/height accepted by the model. - data_format: Image data format to use. - require_flatten: Whether the model is expected to - be linked to a classifier via a Flatten layer. - weights: One of `None` (random initialization) - or 'imagenet' (pre-training on ImageNet). - If weights='imagenet' input channels must be equal to 3. - - Returns: - An integer shape tuple (may include None entries). - - Raises: - ValueError: In case of invalid argument values. - """ - if weights != "imagenet" and input_shape and len(input_shape) == 3: - if data_format == "channels_first": - if input_shape[0] not in {1, 3}: - warnings.warn( - "This model usually expects 1 or 3 input channels. " - "However, it was passed an input_shape with " + str(input_shape[0]) + " input channels.", - stacklevel=2, - ) - default_shape = (input_shape[0], default_size, default_size) - else: - if input_shape[-1] not in {1, 3}: - warnings.warn( - "This model usually expects 1 or 3 input channels. " - "However, it was passed an input_shape with " + str(input_shape[-1]) + " input channels.", - stacklevel=2, - ) - default_shape = (default_size, default_size, input_shape[-1]) - else: - if data_format == "channels_first": - default_shape = (3, default_size, default_size) - else: - default_shape = (default_size, default_size, 3) - if weights == "imagenet" and require_flatten: - if input_shape is not None: - if input_shape != default_shape: - raise ValueError( - "When setting `include_top=True` " - "and loading `imagenet` weights, " - f"`input_shape` should be {default_shape}. " - f"Received: input_shape={input_shape}" - ) - return default_shape - if input_shape: - if data_format == "channels_first": - if input_shape is not None: - if len(input_shape) != 3: - raise ValueError("`input_shape` must be a tuple of three integers.") - if input_shape[0] != 3 and weights == "imagenet": - raise ValueError("The input must have 3 channels; Received " f"`input_shape={input_shape}`") - if (input_shape[1] is not None and input_shape[1] < min_size) or (input_shape[2] is not None and input_shape[2] < min_size): - raise ValueError(f"Input size must be at least {min_size}" f"x{min_size}; Received: " f"input_shape={input_shape}") - else: - if input_shape is not None: - if len(input_shape) != 3: - raise ValueError("`input_shape` must be a tuple of three integers.") - if input_shape[-1] != 3 and weights == "imagenet": - raise ValueError("The input must have 3 channels; Received " f"`input_shape={input_shape}`") - if (input_shape[0] is not None and input_shape[0] < min_size) or (input_shape[1] is not None and input_shape[1] < min_size): - raise ValueError("Input size must be at least " f"{min_size}x{min_size}; Received: " f"input_shape={input_shape}") - else: - if require_flatten: - input_shape = default_shape - else: - if data_format == "channels_first": - input_shape = (3, None, None) - else: - input_shape = (None, None, 3) - if require_flatten: - if None in input_shape: - raise ValueError( - "If `include_top` is True, " "you should specify a static `input_shape`. " f"Received: input_shape={input_shape}" - ) - return input_shape - - -def correct_pad(inputs, kernel_size): - """Returns a tuple for zero-padding for 2D convolution with downsampling. - - Args: - inputs: Input tensor. - kernel_size: An integer or tuple/list of 2 integers. - - Returns: - A tuple. - """ - img_dim = 2 if backend.image_data_format() == "channels_first" else 1 - input_size = backend.int_shape(inputs)[img_dim : (img_dim + 2)] - if isinstance(kernel_size, int): - kernel_size = (kernel_size, kernel_size) - if input_size[0] is None: - adjust = (1, 1) - else: - adjust = (1 - input_size[0] % 2, 1 - input_size[1] % 2) - correct = (kernel_size[0] // 2, kernel_size[1] // 2) - return ( - (correct[0] - adjust[0], correct[0]), - (correct[1] - adjust[1], correct[1]), - ) - - -def validate_activation(classifier_activation, weights): - """validates that the classifer_activation is compatible with the weights. - - Args: - classifier_activation: str or callable activation function - weights: The pretrained weights to load. - - Raises: - ValueError: if an activation other than `None` or `softmax` are used with - pretrained weights. - """ - if weights is None: - return - - classifier_activation = activations.get(classifier_activation) - if classifier_activation not in { - activations.get("softmax"), - activations.get(None), - }: - raise ValueError( - "Only `None` and `softmax` activations are allowed " - "for the `classifier_activation` argument when using " - "pretrained weights, with `include_top=True`; Received: " - f"classifier_activation={classifier_activation}" - ) diff --git a/Archive/keras_applications_mod/models/imagenet_utils_test.py b/Archive/keras_applications_mod/models/imagenet_utils_test.py deleted file mode 100644 index 332f421..0000000 --- a/Archive/keras_applications_mod/models/imagenet_utils_test.py +++ /dev/null @@ -1,305 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for imagenet_utils.""" - -import numpy as np -import tensorflow.compat.v2 as tf -from absl.testing import parameterized - -import keras -from keras.applications import imagenet_utils as utils -from keras.mixed_precision.policy import set_global_policy -from keras.testing_infra import test_combinations - - -class TestImageNetUtils(test_combinations.TestCase): - def test_preprocess_input(self): - # Test invalid mode check - x = np.random.uniform(0, 255, (10, 10, 3)) - with self.assertRaises(ValueError): - utils.preprocess_input(x, mode="some_unknown_mode") - - # Test image batch with float and int image input - x = np.random.uniform(0, 255, (2, 10, 10, 3)) - xint = x.astype("int32") - self.assertEqual(utils.preprocess_input(x).shape, x.shape) - self.assertEqual(utils.preprocess_input(xint).shape, xint.shape) - - out1 = utils.preprocess_input(x, "channels_last") - out1int = utils.preprocess_input(xint, "channels_last") - out2 = utils.preprocess_input(np.transpose(x, (0, 3, 1, 2)), "channels_first") - out2int = utils.preprocess_input(np.transpose(xint, (0, 3, 1, 2)), "channels_first") - self.assertAllClose(out1, out2.transpose(0, 2, 3, 1)) - self.assertAllClose(out1int, out2int.transpose(0, 2, 3, 1)) - - # Test single image - x = np.random.uniform(0, 255, (10, 10, 3)) - xint = x.astype("int32") - self.assertEqual(utils.preprocess_input(x).shape, x.shape) - self.assertEqual(utils.preprocess_input(xint).shape, xint.shape) - - out1 = utils.preprocess_input(x, "channels_last") - out1int = utils.preprocess_input(xint, "channels_last") - out2 = utils.preprocess_input(np.transpose(x, (2, 0, 1)), "channels_first") - out2int = utils.preprocess_input(np.transpose(xint, (2, 0, 1)), "channels_first") - self.assertAllClose(out1, out2.transpose(1, 2, 0)) - self.assertAllClose(out1int, out2int.transpose(1, 2, 0)) - - # Test that writing over the input data works predictably - for mode in ["torch", "tf"]: - x = np.random.uniform(0, 255, (2, 10, 10, 3)) - xint = x.astype("int") - x2 = utils.preprocess_input(x, mode=mode) - xint2 = utils.preprocess_input(xint) - self.assertAllClose(x, x2) - self.assertNotEqual(xint.astype("float").max(), xint2.max()) - - # Caffe mode works differently from the others - x = np.random.uniform(0, 255, (2, 10, 10, 3)) - xint = x.astype("int") - x2 = utils.preprocess_input(x, data_format="channels_last", mode="caffe") - xint2 = utils.preprocess_input(xint) - self.assertAllClose(x, x2[..., ::-1]) - self.assertNotEqual(xint.astype("float").max(), xint2.max()) - - @parameterized.named_parameters([ - {"testcase_name": "mode_torch", "mode": "torch"}, - {"testcase_name": "mode_tf", "mode": "tf"}, - {"testcase_name": "mode_caffe", "mode": "caffe"}, - ]) - def test_preprocess_input_symbolic(self, mode): - # Test image batch - x = np.random.uniform(0, 255, (2, 10, 10, 3)) - inputs = keras.layers.Input(shape=x.shape[1:]) - outputs = keras.layers.Lambda( - lambda x: utils.preprocess_input(x, mode=mode), - output_shape=x.shape[1:], - )(inputs) - model = keras.Model(inputs, outputs) - self.assertEqual(model.predict(x).shape, x.shape) - - outputs1 = keras.layers.Lambda( - lambda x: utils.preprocess_input(x, "channels_last", mode=mode), - output_shape=x.shape[1:], - )(inputs) - model1 = keras.Model(inputs, outputs1) - out1 = model1.predict(x) - x2 = np.transpose(x, (0, 3, 1, 2)) - inputs2 = keras.layers.Input(shape=x2.shape[1:]) - outputs2 = keras.layers.Lambda( - lambda x: utils.preprocess_input(x, "channels_first", mode=mode), - output_shape=x2.shape[1:], - )(inputs2) - model2 = keras.Model(inputs2, outputs2) - out2 = model2.predict(x2) - self.assertAllClose(out1, out2.transpose(0, 2, 3, 1)) - - # Test single image - x = np.random.uniform(0, 255, (10, 10, 3)) - inputs = keras.layers.Input(shape=x.shape) - outputs = keras.layers.Lambda(lambda x: utils.preprocess_input(x, mode=mode), output_shape=x.shape)(inputs) - model = keras.Model(inputs, outputs) - self.assertEqual(model.predict(x[np.newaxis])[0].shape, x.shape) - - outputs1 = keras.layers.Lambda( - lambda x: utils.preprocess_input(x, "channels_last", mode=mode), - output_shape=x.shape, - )(inputs) - model1 = keras.Model(inputs, outputs1) - out1 = model1.predict(x[np.newaxis])[0] - x2 = np.transpose(x, (2, 0, 1)) - inputs2 = keras.layers.Input(shape=x2.shape) - outputs2 = keras.layers.Lambda( - lambda x: utils.preprocess_input(x, "channels_first", mode=mode), - output_shape=x2.shape, - )(inputs2) - model2 = keras.Model(inputs2, outputs2) - out2 = model2.predict(x2[np.newaxis])[0] - self.assertAllClose(out1, out2.transpose(1, 2, 0)) - - @parameterized.named_parameters([ - {"testcase_name": "mode_torch", "mode": "torch"}, - {"testcase_name": "mode_tf", "mode": "tf"}, - {"testcase_name": "mode_caffe", "mode": "caffe"}, - ]) - def test_preprocess_input_symbolic_mixed_precision(self, mode): - if not tf.__internal__.tf2.enabled(): - self.skipTest("The global policy can only be tested in TensorFlow 2") - set_global_policy("mixed_float16") - shape = (20, 20, 3) - inputs = keras.layers.Input(shape=shape) - try: - keras.layers.Lambda( - lambda x: utils.preprocess_input(x, mode=mode), - output_shape=shape, - )(inputs) - finally: - set_global_policy("float32") - - @parameterized.named_parameters([ - { - "testcase_name": "channels_last_format", - "data_format": "channels_last", - }, - { - "testcase_name": "channels_first_format", - "data_format": "channels_first", - }, - ]) - def test_obtain_input_shape(self, data_format): - # input_shape and default_size are not identical. - with self.assertRaises(ValueError): - utils.obtain_input_shape( - input_shape=(224, 224, 3), - default_size=299, - min_size=139, - data_format="channels_last", - require_flatten=True, - weights="imagenet", - ) - - # Test invalid use cases - - shape = (139, 139) - if data_format == "channels_last": - input_shape = shape + (99,) - else: - input_shape = (99,) + shape - - # input_shape is smaller than min_size. - shape = (100, 100) - if data_format == "channels_last": - input_shape = shape + (3,) - else: - input_shape = (3,) + shape - with self.assertRaises(ValueError): - utils.obtain_input_shape( - input_shape=input_shape, - default_size=None, - min_size=139, - data_format=data_format, - require_flatten=False, - ) - - # shape is 1D. - shape = (100,) - if data_format == "channels_last": - input_shape = shape + (3,) - else: - input_shape = (3,) + shape - with self.assertRaises(ValueError): - utils.obtain_input_shape( - input_shape=input_shape, - default_size=None, - min_size=139, - data_format=data_format, - require_flatten=False, - ) - - # the number of channels is 5 not 3. - shape = (100, 100) - if data_format == "channels_last": - input_shape = shape + (5,) - else: - input_shape = (5,) + shape - with self.assertRaises(ValueError): - utils.obtain_input_shape( - input_shape=input_shape, - default_size=None, - min_size=139, - data_format=data_format, - require_flatten=False, - ) - - # require_flatten=True with dynamic input shape. - with self.assertRaises(ValueError): - utils.obtain_input_shape( - input_shape=None, - default_size=None, - min_size=139, - data_format="channels_first", - require_flatten=True, - ) - - # test include top - self.assertEqual( - utils.obtain_input_shape( - input_shape=(3, 200, 200), - default_size=None, - min_size=139, - data_format="channels_first", - require_flatten=True, - ), - (3, 200, 200), - ) - - self.assertEqual( - utils.obtain_input_shape( - input_shape=None, - default_size=None, - min_size=139, - data_format="channels_last", - require_flatten=False, - ), - (None, None, 3), - ) - - self.assertEqual( - utils.obtain_input_shape( - input_shape=None, - default_size=None, - min_size=139, - data_format="channels_first", - require_flatten=False, - ), - (3, None, None), - ) - - self.assertEqual( - utils.obtain_input_shape( - input_shape=None, - default_size=None, - min_size=139, - data_format="channels_last", - require_flatten=False, - ), - (None, None, 3), - ) - - self.assertEqual( - utils.obtain_input_shape( - input_shape=(150, 150, 3), - default_size=None, - min_size=139, - data_format="channels_last", - require_flatten=False, - ), - (150, 150, 3), - ) - - self.assertEqual( - utils.obtain_input_shape( - input_shape=(3, None, None), - default_size=None, - min_size=139, - data_format="channels_first", - require_flatten=False, - ), - (3, None, None), - ) - - -if __name__ == "__main__": - tf.test.main() diff --git a/Archive/keras_applications_mod/models/inception_resnet_v2.py b/Archive/keras_applications_mod/models/inception_resnet_v2.py deleted file mode 100644 index e2334da..0000000 --- a/Archive/keras_applications_mod/models/inception_resnet_v2.py +++ /dev/null @@ -1,409 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Inception-ResNet V2 model for Keras. - -Reference: - - [Inception-v4, Inception-ResNet and the Impact of - Residual Connections on Learning](https://arxiv.org/abs/1602.07261) - (AAAI 2017) -""" - -import tensorflow.compat.v2 as tf - -import keras -from keras import backend -from keras import layers as keras_layers -from keras.applications import imagenet_utils -from keras.engine import training -from keras.layers import VersionAwareLayers -from keras.utils import data_utils -from keras.utils import layer_utils - -# isort: off -from tensorflow.python.util.tf_export import keras_export - -BASE_WEIGHT_URL = "https://storage.googleapis.com/tensorflow/" "keras-applications/inception_resnet_v2/" -layers = None - - -@keras_export( - "keras.applications.inception_resnet_v2.InceptionResNetV2", - "keras.applications.InceptionResNetV2", -) -def InceptionResNetV2( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", - **kwargs, -): - """Instantiates the Inception-ResNet v2 architecture. - - Reference: - - [Inception-v4, Inception-ResNet and the Impact of - Residual Connections on Learning](https://arxiv.org/abs/1602.07261) - (AAAI 2017) - - This function returns a Keras image classification model, - optionally loaded with weights pre-trained on ImageNet. - - For image classification use cases, see - [this page for detailed examples]( - https://keras.io/api/applications/#usage-examples-for-image-classification-models). - - For transfer learning use cases, make sure to read the - [guide to transfer learning & fine-tuning]( - https://keras.io/guides/transfer_learning/). - - Note: each Keras Application expects a specific kind of input preprocessing. - For InceptionResNetV2, call - `tf.keras.applications.inception_resnet_v2.preprocess_input` - on your inputs before passing them to the model. - `inception_resnet_v2.preprocess_input` - will scale input pixels between -1 and 1. - - Args: - include_top: whether to include the fully-connected - layer at the top of the network. - weights: one of `None` (random initialization), - 'imagenet' (pre-training on ImageNet), - or the path to the weights file to be loaded. - input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) - to use as image input for the model. - input_shape: optional shape tuple, only to be specified - if `include_top` is `False` (otherwise the input shape - has to be `(299, 299, 3)` (with `'channels_last'` data format) - or `(3, 299, 299)` (with `'channels_first'` data format). - It should have exactly 3 inputs channels, - and width and height should be no smaller than 75. - E.g. `(150, 150, 3)` would be one valid value. - pooling: Optional pooling mode for feature extraction - when `include_top` is `False`. - - `None` means that the output of the model will be - the 4D tensor output of the last convolutional block. - - `'avg'` means that global average pooling - will be applied to the output of the - last convolutional block, and thus - the output of the model will be a 2D tensor. - - `'max'` means that global max pooling will be applied. - classes: optional number of classes to classify images - into, only to be specified if `include_top` is `True`, and - if no `weights` argument is specified. - classifier_activation: A `str` or callable. The activation function to use - on the "top" layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" layer. - When loading pretrained weights, `classifier_activation` can only - be `None` or `"softmax"`. - **kwargs: For backwards compatibility only. - - Returns: - A `keras.Model` instance. - """ - global layers - if "layers" in kwargs: - layers = kwargs.pop("layers") - else: - layers = VersionAwareLayers() - if kwargs: - raise ValueError(f"Unknown argument(s): {kwargs}") - if not (weights in {"imagenet", None} or tf.io.gfile.exists(weights)): - raise ValueError( - "The `weights` argument should be either " - "`None` (random initialization), `imagenet` " - "(pre-training on ImageNet), " - "or the path to the weights file to be loaded." - ) - - if weights == "imagenet" and include_top and classes != 1000: - raise ValueError('If using `weights` as `"imagenet"` with `include_top`' " as true, `classes` should be 1000") - - # Determine proper input shape - input_shape = imagenet_utils.obtain_input_shape( - input_shape, - default_size=299, - min_size=75, - data_format=backend.image_data_format(), - require_flatten=include_top, - weights=weights, - ) - - if input_tensor is None: - img_input = layers.Input(shape=input_shape) - else: - if not backend.is_keras_tensor(input_tensor): - img_input = layers.Input(tensor=input_tensor, shape=input_shape) - else: - img_input = input_tensor - - # Stem block: 35 x 35 x 192 - x = conv2d_bn(img_input, 32, 3, strides=2, padding="valid") - x = conv2d_bn(x, 32, 3, padding="valid") - x = conv2d_bn(x, 64, 3) - x = layers.MaxPooling2D(3, strides=2)(x) - x = conv2d_bn(x, 80, 1, padding="valid") - x = conv2d_bn(x, 192, 3, padding="valid") - x = layers.MaxPooling2D(3, strides=2)(x) - - # Mixed 5b (Inception-A block): 35 x 35 x 320 - branch_0 = conv2d_bn(x, 96, 1) - branch_1 = conv2d_bn(x, 48, 1) - branch_1 = conv2d_bn(branch_1, 64, 5) - branch_2 = conv2d_bn(x, 64, 1) - branch_2 = conv2d_bn(branch_2, 96, 3) - branch_2 = conv2d_bn(branch_2, 96, 3) - branch_pool = layers.AveragePooling2D(3, strides=1, padding="same")(x) - branch_pool = conv2d_bn(branch_pool, 64, 1) - branches = [branch_0, branch_1, branch_2, branch_pool] - channel_axis = 1 if backend.image_data_format() == "channels_first" else 3 - x = layers.Concatenate(axis=channel_axis, name="mixed_5b")(branches) - - # 10x block35 (Inception-ResNet-A block): 35 x 35 x 320 - for block_idx in range(1, 11): - x = inception_resnet_block(x, scale=0.17, block_type="block35", block_idx=block_idx) - - # Mixed 6a (Reduction-A block): 17 x 17 x 1088 - branch_0 = conv2d_bn(x, 384, 3, strides=2, padding="valid") - branch_1 = conv2d_bn(x, 256, 1) - branch_1 = conv2d_bn(branch_1, 256, 3) - branch_1 = conv2d_bn(branch_1, 384, 3, strides=2, padding="valid") - branch_pool = layers.MaxPooling2D(3, strides=2, padding="valid")(x) - branches = [branch_0, branch_1, branch_pool] - x = layers.Concatenate(axis=channel_axis, name="mixed_6a")(branches) - - # 20x block17 (Inception-ResNet-B block): 17 x 17 x 1088 - for block_idx in range(1, 21): - x = inception_resnet_block(x, scale=0.1, block_type="block17", block_idx=block_idx) - - # Mixed 7a (Reduction-B block): 8 x 8 x 2080 - branch_0 = conv2d_bn(x, 256, 1) - branch_0 = conv2d_bn(branch_0, 384, 3, strides=2, padding="valid") - branch_1 = conv2d_bn(x, 256, 1) - branch_1 = conv2d_bn(branch_1, 288, 3, strides=2, padding="valid") - branch_2 = conv2d_bn(x, 256, 1) - branch_2 = conv2d_bn(branch_2, 288, 3) - branch_2 = conv2d_bn(branch_2, 320, 3, strides=2, padding="valid") - branch_pool = layers.MaxPooling2D(3, strides=2, padding="valid")(x) - branches = [branch_0, branch_1, branch_2, branch_pool] - x = layers.Concatenate(axis=channel_axis, name="mixed_7a")(branches) - - # 10x block8 (Inception-ResNet-C block): 8 x 8 x 2080 - for block_idx in range(1, 10): - x = inception_resnet_block(x, scale=0.2, block_type="block8", block_idx=block_idx) - x = inception_resnet_block(x, scale=1.0, activation=None, block_type="block8", block_idx=10) - - # Final convolution block: 8 x 8 x 1536 - x = conv2d_bn(x, 1536, 1, name="conv_7b") - - if include_top: - # Classification block - x = layers.GlobalAveragePooling2D(name="avg_pool")(x) - imagenet_utils.validate_activation(classifier_activation, weights) - x = layers.Dense(classes, activation=classifier_activation, name="predictions")(x) - else: - if pooling == "avg": - x = layers.GlobalAveragePooling2D()(x) - elif pooling == "max": - x = layers.GlobalMaxPooling2D()(x) - - # Ensure that the model takes into account - # any potential predecessors of `input_tensor`. - if input_tensor is not None: - inputs = layer_utils.get_source_inputs(input_tensor) - else: - inputs = img_input - - # Create model. - model = training.Model(inputs, x, name="inception_resnet_v2") - - # Load weights. - if weights == "imagenet": - if include_top: - fname = "inception_resnet_v2_weights_tf_dim_ordering_tf_kernels.h5" - weights_path = data_utils.get_file( - fname, - BASE_WEIGHT_URL + fname, - cache_subdir="models", - file_hash="e693bd0210a403b3192acc6073ad2e96", - ) - else: - fname = "inception_resnet_v2_weights_" "tf_dim_ordering_tf_kernels_notop.h5" - weights_path = data_utils.get_file( - fname, - BASE_WEIGHT_URL + fname, - cache_subdir="models", - file_hash="d19885ff4a710c122648d3b5c3b684e4", - ) - model.load_weights(weights_path) - elif weights is not None: - model.load_weights(weights) - - return model - - -def conv2d_bn( - x, - filters, - kernel_size, - strides=1, - padding="same", - activation="relu", - use_bias=False, - name=None, -): - """Utility function to apply conv + BN. - - Args: - x: input tensor. - filters: filters in `Conv2D`. - kernel_size: kernel size as in `Conv2D`. - strides: strides in `Conv2D`. - padding: padding mode in `Conv2D`. - activation: activation in `Conv2D`. - use_bias: whether to use a bias in `Conv2D`. - name: name of the ops; will become `name + '_ac'` for the activation - and `name + '_bn'` for the batch norm layer. - - Returns: - Output tensor after applying `Conv2D` and `BatchNormalization`. - """ - x = layers.Conv2D( - filters, - kernel_size, - strides=strides, - padding=padding, - use_bias=use_bias, - name=name, - )(x) - if not use_bias: - bn_axis = 1 if backend.image_data_format() == "channels_first" else 3 - bn_name = None if name is None else name + "_bn" - x = layers.BatchNormalization(axis=bn_axis, scale=False, name=bn_name)(x) - if activation is not None: - ac_name = None if name is None else name + "_ac" - x = layers.Activation(activation, name=ac_name)(x) - return x - - -@keras.utils.register_keras_serializable() -class CustomScaleLayer(keras_layers.Layer): - def __init__(self, scale, **kwargs): - super().__init__(**kwargs) - self.scale = scale - - def get_config(self): - config = super().get_config() - config.update({"scale": self.scale}) - return config - - def call(self, inputs): - return inputs[0] + inputs[1] * self.scale - - -def inception_resnet_block(x, scale, block_type, block_idx, activation="relu"): - """Adds an Inception-ResNet block. - - This function builds 3 types of Inception-ResNet blocks mentioned - in the paper, controlled by the `block_type` argument (which is the - block name used in the official TF-slim implementation): - - Inception-ResNet-A: `block_type='block35'` - - Inception-ResNet-B: `block_type='block17'` - - Inception-ResNet-C: `block_type='block8'` - - Args: - x: input tensor. - scale: scaling factor to scale the residuals (i.e., the output of passing - `x` through an inception module) before adding them to the shortcut - branch. Let `r` be the output from the residual branch, the output of - this block will be `x + scale * r`. - block_type: `'block35'`, `'block17'` or `'block8'`, determines the network - structure in the residual branch. - block_idx: an `int` used for generating layer names. The Inception-ResNet - blocks are repeated many times in this network. We use `block_idx` to - identify each of the repetitions. For example, the first - Inception-ResNet-A block will have `block_type='block35', block_idx=0`, - and the layer names will have a common prefix `'block35_0'`. - activation: activation function to use at the end of the block (see - [activations](../activations.md)). When `activation=None`, no activation - is applied - (i.e., "linear" activation: `a(x) = x`). - - Returns: - Output tensor for the block. - - Raises: - ValueError: if `block_type` is not one of `'block35'`, - `'block17'` or `'block8'`. - """ - if block_type == "block35": - branch_0 = conv2d_bn(x, 32, 1) - branch_1 = conv2d_bn(x, 32, 1) - branch_1 = conv2d_bn(branch_1, 32, 3) - branch_2 = conv2d_bn(x, 32, 1) - branch_2 = conv2d_bn(branch_2, 48, 3) - branch_2 = conv2d_bn(branch_2, 64, 3) - branches = [branch_0, branch_1, branch_2] - elif block_type == "block17": - branch_0 = conv2d_bn(x, 192, 1) - branch_1 = conv2d_bn(x, 128, 1) - branch_1 = conv2d_bn(branch_1, 160, [1, 7]) - branch_1 = conv2d_bn(branch_1, 192, [7, 1]) - branches = [branch_0, branch_1] - elif block_type == "block8": - branch_0 = conv2d_bn(x, 192, 1) - branch_1 = conv2d_bn(x, 192, 1) - branch_1 = conv2d_bn(branch_1, 224, [1, 3]) - branch_1 = conv2d_bn(branch_1, 256, [3, 1]) - branches = [branch_0, branch_1] - else: - raise ValueError("Unknown Inception-ResNet block type. " 'Expects "block35", "block17" or "block8", ' "but got: " + str(block_type)) - - block_name = block_type + "_" + str(block_idx) - channel_axis = 1 if backend.image_data_format() == "channels_first" else 3 - mixed = layers.Concatenate(axis=channel_axis, name=block_name + "_mixed")(branches) - up = conv2d_bn( - mixed, - backend.int_shape(x)[channel_axis], - 1, - activation=None, - use_bias=True, - name=block_name + "_conv", - ) - - x = CustomScaleLayer(scale)([x, up]) - if activation is not None: - x = layers.Activation(activation, name=block_name + "_ac")(x) - return x - - -@keras_export("keras.applications.inception_resnet_v2.preprocess_input") -def preprocess_input(x, data_format=None): - return imagenet_utils.preprocess_input(x, data_format=data_format, mode="tf") - - -@keras_export("keras.applications.inception_resnet_v2.decode_predictions") -def decode_predictions(preds, top=5): - return imagenet_utils.decode_predictions(preds, top=top) - - -preprocess_input.__doc__ = imagenet_utils.PREPROCESS_INPUT_DOC.format( - mode="", - ret=imagenet_utils.PREPROCESS_INPUT_RET_DOC_TF, - error=imagenet_utils.PREPROCESS_INPUT_ERROR_DOC, -) -decode_predictions.__doc__ = imagenet_utils.decode_predictions.__doc__ diff --git a/Archive/keras_applications_mod/models/inception_v3.py b/Archive/keras_applications_mod/models/inception_v3.py deleted file mode 100644 index 7707717..0000000 --- a/Archive/keras_applications_mod/models/inception_v3.py +++ /dev/null @@ -1,429 +0,0 @@ -# Copyright 2015 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Inception V3 model for Keras. - -Reference: - - [Rethinking the Inception Architecture for Computer Vision]( - http://arxiv.org/abs/1512.00567) (CVPR 2016) -""" - -import tensorflow.compat.v2 as tf - -from keras import backend -from keras.applications import imagenet_utils -from keras.engine import training -from keras.layers import VersionAwareLayers -from keras.utils import data_utils -from keras.utils import layer_utils - -# isort: off -from tensorflow.python.util.tf_export import keras_export - -WEIGHTS_PATH = ( - "https://storage.googleapis.com/tensorflow/keras-applications/" "inception_v3/inception_v3_weights_tf_dim_ordering_tf_kernels.h5" -) -WEIGHTS_PATH_NO_TOP = ( - "https://storage.googleapis.com/tensorflow/keras-applications/" "inception_v3/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5" -) - -layers = VersionAwareLayers() - - -@keras_export( - "keras.applications.inception_v3.InceptionV3", - "keras.applications.InceptionV3", -) -def InceptionV3( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - """Instantiates the Inception v3 architecture. - - Reference: - - [Rethinking the Inception Architecture for Computer Vision]( - http://arxiv.org/abs/1512.00567) (CVPR 2016) - - This function returns a Keras image classification model, - optionally loaded with weights pre-trained on ImageNet. - - For image classification use cases, see - [this page for detailed examples]( - https://keras.io/api/applications/#usage-examples-for-image-classification-models). - - For transfer learning use cases, make sure to read the - [guide to transfer learning & fine-tuning]( - https://keras.io/guides/transfer_learning/). - - Note: each Keras Application expects a specific kind of input preprocessing. - For `InceptionV3`, call - `tf.keras.applications.inception_v3.preprocess_input` on your inputs before - passing them to the model. `inception_v3.preprocess_input` will scale input - pixels between -1 and 1. - - Args: - include_top: Boolean, whether to include the fully-connected - layer at the top, as the last layer of the network. Defaults to `True`. - weights: One of `None` (random initialization), - `imagenet` (pre-training on ImageNet), - or the path to the weights file to be loaded. Defaults to `imagenet`. - input_tensor: Optional Keras tensor (i.e. output of `layers.Input()`) - to use as image input for the model. `input_tensor` is useful for - sharing inputs between multiple different networks. Defaults to `None`. - input_shape: Optional shape tuple, only to be specified - if `include_top` is False (otherwise the input shape - has to be `(299, 299, 3)` (with `channels_last` data format) - or `(3, 299, 299)` (with `channels_first` data format). - It should have exactly 3 inputs channels, - and width and height should be no smaller than 75. - E.g. `(150, 150, 3)` would be one valid value. - `input_shape` will be ignored if the `input_tensor` is provided. - pooling: Optional pooling mode for feature extraction - when `include_top` is `False`. - - `None` (default) means that the output of the model will be - the 4D tensor output of the last convolutional block. - - `avg` means that global average pooling - will be applied to the output of the - last convolutional block, and thus - the output of the model will be a 2D tensor. - - `max` means that global max pooling will be applied. - classes: optional number of classes to classify images - into, only to be specified if `include_top` is True, and - if no `weights` argument is specified. Defaults to 1000. - classifier_activation: A `str` or callable. The activation function to use - on the "top" layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" layer. - When loading pretrained weights, `classifier_activation` can only - be `None` or `"softmax"`. - - Returns: - A `keras.Model` instance. - """ - if not (weights in {"imagenet", None} or tf.io.gfile.exists(weights)): - raise ValueError( - "The `weights` argument should be either " - "`None` (random initialization), `imagenet` " - "(pre-training on ImageNet), " - "or the path to the weights file to be loaded; " - f"Received: weights={weights}" - ) - - if weights == "imagenet" and include_top and classes != 1000: - raise ValueError( - 'If using `weights` as `"imagenet"` with `include_top` ' "as true, `classes` should be 1000; " f"Received classes={classes}" - ) - - # Determine proper input shape - input_shape = imagenet_utils.obtain_input_shape( - input_shape, - default_size=299, - min_size=75, - data_format=backend.image_data_format(), - require_flatten=include_top, - weights=weights, - ) - - if input_tensor is None: - img_input = layers.Input(shape=input_shape) - else: - if not backend.is_keras_tensor(input_tensor): - img_input = layers.Input(tensor=input_tensor, shape=input_shape) - else: - img_input = input_tensor - - if backend.image_data_format() == "channels_first": - channel_axis = 1 - else: - channel_axis = 3 - - x = conv2d_bn(img_input, 32, 3, 3, strides=(2, 2), padding="valid") - x = conv2d_bn(x, 32, 3, 3, padding="valid") - x = conv2d_bn(x, 64, 3, 3) - x = layers.MaxPooling2D((3, 3), strides=(2, 2))(x) - - x = conv2d_bn(x, 80, 1, 1, padding="valid") - x = conv2d_bn(x, 192, 3, 3, padding="valid") - x = layers.MaxPooling2D((3, 3), strides=(2, 2))(x) - - # mixed 0: 35 x 35 x 256 - branch1x1 = conv2d_bn(x, 64, 1, 1) - - branch5x5 = conv2d_bn(x, 48, 1, 1) - branch5x5 = conv2d_bn(branch5x5, 64, 5, 5) - - branch3x3dbl = conv2d_bn(x, 64, 1, 1) - branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) - branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) - - branch_pool = layers.AveragePooling2D((3, 3), strides=(1, 1), padding="same")(x) - branch_pool = conv2d_bn(branch_pool, 32, 1, 1) - x = layers.concatenate( - [branch1x1, branch5x5, branch3x3dbl, branch_pool], - axis=channel_axis, - name="mixed0", - ) - - # mixed 1: 35 x 35 x 288 - branch1x1 = conv2d_bn(x, 64, 1, 1) - - branch5x5 = conv2d_bn(x, 48, 1, 1) - branch5x5 = conv2d_bn(branch5x5, 64, 5, 5) - - branch3x3dbl = conv2d_bn(x, 64, 1, 1) - branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) - branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) - - branch_pool = layers.AveragePooling2D((3, 3), strides=(1, 1), padding="same")(x) - branch_pool = conv2d_bn(branch_pool, 64, 1, 1) - x = layers.concatenate( - [branch1x1, branch5x5, branch3x3dbl, branch_pool], - axis=channel_axis, - name="mixed1", - ) - - # mixed 2: 35 x 35 x 288 - branch1x1 = conv2d_bn(x, 64, 1, 1) - - branch5x5 = conv2d_bn(x, 48, 1, 1) - branch5x5 = conv2d_bn(branch5x5, 64, 5, 5) - - branch3x3dbl = conv2d_bn(x, 64, 1, 1) - branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) - branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) - - branch_pool = layers.AveragePooling2D((3, 3), strides=(1, 1), padding="same")(x) - branch_pool = conv2d_bn(branch_pool, 64, 1, 1) - x = layers.concatenate( - [branch1x1, branch5x5, branch3x3dbl, branch_pool], - axis=channel_axis, - name="mixed2", - ) - - # mixed 3: 17 x 17 x 768 - branch3x3 = conv2d_bn(x, 384, 3, 3, strides=(2, 2), padding="valid") - - branch3x3dbl = conv2d_bn(x, 64, 1, 1) - branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3) - branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3, strides=(2, 2), padding="valid") - - branch_pool = layers.MaxPooling2D((3, 3), strides=(2, 2))(x) - x = layers.concatenate([branch3x3, branch3x3dbl, branch_pool], axis=channel_axis, name="mixed3") - - # mixed 4: 17 x 17 x 768 - branch1x1 = conv2d_bn(x, 192, 1, 1) - - branch7x7 = conv2d_bn(x, 128, 1, 1) - branch7x7 = conv2d_bn(branch7x7, 128, 1, 7) - branch7x7 = conv2d_bn(branch7x7, 192, 7, 1) - - branch7x7dbl = conv2d_bn(x, 128, 1, 1) - branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1) - branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 1, 7) - branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1) - branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) - - branch_pool = layers.AveragePooling2D((3, 3), strides=(1, 1), padding="same")(x) - branch_pool = conv2d_bn(branch_pool, 192, 1, 1) - x = layers.concatenate( - [branch1x1, branch7x7, branch7x7dbl, branch_pool], - axis=channel_axis, - name="mixed4", - ) - - # mixed 5, 6: 17 x 17 x 768 - for i in range(2): - branch1x1 = conv2d_bn(x, 192, 1, 1) - - branch7x7 = conv2d_bn(x, 160, 1, 1) - branch7x7 = conv2d_bn(branch7x7, 160, 1, 7) - branch7x7 = conv2d_bn(branch7x7, 192, 7, 1) - - branch7x7dbl = conv2d_bn(x, 160, 1, 1) - branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1) - branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 1, 7) - branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1) - branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) - - branch_pool = layers.AveragePooling2D((3, 3), strides=(1, 1), padding="same")(x) - branch_pool = conv2d_bn(branch_pool, 192, 1, 1) - x = layers.concatenate( - [branch1x1, branch7x7, branch7x7dbl, branch_pool], - axis=channel_axis, - name="mixed" + str(5 + i), - ) - - # mixed 7: 17 x 17 x 768 - branch1x1 = conv2d_bn(x, 192, 1, 1) - - branch7x7 = conv2d_bn(x, 192, 1, 1) - branch7x7 = conv2d_bn(branch7x7, 192, 1, 7) - branch7x7 = conv2d_bn(branch7x7, 192, 7, 1) - - branch7x7dbl = conv2d_bn(x, 192, 1, 1) - branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1) - branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) - branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1) - branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7) - - branch_pool = layers.AveragePooling2D((3, 3), strides=(1, 1), padding="same")(x) - branch_pool = conv2d_bn(branch_pool, 192, 1, 1) - x = layers.concatenate( - [branch1x1, branch7x7, branch7x7dbl, branch_pool], - axis=channel_axis, - name="mixed7", - ) - - # mixed 8: 8 x 8 x 1280 - branch3x3 = conv2d_bn(x, 192, 1, 1) - branch3x3 = conv2d_bn(branch3x3, 320, 3, 3, strides=(2, 2), padding="valid") - - branch7x7x3 = conv2d_bn(x, 192, 1, 1) - branch7x7x3 = conv2d_bn(branch7x7x3, 192, 1, 7) - branch7x7x3 = conv2d_bn(branch7x7x3, 192, 7, 1) - branch7x7x3 = conv2d_bn(branch7x7x3, 192, 3, 3, strides=(2, 2), padding="valid") - - branch_pool = layers.MaxPooling2D((3, 3), strides=(2, 2))(x) - x = layers.concatenate([branch3x3, branch7x7x3, branch_pool], axis=channel_axis, name="mixed8") - - # mixed 9: 8 x 8 x 2048 - for i in range(2): - branch1x1 = conv2d_bn(x, 320, 1, 1) - - branch3x3 = conv2d_bn(x, 384, 1, 1) - branch3x3_1 = conv2d_bn(branch3x3, 384, 1, 3) - branch3x3_2 = conv2d_bn(branch3x3, 384, 3, 1) - branch3x3 = layers.concatenate( - [branch3x3_1, branch3x3_2], - axis=channel_axis, - name="mixed9_" + str(i), - ) - - branch3x3dbl = conv2d_bn(x, 448, 1, 1) - branch3x3dbl = conv2d_bn(branch3x3dbl, 384, 3, 3) - branch3x3dbl_1 = conv2d_bn(branch3x3dbl, 384, 1, 3) - branch3x3dbl_2 = conv2d_bn(branch3x3dbl, 384, 3, 1) - branch3x3dbl = layers.concatenate([branch3x3dbl_1, branch3x3dbl_2], axis=channel_axis) - - branch_pool = layers.AveragePooling2D((3, 3), strides=(1, 1), padding="same")(x) - branch_pool = conv2d_bn(branch_pool, 192, 1, 1) - x = layers.concatenate( - [branch1x1, branch3x3, branch3x3dbl, branch_pool], - axis=channel_axis, - name="mixed" + str(9 + i), - ) - if include_top: - # Classification block - x = layers.GlobalAveragePooling2D(name="avg_pool")(x) - imagenet_utils.validate_activation(classifier_activation, weights) - x = layers.Dense(classes, activation=classifier_activation, name="predictions")(x) - else: - if pooling == "avg": - x = layers.GlobalAveragePooling2D()(x) - elif pooling == "max": - x = layers.GlobalMaxPooling2D()(x) - - # Ensure that the model takes into account - # any potential predecessors of `input_tensor`. - if input_tensor is not None: - inputs = layer_utils.get_source_inputs(input_tensor) - else: - inputs = img_input - # Create model. - model = training.Model(inputs, x, name="inception_v3") - - # Load weights. - if weights == "imagenet": - if include_top: - weights_path = data_utils.get_file( - "inception_v3_weights_tf_dim_ordering_tf_kernels.h5", - WEIGHTS_PATH, - cache_subdir="models", - file_hash="9a0d58056eeedaa3f26cb7ebd46da564", - ) - else: - weights_path = data_utils.get_file( - "inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5", - WEIGHTS_PATH_NO_TOP, - cache_subdir="models", - file_hash="bcbd6486424b2319ff4ef7d526e38f63", - ) - model.load_weights(weights_path) - elif weights is not None: - model.load_weights(weights) - - return model - - -def conv2d_bn(x, filters, num_row, num_col, padding="same", strides=(1, 1), name=None): - """Utility function to apply conv + BN. - - Args: - x: input tensor. - filters: filters in `Conv2D`. - num_row: height of the convolution kernel. - num_col: width of the convolution kernel. - padding: padding mode in `Conv2D`. - strides: strides in `Conv2D`. - name: name of the ops; will become `name + '_conv'` - for the convolution and `name + '_bn'` for the - batch norm layer. - - Returns: - Output tensor after applying `Conv2D` and `BatchNormalization`. - """ - if name is not None: - bn_name = name + "_bn" - conv_name = name + "_conv" - else: - bn_name = None - conv_name = None - if backend.image_data_format() == "channels_first": - bn_axis = 1 - else: - bn_axis = 3 - x = layers.Conv2D( - filters, - (num_row, num_col), - strides=strides, - padding=padding, - use_bias=False, - name=conv_name, - )(x) - x = layers.BatchNormalization(axis=bn_axis, scale=False, name=bn_name)(x) - x = layers.Activation("relu", name=name)(x) - return x - - -@keras_export("keras.applications.inception_v3.preprocess_input") -def preprocess_input(x, data_format=None): - return imagenet_utils.preprocess_input(x, data_format=data_format, mode="tf") - - -@keras_export("keras.applications.inception_v3.decode_predictions") -def decode_predictions(preds, top=5): - return imagenet_utils.decode_predictions(preds, top=top) - - -preprocess_input.__doc__ = imagenet_utils.PREPROCESS_INPUT_DOC.format( - mode="", - ret=imagenet_utils.PREPROCESS_INPUT_RET_DOC_TF, - error=imagenet_utils.PREPROCESS_INPUT_ERROR_DOC, -) -decode_predictions.__doc__ = imagenet_utils.decode_predictions.__doc__ diff --git a/Archive/keras_applications_mod/models/mobilenet.py b/Archive/keras_applications_mod/models/mobilenet.py deleted file mode 100644 index a24c6da..0000000 --- a/Archive/keras_applications_mod/models/mobilenet.py +++ /dev/null @@ -1,459 +0,0 @@ -# Copyright 2015 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""MobileNet v1 models for Keras. - -MobileNet is a general architecture and can be used for multiple use cases. -Depending on the use case, it can use different input layer size and -different width factors. This allows different width models to reduce -the number of multiply-adds and thereby -reduce inference cost on mobile devices. - -MobileNets support any input size greater than 32 x 32, with larger image sizes -offering better performance. -The number of parameters and number of multiply-adds -can be modified by using the `alpha` parameter, -which increases/decreases the number of filters in each layer. -By altering the image size and `alpha` parameter, -all 16 models from the paper can be built, with ImageNet weights provided. - -The paper demonstrates the performance of MobileNets using `alpha` values of -1.0 (also called 100 % MobileNet), 0.75, 0.5 and 0.25. -For each of these `alpha` values, weights for 4 different input image sizes -are provided (224, 192, 160, 128). - -The following table describes the size and accuracy of the 100% MobileNet -on size 224 x 224: ----------------------------------------------------------------------------- -Width Multiplier (alpha) | ImageNet Acc | Multiply-Adds (M) | Params (M) --------------------------|---------------|-------------------|-------------- -| 1.0 MobileNet-224 | 70.6 % | 529 | 4.2 | -| 0.75 MobileNet-224 | 68.4 % | 325 | 2.6 | -| 0.50 MobileNet-224 | 63.7 % | 149 | 1.3 | -| 0.25 MobileNet-224 | 50.6 % | 41 | 0.5 | - -The following table describes the performance of -the 100 % MobileNet on various input sizes: ------------------------------------------------------------------------- -Resolution | ImageNet Acc | Multiply-Adds (M) | Params (M) -----------------------|---------------|-------------------|---------------- -| 1.0 MobileNet-224 | 70.6 % | 569 | 4.2 | -| 1.0 MobileNet-192 | 69.1 % | 418 | 4.2 | -| 1.0 MobileNet-160 | 67.2 % | 290 | 4.2 | -| 1.0 MobileNet-128 | 64.4 % | 186 | 4.2 | - -Reference: - - [MobileNets: Efficient Convolutional Neural Networks - for Mobile Vision Applications]( - https://arxiv.org/abs/1704.04861) -""" - -import tensorflow.compat.v2 as tf - -from keras import backend -from keras.applications import imagenet_utils -from keras.engine import training -from keras.layers import VersionAwareLayers -from keras.utils import data_utils -from keras.utils import layer_utils - -# isort: off -from tensorflow.python.platform import tf_logging as logging -from tensorflow.python.util.tf_export import keras_export - -BASE_WEIGHT_PATH = "https://storage.googleapis.com/tensorflow/keras-applications/mobilenet/" -layers = None - - -@keras_export("keras.applications.mobilenet.MobileNet", "keras.applications.MobileNet") -def MobileNet( - input_shape=None, - alpha=1.0, - depth_multiplier=1, - dropout=1e-3, - include_top=True, - weights="imagenet", - input_tensor=None, - pooling=None, - classes=1000, - classifier_activation="softmax", - **kwargs, -): - """Instantiates the MobileNet architecture. - - Reference: - - [MobileNets: Efficient Convolutional Neural Networks - for Mobile Vision Applications]( - https://arxiv.org/abs/1704.04861) - - This function returns a Keras image classification model, - optionally loaded with weights pre-trained on ImageNet. - - For image classification use cases, see - [this page for detailed examples]( - https://keras.io/api/applications/#usage-examples-for-image-classification-models). - - For transfer learning use cases, make sure to read the - [guide to transfer learning & fine-tuning]( - https://keras.io/guides/transfer_learning/). - - Note: each Keras Application expects a specific kind of input preprocessing. - For MobileNet, call `tf.keras.applications.mobilenet.preprocess_input` - on your inputs before passing them to the model. - `mobilenet.preprocess_input` will scale input pixels between -1 and 1. - - Args: - input_shape: Optional shape tuple, only to be specified if `include_top` - is False (otherwise the input shape has to be `(224, 224, 3)` (with - `channels_last` data format) or (3, 224, 224) (with `channels_first` - data format). It should have exactly 3 inputs channels, and width and - height should be no smaller than 32. E.g. `(200, 200, 3)` would be one - valid value. Defaults to `None`. - `input_shape` will be ignored if the `input_tensor` is provided. - alpha: Controls the width of the network. This is known as the width - multiplier in the MobileNet paper. - If `alpha` < 1.0, proportionally - decreases the number of filters in each layer. - If `alpha` > 1.0, - proportionally increases the number of filters in each layer. - If - `alpha` = 1, default number of filters from the paper are used at each - layer. Defaults to `1.0`. - depth_multiplier: Depth multiplier for depthwise convolution. This is - called the resolution multiplier in the MobileNet paper. - Defaults to `1.0`. - dropout: Dropout rate. Defaults to `0.001`. - include_top: Boolean, whether to include the fully-connected layer at the - top of the network. Defaults to `True`. - weights: One of `None` (random initialization), 'imagenet' (pre-training - on ImageNet), or the path to the weights file to be loaded. Defaults to - `imagenet`. - input_tensor: Optional Keras tensor (i.e. output of `layers.Input()`) to - use as image input for the model. `input_tensor` is useful for sharing - inputs between multiple different networks. Defaults to `None`. - pooling: Optional pooling mode for feature extraction when `include_top` - is `False`. - - `None` (default) means that the output of the model will be - the 4D tensor output of the last convolutional block. - - `avg` means that global average pooling - will be applied to the output of the - last convolutional block, and thus - the output of the model will be a 2D tensor. - - `max` means that global max pooling will be applied. - classes: Optional number of classes to classify images into, only to be - specified if `include_top` is True, and if no `weights` argument is - specified. Defaults to `1000`. - classifier_activation: A `str` or callable. The activation function to use - on the "top" layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" layer. - When loading pretrained weights, `classifier_activation` can only - be `None` or `"softmax"`. - **kwargs: For backwards compatibility only. - Returns: - A `keras.Model` instance. - """ - global layers - if "layers" in kwargs: - layers = kwargs.pop("layers") - else: - layers = VersionAwareLayers() - if kwargs: - raise ValueError(f"Unknown argument(s): {(kwargs,)}") - if not (weights in {"imagenet", None} or tf.io.gfile.exists(weights)): - raise ValueError( - "The `weights` argument should be either " - "`None` (random initialization), `imagenet` " - "(pre-training on ImageNet), " - "or the path to the weights file to be loaded. " - f"Received weights={weights}" - ) - - if weights == "imagenet" and include_top and classes != 1000: - raise ValueError( - 'If using `weights` as `"imagenet"` with `include_top` ' "as true, `classes` should be 1000. " f"Received classes={classes}" - ) - - # Determine proper input shape and default size. - if input_shape is None: - default_size = 224 - else: - if backend.image_data_format() == "channels_first": - rows = input_shape[1] - cols = input_shape[2] - else: - rows = input_shape[0] - cols = input_shape[1] - - if rows == cols and rows in [128, 160, 192, 224]: - default_size = rows - else: - default_size = 224 - - input_shape = imagenet_utils.obtain_input_shape( - input_shape, - default_size=default_size, - min_size=32, - data_format=backend.image_data_format(), - require_flatten=include_top, - weights=weights, - ) - - if backend.image_data_format() == "channels_last": - row_axis, col_axis = (0, 1) - else: - row_axis, col_axis = (1, 2) - rows = input_shape[row_axis] - cols = input_shape[col_axis] - - if weights == "imagenet": - if depth_multiplier != 1: - raise ValueError( - "If imagenet weights are being loaded, " "depth multiplier must be 1. " f"Received depth_multiplier={depth_multiplier}" - ) - - if alpha not in [0.25, 0.50, 0.75, 1.0]: - raise ValueError( - "If imagenet weights are being loaded, " - "alpha can be one of" - "`0.25`, `0.50`, `0.75` or `1.0` only. " - f"Received alpha={alpha}" - ) - - if rows != cols or rows not in [128, 160, 192, 224]: - rows = 224 - logging.warning( - "`input_shape` is undefined or non-square, " - "or `rows` is not in [128, 160, 192, 224]. " - "Weights for input shape (224, 224) will be " - "loaded as the default." - ) - - if input_tensor is None: - img_input = layers.Input(shape=input_shape) - else: - if not backend.is_keras_tensor(input_tensor): - img_input = layers.Input(tensor=input_tensor, shape=input_shape) - else: - img_input = input_tensor - - x = _conv_block(img_input, 32, alpha, strides=(2, 2)) - x = _depthwise_conv_block(x, 64, alpha, depth_multiplier, block_id=1) - - x = _depthwise_conv_block(x, 128, alpha, depth_multiplier, strides=(2, 2), block_id=2) - x = _depthwise_conv_block(x, 128, alpha, depth_multiplier, block_id=3) - - x = _depthwise_conv_block(x, 256, alpha, depth_multiplier, strides=(2, 2), block_id=4) - x = _depthwise_conv_block(x, 256, alpha, depth_multiplier, block_id=5) - - x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, strides=(2, 2), block_id=6) - x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=7) - x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=8) - x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=9) - x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=10) - x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=11) - - x = _depthwise_conv_block(x, 1024, alpha, depth_multiplier, strides=(2, 2), block_id=12) - x = _depthwise_conv_block(x, 1024, alpha, depth_multiplier, block_id=13) - - if include_top: - x = layers.GlobalAveragePooling2D(keepdims=True)(x) - x = layers.Dropout(dropout, name="dropout")(x) - x = layers.Conv2D(classes, (1, 1), padding="same", name="conv_preds")(x) - x = layers.Reshape((classes,), name="reshape_2")(x) - imagenet_utils.validate_activation(classifier_activation, weights) - x = layers.Activation(activation=classifier_activation, name="predictions")(x) - else: - if pooling == "avg": - x = layers.GlobalAveragePooling2D()(x) - elif pooling == "max": - x = layers.GlobalMaxPooling2D()(x) - - # Ensure that the model takes into account - # any potential predecessors of `input_tensor`. - if input_tensor is not None: - inputs = layer_utils.get_source_inputs(input_tensor) - else: - inputs = img_input - - # Create model. - model = training.Model(inputs, x, name=f"mobilenet_{alpha:0.2f}_{rows}") - - # Load weights. - if weights == "imagenet": - if alpha == 1.0: - alpha_text = "1_0" - elif alpha == 0.75: - alpha_text = "7_5" - elif alpha == 0.50: - alpha_text = "5_0" - else: - alpha_text = "2_5" - - if include_top: - model_name = "mobilenet_%s_%d_tf.h5" % (alpha_text, rows) - weight_path = BASE_WEIGHT_PATH + model_name - weights_path = data_utils.get_file(model_name, weight_path, cache_subdir="models") - else: - model_name = "mobilenet_%s_%d_tf_no_top.h5" % (alpha_text, rows) - weight_path = BASE_WEIGHT_PATH + model_name - weights_path = data_utils.get_file(model_name, weight_path, cache_subdir="models") - model.load_weights(weights_path) - elif weights is not None: - model.load_weights(weights) - - return model - - -def _conv_block(inputs, filters, alpha, kernel=(3, 3), strides=(1, 1)): - """Adds an initial convolution layer (with batch normalization and relu6). - - Args: - inputs: Input tensor of shape `(rows, cols, 3)` (with `channels_last` - data format) or (3, rows, cols) (with `channels_first` data format). - It should have exactly 3 inputs channels, and width and height should - be no smaller than 32. E.g. `(224, 224, 3)` would be one valid value. - filters: Integer, the dimensionality of the output space (i.e. the - number of output filters in the convolution). - alpha: controls the width of the network. - If `alpha` < 1.0, - proportionally decreases the number of filters in each layer. - If - `alpha` > 1.0, proportionally increases the number of filters in each - layer. - If `alpha` = 1, default number of filters from the paper are - used at each layer. - kernel: An integer or tuple/list of 2 integers, specifying the width and - height of the 2D convolution window. Can be a single integer to - specify the same value for all spatial dimensions. - strides: An integer or tuple/list of 2 integers, specifying the strides - of the convolution along the width and height. Can be a single integer - to specify the same value for all spatial dimensions. Specifying any - stride value != 1 is incompatible with specifying any `dilation_rate` - value != 1. # Input shape - 4D tensor with shape: `(samples, channels, rows, cols)` if - data_format='channels_first' - or 4D tensor with shape: `(samples, rows, cols, channels)` if - data_format='channels_last'. # Output shape - 4D tensor with shape: `(samples, filters, new_rows, new_cols)` if - data_format='channels_first' - or 4D tensor with shape: `(samples, new_rows, new_cols, filters)` if - data_format='channels_last'. `rows` and `cols` values might have - changed due to stride. - - Returns: - Output tensor of block. - """ - channel_axis = 1 if backend.image_data_format() == "channels_first" else -1 - filters = int(filters * alpha) - x = layers.Conv2D( - filters, - kernel, - padding="same", - use_bias=False, - strides=strides, - name="conv1", - )(inputs) - x = layers.BatchNormalization(axis=channel_axis, name="conv1_bn")(x) - return layers.ReLU(6.0, name="conv1_relu")(x) - - -def _depthwise_conv_block( - inputs, - pointwise_conv_filters, - alpha, - depth_multiplier=1, - strides=(1, 1), - block_id=1, -): - """Adds a depthwise convolution block. - - A depthwise convolution block consists of a depthwise conv, - batch normalization, relu6, pointwise convolution, - batch normalization and relu6 activation. - - Args: - inputs: Input tensor of shape `(rows, cols, channels)` (with - `channels_last` data format) or (channels, rows, cols) (with - `channels_first` data format). - pointwise_conv_filters: Integer, the dimensionality of the output space - (i.e. the number of output filters in the pointwise convolution). - alpha: controls the width of the network. - If `alpha` < 1.0, - proportionally decreases the number of filters in each layer. - If - `alpha` > 1.0, proportionally increases the number of filters in each - layer. - If `alpha` = 1, default number of filters from the paper are - used at each layer. - depth_multiplier: The number of depthwise convolution output channels - for each input channel. The total number of depthwise convolution - output channels will be equal to `filters_in * depth_multiplier`. - strides: An integer or tuple/list of 2 integers, specifying the strides - of the convolution along the width and height. Can be a single integer - to specify the same value for all spatial dimensions. Specifying any - stride value != 1 is incompatible with specifying any `dilation_rate` - value != 1. - block_id: Integer, a unique identification designating the block number. - # Input shape - 4D tensor with shape: `(batch, channels, rows, cols)` if - data_format='channels_first' - or 4D tensor with shape: `(batch, rows, cols, channels)` if - data_format='channels_last'. # Output shape - 4D tensor with shape: `(batch, filters, new_rows, new_cols)` if - data_format='channels_first' - or 4D tensor with shape: `(batch, new_rows, new_cols, filters)` if - data_format='channels_last'. `rows` and `cols` values might have - changed due to stride. - - Returns: - Output tensor of block. - """ - channel_axis = 1 if backend.image_data_format() == "channels_first" else -1 - pointwise_conv_filters = int(pointwise_conv_filters * alpha) - - if strides == (1, 1): - x = inputs - else: - x = layers.ZeroPadding2D(((0, 1), (0, 1)), name="conv_pad_%d" % block_id)(inputs) - x = layers.DepthwiseConv2D( - (3, 3), - padding="same" if strides == (1, 1) else "valid", - depth_multiplier=depth_multiplier, - strides=strides, - use_bias=False, - name="conv_dw_%d" % block_id, - )(x) - x = layers.BatchNormalization(axis=channel_axis, name="conv_dw_%d_bn" % block_id)(x) - x = layers.ReLU(6.0, name="conv_dw_%d_relu" % block_id)(x) - - x = layers.Conv2D( - pointwise_conv_filters, - (1, 1), - padding="same", - use_bias=False, - strides=(1, 1), - name="conv_pw_%d" % block_id, - )(x) - x = layers.BatchNormalization(axis=channel_axis, name="conv_pw_%d_bn" % block_id)(x) - return layers.ReLU(6.0, name="conv_pw_%d_relu" % block_id)(x) - - -@keras_export("keras.applications.mobilenet.preprocess_input") -def preprocess_input(x, data_format=None): - return imagenet_utils.preprocess_input(x, data_format=data_format, mode="tf") - - -@keras_export("keras.applications.mobilenet.decode_predictions") -def decode_predictions(preds, top=5): - return imagenet_utils.decode_predictions(preds, top=top) - - -preprocess_input.__doc__ = imagenet_utils.PREPROCESS_INPUT_DOC.format( - mode="", - ret=imagenet_utils.PREPROCESS_INPUT_RET_DOC_TF, - error=imagenet_utils.PREPROCESS_INPUT_ERROR_DOC, -) -decode_predictions.__doc__ = imagenet_utils.decode_predictions.__doc__ diff --git a/Archive/keras_applications_mod/models/mobilenet_v2.py b/Archive/keras_applications_mod/models/mobilenet_v2.py deleted file mode 100644 index 0204a07..0000000 --- a/Archive/keras_applications_mod/models/mobilenet_v2.py +++ /dev/null @@ -1,513 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""MobileNet v2 models for Keras. - -MobileNetV2 is a general architecture and can be used for multiple use cases. -Depending on the use case, it can use different input layer size and -different width factors. This allows different width models to reduce -the number of multiply-adds and thereby -reduce inference cost on mobile devices. - -MobileNetV2 is very similar to the original MobileNet, -except that it uses inverted residual blocks with -bottlenecking features. It has a drastically lower -parameter count than the original MobileNet. -MobileNets support any input size greater -than 32 x 32, with larger image sizes -offering better performance. - -The number of parameters and number of multiply-adds -can be modified by using the `alpha` parameter, -which increases/decreases the number of filters in each layer. -By altering the image size and `alpha` parameter, -all 22 models from the paper can be built, with ImageNet weights provided. - -The paper demonstrates the performance of MobileNets using `alpha` values of -1.0 (also called 100 % MobileNet), 0.35, 0.5, 0.75, 1.0, 1.3, and 1.4 -For each of these `alpha` values, weights for 5 different input image sizes -are provided (224, 192, 160, 128, and 96). - -The following table describes the performance of -MobileNet on various input sizes: ------------------------------------------------------------------------- -MACs stands for Multiply Adds -Classification Checkpoint|MACs (M)|Parameters (M)|Top 1 Accuracy|Top 5 Accuracy ---------------------------|------------|---------------|---------|------------ -| [mobilenet_v2_1.4_224] | 582 | 6.06 | 75.0 | 92.5 | -| [mobilenet_v2_1.3_224] | 509 | 5.34 | 74.4 | 92.1 | -| [mobilenet_v2_1.0_224] | 300 | 3.47 | 71.8 | 91.0 | -| [mobilenet_v2_1.0_192] | 221 | 3.47 | 70.7 | 90.1 | -| [mobilenet_v2_1.0_160] | 154 | 3.47 | 68.8 | 89.0 | -| [mobilenet_v2_1.0_128] | 99 | 3.47 | 65.3 | 86.9 | -| [mobilenet_v2_1.0_96] | 56 | 3.47 | 60.3 | 83.2 | -| [mobilenet_v2_0.75_224] | 209 | 2.61 | 69.8 | 89.6 | -| [mobilenet_v2_0.75_192] | 153 | 2.61 | 68.7 | 88.9 | -| [mobilenet_v2_0.75_160] | 107 | 2.61 | 66.4 | 87.3 | -| [mobilenet_v2_0.75_128] | 69 | 2.61 | 63.2 | 85.3 | -| [mobilenet_v2_0.75_96] | 39 | 2.61 | 58.8 | 81.6 | -| [mobilenet_v2_0.5_224] | 97 | 1.95 | 65.4 | 86.4 | -| [mobilenet_v2_0.5_192] | 71 | 1.95 | 63.9 | 85.4 | -| [mobilenet_v2_0.5_160] | 50 | 1.95 | 61.0 | 83.2 | -| [mobilenet_v2_0.5_128] | 32 | 1.95 | 57.7 | 80.8 | -| [mobilenet_v2_0.5_96] | 18 | 1.95 | 51.2 | 75.8 | -| [mobilenet_v2_0.35_224] | 59 | 1.66 | 60.3 | 82.9 | -| [mobilenet_v2_0.35_192] | 43 | 1.66 | 58.2 | 81.2 | -| [mobilenet_v2_0.35_160] | 30 | 1.66 | 55.7 | 79.1 | -| [mobilenet_v2_0.35_128] | 20 | 1.66 | 50.8 | 75.0 | -| [mobilenet_v2_0.35_96] | 11 | 1.66 | 45.5 | 70.4 | - - Reference: - - [MobileNetV2: Inverted Residuals and Linear Bottlenecks]( - https://arxiv.org/abs/1801.04381) (CVPR 2018) -""" - -import tensorflow.compat.v2 as tf - -from keras import backend -from keras.applications import imagenet_utils -from keras.engine import training -from keras.layers import VersionAwareLayers -from keras.utils import data_utils -from keras.utils import layer_utils - -# isort: off -from tensorflow.python.platform import tf_logging as logging -from tensorflow.python.util.tf_export import keras_export - -BASE_WEIGHT_PATH = "https://storage.googleapis.com/tensorflow/keras-applications/mobilenet_v2/" -layers = None - - -@keras_export( - "keras.applications.mobilenet_v2.MobileNetV2", - "keras.applications.MobileNetV2", -) -def MobileNetV2( - input_shape=None, - alpha=1.0, - include_top=True, - weights="imagenet", - input_tensor=None, - pooling=None, - classes=1000, - classifier_activation="softmax", - **kwargs, -): - """Instantiates the MobileNetV2 architecture. - - MobileNetV2 is very similar to the original MobileNet, - except that it uses inverted residual blocks with - bottlenecking features. It has a drastically lower - parameter count than the original MobileNet. - MobileNets support any input size greater - than 32 x 32, with larger image sizes - offering better performance. - - Reference: - - [MobileNetV2: Inverted Residuals and Linear Bottlenecks]( - https://arxiv.org/abs/1801.04381) (CVPR 2018) - - This function returns a Keras image classification model, - optionally loaded with weights pre-trained on ImageNet. - - For image classification use cases, see - [this page for detailed examples]( - https://keras.io/api/applications/#usage-examples-for-image-classification-models). - - For transfer learning use cases, make sure to read the - [guide to transfer learning & fine-tuning]( - https://keras.io/guides/transfer_learning/). - - Note: each Keras Application expects a specific kind of input preprocessing. - For MobileNetV2, call `tf.keras.applications.mobilenet_v2.preprocess_input` - on your inputs before passing them to the model. - `mobilenet_v2.preprocess_input` will scale input pixels between -1 and 1. - - Args: - input_shape: Optional shape tuple, to be specified if you would - like to use a model with an input image resolution that is not - (224, 224, 3). - It should have exactly 3 inputs channels (224, 224, 3). - You can also omit this option if you would like - to infer input_shape from an input_tensor. - If you choose to include both input_tensor and input_shape then - input_shape will be used if they match, if the shapes - do not match then we will throw an error. - E.g. `(160, 160, 3)` would be one valid value. - alpha: Float, larger than zero, controls the width of the network. This is - known as the width multiplier in the MobileNetV2 paper, but the name is - kept for consistency with `applications.MobileNetV1` model in Keras. - - If `alpha` < 1.0, proportionally decreases the number - of filters in each layer. - - If `alpha` > 1.0, proportionally increases the number - of filters in each layer. - - If `alpha` = 1.0, default number of filters from the paper - are used at each layer. - include_top: Boolean, whether to include the fully-connected layer at the - top of the network. Defaults to `True`. - weights: String, one of `None` (random initialization), 'imagenet' - (pre-training on ImageNet), or the path to the weights file to be - loaded. - input_tensor: Optional Keras tensor (i.e. output of `layers.Input()`) - to use as image input for the model. - pooling: String, optional pooling mode for feature extraction when - `include_top` is `False`. - - `None` means that the output of the model - will be the 4D tensor output of the - last convolutional block. - - `avg` means that global average pooling - will be applied to the output of the - last convolutional block, and thus - the output of the model will be a - 2D tensor. - - `max` means that global max pooling will - be applied. - classes: Optional integer number of classes to classify images into, only - to be specified if `include_top` is True, and if no `weights` argument - is specified. - classifier_activation: A `str` or callable. The activation function to use - on the "top" layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" layer. - When loading pretrained weights, `classifier_activation` can only - be `None` or `"softmax"`. - **kwargs: For backwards compatibility only. - - Returns: - A `keras.Model` instance. - """ - global layers - if "layers" in kwargs: - layers = kwargs.pop("layers") - else: - layers = VersionAwareLayers() - if kwargs: - raise ValueError(f"Unknown argument(s): {kwargs}") - if not (weights in {"imagenet", None} or tf.io.gfile.exists(weights)): - raise ValueError( - "The `weights` argument should be either " - "`None` (random initialization), `imagenet` " - "(pre-training on ImageNet), " - "or the path to the weights file to be loaded. " - f"Received `weights={weights}`" - ) - - if weights == "imagenet" and include_top and classes != 1000: - raise ValueError( - 'If using `weights` as `"imagenet"` with `include_top` ' f"as true, `classes` should be 1000. Received `classes={classes}`" - ) - - # Determine proper input shape and default size. - # If both input_shape and input_tensor are used, they should match - if input_shape is not None and input_tensor is not None: - try: - is_input_t_tensor = backend.is_keras_tensor(input_tensor) - except ValueError: - try: - is_input_t_tensor = backend.is_keras_tensor(layer_utils.get_source_inputs(input_tensor)) - except ValueError: - raise ValueError( - f"input_tensor: {input_tensor}" "is not type input_tensor. " f"Received `type(input_tensor)={type(input_tensor)}`" - ) - if is_input_t_tensor: - if backend.image_data_format() == "channels_first": - if backend.int_shape(input_tensor)[1] != input_shape[1]: - raise ValueError( - "input_shape[1] must equal shape(input_tensor)[1] " - "when `image_data_format` is `channels_first`; " - "Received `input_tensor.shape=" - f"{input_tensor.shape}`" - f", `input_shape={input_shape}`" - ) - else: - if backend.int_shape(input_tensor)[2] != input_shape[1]: - raise ValueError( - "input_tensor.shape[2] must equal input_shape[1]; " - "Received `input_tensor.shape=" - f"{input_tensor.shape}`, " - f"`input_shape={input_shape}`" - ) - else: - raise ValueError("input_tensor is not a Keras tensor; " f"Received `input_tensor={input_tensor}`") - - # If input_shape is None, infer shape from input_tensor. - if input_shape is None and input_tensor is not None: - try: - backend.is_keras_tensor(input_tensor) - except ValueError: - raise ValueError("input_tensor must be a valid Keras tensor type; " f"Received {input_tensor} of type {type(input_tensor)}") - - if input_shape is None and not backend.is_keras_tensor(input_tensor): - default_size = 224 - elif input_shape is None and backend.is_keras_tensor(input_tensor): - if backend.image_data_format() == "channels_first": - rows = backend.int_shape(input_tensor)[2] - cols = backend.int_shape(input_tensor)[3] - else: - rows = backend.int_shape(input_tensor)[1] - cols = backend.int_shape(input_tensor)[2] - - if rows == cols and rows in [96, 128, 160, 192, 224]: - default_size = rows - else: - default_size = 224 - - # If input_shape is None and no input_tensor - elif input_shape is None: - default_size = 224 - - # If input_shape is not None, assume default size. - else: - if backend.image_data_format() == "channels_first": - rows = input_shape[1] - cols = input_shape[2] - else: - rows = input_shape[0] - cols = input_shape[1] - - if rows == cols and rows in [96, 128, 160, 192, 224]: - default_size = rows - else: - default_size = 224 - - input_shape = imagenet_utils.obtain_input_shape( - input_shape, - default_size=default_size, - min_size=32, - data_format=backend.image_data_format(), - require_flatten=include_top, - weights=weights, - ) - - if backend.image_data_format() == "channels_last": - row_axis, col_axis = (0, 1) - else: - row_axis, col_axis = (1, 2) - rows = input_shape[row_axis] - cols = input_shape[col_axis] - - if weights == "imagenet": - if alpha not in [0.35, 0.50, 0.75, 1.0, 1.3, 1.4]: - raise ValueError( - "If imagenet weights are being loaded, " - "alpha must be one of `0.35`, `0.50`, `0.75`, " - "`1.0`, `1.3` or `1.4` only;" - f" Received `alpha={alpha}`" - ) - - if rows != cols or rows not in [96, 128, 160, 192, 224]: - rows = 224 - logging.warning( - "`input_shape` is undefined or non-square, " - "or `rows` is not in [96, 128, 160, 192, 224]. " - "Weights for input shape (224, 224) will be " - "loaded as the default." - ) - - if input_tensor is None: - img_input = layers.Input(shape=input_shape) - else: - if not backend.is_keras_tensor(input_tensor): - img_input = layers.Input(tensor=input_tensor, shape=input_shape) - else: - img_input = input_tensor - - channel_axis = 1 if backend.image_data_format() == "channels_first" else -1 - - first_block_filters = _make_divisible(32 * alpha, 8) - x = layers.Conv2D( - first_block_filters, - kernel_size=3, - strides=(2, 2), - padding="same", - use_bias=False, - name="Conv1", - )(img_input) - x = layers.BatchNormalization(axis=channel_axis, epsilon=1e-3, momentum=0.999, name="bn_Conv1")(x) - x = layers.ReLU(6.0, name="Conv1_relu")(x) - - x = _inverted_res_block(x, filters=16, alpha=alpha, stride=1, expansion=1, block_id=0) - - x = _inverted_res_block(x, filters=24, alpha=alpha, stride=2, expansion=6, block_id=1) - x = _inverted_res_block(x, filters=24, alpha=alpha, stride=1, expansion=6, block_id=2) - - x = _inverted_res_block(x, filters=32, alpha=alpha, stride=2, expansion=6, block_id=3) - x = _inverted_res_block(x, filters=32, alpha=alpha, stride=1, expansion=6, block_id=4) - x = _inverted_res_block(x, filters=32, alpha=alpha, stride=1, expansion=6, block_id=5) - - x = _inverted_res_block(x, filters=64, alpha=alpha, stride=2, expansion=6, block_id=6) - x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, expansion=6, block_id=7) - x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, expansion=6, block_id=8) - x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, expansion=6, block_id=9) - - x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, expansion=6, block_id=10) - x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, expansion=6, block_id=11) - x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, expansion=6, block_id=12) - - x = _inverted_res_block(x, filters=160, alpha=alpha, stride=2, expansion=6, block_id=13) - x = _inverted_res_block(x, filters=160, alpha=alpha, stride=1, expansion=6, block_id=14) - x = _inverted_res_block(x, filters=160, alpha=alpha, stride=1, expansion=6, block_id=15) - - x = _inverted_res_block(x, filters=320, alpha=alpha, stride=1, expansion=6, block_id=16) - - # no alpha applied to last conv as stated in the paper: - # if the width multiplier is greater than 1 we increase the number of output - # channels. - if alpha > 1.0: - last_block_filters = _make_divisible(1280 * alpha, 8) - else: - last_block_filters = 1280 - - x = layers.Conv2D(last_block_filters, kernel_size=1, use_bias=False, name="Conv_1")(x) - x = layers.BatchNormalization(axis=channel_axis, epsilon=1e-3, momentum=0.999, name="Conv_1_bn")(x) - x = layers.ReLU(6.0, name="out_relu")(x) - - if include_top: - x = layers.GlobalAveragePooling2D()(x) - imagenet_utils.validate_activation(classifier_activation, weights) - x = layers.Dense(classes, activation=classifier_activation, name="predictions")(x) - - else: - if pooling == "avg": - x = layers.GlobalAveragePooling2D()(x) - elif pooling == "max": - x = layers.GlobalMaxPooling2D()(x) - - # Ensure that the model takes into account any potential predecessors of - # `input_tensor`. - if input_tensor is not None: - inputs = layer_utils.get_source_inputs(input_tensor) - else: - inputs = img_input - - # Create model. - model = training.Model(inputs, x, name=f"mobilenetv2_{alpha:0.2f}_{rows}") - - # Load weights. - if weights == "imagenet": - if include_top: - model_name = "mobilenet_v2_weights_tf_dim_ordering_tf_kernels_" + str(float(alpha)) + "_" + str(rows) + ".h5" - weight_path = BASE_WEIGHT_PATH + model_name - weights_path = data_utils.get_file(model_name, weight_path, cache_subdir="models") - else: - model_name = "mobilenet_v2_weights_tf_dim_ordering_tf_kernels_" + str(float(alpha)) + "_" + str(rows) + "_no_top" + ".h5" - weight_path = BASE_WEIGHT_PATH + model_name - weights_path = data_utils.get_file(model_name, weight_path, cache_subdir="models") - model.load_weights(weights_path) - elif weights is not None: - model.load_weights(weights) - - return model - - -def _inverted_res_block(inputs, expansion, stride, alpha, filters, block_id): - """Inverted ResNet block.""" - channel_axis = 1 if backend.image_data_format() == "channels_first" else -1 - - in_channels = backend.int_shape(inputs)[channel_axis] - pointwise_conv_filters = int(filters * alpha) - # Ensure the number of filters on the last 1x1 convolution is divisible by - # 8. - pointwise_filters = _make_divisible(pointwise_conv_filters, 8) - x = inputs - prefix = f"block_{block_id}_" - - if block_id: - # Expand with a pointwise 1x1 convolution. - x = layers.Conv2D( - expansion * in_channels, - kernel_size=1, - padding="same", - use_bias=False, - activation=None, - name=prefix + "expand", - )(x) - x = layers.BatchNormalization( - axis=channel_axis, - epsilon=1e-3, - momentum=0.999, - name=prefix + "expand_BN", - )(x) - x = layers.ReLU(6.0, name=prefix + "expand_relu")(x) - else: - prefix = "expanded_conv_" - - # Depthwise 3x3 convolution. - if stride == 2: - x = layers.ZeroPadding2D(padding=imagenet_utils.correct_pad(x, 3), name=prefix + "pad")(x) - x = layers.DepthwiseConv2D( - kernel_size=3, - strides=stride, - activation=None, - use_bias=False, - padding="same" if stride == 1 else "valid", - name=prefix + "depthwise", - )(x) - x = layers.BatchNormalization( - axis=channel_axis, - epsilon=1e-3, - momentum=0.999, - name=prefix + "depthwise_BN", - )(x) - - x = layers.ReLU(6.0, name=prefix + "depthwise_relu")(x) - - # Project with a pointwise 1x1 convolution. - x = layers.Conv2D( - pointwise_filters, - kernel_size=1, - padding="same", - use_bias=False, - activation=None, - name=prefix + "project", - )(x) - x = layers.BatchNormalization( - axis=channel_axis, - epsilon=1e-3, - momentum=0.999, - name=prefix + "project_BN", - )(x) - - if in_channels == pointwise_filters and stride == 1: - return layers.Add(name=prefix + "add")([inputs, x]) - return x - - -def _make_divisible(v, divisor, min_value=None): - if min_value is None: - min_value = divisor - new_v = max(min_value, int(v + divisor / 2) // divisor * divisor) - # Make sure that round down does not go down by more than 10%. - if new_v < 0.9 * v: - new_v += divisor - return new_v - - -@keras_export("keras.applications.mobilenet_v2.preprocess_input") -def preprocess_input(x, data_format=None): - return imagenet_utils.preprocess_input(x, data_format=data_format, mode="tf") - - -@keras_export("keras.applications.mobilenet_v2.decode_predictions") -def decode_predictions(preds, top=5): - return imagenet_utils.decode_predictions(preds, top=top) - - -preprocess_input.__doc__ = imagenet_utils.PREPROCESS_INPUT_DOC.format( - mode="", - ret=imagenet_utils.PREPROCESS_INPUT_RET_DOC_TF, - error=imagenet_utils.PREPROCESS_INPUT_ERROR_DOC, -) -decode_predictions.__doc__ = imagenet_utils.decode_predictions.__doc__ diff --git a/Archive/keras_applications_mod/models/mobilenet_v3.py b/Archive/keras_applications_mod/models/mobilenet_v3.py deleted file mode 100644 index 93811cb..0000000 --- a/Archive/keras_applications_mod/models/mobilenet_v3.py +++ /dev/null @@ -1,642 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - - -"""MobileNet v3 models for Keras.""" - -import tensorflow.compat.v2 as tf - -from keras import backend -from keras import models -from keras.applications import imagenet_utils -from keras.layers import VersionAwareLayers -from keras.utils import data_utils -from keras.utils import layer_utils - -# isort: off -from tensorflow.python.platform import tf_logging as logging -from tensorflow.python.util.tf_export import keras_export - -# TODO(scottzhu): Change this to the GCS path. -BASE_WEIGHT_PATH = "https://storage.googleapis.com/tensorflow/keras-applications/mobilenet_v3/" -WEIGHTS_HASHES = { - "large_224_0.75_float": ( - "765b44a33ad4005b3ac83185abf1d0eb", - "40af19a13ebea4e2ee0c676887f69a2e", - ), - "large_224_1.0_float": ( - "59e551e166be033d707958cf9e29a6a7", - "07fb09a5933dd0c8eaafa16978110389", - ), - "large_minimalistic_224_1.0_float": ( - "675e7b876c45c57e9e63e6d90a36599c", - "ec5221f64a2f6d1ef965a614bdae7973", - ), - "small_224_0.75_float": ( - "cb65d4e5be93758266aa0a7f2c6708b7", - "ebdb5cc8e0b497cd13a7c275d475c819", - ), - "small_224_1.0_float": ( - "8768d4c2e7dee89b9d02b2d03d65d862", - "d3e8ec802a04aa4fc771ee12a9a9b836", - ), - "small_minimalistic_224_1.0_float": ( - "99cd97fb2fcdad2bf028eb838de69e37", - "cde8136e733e811080d9fcd8a252f7e4", - ), -} - -layers = VersionAwareLayers() - - -BASE_DOCSTRING = """Instantiates the {name} architecture. - - Reference: - - [Searching for MobileNetV3]( - https://arxiv.org/pdf/1905.02244.pdf) (ICCV 2019) - - The following table describes the performance of MobileNets v3: - ------------------------------------------------------------------------ - MACs stands for Multiply Adds - - |Classification Checkpoint|MACs(M)|Parameters(M)|Top1 Accuracy|Pixel1 CPU(ms)| - |---|---|---|---|---| - | mobilenet_v3_large_1.0_224 | 217 | 5.4 | 75.6 | 51.2 | - | mobilenet_v3_large_0.75_224 | 155 | 4.0 | 73.3 | 39.8 | - | mobilenet_v3_large_minimalistic_1.0_224 | 209 | 3.9 | 72.3 | 44.1 | - | mobilenet_v3_small_1.0_224 | 66 | 2.9 | 68.1 | 15.8 | - | mobilenet_v3_small_0.75_224 | 44 | 2.4 | 65.4 | 12.8 | - | mobilenet_v3_small_minimalistic_1.0_224 | 65 | 2.0 | 61.9 | 12.2 | - - For image classification use cases, see - [this page for detailed examples]( - https://keras.io/api/applications/#usage-examples-for-image-classification-models). - - For transfer learning use cases, make sure to read the - [guide to transfer learning & fine-tuning]( - https://keras.io/guides/transfer_learning/). - - Note: each Keras Application expects a specific kind of input preprocessing. - For MobileNetV3, by default input preprocessing is included as a part of the - model (as a `Rescaling` layer), and thus - `tf.keras.applications.mobilenet_v3.preprocess_input` is actually a - pass-through function. In this use case, MobileNetV3 models expect their - inputs to be float tensors of pixels with values in the [0-255] range. - At the same time, preprocessing as a part of the model (i.e. `Rescaling` - layer) can be disabled by setting `include_preprocessing` argument to False. - With preprocessing disabled MobileNetV3 models expect their inputs to be float - tensors of pixels with values in the [-1, 1] range. - - Args: - input_shape: Optional shape tuple, to be specified if you would - like to use a model with an input image resolution that is not - (224, 224, 3). - It should have exactly 3 inputs channels (224, 224, 3). - You can also omit this option if you would like - to infer input_shape from an input_tensor. - If you choose to include both input_tensor and input_shape then - input_shape will be used if they match, if the shapes - do not match then we will throw an error. - E.g. `(160, 160, 3)` would be one valid value. - alpha: controls the width of the network. This is known as the - depth multiplier in the MobileNetV3 paper, but the name is kept for - consistency with MobileNetV1 in Keras. - - If `alpha` < 1.0, proportionally decreases the number - of filters in each layer. - - If `alpha` > 1.0, proportionally increases the number - of filters in each layer. - - If `alpha` = 1, default number of filters from the paper - are used at each layer. - minimalistic: In addition to large and small models this module also - contains so-called minimalistic models, these models have the same - per-layer dimensions characteristic as MobilenetV3 however, they don't - utilize any of the advanced blocks (squeeze-and-excite units, hard-swish, - and 5x5 convolutions). While these models are less efficient on CPU, they - are much more performant on GPU/DSP. - include_top: Boolean, whether to include the fully-connected - layer at the top of the network. Defaults to `True`. - weights: String, one of `None` (random initialization), - 'imagenet' (pre-training on ImageNet), - or the path to the weights file to be loaded. - input_tensor: Optional Keras tensor (i.e. output of - `layers.Input()`) - to use as image input for the model. - pooling: String, optional pooling mode for feature extraction - when `include_top` is `False`. - - `None` means that the output of the model - will be the 4D tensor output of the - last convolutional block. - - `avg` means that global average pooling - will be applied to the output of the - last convolutional block, and thus - the output of the model will be a - 2D tensor. - - `max` means that global max pooling will - be applied. - classes: Integer, optional number of classes to classify images - into, only to be specified if `include_top` is True, and - if no `weights` argument is specified. - dropout_rate: fraction of the input units to drop on the last layer. - classifier_activation: A `str` or callable. The activation function to use - on the "top" layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" layer. - When loading pretrained weights, `classifier_activation` can only - be `None` or `"softmax"`. - include_preprocessing: Boolean, whether to include the preprocessing - layer (`Rescaling`) at the bottom of the network. Defaults to `True`. - - Call arguments: - inputs: A floating point `numpy.array` or a `tf.Tensor`, 4D with 3 color - channels, with values in the range [0, 255] if `include_preprocessing` - is True and in the range [-1, 1] otherwise. - - Returns: - A `keras.Model` instance. -""" - - -def MobileNetV3( - stack_fn, - last_point_ch, - input_shape=None, - alpha=1.0, - model_type="large", - minimalistic=False, - include_top=True, - weights="imagenet", - input_tensor=None, - classes=1000, - pooling=None, - dropout_rate=0.2, - classifier_activation="softmax", - include_preprocessing=True, -): - if not (weights in {"imagenet", None} or tf.io.gfile.exists(weights)): - raise ValueError( - "The `weights` argument should be either " - "`None` (random initialization), `imagenet` " - "(pre-training on ImageNet), " - "or the path to the weights file to be loaded. " - f"Received weights={weights}" - ) - - if weights == "imagenet" and include_top and classes != 1000: - raise ValueError( - 'If using `weights` as `"imagenet"` with `include_top` ' "as true, `classes` should be 1000. " f"Received classes={classes}" - ) - - # Determine proper input shape and default size. - # If both input_shape and input_tensor are used, they should match - if input_shape is not None and input_tensor is not None: - try: - is_input_t_tensor = backend.is_keras_tensor(input_tensor) - except ValueError: - try: - is_input_t_tensor = backend.is_keras_tensor(layer_utils.get_source_inputs(input_tensor)) - except ValueError: - raise ValueError( - "input_tensor: ", - input_tensor, - "is not type input_tensor. " f"Received type(input_tensor)={type(input_tensor)}", - ) - if is_input_t_tensor: - if backend.image_data_format() == "channels_first": - if backend.int_shape(input_tensor)[1] != input_shape[1]: - raise ValueError( - "When backend.image_data_format()=channels_first, " - "input_shape[1] must equal " - "backend.int_shape(input_tensor)[1]. Received " - f"input_shape={input_shape}, " - "backend.int_shape(input_tensor)=" - f"{backend.int_shape(input_tensor)}" - ) - else: - if backend.int_shape(input_tensor)[2] != input_shape[1]: - raise ValueError( - "input_shape[1] must equal " - "backend.int_shape(input_tensor)[2]. Received " - f"input_shape={input_shape}, " - "backend.int_shape(input_tensor)=" - f"{backend.int_shape(input_tensor)}" - ) - else: - raise ValueError( - "input_tensor specified: ", - input_tensor, - "is not a keras tensor", - ) - - # If input_shape is None, infer shape from input_tensor - if input_shape is None and input_tensor is not None: - try: - backend.is_keras_tensor(input_tensor) - except ValueError: - raise ValueError( - "input_tensor: ", - input_tensor, - "is type: ", - type(input_tensor), - "which is not a valid type", - ) - - if backend.is_keras_tensor(input_tensor): - if backend.image_data_format() == "channels_first": - rows = backend.int_shape(input_tensor)[2] - cols = backend.int_shape(input_tensor)[3] - input_shape = (3, cols, rows) - else: - rows = backend.int_shape(input_tensor)[1] - cols = backend.int_shape(input_tensor)[2] - input_shape = (cols, rows, 3) - # If input_shape is None and input_tensor is None using standard shape - if input_shape is None and input_tensor is None: - input_shape = (None, None, 3) - - if backend.image_data_format() == "channels_last": - row_axis, col_axis = (0, 1) - else: - row_axis, col_axis = (1, 2) - rows = input_shape[row_axis] - cols = input_shape[col_axis] - if rows and cols and (rows < 32 or cols < 32): - raise ValueError("Input size must be at least 32x32; Received `input_shape=" f"{input_shape}`") - if weights == "imagenet": - if not minimalistic and alpha not in [0.75, 1.0] or minimalistic and alpha != 1.0: - raise ValueError( - "If imagenet weights are being loaded, " - "alpha can be one of `0.75`, `1.0` for non minimalistic " - "or `1.0` for minimalistic only." - ) - - if rows != cols or rows != 224: - logging.warning( - "`input_shape` is undefined or non-square, " - "or `rows` is not 224. " - "Weights for input shape (224, 224) will be " - "loaded as the default." - ) - - if input_tensor is None: - img_input = layers.Input(shape=input_shape) - else: - if not backend.is_keras_tensor(input_tensor): - img_input = layers.Input(tensor=input_tensor, shape=input_shape) - else: - img_input = input_tensor - - channel_axis = 1 if backend.image_data_format() == "channels_first" else -1 - - if minimalistic: - kernel = 3 - activation = relu - se_ratio = None - else: - kernel = 5 - activation = hard_swish - se_ratio = 0.25 - - x = img_input - if include_preprocessing: - x = layers.Rescaling(scale=1.0 / 127.5, offset=-1.0)(x) - x = layers.Conv2D( - 16, - kernel_size=3, - strides=(2, 2), - padding="same", - use_bias=False, - name="Conv", - )(x) - x = layers.BatchNormalization(axis=channel_axis, epsilon=1e-3, momentum=0.999, name="Conv/BatchNorm")(x) - x = activation(x) - - x = stack_fn(x, kernel, activation, se_ratio) - - last_conv_ch = _depth(backend.int_shape(x)[channel_axis] * 6) - - # if the width multiplier is greater than 1 we - # increase the number of output channels - if alpha > 1.0: - last_point_ch = _depth(last_point_ch * alpha) - x = layers.Conv2D( - last_conv_ch, - kernel_size=1, - padding="same", - use_bias=False, - name="Conv_1", - )(x) - x = layers.BatchNormalization(axis=channel_axis, epsilon=1e-3, momentum=0.999, name="Conv_1/BatchNorm")(x) - x = activation(x) - if include_top: - x = layers.GlobalAveragePooling2D(keepdims=True)(x) - x = layers.Conv2D( - last_point_ch, - kernel_size=1, - padding="same", - use_bias=True, - name="Conv_2", - )(x) - x = activation(x) - - if dropout_rate > 0: - x = layers.Dropout(dropout_rate)(x) - x = layers.Conv2D(classes, kernel_size=1, padding="same", name="Logits")(x) - x = layers.Flatten()(x) - imagenet_utils.validate_activation(classifier_activation, weights) - x = layers.Activation(activation=classifier_activation, name="Predictions")(x) - else: - if pooling == "avg": - x = layers.GlobalAveragePooling2D(name="avg_pool")(x) - elif pooling == "max": - x = layers.GlobalMaxPooling2D(name="max_pool")(x) - # Ensure that the model takes into account - # any potential predecessors of `input_tensor`. - if input_tensor is not None: - inputs = layer_utils.get_source_inputs(input_tensor) - else: - inputs = img_input - - # Create model. - model = models.Model(inputs, x, name="MobilenetV3" + model_type) - - # Load weights. - if weights == "imagenet": - model_name = "{}{}_224_{}_float".format(model_type, "_minimalistic" if minimalistic else "", str(alpha)) - if include_top: - file_name = "weights_mobilenet_v3_" + model_name + ".h5" - file_hash = WEIGHTS_HASHES[model_name][0] - else: - file_name = "weights_mobilenet_v3_" + model_name + "_no_top_v2.h5" - file_hash = WEIGHTS_HASHES[model_name][1] - weights_path = data_utils.get_file( - file_name, - BASE_WEIGHT_PATH + file_name, - cache_subdir="models", - file_hash=file_hash, - ) - model.load_weights(weights_path) - elif weights is not None: - model.load_weights(weights) - - return model - - -@keras_export("keras.applications.MobileNetV3Small") -def MobileNetV3Small( - input_shape=None, - alpha=1.0, - minimalistic=False, - include_top=True, - weights="imagenet", - input_tensor=None, - classes=1000, - pooling=None, - dropout_rate=0.2, - classifier_activation="softmax", - include_preprocessing=True, -): - def stack_fn(x, kernel, activation, se_ratio): - def depth(d): - return _depth(d * alpha) - - x = _inverted_res_block(x, 1, depth(16), 3, 2, se_ratio, relu, 0) - x = _inverted_res_block(x, 72.0 / 16, depth(24), 3, 2, None, relu, 1) - x = _inverted_res_block(x, 88.0 / 24, depth(24), 3, 1, None, relu, 2) - x = _inverted_res_block(x, 4, depth(40), kernel, 2, se_ratio, activation, 3) - x = _inverted_res_block(x, 6, depth(40), kernel, 1, se_ratio, activation, 4) - x = _inverted_res_block(x, 6, depth(40), kernel, 1, se_ratio, activation, 5) - x = _inverted_res_block(x, 3, depth(48), kernel, 1, se_ratio, activation, 6) - x = _inverted_res_block(x, 3, depth(48), kernel, 1, se_ratio, activation, 7) - x = _inverted_res_block(x, 6, depth(96), kernel, 2, se_ratio, activation, 8) - x = _inverted_res_block(x, 6, depth(96), kernel, 1, se_ratio, activation, 9) - x = _inverted_res_block(x, 6, depth(96), kernel, 1, se_ratio, activation, 10) - return x - - return MobileNetV3( - stack_fn, - 1024, - input_shape, - alpha, - "small", - minimalistic, - include_top, - weights, - input_tensor, - classes, - pooling, - dropout_rate, - classifier_activation, - include_preprocessing, - ) - - -@keras_export("keras.applications.MobileNetV3Large") -def MobileNetV3Large( - input_shape=None, - alpha=1.0, - minimalistic=False, - include_top=True, - weights="imagenet", - input_tensor=None, - classes=1000, - pooling=None, - dropout_rate=0.2, - classifier_activation="softmax", - include_preprocessing=True, -): - def stack_fn(x, kernel, activation, se_ratio): - def depth(d): - return _depth(d * alpha) - - x = _inverted_res_block(x, 1, depth(16), 3, 1, None, relu, 0) - x = _inverted_res_block(x, 4, depth(24), 3, 2, None, relu, 1) - x = _inverted_res_block(x, 3, depth(24), 3, 1, None, relu, 2) - x = _inverted_res_block(x, 3, depth(40), kernel, 2, se_ratio, relu, 3) - x = _inverted_res_block(x, 3, depth(40), kernel, 1, se_ratio, relu, 4) - x = _inverted_res_block(x, 3, depth(40), kernel, 1, se_ratio, relu, 5) - x = _inverted_res_block(x, 6, depth(80), 3, 2, None, activation, 6) - x = _inverted_res_block(x, 2.5, depth(80), 3, 1, None, activation, 7) - x = _inverted_res_block(x, 2.3, depth(80), 3, 1, None, activation, 8) - x = _inverted_res_block(x, 2.3, depth(80), 3, 1, None, activation, 9) - x = _inverted_res_block(x, 6, depth(112), 3, 1, se_ratio, activation, 10) - x = _inverted_res_block(x, 6, depth(112), 3, 1, se_ratio, activation, 11) - x = _inverted_res_block(x, 6, depth(160), kernel, 2, se_ratio, activation, 12) - x = _inverted_res_block(x, 6, depth(160), kernel, 1, se_ratio, activation, 13) - x = _inverted_res_block(x, 6, depth(160), kernel, 1, se_ratio, activation, 14) - return x - - return MobileNetV3( - stack_fn, - 1280, - input_shape, - alpha, - "large", - minimalistic, - include_top, - weights, - input_tensor, - classes, - pooling, - dropout_rate, - classifier_activation, - include_preprocessing, - ) - - -MobileNetV3Small.__doc__ = BASE_DOCSTRING.format(name="MobileNetV3Small") -MobileNetV3Large.__doc__ = BASE_DOCSTRING.format(name="MobileNetV3Large") - - -def relu(x): - return layers.ReLU()(x) - - -def hard_sigmoid(x): - return layers.ReLU(6.0)(x + 3.0) * (1.0 / 6.0) - - -def hard_swish(x): - return layers.Multiply()([x, hard_sigmoid(x)]) - - -# This function is taken from the original tf repo. -# It ensures that all layers have a channel number that is divisible by 8 -# It can be seen here: -# https://github.com/tensorflow/models/blob/master/research/ -# slim/nets/mobilenet/mobilenet.py - - -def _depth(v, divisor=8, min_value=None): - if min_value is None: - min_value = divisor - new_v = max(min_value, int(v + divisor / 2) // divisor * divisor) - # Make sure that round down does not go down by more than 10%. - if new_v < 0.9 * v: - new_v += divisor - return new_v - - -def _se_block(inputs, filters, se_ratio, prefix): - x = layers.GlobalAveragePooling2D(keepdims=True, name=prefix + "squeeze_excite/AvgPool")(inputs) - x = layers.Conv2D( - _depth(filters * se_ratio), - kernel_size=1, - padding="same", - name=prefix + "squeeze_excite/Conv", - )(x) - x = layers.ReLU(name=prefix + "squeeze_excite/Relu")(x) - x = layers.Conv2D( - filters, - kernel_size=1, - padding="same", - name=prefix + "squeeze_excite/Conv_1", - )(x) - x = hard_sigmoid(x) - x = layers.Multiply(name=prefix + "squeeze_excite/Mul")([inputs, x]) - return x - - -def _inverted_res_block(x, expansion, filters, kernel_size, stride, se_ratio, activation, block_id): - channel_axis = 1 if backend.image_data_format() == "channels_first" else -1 - shortcut = x - prefix = "expanded_conv/" - infilters = backend.int_shape(x)[channel_axis] - if block_id: - # Expand - prefix = f"expanded_conv_{block_id}/" - x = layers.Conv2D( - _depth(infilters * expansion), - kernel_size=1, - padding="same", - use_bias=False, - name=prefix + "expand", - )(x) - x = layers.BatchNormalization( - axis=channel_axis, - epsilon=1e-3, - momentum=0.999, - name=prefix + "expand/BatchNorm", - )(x) - x = activation(x) - - if stride == 2: - x = layers.ZeroPadding2D( - padding=imagenet_utils.correct_pad(x, kernel_size), - name=prefix + "depthwise/pad", - )(x) - x = layers.DepthwiseConv2D( - kernel_size, - strides=stride, - padding="same" if stride == 1 else "valid", - use_bias=False, - name=prefix + "depthwise", - )(x) - x = layers.BatchNormalization( - axis=channel_axis, - epsilon=1e-3, - momentum=0.999, - name=prefix + "depthwise/BatchNorm", - )(x) - x = activation(x) - - if se_ratio: - x = _se_block(x, _depth(infilters * expansion), se_ratio, prefix) - - x = layers.Conv2D( - filters, - kernel_size=1, - padding="same", - use_bias=False, - name=prefix + "project", - )(x) - x = layers.BatchNormalization( - axis=channel_axis, - epsilon=1e-3, - momentum=0.999, - name=prefix + "project/BatchNorm", - )(x) - - if stride == 1 and infilters == filters: - x = layers.Add(name=prefix + "Add")([shortcut, x]) - return x - - -@keras_export("keras.applications.mobilenet_v3.preprocess_input") -def preprocess_input(x, data_format=None): - """A placeholder method for backward compatibility. - - The preprocessing logic has been included in the mobilenet_v3 model - implementation. Users are no longer required to call this method to - normalize the input data. This method does nothing and only kept as a - placeholder to align the API surface between old and new version of model. - - Args: - x: A floating point `numpy.array` or a `tf.Tensor`. - data_format: Optional data format of the image tensor/array. `None` means - the global setting `tf.keras.backend.image_data_format()` is used - (unless you changed it, it uses "channels_last"). - Defaults to `None`. - - Returns: - Unchanged `numpy.array` or `tf.Tensor`. - """ - return x - - -@keras_export("keras.applications.mobilenet_v3.decode_predictions") -def decode_predictions(preds, top=5): - return imagenet_utils.decode_predictions(preds, top=top) - - -decode_predictions.__doc__ = imagenet_utils.decode_predictions.__doc__ diff --git a/Archive/keras_applications_mod/models/nasnet.py b/Archive/keras_applications_mod/models/nasnet.py deleted file mode 100644 index d419dd5..0000000 --- a/Archive/keras_applications_mod/models/nasnet.py +++ /dev/null @@ -1,867 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""NASNet-A models for Keras. - -NASNet refers to Neural Architecture Search Network, a family of models -that were designed automatically by learning the model architectures -directly on the dataset of interest. - -Here we consider NASNet-A, the highest performance model that was found -for the CIFAR-10 dataset, and then extended to ImageNet 2012 dataset, -obtaining state of the art performance on CIFAR-10 and ImageNet 2012. -Only the NASNet-A models, and their respective weights, which are suited -for ImageNet 2012 are provided. - -The below table describes the performance on ImageNet 2012: ---------------------------------------------------------------------------- -Architecture | Top-1 Acc | Top-5 Acc | Multiply-Adds | Params (M) ----------------------|-----------|-----------|----------------|------------ -NASNet-A (4 @ 1056) | 74.0 % | 91.6 % | 564 M | 5.3 -NASNet-A (6 @ 4032) | 82.7 % | 96.2 % | 23.8 B | 88.9 - -Reference: - - [Learning Transferable Architectures for Scalable Image Recognition]( - https://arxiv.org/abs/1707.07012) (CVPR 2018) -""" - -import tensorflow.compat.v2 as tf - -from keras import backend -from keras.applications import imagenet_utils -from keras.engine import training -from keras.layers import VersionAwareLayers -from keras.utils import data_utils -from keras.utils import layer_utils - -# isort: off -from tensorflow.python.platform import tf_logging as logging -from tensorflow.python.util.tf_export import keras_export - -BASE_WEIGHTS_PATH = "https://storage.googleapis.com/tensorflow/keras-applications/nasnet/" -NASNET_MOBILE_WEIGHT_PATH = BASE_WEIGHTS_PATH + "NASNet-mobile.h5" -NASNET_MOBILE_WEIGHT_PATH_NO_TOP = BASE_WEIGHTS_PATH + "NASNet-mobile-no-top.h5" -NASNET_LARGE_WEIGHT_PATH = BASE_WEIGHTS_PATH + "NASNet-large.h5" -NASNET_LARGE_WEIGHT_PATH_NO_TOP = BASE_WEIGHTS_PATH + "NASNet-large-no-top.h5" - -layers = VersionAwareLayers() - - -def NASNet( - input_shape=None, - penultimate_filters=4032, - num_blocks=6, - stem_block_filters=96, - skip_reduction=True, - filter_multiplier=2, - include_top=True, - weights="imagenet", - input_tensor=None, - pooling=None, - classes=1000, - default_size=None, - classifier_activation="softmax", -): - """Instantiates a NASNet model. - - Reference: - - [Learning Transferable Architectures for Scalable Image Recognition]( - https://arxiv.org/abs/1707.07012) (CVPR 2018) - - For image classification use cases, see - [this page for detailed examples]( - https://keras.io/api/applications/#usage-examples-for-image-classification-models). - - For transfer learning use cases, make sure to read the - [guide to transfer learning & fine-tuning]( - https://keras.io/guides/transfer_learning/). - - Note: each Keras Application expects a specific kind of input preprocessing. - For NasNet, call `tf.keras.applications.nasnet.preprocess_input` - on your inputs before passing them to the model. - `nasnet.preprocess_input` will scale input pixels between -1 and 1. - - Args: - input_shape: Optional shape tuple, the input shape - is by default `(331, 331, 3)` for NASNetLarge and - `(224, 224, 3)` for NASNetMobile. - It should have exactly 3 input channels, - and width and height should be no smaller than 32. - E.g. `(224, 224, 3)` would be one valid value. - penultimate_filters: Number of filters in the penultimate layer. - NASNet models use the notation `NASNet (N @ P)`, where: - - N is the number of blocks - - P is the number of penultimate filters - num_blocks: Number of repeated blocks of the NASNet model. - NASNet models use the notation `NASNet (N @ P)`, where: - - N is the number of blocks - - P is the number of penultimate filters - stem_block_filters: Number of filters in the initial stem block - skip_reduction: Whether to skip the reduction step at the tail - end of the network. - filter_multiplier: Controls the width of the network. - - If `filter_multiplier` < 1.0, proportionally decreases the number - of filters in each layer. - - If `filter_multiplier` > 1.0, proportionally increases the number - of filters in each layer. - - If `filter_multiplier` = 1, default number of filters from the - paper are used at each layer. - include_top: Whether to include the fully-connected - layer at the top of the network. - weights: `None` (random initialization) or - `imagenet` (ImageNet weights) - input_tensor: Optional Keras tensor (i.e. output of - `layers.Input()`) - to use as image input for the model. - pooling: Optional pooling mode for feature extraction - when `include_top` is `False`. - - `None` means that the output of the model - will be the 4D tensor output of the - last convolutional block. - - `avg` means that global average pooling - will be applied to the output of the - last convolutional block, and thus - the output of the model will be a - 2D tensor. - - `max` means that global max pooling will - be applied. - classes: Optional number of classes to classify images - into, only to be specified if `include_top` is True, and - if no `weights` argument is specified. - default_size: Specifies the default image size of the model - classifier_activation: A `str` or callable. The activation function to use - on the "top" layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" layer. - When loading pretrained weights, `classifier_activation` can only - be `None` or `"softmax"`. - - Returns: - A `keras.Model` instance. - """ - if not (weights in {"imagenet", None} or tf.io.gfile.exists(weights)): - raise ValueError( - "The `weights` argument should be either " - "`None` (random initialization), `imagenet` " - "(pre-training on ImageNet), " - "or the path to the weights file to be loaded." - ) - - if weights == "imagenet" and include_top and classes != 1000: - raise ValueError('If using `weights` as `"imagenet"` with `include_top` ' "as true, `classes` should be 1000") - - if isinstance(input_shape, tuple) and None in input_shape and weights == "imagenet": - raise ValueError( - "When specifying the input shape of a NASNet" - " and loading `ImageNet` weights, " - "the input_shape argument must be static " - "(no None entries). Got: `input_shape=" + str(input_shape) + "`." - ) - - if default_size is None: - default_size = 331 - - # Determine proper input shape and default size. - input_shape = imagenet_utils.obtain_input_shape( - input_shape, - default_size=default_size, - min_size=32, - data_format=backend.image_data_format(), - require_flatten=include_top, - weights=weights, - ) - - if backend.image_data_format() != "channels_last": - logging.warning( - "The NASNet family of models is only available " - 'for the input data format "channels_last" ' - "(width, height, channels). " - "However your settings specify the default " - 'data format "channels_first" (channels, width, height).' - ' You should set `image_data_format="channels_last"` ' - "in your Keras config located at ~/.keras/keras.json. " - "The model being returned right now will expect inputs " - 'to follow the "channels_last" data format.' - ) - backend.set_image_data_format("channels_last") - old_data_format = "channels_first" - else: - old_data_format = None - - if input_tensor is None: - img_input = layers.Input(shape=input_shape) - else: - if not backend.is_keras_tensor(input_tensor): - img_input = layers.Input(tensor=input_tensor, shape=input_shape) - else: - img_input = input_tensor - - if penultimate_filters % (24 * (filter_multiplier**2)) != 0: - raise ValueError( - "For NASNet-A models, the `penultimate_filters` must be a multiple " - "of 24 * (`filter_multiplier` ** 2). Current value: %d" % penultimate_filters - ) - - channel_dim = 1 if backend.image_data_format() == "channels_first" else -1 - filters = penultimate_filters // 24 - - x = layers.Conv2D( - stem_block_filters, - (3, 3), - strides=(2, 2), - padding="valid", - use_bias=False, - name="stem_conv1", - kernel_initializer="he_normal", - )(img_input) - - x = layers.BatchNormalization(axis=channel_dim, momentum=0.9997, epsilon=1e-3, name="stem_bn1")(x) - - p = None - x, p = _reduction_a_cell(x, p, filters // (filter_multiplier**2), block_id="stem_1") - x, p = _reduction_a_cell(x, p, filters // filter_multiplier, block_id="stem_2") - - for i in range(num_blocks): - x, p = _normal_a_cell(x, p, filters, block_id="%d" % (i)) - - x, p0 = _reduction_a_cell(x, p, filters * filter_multiplier, block_id="reduce_%d" % (num_blocks)) - - p = p0 if not skip_reduction else p - - for i in range(num_blocks): - x, p = _normal_a_cell( - x, - p, - filters * filter_multiplier, - block_id="%d" % (num_blocks + i + 1), - ) - - x, p0 = _reduction_a_cell( - x, - p, - filters * filter_multiplier**2, - block_id="reduce_%d" % (2 * num_blocks), - ) - - p = p0 if not skip_reduction else p - - for i in range(num_blocks): - x, p = _normal_a_cell( - x, - p, - filters * filter_multiplier**2, - block_id="%d" % (2 * num_blocks + i + 1), - ) - - x = layers.Activation("relu")(x) - - if include_top: - x = layers.GlobalAveragePooling2D()(x) - imagenet_utils.validate_activation(classifier_activation, weights) - x = layers.Dense(classes, activation=classifier_activation, name="predictions")(x) - else: - if pooling == "avg": - x = layers.GlobalAveragePooling2D()(x) - elif pooling == "max": - x = layers.GlobalMaxPooling2D()(x) - - # Ensure that the model takes into account - # any potential predecessors of `input_tensor`. - if input_tensor is not None: - inputs = layer_utils.get_source_inputs(input_tensor) - else: - inputs = img_input - - model = training.Model(inputs, x, name="NASNet") - - # Load weights. - if weights == "imagenet": - if default_size == 224: # mobile version - if include_top: - weights_path = data_utils.get_file( - "nasnet_mobile.h5", - NASNET_MOBILE_WEIGHT_PATH, - cache_subdir="models", - file_hash="020fb642bf7360b370c678b08e0adf61", - ) - else: - weights_path = data_utils.get_file( - "nasnet_mobile_no_top.h5", - NASNET_MOBILE_WEIGHT_PATH_NO_TOP, - cache_subdir="models", - file_hash="1ed92395b5b598bdda52abe5c0dbfd63", - ) - model.load_weights(weights_path) - elif default_size == 331: # large version - if include_top: - weights_path = data_utils.get_file( - "nasnet_large.h5", - NASNET_LARGE_WEIGHT_PATH, - cache_subdir="models", - file_hash="11577c9a518f0070763c2b964a382f17", - ) - else: - weights_path = data_utils.get_file( - "nasnet_large_no_top.h5", - NASNET_LARGE_WEIGHT_PATH_NO_TOP, - cache_subdir="models", - file_hash="d81d89dc07e6e56530c4e77faddd61b5", - ) - model.load_weights(weights_path) - else: - raise ValueError("ImageNet weights can only be loaded with NASNetLarge" " or NASNetMobile") - elif weights is not None: - model.load_weights(weights) - - if old_data_format: - backend.set_image_data_format(old_data_format) - - return model - - -@keras_export("keras.applications.nasnet.NASNetMobile", "keras.applications.NASNetMobile") -def NASNetMobile( - input_shape=None, - include_top=True, - weights="imagenet", - input_tensor=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - """Instantiates a Mobile NASNet model in ImageNet mode. - - Reference: - - [Learning Transferable Architectures for Scalable Image Recognition]( - https://arxiv.org/abs/1707.07012) (CVPR 2018) - - Optionally loads weights pre-trained on ImageNet. - Note that the data format convention used by the model is - the one specified in your Keras config at `~/.keras/keras.json`. - - Note: each Keras Application expects a specific kind of input preprocessing. - For NASNet, call `tf.keras.applications.nasnet.preprocess_input` on your - inputs before passing them to the model. - - Args: - input_shape: Optional shape tuple, only to be specified - if `include_top` is False (otherwise the input shape - has to be `(224, 224, 3)` for NASNetMobile - It should have exactly 3 inputs channels, - and width and height should be no smaller than 32. - E.g. `(224, 224, 3)` would be one valid value. - include_top: Whether to include the fully-connected - layer at the top of the network. - weights: `None` (random initialization) or - `imagenet` (ImageNet weights). For loading `imagenet` weights, - `input_shape` should be (224, 224, 3) - input_tensor: Optional Keras tensor (i.e. output of - `layers.Input()`) - to use as image input for the model. - pooling: Optional pooling mode for feature extraction - when `include_top` is `False`. - - `None` means that the output of the model - will be the 4D tensor output of the - last convolutional layer. - - `avg` means that global average pooling - will be applied to the output of the - last convolutional layer, and thus - the output of the model will be a - 2D tensor. - - `max` means that global max pooling will - be applied. - classes: Optional number of classes to classify images - into, only to be specified if `include_top` is True, and - if no `weights` argument is specified. - classifier_activation: A `str` or callable. The activation function to - use on the "top" layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" - layer. When loading pretrained weights, `classifier_activation` can - only be `None` or `"softmax"`. - - Returns: - A Keras model instance. - - Raises: - ValueError: In case of invalid argument for `weights`, - or invalid input shape. - RuntimeError: If attempting to run this model with a - backend that does not support separable convolutions. - """ - return NASNet( - input_shape, - penultimate_filters=1056, - num_blocks=4, - stem_block_filters=32, - skip_reduction=False, - filter_multiplier=2, - include_top=include_top, - weights=weights, - input_tensor=input_tensor, - pooling=pooling, - classes=classes, - default_size=224, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.nasnet.NASNetLarge", "keras.applications.NASNetLarge") -def NASNetLarge( - input_shape=None, - include_top=True, - weights="imagenet", - input_tensor=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - """Instantiates a NASNet model in ImageNet mode. - - Reference: - - [Learning Transferable Architectures for Scalable Image Recognition]( - https://arxiv.org/abs/1707.07012) (CVPR 2018) - - Optionally loads weights pre-trained on ImageNet. - Note that the data format convention used by the model is - the one specified in your Keras config at `~/.keras/keras.json`. - - Note: each Keras Application expects a specific kind of input preprocessing. - For NASNet, call `tf.keras.applications.nasnet.preprocess_input` on your - inputs before passing them to the model. - - Args: - input_shape: Optional shape tuple, only to be specified - if `include_top` is False (otherwise the input shape - has to be `(331, 331, 3)` for NASNetLarge. - It should have exactly 3 inputs channels, - and width and height should be no smaller than 32. - E.g. `(224, 224, 3)` would be one valid value. - include_top: Whether to include the fully-connected - layer at the top of the network. - weights: `None` (random initialization) or - `imagenet` (ImageNet weights). For loading `imagenet` weights, - `input_shape` should be (331, 331, 3) - input_tensor: Optional Keras tensor (i.e. output of - `layers.Input()`) - to use as image input for the model. - pooling: Optional pooling mode for feature extraction - when `include_top` is `False`. - - `None` means that the output of the model - will be the 4D tensor output of the - last convolutional layer. - - `avg` means that global average pooling - will be applied to the output of the - last convolutional layer, and thus - the output of the model will be a - 2D tensor. - - `max` means that global max pooling will - be applied. - classes: Optional number of classes to classify images - into, only to be specified if `include_top` is True, and - if no `weights` argument is specified. - classifier_activation: A `str` or callable. The activation function to - use on the "top" layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" - layer. When loading pretrained weights, `classifier_activation` can - only be `None` or `"softmax"`. - - Returns: - A Keras model instance. - - Raises: - ValueError: in case of invalid argument for `weights`, - or invalid input shape. - RuntimeError: If attempting to run this model with a - backend that does not support separable convolutions. - """ - return NASNet( - input_shape, - penultimate_filters=4032, - num_blocks=6, - stem_block_filters=96, - skip_reduction=True, - filter_multiplier=2, - include_top=include_top, - weights=weights, - input_tensor=input_tensor, - pooling=pooling, - classes=classes, - default_size=331, - classifier_activation=classifier_activation, - ) - - -def _separable_conv_block(ip, filters, kernel_size=(3, 3), strides=(1, 1), block_id=None): - """Adds 2 blocks of [relu-separable conv-batchnorm]. - - Args: - ip: Input tensor - filters: Number of output filters per layer - kernel_size: Kernel size of separable convolutions - strides: Strided convolution for downsampling - block_id: String block_id - - Returns: - A Keras tensor - """ - channel_dim = 1 if backend.image_data_format() == "channels_first" else -1 - - with backend.name_scope(f"separable_conv_block_{block_id}"): - x = layers.Activation("relu")(ip) - if strides == (2, 2): - x = layers.ZeroPadding2D( - padding=imagenet_utils.correct_pad(x, kernel_size), - name=f"separable_conv_1_pad_{block_id}", - )(x) - conv_pad = "valid" - else: - conv_pad = "same" - x = layers.SeparableConv2D( - filters, - kernel_size, - strides=strides, - name=f"separable_conv_1_{block_id}", - padding=conv_pad, - use_bias=False, - kernel_initializer="he_normal", - )(x) - x = layers.BatchNormalization( - axis=channel_dim, - momentum=0.9997, - epsilon=1e-3, - name=f"separable_conv_1_bn_{block_id}", - )(x) - x = layers.Activation("relu")(x) - x = layers.SeparableConv2D( - filters, - kernel_size, - name=f"separable_conv_2_{block_id}", - padding="same", - use_bias=False, - kernel_initializer="he_normal", - )(x) - x = layers.BatchNormalization( - axis=channel_dim, - momentum=0.9997, - epsilon=1e-3, - name=f"separable_conv_2_bn_{block_id}", - )(x) - return x - - -def _adjust_block(p, ip, filters, block_id=None): - """Adjusts the input `previous path` to match the shape of the `input`. - - Used in situations where the output number of filters needs to be changed. - - Args: - p: Input tensor which needs to be modified - ip: Input tensor whose shape needs to be matched - filters: Number of output filters to be matched - block_id: String block_id - - Returns: - Adjusted Keras tensor - """ - channel_dim = 1 if backend.image_data_format() == "channels_first" else -1 - img_dim = 2 if backend.image_data_format() == "channels_first" else -2 - - ip_shape = backend.int_shape(ip) - - if p is not None: - p_shape = backend.int_shape(p) - - with backend.name_scope("adjust_block"): - if p is None: - p = ip - - elif p_shape[img_dim] != ip_shape[img_dim]: - with backend.name_scope(f"adjust_reduction_block_{block_id}"): - p = layers.Activation("relu", name=f"adjust_relu_1_{block_id}")(p) - p1 = layers.AveragePooling2D( - (1, 1), - strides=(2, 2), - padding="valid", - name=f"adjust_avg_pool_1_{block_id}", - )(p) - p1 = layers.Conv2D( - filters // 2, - (1, 1), - padding="same", - use_bias=False, - name=f"adjust_conv_1_{block_id}", - kernel_initializer="he_normal", - )(p1) - - p2 = layers.ZeroPadding2D(padding=((0, 1), (0, 1)))(p) - p2 = layers.Cropping2D(cropping=((1, 0), (1, 0)))(p2) - p2 = layers.AveragePooling2D( - (1, 1), - strides=(2, 2), - padding="valid", - name=f"adjust_avg_pool_2_{block_id}", - )(p2) - p2 = layers.Conv2D( - filters // 2, - (1, 1), - padding="same", - use_bias=False, - name=f"adjust_conv_2_{block_id}", - kernel_initializer="he_normal", - )(p2) - - p = layers.concatenate([p1, p2], axis=channel_dim) - p = layers.BatchNormalization( - axis=channel_dim, - momentum=0.9997, - epsilon=1e-3, - name=f"adjust_bn_{block_id}", - )(p) - - elif p_shape[channel_dim] != filters: - with backend.name_scope(f"adjust_projection_block_{block_id}"): - p = layers.Activation("relu")(p) - p = layers.Conv2D( - filters, - (1, 1), - strides=(1, 1), - padding="same", - name=f"adjust_conv_projection_{block_id}", - use_bias=False, - kernel_initializer="he_normal", - )(p) - p = layers.BatchNormalization( - axis=channel_dim, - momentum=0.9997, - epsilon=1e-3, - name=f"adjust_bn_{block_id}", - )(p) - return p - - -def _normal_a_cell(ip, p, filters, block_id=None): - """Adds a Normal cell for NASNet-A (Fig. 4 in the paper). - - Args: - ip: Input tensor `x` - p: Input tensor `p` - filters: Number of output filters - block_id: String block_id - - Returns: - A Keras tensor - """ - channel_dim = 1 if backend.image_data_format() == "channels_first" else -1 - - with backend.name_scope(f"normal_A_block_{block_id}"): - p = _adjust_block(p, ip, filters, block_id) - - h = layers.Activation("relu")(ip) - h = layers.Conv2D( - filters, - (1, 1), - strides=(1, 1), - padding="same", - name=f"normal_conv_1_{block_id}", - use_bias=False, - kernel_initializer="he_normal", - )(h) - h = layers.BatchNormalization( - axis=channel_dim, - momentum=0.9997, - epsilon=1e-3, - name=f"normal_bn_1_{block_id}", - )(h) - - with backend.name_scope("block_1"): - x1_1 = _separable_conv_block( - h, - filters, - kernel_size=(5, 5), - block_id=f"normal_left1_{block_id}", - ) - x1_2 = _separable_conv_block(p, filters, block_id=f"normal_right1_{block_id}") - x1 = layers.add([x1_1, x1_2], name=f"normal_add_1_{block_id}") - - with backend.name_scope("block_2"): - x2_1 = _separable_conv_block(p, filters, (5, 5), block_id=f"normal_left2_{block_id}") - x2_2 = _separable_conv_block(p, filters, (3, 3), block_id=f"normal_right2_{block_id}") - x2 = layers.add([x2_1, x2_2], name=f"normal_add_2_{block_id}") - - with backend.name_scope("block_3"): - x3 = layers.AveragePooling2D( - (3, 3), - strides=(1, 1), - padding="same", - name=f"normal_left3_{block_id}", - )(h) - x3 = layers.add([x3, p], name=f"normal_add_3_{block_id}") - - with backend.name_scope("block_4"): - x4_1 = layers.AveragePooling2D( - (3, 3), - strides=(1, 1), - padding="same", - name=f"normal_left4_{block_id}", - )(p) - x4_2 = layers.AveragePooling2D( - (3, 3), - strides=(1, 1), - padding="same", - name=f"normal_right4_{block_id}", - )(p) - x4 = layers.add([x4_1, x4_2], name=f"normal_add_4_{block_id}") - - with backend.name_scope("block_5"): - x5 = _separable_conv_block(h, filters, block_id=f"normal_left5_{block_id}") - x5 = layers.add([x5, h], name=f"normal_add_5_{block_id}") - - x = layers.concatenate( - [p, x1, x2, x3, x4, x5], - axis=channel_dim, - name=f"normal_concat_{block_id}", - ) - return x, ip - - -def _reduction_a_cell(ip, p, filters, block_id=None): - """Adds a Reduction cell for NASNet-A (Fig. 4 in the paper). - - Args: - ip: Input tensor `x` - p: Input tensor `p` - filters: Number of output filters - block_id: String block_id - - Returns: - A Keras tensor - """ - channel_dim = 1 if backend.image_data_format() == "channels_first" else -1 - - with backend.name_scope(f"reduction_A_block_{block_id}"): - p = _adjust_block(p, ip, filters, block_id) - - h = layers.Activation("relu")(ip) - h = layers.Conv2D( - filters, - (1, 1), - strides=(1, 1), - padding="same", - name=f"reduction_conv_1_{block_id}", - use_bias=False, - kernel_initializer="he_normal", - )(h) - h = layers.BatchNormalization( - axis=channel_dim, - momentum=0.9997, - epsilon=1e-3, - name=f"reduction_bn_1_{block_id}", - )(h) - h3 = layers.ZeroPadding2D( - padding=imagenet_utils.correct_pad(h, 3), - name=f"reduction_pad_1_{block_id}", - )(h) - - with backend.name_scope("block_1"): - x1_1 = _separable_conv_block( - h, - filters, - (5, 5), - strides=(2, 2), - block_id=f"reduction_left1_{block_id}", - ) - x1_2 = _separable_conv_block( - p, - filters, - (7, 7), - strides=(2, 2), - block_id=f"reduction_right1_{block_id}", - ) - x1 = layers.add([x1_1, x1_2], name=f"reduction_add_1_{block_id}") - - with backend.name_scope("block_2"): - x2_1 = layers.MaxPooling2D( - (3, 3), - strides=(2, 2), - padding="valid", - name=f"reduction_left2_{block_id}", - )(h3) - x2_2 = _separable_conv_block( - p, - filters, - (7, 7), - strides=(2, 2), - block_id=f"reduction_right2_{block_id}", - ) - x2 = layers.add([x2_1, x2_2], name=f"reduction_add_2_{block_id}") - - with backend.name_scope("block_3"): - x3_1 = layers.AveragePooling2D( - (3, 3), - strides=(2, 2), - padding="valid", - name=f"reduction_left3_{block_id}", - )(h3) - x3_2 = _separable_conv_block( - p, - filters, - (5, 5), - strides=(2, 2), - block_id=f"reduction_right3_{block_id}", - ) - x3 = layers.add([x3_1, x3_2], name=f"reduction_add3_{block_id}") - - with backend.name_scope("block_4"): - x4 = layers.AveragePooling2D( - (3, 3), - strides=(1, 1), - padding="same", - name=f"reduction_left4_{block_id}", - )(x1) - x4 = layers.add([x2, x4]) - - with backend.name_scope("block_5"): - x5_1 = _separable_conv_block(x1, filters, (3, 3), block_id=f"reduction_left4_{block_id}") - x5_2 = layers.MaxPooling2D( - (3, 3), - strides=(2, 2), - padding="valid", - name=f"reduction_right5_{block_id}", - )(h3) - x5 = layers.add([x5_1, x5_2], name=f"reduction_add4_{block_id}") - - x = layers.concatenate( - [x2, x3, x4, x5], - axis=channel_dim, - name=f"reduction_concat_{block_id}", - ) - return x, ip - - -@keras_export("keras.applications.nasnet.preprocess_input") -def preprocess_input(x, data_format=None): - return imagenet_utils.preprocess_input(x, data_format=data_format, mode="tf") - - -@keras_export("keras.applications.nasnet.decode_predictions") -def decode_predictions(preds, top=5): - return imagenet_utils.decode_predictions(preds, top=top) - - -preprocess_input.__doc__ = imagenet_utils.PREPROCESS_INPUT_DOC.format( - mode="", - ret=imagenet_utils.PREPROCESS_INPUT_RET_DOC_TF, - error=imagenet_utils.PREPROCESS_INPUT_ERROR_DOC, -) -decode_predictions.__doc__ = imagenet_utils.decode_predictions.__doc__ diff --git a/Archive/keras_applications_mod/models/regnet.py b/Archive/keras_applications_mod/models/regnet.py deleted file mode 100644 index 7186c25..0000000 --- a/Archive/keras_applications_mod/models/regnet.py +++ /dev/null @@ -1,1754 +0,0 @@ -# Copyright 2021 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - - -"""RegNet models for Keras. - -References: - -- [Designing Network Design Spaces](https://arxiv.org/abs/2003.13678) - (CVPR 2020) -- [Fast and Accurate Model Scaling](https://arxiv.org/abs/2103.06877) - (CVPR 2021) -""" - -import tensorflow.compat.v2 as tf - -from keras import backend -from keras import layers -from keras.applications import imagenet_utils -from keras.engine import training -from keras.utils import data_utils -from keras.utils import layer_utils - -# isort: off -from tensorflow.python.util.tf_export import keras_export - -BASE_WEIGHTS_PATH = "https://storage.googleapis.com/tensorflow/keras-applications/regnet/" - -WEIGHTS_HASHES = { - "x002": ( - "49fb46e56cde07fdaf57bffd851461a86548f6a3a4baef234dd37290b826c0b8", - "5445b66cd50445eb7ecab094c1e78d4d3d29375439d1a7798861c4af15ffff21", - ), - "x004": ( - "3523c7f5ac0dbbcc2fd6d83b3570e7540f7449d3301cc22c29547302114e4088", - "de139bf07a66c9256f2277bf5c1b6dd2d5a3a891a5f8a925a10c8a0a113fd6f3", - ), - "x006": ( - "340216ef334a7bae30daac9f414e693c136fac9ab868704bbfcc9ce6a5ec74bb", - "a43ec97ad62f86b2a96a783bfdc63a5a54de02eef54f26379ea05e1bf90a9505", - ), - "x008": ( - "8f145d6a5fae6da62677bb8d26eb92d0b9dfe143ec1ebf68b24a57ae50a2763d", - "3c7e4b0917359304dc18e644475c5c1f5e88d795542b676439c4a3acd63b7207", - ), - "x016": ( - "31c386f4c7bfef4c021a583099aa79c1b3928057ba1b7d182f174674c5ef3510", - "1b8e3d545d190271204a7b2165936a227d26b79bb7922bac5ee4d303091bf17a", - ), - "x032": ( - "6c025df1409e5ea846375bc9dfa240956cca87ef57384d93fef7d6fa90ca8c7f", - "9cd4522806c0fcca01b37874188b2bd394d7c419956d77472a4e072b01d99041", - ), - "x040": ( - "ba128046c588a26dbd3b3a011b26cb7fa3cf8f269c184c132372cb20b6eb54c1", - "b4ed0ca0b9a98e789e05000e830403a7ade4d8afa01c73491c44610195198afe", - ), - "x064": ( - "0f4489c3cd3ad979bd6b0324213998bcb36dc861d178f977997ebfe53c3ba564", - "3e706fa416a18dfda14c713423eba8041ae2509db3e0a611d5f599b5268a46c4", - ), - "x080": ( - "76320e43272719df648db37271a247c22eb6e810fe469c37a5db7e2cb696d162", - "7b1ce8e29ceefec10a6569640ee329dba7fbc98b5d0f6346aabade058b66cf29", - ), - "x120": ( - "5cafc461b78897d5e4f24e68cb406d18e75f31105ef620e7682b611bb355eb3a", - "36174ddd0299db04a42631d028abcb1cc7afec2b705e42bd28fcd325e5d596bf", - ), - "x160": ( - "8093f57a5824b181fb734ea21ae34b1f7ee42c5298e63cf6d587c290973195d2", - "9d1485050bdf19531ffa1ed7827c75850e0f2972118a996b91aa9264b088fd43", - ), - "x320": ( - "91fb3e6f4e9e44b3687e80977f7f4412ee9937c0c704232664fc83e4322ea01e", - "9db7eacc37b85c98184070e1a172e6104c00846f44bcd4e727da9e50d9692398", - ), - "y002": ( - "1e8091c674532b1a61c04f6393a9c570113e0197f22bd1b98cc4c4fe800c6465", - "f63221f63d625b8e201221499682587bfe29d33f50a4c4f4d53be00f66c0f12c", - ), - "y004": ( - "752fdbad21c78911bf1dcb8c513e5a0e14697b068e5d9e73525dbaa416d18d8e", - "45e6ba8309a17a77e67afc05228454b2e0ee6be0dae65edc0f31f1da10cc066b", - ), - "y006": ( - "98942e07b273da500ff9699a1f88aca78dfad4375faabb0bab784bb0dace80a9", - "b70261cba4e60013c99d130cc098d2fce629ff978a445663b6fa4f8fc099a2be", - ), - "y008": ( - "1b099377cc9a4fb183159a6f9b24bc998e5659d25a449f40c90cbffcbcfdcae4", - "b11f5432a216ee640fe9be6e32939defa8d08b8d136349bf3690715a98752ca1", - ), - "y016": ( - "b7ce1f5e223f0941c960602de922bcf846288ce7a4c33b2a4f2e4ac4b480045b", - "d7404f50205e82d793e219afb9eb2bfeb781b6b2d316a6128c6d7d7dacab7f57", - ), - "y032": ( - "6a6a545cf3549973554c9b94f0cd40e25f229fffb1e7f7ac779a59dcbee612bd", - "eb3ac1c45ec60f4f031c3f5180573422b1cf7bebc26c004637517372f68f8937", - ), - "y040": ( - "98d00118b335162bbffe8f1329e54e5c8e75ee09b2a5414f97b0ddfc56e796f6", - "b5be2a5e5f072ecdd9c0b8a437cd896df0efa1f6a1f77e41caa8719b7dfcb05d", - ), - "y064": ( - "65c948c7a18aaecaad2d1bd4fd978987425604ba6669ef55a1faa0069a2804b7", - "885c4b7ed7ea339daca7dafa1a62cb7d41b1068897ef90a5a3d71b4a2e2db31a", - ), - "y080": ( - "7a2c62da2982e369a4984d3c7c3b32d6f8d3748a71cb37a31156c436c37f3e95", - "3d119577e1e3bf8d153b895e8ea9e4ec150ff2d92abdca711b6e949c3fd7115d", - ), - "y120": ( - "a96ab0d27d3ae35a422ee7df0d789069b3e3217a99334e0ce861a96595bc5986", - "4a6fa387108380b730b71feea2ad80b5224b5ea9dc21dc156c93fe3c6186485c", - ), - "y160": ( - "45067240ffbc7ca2591313fee2f80dbdda6d66ec1a7451446f9a6d00d8f7ac6e", - "ead1e6b568be8f34447ec8941299a9df4368736ba9a8205de5427fa20a1fb316", - ), - "y320": ( - "b05e173e4ae635cfa22d06392ee3741284d17dadfee68f2aa6fd8cb2b7561112", - "cad78f74a586e24c61d38be17f3ae53bb9674380174d2585da1a526b8c20e1fd", - ), -} - -# The widths and depths are deduced from a quantized linear function. For -# more information, please refer to "Designing Network Design Spaces" by -# Radosavovic et al. - -# BatchNorm momentum and epsilon values taken from original implementation. - -MODEL_CONFIGS = { - "x002": { - "depths": [1, 1, 4, 7], - "widths": [24, 56, 152, 368], - "group_width": 8, - "default_size": 224, - "block_type": "X", - }, - "x004": { - "depths": [1, 2, 7, 12], - "widths": [32, 64, 160, 384], - "group_width": 16, - "default_size": 224, - "block_type": "X", - }, - "x006": { - "depths": [1, 3, 5, 7], - "widths": [48, 96, 240, 528], - "group_width": 24, - "default_size": 224, - "block_type": "X", - }, - "x008": { - "depths": [1, 3, 7, 5], - "widths": [64, 128, 288, 672], - "group_width": 16, - "default_size": 224, - "block_type": "X", - }, - "x016": { - "depths": [2, 4, 10, 2], - "widths": [72, 168, 408, 912], - "group_width": 24, - "default_size": 224, - "block_type": "X", - }, - "x032": { - "depths": [2, 6, 15, 2], - "widths": [96, 192, 432, 1008], - "group_width": 48, - "default_size": 224, - "block_type": "X", - }, - "x040": { - "depths": [2, 5, 14, 2], - "widths": [80, 240, 560, 1360], - "group_width": 40, - "default_size": 224, - "block_type": "X", - }, - "x064": { - "depths": [2, 4, 10, 1], - "widths": [168, 392, 784, 1624], - "group_width": 56, - "default_size": 224, - "block_type": "X", - }, - "x080": { - "depths": [2, 5, 15, 1], - "widths": [80, 240, 720, 1920], - "group_width": 120, - "default_size": 224, - "block_type": "X", - }, - "x120": { - "depths": [2, 5, 11, 1], - "widths": [224, 448, 896, 2240], - "group_width": 112, - "default_size": 224, - "block_type": "X", - }, - "x160": { - "depths": [2, 6, 13, 1], - "widths": [256, 512, 896, 2048], - "group_width": 128, - "default_size": 224, - "block_type": "X", - }, - "x320": { - "depths": [2, 7, 13, 1], - "widths": [336, 672, 1344, 2520], - "group_width": 168, - "default_size": 224, - "block_type": "X", - }, - "y002": { - "depths": [1, 1, 4, 7], - "widths": [24, 56, 152, 368], - "group_width": 8, - "default_size": 224, - "block_type": "Y", - }, - "y004": { - "depths": [1, 3, 6, 6], - "widths": [48, 104, 208, 440], - "group_width": 8, - "default_size": 224, - "block_type": "Y", - }, - "y006": { - "depths": [1, 3, 7, 4], - "widths": [48, 112, 256, 608], - "group_width": 16, - "default_size": 224, - "block_type": "Y", - }, - "y008": { - "depths": [1, 3, 8, 2], - "widths": [64, 128, 320, 768], - "group_width": 16, - "default_size": 224, - "block_type": "Y", - }, - "y016": { - "depths": [2, 6, 17, 2], - "widths": [48, 120, 336, 888], - "group_width": 24, - "default_size": 224, - "block_type": "Y", - }, - "y032": { - "depths": [2, 5, 13, 1], - "widths": [72, 216, 576, 1512], - "group_width": 24, - "default_size": 224, - "block_type": "Y", - }, - "y040": { - "depths": [2, 6, 12, 2], - "widths": [128, 192, 512, 1088], - "group_width": 64, - "default_size": 224, - "block_type": "Y", - }, - "y064": { - "depths": [2, 7, 14, 2], - "widths": [144, 288, 576, 1296], - "group_width": 72, - "default_size": 224, - "block_type": "Y", - }, - "y080": { - "depths": [2, 4, 10, 1], - "widths": [168, 448, 896, 2016], - "group_width": 56, - "default_size": 224, - "block_type": "Y", - }, - "y120": { - "depths": [2, 5, 11, 1], - "widths": [224, 448, 896, 2240], - "group_width": 112, - "default_size": 224, - "block_type": "Y", - }, - "y160": { - "depths": [2, 4, 11, 1], - "widths": [224, 448, 1232, 3024], - "group_width": 112, - "default_size": 224, - "block_type": "Y", - }, - "y320": { - "depths": [2, 5, 12, 1], - "widths": [232, 696, 1392, 3712], - "group_width": 232, - "default_size": 224, - "block_type": "Y", - }, -} - -BASE_DOCSTRING = """Instantiates the {name} architecture. - - Reference: - - [Designing Network Design Spaces](https://arxiv.org/abs/2003.13678) - (CVPR 2020) - - For image classification use cases, see - [this page for detailed examples]( - https://keras.io/api/applications/#usage-examples-for-image-classification-models). - - For transfer learning use cases, make sure to read the - [guide to transfer learning & fine-tuning]( - https://keras.io/guides/transfer_learning/). - - Note: Each Keras Application expects a specific kind of input preprocessing. - For Regnets, preprocessing is included in the model using a `Rescaling` layer. - RegNet models expect their inputs to be float or uint8 tensors of pixels with - values in the [0-255] range. - - The naming of models is as follows: `RegNet` where - `block_type` is one of `(X, Y)` and `flops` signifies hundred million - floating point operations. For example RegNetY064 corresponds to RegNet with - Y block and 6.4 giga flops (64 hundred million flops). - - Args: - include_top: Whether to include the fully-connected - layer at the top of the network. Defaults to `True`. - weights: One of `None` (random initialization), - `"imagenet"` (pre-training on ImageNet), or the path to the weights - file to be loaded. Defaults to `"imagenet"`. - input_tensor: Optional Keras tensor - (i.e. output of `layers.Input()`) - to use as image input for the model. - input_shape: Optional shape tuple, only to be specified - if `include_top` is False. - It should have exactly 3 inputs channels. - pooling: Optional pooling mode for feature extraction - when `include_top` is `False`. - - `None` means that the output of the model will be - the 4D tensor output of the - last convolutional layer. - - `avg` means that global average pooling - will be applied to the output of the - last convolutional layer, and thus - the output of the model will be a 2D tensor. - - `max` means that global max pooling will - be applied. - Defaults to `None`. - classes: Optional number of classes to classify images - into, only to be specified if `include_top` is True, and - if no `weights` argument is specified. 1000 is how many - ImageNet classes there are. Defaults to `1000`. - classifier_activation: A `str` or callable. The activation function to use - on the "top" layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" layer. - When loading pretrained weights, `classifier_activation` can only - be `None` or `"softmax"`. Defaults to `"softmax"`. - - Returns: - A `keras.Model` instance. -""" - - -def PreStem(name=None): - """Rescales and normalizes inputs to [0,1] and ImageNet mean and std. - - Args: - name: name prefix - - Returns: - Rescaled and normalized tensor - """ - if name is None: - name = "prestem" + str(backend.get_uid("prestem")) - - def apply(x): - x = layers.Rescaling(scale=1.0 / 255.0, name=name + "_prestem_rescaling")(x) - return x - - return apply - - -def Stem(name=None): - """Implementation of RegNet stem. - - (Common to all model variants) - Args: - name: name prefix - - Returns: - Output tensor of the Stem - """ - if name is None: - name = "stem" + str(backend.get_uid("stem")) - - def apply(x): - x = layers.Conv2D( - 32, - (3, 3), - strides=2, - use_bias=False, - padding="same", - kernel_initializer="he_normal", - name=name + "_stem_conv", - )(x) - x = layers.BatchNormalization(momentum=0.9, epsilon=1e-5, name=name + "_stem_bn")(x) - x = layers.ReLU(name=name + "_stem_relu")(x) - return x - - return apply - - -def SqueezeAndExciteBlock(filters_in, se_filters, name=None): - """Implements the Squeeze & Excite block (https://arxiv.org/abs/1709.01507). - - Args: - filters_in: input filters to the block - se_filters: filters to squeeze to - name: name prefix - - Returns: - A function object - """ - if name is None: - name = str(backend.get_uid("squeeze_and_excite")) - - def apply(inputs): - x = layers.GlobalAveragePooling2D(name=name + "_squeeze_and_excite_gap", keepdims=True)(inputs) - x = layers.Conv2D( - se_filters, - (1, 1), - activation="relu", - kernel_initializer="he_normal", - name=name + "_squeeze_and_excite_squeeze", - )(x) - x = layers.Conv2D( - filters_in, - (1, 1), - activation="sigmoid", - kernel_initializer="he_normal", - name=name + "_squeeze_and_excite_excite", - )(x) - x = tf.math.multiply(x, inputs) - return x - - return apply - - -def XBlock(filters_in, filters_out, group_width, stride=1, name=None): - """Implementation of X Block. - - Reference: [Designing Network Design - Spaces](https://arxiv.org/abs/2003.13678) - Args: - filters_in: filters in the input tensor - filters_out: filters in the output tensor - group_width: group width - stride: stride - name: name prefix - Returns: - Output tensor of the block - """ - if name is None: - name = str(backend.get_uid("xblock")) - - def apply(inputs): - if filters_in != filters_out and stride == 1: - raise ValueError( - f"Input filters({filters_in}) and output " - f"filters({filters_out}) " - f"are not equal for stride {stride}. Input and output filters " - f"must be equal for stride={stride}." - ) - - # Declare layers - groups = filters_out // group_width - - if stride != 1: - skip = layers.Conv2D( - filters_out, - (1, 1), - strides=stride, - use_bias=False, - kernel_initializer="he_normal", - name=name + "_skip_1x1", - )(inputs) - skip = layers.BatchNormalization(momentum=0.9, epsilon=1e-5, name=name + "_skip_bn")(skip) - else: - skip = inputs - - # Build block - # conv_1x1_1 - x = layers.Conv2D( - filters_out, - (1, 1), - use_bias=False, - kernel_initializer="he_normal", - name=name + "_conv_1x1_1", - )(inputs) - x = layers.BatchNormalization(momentum=0.9, epsilon=1e-5, name=name + "_conv_1x1_1_bn")(x) - x = layers.ReLU(name=name + "_conv_1x1_1_relu")(x) - - # conv_3x3 - x = layers.Conv2D( - filters_out, - (3, 3), - use_bias=False, - strides=stride, - groups=groups, - padding="same", - kernel_initializer="he_normal", - name=name + "_conv_3x3", - )(x) - x = layers.BatchNormalization(momentum=0.9, epsilon=1e-5, name=name + "_conv_3x3_bn")(x) - x = layers.ReLU(name=name + "_conv_3x3_relu")(x) - - # conv_1x1_2 - x = layers.Conv2D( - filters_out, - (1, 1), - use_bias=False, - kernel_initializer="he_normal", - name=name + "_conv_1x1_2", - )(x) - x = layers.BatchNormalization(momentum=0.9, epsilon=1e-5, name=name + "_conv_1x1_2_bn")(x) - - x = layers.ReLU(name=name + "_exit_relu")(x + skip) - - return x - - return apply - - -def YBlock( - filters_in, - filters_out, - group_width, - stride=1, - squeeze_excite_ratio=0.25, - name=None, -): - """Implementation of Y Block. - - Reference: [Designing Network Design - Spaces](https://arxiv.org/abs/2003.13678) - Args: - filters_in: filters in the input tensor - filters_out: filters in the output tensor - group_width: group width - stride: stride - squeeze_excite_ratio: expansion ration for Squeeze and Excite block - name: name prefix - Returns: - Output tensor of the block - """ - if name is None: - name = str(backend.get_uid("yblock")) - - def apply(inputs): - if filters_in != filters_out and stride == 1: - raise ValueError( - f"Input filters({filters_in}) and output " - f"filters({filters_out}) " - f"are not equal for stride {stride}. Input and output filters " - f"must be equal for stride={stride}." - ) - - groups = filters_out // group_width - se_filters = int(filters_in * squeeze_excite_ratio) - - if stride != 1: - skip = layers.Conv2D( - filters_out, - (1, 1), - strides=stride, - use_bias=False, - kernel_initializer="he_normal", - name=name + "_skip_1x1", - )(inputs) - skip = layers.BatchNormalization(momentum=0.9, epsilon=1e-5, name=name + "_skip_bn")(skip) - else: - skip = inputs - - # Build block - # conv_1x1_1 - x = layers.Conv2D( - filters_out, - (1, 1), - use_bias=False, - kernel_initializer="he_normal", - name=name + "_conv_1x1_1", - )(inputs) - x = layers.BatchNormalization(momentum=0.9, epsilon=1e-5, name=name + "_conv_1x1_1_bn")(x) - x = layers.ReLU(name=name + "_conv_1x1_1_relu")(x) - - # conv_3x3 - x = layers.Conv2D( - filters_out, - (3, 3), - use_bias=False, - strides=stride, - groups=groups, - padding="same", - kernel_initializer="he_normal", - name=name + "_conv_3x3", - )(x) - x = layers.BatchNormalization(momentum=0.9, epsilon=1e-5, name=name + "_conv_3x3_bn")(x) - x = layers.ReLU(name=name + "_conv_3x3_relu")(x) - - # Squeeze-Excitation block - x = SqueezeAndExciteBlock(filters_out, se_filters, name=name)(x) - - # conv_1x1_2 - x = layers.Conv2D( - filters_out, - (1, 1), - use_bias=False, - kernel_initializer="he_normal", - name=name + "_conv_1x1_2", - )(x) - x = layers.BatchNormalization(momentum=0.9, epsilon=1e-5, name=name + "_conv_1x1_2_bn")(x) - - x = layers.ReLU(name=name + "_exit_relu")(x + skip) - - return x - - return apply - - -def ZBlock( - filters_in, - filters_out, - group_width, - stride=1, - squeeze_excite_ratio=0.25, - bottleneck_ratio=0.25, - name=None, -): - """Implementation of Z block Reference: [Fast and Accurate Model - Scaling](https://arxiv.org/abs/2103.06877). - - Args: - filters_in: filters in the input tensor - filters_out: filters in the output tensor - group_width: group width - stride: stride - squeeze_excite_ratio: expansion ration for Squeeze and Excite block - bottleneck_ratio: inverted bottleneck ratio - name: name prefix - Returns: - Output tensor of the block - """ - if name is None: - name = str(backend.get_uid("zblock")) - - def apply(inputs): - if filters_in != filters_out and stride == 1: - raise ValueError( - f"Input filters({filters_in}) and output filters({filters_out})" - f"are not equal for stride {stride}. Input and output filters " - f"must be equal for stride={stride}." - ) - - groups = filters_out // group_width - se_filters = int(filters_in * squeeze_excite_ratio) - - inv_btlneck_filters = int(filters_out / bottleneck_ratio) - - # Build block - # conv_1x1_1 - x = layers.Conv2D( - inv_btlneck_filters, - (1, 1), - use_bias=False, - kernel_initializer="he_normal", - name=name + "_conv_1x1_1", - )(inputs) - x = layers.BatchNormalization(momentum=0.9, epsilon=1e-5, name=name + "_conv_1x1_1_bn")(x) - x = tf.nn.silu(x) - - # conv_3x3 - x = layers.Conv2D( - inv_btlneck_filters, - (3, 3), - use_bias=False, - strides=stride, - groups=groups, - padding="same", - kernel_initializer="he_normal", - name=name + "_conv_3x3", - )(x) - x = layers.BatchNormalization(momentum=0.9, epsilon=1e-5, name=name + "_conv_3x3_bn")(x) - x = tf.nn.silu(x) - - # Squeeze-Excitation block - x = SqueezeAndExciteBlock(inv_btlneck_filters, se_filters, name=name) - - # conv_1x1_2 - x = layers.Conv2D( - filters_out, - (1, 1), - use_bias=False, - kernel_initializer="he_normal", - name=name + "_conv_1x1_2", - )(x) - x = layers.BatchNormalization(momentum=0.9, epsilon=1e-5, name=name + "_conv_1x1_2_bn")(x) - - if stride != 1: - return x - else: - return x + inputs - - return apply - - -def Stage(block_type, depth, group_width, filters_in, filters_out, name=None): - """Implementation of Stage in RegNet. - - Args: - block_type: must be one of "X", "Y", "Z" - depth: depth of stage, number of blocks to use - group_width: group width of all blocks in this stage - filters_in: input filters to this stage - filters_out: output filters from this stage - name: name prefix - - Returns: - Output tensor of Stage - """ - if name is None: - name = str(backend.get_uid("stage")) - - def apply(inputs): - x = inputs - if block_type == "X": - x = XBlock( - filters_in, - filters_out, - group_width, - stride=2, - name=f"{name}_XBlock_0", - )(x) - for i in range(1, depth): - x = XBlock( - filters_out, - filters_out, - group_width, - name=f"{name}_XBlock_{i}", - )(x) - elif block_type == "Y": - x = YBlock( - filters_in, - filters_out, - group_width, - stride=2, - name=name + "_YBlock_0", - )(x) - for i in range(1, depth): - x = YBlock( - filters_out, - filters_out, - group_width, - name=f"{name}_YBlock_{i}", - )(x) - elif block_type == "Z": - x = ZBlock( - filters_in, - filters_out, - group_width, - stride=2, - name=f"{name}_ZBlock_0", - )(x) - for i in range(1, depth): - x = ZBlock( - filters_out, - filters_out, - group_width, - name=f"{name}_ZBlock_{i}", - )(x) - else: - raise NotImplementedError(f"Block type `{block_type}` not recognized." "block_type must be one of (`X`, `Y`, `Z`). ") - return x - - return apply - - -def Head(num_classes=1000, name=None): - """Implementation of classification head of RegNet. - - Args: - num_classes: number of classes for Dense layer - name: name prefix - - Returns: - Classification head function. - """ - if name is None: - name = str(backend.get_uid("head")) - - def apply(x): - x = layers.GlobalAveragePooling2D(name=name + "_head_gap")(x) - x = layers.Dense(num_classes, name=name + "head_dense")(x) - return x - - return apply - - -def RegNet( - depths, - widths, - group_width, - block_type, - default_size, - model_name="regnet", - include_preprocessing=True, - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - """Instantiates RegNet architecture given specific configuration. - - Args: - depths: An iterable containing depths for each individual stages. - widths: An iterable containing output channel width of each individual - stages - group_width: Number of channels to be used in each group. See grouped - convolutions for more information. - block_type: Must be one of `{"X", "Y", "Z"}`. For more details see the - papers "Designing network design spaces" and "Fast and Accurate Model - Scaling" - default_size: Default input image size. - model_name: An optional name for the model. - include_preprocessing: boolean denoting whther to include preprocessing in - the model - include_top: Boolean denoting whether to include classification head to - the model. - weights: one of `None` (random initialization), "imagenet" (pre-training - on ImageNet), or the path to the weights file to be loaded. - input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to - use as image input for the model. - input_shape: optional shape tuple, only to be specified if `include_top` - is False. It should have exactly 3 inputs channels. - pooling: optional pooling mode for feature extraction when `include_top` - is `False`. - `None` means that the output of the model will be the 4D - tensor output of the last convolutional layer. - `avg` means that global - average pooling will be applied to the output of the last convolutional - layer, and thus the output of the model will be a 2D tensor. - `max` - means that global max pooling will be applied. - classes: optional number of classes to classify images into, only to be - specified if `include_top` is True, and if no `weights` argument is - specified. - classifier_activation: A `str` or callable. The activation function to use - on the "top" layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" layer. - - Returns: - A `keras.Model` instance. - - Raises: - ValueError: in case of invalid argument for `weights`, - or invalid input shape. - ValueError: if `classifier_activation` is not `softmax` or `None` when - using a pretrained top layer. - ValueError: if `include_top` is True but `num_classes` is not 1000. - ValueError: if `block_type` is not one of `{"X", "Y", "Z"}` - - """ - if not (weights in {"imagenet", None} or tf.io.gfile.exists(weights)): - raise ValueError( - "The `weights` argument should be either " - "`None` (random initialization), `imagenet` " - "(pre-training on ImageNet), " - "or the path to the weights file to be loaded." - ) - - if weights == "imagenet" and include_top and classes != 1000: - raise ValueError("If using `weights` as `'imagenet'` with `include_top`" " as true, `classes` should be 1000") - - # Determine proper input shape - input_shape = imagenet_utils.obtain_input_shape( - input_shape, - default_size=default_size, - min_size=32, - data_format=backend.image_data_format(), - require_flatten=include_top, - weights=weights, - ) - - if input_tensor is None: - img_input = layers.Input(shape=input_shape) - else: - if not backend.is_keras_tensor(input_tensor): - img_input = layers.Input(tensor=input_tensor, shape=input_shape) - else: - img_input = input_tensor - - if input_tensor is not None: - inputs = layer_utils.get_source_inputs(input_tensor)[0] - else: - inputs = img_input - - x = inputs - if include_preprocessing: - x = PreStem(name=model_name)(x) - x = Stem(name=model_name)(x) - - in_channels = 32 # Output from Stem - - for num_stage in range(4): - depth = depths[num_stage] - out_channels = widths[num_stage] - - x = Stage( - block_type, - depth, - group_width, - in_channels, - out_channels, - name=model_name + "_Stage_" + str(num_stage), - )(x) - in_channels = out_channels - - if include_top: - x = Head(num_classes=classes)(x) - imagenet_utils.validate_activation(classifier_activation, weights) - - else: - if pooling == "avg": - x = layers.GlobalAveragePooling2D()(x) - elif pooling == "max": - x = layers.GlobalMaxPooling2D()(x) - - model = training.Model(inputs=inputs, outputs=x, name=model_name) - - # Load weights. - if weights == "imagenet": - if include_top: - file_suffix = ".h5" - file_hash = WEIGHTS_HASHES[model_name[-4:]][0] - else: - file_suffix = "_notop.h5" - file_hash = WEIGHTS_HASHES[model_name[-4:]][1] - file_name = model_name + file_suffix - weights_path = data_utils.get_file( - file_name, - BASE_WEIGHTS_PATH + file_name, - cache_subdir="models", - file_hash=file_hash, - ) - model.load_weights(weights_path) - elif weights is not None: - model.load_weights(weights) - - return model - - -## Instantiating variants ## - - -@keras_export("keras.applications.regnet.RegNetX002", "keras.applications.RegNetX002") -def RegNetX002( - model_name="regnetx002", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return RegNet( - MODEL_CONFIGS["x002"]["depths"], - MODEL_CONFIGS["x002"]["widths"], - MODEL_CONFIGS["x002"]["group_width"], - MODEL_CONFIGS["x002"]["block_type"], - MODEL_CONFIGS["x002"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.regnet.RegNetX004", "keras.applications.RegNetX004") -def RegNetX004( - model_name="regnetx004", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return RegNet( - MODEL_CONFIGS["x004"]["depths"], - MODEL_CONFIGS["x004"]["widths"], - MODEL_CONFIGS["x004"]["group_width"], - MODEL_CONFIGS["x004"]["block_type"], - MODEL_CONFIGS["x004"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.regnet.RegNetX006", "keras.applications.RegNetX006") -def RegNetX006( - model_name="regnetx006", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return RegNet( - MODEL_CONFIGS["x006"]["depths"], - MODEL_CONFIGS["x006"]["widths"], - MODEL_CONFIGS["x006"]["group_width"], - MODEL_CONFIGS["x006"]["block_type"], - MODEL_CONFIGS["x006"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.regnet.RegNetX008", "keras.applications.RegNetX008") -def RegNetX008( - model_name="regnetx008", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return RegNet( - MODEL_CONFIGS["x008"]["depths"], - MODEL_CONFIGS["x008"]["widths"], - MODEL_CONFIGS["x008"]["group_width"], - MODEL_CONFIGS["x008"]["block_type"], - MODEL_CONFIGS["x008"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.regnet.RegNetX016", "keras.applications.RegNetX016") -def RegNetX016( - model_name="regnetx016", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return RegNet( - MODEL_CONFIGS["x016"]["depths"], - MODEL_CONFIGS["x016"]["widths"], - MODEL_CONFIGS["x016"]["group_width"], - MODEL_CONFIGS["x016"]["block_type"], - MODEL_CONFIGS["x016"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.regnet.RegNetX032", "keras.applications.RegNetX032") -def RegNetX032( - model_name="regnetx032", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return RegNet( - MODEL_CONFIGS["x032"]["depths"], - MODEL_CONFIGS["x032"]["widths"], - MODEL_CONFIGS["x032"]["group_width"], - MODEL_CONFIGS["x032"]["block_type"], - MODEL_CONFIGS["x032"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.regnet.RegNetX040", "keras.applications.RegNetX040") -def RegNetX040( - model_name="regnetx040", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return RegNet( - MODEL_CONFIGS["x040"]["depths"], - MODEL_CONFIGS["x040"]["widths"], - MODEL_CONFIGS["x040"]["group_width"], - MODEL_CONFIGS["x040"]["block_type"], - MODEL_CONFIGS["x040"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.regnet.RegNetX064", "keras.applications.RegNetX064") -def RegNetX064( - model_name="regnetx064", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return RegNet( - MODEL_CONFIGS["x064"]["depths"], - MODEL_CONFIGS["x064"]["widths"], - MODEL_CONFIGS["x064"]["group_width"], - MODEL_CONFIGS["x064"]["block_type"], - MODEL_CONFIGS["x064"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.regnet.RegNetX080", "keras.applications.RegNetX080") -def RegNetX080( - model_name="regnetx080", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return RegNet( - MODEL_CONFIGS["x080"]["depths"], - MODEL_CONFIGS["x080"]["widths"], - MODEL_CONFIGS["x080"]["group_width"], - MODEL_CONFIGS["x080"]["block_type"], - MODEL_CONFIGS["x080"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.regnet.RegNetX120", "keras.applications.RegNetX120") -def RegNetX120( - model_name="regnetx120", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return RegNet( - MODEL_CONFIGS["x120"]["depths"], - MODEL_CONFIGS["x120"]["widths"], - MODEL_CONFIGS["x120"]["group_width"], - MODEL_CONFIGS["x120"]["block_type"], - MODEL_CONFIGS["x120"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.regnet.RegNetX160", "keras.applications.RegNetX160") -def RegNetX160( - model_name="regnetx160", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return RegNet( - MODEL_CONFIGS["x160"]["depths"], - MODEL_CONFIGS["x160"]["widths"], - MODEL_CONFIGS["x160"]["group_width"], - MODEL_CONFIGS["x160"]["block_type"], - MODEL_CONFIGS["x160"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.regnet.RegNetX320", "keras.applications.RegNetX320") -def RegNetX320( - model_name="regnetx320", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return RegNet( - MODEL_CONFIGS["x320"]["depths"], - MODEL_CONFIGS["x320"]["widths"], - MODEL_CONFIGS["x320"]["group_width"], - MODEL_CONFIGS["x320"]["block_type"], - MODEL_CONFIGS["x320"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.regnet.RegNetY002", "keras.applications.RegNetY002") -def RegNetY002( - model_name="regnety002", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return RegNet( - MODEL_CONFIGS["y002"]["depths"], - MODEL_CONFIGS["y002"]["widths"], - MODEL_CONFIGS["y002"]["group_width"], - MODEL_CONFIGS["y002"]["block_type"], - MODEL_CONFIGS["y002"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.regnet.RegNetY004", "keras.applications.RegNetY004") -def RegNetY004( - model_name="regnety004", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return RegNet( - MODEL_CONFIGS["y004"]["depths"], - MODEL_CONFIGS["y004"]["widths"], - MODEL_CONFIGS["y004"]["group_width"], - MODEL_CONFIGS["y004"]["block_type"], - MODEL_CONFIGS["y004"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.regnet.RegNetY006", "keras.applications.RegNetY006") -def RegNetY006( - model_name="regnety006", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return RegNet( - MODEL_CONFIGS["y006"]["depths"], - MODEL_CONFIGS["y006"]["widths"], - MODEL_CONFIGS["y006"]["group_width"], - MODEL_CONFIGS["y006"]["block_type"], - MODEL_CONFIGS["y006"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.regnet.RegNetY008", "keras.applications.RegNetY008") -def RegNetY008( - model_name="regnety008", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return RegNet( - MODEL_CONFIGS["y008"]["depths"], - MODEL_CONFIGS["y008"]["widths"], - MODEL_CONFIGS["y008"]["group_width"], - MODEL_CONFIGS["y008"]["block_type"], - MODEL_CONFIGS["y008"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.regnet.RegNetY016", "keras.applications.RegNetY016") -def RegNetY016( - model_name="regnety016", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return RegNet( - MODEL_CONFIGS["y016"]["depths"], - MODEL_CONFIGS["y016"]["widths"], - MODEL_CONFIGS["y016"]["group_width"], - MODEL_CONFIGS["y016"]["block_type"], - MODEL_CONFIGS["y016"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.regnet.RegNetY032", "keras.applications.RegNetY032") -def RegNetY032( - model_name="regnety032", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return RegNet( - MODEL_CONFIGS["y032"]["depths"], - MODEL_CONFIGS["y032"]["widths"], - MODEL_CONFIGS["y032"]["group_width"], - MODEL_CONFIGS["y032"]["block_type"], - MODEL_CONFIGS["y032"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.regnet.RegNetY040", "keras.applications.RegNetY040") -def RegNetY040( - model_name="regnety040", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return RegNet( - MODEL_CONFIGS["y040"]["depths"], - MODEL_CONFIGS["y040"]["widths"], - MODEL_CONFIGS["y040"]["group_width"], - MODEL_CONFIGS["y040"]["block_type"], - MODEL_CONFIGS["y040"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.regnet.RegNetY064", "keras.applications.RegNetY064") -def RegNetY064( - model_name="regnety064", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return RegNet( - MODEL_CONFIGS["y064"]["depths"], - MODEL_CONFIGS["y064"]["widths"], - MODEL_CONFIGS["y064"]["group_width"], - MODEL_CONFIGS["y064"]["block_type"], - MODEL_CONFIGS["y064"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.regnet.RegNetY080", "keras.applications.RegNetY080") -def RegNetY080( - model_name="regnety080", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return RegNet( - MODEL_CONFIGS["y080"]["depths"], - MODEL_CONFIGS["y080"]["widths"], - MODEL_CONFIGS["y080"]["group_width"], - MODEL_CONFIGS["y080"]["block_type"], - MODEL_CONFIGS["y080"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.regnet.RegNetY120", "keras.applications.RegNetY120") -def RegNetY120( - model_name="regnety120", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return RegNet( - MODEL_CONFIGS["y120"]["depths"], - MODEL_CONFIGS["y120"]["widths"], - MODEL_CONFIGS["y120"]["group_width"], - MODEL_CONFIGS["y120"]["block_type"], - MODEL_CONFIGS["y120"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.regnet.RegNetY160", "keras.applications.RegNetY160") -def RegNetY160( - model_name="regnety160", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return RegNet( - MODEL_CONFIGS["y160"]["depths"], - MODEL_CONFIGS["y160"]["widths"], - MODEL_CONFIGS["y160"]["group_width"], - MODEL_CONFIGS["y160"]["block_type"], - MODEL_CONFIGS["y160"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.regnet.RegNetY320", "keras.applications.RegNetY320") -def RegNetY320( - model_name="regnety320", - include_top=True, - include_preprocessing=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - return RegNet( - MODEL_CONFIGS["y320"]["depths"], - MODEL_CONFIGS["y320"]["widths"], - MODEL_CONFIGS["y320"]["group_width"], - MODEL_CONFIGS["y320"]["block_type"], - MODEL_CONFIGS["y320"]["default_size"], - model_name=model_name, - include_top=include_top, - include_preprocessing=include_preprocessing, - weights=weights, - input_tensor=input_tensor, - input_shape=input_shape, - pooling=pooling, - classes=classes, - classifier_activation=classifier_activation, - ) - - -RegNetX002.__doc__ = BASE_DOCSTRING.format(name="RegNetX002") -RegNetX004.__doc__ = BASE_DOCSTRING.format(name="RegNetX004") -RegNetX006.__doc__ = BASE_DOCSTRING.format(name="RegNetX006") -RegNetX008.__doc__ = BASE_DOCSTRING.format(name="RegNetX008") -RegNetX016.__doc__ = BASE_DOCSTRING.format(name="RegNetX016") -RegNetX032.__doc__ = BASE_DOCSTRING.format(name="RegNetX032") -RegNetX040.__doc__ = BASE_DOCSTRING.format(name="RegNetX040") -RegNetX064.__doc__ = BASE_DOCSTRING.format(name="RegNetX064") -RegNetX080.__doc__ = BASE_DOCSTRING.format(name="RegNetX080") -RegNetX120.__doc__ = BASE_DOCSTRING.format(name="RegNetX120") -RegNetX160.__doc__ = BASE_DOCSTRING.format(name="RegNetX160") -RegNetX320.__doc__ = BASE_DOCSTRING.format(name="RegNetX320") - -RegNetY002.__doc__ = BASE_DOCSTRING.format(name="RegNetY002") -RegNetY004.__doc__ = BASE_DOCSTRING.format(name="RegNetY004") -RegNetY006.__doc__ = BASE_DOCSTRING.format(name="RegNetY006") -RegNetY008.__doc__ = BASE_DOCSTRING.format(name="RegNetY008") -RegNetY016.__doc__ = BASE_DOCSTRING.format(name="RegNetY016") -RegNetY032.__doc__ = BASE_DOCSTRING.format(name="RegNetY032") -RegNetY040.__doc__ = BASE_DOCSTRING.format(name="RegNetY040") -RegNetY064.__doc__ = BASE_DOCSTRING.format(name="RegNetY064") -RegNetY080.__doc__ = BASE_DOCSTRING.format(name="RegNetY080") -RegNetY120.__doc__ = BASE_DOCSTRING.format(name="RegNetY120") -RegNetY160.__doc__ = BASE_DOCSTRING.format(name="RegNetY160") -RegNetY320.__doc__ = BASE_DOCSTRING.format(name="RegNetY320") - - -@keras_export("keras.applications.regnet.preprocess_input") -def preprocess_input(x, data_format=None): - """A placeholder method for backward compatibility. - - The preprocessing logic has been included in the regnet model - implementation. Users are no longer required to call this method to - normalize the input data. This method does nothing and only kept as a - placeholder to align the API surface between old and new version of model. - - Args: - x: A floating point `numpy.array` or a `tf.Tensor`. - data_format: Optional data format of the image tensor/array. `None` means - the global setting `tf.keras.backend.image_data_format()` is used - (unless you changed it, it uses "channels_last"). - Defaults to `None`. - - Returns: - Unchanged `numpy.array` or `tf.Tensor`. - """ - return x - - -@keras_export("keras.applications.regnet.decode_predictions") -def decode_predictions(preds, top=5): - return imagenet_utils.decode_predictions(preds, top=top) - - -decode_predictions.__doc__ = imagenet_utils.decode_predictions.__doc__ diff --git a/Archive/keras_applications_mod/models/resnet.py b/Archive/keras_applications_mod/models/resnet.py deleted file mode 100644 index 7ca226b..0000000 --- a/Archive/keras_applications_mod/models/resnet.py +++ /dev/null @@ -1,636 +0,0 @@ -# Copyright 2015 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""ResNet models for Keras. - -Reference: - - [Deep Residual Learning for Image Recognition]( - https://arxiv.org/abs/1512.03385) (CVPR 2015) -""" - -import tensorflow.compat.v2 as tf - -from keras import backend -from keras.applications import imagenet_utils -from keras.engine import training -from keras.layers import VersionAwareLayers -from keras.utils import data_utils -from keras.utils import layer_utils - -# isort: off -from tensorflow.python.util.tf_export import keras_export - -BASE_WEIGHTS_PATH = "https://storage.googleapis.com/tensorflow/keras-applications/resnet/" -WEIGHTS_HASHES = { - "resnet50": ( - "2cb95161c43110f7111970584f804107", - "4d473c1dd8becc155b73f8504c6f6626", - ), - "resnet101": ( - "f1aeb4b969a6efcfb50fad2f0c20cfc5", - "88cf7a10940856eca736dc7b7e228a21", - ), - "resnet152": ( - "100835be76be38e30d865e96f2aaae62", - "ee4c566cf9a93f14d82f913c2dc6dd0c", - ), - "resnet50v2": ( - "3ef43a0b657b3be2300d5770ece849e0", - "fac2f116257151a9d068a22e544a4917", - ), - "resnet101v2": ( - "6343647c601c52e1368623803854d971", - "c0ed64b8031c3730f411d2eb4eea35b5", - ), - "resnet152v2": ( - "a49b44d1979771252814e80f8ec446f9", - "ed17cf2e0169df9d443503ef94b23b33", - ), - "resnext50": ( - "67a5b30d522ed92f75a1f16eef299d1a", - "62527c363bdd9ec598bed41947b379fc", - ), - "resnext101": ( - "34fb605428fcc7aa4d62f44404c11509", - "0f678c91647380debd923963594981b3", - ), -} - -layers = None - - -def ResNet( - stack_fn, - preact, - use_bias, - model_name="resnet", - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", - **kwargs, -): - """Instantiates the ResNet, ResNetV2, and ResNeXt architecture. - - Args: - stack_fn: a function that returns output tensor for the - stacked residual blocks. - preact: whether to use pre-activation or not - (True for ResNetV2, False for ResNet and ResNeXt). - use_bias: whether to use biases for convolutional layers or not - (True for ResNet and ResNetV2, False for ResNeXt). - model_name: string, model name. - include_top: whether to include the fully-connected - layer at the top of the network. - weights: one of `None` (random initialization), - 'imagenet' (pre-training on ImageNet), - or the path to the weights file to be loaded. - input_tensor: optional Keras tensor - (i.e. output of `layers.Input()`) - to use as image input for the model. - input_shape: optional shape tuple, only to be specified - if `include_top` is False (otherwise the input shape - has to be `(224, 224, 3)` (with `channels_last` data format) - or `(3, 224, 224)` (with `channels_first` data format). - It should have exactly 3 inputs channels. - pooling: optional pooling mode for feature extraction - when `include_top` is `False`. - - `None` means that the output of the model will be - the 4D tensor output of the - last convolutional layer. - - `avg` means that global average pooling - will be applied to the output of the - last convolutional layer, and thus - the output of the model will be a 2D tensor. - - `max` means that global max pooling will - be applied. - classes: optional number of classes to classify images - into, only to be specified if `include_top` is True, and - if no `weights` argument is specified. - classifier_activation: A `str` or callable. The activation function to use - on the "top" layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" layer. - When loading pretrained weights, `classifier_activation` can only - be `None` or `"softmax"`. - **kwargs: For backwards compatibility only. - - Returns: - A `keras.Model` instance. - """ - global layers - if "layers" in kwargs: - layers = kwargs.pop("layers") - else: - layers = VersionAwareLayers() - if kwargs: - raise ValueError(f"Unknown argument(s): {kwargs}") - if not (weights in {"imagenet", None} or tf.io.gfile.exists(weights)): - raise ValueError( - "The `weights` argument should be either " - "`None` (random initialization), `imagenet` " - "(pre-training on ImageNet), " - "or the path to the weights file to be loaded." - ) - - if weights == "imagenet" and include_top and classes != 1000: - raise ValueError('If using `weights` as `"imagenet"` with `include_top`' " as true, `classes` should be 1000") - - # Determine proper input shape - input_shape = imagenet_utils.obtain_input_shape( - input_shape, - default_size=224, - min_size=32, - data_format=backend.image_data_format(), - require_flatten=include_top, - weights=weights, - ) - - if input_tensor is None: - img_input = layers.Input(shape=input_shape) - else: - if not backend.is_keras_tensor(input_tensor): - img_input = layers.Input(tensor=input_tensor, shape=input_shape) - else: - img_input = input_tensor - - bn_axis = 3 if backend.image_data_format() == "channels_last" else 1 - - x = layers.ZeroPadding2D(padding=((3, 3), (3, 3)), name="conv1_pad")(img_input) - x = layers.Conv2D(64, 7, strides=2, use_bias=use_bias, name="conv1_conv")(x) - - if not preact: - x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name="conv1_bn")(x) - x = layers.Activation("relu", name="conv1_relu")(x) - - x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)), name="pool1_pad")(x) - x = layers.MaxPooling2D(3, strides=2, name="pool1_pool")(x) - - x = stack_fn(x) - - if preact: - x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name="post_bn")(x) - x = layers.Activation("relu", name="post_relu")(x) - - if include_top: - x = layers.GlobalAveragePooling2D(name="avg_pool")(x) - imagenet_utils.validate_activation(classifier_activation, weights) - x = layers.Dense(classes, activation=classifier_activation, name="predictions")(x) - else: - if pooling == "avg": - x = layers.GlobalAveragePooling2D(name="avg_pool")(x) - elif pooling == "max": - x = layers.GlobalMaxPooling2D(name="max_pool")(x) - - # Ensure that the model takes into account - # any potential predecessors of `input_tensor`. - if input_tensor is not None: - inputs = layer_utils.get_source_inputs(input_tensor) - else: - inputs = img_input - - # Create model. - model = training.Model(inputs, x, name=model_name) - - # Load weights. - if (weights == "imagenet") and (model_name in WEIGHTS_HASHES): - if include_top: - file_name = model_name + "_weights_tf_dim_ordering_tf_kernels.h5" - file_hash = WEIGHTS_HASHES[model_name][0] - else: - file_name = model_name + "_weights_tf_dim_ordering_tf_kernels_notop.h5" - file_hash = WEIGHTS_HASHES[model_name][1] - weights_path = data_utils.get_file( - file_name, - BASE_WEIGHTS_PATH + file_name, - cache_subdir="models", - file_hash=file_hash, - ) - model.load_weights(weights_path) - elif weights is not None: - model.load_weights(weights) - - return model - - -def block1(x, filters, kernel_size=3, stride=1, conv_shortcut=True, name=None): - """A residual block. - - Args: - x: input tensor. - filters: integer, filters of the bottleneck layer. - kernel_size: default 3, kernel size of the bottleneck layer. - stride: default 1, stride of the first layer. - conv_shortcut: default True, use convolution shortcut if True, - otherwise identity shortcut. - name: string, block label. - - Returns: - Output tensor for the residual block. - """ - bn_axis = 3 if backend.image_data_format() == "channels_last" else 1 - - if conv_shortcut: - shortcut = layers.Conv2D(4 * filters, 1, strides=stride, name=name + "_0_conv")(x) - shortcut = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + "_0_bn")(shortcut) - else: - shortcut = x - - x = layers.Conv2D(filters, 1, strides=stride, name=name + "_1_conv")(x) - x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + "_1_bn")(x) - x = layers.Activation("relu", name=name + "_1_relu")(x) - - x = layers.Conv2D(filters, kernel_size, padding="SAME", name=name + "_2_conv")(x) - x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + "_2_bn")(x) - x = layers.Activation("relu", name=name + "_2_relu")(x) - - x = layers.Conv2D(4 * filters, 1, name=name + "_3_conv")(x) - x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + "_3_bn")(x) - - x = layers.Add(name=name + "_add")([shortcut, x]) - x = layers.Activation("relu", name=name + "_out")(x) - return x - - -def stack1(x, filters, blocks, stride1=2, name=None): - """A set of stacked residual blocks. - - Args: - x: input tensor. - filters: integer, filters of the bottleneck layer in a block. - blocks: integer, blocks in the stacked blocks. - stride1: default 2, stride of the first layer in the first block. - name: string, stack label. - - Returns: - Output tensor for the stacked blocks. - """ - x = block1(x, filters, stride=stride1, name=name + "_block1") - for i in range(2, blocks + 1): - x = block1(x, filters, conv_shortcut=False, name=name + "_block" + str(i)) - return x - - -def block2(x, filters, kernel_size=3, stride=1, conv_shortcut=False, name=None): - """A residual block. - - Args: - x: input tensor. - filters: integer, filters of the bottleneck layer. - kernel_size: default 3, kernel size of the bottleneck layer. - stride: default 1, stride of the first layer. - conv_shortcut: default False, use convolution shortcut if True, - otherwise identity shortcut. - name: string, block label. - - Returns: - Output tensor for the residual block. - """ - bn_axis = 3 if backend.image_data_format() == "channels_last" else 1 - - preact = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + "_preact_bn")(x) - preact = layers.Activation("relu", name=name + "_preact_relu")(preact) - - if conv_shortcut: - shortcut = layers.Conv2D(4 * filters, 1, strides=stride, name=name + "_0_conv")(preact) - else: - shortcut = layers.MaxPooling2D(1, strides=stride)(x) if stride > 1 else x - - x = layers.Conv2D(filters, 1, strides=1, use_bias=False, name=name + "_1_conv")(preact) - x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + "_1_bn")(x) - x = layers.Activation("relu", name=name + "_1_relu")(x) - - x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)), name=name + "_2_pad")(x) - x = layers.Conv2D( - filters, - kernel_size, - strides=stride, - use_bias=False, - name=name + "_2_conv", - )(x) - x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + "_2_bn")(x) - x = layers.Activation("relu", name=name + "_2_relu")(x) - - x = layers.Conv2D(4 * filters, 1, name=name + "_3_conv")(x) - x = layers.Add(name=name + "_out")([shortcut, x]) - return x - - -def stack2(x, filters, blocks, stride1=2, name=None): - """A set of stacked residual blocks. - - Args: - x: input tensor. - filters: integer, filters of the bottleneck layer in a block. - blocks: integer, blocks in the stacked blocks. - stride1: default 2, stride of the first layer in the first block. - name: string, stack label. - - Returns: - Output tensor for the stacked blocks. - """ - x = block2(x, filters, conv_shortcut=True, name=name + "_block1") - for i in range(2, blocks): - x = block2(x, filters, name=name + "_block" + str(i)) - x = block2(x, filters, stride=stride1, name=name + "_block" + str(blocks)) - return x - - -def block3( - x, - filters, - kernel_size=3, - stride=1, - groups=32, - conv_shortcut=True, - name=None, -): - """A residual block. - - Args: - x: input tensor. - filters: integer, filters of the bottleneck layer. - kernel_size: default 3, kernel size of the bottleneck layer. - stride: default 1, stride of the first layer. - groups: default 32, group size for grouped convolution. - conv_shortcut: default True, use convolution shortcut if True, - otherwise identity shortcut. - name: string, block label. - - Returns: - Output tensor for the residual block. - """ - bn_axis = 3 if backend.image_data_format() == "channels_last" else 1 - - if conv_shortcut: - shortcut = layers.Conv2D( - (64 // groups) * filters, - 1, - strides=stride, - use_bias=False, - name=name + "_0_conv", - )(x) - shortcut = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + "_0_bn")(shortcut) - else: - shortcut = x - - x = layers.Conv2D(filters, 1, use_bias=False, name=name + "_1_conv")(x) - x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + "_1_bn")(x) - x = layers.Activation("relu", name=name + "_1_relu")(x) - - c = filters // groups - x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)), name=name + "_2_pad")(x) - x = layers.DepthwiseConv2D( - kernel_size, - strides=stride, - depth_multiplier=c, - use_bias=False, - name=name + "_2_conv", - )(x) - x_shape = backend.shape(x)[:-1] - x = backend.reshape(x, backend.concatenate([x_shape, (groups, c, c)])) - x = layers.Lambda( - lambda x: sum(x[:, :, :, :, i] for i in range(c)), - name=name + "_2_reduce", - )(x) - x = backend.reshape(x, backend.concatenate([x_shape, (filters,)])) - x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + "_2_bn")(x) - x = layers.Activation("relu", name=name + "_2_relu")(x) - - x = layers.Conv2D((64 // groups) * filters, 1, use_bias=False, name=name + "_3_conv")(x) - x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + "_3_bn")(x) - - x = layers.Add(name=name + "_add")([shortcut, x]) - x = layers.Activation("relu", name=name + "_out")(x) - return x - - -def stack3(x, filters, blocks, stride1=2, groups=32, name=None): - """A set of stacked residual blocks. - - Args: - x: input tensor. - filters: integer, filters of the bottleneck layer in a block. - blocks: integer, blocks in the stacked blocks. - stride1: default 2, stride of the first layer in the first block. - groups: default 32, group size for grouped convolution. - name: string, stack label. - - Returns: - Output tensor for the stacked blocks. - """ - x = block3(x, filters, stride=stride1, groups=groups, name=name + "_block1") - for i in range(2, blocks + 1): - x = block3( - x, - filters, - groups=groups, - conv_shortcut=False, - name=name + "_block" + str(i), - ) - return x - - -@keras_export( - "keras.applications.resnet50.ResNet50", - "keras.applications.resnet.ResNet50", - "keras.applications.ResNet50", -) -def ResNet50( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - **kwargs, -): - """Instantiates the ResNet50 architecture.""" - - def stack_fn(x): - x = stack1(x, 64, 3, stride1=1, name="conv2") - x = stack1(x, 128, 4, name="conv3") - x = stack1(x, 256, 6, name="conv4") - return stack1(x, 512, 3, name="conv5") - - return ResNet( - stack_fn, - False, - True, - "resnet50", - include_top, - weights, - input_tensor, - input_shape, - pooling, - classes, - **kwargs, - ) - - -@keras_export("keras.applications.resnet.ResNet101", "keras.applications.ResNet101") -def ResNet101( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - **kwargs, -): - """Instantiates the ResNet101 architecture.""" - - def stack_fn(x): - x = stack1(x, 64, 3, stride1=1, name="conv2") - x = stack1(x, 128, 4, name="conv3") - x = stack1(x, 256, 23, name="conv4") - return stack1(x, 512, 3, name="conv5") - - return ResNet( - stack_fn, - False, - True, - "resnet101", - include_top, - weights, - input_tensor, - input_shape, - pooling, - classes, - **kwargs, - ) - - -@keras_export("keras.applications.resnet.ResNet152", "keras.applications.ResNet152") -def ResNet152( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - **kwargs, -): - """Instantiates the ResNet152 architecture.""" - - def stack_fn(x): - x = stack1(x, 64, 3, stride1=1, name="conv2") - x = stack1(x, 128, 8, name="conv3") - x = stack1(x, 256, 36, name="conv4") - return stack1(x, 512, 3, name="conv5") - - return ResNet( - stack_fn, - False, - True, - "resnet152", - include_top, - weights, - input_tensor, - input_shape, - pooling, - classes, - **kwargs, - ) - - -@keras_export( - "keras.applications.resnet50.preprocess_input", - "keras.applications.resnet.preprocess_input", -) -def preprocess_input(x, data_format=None): - return imagenet_utils.preprocess_input(x, data_format=data_format, mode="caffe") - - -@keras_export( - "keras.applications.resnet50.decode_predictions", - "keras.applications.resnet.decode_predictions", -) -def decode_predictions(preds, top=5): - return imagenet_utils.decode_predictions(preds, top=top) - - -preprocess_input.__doc__ = imagenet_utils.PREPROCESS_INPUT_DOC.format( - mode="", - ret=imagenet_utils.PREPROCESS_INPUT_RET_DOC_CAFFE, - error=imagenet_utils.PREPROCESS_INPUT_ERROR_DOC, -) -decode_predictions.__doc__ = imagenet_utils.decode_predictions.__doc__ - -DOC = """ - - Reference: - - [Deep Residual Learning for Image Recognition]( - https://arxiv.org/abs/1512.03385) (CVPR 2015) - - For image classification use cases, see - [this page for detailed examples]( - https://keras.io/api/applications/#usage-examples-for-image-classification-models). - - For transfer learning use cases, make sure to read the - [guide to transfer learning & fine-tuning]( - https://keras.io/guides/transfer_learning/). - - Note: each Keras Application expects a specific kind of input preprocessing. - For ResNet, call `tf.keras.applications.resnet.preprocess_input` on your - inputs before passing them to the model. - `resnet.preprocess_input` will convert the input images from RGB to BGR, - then will zero-center each color channel with respect to the ImageNet dataset, - without scaling. - - Args: - include_top: whether to include the fully-connected - layer at the top of the network. - weights: one of `None` (random initialization), - 'imagenet' (pre-training on ImageNet), - or the path to the weights file to be loaded. - input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) - to use as image input for the model. - input_shape: optional shape tuple, only to be specified - if `include_top` is False (otherwise the input shape - has to be `(224, 224, 3)` (with `'channels_last'` data format) - or `(3, 224, 224)` (with `'channels_first'` data format). - It should have exactly 3 inputs channels, - and width and height should be no smaller than 32. - E.g. `(200, 200, 3)` would be one valid value. - pooling: Optional pooling mode for feature extraction - when `include_top` is `False`. - - `None` means that the output of the model will be - the 4D tensor output of the - last convolutional block. - - `avg` means that global average pooling - will be applied to the output of the - last convolutional block, and thus - the output of the model will be a 2D tensor. - - `max` means that global max pooling will - be applied. - classes: optional number of classes to classify images - into, only to be specified if `include_top` is True, and - if no `weights` argument is specified. - classifier_activation: A `str` or callable. The activation function to use - on the "top" layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" layer. - When loading pretrained weights, `classifier_activation` can only - be `None` or `"softmax"`. - - Returns: - A Keras model instance. -""" - -setattr(ResNet50, "__doc__", ResNet50.__doc__ + DOC) -setattr(ResNet101, "__doc__", ResNet101.__doc__ + DOC) -setattr(ResNet152, "__doc__", ResNet152.__doc__ + DOC) diff --git a/Archive/keras_applications_mod/models/resnet_rs.py b/Archive/keras_applications_mod/models/resnet_rs.py deleted file mode 100644 index b340d9a..0000000 --- a/Archive/keras_applications_mod/models/resnet_rs.py +++ /dev/null @@ -1,947 +0,0 @@ -# Copyright 2022 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - - -"""ResNet-RS models for Keras. - -Reference: -- [Revisiting ResNets: Improved Training and Scaling Strategies]( - https://arxiv.org/pdf/2103.07579.pdf) -""" - -import sys -from typing import Callable -from typing import Dict -from typing import List -from typing import Union - -import tensorflow.compat.v2 as tf - -from keras import backend -from keras import layers -from keras.applications import imagenet_utils -from keras.engine import training -from keras.utils import data_utils -from keras.utils import layer_utils - -# isort: off -from tensorflow.python.util.tf_export import keras_export - -BASE_WEIGHTS_URL = "https://storage.googleapis.com/tensorflow/keras-applications/resnet_rs/" - -WEIGHT_HASHES = { - "resnet-rs-101-i160.h5": "544b3434d00efc199d66e9058c7f3379", - "resnet-rs-101-i160_notop.h5": "82d5b90c5ce9d710da639d6216d0f979", - "resnet-rs-101-i192.h5": "eb285be29ab42cf4835ff20a5e3b5d23", - "resnet-rs-101-i192_notop.h5": "f9a0f6b85faa9c3db2b6e233c4eebb5b", - "resnet-rs-152-i192.h5": "8d72a301ed8a6f11a47c4ced4396e338", - "resnet-rs-152-i192_notop.h5": "5fbf7ac2155cb4d5a6180ee9e3aa8704", - "resnet-rs-152-i224.h5": "31a46a92ab21b84193d0d71dd8c3d03b", - "resnet-rs-152-i224_notop.h5": "dc8b2cba2005552eafa3167f00dc2133", - "resnet-rs-152-i256.h5": "ba6271b99bdeb4e7a9b15c05964ef4ad", - "resnet-rs-152-i256_notop.h5": "fa79794252dbe47c89130f65349d654a", - "resnet-rs-200-i256.h5": "a76930b741884e09ce90fa7450747d5f", - "resnet-rs-200-i256_notop.h5": "bbdb3994718dfc0d1cd45d7eff3f3d9c", - "resnet-rs-270-i256.h5": "20d575825ba26176b03cb51012a367a8", - "resnet-rs-270-i256_notop.h5": "2c42ecb22e35f3e23d2f70babce0a2aa", - "resnet-rs-350-i256.h5": "f4a039dc3c421321b7fc240494574a68", - "resnet-rs-350-i256_notop.h5": "6e44b55025bbdff8f51692a023143d66", - "resnet-rs-350-i320.h5": "7ccb858cc738305e8ceb3c0140bee393", - "resnet-rs-350-i320_notop.h5": "ab0c1f9079d2f85a9facbd2c88aa6079", - "resnet-rs-420-i320.h5": "ae0eb9bed39e64fc8d7e0db4018dc7e8", - "resnet-rs-420-i320_notop.h5": "fe6217c32be8305b1889657172b98884", - "resnet-rs-50-i160.h5": "69d9d925319f00a8bdd4af23c04e4102", - "resnet-rs-50-i160_notop.h5": "90daa68cd26c95aa6c5d25451e095529", -} - -DEPTH_TO_WEIGHT_VARIANTS = { - 50: [160], - 101: [160, 192], - 152: [192, 224, 256], - 200: [256], - 270: [256], - 350: [256, 320], - 420: [320], -} -BLOCK_ARGS = { - 50: [ - {"input_filters": 64, "num_repeats": 3}, - {"input_filters": 128, "num_repeats": 4}, - {"input_filters": 256, "num_repeats": 6}, - {"input_filters": 512, "num_repeats": 3}, - ], - 101: [ - {"input_filters": 64, "num_repeats": 3}, - {"input_filters": 128, "num_repeats": 4}, - {"input_filters": 256, "num_repeats": 23}, - {"input_filters": 512, "num_repeats": 3}, - ], - 152: [ - {"input_filters": 64, "num_repeats": 3}, - {"input_filters": 128, "num_repeats": 8}, - {"input_filters": 256, "num_repeats": 36}, - {"input_filters": 512, "num_repeats": 3}, - ], - 200: [ - {"input_filters": 64, "num_repeats": 3}, - {"input_filters": 128, "num_repeats": 24}, - {"input_filters": 256, "num_repeats": 36}, - {"input_filters": 512, "num_repeats": 3}, - ], - 270: [ - {"input_filters": 64, "num_repeats": 4}, - {"input_filters": 128, "num_repeats": 29}, - {"input_filters": 256, "num_repeats": 53}, - {"input_filters": 512, "num_repeats": 4}, - ], - 350: [ - {"input_filters": 64, "num_repeats": 4}, - {"input_filters": 128, "num_repeats": 36}, - {"input_filters": 256, "num_repeats": 72}, - {"input_filters": 512, "num_repeats": 4}, - ], - 420: [ - {"input_filters": 64, "num_repeats": 4}, - {"input_filters": 128, "num_repeats": 44}, - {"input_filters": 256, "num_repeats": 87}, - {"input_filters": 512, "num_repeats": 4}, - ], -} -CONV_KERNEL_INITIALIZER = { - "class_name": "VarianceScaling", - "config": { - "scale": 2.0, - "mode": "fan_out", - "distribution": "truncated_normal", - }, -} - -BASE_DOCSTRING = """Instantiates the {name} architecture. - - Reference: - [Revisiting ResNets: Improved Training and Scaling Strategies]( - https://arxiv.org/pdf/2103.07579.pdf) - - For image classification use cases, see - [this page for detailed examples]( - https://keras.io/api/applications/#usage-examples-for-image-classification-models). - - For transfer learning use cases, make sure to read the - [guide to transfer learning & fine-tuning]( - https://keras.io/guides/transfer_learning/). - - Note: each Keras Application expects a specific kind of input preprocessing. - For ResNetRs, by default input preprocessing is included as a part of the - model (as a `Rescaling` layer), and thus - `tf.keras.applications.resnet_rs.preprocess_input` is actually a - pass-through function. In this use case, ResNetRS models expect their inputs - to be float tensors of pixels with values in the [0-255] range. - At the same time, preprocessing as a part of the model (i.e. `Rescaling` - layer) can be disabled by setting `include_preprocessing` argument to False. - With preprocessing disabled ResNetRS models expect their inputs to be float - tensors of pixels with values in the [-1, 1] range. - - Args: - depth: Depth of ResNet network. - input_shape: optional shape tuple. It should have exactly 3 inputs - channels, and width and height should be no smaller than 32. - E.g. (200, 200, 3) would be one valid value. - bn_momentum: Momentum parameter for Batch Normalization layers. - bn_epsilon: Epsilon parameter for Batch Normalization layers. - activation: activation function. - se_ratio: Squeeze and Excitation layer ratio. - dropout_rate: dropout rate before final classifier layer. - drop_connect_rate: dropout rate at skip connections. - include_top: whether to include the fully-connected layer at the top of - the network. - block_args: list of dicts, parameters to construct block modules. - model_name: name of the model. - pooling: optional pooling mode for feature extraction when `include_top` - is `False`. - - `None` means that the output of the model will be - the 4D tensor output of the - last convolutional layer. - - `avg` means that global average pooling - will be applied to the output of the - last convolutional layer, and thus - the output of the model will be a 2D tensor. - - `max` means that global max pooling will - be applied. - weights: one of `None` (random initialization), `'imagenet'` - (pre-training on ImageNet), or the path to the weights file to be - loaded. Note: one model can have multiple imagenet variants - depending on input shape it was trained with. For input_shape - 224x224 pass `imagenet-i224` as argument. By default, highest input - shape weights are downloaded. - input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to - use as image input for the model. - classes: optional number of classes to classify images into, only to be - specified if `include_top` is True, and if no `weights` argument is - specified. - classifier_activation: A `str` or callable. The activation function to - use on the "top" layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" - layer. - include_preprocessing: Boolean, whether to include the preprocessing - layer (`Rescaling`) at the bottom of the network. Note: Input image - is normalized by ImageNet mean and standard deviation. - Defaults to `True`. - - Returns: - A `keras.Model` instance. -""" - - -def Conv2DFixedPadding(filters, kernel_size, strides, name=None): - """Conv2D block with fixed padding.""" - if name is None: - counter = backend.get_uid("conv_") - name = f"conv_{counter}" - - def apply(inputs): - if strides > 1: - inputs = fixed_padding(inputs, kernel_size) - return layers.Conv2D( - filters=filters, - kernel_size=kernel_size, - strides=strides, - padding="same" if strides == 1 else "valid", - use_bias=False, - kernel_initializer=CONV_KERNEL_INITIALIZER, - name=name, - )(inputs) - - return apply - - -def STEM( - bn_momentum: float = 0.0, - bn_epsilon: float = 1e-5, - activation: str = "relu", - name=None, -): - """ResNet-D type STEM block.""" - if name is None: - counter = backend.get_uid("stem_") - name = f"stem_{counter}" - - def apply(inputs): - bn_axis = 3 if backend.image_data_format() == "channels_last" else 1 - - # First stem block - x = Conv2DFixedPadding(filters=32, kernel_size=3, strides=2, name=name + "_stem_conv_1")(inputs) - x = layers.BatchNormalization( - axis=bn_axis, - momentum=bn_momentum, - epsilon=bn_epsilon, - name=name + "_stem_batch_norm_1", - )(x) - x = layers.Activation(activation, name=name + "_stem_act_1")(x) - - # Second stem block - x = Conv2DFixedPadding(filters=32, kernel_size=3, strides=1, name=name + "_stem_conv_2")(x) - x = layers.BatchNormalization( - axis=bn_axis, - momentum=bn_momentum, - epsilon=bn_epsilon, - name=name + "_stem_batch_norm_2", - )(x) - x = layers.Activation(activation, name=name + "_stem_act_2")(x) - - # Final Stem block: - x = Conv2DFixedPadding(filters=64, kernel_size=3, strides=1, name=name + "_stem_conv_3")(x) - x = layers.BatchNormalization( - axis=bn_axis, - momentum=bn_momentum, - epsilon=bn_epsilon, - name=name + "_stem_batch_norm_3", - )(x) - x = layers.Activation(activation, name=name + "_stem_act_3")(x) - - # Replace stem max pool: - x = Conv2DFixedPadding(filters=64, kernel_size=3, strides=2, name=name + "_stem_conv_4")(x) - x = layers.BatchNormalization( - axis=bn_axis, - momentum=bn_momentum, - epsilon=bn_epsilon, - name=name + "_stem_batch_norm_4", - )(x) - x = layers.Activation(activation, name=name + "_stem_act_4")(x) - return x - - return apply - - -def SE(in_filters: int, se_ratio: float = 0.25, expand_ratio: int = 1, name=None): - """Squeeze and Excitation block.""" - bn_axis = 3 if backend.image_data_format() == "channels_last" else 1 - if name is None: - counter = backend.get_uid("se_") - name = f"se_{counter}" - - def apply(inputs): - x = layers.GlobalAveragePooling2D(name=name + "_se_squeeze")(inputs) - if bn_axis == 1: - se_shape = (x.shape[-1], 1, 1) - else: - se_shape = (1, 1, x.shape[-1]) - x = layers.Reshape(se_shape, name=name + "_se_reshape")(x) - - num_reduced_filters = max(1, int(in_filters * 4 * se_ratio)) - - x = layers.Conv2D( - filters=num_reduced_filters, - kernel_size=[1, 1], - strides=[1, 1], - kernel_initializer=CONV_KERNEL_INITIALIZER, - padding="same", - use_bias=True, - activation="relu", - name=name + "_se_reduce", - )(x) - - x = layers.Conv2D( - filters=4 * in_filters * expand_ratio, # Expand ratio is 1 by default - kernel_size=[1, 1], - strides=[1, 1], - kernel_initializer=CONV_KERNEL_INITIALIZER, - padding="same", - use_bias=True, - activation="sigmoid", - name=name + "_se_expand", - )(x) - - return layers.multiply([inputs, x], name=name + "_se_excite") - - return apply - - -def BottleneckBlock( - filters: int, - strides: int, - use_projection: bool, - bn_momentum: float = 0.0, - bn_epsilon: float = 1e-5, - activation: str = "relu", - se_ratio: float = 0.25, - survival_probability: float = 0.8, - name=None, -): - """Bottleneck block variant for residual networks with BN.""" - if name is None: - counter = backend.get_uid("block_0_") - name = f"block_0_{counter}" - - def apply(inputs): - bn_axis = 3 if backend.image_data_format() == "channels_last" else 1 - - shortcut = inputs - - if use_projection: - filters_out = filters * 4 - if strides == 2: - shortcut = layers.AveragePooling2D( - pool_size=(2, 2), - strides=(2, 2), - padding="same", - name=name + "_projection_pooling", - )(inputs) - shortcut = Conv2DFixedPadding( - filters=filters_out, - kernel_size=1, - strides=1, - name=name + "_projection_conv", - )(shortcut) - else: - shortcut = Conv2DFixedPadding( - filters=filters_out, - kernel_size=1, - strides=strides, - name=name + "_projection_conv", - )(inputs) - - shortcut = layers.BatchNormalization( - axis=bn_axis, - momentum=bn_momentum, - epsilon=bn_epsilon, - name=name + "_projection_batch_norm", - )(shortcut) - - # First conv layer: - x = Conv2DFixedPadding(filters=filters, kernel_size=1, strides=1, name=name + "_conv_1")(inputs) - x = layers.BatchNormalization( - axis=bn_axis, - momentum=bn_momentum, - epsilon=bn_epsilon, - name=name + "batch_norm_1", - )(x) - x = layers.Activation(activation, name=name + "_act_1")(x) - - # Second conv layer: - x = Conv2DFixedPadding( - filters=filters, - kernel_size=3, - strides=strides, - name=name + "_conv_2", - )(x) - x = layers.BatchNormalization( - axis=bn_axis, - momentum=bn_momentum, - epsilon=bn_epsilon, - name=name + "_batch_norm_2", - )(x) - x = layers.Activation(activation, name=name + "_act_2")(x) - - # Third conv layer: - x = Conv2DFixedPadding(filters=filters * 4, kernel_size=1, strides=1, name=name + "_conv_3")(x) - x = layers.BatchNormalization( - axis=bn_axis, - momentum=bn_momentum, - epsilon=bn_epsilon, - name=name + "_batch_norm_3", - )(x) - - if 0 < se_ratio < 1: - x = SE(filters, se_ratio=se_ratio, name=name + "_se")(x) - - # Drop connect - if survival_probability: - x = layers.Dropout( - survival_probability, - noise_shape=(None, 1, 1, 1), - name=name + "_drop", - )(x) - - x = layers.Add()([x, shortcut]) - - return layers.Activation(activation, name=name + "_output_act")(x) - - return apply - - -def BlockGroup( - filters, - strides, - num_repeats, - se_ratio: float = 0.25, - bn_epsilon: float = 1e-5, - bn_momentum: float = 0.0, - activation: str = "relu", - survival_probability: float = 0.8, - name=None, -): - """Create one group of blocks for the ResNet model.""" - if name is None: - counter = backend.get_uid("block_group_") - name = f"block_group_{counter}" - - def apply(inputs): - # Only the first block per block_group uses projection shortcut and - # strides. - x = BottleneckBlock( - filters=filters, - strides=strides, - use_projection=True, - se_ratio=se_ratio, - bn_epsilon=bn_epsilon, - bn_momentum=bn_momentum, - activation=activation, - survival_probability=survival_probability, - name=name + "_block_0_", - )(inputs) - - for i in range(1, num_repeats): - x = BottleneckBlock( - filters=filters, - strides=1, - use_projection=False, - se_ratio=se_ratio, - activation=activation, - bn_epsilon=bn_epsilon, - bn_momentum=bn_momentum, - survival_probability=survival_probability, - name=name + f"_block_{i}_", - )(x) - return x - - return apply - - -def get_survival_probability(init_rate, block_num, total_blocks): - """Get survival probability based on block number and initial rate.""" - return init_rate * float(block_num) / total_blocks - - -def allow_bigger_recursion(target_limit: int): - """Increase default recursion limit to create larger models.""" - current_limit = sys.getrecursionlimit() - if current_limit < target_limit: - sys.setrecursionlimit(target_limit) - - -def fixed_padding(inputs, kernel_size): - """Pad the input along the spatial dimensions independently of input - size.""" - pad_total = kernel_size - 1 - pad_beg = pad_total // 2 - pad_end = pad_total - pad_beg - - # Use ZeroPadding as to avoid TFOpLambda layer - padded_inputs = layers.ZeroPadding2D(padding=((pad_beg, pad_end), (pad_beg, pad_end)))(inputs) - - return padded_inputs - - -def ResNetRS( - depth: int, - input_shape=None, - bn_momentum=0.0, - bn_epsilon=1e-5, - activation: str = "relu", - se_ratio=0.25, - dropout_rate=0.25, - drop_connect_rate=0.2, - include_top=True, - block_args: List[Dict[str, int]] = None, - model_name="resnet-rs", - pooling=None, - weights="imagenet", - input_tensor=None, - classes=1000, - classifier_activation: Union[str, Callable] = "softmax", - include_preprocessing=True, -): - """Build Resnet-RS model, given provided parameters. - - Args: - depth: Depth of ResNet network. - input_shape: optional shape tuple. It should have exactly 3 inputs - channels, and width and height should be no smaller than 32. E.g. - (200, 200, 3) would be one valid value. - bn_momentum: Momentum parameter for Batch Normalization layers. - bn_epsilon: Epsilon parameter for Batch Normalization layers. - activation: activation function. - se_ratio: Squeeze and Excitation layer ratio. - dropout_rate: dropout rate before final classifier layer. - drop_connect_rate: dropout rate at skip connections. - include_top: whether to include the fully-connected layer at the top of - the network. - block_args: list of dicts, parameters to construct block modules. - model_name: name of the model. - pooling: optional pooling mode for feature extraction when `include_top` - is `False`. - - `None` means that the output of the model will be the 4D tensor - output of the last convolutional layer. - - `avg` means that global average pooling will be applied to the - output of the last convolutional layer, and thus the output of the - model will be a 2D tensor. - - `max` means that global max pooling will be applied. - weights: one of `None` (random initialization), `'imagenet'` - (pre-training on ImageNet), or the path to the weights file to be - loaded. Note- one model can have multiple imagenet variants depending - on input shape it was trained with. For input_shape 224x224 pass - `imagenet-i224` as argument. By default, highest input shape weights - are downloaded. - input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to - use as image input for the model. - classes: optional number of classes to classify images into, only to be - specified if `include_top` is True, and if no `weights` argument is - specified. - classifier_activation: A `str` or callable. The activation function to - use on the "top" layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" layer. - include_preprocessing: Boolean, whether to include the preprocessing - layer (`Rescaling`) at the bottom of the network. Note - Input image - is normalized by ImageNet mean and standard deviation. - Defaults to `True`. - - - Returns: - A `tf.keras.Model` instance. - - Raises: - ValueError: in case of invalid argument for `weights`, or invalid input - shape. - ValueError: if `classifier_activation` is not `softmax` or `None` when - using a pretrained top layer. - """ - # Validate parameters - available_weight_variants = DEPTH_TO_WEIGHT_VARIANTS[depth] - if weights == "imagenet": - max_input_shape = max(available_weight_variants) - # `imagenet` argument without explicit weights input size. - # Picking weights trained with biggest available shape - weights = f"{weights}-i{max_input_shape}" - - weights_allow_list = [f"imagenet-i{x}" for x in available_weight_variants] - if not (weights in {*weights_allow_list, None} or tf.io.gfile.exists(weights)): - raise ValueError( - "The `weights` argument should be either " - "`None` (random initialization), `'imagenet'` " - "(pre-training on ImageNet, with highest available input shape)," - " or the path to the weights file to be loaded. " - f"For ResNetRS{depth} the following weight variants are " - f"available {weights_allow_list} (default=highest)." - f" Received weights={weights}" - ) - - if weights in weights_allow_list and include_top and classes != 1000: - raise ValueError( - "If using `weights` as `'imagenet'` or any " - f"of {weights_allow_list} " - "with `include_top` as true, `classes` should be 1000. " - f"Received classes={classes}" - ) - - input_shape = imagenet_utils.obtain_input_shape( - input_shape, - default_size=224, - min_size=32, - data_format=backend.image_data_format(), - require_flatten=include_top, - weights=weights, - ) - # Define input tensor - if input_tensor is None: - img_input = layers.Input(shape=input_shape) - else: - if not backend.is_keras_tensor(input_tensor): - img_input = layers.Input(tensor=input_tensor, shape=input_shape) - else: - img_input = input_tensor - - bn_axis = 3 if backend.image_data_format() == "channels_last" else 1 - - x = img_input - - if include_preprocessing: - num_channels = input_shape[bn_axis - 1] - x = layers.Rescaling(scale=1.0 / 255)(x) - if num_channels == 3: - x = layers.Normalization( - mean=[0.485, 0.456, 0.406], - variance=[0.229**2, 0.224**2, 0.225**2], - axis=bn_axis, - )(x) - - # Build stem - x = STEM(bn_momentum=bn_momentum, bn_epsilon=bn_epsilon, activation=activation)(x) - - # Build blocks - if block_args is None: - block_args = BLOCK_ARGS[depth] - - for i, args in enumerate(block_args): - survival_probability = get_survival_probability( - init_rate=drop_connect_rate, - block_num=i + 2, - total_blocks=len(block_args) + 1, - ) - - x = BlockGroup( - filters=args["input_filters"], - activation=activation, - strides=(1 if i == 0 else 2), - num_repeats=args["num_repeats"], - se_ratio=se_ratio, - bn_momentum=bn_momentum, - bn_epsilon=bn_epsilon, - survival_probability=survival_probability, - name=f"BlockGroup{i + 2}_", - )(x) - - # Build head: - if include_top: - x = layers.GlobalAveragePooling2D(name="avg_pool")(x) - if dropout_rate > 0: - x = layers.Dropout(dropout_rate, name="top_dropout")(x) - - imagenet_utils.validate_activation(classifier_activation, weights) - x = layers.Dense(classes, activation=classifier_activation, name="predictions")(x) - else: - if pooling == "avg": - x = layers.GlobalAveragePooling2D(name="avg_pool")(x) - elif pooling == "max": - x = layers.GlobalMaxPooling2D(name="max_pool")(x) - - # Ensure that the model takes into account - # any potential predecessors of `input_tensor`. - if input_tensor is not None: - inputs = layer_utils.get_source_inputs(input_tensor) - else: - inputs = img_input - - # Create model. - model = training.Model(inputs, x, name=model_name) - - # Download weights - if weights in weights_allow_list: - weights_input_shape = weights.split("-")[-1] # e. g. "i160" - weights_name = f"{model_name}-{weights_input_shape}" - if not include_top: - weights_name += "_notop" - - filename = f"{weights_name}.h5" - download_url = BASE_WEIGHTS_URL + filename - weights_path = data_utils.get_file( - fname=filename, - origin=download_url, - cache_subdir="models", - file_hash=WEIGHT_HASHES[filename], - ) - model.load_weights(weights_path) - - elif weights is not None: - model.load_weights(weights) - - return model - - -@keras_export("keras.applications.resnet_rs.ResNetRS50", "keras.applications.ResNetRS50") -def ResNetRS50( - include_top=True, - weights="imagenet", - classes=1000, - input_shape=None, - input_tensor=None, - pooling=None, - classifier_activation="softmax", - include_preprocessing=True, -): - """Build ResNet-RS50 model.""" - return ResNetRS( - depth=50, - include_top=include_top, - drop_connect_rate=0.0, - dropout_rate=0.25, - weights=weights, - classes=classes, - input_shape=input_shape, - input_tensor=input_tensor, - pooling=pooling, - classifier_activation=classifier_activation, - model_name="resnet-rs-50", - include_preprocessing=include_preprocessing, - ) - - -@keras_export("keras.applications.resnet_rs.ResNetRS101", "keras.applications.ResNetRS101") -def ResNetRS101( - include_top=True, - weights="imagenet", - classes=1000, - input_shape=None, - input_tensor=None, - pooling=None, - classifier_activation="softmax", - include_preprocessing=True, -): - """Build ResNet-RS101 model.""" - return ResNetRS( - depth=101, - include_top=include_top, - drop_connect_rate=0.0, - dropout_rate=0.25, - weights=weights, - classes=classes, - input_shape=input_shape, - input_tensor=input_tensor, - pooling=pooling, - classifier_activation=classifier_activation, - model_name="resnet-rs-101", - include_preprocessing=include_preprocessing, - ) - - -@keras_export("keras.applications.resnet_rs.ResNetRS152", "keras.applications.ResNetRS152") -def ResNetRS152( - include_top=True, - weights="imagenet", - classes=1000, - input_shape=None, - input_tensor=None, - pooling=None, - classifier_activation="softmax", - include_preprocessing=True, -): - """Build ResNet-RS152 model.""" - return ResNetRS( - depth=152, - include_top=include_top, - drop_connect_rate=0.0, - dropout_rate=0.25, - weights=weights, - classes=classes, - input_shape=input_shape, - input_tensor=input_tensor, - pooling=pooling, - classifier_activation=classifier_activation, - model_name="resnet-rs-152", - include_preprocessing=include_preprocessing, - ) - - -@keras_export("keras.applications.resnet_rs.ResNetRS200", "keras.applications.ResNetRS200") -def ResNetRS200( - include_top=True, - weights="imagenet", - classes=1000, - input_shape=None, - input_tensor=None, - pooling=None, - classifier_activation="softmax", - include_preprocessing=True, -): - """Build ResNet-RS200 model.""" - return ResNetRS( - depth=200, - include_top=include_top, - drop_connect_rate=0.1, - dropout_rate=0.25, - weights=weights, - classes=classes, - input_shape=input_shape, - input_tensor=input_tensor, - pooling=pooling, - classifier_activation=classifier_activation, - model_name="resnet-rs-200", - include_preprocessing=include_preprocessing, - ) - - -@keras_export("keras.applications.resnet_rs.ResNetRS270", "keras.applications.ResNetRS270") -def ResNetRS270( - include_top=True, - weights="imagenet", - classes=1000, - input_shape=None, - input_tensor=None, - pooling=None, - classifier_activation="softmax", - include_preprocessing=True, -): - """Build ResNet-RS-270 model.""" - allow_bigger_recursion(1300) - return ResNetRS( - depth=270, - include_top=include_top, - drop_connect_rate=0.1, - dropout_rate=0.25, - weights=weights, - classes=classes, - input_shape=input_shape, - input_tensor=input_tensor, - pooling=pooling, - classifier_activation=classifier_activation, - model_name="resnet-rs-270", - include_preprocessing=include_preprocessing, - ) - - -@keras_export("keras.applications.resnet_rs.ResNetRS350", "keras.applications.ResNetRS350") -def ResNetRS350( - include_top=True, - weights="imagenet", - classes=1000, - input_shape=None, - input_tensor=None, - pooling=None, - classifier_activation="softmax", - include_preprocessing=True, -): - """Build ResNet-RS350 model.""" - allow_bigger_recursion(1500) - return ResNetRS( - depth=350, - include_top=include_top, - drop_connect_rate=0.1, - dropout_rate=0.4, - weights=weights, - classes=classes, - input_shape=input_shape, - input_tensor=input_tensor, - pooling=pooling, - classifier_activation=classifier_activation, - model_name="resnet-rs-350", - include_preprocessing=include_preprocessing, - ) - - -@keras_export("keras.applications.resnet_rs.ResNetRS420", "keras.applications.ResNetRS420") -def ResNetRS420( - include_top=True, - weights="imagenet", - classes=1000, - input_shape=None, - input_tensor=None, - pooling=None, - classifier_activation="softmax", - include_preprocessing=True, -): - """Build ResNet-RS420 model.""" - allow_bigger_recursion(1800) - return ResNetRS( - depth=420, - include_top=include_top, - dropout_rate=0.4, - drop_connect_rate=0.1, - weights=weights, - classes=classes, - input_shape=input_shape, - input_tensor=input_tensor, - pooling=pooling, - classifier_activation=classifier_activation, - model_name="resnet-rs-420", - include_preprocessing=include_preprocessing, - ) - - -@keras_export("keras.applications.resnet_rs.preprocess_input") -def preprocess_input(x, data_format=None): - """A placeholder method for backward compatibility. - - The preprocessing logic has been included in the ResnetRS model - implementation. Users are no longer required to call this method to - normalize - the input data. This method does nothing and only kept as a placeholder to - align the API surface between old and new version of model. - - Args: - x: A floating point `numpy.array` or a `tf.Tensor`. - data_format: Optional data format of the image tensor/array. `None` means - the global setting `tf.keras.backend.image_data_format()` is used - (unless you changed it, it uses "channels_last"). - Defaults to `None`. - - Returns: - Unchanged `numpy.array` or `tf.Tensor`. - """ - return x - - -@keras_export("keras.applications.resnet_rs.decode_predictions") -def decode_predictions(preds, top=5): - return imagenet_utils.decode_predictions(preds, top=top) - - -decode_predictions.__doc__ = imagenet_utils.decode_predictions.__doc__ - -ResNetRS50.__doc__ = BASE_DOCSTRING.format(name="ResNetRS50") -ResNetRS101.__doc__ = BASE_DOCSTRING.format(name="ResNetRS101") -ResNetRS152.__doc__ = BASE_DOCSTRING.format(name="ResNetRS152") -ResNetRS200.__doc__ = BASE_DOCSTRING.format(name="ResNetRS200") -ResNetRS270.__doc__ = BASE_DOCSTRING.format(name="ResNetRS270") -ResNetRS350.__doc__ = BASE_DOCSTRING.format(name="ResNetRS350") -ResNetRS420.__doc__ = BASE_DOCSTRING.format(name="ResNetRS420") diff --git a/Archive/keras_applications_mod/models/resnet_v2.py b/Archive/keras_applications_mod/models/resnet_v2.py deleted file mode 100644 index bb37bd3..0000000 --- a/Archive/keras_applications_mod/models/resnet_v2.py +++ /dev/null @@ -1,206 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""ResNet v2 models for Keras. - -Reference: - - [Identity Mappings in Deep Residual Networks]( - https://arxiv.org/abs/1603.05027) (CVPR 2016) -""" - -from keras.applications import imagenet_utils -from keras.applications import resnet - -# isort: off -from tensorflow.python.util.tf_export import keras_export - - -@keras_export("keras.applications.resnet_v2.ResNet50V2", "keras.applications.ResNet50V2") -def ResNet50V2( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - """Instantiates the ResNet50V2 architecture.""" - - def stack_fn(x): - x = resnet.stack2(x, 64, 3, name="conv2") - x = resnet.stack2(x, 128, 4, name="conv3") - x = resnet.stack2(x, 256, 6, name="conv4") - return resnet.stack2(x, 512, 3, stride1=1, name="conv5") - - return resnet.ResNet( - stack_fn, - True, - True, - "resnet50v2", - include_top, - weights, - input_tensor, - input_shape, - pooling, - classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.resnet_v2.ResNet101V2", "keras.applications.ResNet101V2") -def ResNet101V2( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - """Instantiates the ResNet101V2 architecture.""" - - def stack_fn(x): - x = resnet.stack2(x, 64, 3, name="conv2") - x = resnet.stack2(x, 128, 4, name="conv3") - x = resnet.stack2(x, 256, 23, name="conv4") - return resnet.stack2(x, 512, 3, stride1=1, name="conv5") - - return resnet.ResNet( - stack_fn, - True, - True, - "resnet101v2", - include_top, - weights, - input_tensor, - input_shape, - pooling, - classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.resnet_v2.ResNet152V2", "keras.applications.ResNet152V2") -def ResNet152V2( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - """Instantiates the ResNet152V2 architecture.""" - - def stack_fn(x): - x = resnet.stack2(x, 64, 3, name="conv2") - x = resnet.stack2(x, 128, 8, name="conv3") - x = resnet.stack2(x, 256, 36, name="conv4") - return resnet.stack2(x, 512, 3, stride1=1, name="conv5") - - return resnet.ResNet( - stack_fn, - True, - True, - "resnet152v2", - include_top, - weights, - input_tensor, - input_shape, - pooling, - classes, - classifier_activation=classifier_activation, - ) - - -@keras_export("keras.applications.resnet_v2.preprocess_input") -def preprocess_input(x, data_format=None): - return imagenet_utils.preprocess_input(x, data_format=data_format, mode="tf") - - -@keras_export("keras.applications.resnet_v2.decode_predictions") -def decode_predictions(preds, top=5): - return imagenet_utils.decode_predictions(preds, top=top) - - -preprocess_input.__doc__ = imagenet_utils.PREPROCESS_INPUT_DOC.format( - mode="", - ret=imagenet_utils.PREPROCESS_INPUT_RET_DOC_TF, - error=imagenet_utils.PREPROCESS_INPUT_ERROR_DOC, -) -decode_predictions.__doc__ = imagenet_utils.decode_predictions.__doc__ - -DOC = """ - - Reference: - - [Identity Mappings in Deep Residual Networks]( - https://arxiv.org/abs/1603.05027) (CVPR 2016) - - For image classification use cases, see - [this page for detailed examples]( - https://keras.io/api/applications/#usage-examples-for-image-classification-models). - - For transfer learning use cases, make sure to read the - [guide to transfer learning & fine-tuning]( - https://keras.io/guides/transfer_learning/). - - Note: each Keras Application expects a specific kind of input preprocessing. - For ResNetV2, call `tf.keras.applications.resnet_v2.preprocess_input` on your - inputs before passing them to the model. - `resnet_v2.preprocess_input` will scale input pixels between -1 and 1. - - Args: - include_top: whether to include the fully-connected - layer at the top of the network. - weights: one of `None` (random initialization), - 'imagenet' (pre-training on ImageNet), - or the path to the weights file to be loaded. - input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) - to use as image input for the model. - input_shape: optional shape tuple, only to be specified - if `include_top` is False (otherwise the input shape - has to be `(224, 224, 3)` (with `'channels_last'` data format) - or `(3, 224, 224)` (with `'channels_first'` data format). - It should have exactly 3 inputs channels, - and width and height should be no smaller than 32. - E.g. `(200, 200, 3)` would be one valid value. - pooling: Optional pooling mode for feature extraction - when `include_top` is `False`. - - `None` means that the output of the model will be - the 4D tensor output of the - last convolutional block. - - `avg` means that global average pooling - will be applied to the output of the - last convolutional block, and thus - the output of the model will be a 2D tensor. - - `max` means that global max pooling will - be applied. - classes: optional number of classes to classify images - into, only to be specified if `include_top` is True, and - if no `weights` argument is specified. - classifier_activation: A `str` or callable. The activation function to use - on the "top" layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" layer. - When loading pretrained weights, `classifier_activation` can only - be `None` or `"softmax"`. - - Returns: - A `keras.Model` instance. -""" - -setattr(ResNet50V2, "__doc__", ResNet50V2.__doc__ + DOC) -setattr(ResNet101V2, "__doc__", ResNet101V2.__doc__ + DOC) -setattr(ResNet152V2, "__doc__", ResNet152V2.__doc__ + DOC) diff --git a/Archive/keras_applications_mod/models/vgg16.py b/Archive/keras_applications_mod/models/vgg16.py deleted file mode 100644 index d345a66..0000000 --- a/Archive/keras_applications_mod/models/vgg16.py +++ /dev/null @@ -1,235 +0,0 @@ -# Copyright 2015 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""VGG16 model for Keras. - -Reference: - - [Very Deep Convolutional Networks for Large-Scale Image Recognition] - (https://arxiv.org/abs/1409.1556) (ICLR 2015) -""" - -import tensorflow.compat.v2 as tf - -from keras import backend -from keras.applications import imagenet_utils -from keras.engine import training -from keras.layers import VersionAwareLayers -from keras.utils import data_utils -from keras.utils import layer_utils - -# isort: off -from tensorflow.python.util.tf_export import keras_export - -WEIGHTS_PATH = "https://storage.googleapis.com/tensorflow/keras-applications/" "vgg16/vgg16_weights_tf_dim_ordering_tf_kernels.h5" -WEIGHTS_PATH_NO_TOP = ( - "https://storage.googleapis.com/tensorflow/" "keras-applications/vgg16/" "vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5" -) - -layers = VersionAwareLayers() - - -@keras_export("keras.applications.vgg16.VGG16", "keras.applications.VGG16") -def VGG16( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - """Instantiates the VGG16 model. - - Reference: - - [Very Deep Convolutional Networks for Large-Scale Image Recognition]( - https://arxiv.org/abs/1409.1556) (ICLR 2015) - - For image classification use cases, see - [this page for detailed examples]( - https://keras.io/api/applications/#usage-examples-for-image-classification-models). - - For transfer learning use cases, make sure to read the - [guide to transfer learning & fine-tuning]( - https://keras.io/guides/transfer_learning/). - - The default input size for this model is 224x224. - - Note: each Keras Application expects a specific kind of input preprocessing. - For VGG16, call `tf.keras.applications.vgg16.preprocess_input` on your - inputs before passing them to the model. - `vgg16.preprocess_input` will convert the input images from RGB to BGR, - then will zero-center each color channel with respect to the ImageNet - dataset, without scaling. - - Args: - include_top: whether to include the 3 fully-connected - layers at the top of the network. - weights: one of `None` (random initialization), - 'imagenet' (pre-training on ImageNet), - or the path to the weights file to be loaded. - input_tensor: optional Keras tensor - (i.e. output of `layers.Input()`) - to use as image input for the model. - input_shape: optional shape tuple, only to be specified - if `include_top` is False (otherwise the input shape - has to be `(224, 224, 3)` - (with `channels_last` data format) - or `(3, 224, 224)` (with `channels_first` data format). - It should have exactly 3 input channels, - and width and height should be no smaller than 32. - E.g. `(200, 200, 3)` would be one valid value. - pooling: Optional pooling mode for feature extraction - when `include_top` is `False`. - - `None` means that the output of the model will be - the 4D tensor output of the - last convolutional block. - - `avg` means that global average pooling - will be applied to the output of the - last convolutional block, and thus - the output of the model will be a 2D tensor. - - `max` means that global max pooling will - be applied. - classes: optional number of classes to classify images - into, only to be specified if `include_top` is True, and - if no `weights` argument is specified. - classifier_activation: A `str` or callable. The activation function to - use on the "top" layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" - layer. When loading pretrained weights, `classifier_activation` can - only be `None` or `"softmax"`. - - Returns: - A `keras.Model` instance. - """ - if not (weights in {"imagenet", None} or tf.io.gfile.exists(weights)): - raise ValueError( - "The `weights` argument should be either " - "`None` (random initialization), `imagenet` " - "(pre-training on ImageNet), " - "or the path to the weights file to be loaded. Received: " - f"weights={weights}" - ) - - if weights == "imagenet" and include_top and classes != 1000: - raise ValueError( - 'If using `weights` as `"imagenet"` with `include_top` ' "as true, `classes` should be 1000. " f"Received `classes={classes}`" - ) - # Determine proper input shape - input_shape = imagenet_utils.obtain_input_shape( - input_shape, - default_size=224, - min_size=32, - data_format=backend.image_data_format(), - require_flatten=include_top, - weights=weights, - ) - - if input_tensor is None: - img_input = layers.Input(shape=input_shape) - else: - if not backend.is_keras_tensor(input_tensor): - img_input = layers.Input(tensor=input_tensor, shape=input_shape) - else: - img_input = input_tensor - # Block 1 - x = layers.Conv2D(64, (3, 3), activation="relu", padding="same", name="block1_conv1")(img_input) - x = layers.Conv2D(64, (3, 3), activation="relu", padding="same", name="block1_conv2")(x) - x = layers.MaxPooling2D((2, 2), strides=(2, 2), name="block1_pool")(x) - - # Block 2 - x = layers.Conv2D(128, (3, 3), activation="relu", padding="same", name="block2_conv1")(x) - x = layers.Conv2D(128, (3, 3), activation="relu", padding="same", name="block2_conv2")(x) - x = layers.MaxPooling2D((2, 2), strides=(2, 2), name="block2_pool")(x) - - # Block 3 - x = layers.Conv2D(256, (3, 3), activation="relu", padding="same", name="block3_conv1")(x) - x = layers.Conv2D(256, (3, 3), activation="relu", padding="same", name="block3_conv2")(x) - x = layers.Conv2D(256, (3, 3), activation="relu", padding="same", name="block3_conv3")(x) - x = layers.MaxPooling2D((2, 2), strides=(2, 2), name="block3_pool")(x) - - # Block 4 - x = layers.Conv2D(512, (3, 3), activation="relu", padding="same", name="block4_conv1")(x) - x = layers.Conv2D(512, (3, 3), activation="relu", padding="same", name="block4_conv2")(x) - x = layers.Conv2D(512, (3, 3), activation="relu", padding="same", name="block4_conv3")(x) - x = layers.MaxPooling2D((2, 2), strides=(2, 2), name="block4_pool")(x) - - # Block 5 - x = layers.Conv2D(512, (3, 3), activation="relu", padding="same", name="block5_conv1")(x) - x = layers.Conv2D(512, (3, 3), activation="relu", padding="same", name="block5_conv2")(x) - x = layers.Conv2D(512, (3, 3), activation="relu", padding="same", name="block5_conv3")(x) - x = layers.MaxPooling2D((2, 2), strides=(2, 2), name="block5_pool")(x) - - if include_top: - # Classification block - x = layers.Flatten(name="flatten")(x) - x = layers.Dense(4096, activation="relu", name="fc1")(x) - x = layers.Dense(4096, activation="relu", name="fc2")(x) - - imagenet_utils.validate_activation(classifier_activation, weights) - x = layers.Dense(classes, activation=classifier_activation, name="predictions")(x) - else: - if pooling == "avg": - x = layers.GlobalAveragePooling2D()(x) - elif pooling == "max": - x = layers.GlobalMaxPooling2D()(x) - - # Ensure that the model takes into account - # any potential predecessors of `input_tensor`. - if input_tensor is not None: - inputs = layer_utils.get_source_inputs(input_tensor) - else: - inputs = img_input - # Create model. - model = training.Model(inputs, x, name="vgg16") - - # Load weights. - if weights == "imagenet": - if include_top: - weights_path = data_utils.get_file( - "vgg16_weights_tf_dim_ordering_tf_kernels.h5", - WEIGHTS_PATH, - cache_subdir="models", - file_hash="64373286793e3c8b2b4e3219cbf3544b", - ) - else: - weights_path = data_utils.get_file( - "vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5", - WEIGHTS_PATH_NO_TOP, - cache_subdir="models", - file_hash="6d6bbae143d832006294945121d1f1fc", - ) - model.load_weights(weights_path) - elif weights is not None: - model.load_weights(weights) - - return model - - -@keras_export("keras.applications.vgg16.preprocess_input") -def preprocess_input(x, data_format=None): - return imagenet_utils.preprocess_input(x, data_format=data_format, mode="caffe") - - -@keras_export("keras.applications.vgg16.decode_predictions") -def decode_predictions(preds, top=5): - return imagenet_utils.decode_predictions(preds, top=top) - - -preprocess_input.__doc__ = imagenet_utils.PREPROCESS_INPUT_DOC.format( - mode="", - ret=imagenet_utils.PREPROCESS_INPUT_RET_DOC_CAFFE, - error=imagenet_utils.PREPROCESS_INPUT_ERROR_DOC, -) -decode_predictions.__doc__ = imagenet_utils.decode_predictions.__doc__ diff --git a/Archive/keras_applications_mod/models/vgg19.py b/Archive/keras_applications_mod/models/vgg19.py deleted file mode 100644 index 500ea38..0000000 --- a/Archive/keras_applications_mod/models/vgg19.py +++ /dev/null @@ -1,239 +0,0 @@ -# Copyright 2015 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""VGG19 model for Keras. - -Reference: - - [Very Deep Convolutional Networks for Large-Scale Image Recognition]( - https://arxiv.org/abs/1409.1556) (ICLR 2015) -""" - -import tensorflow.compat.v2 as tf - -from keras import backend -from keras.applications import imagenet_utils -from keras.engine import training -from keras.layers import VersionAwareLayers -from keras.utils import data_utils -from keras.utils import layer_utils - -# isort: off -from tensorflow.python.util.tf_export import keras_export - -WEIGHTS_PATH = "https://storage.googleapis.com/tensorflow/keras-applications/" "vgg19/vgg19_weights_tf_dim_ordering_tf_kernels.h5" -WEIGHTS_PATH_NO_TOP = ( - "https://storage.googleapis.com/tensorflow/" "keras-applications/vgg19/" "vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5" -) - -layers = VersionAwareLayers() - - -@keras_export("keras.applications.vgg19.VGG19", "keras.applications.VGG19") -def VGG19( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - """Instantiates the VGG19 architecture. - - Reference: - - [Very Deep Convolutional Networks for Large-Scale Image Recognition]( - https://arxiv.org/abs/1409.1556) (ICLR 2015) - - For image classification use cases, see - [this page for detailed examples]( - https://keras.io/api/applications/#usage-examples-for-image-classification-models). - - For transfer learning use cases, make sure to read the - [guide to transfer learning & fine-tuning]( - https://keras.io/guides/transfer_learning/). - - The default input size for this model is 224x224. - - Note: each Keras Application expects a specific kind of input preprocessing. - For VGG19, call `tf.keras.applications.vgg19.preprocess_input` on your - inputs before passing them to the model. - `vgg19.preprocess_input` will convert the input images from RGB to BGR, - then will zero-center each color channel with respect to the ImageNet - dataset, without scaling. - - Args: - include_top: whether to include the 3 fully-connected - layers at the top of the network. - weights: one of `None` (random initialization), - 'imagenet' (pre-training on ImageNet), - or the path to the weights file to be loaded. - input_tensor: optional Keras tensor - (i.e. output of `layers.Input()`) - to use as image input for the model. - input_shape: optional shape tuple, only to be specified - if `include_top` is False (otherwise the input shape - has to be `(224, 224, 3)` - (with `channels_last` data format) - or `(3, 224, 224)` (with `channels_first` data format). - It should have exactly 3 inputs channels, - and width and height should be no smaller than 32. - E.g. `(200, 200, 3)` would be one valid value. - pooling: Optional pooling mode for feature extraction - when `include_top` is `False`. - - `None` means that the output of the model will be - the 4D tensor output of the - last convolutional block. - - `avg` means that global average pooling - will be applied to the output of the - last convolutional block, and thus - the output of the model will be a 2D tensor. - - `max` means that global max pooling will - be applied. - classes: optional number of classes to classify images - into, only to be specified if `include_top` is True, and - if no `weights` argument is specified. - classifier_activation: A `str` or callable. The activation function to use - on the "top" layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" layer. - When loading pretrained weights, `classifier_activation` can only - be `None` or `"softmax"`. - - Returns: - A `keras.Model` instance. - """ - if not (weights in {"imagenet", None} or tf.io.gfile.exists(weights)): - raise ValueError( - "The `weights` argument should be either " - "`None` (random initialization), `imagenet` " - "(pre-training on ImageNet), " - "or the path to the weights file to be loaded. " - f"Received: `weights={weights}.`" - ) - - if weights == "imagenet" and include_top and classes != 1000: - raise ValueError( - 'If using `weights` as `"imagenet"` with `include_top` ' - "as true, `classes` should be 1000. " - f"Received: `classes={classes}.`" - ) - # Determine proper input shape - input_shape = imagenet_utils.obtain_input_shape( - input_shape, - default_size=224, - min_size=32, - data_format=backend.image_data_format(), - require_flatten=include_top, - weights=weights, - ) - - if input_tensor is None: - img_input = layers.Input(shape=input_shape) - else: - if not backend.is_keras_tensor(input_tensor): - img_input = layers.Input(tensor=input_tensor, shape=input_shape) - else: - img_input = input_tensor - # Block 1 - x = layers.Conv2D(64, (3, 3), activation="relu", padding="same", name="block1_conv1")(img_input) - x = layers.Conv2D(64, (3, 3), activation="relu", padding="same", name="block1_conv2")(x) - x = layers.MaxPooling2D((2, 2), strides=(2, 2), name="block1_pool")(x) - - # Block 2 - x = layers.Conv2D(128, (3, 3), activation="relu", padding="same", name="block2_conv1")(x) - x = layers.Conv2D(128, (3, 3), activation="relu", padding="same", name="block2_conv2")(x) - x = layers.MaxPooling2D((2, 2), strides=(2, 2), name="block2_pool")(x) - - # Block 3 - x = layers.Conv2D(256, (3, 3), activation="relu", padding="same", name="block3_conv1")(x) - x = layers.Conv2D(256, (3, 3), activation="relu", padding="same", name="block3_conv2")(x) - x = layers.Conv2D(256, (3, 3), activation="relu", padding="same", name="block3_conv3")(x) - x = layers.Conv2D(256, (3, 3), activation="relu", padding="same", name="block3_conv4")(x) - x = layers.MaxPooling2D((2, 2), strides=(2, 2), name="block3_pool")(x) - - # Block 4 - x = layers.Conv2D(512, (3, 3), activation="relu", padding="same", name="block4_conv1")(x) - x = layers.Conv2D(512, (3, 3), activation="relu", padding="same", name="block4_conv2")(x) - x = layers.Conv2D(512, (3, 3), activation="relu", padding="same", name="block4_conv3")(x) - x = layers.Conv2D(512, (3, 3), activation="relu", padding="same", name="block4_conv4")(x) - x = layers.MaxPooling2D((2, 2), strides=(2, 2), name="block4_pool")(x) - - # Block 5 - x = layers.Conv2D(512, (3, 3), activation="relu", padding="same", name="block5_conv1")(x) - x = layers.Conv2D(512, (3, 3), activation="relu", padding="same", name="block5_conv2")(x) - x = layers.Conv2D(512, (3, 3), activation="relu", padding="same", name="block5_conv3")(x) - x = layers.Conv2D(512, (3, 3), activation="relu", padding="same", name="block5_conv4")(x) - x = layers.MaxPooling2D((2, 2), strides=(2, 2), name="block5_pool")(x) - - if include_top: - # Classification block - x = layers.Flatten(name="flatten")(x) - x = layers.Dense(4096, activation="relu", name="fc1")(x) - x = layers.Dense(4096, activation="relu", name="fc2")(x) - imagenet_utils.validate_activation(classifier_activation, weights) - x = layers.Dense(classes, activation=classifier_activation, name="predictions")(x) - else: - if pooling == "avg": - x = layers.GlobalAveragePooling2D()(x) - elif pooling == "max": - x = layers.GlobalMaxPooling2D()(x) - - # Ensure that the model takes into account - # any potential predecessors of `input_tensor`. - if input_tensor is not None: - inputs = layer_utils.get_source_inputs(input_tensor) - else: - inputs = img_input - # Create model. - model = training.Model(inputs, x, name="vgg19") - - # Load weights. - if weights == "imagenet": - if include_top: - weights_path = data_utils.get_file( - "vgg19_weights_tf_dim_ordering_tf_kernels.h5", - WEIGHTS_PATH, - cache_subdir="models", - file_hash="cbe5617147190e668d6c5d5026f83318", - ) - else: - weights_path = data_utils.get_file( - "vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5", - WEIGHTS_PATH_NO_TOP, - cache_subdir="models", - file_hash="253f8cb515780f3b799900260a226db6", - ) - model.load_weights(weights_path) - elif weights is not None: - model.load_weights(weights) - - return model - - -@keras_export("keras.applications.vgg19.preprocess_input") -def preprocess_input(x, data_format=None): - return imagenet_utils.preprocess_input(x, data_format=data_format, mode="caffe") - - -@keras_export("keras.applications.vgg19.decode_predictions") -def decode_predictions(preds, top=5): - return imagenet_utils.decode_predictions(preds, top=top) - - -preprocess_input.__doc__ = imagenet_utils.PREPROCESS_INPUT_DOC.format( - mode="", - ret=imagenet_utils.PREPROCESS_INPUT_RET_DOC_CAFFE, - error=imagenet_utils.PREPROCESS_INPUT_ERROR_DOC, -) -decode_predictions.__doc__ = imagenet_utils.decode_predictions.__doc__ diff --git a/Archive/keras_applications_mod/models/xception.py b/Archive/keras_applications_mod/models/xception.py deleted file mode 100644 index b95540b..0000000 --- a/Archive/keras_applications_mod/models/xception.py +++ /dev/null @@ -1,303 +0,0 @@ -# Copyright 2016 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Xception V1 model for Keras. - -On ImageNet, this model gets to a top-1 validation accuracy of 0.790 -and a top-5 validation accuracy of 0.945. - -Reference: - - [Xception: Deep Learning with Depthwise Separable Convolutions]( - https://arxiv.org/abs/1610.02357) (CVPR 2017) -""" - -import tensorflow.compat.v2 as tf - -from keras import backend -from keras.applications import imagenet_utils -from keras.engine import training -from keras.layers import VersionAwareLayers -from keras.utils import data_utils -from keras.utils import layer_utils - -# isort: off -from tensorflow.python.util.tf_export import keras_export - -TF_WEIGHTS_PATH = "https://storage.googleapis.com/tensorflow/keras-applications/" "xception/xception_weights_tf_dim_ordering_tf_kernels.h5" -TF_WEIGHTS_PATH_NO_TOP = ( - "https://storage.googleapis.com/tensorflow/keras-applications/" "xception/xception_weights_tf_dim_ordering_tf_kernels_notop.h5" -) - -layers = VersionAwareLayers() - - -@keras_export("keras.applications.xception.Xception", "keras.applications.Xception") -def Xception( - include_top=True, - weights="imagenet", - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation="softmax", -): - """Instantiates the Xception architecture. - - Reference: - - [Xception: Deep Learning with Depthwise Separable Convolutions]( - https://arxiv.org/abs/1610.02357) (CVPR 2017) - - For image classification use cases, see - [this page for detailed examples]( - https://keras.io/api/applications/#usage-examples-for-image-classification-models). - - For transfer learning use cases, make sure to read the - [guide to transfer learning & fine-tuning]( - https://keras.io/guides/transfer_learning/). - - The default input image size for this model is 299x299. - - Note: each Keras Application expects a specific kind of input preprocessing. - For Xception, call `tf.keras.applications.xception.preprocess_input` on your - inputs before passing them to the model. - `xception.preprocess_input` will scale input pixels between -1 and 1. - - Args: - include_top: whether to include the fully-connected - layer at the top of the network. - weights: one of `None` (random initialization), - 'imagenet' (pre-training on ImageNet), - or the path to the weights file to be loaded. - input_tensor: optional Keras tensor - (i.e. output of `layers.Input()`) - to use as image input for the model. - input_shape: optional shape tuple, only to be specified - if `include_top` is False (otherwise the input shape - has to be `(299, 299, 3)`. - It should have exactly 3 inputs channels, - and width and height should be no smaller than 71. - E.g. `(150, 150, 3)` would be one valid value. - pooling: Optional pooling mode for feature extraction - when `include_top` is `False`. - - `None` means that the output of the model will be - the 4D tensor output of the - last convolutional block. - - `avg` means that global average pooling - will be applied to the output of the - last convolutional block, and thus - the output of the model will be a 2D tensor. - - `max` means that global max pooling will - be applied. - classes: optional number of classes to classify images - into, only to be specified if `include_top` is True, - and if no `weights` argument is specified. - classifier_activation: A `str` or callable. The activation function to use - on the "top" layer. Ignored unless `include_top=True`. Set - `classifier_activation=None` to return the logits of the "top" layer. - When loading pretrained weights, `classifier_activation` can only - be `None` or `"softmax"`. - - Returns: - A `keras.Model` instance. - """ - if not (weights in {"imagenet", None} or tf.io.gfile.exists(weights)): - raise ValueError( - "The `weights` argument should be either " - "`None` (random initialization), `imagenet` " - "(pre-training on ImageNet), " - "or the path to the weights file to be loaded." - ) - - if weights == "imagenet" and include_top and classes != 1000: - raise ValueError('If using `weights` as `"imagenet"` with `include_top`' " as true, `classes` should be 1000") - - # Determine proper input shape - input_shape = imagenet_utils.obtain_input_shape( - input_shape, - default_size=299, - min_size=71, - data_format=backend.image_data_format(), - require_flatten=include_top, - weights=weights, - ) - - if input_tensor is None: - img_input = layers.Input(shape=input_shape) - else: - if not backend.is_keras_tensor(input_tensor): - img_input = layers.Input(tensor=input_tensor, shape=input_shape) - else: - img_input = input_tensor - - channel_axis = 1 if backend.image_data_format() == "channels_first" else -1 - - x = layers.Conv2D(32, (3, 3), strides=(2, 2), use_bias=False, name="block1_conv1")(img_input) - x = layers.BatchNormalization(axis=channel_axis, name="block1_conv1_bn")(x) - x = layers.Activation("relu", name="block1_conv1_act")(x) - x = layers.Conv2D(64, (3, 3), use_bias=False, name="block1_conv2")(x) - x = layers.BatchNormalization(axis=channel_axis, name="block1_conv2_bn")(x) - x = layers.Activation("relu", name="block1_conv2_act")(x) - - residual = layers.Conv2D(128, (1, 1), strides=(2, 2), padding="same", use_bias=False)(x) - residual = layers.BatchNormalization(axis=channel_axis)(residual) - - x = layers.SeparableConv2D(128, (3, 3), padding="same", use_bias=False, name="block2_sepconv1")(x) - x = layers.BatchNormalization(axis=channel_axis, name="block2_sepconv1_bn")(x) - x = layers.Activation("relu", name="block2_sepconv2_act")(x) - x = layers.SeparableConv2D(128, (3, 3), padding="same", use_bias=False, name="block2_sepconv2")(x) - x = layers.BatchNormalization(axis=channel_axis, name="block2_sepconv2_bn")(x) - - x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding="same", name="block2_pool")(x) - x = layers.add([x, residual]) - - residual = layers.Conv2D(256, (1, 1), strides=(2, 2), padding="same", use_bias=False)(x) - residual = layers.BatchNormalization(axis=channel_axis)(residual) - - x = layers.Activation("relu", name="block3_sepconv1_act")(x) - x = layers.SeparableConv2D(256, (3, 3), padding="same", use_bias=False, name="block3_sepconv1")(x) - x = layers.BatchNormalization(axis=channel_axis, name="block3_sepconv1_bn")(x) - x = layers.Activation("relu", name="block3_sepconv2_act")(x) - x = layers.SeparableConv2D(256, (3, 3), padding="same", use_bias=False, name="block3_sepconv2")(x) - x = layers.BatchNormalization(axis=channel_axis, name="block3_sepconv2_bn")(x) - - x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding="same", name="block3_pool")(x) - x = layers.add([x, residual]) - - residual = layers.Conv2D(728, (1, 1), strides=(2, 2), padding="same", use_bias=False)(x) - residual = layers.BatchNormalization(axis=channel_axis)(residual) - - x = layers.Activation("relu", name="block4_sepconv1_act")(x) - x = layers.SeparableConv2D(728, (3, 3), padding="same", use_bias=False, name="block4_sepconv1")(x) - x = layers.BatchNormalization(axis=channel_axis, name="block4_sepconv1_bn")(x) - x = layers.Activation("relu", name="block4_sepconv2_act")(x) - x = layers.SeparableConv2D(728, (3, 3), padding="same", use_bias=False, name="block4_sepconv2")(x) - x = layers.BatchNormalization(axis=channel_axis, name="block4_sepconv2_bn")(x) - - x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding="same", name="block4_pool")(x) - x = layers.add([x, residual]) - - for i in range(8): - residual = x - prefix = "block" + str(i + 5) - - x = layers.Activation("relu", name=prefix + "_sepconv1_act")(x) - x = layers.SeparableConv2D( - 728, - (3, 3), - padding="same", - use_bias=False, - name=prefix + "_sepconv1", - )(x) - x = layers.BatchNormalization(axis=channel_axis, name=prefix + "_sepconv1_bn")(x) - x = layers.Activation("relu", name=prefix + "_sepconv2_act")(x) - x = layers.SeparableConv2D( - 728, - (3, 3), - padding="same", - use_bias=False, - name=prefix + "_sepconv2", - )(x) - x = layers.BatchNormalization(axis=channel_axis, name=prefix + "_sepconv2_bn")(x) - x = layers.Activation("relu", name=prefix + "_sepconv3_act")(x) - x = layers.SeparableConv2D( - 728, - (3, 3), - padding="same", - use_bias=False, - name=prefix + "_sepconv3", - )(x) - x = layers.BatchNormalization(axis=channel_axis, name=prefix + "_sepconv3_bn")(x) - - x = layers.add([x, residual]) - - residual = layers.Conv2D(1024, (1, 1), strides=(2, 2), padding="same", use_bias=False)(x) - residual = layers.BatchNormalization(axis=channel_axis)(residual) - - x = layers.Activation("relu", name="block13_sepconv1_act")(x) - x = layers.SeparableConv2D(728, (3, 3), padding="same", use_bias=False, name="block13_sepconv1")(x) - x = layers.BatchNormalization(axis=channel_axis, name="block13_sepconv1_bn")(x) - x = layers.Activation("relu", name="block13_sepconv2_act")(x) - x = layers.SeparableConv2D(1024, (3, 3), padding="same", use_bias=False, name="block13_sepconv2")(x) - x = layers.BatchNormalization(axis=channel_axis, name="block13_sepconv2_bn")(x) - - x = layers.MaxPooling2D((3, 3), strides=(2, 2), padding="same", name="block13_pool")(x) - x = layers.add([x, residual]) - - x = layers.SeparableConv2D(1536, (3, 3), padding="same", use_bias=False, name="block14_sepconv1")(x) - x = layers.BatchNormalization(axis=channel_axis, name="block14_sepconv1_bn")(x) - x = layers.Activation("relu", name="block14_sepconv1_act")(x) - - x = layers.SeparableConv2D(2048, (3, 3), padding="same", use_bias=False, name="block14_sepconv2")(x) - x = layers.BatchNormalization(axis=channel_axis, name="block14_sepconv2_bn")(x) - x = layers.Activation("relu", name="block14_sepconv2_act")(x) - - if include_top: - x = layers.GlobalAveragePooling2D(name="avg_pool")(x) - imagenet_utils.validate_activation(classifier_activation, weights) - x = layers.Dense(classes, activation=classifier_activation, name="predictions")(x) - else: - if pooling == "avg": - x = layers.GlobalAveragePooling2D()(x) - elif pooling == "max": - x = layers.GlobalMaxPooling2D()(x) - - # Ensure that the model takes into account - # any potential predecessors of `input_tensor`. - if input_tensor is not None: - inputs = layer_utils.get_source_inputs(input_tensor) - else: - inputs = img_input - # Create model. - model = training.Model(inputs, x, name="xception") - - # Load weights. - if weights == "imagenet": - if include_top: - weights_path = data_utils.get_file( - "xception_weights_tf_dim_ordering_tf_kernels.h5", - TF_WEIGHTS_PATH, - cache_subdir="models", - file_hash="0a58e3b7378bc2990ea3b43d5981f1f6", - ) - else: - weights_path = data_utils.get_file( - "xception_weights_tf_dim_ordering_tf_kernels_notop.h5", - TF_WEIGHTS_PATH_NO_TOP, - cache_subdir="models", - file_hash="b0042744bf5b25fce3cb969f33bebb97", - ) - model.load_weights(weights_path) - elif weights is not None: - model.load_weights(weights) - - return model - - -@keras_export("keras.applications.xception.preprocess_input") -def preprocess_input(x, data_format=None): - return imagenet_utils.preprocess_input(x, data_format=data_format, mode="tf") - - -@keras_export("keras.applications.xception.decode_predictions") -def decode_predictions(preds, top=5): - return imagenet_utils.decode_predictions(preds, top=top) - - -preprocess_input.__doc__ = imagenet_utils.PREPROCESS_INPUT_DOC.format( - mode="", - ret=imagenet_utils.PREPROCESS_INPUT_RET_DOC_TF, - error=imagenet_utils.PREPROCESS_INPUT_ERROR_DOC, -) -decode_predictions.__doc__ = imagenet_utils.decode_predictions.__doc__ diff --git a/BETA_E_Model_T&T.ipynb b/BETA_E_Model_T&T.ipynb index caf7eea..ef81a87 100644 --- a/BETA_E_Model_T&T.ipynb +++ b/BETA_E_Model_T&T.ipynb @@ -114,7 +114,7 @@ "from Utils.Grad_cam import make_gradcam_heatmap\n", "from Utils.print_color_V2_NEW import print_Color_V2\n", "from Utils.print_color_V1_OLD import print_Color\n", - "from Utils.Other import *\n", + "from Utils.Other import * # noqa: F403\n", "\n", "# Other\n", "tf.get_logger().setLevel(\"ERROR\")\n", @@ -10356,14 +10356,14 @@ "\n", "\n", "# Funcs\n", - "def normalize_TO_RANGE(arr, min_val, max_val):\n", + "def normalize_TO_RANGE(arr, min_val, max_val): # noqa: F811\n", " arr = arr.astype(\"float32\")\n", " arr = (arr - arr.min()) / (arr.max() - arr.min())\n", " arr = arr * (max_val - min_val) + min_val\n", " return arr\n", "\n", "\n", - "def Z_SCORE_normalize(arr):\n", + "def Z_SCORE_normalize(arr): # noqa: F811\n", " arr = arr.astype(\"float32\")\n", " mean = np.mean(arr)\n", " std_dev = np.std(arr)\n", @@ -10613,8 +10613,8 @@ " advanced_mode=True,\n", ")\n", "# warnings\n", - "P_warning(\"[RES_Train -> True].\") if RES_Train else None\n", - "P_warning(\"[TerminateOnHighTemp_M -> False] GPU temperature protection is OFF\") if not TerminateOnHighTemp_M else None\n", + "P_warning(\"[RES_Train -> True].\") if RES_Train else None # noqa: F405\n", + "P_warning(\"[TerminateOnHighTemp_M -> False] GPU temperature protection is OFF\") if not TerminateOnHighTemp_M else None # noqa: F405\n", "print_Color(\"Setup Verbose END.\", [\"yellow\"])\n", "# MAIN LOOP\n", "try:\n", @@ -10636,7 +10636,7 @@ " advanced_mode=True,\n", " )\n", " # warnings\n", - " P_warning(\"[TerminateOnHighTemp_M -> False] GPU temperature protection is OFF\") if not TerminateOnHighTemp_M else None\n", + " P_warning(\"[TerminateOnHighTemp_M -> False] GPU temperature protection is OFF\") if not TerminateOnHighTemp_M else None # noqa: F405\n", " # DP\n", " if not AdvSubsetC:\n", " print_Color(\"Shuffling data...\", [\"yellow\"])\n", @@ -10860,7 +10860,7 @@ " # Garbage Collection (memory)\n", " gc.collect()\n", " tf.keras.backend.clear_session()\n", - " GPU_memUsage()\n", + " GPU_memUsage() # noqa: F405\n", " # Update TF summary text\n", " for Key in TF_Summary_text_Dict:\n", " TF_Summary_text_cache = f\"# @[{Key}].Data:\\n\"\n", @@ -11200,10 +11200,10 @@ "from numba import cuda\n", "\n", "device = cuda.get_current_device()\n", - "GPU_memUsage()\n", + "GPU_memUsage() # noqa: F405\n", "print(\"Realising all memory...\")\n", "device.reset()\n", - "GPU_memUsage()\n", + "GPU_memUsage() # noqa: F405\n", "print(\"done.\")" ] }, @@ -11221,7 +11221,7 @@ "outputs": [], "source": [ "# Save history\n", - "save_list(history, \"history\\\\model_history.pkl.gz\", compress=True)" + "save_list(history, \"history\\\\model_history.pkl.gz\", compress=True) # noqa: F405" ] }, { @@ -11231,7 +11231,7 @@ "outputs": [], "source": [ "# load history\n", - "history = load_list(\"history\\\\model_history.pkl.gz\", compressed=True)" + "history = load_list(\"history\\\\model_history.pkl.gz\", compressed=True) # noqa: F405" ] }, { @@ -11705,9 +11705,9 @@ "\n", "# Print acc\n", "print(\"Val data acc:\")\n", - "evaluate_model_full(y_val, val_predictions)\n", + "evaluate_model_full(y_val, val_predictions) # noqa: F405\n", "print(\"Test data acc:\")\n", - "evaluate_model_full(y_test, test_predictions)\n", + "evaluate_model_full(y_test, test_predictions) # noqa: F405\n", "\n", "# format data\n", "val_predictions = np.argmax(val_predictions, axis=1)\n", @@ -11824,7 +11824,7 @@ " plt.title(\"Number of Incorrect Predictions vs. Number of Data Points\")\n", " plt.show()\n", "except Exception:\n", - " P_warning(\"Failed to plot incorrect predictions vs. data points\")" + " P_warning(\"Failed to plot incorrect predictions vs. data points\") # noqa: F405" ] } ], diff --git a/Interface/CLI/Data/CLI_main.py b/Interface/CLI/Data/CLI_main.py index 215174c..5e8ff25 100644 --- a/Interface/CLI/Data/CLI_main.py +++ b/Interface/CLI/Data/CLI_main.py @@ -29,7 +29,7 @@ # Utils from Utils.Grad_cam import make_gradcam_heatmap from Utils.print_color_V1_OLD import print_Color -from Utils.Other import * +from Utils.Other import * # noqa: F403 # global vars>>> # CONST SYS @@ -463,7 +463,7 @@ def CI_tmwd(argv_Split: list = ["none"]): else: print("Training the model...\n") # training - history = model.fit(images, labels, epochs=train_epochs, batch_size=1, verbose="auto") # history not used + model.fit(images, labels, epochs=train_epochs, batch_size=1, verbose="auto") print("Training done.\n") else: print_Color( diff --git a/Interface/CLI/Data/Utils/lr_find.py b/Interface/CLI/Data/Utils/lr_find.py index 5049644..9d660cc 100644 --- a/Interface/CLI/Data/Utils/lr_find.py +++ b/Interface/CLI/Data/Utils/lr_find.py @@ -108,7 +108,7 @@ def range_test( # save original model weights try: self.model.save_weights(self.weightsFile) - except: + except Exception: print("Unable to save initial weights, weights of model will change. Re-instantiate model to load previous weights ...") # start scheduler sched = Scheduler((start_lr, end_lr), num_iter) @@ -161,7 +161,7 @@ def _print_prompt(self) -> None: "Cleanup model weights disturbed during LRFinder exploration." try: self.model.load_weights(self.weightsFile) - except: + except Exception: print("Unable to load inital weights. Re-instantiate model to load previous weights ...") K.set_value(self.optimizer.lr, self.init_lr) print("LR Finder is complete, type {LrFinder}.plot_lrs() to see the graph.") @@ -194,7 +194,7 @@ def plot_lrs( if suggestion: try: mg = (np.gradient(np.array(losses))).argmin() - except: + except Exception: print("Failed to compute the gradients, there might not be enough points.") return print(f"Min numerical gradient: {lrs[mg]:.2E}") diff --git a/Interface/CLI/Data/Utils/one_cycle.py b/Interface/CLI/Data/Utils/one_cycle.py index 993baa9..0972449 100644 --- a/Interface/CLI/Data/Utils/one_cycle.py +++ b/Interface/CLI/Data/Utils/one_cycle.py @@ -156,7 +156,7 @@ def set_lr_mom(self) -> None: computed_momentum = self.anneal_func(self.m_momentum, self.b_momentum, self.step_num / self.step_size_up) try: K.set_value(self.model.optimizer.momentum, computed_momentum) - except: + except Exception: K.set_value(self.model.optimizer.beta_1, computed_momentum) else: down_step_num = self.step_num - self.step_size_up @@ -172,7 +172,7 @@ def set_lr_mom(self) -> None: ) try: K.set_value(self.model.optimizer.momentum, computed_momentum) - except: + except Exception: K.set_value(self.model.optimizer.beta_1, computed_momentum) def on_train_begin(self, logs=None) -> None: @@ -181,7 +181,7 @@ def on_train_begin(self, logs=None) -> None: if self.cycle_momentum: try: K.set_value(self.model.optimizer.momentum, self.momentum) - except: + except Exception: K.set_value(self.model.optimizer.beta_1, self.momentum) def on_train_batch_end(self, batch, logs=None) -> None: @@ -189,7 +189,7 @@ def on_train_batch_end(self, batch, logs=None) -> None: lr = float(K.get_value(self.model.optimizer.lr)) try: mom = float(K.get_value(self.model.optimizer.momentum)) - except: + except Exception: mom = float(K.get_value(self.model.optimizer.beta_1)) # Append to the list self.track_lr.append(lr) @@ -200,12 +200,12 @@ def on_train_batch_end(self, batch, logs=None) -> None: self.step_num += 1 def plot_lrs_moms(self, axes=None) -> None: - if axes == None: + if axes is None: _, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 5)) else: try: ax1, ax2 = axes - except: + except Exception: ax1, ax2 = axes[0], axes[1] ax1.plot(self.track_lr) ax1.set_title("Learning Rate vs Steps") diff --git a/Interface/GUI/Data/Utils/lr_find.py b/Interface/GUI/Data/Utils/lr_find.py index 5049644..9d660cc 100644 --- a/Interface/GUI/Data/Utils/lr_find.py +++ b/Interface/GUI/Data/Utils/lr_find.py @@ -108,7 +108,7 @@ def range_test( # save original model weights try: self.model.save_weights(self.weightsFile) - except: + except Exception: print("Unable to save initial weights, weights of model will change. Re-instantiate model to load previous weights ...") # start scheduler sched = Scheduler((start_lr, end_lr), num_iter) @@ -161,7 +161,7 @@ def _print_prompt(self) -> None: "Cleanup model weights disturbed during LRFinder exploration." try: self.model.load_weights(self.weightsFile) - except: + except Exception: print("Unable to load inital weights. Re-instantiate model to load previous weights ...") K.set_value(self.optimizer.lr, self.init_lr) print("LR Finder is complete, type {LrFinder}.plot_lrs() to see the graph.") @@ -194,7 +194,7 @@ def plot_lrs( if suggestion: try: mg = (np.gradient(np.array(losses))).argmin() - except: + except Exception: print("Failed to compute the gradients, there might not be enough points.") return print(f"Min numerical gradient: {lrs[mg]:.2E}") diff --git a/Interface/GUI/Data/Utils/one_cycle.py b/Interface/GUI/Data/Utils/one_cycle.py index 993baa9..0972449 100644 --- a/Interface/GUI/Data/Utils/one_cycle.py +++ b/Interface/GUI/Data/Utils/one_cycle.py @@ -156,7 +156,7 @@ def set_lr_mom(self) -> None: computed_momentum = self.anneal_func(self.m_momentum, self.b_momentum, self.step_num / self.step_size_up) try: K.set_value(self.model.optimizer.momentum, computed_momentum) - except: + except Exception: K.set_value(self.model.optimizer.beta_1, computed_momentum) else: down_step_num = self.step_num - self.step_size_up @@ -172,7 +172,7 @@ def set_lr_mom(self) -> None: ) try: K.set_value(self.model.optimizer.momentum, computed_momentum) - except: + except Exception: K.set_value(self.model.optimizer.beta_1, computed_momentum) def on_train_begin(self, logs=None) -> None: @@ -181,7 +181,7 @@ def on_train_begin(self, logs=None) -> None: if self.cycle_momentum: try: K.set_value(self.model.optimizer.momentum, self.momentum) - except: + except Exception: K.set_value(self.model.optimizer.beta_1, self.momentum) def on_train_batch_end(self, batch, logs=None) -> None: @@ -189,7 +189,7 @@ def on_train_batch_end(self, batch, logs=None) -> None: lr = float(K.get_value(self.model.optimizer.lr)) try: mom = float(K.get_value(self.model.optimizer.momentum)) - except: + except Exception: mom = float(K.get_value(self.model.optimizer.beta_1)) # Append to the list self.track_lr.append(lr) @@ -200,12 +200,12 @@ def on_train_batch_end(self, batch, logs=None) -> None: self.step_num += 1 def plot_lrs_moms(self, axes=None) -> None: - if axes == None: + if axes is None: _, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 5)) else: try: ax1, ax2 = axes - except: + except Exception: ax1, ax2 = axes[0], axes[1] ax1.plot(self.track_lr) ax1.set_title("Learning Rate vs Steps") diff --git a/Model_T&T.ipynb b/Model_T&T.ipynb index caf7eea..ef81a87 100644 --- a/Model_T&T.ipynb +++ b/Model_T&T.ipynb @@ -114,7 +114,7 @@ "from Utils.Grad_cam import make_gradcam_heatmap\n", "from Utils.print_color_V2_NEW import print_Color_V2\n", "from Utils.print_color_V1_OLD import print_Color\n", - "from Utils.Other import *\n", + "from Utils.Other import * # noqa: F403\n", "\n", "# Other\n", "tf.get_logger().setLevel(\"ERROR\")\n", @@ -10356,14 +10356,14 @@ "\n", "\n", "# Funcs\n", - "def normalize_TO_RANGE(arr, min_val, max_val):\n", + "def normalize_TO_RANGE(arr, min_val, max_val): # noqa: F811\n", " arr = arr.astype(\"float32\")\n", " arr = (arr - arr.min()) / (arr.max() - arr.min())\n", " arr = arr * (max_val - min_val) + min_val\n", " return arr\n", "\n", "\n", - "def Z_SCORE_normalize(arr):\n", + "def Z_SCORE_normalize(arr): # noqa: F811\n", " arr = arr.astype(\"float32\")\n", " mean = np.mean(arr)\n", " std_dev = np.std(arr)\n", @@ -10613,8 +10613,8 @@ " advanced_mode=True,\n", ")\n", "# warnings\n", - "P_warning(\"[RES_Train -> True].\") if RES_Train else None\n", - "P_warning(\"[TerminateOnHighTemp_M -> False] GPU temperature protection is OFF\") if not TerminateOnHighTemp_M else None\n", + "P_warning(\"[RES_Train -> True].\") if RES_Train else None # noqa: F405\n", + "P_warning(\"[TerminateOnHighTemp_M -> False] GPU temperature protection is OFF\") if not TerminateOnHighTemp_M else None # noqa: F405\n", "print_Color(\"Setup Verbose END.\", [\"yellow\"])\n", "# MAIN LOOP\n", "try:\n", @@ -10636,7 +10636,7 @@ " advanced_mode=True,\n", " )\n", " # warnings\n", - " P_warning(\"[TerminateOnHighTemp_M -> False] GPU temperature protection is OFF\") if not TerminateOnHighTemp_M else None\n", + " P_warning(\"[TerminateOnHighTemp_M -> False] GPU temperature protection is OFF\") if not TerminateOnHighTemp_M else None # noqa: F405\n", " # DP\n", " if not AdvSubsetC:\n", " print_Color(\"Shuffling data...\", [\"yellow\"])\n", @@ -10860,7 +10860,7 @@ " # Garbage Collection (memory)\n", " gc.collect()\n", " tf.keras.backend.clear_session()\n", - " GPU_memUsage()\n", + " GPU_memUsage() # noqa: F405\n", " # Update TF summary text\n", " for Key in TF_Summary_text_Dict:\n", " TF_Summary_text_cache = f\"# @[{Key}].Data:\\n\"\n", @@ -11200,10 +11200,10 @@ "from numba import cuda\n", "\n", "device = cuda.get_current_device()\n", - "GPU_memUsage()\n", + "GPU_memUsage() # noqa: F405\n", "print(\"Realising all memory...\")\n", "device.reset()\n", - "GPU_memUsage()\n", + "GPU_memUsage() # noqa: F405\n", "print(\"done.\")" ] }, @@ -11221,7 +11221,7 @@ "outputs": [], "source": [ "# Save history\n", - "save_list(history, \"history\\\\model_history.pkl.gz\", compress=True)" + "save_list(history, \"history\\\\model_history.pkl.gz\", compress=True) # noqa: F405" ] }, { @@ -11231,7 +11231,7 @@ "outputs": [], "source": [ "# load history\n", - "history = load_list(\"history\\\\model_history.pkl.gz\", compressed=True)" + "history = load_list(\"history\\\\model_history.pkl.gz\", compressed=True) # noqa: F405" ] }, { @@ -11705,9 +11705,9 @@ "\n", "# Print acc\n", "print(\"Val data acc:\")\n", - "evaluate_model_full(y_val, val_predictions)\n", + "evaluate_model_full(y_val, val_predictions) # noqa: F405\n", "print(\"Test data acc:\")\n", - "evaluate_model_full(y_test, test_predictions)\n", + "evaluate_model_full(y_test, test_predictions) # noqa: F405\n", "\n", "# format data\n", "val_predictions = np.argmax(val_predictions, axis=1)\n", @@ -11824,7 +11824,7 @@ " plt.title(\"Number of Incorrect Predictions vs. Number of Data Points\")\n", " plt.show()\n", "except Exception:\n", - " P_warning(\"Failed to plot incorrect predictions vs. data points\")" + " P_warning(\"Failed to plot incorrect predictions vs. data points\") # noqa: F405" ] } ], diff --git a/Utils/lr_find.py b/Utils/lr_find.py index 5049644..9d660cc 100644 --- a/Utils/lr_find.py +++ b/Utils/lr_find.py @@ -108,7 +108,7 @@ def range_test( # save original model weights try: self.model.save_weights(self.weightsFile) - except: + except Exception: print("Unable to save initial weights, weights of model will change. Re-instantiate model to load previous weights ...") # start scheduler sched = Scheduler((start_lr, end_lr), num_iter) @@ -161,7 +161,7 @@ def _print_prompt(self) -> None: "Cleanup model weights disturbed during LRFinder exploration." try: self.model.load_weights(self.weightsFile) - except: + except Exception: print("Unable to load inital weights. Re-instantiate model to load previous weights ...") K.set_value(self.optimizer.lr, self.init_lr) print("LR Finder is complete, type {LrFinder}.plot_lrs() to see the graph.") @@ -194,7 +194,7 @@ def plot_lrs( if suggestion: try: mg = (np.gradient(np.array(losses))).argmin() - except: + except Exception: print("Failed to compute the gradients, there might not be enough points.") return print(f"Min numerical gradient: {lrs[mg]:.2E}") diff --git a/Utils/one_cycle.py b/Utils/one_cycle.py index a8151b4..d62aa96 100644 --- a/Utils/one_cycle.py +++ b/Utils/one_cycle.py @@ -156,7 +156,7 @@ def set_lr_mom(self) -> None: computed_momentum = self.anneal_func(self.m_momentum, self.b_momentum, self.step_num / self.step_size_up) try: K.set_value(self.model.optimizer.momentum, computed_momentum) - except: + except Exception: K.set_value(self.model.optimizer.beta_1, computed_momentum) else: down_step_num = self.step_num - self.step_size_up @@ -172,7 +172,7 @@ def set_lr_mom(self) -> None: ) try: K.set_value(self.model.optimizer.momentum, computed_momentum) - except: + except Exception: K.set_value(self.model.optimizer.beta_1, computed_momentum) def on_train_begin(self, logs=None) -> None: @@ -181,7 +181,7 @@ def on_train_begin(self, logs=None) -> None: if self.cycle_momentum: try: K.set_value(self.model.optimizer.momentum, self.momentum) - except: + except Exception: K.set_value(self.model.optimizer.beta_1, self.momentum) def on_train_batch_end(self, batch, logs=None) -> None: @@ -189,7 +189,7 @@ def on_train_batch_end(self, batch, logs=None) -> None: lr = float(K.get_value(self.model.optimizer.lr)) try: mom = float(K.get_value(self.model.optimizer.momentum)) - except: + except Exception: mom = float(K.get_value(self.model.optimizer.beta_1)) # Append to the list self.track_lr.append(lr) @@ -200,12 +200,12 @@ def on_train_batch_end(self, batch, logs=None) -> None: self.step_num += 1 def plot_lrs_moms(self, axes=None) -> None: - if axes == None: + if axes is None: _, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 5)) else: try: ax1, ax2 = axes - except: + except Exception: ax1, ax2 = axes[0], axes[1] ax1.plot(self.track_lr) ax1.set_title("Learning Rate vs Steps") diff --git a/history_vis.py b/history_vis.py index 5ac17bb..6eb6eee 100644 --- a/history_vis.py +++ b/history_vis.py @@ -1,9 +1,9 @@ -from Utils.Other import * +from Utils.Other import * # noqa: F403 import matplotlib.pyplot as plt import numpy as np # load history -history = load_list("history\\model_history.pkl.gz", compressed=True) +history = load_list("history\\model_history.pkl.gz", compressed=True) # noqa: F405 # Chunk size for 3D plot chunk_size = 6 # Change this to your desired chunk size diff --git a/ruff.toml b/ruff.toml index e7d019f..785793a 100644 --- a/ruff.toml +++ b/ruff.toml @@ -12,3 +12,5 @@ show-fixes = true fix = true # Err Ignore lint.ignore = ["E402"] +# exclude +exclude = ["backup", "Archive", "Exports"]