Optuna makes error messsage

Hi Guys,
I don’t know why this script for optimising doesn’t work.
Could you help me please?

from fastai.data.core import DataLoaders
import os

train_ds = ImageDataset(files[~is_valid], targets[~is_valid], im_tfm=train_tfms)
valid_ds = ImageDataset(files[is_valid], targets[is_valid], im_tfm=val_tfms)
dls = DataLoaders.from_dsets(train_ds, valid_ds, num_workers=min(os.cpu_count(), 16))

from fastai.vision.all import *
import torchvision.models as models
from fastai.metrics import accuracy
import optuna

# Définition des architectures de ResNet
architectures = [models.resnet18, models.resnet34, models.resnet50, models.resnet101, models.resnet152]

# Définition des hyperparamètres à tester
batch_sizes = [40, 80, 120, 160, 200, 240, 280, 320, 350]
optimizers = [Adam, SGD]  # Ajoutez d'autres optimiseurs si nécessaire
learning_rates = [0.001, 0.01, 0.1, 0.5]
epochs = [3, 10, 20, 40, 60]


# Utilisation du dataset
per_data = 0.1
resizes = 100

nber_trials = 10

best_accuracy = 0.0
best_config = {}

# Fonction objectif pour Optuna

def objective(trial):
    # Paramètres à optimiser
    architecture = trial.suggest_categorical('architecture', architectures)
    batch_size = trial.suggest_categorical('batch_size', batch_sizes)
    optimizer = trial.suggest_categorical('optimizer', optimizers)
    learning_rate = trial.suggest_categorical('learning_rate', learning_rates)
    num_epochs = trial.suggest_categorical('num_epochs', epochs)
  
     
    # Création du modèle
    num_classes = 2
    model = architecture(pretrained=True)
    num_ftrs = model.fc.in_features
    model.fc = nn.Linear(num_ftrs, 2)
    
    # Entraînement du modèle   // , metrics=[accuracy
    learn = Learner(dls.cuda(), model.cuda(), loss_func=nn.CrossEntropyLoss(), opt_func=optimizer, metrics = accuracy)
    learn.fine_tune(num_epochs, base_lr=learning_rate, freeze_epochs=2, pct_start=0.3)
        
    # Évaluation sur les données de validation
    accuracy = learn.validate(dl=val_ds)[1]
    
    # Mise à jour de la meilleure configuration
    global best_accuracy, best_config
    if accuracy > best_accuracy:
        best_accuracy = accuracy
        best_config = {
            'architecture': architecture.__name__,
            'batch_size': batch_size,
            'optimizer': optimizer.__name__,
            'learning_rate': learning_rate,
            'num_epochs': num_epochs}
    
    return accuracy

nber_trials = 2
from fastai.metrics import accuracy


# Optimisation des hyperparamètres avec Optuna
study = optuna.create_study(direction='maximize')
study.optimize(objective, n_trials=2)

print("Meilleure configuration :")
print(best_config)
print("Meilleure précision : ", best_accuracy)

Here you can see the error message where I don’t know why the accuracy metric didn’t work:

type or paste co[I 2023-06-30 13:05:29,422] A new study created in memory with name: no-name-29ac0615-393c-495d-ba7a-771f645a2872
[W 2023-06-30 13:05:29,854] Trial 0 failed with parameters: {'architecture': <function resnet101 at 0x7fc09c249120>, 'batch_size': 200, 'optimizer': <function SGD at 0x7fc090218ee0>, 'learning_rate': 0.5, 'num_epochs': 20} because of the following error: UnboundLocalError("local variable 'accuracy' referenced before assignment").
Traceback (most recent call last):
  File "/home/ben/anaconda3/envs/tor-base2/lib/python3.10/site-packages/optuna/study/_optimize.py", line 200, in _run_trial
    value_or_values = func(trial)
  File "/tmp/ipykernel_6138/3298889843.py", line 19, in objective
    learn = Learner(dls.cuda(), model.cuda(), loss_func=nn.CrossEntropyLoss(), opt_func=optimizer,metrics = accuracy)
UnboundLocalError: local variable 'accuracy' referenced before assignment
[W 2023-06-30 13:05:29,854] Trial 0 failed with value None.

---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
Cell In[91], line 7
      5 # Optimisation des hyperparamètres avec Optuna
      6 study = optuna.create_study(direction='maximize')
----> 7 study.optimize(objective, n_trials=2)
      9 print("Meilleure configuration :")
     10 print(best_config)

File ~/anaconda3/envs/tor-base2/lib/python3.10/site-packages/optuna/study/study.py:443, in Study.optimize(self, func, n_trials, timeout, n_jobs, catch, callbacks, gc_after_trial, show_progress_bar)
    339 def optimize(
    340     self,
    341     func: ObjectiveFuncType,
   (...)
    348     show_progress_bar: bool = False,
    349 ) -> None:
    350     """Optimize an objective function.
    351 
    352     Optimization is done by choosing a suitable set of hyperparameter values from a given
   (...)
    440             If nested invocation of this method occurs.
    441     """
--> 443     _optimize(
    444         study=self,
    445         func=func,
    446         n_trials=n_trials,
    447         timeout=timeout,
    448         n_jobs=n_jobs,
    449         catch=tuple(catch) if isinstance(catch, Iterable) else (catch,),
    450         callbacks=callbacks,
    451         gc_after_trial=gc_after_trial,
    452         show_progress_bar=show_progress_bar,
    453     )

File ~/anaconda3/envs/tor-base2/lib/python3.10/site-packages/optuna/study/_optimize.py:66, in _optimize(study, func, n_trials, timeout, n_jobs, catch, callbacks, gc_after_trial, show_progress_bar)
     64 try:
     65     if n_jobs == 1:
---> 66         _optimize_sequential(
     67             study,
     68             func,
     69             n_trials,
     70             timeout,
     71             catch,
     72             callbacks,
     73             gc_after_trial,
     74             reseed_sampler_rng=False,
     75             time_start=None,
     76             progress_bar=progress_bar,
     77         )
     78     else:
     79         if n_jobs == -1:

File ~/anaconda3/envs/tor-base2/lib/python3.10/site-packages/optuna/study/_optimize.py:163, in _optimize_sequential(study, func, n_trials, timeout, catch, callbacks, gc_after_trial, reseed_sampler_rng, time_start, progress_bar)
    160         break
    162 try:
--> 163     frozen_trial = _run_trial(study, func, catch)
    164 finally:
    165     # The following line mitigates memory problems that can be occurred in some
    166     # environments (e.g., services that use computing containers such as GitHub Actions).
    167     # Please refer to the following PR for further details:
    168     # https://github.com/optuna/optuna/pull/325.
    169     if gc_after_trial:

File ~/anaconda3/envs/tor-base2/lib/python3.10/site-packages/optuna/study/_optimize.py:251, in _run_trial(study, func, catch)
    244         assert False, "Should not reach."
    246 if (
    247     frozen_trial.state == TrialState.FAIL
    248     and func_err is not None
    249     and not isinstance(func_err, catch)
    250 ):
--> 251     raise func_err
    252 return frozen_trial

File ~/anaconda3/envs/tor-base2/lib/python3.10/site-packages/optuna/study/_optimize.py:200, in _run_trial(study, func, catch)
    198 with get_heartbeat_thread(trial._trial_id, study._storage):
    199     try:
--> 200         value_or_values = func(trial)
    201     except exceptions.TrialPruned as e:
    202         # TODO(mamu): Handle multi-objective cases.
    203         state = TrialState.PRUNED

Cell In[90], line 19, in objective(trial)
     16 model.fc = nn.Linear(num_ftrs, 2)
     18 # Entraînement du modèle   // , metrics=[accuracy
---> 19 learn = Learner(dls.cuda(), model.cuda(), loss_func=nn.CrossEntropyLoss(), opt_func=optimizer,metrics = accuracy)
     20 learn.fine_tune(num_epochs, base_lr=learning_rate, freeze_epochs=2, pct_start=0.3)
     22 # Évaluation sur les données de validation

UnboundLocalError: local variable 'accuracy' referenced before assignment

de here

Thank you in advance
Best
Nazil

In the instantiation of Learner you are using metrics = accuracy where accuracy is undefined. Check if e.g. a string is expected instead of this undefined object.