Git Product home page Git Product logo

Comments (10)

leondgarse avatar leondgarse commented on June 11, 2024

I think you are using a model with 2 outputs and 2 losses? Technically, you can modify coco_train_script.py for your own usage, or just follow the steps build model -> build dataset -> build optimzier -> init loss -> compile -> fit. Here is a standalone one extracted from the script.

import tensorflow as tf
from tensorflow import keras
from keras_cv_attention_models import yolov8, hornet
from keras_cv_attention_models.coco import init_dataset, eval_func, losses
from keras_cv_attention_models.imagenet import train, init_global_strategy, init_lr_scheduler

input_shape = (320, 320, 3)
num_classes = 2  # Specify the actual num_classes
batch_size = 4
anchors_mode = "anchor_free"
anchor_pyramid_levels = [3, 5]
rescale_mode = "raw01"
lr_base = 1e-3
lr_decay_steps = 30
data_name = "coco/2017"  # Or custom one "datasets/coco_dog_cat/detections.json"

strategy = init_global_strategy(enable_float16=True)

with strategy.scope():
    # Build own model
    backbone = hornet.HorNetTiny(num_classes=0, input_shape=input_shape)
    model = yolov8.YOLOV8_S(
        backbone=backbone, input_shape=input_shape, anchors_mode='anchor_free', regression_len=4, pretrained=None, num_classes=num_classes
    )
    # Add another output
    another_output = keras.layers.GlobalAveragePooling2D(name="another")(model.inputs[0])
    model = keras.models.Model(model.inputs[0], model.outputs + [another_output])

    train_dataset = init_dataset(
        data_name=data_name, input_shape=input_shape, rescale_mode=rescale_mode, batch_size=batch_size, anchors_mode=anchors_mode, anchor_pyramid_levels=anchor_pyramid_levels,
    )[0]
    # Add another output for dataset
    train_dataset = train_dataset.map(lambda xx, yy: (xx, (yy, tf.random.uniform([batch_size, 3]))))

    lr_scheduler, lr_total_epochs = init_lr_scheduler(lr_base, lr_decay_steps)
    loss = losses.AnchorFreeLoss(input_shape, pyramid_levels=anchor_pyramid_levels)
    metrics = losses.ClassAccuracyWithBboxWrapper(loss)
    optimizer = keras.optimizers.AdamW(learning_rate=lr_base, weight_decay=0.002)

    # Add another loss and metrics
    another_loss = keras.losses.MeanSquaredError()
    model.compile(optimizer, loss=[loss, another_loss], metrics={"outputs_fp32": metrics, 'another': None})
    coco_ap_eval = eval_func.COCOEvalCallback(data_name, rescale_mode=rescale_mode, batch_size=batch_size)

    other_fit_kwargs = {}  # Set custom ones for `model.fit`
    latest_save, hist = train(
        compiled_model=model,
        epochs=lr_total_epochs,
        train_dataset=train_dataset,
        test_dataset=None,  # COCO eval using coco_ap_eval callback, set `validation_data` for `model.fit` to None
        initial_epoch=0,
        lr_scheduler=lr_scheduler,
        basic_save_name="coco_test",
        init_callbacks=[coco_ap_eval],
        logs=None,
        **other_fit_kwargs,
    )

from keras_cv_attention_models.

shshrzad avatar shshrzad commented on June 11, 2024

Thank you for your kindness and helpful response. My first outputs of dataset are a set of images and the second ones are object detection labels. Isn't there any way to feed directory of two sets of images and labels to the model in keras_cv_attention_model? If I want to create json file how should I do it?

from keras_cv_attention_models.

leondgarse avatar leondgarse commented on June 11, 2024

The json file doesn't supports this, you can modify the dataset after creation anyway. Like using tf.data.Dataset.zip and map combining 2 built datasets, or justing adding an input to dataset like the output one.

train_dataset = init_dataset(...)[0]
another_dataset = init_dataset(...)[0]
# Zip them
tt = tf.data.Dataset.zip((train_dataset, another_dataset))

(aa, bb), (cc, dd) = tt.as_numpy_iterator().next()
print(aa.shape, bb.shape, cc.shape, dd.shape)
# (4, 320, 320, 3) (4, 320, 320, 3) (4, 100, 7) (4, 100, 7)

# Map and rearrange, here keeps only the first lables output, but repeat twice
tt = tt.map(lambda one, another: ((one[0], another[0]), (one[1], one[1])))
(images, another_images), (labels, another_labels) = tt.as_numpy_iterator().next()
print(images.shape, another_images.shape, labels.shape, another_labels.shape)
# (4, 320, 320, 3) (4, 320, 320, 3) (4, 100, 7) (4, 100, 7)

Then create a model with 2 inputs, and 2 outputs, compile a new loss, and feed the dataset in.

another_input = keras.layers.Input(model.input_shape[1:])
another_output = model(another_input)
model = keras.models.Model([model.inputs[0], another_input], [model.outputs[0], another_output])
...
model.compile(optimizer, loss=[loss, another_loss], metrics={model.output_names[0]: metrics, model.output_names[1]: None})
latest_save, hist = train(compiled_model=model, train_dataset=train_dataset, ...)

Anyway, train_dataset and model are just typical TF Dataset and Model, you can apply any available TF / Keras api on them.

from keras_cv_attention_models.

shshrzad avatar shshrzad commented on June 11, 2024

Thank you. I appreciate your kindness. With your helpful guidance I could design a new object detection model that consists of several models. Now a new problem is appeared. Due to changing the model and using 2 inputs and 4 outputs in my model (the last output is object detection model output), a lot of bugs occured in eval_func.py during training. I fixed some bugs but now I can not do anything more to resolving them. What do you propose? Is there any straight forward method to evaluate the performance of model without using eval_func.py?

from keras_cv_attention_models.

leondgarse avatar leondgarse commented on June 11, 2024

May try if rebuild a model taking required input/output for coco eval works in your case, before COCOEvalCallback.build.

def on_epoch_end(self, epoch=0, logs=None):
    if not self.built:
        if self.dataset_kwargs["rescale_mode"] == "auto":
            self.dataset_kwargs["rescale_mode"] = getattr(self.model, "rescale_mode", "torch")
        self.model = models.Model(self.model.inputs[0], self.model.outputs[-1])  # <- rebuild model
        self.build(self.model.input_shape, self.model.output_shape)

It should work without much issue...

from keras_cv_attention_models.

shshrzad avatar shshrzad commented on June 11, 2024

from keras_cv_attention_models.

leondgarse avatar leondgarse commented on June 11, 2024

How about build model with self.model = models.Model(self.model.outputs[0], self.model.outputs[-1])? Also note if eval_func.py#L467 giving correct print info. Anchors shape should match with correct model input_shape.

from keras_cv_attention_models.coco import anchors_func
print(anchors_func.get_anchor_free_anchors(input_shape=(512, 512, 3)).shape)
# (5376, 4)

from keras_cv_attention_models.

shshrzad avatar shshrzad commented on June 11, 2024

Thank you sir. I appreciate your responsibility. It works. Doesn't using "self.model = models.Model(self.model.outputs[0], self.model.outputs[-1])" lead to incorrect anchors? what about plotting predicted bounding boxes on images in the test phase in this case? As you know I have a custom model with different input shapes. Also, I changed the integration of features feeding to neck. When I load my trained model although I use my custom model architeture it skipps some layers and returns false results of object detection that is so different from AP values obtained by eval_func.py.

from keras_cv_attention_models.

leondgarse avatar leondgarse commented on June 11, 2024
  • The anchor initializing actually needs only the model inpu_shape for anchor_free mode. Just providing a model with correct input_shape and output_shape should be fine.
  • It shouldn't skip any layer if using same architecture, may check the log which layers are skipped, and why they mismatch.
  • May also try load directly
    from tensorflow import keras
    import keras_cv_attention_models  # In case missing custom layer definition
    
    model = keras.models.load_model("xxxx.h5", compile=False)
    # build a sub-model again
    model = keras.models.Model(model.outputs[0], model.outputs[-1])

from keras_cv_attention_models.

leondgarse avatar leondgarse commented on June 11, 2024

Closing now, may raise new issue if still needed.

from keras_cv_attention_models.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.