Hello Rory!



 roryw10:

I tried using pytorchs BCEWithLogitsLoss out the box however i was getting an error “expecting target data type as long but BCElosswithlogistic loss function expecting as float datatype”


The error message you quote seems somewhat garbled.  However,
I think your issue is this:  BCEWithLogitsLoss expects a target of
type float, rather than a long.  So convert your target tensor from
long to float.
(You can think of integer targets as being class labels: 0 means “no,”
and 1 means “yes.”  But you can also understand them as probabilities:
0.0 mean 0% probability of being “yes” (thus, “no”), and 1.0 means
100% probability of being “yes” (thus “yes”).  Unlike something like
CrossEntropyLoss that requires its targets to be integer class labels,
BCEWithLogitsLoss does accept non-integer probabilities for its
targets.  So you can have 0.15 (probably “no”) or 0.90 (probably
“yes”).  This is a good thing, but in order to support this use case,
BCEWithLogitsLoss requires floats for its targets, even if you are
only passing it 0.0s and 1.0s.)


the only way I could see to do this is to turn each image into an array and then iterate over all the images in the folder to see how many 0s and how many 1s?


Well, yes, at some point you have to count the 0s and 1s, at least for a
representative sample of your data.
If I were doing it, I would recognize that my pytorch code is somewhere
already converting those images into target tensors that consist of 0s
and 1s.  Then target.mean() will give you the fraction of 1s in that
target tensor.
Good luck!
K. Frank