fastai / fastprogress

Simple and flexible progress bar for Jupyter Notebook and console
Apache License 2.0
1.09k stars 106 forks source link

UnicodeEncodeError ascii' codec can't encode characters #21

Closed mattmcclean closed 5 years ago

mattmcclean commented 5 years ago

Am trying to run a training job using fastai in a Docker container (not Jupyter notebook) and getting a UnicodeEncodeError in the fastprogress library. Am running fastprogress version 0.1.15, fastai version 1.0.28 with Python v3.6.

Full stack trace includes the following:

algo-1-3QNM9_1_51756cf5708d | Called _train method with model arch: resnet34, batch size: 64, image size: 224, epochs: 4
algo-1-3QNM9_1_51756cf5708d | Getting training data from dir: /opt/ml/input/data/training
algo-1-3QNM9_1_51756cf5708d | Model architecture is resnet34
algo-1-3QNM9_1_51756cf5708d | Creating pretrained conv net
algo-1-3QNM9_1_51756cf5708d | Downloading: "https://download.pytorch.org/models/resnet34-333f7ec4.pth" to /root/.torch/models/resnet34-333f7ec4.pth
100% 87306240/87306240 [00:01<00:00, 70374700.25it/s]
algo-1-3QNM9_1_51756cf5708d | Fit four cycles
algo-1-3QNM9_1_51756cf5708d | epoch  train_loss  valid_loss  accuracy
/usr/local/lib/python3.6/dist-packages/PIL/Image.py:953: UserWarning: Palette images with Transparency   expressed in bytes should be converted to RGBA images
algo-1-3QNM9_1_51756cf5708d |   'to RGBA images')
Traceback (most recent call last):                                                              
algo-1-3QNM9_1_51756cf5708d |   File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
algo-1-3QNM9_1_51756cf5708d |     "__main__", mod_spec)
algo-1-3QNM9_1_51756cf5708d |   File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
algo-1-3QNM9_1_51756cf5708d |     exec(code, run_globals)
algo-1-3QNM9_1_51756cf5708d |   File "/opt/ml/code/shirts.py", line 152, in <module>
algo-1-3QNM9_1_51756cf5708d |     _train(parser.parse_args())
algo-1-3QNM9_1_51756cf5708d |   File "/opt/ml/code/shirts.py", line 66, in _train
algo-1-3QNM9_1_51756cf5708d |     learn.fit_one_cycle(4)
algo-1-3QNM9_1_51756cf5708d |   File "/usr/local/lib/python3.6/dist-packages/fastai/train.py", line 20, in fit_one_cycle
algo-1-3QNM9_1_51756cf5708d |     learn.fit(cyc_len, max_lr, wd=wd, callbacks=callbacks)
algo-1-3QNM9_1_51756cf5708d |   File "/usr/local/lib/python3.6/dist-packages/fastai/basic_train.py", line 162, in fit
algo-1-3QNM9_1_51756cf5708d |     callbacks=self.callbacks+callbacks)
algo-1-3QNM9_1_51756cf5708d |   File "/usr/local/lib/python3.6/dist-packages/fastai/basic_train.py", line 94, in fit
algo-1-3QNM9_1_51756cf5708d |     raise e
algo-1-3QNM9_1_51756cf5708d |   File "/usr/local/lib/python3.6/dist-packages/fastai/basic_train.py", line 82, in fit
algo-1-3QNM9_1_51756cf5708d |     for xb,yb in progress_bar(data.train_dl, parent=pbar):
algo-1-3QNM9_1_51756cf5708d |   File "/usr/local/lib/python3.6/dist-packages/fastprogress/fastprogress.py", line 67, in __iter__
algo-1-3QNM9_1_51756cf5708d |     if self.auto_update: self.update(i+1)
algo-1-3QNM9_1_51756cf5708d |   File "/usr/local/lib/python3.6/dist-packages/fastprogress/fastprogress.py", line 85, in update
algo-1-3QNM9_1_51756cf5708d |     self.update_bar(val)
algo-1-3QNM9_1_51756cf5708d |   File "/usr/local/lib/python3.6/dist-packages/fastprogress/fastprogress.py", line 97, in update_bar
algo-1-3QNM9_1_51756cf5708d |     else: self.on_update(val, f'{100 * val/self.total:.2f}% [{val}/{self.total} {elapsed_t}<{remaining_t}{end}]')
algo-1-3QNM9_1_51756cf5708d |   File "/usr/local/lib/python3.6/dist-packages/fastprogress/fastprogress.py", line 262, in on_update
algo-1-3QNM9_1_51756cf5708d |     if printing(): WRITER_FN(to_write, end = '\r')
algo-1-3QNM9_1_51756cf5708d | UnicodeEncodeError: 'ascii' codec can't encode characters in position 14-15: ordinal not in range(128)
algo-1-3QNM9_1_51756cf5708d | 2018-11-20 08:10:39,646 sagemaker-containers ERROR    ExecuteUserScriptError:
algo-1-3QNM9_1_51756cf5708d | Command "/usr/local/bin/python -m shirts --batch-size 64 --epochs 4"
sgugger commented 5 years ago

I'm guessing this line is to blame. Could you tell me if changing the value of the fill character to some basic char solves the issue?

mattmcclean commented 5 years ago

Bit tricky as this is running in a docker container and hard to modify thefastprogress.py file. Perhaps encoding it in UTF-8 would work

e.g. replace this line with the following:

bar = self.fill.encode('utf-8') * filled_len + '-' * (self.length - filled_len)
mattmcclean commented 5 years ago

Seems to only happen with Python 3.6. Works fine with Python 3.7

mattmcclean commented 5 years ago

This has been fixed as I set the locale inside the Docker container to the following:

ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8

Now working fine

daMichaelB commented 5 years ago

This has been fixed as I set the locale inside the Docker container to the following:

ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8

Now working fine

I had the same issue with Python3.6 and putting those ENVs to my container fixed the problem. Thanks!

xthursdayx commented 5 years ago

Was having the same issue in a different docker container I created. Thanks for this fix @mattmcclean !