rich icon indicating copy to clipboard operation
rich copied to clipboard

[BUG] rich.prograss.track() Prograss Bar without color

Open PinkR1ver opened this issue 2 years ago • 16 comments

Describe the bug I want use rich colorful terminal progress bar to monitor training processing by PyTorch module. You can see the code here

  for i, (image, mask) in track(enumerate(train_loader), description=f"train_{epoch}", total=len(train_loader)):
      image, mask = image.to(device), mask.to(device)

      predict_image = net(image)
      train_loss = loss_function(predict_image, mask)
      loss_list = np.append(loss_list, train_loss.item())

      opt.zero_grad()
      train_loss.backward()
      opt.step()

      train_preds = torch.cat((train_preds, predict_image.cpu().detach()), 0)
      train_truths = torch.cat((train_truths, mask.cpu().detach()), 0)


      _image = image[0]
      _mask = mask[0]
      _pred_image = predict_image[0]

      _pred_image[_pred_image >= threshold] = 1
      _pred_image[_pred_image < threshold] = 0

      vaisual_image = torch.stack([_image, _mask, _pred_image], dim=0)
      torchvision.utils.save_image(vaisual_image, os.path.join(monitor_path, 'train', f'{i}.png'))

But the bar is without color and in a high flicker.

Platform

Win and Powershell

│ │ │ color_system = 'truecolor' │ │ encoding = 'utf-8' │ │ file = <_io.TextIOWrapper name='' mode='w' encoding='utf-8'> │ │ height = 30 │ │ is_alt_screen = False │ │ is_dumb_terminal = False │ │ is_interactive = True │ │ is_jupyter = False │ │ is_terminal = True │ │ legacy_windows = False │ │ no_color = False │ │ options = ConsoleOptions( │ │ size=ConsoleDimensions(width=120, height=30), │ │ legacy_windows=False, │ │ min_width=1, │ │ max_width=120, │ │ is_terminal=True, │ │ encoding='utf-8', │ │ max_height=30, │ │ justify=None, │ │ overflow=None, │ │ no_wrap=False, │ │ highlight=None, │ │ markup=None, │ │ height=None │ │ ) │ │ quiet = False │ │ record = False │ │ safe_box = True │ │ size = ConsoleDimensions(width=120, height=30) │ │ soft_wrap = False │ │ stderr = False │ │ style = None │ │ tab_size = 8 │ │ width = 120

PinkR1ver avatar Apr 19 '22 18:04 PinkR1ver

You’re using “tqdm”, not Rich. 🤷‍♂️

willmcgugan avatar Apr 19 '22 18:04 willmcgugan

Omg, 😂, my wrong. I paste the wrong code. I will correct it. The question is still here.

PinkR1ver avatar Apr 19 '22 19:04 PinkR1ver

There is the video to show this question: https://youtu.be/f5GONlErKJ8 Thanks for replying and sorry for the last wrong version of the code. 😅 I use tqdm first, but I like rich's terminal progress bar style and color. Really enjoy your work of rich. Thanks again and respect!

PinkR1ver avatar Apr 19 '22 19:04 PinkR1ver

Could you please upgrade Windows Terminal if you're not on the latest.

@darrenburns Could you check progress bars on Windows?

willmcgugan avatar Apr 20 '22 07:04 willmcgugan

@willmcgugan Yeah, that's right. The Linux platform to run the same code will not have the question. But I train in Linux with CPU and in windows with GPU, I don't know if it will have a relation to the question or it is just the platform question.

In windows, I run other simple code demos with the rich.progress.track() function are all ok. It only fails in this PyTorch training processing.

PinkR1ver avatar Apr 20 '22 13:04 PinkR1ver

Same problem here, I checked out with rich.progress.track() and in another example, I used the "Live" progress bar like your example in here, It works perfectly. BUT when I combine it with Pytorch, It goes wrong.

Here is some screenshot: Good (This should look like): image

Bad (What I got): image

I Couldn't figure out why, Please help.

Msameim181 avatar May 20 '22 20:05 Msameim181

Hi there! 🙂

So, I gave a try to the "Powershell + PyTorch + Rich" combo on Windows myself, but... I'm afraid I cannot reproduce the bug? Everything looks to be working smoothly - i.e. the progress bar is well and truly coloured, and it's not flickering. N.B. For some reason we can see it flickering on this video capture, but in reality it was not flickering - maybe an artefact caused by the screen recorder? 🤔

https://user-images.githubusercontent.com/722388/170707020-40a9f61a-8af4-4c36-8d88-fb96252fd8cb.mp4

It's difficult to understandwhat's going wrong on your own attempts as I don't have the full working code you folks have used... So what we could try is to do it the other way around: here are some commands, as well as the self-contained Python script I've used to do my own test ; you may want to reproduce what I've done and check if you see any issues with the progress bar? 🙂

The commands:

PS > python -V
Python 3.10.4
PS > python -m venv .venv
PS > .\.venv\Scripts\activate 
(.venv) PS > pip install rich torch torchvision
(.venv) PS > python.exe .\test_script.py

And here is the content of test_script.py: (DISCLAIMER: I know absolutely nothing about PyTorch 😅 , so I just copy-pasted code from their tutorial. But it seems to do something, and work hard to produce a result... Which I guess should meet the conditions to reproduce the issue that you folks have been experiencing?)

# test_script.py

import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision.transforms import ToTensor

from rich.progress import track

# Download training data from open datasets.
training_data = datasets.FashionMNIST(
    root="data",
    train=True,
    download=True,
    transform=ToTensor(),
)

# Download test data from open datasets.
test_data = datasets.FashionMNIST(
    root="data",
    train=False,
    download=True,
    transform=ToTensor(),
)

batch_size = 64

# Create data loaders.
train_dataloader = DataLoader(training_data, batch_size=batch_size)
test_dataloader = DataLoader(test_data, batch_size=batch_size)

for X, y in test_dataloader:
    print(f"Shape of X [N, C, H, W]: {X.shape}")
    print(f"Shape of y: {y.shape} {y.dtype}")
    break

# Get cpu or gpu device for training.
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using {device} device")

# Define model
class NeuralNetwork(nn.Module):
    def __init__(self):
        super(NeuralNetwork, self).__init__()
        self.flatten = nn.Flatten()
        self.linear_relu_stack = nn.Sequential(
            nn.Linear(28 * 28, 512),
            nn.ReLU(),
            nn.Linear(512, 512),
            nn.ReLU(),
            nn.Linear(512, 10),
        )

    def forward(self, x):
        x = self.flatten(x)
        logits = self.linear_relu_stack(x)
        return logits


model = NeuralNetwork().to(device)
print(model)

loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=1e-3)


def train(dataloader, model, loss_fn, optimizer):
    size = len(dataloader)
    # size = len(dataloader.dataset)
    model.train()
    for batch, (X, y) in track(
        enumerate(dataloader), description="Processing...", total=size
    ):
        # for batch, (X, y) in enumerate(dataloader):
        X, y = X.to(device), y.to(device)

        # Compute prediction error
        pred = model(X)
        loss = loss_fn(pred, y)

        # Backpropagation
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()


def test(dataloader, model, loss_fn):
    size = len(dataloader.dataset)
    num_batches = len(dataloader)
    model.eval()
    test_loss, correct = 0, 0
    with torch.no_grad():
        for X, y in dataloader:
            X, y = X.to(device), y.to(device)
            pred = model(X)
            test_loss += loss_fn(pred, y).item()
            correct += (pred.argmax(1) == y).type(torch.float).sum().item()
    test_loss /= num_batches
    correct /= size
    print(
        f"Test Error: \n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \n"
    )


epochs = 5
for t in range(epochs):
    print(f"Epoch {t+1}\n-------------------------------")
    train(train_dataloader, model, loss_fn, optimizer)
    test(test_dataloader, model, loss_fn)
print("Done!")

However, I should also stress that in my own test PyTorch is using the CPU, rather than the GPU - which could potentially explain why it's working on my own setup? But as my computer is not a CUDA-capable system, unfortunately I cannot reproduce any issues that would be caused by the use of CUDA 😔

olivierphi avatar May 27 '22 13:05 olivierphi

I've tried your code in cmd and it's working ideally I think. I got some videos of working on my side.

Your Code on cmd:

https://user-images.githubusercontent.com/25700939/170732425-699c9cc3-4ac9-49fb-9bdf-e9a506f0c697.mp4

Mine on cmd (Without any PyTorch, Just Template) :

Here are the code that I used: RichTrainTemplate

Every Rich progress bar on cmd is the same, but colors ...

https://user-images.githubusercontent.com/25700939/170732702-b006ce77-dcb8-4541-b5c0-1ba80ed0ce65.mp4

All samples in VsCode insider PowerShell:

https://user-images.githubusercontent.com/25700939/170732940-7a85d54a-1334-49c2-9783-0387742273ff.mp4

Msameim181 avatar May 27 '22 15:05 Msameim181

If you still need a sample with PyTorch, let me know.

Msameim181 avatar May 27 '22 19:05 Msameim181

I have a similar problem. no pytorch involved with my code. the whole color scheme seems to be influenced, yet only on one of my computers. I'm using track for the progress bars.

wrong (windows terminal 1.12.10982.0 and 1.13.11431.0): immagine

correct : immagine

peppedilillo avatar Jun 07 '22 12:06 peppedilillo

I was able to run on WSL (Ubuntu 20.04), It seems okay and works fine on it.

So it might be the problem on Windows (when you use it with some libraries like PyTorch, W&B or etc).

Any idea why it reacts like this on windows? (This issue is similar on W&B). Both work fine on WSL (Linux) but not on Windows.

sample

Msameim181 avatar Jun 11 '22 15:06 Msameim181

Can anyone reproduce the issue running the following from the command line:

python -m rich.progress

If you can, please post your OS, terminal (+version) and the result of the following:

python -m rich.diagnose

willmcgugan avatar Jun 12 '22 10:06 willmcgugan

First Command:

https://user-images.githubusercontent.com/25700939/173234325-a9d2e53c-1005-4bb9-a0f8-1eefe5fe295e.mp4


Second Command:

https://user-images.githubusercontent.com/25700939/173234334-11fc0a6b-42ff-4e37-97c9-ce85379e698a.mp4

But

The point is, that Rich doing just fine alone, but when you use other Libs like PyTorch or W&B, the problem shows up (I believe only on windows).

Msameim181 avatar Jun 12 '22 13:06 Msameim181

The hyphens and flickering in progress bars are to be expected on legacy windows console, which has poor support for these things.

The only issue above is the pure white progress bar. In order to diagnose that we will need:

  • What version of Windows are you on?
  • What is the code you are running (a small reproducible example would be best)
  • a copy of your environment variables as they are in the running code import os;print(os.environ)

willmcgugan avatar Jun 28 '22 08:06 willmcgugan

Deinitializing colorama might help solve this problem.

https://github.com/Textualize/rich/issues/1201#issuecomment-829959040

----update---- Torch imports tqdm if the latter is installed, and tqdm initializes colorama. You could also try uninstalling tqdm.

tiruns avatar Jul 23 '22 07:07 tiruns

I think this comment solves this issue. The result is good on VSCode (or -insider). But on the windows console, as the same as before, and NO improvement has been achieved.

Msameim181 avatar Jul 24 '22 07:07 Msameim181

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual

github-actions[bot] avatar Dec 15 '22 08:12 github-actions[bot]