microsoft / CNTK

Microsoft Cognitive Toolkit (CNTK), an open source deep-learning toolkit
https://docs.microsoft.com/cognitive-toolkit/
Other
17.5k stars 4.29k forks source link

Scoring grayscale images #1468

Open fcivardi opened 7 years ago

fcivardi commented 7 years ago

To score new images and print the probability of the class being 1 (it's a binary classifier), I've written this code:

def eval_mri(model, image_path):
    mri = Image.open(image_path)
#    mri = mri.resize((image_width, image_height), Image.BILINEAR)
    mri = mri.resize((image_width, image_height), Image.ANTIALIAS)
    image_mean   = 32768.0
    image_data   = np.array(mri, dtype=np.float32)
    image_data  -= image_mean
    image_data = image_data/65536.0
    image_data = image_data[np.newaxis,:,:]
    image_data = np.ascontiguousarray(image_data)
    result     = np.squeeze(model.eval({model.arguments[0]:[image_data]}))
    print(image_path, result[:])
    return

data_path = 'E:/Labels'
# model dimensions
image_height = 512
image_width  = 512
num_channels = 1
num_classes  = 2

z = load_model('E:/Models/detMen-T1.dnn')
y = softmax(z)

file_path = os.path.join(data_path, 'Test-t1-meniscus.csv')
with open(file_path, 'r') as csvfile:
    reader = csv.reader(csvfile, delimiter='\t')
    for row in reader:
        image_path = row[0]
        ris = eval_mri(y, image_path )

but it always returns the same classification, [1.0 1....e-17] , even for images in the test set, where the Training Error was very low. Images are grayscale png (0-65535), and I trained the model using the CNTK reader (so it expects a color axis). Am I doing something wrong?

fcivardi commented 7 years ago

It seems I have the same problem reported here: https://github.com/Microsoft/CNTK/issues/1369 but I'm training using python, how can I specify grayscale=True in the Python trainer?

fcivardi commented 7 years ago

I tried deserializer['grayscale'] = True, suggested here: https://github.com/Microsoft/CNTK/issues/1172 but nothing changed.

cha-zhang commented 7 years ago

Try:

cntk.io.MinibatchSource(cntk.io.ImageDeserializer(map_file, cntk.io.StreamDefs(
        features = cntk.io.StreamDef(field='image', transforms=transforms), 
        labels   = cntk.io.StreamDef(field='label', shape=num_classes))), 
        grayscale = True, 
        randomize=is_training)
fcivardi commented 7 years ago

With that I get this error: TypeError: init() got an unexpected keyword argument 'grayscale' I'm using beta10.

cha-zhang commented 7 years ago

That's a bug. We will fix it.

fcivardi commented 7 years ago

By the way, because my images are 16bit grayscale instead of 8bit , do you know if the issue below has been fixed in the current code: https://github.com/Microsoft/CNTK/issues/961 ? MartIgap proposed to fix it, but I don't now if it has been done. Maybe my problem is affected by both flags that should be defined in the Reader (grayscale, and CV_LOAD_IMAGE_ANYDEPTH for opencv) . Could you also tell me when the fix will be available? Thanks

patykov commented 6 years ago

Any updates on this bug fix?

nietras commented 6 years ago

No update since december 2017, what is the status of this?