CNTK icon indicating copy to clipboard operation
CNTK copied to clipboard

Binary image classification

Open Arminea opened this issue 9 years ago • 12 comments

Hi, I would like to know if I have a right direction in my work.

I trained a model for image binary classification. Class 1 for target image and class 0 for the rest. Class 1 has 2k images for training. I check all images myself manually. Class 0 had 25k random images for training, downloaded from Flickr. The network achieved 3.907% classification error with 810030 total samples seen during training. For test phase I used 200 images, 100 target and 100 non-target random images (I's probably not enough). The network achieved 15.942% classification error. I made a web app where user can upload image and get back the classification result (via python script). I'm trying upload images but the result is always non-target even if I upload a target image.

Did I trained the model properly (with the bunch of random flickr images)? Or maybe could the problem be in image resizing? I resize image before processing with PIL resize function, not with the cntk python api.

Arminea avatar Mar 11 '17 20:03 Arminea

I think you have class imbalance during training with more Class 0 than Class 1. You should balance your data set. Or Augment class 1. Also, I am assuming the input images that the images trained on the same size as the ones that you are passing in the app.

sayanpa avatar Mar 11 '17 22:03 sayanpa

Hi, thanks for answering. I balanced data sets as you suggested. The network achieved 19% classification error in test phase. Is there a way to log classification results for each image? I would like to know which image was classified correctly. When I run python test script for classification I get weird output. For every image (target and non-target) I get predictions like

[ 0.96888059  0.03111935]
[ 0.99268299  0.00731696]
[ 0.99414659  0.00585334]
[ 0.99291366  0.00708631]

and then the class of image is always 0. My code is:

def prediction(pathToImage):
	size = 224, 224
	im = Image.open(pathToImage)
	im = remove_transparency(im)
	im = im.resize(size) # PIL.Image.NEAREST resampling

	rgb_image = np.asarray(im, dtype=np.float32) - 128
	bgr_image = rgb_image[..., [2, 1, 0]]
	pic = np.ascontiguousarray(np.rollaxis(bgr_image, 2))

	z = load_model("model.dnn")
	z_out = ct.combine([z.outputs[2].owner])
	predictions = np.squeeze(z_out.eval({z_out.arguments[0]:[pic]}))
	top_class = np.argmax(predictions)

	print(predictions)
	print(top_class)

Arminea avatar Mar 13 '17 13:03 Arminea

Can you share also the training code?

ebarsoumMS avatar Mar 13 '17 17:03 ebarsoumMS

Sure thing :) https://1drv.ms/f/s!As_qGuBAm_dbiNldCItxglz_ELfz1A

Arminea avatar Mar 13 '17 17:03 Arminea

May be I am missing it, I don't see in yours code that you substract the mean during training? So why are you substracting 128 during evaluation?

ebarsoumMS avatar Mar 13 '17 17:03 ebarsoumMS

Well, that could happen during my ctr+c and ctrl+v :D Do you have any suggestion what should I do better? Please :)

Arminea avatar Mar 13 '17 19:03 Arminea

The data fed to your evaluator need to match what was fed to your model during training. So if you trainer doesn't normalize the input, then you need to change "rgb_image = np.asarray(im, dtype=np.float32) - 128 " to "rgb_image = np.asarray(im, dtype=np.float32)".

ebarsoumMS avatar Mar 13 '17 20:03 ebarsoumMS

That actually makes sense. Thank you for your time :) And if I would want to normalize the input in reader I would just add the minibatchSize=128 line to train block?

Arminea avatar Mar 13 '17 20:03 Arminea

minibatchSize has nothing to do with input normalization. Here an example:

https://github.com/Microsoft/CNTK/blob/master/Examples/Image/Classification/ResNet/BrainScript/ResNet20_CIFAR10.cntk

Look at those 2 lines: featScale = 1/256 Normalize{f} = x => f .* x

ebarsoumMS avatar Mar 13 '17 21:03 ebarsoumMS

Thanks, I'll look into it :)

Arminea avatar Mar 13 '17 21:03 Arminea

If you want to build your own image classifier I'd recommend to also try transfer learning as described here: https://github.com/Microsoft/CNTK/wiki/Build-your-own-image-classifier-using-Transfer-Learning.

pkranen avatar Mar 16 '17 08:03 pkranen

@pkranen Thanks, I'll definitely try it :)

Arminea avatar Mar 16 '17 09:03 Arminea