Keras
TensorBoard
image
myology
biology
histology
muscle
cells
fibers
myopathy
SDH
myoquant
classification
mitochondria
Eval Results (legacy)
Run this model with HF Jobs
Run corentinm7/MyoQuant-SDH-Model on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/corentinm7/MyoQuant-SDH-Model. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/corentinm7/MyoQuant-SDH-Model on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use corentinm7/MyoQuant-SDH-Model with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use corentinm7/MyoQuant-SDH-Model with Keras:
# !pip install -U keras tensorflow huggingface_hub # Keras needs TensorFlow installed to read "hf://" paths, so the tensorflow backend is selected here; # "jax" and "torch" also work for computation once TensorFlow is installed. import os os.environ["KERAS_BACKEND"] = "tensorflow" import keras model = keras.saving.load_model("hf://corentinm7/MyoQuant-SDH-Model") - Notebooks
- Google Colab
- Kaggle
Corentin commited on
Commit ·
8db3ab9
1
Parent(s): 3282ccf
Model Card and tensorboard
Browse filesFiles changed (4)
123---license: agpl-3.0---123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138---license: agpl-3.0tags:- image- keras- myology- biology- histology- muscle- cells- fibers- myopathy- SDH- myoquant- classification- mitochondriadatasets:- corentinm7/MyoQuant-SDH-Datametrics:- accuracylibrary_name: kerasmodel-index:- name: MyoQuant-SDH-Resnet50V2results:- task:type: image-classification # Required. Example: automatic-speech-recognitionname: Image Classification # Optional. Example: Speech Recognitiondataset:type: corentinm7/MyoQuant-SDH-Data # Required. Example: common_voice. Use dataset id from https://hf.co/datasetsname: MyoQuant SDH Data # Required. A pretty name for the dataset. Example: Common Voice (French)split: test # Optional. Example: testmetrics:- type: accuracy # Required. Example: wer. Use metric id from https://hf.co/metricsvalue: 0.9285 # Required. Example: 20.90name: Test Accuracy # Optional. Example: Test WER---## Model descriptionThis is the model card for the SDH Model used by the [MyoQuant](https://github.com/lambda-science/MyoQuant) tool.## Intended uses & limitationsIt's intended to allow people to use, improve and verify the reproducibility of our MyoQuant tool. The SDH model is used to classify SDH stained muscle fiber with abnormal mitochondria profile.## Training and evaluation dataIt's trained on the [corentinm7/MyoQuant-SDH-Data](https://huggingface.co/datasets/corentinm7/MyoQuant-SDH-Data), avaliable on HuggingFace Dataset Hub.## Training procedureThis model was trained using the ResNet50V2 model architecture in Keras.All images have been resized to 256x256 using the `tf.image.resize()` function from Tensorflow.Data augmentation was included as layers before ResNet50V2.Full model code:```pythondata_augmentation = tf.keras.Sequential([layers.Resizing(256, 256, interpolation="bilinear", crop_to_aspect_ratio=True, input_shape=(None, None, 3)),layers.Rescaling(scale=1./127.5, offset=-1),RandomBrightness(factor=0.2, value_range=(-1.0, 1.0)), # Not avaliable in tensorflow 2.8layers.RandomContrast(factor=0.2),layers.RandomFlip("horizontal_and_vertical"),layers.RandomRotation(0.3, fill_mode="constant"),layers.RandomZoom(.2, .2, fill_mode="constant"),layers.RandomTranslation(0.2, .2,fill_mode="constant"),])model = models.Sequential()model.add(data_augmentation)model.add(ResNet50V2(include_top=False,input_shape=(256,256,3),pooling="avg",))model.add(layers.Flatten())model.add(layers.Dense(2, activation='softmax'))``````_________________________________________________________________Layer (type) Output Shape Param #=================================================================sequential (Sequential) (None, 256, 256, 3) 0resnet50v2 (Functional) (None, 2048) 23564800flatten (Flatten) (None, 2048) 0dense (Dense) (None, 2) 4098=================================================================Total params: 23,568,898Trainable params: 23,523,458Non-trainable params: 45,440_________________________________________________________________```We used a ResNet50V2 pre-trained on ImageNet as a starting point and trained the model using an EarlyStopping with a value of 20 (i.e. if validation loss doesn't improve after 20 epoch, stop the training and roll back to the epoch with lowest val loss.)Class imbalance was handled by using the class\_-weight attribute during training. It was calculated for each class as `(1/n. elem of the class) * (n. of all training elem / 2)` giving in our case: `{0: 0.6593016912165849, 1: 2.069349315068493}`### Training hyperparametersThe following hyperparameters were used during training:- optimizer: Adam- Learning Rate Schedule: `ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=5, min_lr=MIN_LR` with START_LR = 1e-5 and MIN_LR = 1e-7- Loss Function: SparseCategoricalCrossentropy- Metric: Accuracy## Training CurvePlot of the accuracy vs epoch and loss vs epoch for training and validation set.## Test ResultsResults for accuracy metrics on the test split of the [corentinm7/MyoQuant-SDH-Data](https://huggingface.co/datasets/corentinm7/MyoQuant-SDH-Data) dataset.```105/105 - 15s - loss: 0.1618 - accuracy: 0.9285 - 15s/epoch - 140ms/stepTest data results:0.928528904914856```# How to Import the ModelTo import this model as it was trained in Tensorflow 2.8 on Google Colab, RandomBrightness layer had to be added by hand (it was only introduced in Tensorflow 2.10.). So you will need to download the `random_brightness.py` fille in addition to the model.Then the model can easily be imported in Tensorflow/Keras using:```pythonfrom .random_brightness import *model_sdh = keras.models.load_model("model.h5", custom_objects={"RandomBrightness": RandomBrightness})```
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345# @title Random Brightness Layerimport tensorflow as tffrom keras import backendfrom keras.engine import base_layerfrom keras.engine import base_preprocessing_layerfrom keras.layers.preprocessing import preprocessing_utils as utilsfrom keras.utils import tf_utils
from tensorflow.python.ops import stateless_random_opsfrom tensorflow.python.util.tf_export import keras_exportfrom tensorflow.tools.docs import doc_controls
@keras_export("keras.__internal__.layers.BaseImageAugmentationLayer")class BaseImageAugmentationLayer(base_layer.BaseRandomLayer): """Abstract base layer for image augmentaion. This layer contains base functionalities for preprocessing layers which augment image related data, eg. image and in future, label and bounding boxes. The subclasses could avoid making certain mistakes and reduce code duplications. This layer requires you to implement one method: `augment_image()`, which augments one single image during the training. There are a few additional methods that you can implement for added functionality on the layer: `augment_label()`, which handles label augmentation if the layer supports that. `augment_bounding_box()`, which handles the bounding box augmentation, if the layer supports that. `get_random_transformation()`, which should produce a random transformation setting. The tranformation object, which could be any type, will be passed to `augment_image`, `augment_label` and `augment_bounding_box`, to coodinate the randomness behavior, eg, in the RandomFlip layer, the image and bounding_box should be changed in the same way. The `call()` method support two formats of inputs: 1. Single image tensor with 3D (HWC) or 4D (NHWC) format. 2. A dict of tensors with stable keys. The supported keys are: `"images"`, `"labels"` and `"bounding_boxes"` at the moment. We might add more keys in future when we support more types of augmentation. The output of the `call()` will be in two formats, which will be the same structure as the inputs. The `call()` will handle the logic detecting the training/inference mode, unpack the inputs, forward to the correct function, and pack the output back to the same structure as the inputs. By default the `call()` method leverages the `tf.vectorized_map()` function. Auto-vectorization can be disabled by setting `self.auto_vectorize = False` in your `__init__()` method. When disabled, `call()` instead relies on `tf.map_fn()`. For example: ```python class SubclassLayer(BaseImageAugmentationLayer): def __init__(self): super().__init__() self.auto_vectorize = False ``` Example: ```python class RandomContrast(BaseImageAugmentationLayer): def __init__(self, factor=(0.5, 1.5), **kwargs): super().__init__(**kwargs) self._factor = factor def augment_image(self, image, transformation=None): random_factor = tf.random.uniform([], self._factor[0], self._factor[1]) mean = tf.math.reduced_mean(inputs, axis=-1, keep_dim=True) return (inputs - mean) * random_factor + mean ``` Note that since the randomness is also a common functionnality, this layer also includes a tf.keras.backend.RandomGenerator, which can be used to produce the random numbers. The random number generator is stored in the `self._random_generator` attribute. """
def __init__(self, rate=1.0, seed=None, **kwargs): super().__init__(seed=seed, **kwargs) self.rate = rate
@property def auto_vectorize(self): """Control whether automatic vectorization occurs. By default the `call()` method leverages the `tf.vectorized_map()` function. Auto-vectorization can be disabled by setting `self.auto_vectorize = False` in your `__init__()` method. When disabled, `call()` instead relies on `tf.map_fn()`. For example: ```python class SubclassLayer(BaseImageAugmentationLayer): def __init__(self): super().__init__() self.auto_vectorize = False ``` """ return getattr(self, "_auto_vectorize", True)
@auto_vectorize.setter def auto_vectorize(self, auto_vectorize): self._auto_vectorize = auto_vectorize
@property def _map_fn(self): if self.auto_vectorize: return tf.vectorized_map else: return tf.map_fn
@doc_controls.for_subclass_implementers def augment_image(self, image, transformation=None): """Augment a single image during training. Args: image: 3D image input tensor to the layer. Forwarded from `layer.call()`. transformation: The transformation object produced by `get_random_transformation`. Used to coordinate the randomness between image, label and bounding box. Returns: output 3D tensor, which will be forward to `layer.call()`. """ raise NotImplementedError()
@doc_controls.for_subclass_implementers def augment_label(self, label, transformation=None): """Augment a single label during training. Args: label: 1D label to the layer. Forwarded from `layer.call()`. transformation: The transformation object produced by `get_random_transformation`. Used to coordinate the randomness between image, label and bounding box. Returns: output 1D tensor, which will be forward to `layer.call()`. """ raise NotImplementedError()
@doc_controls.for_subclass_implementers def augment_bounding_box(self, bounding_box, transformation=None): """Augment bounding boxes for one image during training. Args: bounding_box: 2D bounding boxes to the layer. Forwarded from `call()`. transformation: The transformation object produced by `get_random_transformation`. Used to coordinate the randomness between image, label and bounding box. Returns: output 2D tensor, which will be forward to `layer.call()`. """ raise NotImplementedError()
@doc_controls.for_subclass_implementers def get_random_transformation(self, image=None, label=None, bounding_box=None): """Produce random transformation config for one single input. This is used to produce same randomness between image/label/bounding_box. Args: image: 3D image tensor from inputs. label: optional 1D label tensor from inputs. bounding_box: optional 2D bounding boxes tensor from inputs. Returns: Any type of object, which will be forwarded to `augment_image`, `augment_label` and `augment_bounding_box` as the `transformation` parameter. """ return None
def call(self, inputs, training=True): inputs = self._ensure_inputs_are_compute_dtype(inputs) if training: inputs, is_dict = self._format_inputs(inputs) images = inputs["images"] if images.shape.rank == 3: return self._format_output(self._augment(inputs), is_dict) elif images.shape.rank == 4: return self._format_output(self._batch_augment(inputs), is_dict) else: raise ValueError( "Image augmentation layers are expecting inputs to be " "rank 3 (HWC) or 4D (NHWC) tensors. Got shape: " f"{images.shape}" ) else: return inputs
def _augment(self, inputs): image = inputs.get("images", None) label = inputs.get("labels", None) bounding_box = inputs.get("bounding_boxes", None) transformation = self.get_random_transformation( image=image, label=label, bounding_box=bounding_box ) # pylint: disable=assignment-from-none image = self.augment_image(image, transformation=transformation) result = {"images": image} if label is not None: label = self.augment_label(label, transformation=transformation) result["labels"] = label if bounding_box is not None: bounding_box = self.augment_bounding_box( bounding_box, transformation=transformation ) result["bounding_boxes"] = bounding_box return result
def _batch_augment(self, inputs): return self._map_fn(self._augment, inputs)
def _format_inputs(self, inputs): if tf.is_tensor(inputs): # single image input tensor return {"images": inputs}, False elif isinstance(inputs, dict): # TODO(scottzhu): Check if it only contains the valid keys return inputs, True else: raise ValueError( f"Expect the inputs to be image tensor or dict. Got {inputs}" )
def _format_output(self, output, is_dict): if not is_dict: return output["images"] else: return output
def _ensure_inputs_are_compute_dtype(self, inputs): if isinstance(inputs, dict): inputs["images"] = utils.ensure_tensor(inputs["images"], self.compute_dtype) else: inputs = utils.ensure_tensor(inputs, self.compute_dtype) return inputs
@keras_export("keras.layers.RandomBrightness", v1=[])class RandomBrightness(BaseImageAugmentationLayer): """A preprocessing layer which randomly adjusts brightness during training. This layer will randomly increase/reduce the brightness for the input RGB images. At inference time, the output will be identical to the input. Call the layer with `training=True` to adjust the brightness of the input. Note that different brightness adjustment factors will be apply to each the images in the batch. For an overview and full list of preprocessing layers, see the preprocessing [guide](https://www.tensorflow.org/guide/keras/preprocessing_layers). Args: factor: Float or a list/tuple of 2 floats between -1.0 and 1.0. The factor is used to determine the lower bound and upper bound of the brightness adjustment. A float value will be chosen randomly between the limits. When -1.0 is chosen, the output image will be black, and when 1.0 is chosen, the image will be fully white. When only one float is provided, eg, 0.2, then -0.2 will be used for lower bound and 0.2 will be used for upper bound. value_range: Optional list/tuple of 2 floats for the lower and upper limit of the values of the input data. Defaults to [0.0, 255.0]. Can be changed to e.g. [0.0, 1.0] if the image input has been scaled before this layer. The brightness adjustment will be scaled to this range, and the output values will be clipped to this range. seed: optional integer, for fixed RNG behavior. Inputs: 3D (HWC) or 4D (NHWC) tensor, with float or int dtype. Input pixel values can be of any range (e.g. `[0., 1.)` or `[0, 255]`) Output: 3D (HWC) or 4D (NHWC) tensor with brightness adjusted based on the `factor`. By default, the layer will output floats. The output value will be clipped to the range `[0, 255]`, the valid range of RGB colors, and rescaled based on the `value_range` if needed. Sample usage: ```python random_bright = tf.keras.layers.RandomBrightness(factor=0.2) # An image with shape [2, 2, 3] image = [[[1, 2, 3], [4 ,5 ,6]], [[7, 8, 9], [10, 11, 12]]] # Assume we randomly select the factor to be 0.1, then it will apply # 0.1 * 255 to all the channel output = random_bright(image, training=True) # output will be int64 with 25.5 added to each channel and round down. tf.Tensor([[[26.5, 27.5, 28.5] [29.5, 30.5, 31.5]] [[32.5, 33.5, 34.5] [35.5, 36.5, 37.5]]], shape=(2, 2, 3), dtype=int64) ``` """
_FACTOR_VALIDATION_ERROR = ( "The `factor` argument should be a number (or a list of two numbers) " "in the range [-1.0, 1.0]. " ) _VALUE_RANGE_VALIDATION_ERROR = ( "The `value_range` argument should be a list of two numbers. " )
def __init__(self, factor, value_range=(0, 255), seed=None, **kwargs): base_preprocessing_layer.keras_kpl_gauge.get_cell("RandomBrightness").set(True) super().__init__(seed=seed, force_generator=True, **kwargs) self._set_factor(factor) self._set_value_range(value_range) self._seed = seed
def augment_image(self, image, transformation=None): return self._brightness_adjust(image, transformation["rgb_delta"])
def augment_label(self, label, transformation=None): return label
def get_random_transformation(self, image=None, label=None, bounding_box=None): rgb_delta_shape = (1, 1, 1) random_rgb_delta = self._random_generator.random_uniform( shape=rgb_delta_shape, minval=self._factor[0], maxval=self._factor[1], ) random_rgb_delta = random_rgb_delta * ( self._value_range[1] - self._value_range[0] ) return {"rgb_delta": random_rgb_delta}
def _set_value_range(self, value_range): if not isinstance(value_range, (tuple, list)): raise ValueError(self._VALUE_RANGE_VALIDATION_ERROR + f"Got {value_range}") if len(value_range) != 2: raise ValueError(self._VALUE_RANGE_VALIDATION_ERROR + f"Got {value_range}") self._value_range = sorted(value_range)
def _set_factor(self, factor): if isinstance(factor, (tuple, list)): if len(factor) != 2: raise ValueError(self._FACTOR_VALIDATION_ERROR + f"Got {factor}") self._check_factor_range(factor[0]) self._check_factor_range(factor[1]) self._factor = sorted(factor) elif isinstance(factor, (int, float)): self._check_factor_range(factor) factor = abs(factor) self._factor = [-factor, factor] else: raise ValueError(self._FACTOR_VALIDATION_ERROR + f"Got {factor}")
def _check_factor_range(self, input_number): if input_number > 1.0 or input_number < -1.0: raise ValueError(self._FACTOR_VALIDATION_ERROR + f"Got {input_number}")
def _brightness_adjust(self, image, rgb_delta): image = utils.ensure_tensor(image, self.compute_dtype) rank = image.shape.rank if rank != 3: raise ValueError( "Expected the input image to be rank 3. Got " f"inputs.shape = {image.shape}" ) rgb_delta = tf.cast(rgb_delta, image.dtype) image += rgb_delta return tf.clip_by_value(image, self._value_range[0], self._value_range[1])
def get_config(self): config = { "factor": self._factor, "value_range": self._value_range, "seed": self._seed, } base_config = super().get_config() return dict(list(base_config.items()) + list(config.items()))123version https://git-lfs.github.com/spec/v1oid sha256:b74dc7d937740821f56203f12e53a9f279e14e65e58aae07b6f0b2cd1c37c2b9size 8894151123version https://git-lfs.github.com/spec/v1oid sha256:f2a41bb11af557b261892fa9b42a6a8ff9ddd0917418cbdccac031a2009f7c6fsize 11236README.md+135
random_brightness.pyAdded+345
runs/sdh16k_normal_resize_20220830-083856/train/events.out.tfevents.1661848752.561a638614d6.77.0.v2Added+3
runs/sdh16k_normal_resize_20220830-083856/validation/events.out.tfevents.1661848941.561a638614d6.77.1.v2Added+3