Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Fix wrong condition judgment in analyze_logs.py and prevent empty curve. #510

Merged
merged 3 commits into from
Nov 3, 2021
Merged

[Fix] Fix wrong condition judgment in analyze_logs.py and prevent empty curve. #510

merged 3 commits into from
Nov 3, 2021

Conversation

imyhxy
Copy link
Contributor

@imyhxy imyhxy commented Nov 2, 2021

Motivation

Make analyze_log.py works with single validation iteration. Currently, the analyza_logs.py use iters = iters[:-1] to get rid of the last validation iteration, but in a very small val set, there maybe only one validation step. In such case, the analyze_log.py won't work, it use print out a blank figure. This PR fixed that.

Modification

When there is only one item in the iters list, then don't slice it.

Checklist

Before PR:

  • Pre-commit or other linting tools are used to fix the potential lint issues.
  • Bug fixes are fully covered by unit tests, the case that causes the bug should be added in the unit tests.
  • The modification is covered by complete unit tests. If not, please add more unit test to ensure the correctness.
  • The documentation has been modified accordingly, like docstring or example tutorials.

After PR:

  • If the modification has potential influence on downstream or other related projects, this PR should be tested with those projects, like MMDet or MMSeg.
  • CLA has been signed and all committers have signed the CLA in this PR.

@imyhxy
Copy link
Contributor Author

imyhxy commented Nov 2, 2021

Test with this log file.

python tools/analysis_tools/analyze_logs.py plot_curve 20211028_115735.log.json --keys accuracy_top-1

20211028_115735.log

@codecov
Copy link

codecov bot commented Nov 2, 2021

Codecov Report

Merging #510 (1bacb05) into master (72cffac) will increase coverage by 0.05%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #510      +/-   ##
==========================================
+ Coverage   79.42%   79.48%   +0.05%     
==========================================
  Files         106      106              
  Lines        5959     5975      +16     
  Branches      962      968       +6     
==========================================
+ Hits         4733     4749      +16     
  Misses       1095     1095              
  Partials      131      131              
Flag Coverage Δ
unittests 79.48% <ø> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
mmcls/utils/logger.py 100.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 72cffac...1bacb05. Read the comment docs.

@mzr1996
Copy link
Member

mzr1996 commented Nov 2, 2021

Test with this log file.

python tools/analysis_tools/analyze_logs.py plot_curve 20211028_115735.log.json --keys accuracy_top-1

20211028_115735.log

Thanks for your contribution.
Here, I think you want to put the log json file, but put a log file by mistake.

@imyhxy
Copy link
Contributor Author

imyhxy commented Nov 2, 2021

20211028_115735.log.json.txt
@mzr1996 Please remove the .txt suffix because github not supported raw json.

@mzr1996
Copy link
Member

mzr1996 commented Nov 2, 2021

I have read your modification.
First, let me confirm your need. You want to analyze the log and plot the accuracy curve, right?
Let me show the code:

            if 'mAP' in metric:    # <-- Here, it's the condition to find the metric in val steps
                xs = np.arange(1, max(epochs) + 1)
                ys = []
                for epoch in epochs:
                    ys += log_dict[epoch][metric]
                ax = plt.gca()
                ax.set_xticks(xs)
                plt.xlabel('epoch')
                plt.plot(xs, ys, label=legend[i * num_metrics + j], marker='o')
            else:                  # <-- Else, it will find the metric in train steps
                xs = []
                ys = []
                num_iters_per_epoch = log_dict[epochs[0]]['iter'][-1]
                for epoch in epochs:
                    iters = log_dict[epoch]['iter']
                    if log_dict[epoch]['mode'][-1] == 'val':
                        iters = iters[:-1]
                    xs.append(
                        np.array(iters) + (epoch - 1) * num_iters_per_epoch)
                    ys.append(np.array(log_dict[epoch][metric][:len(iters)]))
                xs = np.concatenate(xs)
                ys = np.concatenate(ys)
                plt.xlabel('iter')
                plt.plot(
                    xs, ys, label=legend[i * num_metrics + j], linewidth=0.5)

The problem is not the last eval step is skipped, but we forget to add "accuracy" to the condition to find the metric in val steps.
So I think a better solution is to modify the first line to:

            if 'mAP' in metric or 'accuracy' in metric:

And the original code can only work when evaluate in every epoch. So please change the original code to:

            if 'mAP' in metric or 'accuracy' in metric:
                xs = []
                ys = []
                for epoch in epochs:
                    xs.append(epoch)
                    ys.extend(log_dict[epoch][metric])
                ax = plt.gca()
                ax.set_xticks(xs)
                plt.xlabel('epoch')
                plt.plot(xs, ys, label=legend[i * num_metrics + j], marker='o')

@imyhxy
Copy link
Contributor Author

imyhxy commented Nov 2, 2021

Yeah, you are right. I have modified the code according to your suggestion. I think the iters thing still need a sentinel value to prevent it fall into an empty list.

tools/analysis_tools/analyze_logs.py Outdated Show resolved Hide resolved
Copy link
Member

@mzr1996 mzr1996 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for your contribution 😃

@mzr1996 mzr1996 changed the title [Fix] Analyze logs with single val iteration [Fix] Fix wrong condition judgment in analyze_logs.py and prevent empty curve. Nov 3, 2021
@mzr1996 mzr1996 merged commit abb5a22 into open-mmlab:master Nov 3, 2021
@imyhxy imyhxy deleted the fix-analyze-logs branch November 3, 2021 05:06
mzr1996 added a commit to mzr1996/mmpretrain that referenced this pull request Nov 24, 2022
…mpty curve. (open-mmlab#510)

* [Fix] Analyze logs with single training iteration

* [Fix] Make `plot_curve` with 'accuracy' metric

* [Fix] Assert length of training log records of each epoch is larger than 1

Co-authored-by: Ma Zerun <[email protected]>

Co-authored-by: Ma Zerun <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants