Search code examples
pythoncntk

Not able to eval an image in CNTK python


Below is the code I am using:

import sys,os
import numpy as np
# import dicom
import glob
import cv2
import time
import cntk
import pandas as pd
from PIL import Image
from sklearn import cross_validation
from cntk import load_model
from cntk.ops import combine
from cntk.io import MinibatchSource, ImageDeserializer, StreamDef, StreamDefs
# from lightgbm.sklearn import LGBMRegressor

EXPERIMENT_NUMBER = '0042'
MODEL_PATH='C:/Projects/CNTK_data/Model/ResNet_152.model'
BATCH_SIZE=15
DATA_PATH='C:/Users/sharm_000/Desktop/Scraper/Images_Myntra/'

loaded_model=load_model(MODEL_PATH)

node_in_graph=loaded_model.find_all_with_name('z.x')

output_nodes  = combine(node_in_graph)

import os
files=os.listdir(DATA_PATH)[0]
print files
>>> 0.jpg
fileswa=cv2.imread(DATA_PATH+files)

imgawa=cv2.resize(fileswa,(224,224))
rgb_image = np.asarray(imgawa, dtype=np.float32)
imgwa=np.ascontiguousarray(np.rollaxis(rgb_image, 2))

rgb_image.shape,imgwa.shape

>>>>((224L, 224L, 3L), (3L, 224L, 224L))

output_nodes.eval(imgwa)

ValueError: Value::Create:: The shape of the sequence 0 ([224 x 224]) is not compatible with the sample shape ([3 x 224 x 224])

Please suggest What am I missing and where I am wrong here:

I have also used:

imag1=cv2.imread(DATA_PATH+files)
imag2=cv2.resize(imag1,(224,224))
imag3=imag2.T
imag3=imag3.astype(int)

print imag2.shape,imag3.shape
>>>> (224L, 224L, 3L) (3L, 224L, 224L) 
output_nodes.eval(imag3)

But same value error

Issue solved final code:

imag1=cv2.imread(DATA_PATH+files)
imag2=cv2.resize(imag1,(224,224))
imag3=imag2.T.astype(int)
imag4=np.ascontiguousarray(imag3)
print imag2.shape,imag3.shape

xxxx=output_nodes.eval({output_nodes.arguments[0]:[imag4]})

Solution

  • Can you try:

    output_nodes.eval({output_nodes.arguments[0]:[imag3]}))