Skip to content

Throw exception in ImageClassificationTrainer when dataset contains only 1 class #4662

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

Merged
merged 4 commits into from
Jan 25, 2020

Conversation

antoniovs1029
Copy link
Member

Throw exception in ImageClassificationTrainer when dataset contains only 1 class

Fixes #4660

@antoniovs1029 antoniovs1029 requested a review from a team as a code owner January 15, 2020 23:58
@codecov
Copy link

codecov bot commented Jan 16, 2020

Codecov Report

❗ No coverage uploaded for pull request base (master@b045e4d). Click here to learn what that means.
The diff coverage is 100%.

@@            Coverage Diff            @@
##             master    #4662   +/-   ##
=========================================
  Coverage          ?   75.83%           
=========================================
  Files             ?      951           
  Lines             ?   172306           
  Branches          ?    18588           
=========================================
  Hits              ?   130664           
  Misses            ?    36466           
  Partials          ?     5176
Flag Coverage Δ
#Debug 75.83% <100%> (?)
#production 71.43% <100%> (?)
#test 90.6% <ø> (?)
Impacted Files Coverage Δ
.../Microsoft.ML.Vision/ImageClassificationTrainer.cs 88.98% <100%> (ø)

Copy link
Contributor

@harishsk harishsk left a comment

Choose a reason for hiding this comment

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

🕐

Copy link
Contributor

@harishsk harishsk left a comment

Choose a reason for hiding this comment

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

:shipit:

@antoniovs1029 antoniovs1029 requested a review from ganik January 17, 2020 21:17
@harishsk harishsk requested a review from natke January 17, 2020 21:34
Copy link
Member

@codemzs codemzs left a comment

Choose a reason for hiding this comment

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

:shipit:

Copy link
Contributor

@natke natke left a comment

Choose a reason for hiding this comment

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

See my reply to the discussion on message clarity and consistency

@@ -603,7 +603,10 @@ private void InitializeTrainingGraph(IDataView input)
(string)labelType.ToString());
}

_classCount = labelCount == 1 ? 2 : (int)labelCount;
var msg = $"Only one class found in {_options.LabelColumnName} column. To build a multiclass classification model, the number of classes needs to be 2 or greater";
Contracts.CheckParam(labelCount > 1, nameof(labelCount), msg);
Copy link
Member Author

Choose a reason for hiding this comment

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

I've implemented the message that @natke suggested.

I had to use the "var msg" to store the string, because I couldn't use string interpolation inside the CheckParam() argument, because of this:

/// <summary>
/// Looks up a localized string similar to Since C# has no concept of lazy evaluation of parameters, we prefer Contracts.Check&apos;s message arguments to not involve string formatting, or other complex operations, since such operations will happen always, whether the check fails or not. If you want to have detailed messages that&apos;s great, but use Contracts.Except instead. That is instead of something like &apos;Check(c, msg)&apos;, prefer &apos;if (!c) throw Except(msg)&apos;..
/// </summary>
internal static string ContractsCheckMessageNotLiteralOrIdentifier {
get {
return ResourceManager.GetString("ContractsCheckMessageNotLiteralOrIdentifier", resourceCulture);
}
}

So the suggestion there is to use Contracts.Except() instead of Contracts.Param() if I want to use string interpolation. Problem is Contracts.Except() throws an InvalidOperationException (which @justinormont and @harishsk had suggested I shouldn't use). So I guess it's best to simply create the "msg" string before passing it to the Contracts.CheckParam()...

@antoniovs1029 antoniovs1029 merged commit 0618515 into dotnet:master Jan 25, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Mar 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using an ImageClassificationTrainer with a dataset that only contains one class of images
6 participants