mlco2 / codecarbon

Track emissions from Compute and recommend ways to reduce their impact on the environment.
https://mlco2.github.io/codecarbon
MIT License
1.01k stars 158 forks source link

AttributeError: 'NoneType' object has no attribute 'out' #406

Closed HishamAbulfeilat closed 1 year ago

HishamAbulfeilat commented 1 year ago

Description

the code was working well however today when I launched it I get attribute Error , I tried to reinstall codecarbon package, still same error

What I Did

import logging
log_name = 'CodeCarbon'
LoggerOutput = logging.debug
# Create a dedicated logger (log name can be the Code Carbon project name for example)
_logger = logging.getLogger(log_name)

# Add a handler, see Python logging for various handlers (here a local file named after log_name)
_channel = logging.FileHandler(log_name + '.log')
_logger.addHandler(_channel)

# Set logging level from DEBUG to CRITICAL (typically INFO)
# This level can be used in the logging process to filter emissions messages
_logger.setLevel(logging.INFO)

# Create a Code Carbon LoggerOutput with the logger, specifying the logging level to be used for emissions data messages
my_logger = LoggerOutput(_logger, logging.INFO)
print(my_logger)

import tensorflow as tf
import pandas as pd

from codecarbon import EmissionsTracker

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential(
    [
        tf.keras.layers.Flatten(input_shape=(28, 28)),
        tf.keras.layers.Dense(128, activation="relu"),
        tf.keras.layers.Dropout(0.2),
        tf.keras.layers.Dense(10),
    ]
)

loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)

model.compile(optimizer="adam", loss=loss_fn, metrics=["accuracy"])
tracker = EmissionsTracker(save_to_logger=True, logging_logger=my_logger)
# tracker = EmissionsTracker()
tracker.start()
model.fit(x_train, y_train, epochs=10)
emissions: float = tracker.stop()
print(emissions)

[codecarbon WARNING @ 20:09:00] graceful shutdown. Exceptions:
[codecarbon WARNING @ 20:09:00] <class 'Exception'>
Traceback (most recent call last):
  File "C:\Users\Hisham\anaconda3\lib\site-packages\codecarbon\core\util.py", line 18, in suppress
    yield
  File "C:\Users\Hisham\anaconda3\lib\contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "C:\Users\Hisham\anaconda3\lib\site-packages\codecarbon\emissions_tracker.py", line 430, in stop
    persistence.out(emissions_data)
AttributeError: 'NoneType' object has no attribute 'out'
[codecarbon WARNING @ 20:09:00] stopping.
inimaz commented 1 year ago

I think the issue is in your logger. You are not importing loggerOutput from codecarbon.

Instead of

import logging
log_name = 'CodeCarbon'
LoggerOutput = logging.debug ```
do

import logging from codecarbon.output import ( LoggerOutput, ) log_name = "CodeCarbon"


and you will be alright.
HishamAbulfeilat commented 1 year ago

alright thank you