Skip to content

Commit

Permalink
Restrict logging messages to ERROR level for tf and matplotlib. Switc…
Browse files Browse the repository at this point in the history
…h kivy logging to python base logging.
  • Loading branch information
DocGarbanzo committed Sep 28, 2024
1 parent a43c327 commit 0f850e2
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 41 deletions.
7 changes: 4 additions & 3 deletions donkeycar/management/makemovie.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import tempfile
import logging

logging.getLogger('tensorflow').setLevel(logging.WARNING)
import tensorflow as tf
from tensorflow.python.keras import activations
from tensorflow.python.keras import backend as K
from tensorflow.python.keras.models import load_model
import tensorflow as tf
import cv2
from matplotlib import cm

import cv2

import donkeycar as dk
from donkeycar.parts.tub_v2 import Tub
Expand Down
19 changes: 11 additions & 8 deletions donkeycar/management/ui/car_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import re
from functools import partial
from subprocess import Popen, PIPE, STDOUT
import logging

from kivy import Logger
from kivy.clock import Clock
from kivy.properties import NumericProperty, ObjectProperty, StringProperty, \
ListProperty, BooleanProperty
Expand All @@ -12,6 +12,9 @@
from donkeycar.management.ui.rc_file_handler import rc_handler


logger = logging.getLogger(__name__)


class CarScreen(AppScreen):
""" Screen for interacting with the car. """
config = ObjectProperty(force_dispatch=True, allownone=True)
Expand Down Expand Up @@ -55,7 +58,7 @@ def pull(self, tub_dir):
if not self.ids.create_dir.active:
target += '/'
cmd = ['rsync', '-rv', '--progress', '--partial', target, dest]
Logger.info('car pull: ' + str(cmd))
logger.info('car pull: ' + str(cmd))
proc = Popen(cmd, shell=False, stdout=PIPE, text=True,
encoding='utf-8', universal_newlines=True)
repeats = 100
Expand Down Expand Up @@ -84,7 +87,7 @@ def send_pilot(self):
dest = f'{self.config.PI_USERNAME}@{self.config.PI_HOSTNAME}:' + \
f'{os.path.join(self.car_dir, "models")}'
cmd = ['rsync', '-rv', '--progress', '--partial', *filter, src, dest]
Logger.info('car push: ' + ' '.join(cmd))
logger.info('car push: ' + ' '.join(cmd))
proc = Popen(cmd, shell=False, stdout=PIPE,
encoding='utf-8', universal_newlines=True)
repeats = 0
Expand Down Expand Up @@ -189,7 +192,7 @@ def drive(self):
f'{self.config.PI_USERNAME}@{self.config.PI_HOSTNAME}',
f'source env/bin/activate; cd {self.car_dir}; ./manage.py '
f'drive {model_args} 2>&1']
Logger.info(f'car connect: {cmd}')
logger.info(f'car connect: {cmd}')
proc = Popen(cmd, shell=False, stdout=PIPE, text=True,
encoding='utf-8', universal_newlines=True)
while True:
Expand All @@ -201,12 +204,12 @@ def drive(self):
if res:
try:
self.pid = int(res.group(0).split('PID: ')[1])
Logger.info(f'car connect: manage.py drive PID: '
logger.info(f'car connect: manage.py drive PID: '
f'{self.pid}')
except Exception as e:
Logger.error(f'car connect: {e}')
logger.error(f'car connect: {e}')
return
Logger.info(f'car connect: {stdout_data}')
logger.info(f'car connect: {stdout_data}')
else:
return

Expand All @@ -215,5 +218,5 @@ def stop(self):
cmd = f'ssh {self.config.PI_USERNAME}@{self.config.PI_HOSTNAME} '\
+ f'kill -SIGINT {self.pid}'
out = os.popen(cmd).read()
Logger.info(f"car connect: Kill PID {self.pid} + {out}")
logger.info(f"car connect: Kill PID {self.pid} + {out}")
self.pid = None
12 changes: 8 additions & 4 deletions donkeycar/management/ui/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
import os
import io
import time
import logging

import numpy as np
from functools import partial
from PIL import Image as PilImage

from kivy import Logger
#from kivy import logger
from kivy.app import App
from kivy.clock import Clock
from kivy.core.window import Window
Expand All @@ -26,6 +27,9 @@

from donkeycar.management.ui.rc_file_handler import rc_handler

logger = logging.getLogger(__name__)


LABEL_SPINNER_TEXT = 'Add/remove'


Expand Down Expand Up @@ -139,7 +143,7 @@ def update(self, record):
return
field, index = decompose(self.field)
if not field in record.underlying:
Logger.error(f'Record: Bad record {record.underlying["_index"]} - '
logger.error(f'Record: Bad record {record.underlying["_index"]} - '
f'missing field {self.field}')
return
val = record.underlying[field]
Expand Down Expand Up @@ -236,10 +240,10 @@ def update(self, record):
self.core_image = CoreImage(bytes_io, ext='png')
self.texture = self.core_image.texture
except KeyError as e:
Logger.error(f'Record {record.underlying["_index"]}: '
logger.error(f'Record {record.underlying["_index"]}: '
f'Missing key: {e}')
except Exception as e:
Logger.error(f'Record : {record.underlying["_index"]}'
logger.error(f'Record : {record.underlying["_index"]}'
f'Bad record: {e}')

def get_image(self, record):
Expand Down
17 changes: 10 additions & 7 deletions donkeycar/management/ui/pilot_screen.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from copy import copy #, deepcopy
import os
import logging

from kivy import Logger
#from kivy import logger
from kivy.properties import StringProperty, ObjectProperty, ListProperty, \
NumericProperty, BooleanProperty
from kivy.uix.boxlayout import BoxLayout
Expand All @@ -18,6 +19,8 @@
from donkeycar.utils import get_model_by_type
from donkeycar.parts.keras_2 import KerasSquarePlusImu, KerasSquarePlusMemoryLap

logger = logging.getLogger(__name__)


ALL_FILTERS = ['*.h5', '*.tflite', '*.savedmodel', '*.trt']

Expand All @@ -43,12 +46,12 @@ def remove_pilot_from_db(entry):
# if successfully loaded, add to rc file
if entry not in rc_handler.data['pilots']:
rc_handler.data['pilots'].append(entry)
Logger.info(f'Pilot: Successfully loaded {self.file_path}')
logger.info(f'Pilot: Successfully loaded {self.file_path}')
except FileNotFoundError:
Logger.error(f'Pilot: Model {self.file_path} not found')
logger.error(f'Pilot: Model {self.file_path} not found')
remove_pilot_from_db(entry)
except Exception as e:
Logger.error(f'Failed loading {self.file_path}: {e}')
logger.error(f'Failed loading {self.file_path}: {e}')
remove_pilot_from_db(entry)

def on_model_type(self, obj, model_type):
Expand Down Expand Up @@ -131,7 +134,7 @@ def get_image(self, record):
# Not each model is supported in each interpreter
output = self.pilot_loader.pilot.run(*args)
except Exception as e:
Logger.error(e)
logger.error(e)

rgb = (0, 0, 255)
MakeMovie.draw_line_into_image(output[0], output[1], True, img_arr, rgb)
Expand Down Expand Up @@ -196,7 +199,7 @@ def open_popup(self):
popup.open()

def on_selected(self, obj, select):
Logger.info(f"Selected {select}")
logger.info(f"Selected {select}")
if self.is_post:
self.pilot_screen.post_trans_list = self.selected
else:
Expand Down Expand Up @@ -292,7 +295,7 @@ def on_config(self, obj, cfg):
for c in self.ids.pilot_board.children:
c.ids.pilot_loader.root_path = self.config.MODELS_PATH
except Exception as e:
Logger.error(f'Error at config update in train screen: {e}')
logger.error(f'Error at config update in train screen: {e}')

def initialise(self, e):
# self.ids.pilot_board.add_viewer()
Expand Down
17 changes: 11 additions & 6 deletions donkeycar/management/ui/train_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import json

import pandas as pd
from kivy import Logger
import logging

from kivy.clock import Clock
from kivy.properties import ObjectProperty, NumericProperty, ListProperty, \
StringProperty
Expand All @@ -13,9 +14,11 @@
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy_garden.matplotlib import FigureCanvasKivyAgg
logging.getLogger('matplotlib').setLevel(logging.ERROR)
import matplotlib as mpl
import matplotlib.pyplot as plt


from donkeycar.config import Config
from donkeycar.management.ui.common import FileChooserBase, get_app_screen, \
AppScreen, status
Expand All @@ -24,6 +27,8 @@
from donkeycar.pipeline.training import train


logger = logging.getLogger(__name__)

mpl.rcParams.update({'font.size': 8})
plt.style.use('dark_background')
fig1, ax1 = plt.subplots()
Expand Down Expand Up @@ -144,7 +149,7 @@ def _config_to_dict(self):
s = s.replace("None", "null")
cfg_list = json.loads(s)
except Exception as e:
Logger.error(f'Failed json read of config: {e}')
logger.error(f'Failed json read of config: {e}')
assert isinstance(cfg_list, list), "De-jsonised config should be list"
return dict(cfg_list)

Expand Down Expand Up @@ -177,7 +182,7 @@ def on_df(self, e=None, z=None):
# arrange subplots
non_val_cols = [c for c in loss_df.columns if c[:4] != 'val_']
if len(non_val_cols) != n / 2:
Logger.Error(f"Issue with history data, validation data history "
logger.Error(f"Issue with history data, validation data history "
f"is not half of the loss data")
return
subplots = [(nv, f'val_{nv}') for nv in non_val_cols]
Expand Down Expand Up @@ -217,7 +222,7 @@ def train_call(self, *args):
transfer=transfer_model,
comment=self.ids.comment.text)
except Exception as e:
Logger.error(e)
logger.error(e)
status(f'Training failed see console')

def train(self):
Expand Down Expand Up @@ -298,7 +303,7 @@ def show_config(self, obj=None):
pilot = self.ids.select_spinner.text
cfg = self.database.get_entry(pilot).get('Config')
if not cfg:
Logger.Error(f'Config for pilot {pilot} not found in database')
logger.Error(f'Config for pilot {pilot} not found in database')
return
popup = ConfigViewerPopup(config=cfg, title=f'Config for {pilot}')
popup.fill_grid()
Expand All @@ -308,7 +313,7 @@ def show_history(self, obj=None):
pilot = self.ids.select_spinner.text
history = self.database.get_entry(pilot).get('History')
if not history:
Logger.Error(f'History for pilot {pilot} not found in database')
logger.Error(f'History for pilot {pilot} not found in database')
return
df = pd.DataFrame(history)
popup = HistoryViewerPopup(df=df, title=f'Training history for {pilot}')
Expand Down
14 changes: 8 additions & 6 deletions donkeycar/management/ui/tub_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
import plotly.express as px
import pandas as pd


from kivy import Logger
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import NumericProperty, ObjectProperty, StringProperty, \
ListProperty, BooleanProperty
from kivy_garden.matplotlib.backend_kivyagg import FigureCanvasKivyAgg

import logging
logging.getLogger('matplotlib').setLevel(logging.ERROR)
import matplotlib as mpl
import matplotlib.pyplot as plt


from donkeycar import load_config
from donkeycar.management.ui.common import FileChooserBase, \
PaddedBoxLayout, decompose, get_app_screen, BackgroundBoxLayout, AppScreen, \
Expand All @@ -23,6 +23,8 @@
from donkeycar.pipeline.types import TubRecord


logger = logging.getLogger(__name__)

mpl.rcParams.update({'font.size': 8})
plt.style.use('dark_background')
fig, ax = plt.subplots()
Expand All @@ -47,11 +49,11 @@ def load_action(self):
# If load successful, store into app config
rc_handler.data['car_dir'] = self.file_path
except FileNotFoundError:
Logger.error(f'Config: Directory {self.file_path} has no '
logger.error(f'Config: Directory {self.file_path} has no '
f'config.py')
self.config = None
except Exception as e:
Logger.error(f'Config: {e}')
logger.error(f'Config: {e}')
self.config = None

def on_config(self, obj, cfg):
Expand Down Expand Up @@ -118,7 +120,7 @@ def select(underlying):
res = train_filter(record)
return res
except KeyError as err:
Logger.error(f'Filter: {err}')
logger.error(f'Filter: {err}')
return True

self.records = [TubRecord(cfg, self.tub.base_path, record)
Expand Down
10 changes: 6 additions & 4 deletions donkeycar/management/ui/ui.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
from kivy.logger import Logger, LOG_LEVELS
import logging

#from kivy.logger import Logger, LOG_LEVELS
from kivy.clock import Clock
from kivy.app import App
from kivy.properties import StringProperty
Expand All @@ -14,8 +16,8 @@
from donkeycar.management.ui.tub_screen import TubScreen
from donkeycar.management.ui.common import AppScreen

Logger.setLevel(LOG_LEVELS["info"])

# Logger.setLevel(LOG_LEVELS["info"])
logger = logging.getLogger(__name__)
Window.size = (800, 800)


Expand Down Expand Up @@ -55,7 +57,7 @@ def on_stop(self, *args):
tub = self.root.ids.tub_screen.ids.tub_loader.tub
if tub:
tub.close()
Logger.info("App: Good bye Donkey")
logger.info("App: Good bye Donkey")


def main():
Expand Down
3 changes: 2 additions & 1 deletion donkeycar/parts/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from abc import ABC, abstractmethod
import logging
import numpy as np
from typing import Union, Sequence, List
from typing import Union, Sequence

logging.getLogger('tensorflow').setLevel(logging.WARNING)
import tensorflow as tf
from tensorflow import keras
from tensorflow.python.saved_model import tag_constants, signature_constants
Expand Down
5 changes: 3 additions & 2 deletions donkeycar/parts/keras.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import numpy as np
from typing import Dict, Tuple, Optional, Union, List, Sequence, Callable, Any
from logging import getLogger
import logging

from tensorflow.python.data.ops.dataset_ops import DatasetV1, DatasetV2

Expand All @@ -24,6 +24,7 @@
from donkeycar.pipeline.types import TubRecord
from donkeycar.parts.interpreter import Interpreter, KerasInterpreter

logging.getLogger('tensorflow').setLevel(logging.WARNING)
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import (Dense, Input,Convolution2D,
Expand All @@ -42,7 +43,7 @@
XY = Union[float, np.ndarray, Tuple[Union[float, np.ndarray], ...]]


logger = getLogger(__name__)
logger = logging.getLogger(__name__)


class KerasPilot(ABC):
Expand Down
1 change: 1 addition & 0 deletions donkeycar/parts/keras_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import numpy as np
from typing import Dict, Tuple, Union, List, Callable
logging.getLogger('tensorflow').setLevel(logging.WARNING)
import tensorflow as tf

from tensorflow import keras
Expand Down
Loading

0 comments on commit 0f850e2

Please sign in to comment.