Skip to content

03h_finetune_DENSENET201_flowers104 : Mixing different tf.distribute.Strategy objects error #19

Description

@Digital1O1

Current issue

I'm currently facing the following error

Error

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[11], line 1
----> 1 history = model.fit(get_training_dataset(), steps_per_epoch=STEPS_PER_EPOCH, epochs=EPOCHS,
      2                     validation_data=get_validation_dataset(), validation_steps=VALIDATION_STEPS,
      3                     callbacks=[lr_callback])

File ~/.local/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py:122, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    119     filtered_tb = _process_traceback_frames(e.__traceback__)
    120     # To get the full stack trace, call:
    121     # `keras.config.disable_traceback_filtering()`
--> 122     raise e.with_traceback(filtered_tb) from None
    123 finally:
    124     del filtered_tb

File ~/.local/lib/python3.10/site-packages/tensorflow/python/distribute/distribute_lib.py:303, in _wrong_strategy_scope(strategy, context)
    299   raise RuntimeError(
    300       'Need to be inside "with strategy.scope()" for %s' %
    301       (strategy,))
    302 else:
--> 303   raise RuntimeError(
    304       "Mixing different tf.distribute.Strategy objects: %s is not %s" %
    305       (context.strategy, strategy))

RuntimeError: Mixing different tf.distribute.Strategy objects: 
<tensorflow.python.distribute.mirrored_strategy.MirroredStrategy object at 0x7f11542a22f0>
is not <tensorflow.python.distribute.distribute_lib._DefaultDistributionStrategy object at 0x7f0fa82065c0>

Jupypter Cell In Question causing the error stated above

history = model.fit(get_training_dataset(), steps_per_epoch=STEPS_PER_EPOCH, epochs=EPOCHS,
                    validation_data=get_validation_dataset(), validation_steps=VALIDATION_STEPS,
                    callbacks=[lr_callback])

Things I've tried to resolve the issue

Since I suck at Python and I'm very green when it comes to machine vision as a whole, this is what I got form ChatGPT

First: What the Error Means

This part:

RuntimeError: Mixing different tf.distribute.Strategy objects...

means that your model was created or compiled under one strategy (e.g., MirroredStrategy)
but you're trying to train it while TensorFlow thinks it should use the default strategy
(_DefaultDistributionStrategy) — no distribution.

They must match:
Either both created and trained under a strategy, or neither.

Things I've tried so far

  • Just using strategy = tf.distribute.MirroredStrategy() and got the same error as stated earlier
  • Verifying that the MirroredStrategy was actually being used (See code below)
try: # detect TPUs
    tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect()
    strategy = tf.distribute.TPUStrategy(tpu)
except ValueError: # detect GPUs or multi-GPU machines
    strategy = tf.distribute.MirroredStrategy()

print("REPLICAS: ", strategy.num_replicas_in_sync)
print(strategy)

# Output from terminal
# INFO:tensorflow:Using MirroredStrategy with devices ('/job:localhost/replica:0/task:0/device:GPU:0',)
# REPLICAS:  1
# <tensorflow.python.distribute.mirrored_strategy.MirroredStrategy object at 0x7f0f147ee170>

The following is what I have/currently running on my machine

  • I have a NVIDIA GTX 1660 Ti
  • Using WSL2 on my Windows 11 machine (Doubt that's even the issue)
  • Python 3.10.12

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions