CNTK icon indicating copy to clipboard operation
CNTK copied to clipboard

How to predict my images using trained-model file??

Open edwardcho opened this issue 9 years ago • 30 comments

I am using ImageNet-sample python code.

My main code is as follows,

modelpath = 'BN-Inception_99.model'
network = create_bn_inception_by_exist_model(modelpath)
trainer = create_trainer(network, epoch_size, max_epochs, minibatch_size)
train_source = create_image_mb_source(train_data, mean_data, True, total_number_of_samples=max_epochs * epoch_size)
test_source = create_image_mb_source(test_data, mean_data, False, total_number_of_samples=FULL_DATA_SWEEP)
train_re_source = create_image_mb_source(train_re_data, mean_data, False, total_number_of_samples=FULL_DATA_SWEEP)

prediction_TRAIN_AND_TEST_existingmodel(network, trainer, train_source, test_source, train_re_source, progress_printer, max_epochs, minibatch_size, epoch_size, restore, profiler_dir, testing_parameters)

And, "create_bn_inception_by_exist_model" function is this. def create_bn_inception_by_exist_model(modelpath):

# Input variables denoting the features and label data
feature_var = input_variable((NUM_CHANNELS, IMAGE_HEIGHT, IMAGE_WIDTH))
label_var = input_variable((NUM_CLASSES))

bn_time_const = 4096
z = load_model(modelpath)

# loss and metric
ce  = cross_entropy_with_softmax(z, label_var)
pe  = classification_error(z, label_var)
pe5 = classification_error(z, label_var, topN=5)

log_number_of_parameters(z)
print()

return {
    'feature': feature_var,
    'label'  : label_var,
    'ce'     : ce,
    'pe'     : pe,
    'pe5'    : pe5,
    'output' : z
}

And, "prediction_TRAIN_AND_TEST_existingmodel" function is this. def prediction_TRAIN_AND_TEST_existingmodel(network, trainer, train_source, test_source, train_re_source, progress_printer, max_epochs, minibatch_size, epoch_size, restore, profiler_dir, testing_parameters):

# define mapping from intput streams to network inputs
input_map = {
    network['feature']: train_source.streams.features,
    network['label']: train_source.streams.labels
}
# process minibatches and evaluate the model
metric_numer    = 0
metric_denom    = 0
sample_count    = 0
minibatch_index = 0

top_k = 5    # top-5

while sample_count < epoch_size:
    current_minibatch = min(minibatch_size, epoch_size - sample_count)
    # Fetch next test min batch for train data.
    data = train_re_source.next_minibatch(current_minibatch, input_map=input_map)
    # minibatch data to be trained with
    metric_numer += trainer.test_minibatch(data) * current_minibatch      ###### <- error code

    ## added edward.cho
    out = cntk.softmax(network['output'])
    print ("out :", out)
    predicted_label_probs = out.eval(data)

BTW, I met some error(###### <- error code) in "prediction_TRAIN_AND_TEST_by_existingmodel" function.

This is error message. metric_numer += trainer.test_minibatch(data) * current_minibatch File "/home/mirero/anaconda2/lib/python2.7/site-packages/cntk/train/trainer.py", line 223, in test_minibatch return super(Trainer, self).test_minibatch(arguments, device) File "/home/mirero/anaconda2/lib/python2.7/site-packages/cntk/cntk_py.py", line 2466, in test_minibatch return _cntk_py.Evaluator_test_minibatch(self, *args) ValueError: Values for 1 required arguments 'Input('Input3', [#], [3 x 224 x 224])', that the requested output(s) 'Output('aggregateEvalMetric', [], []), Output('Block4979_Output_0', [#], [1])' depend on, have not been provided.

How should I do modify this code??

edwardcho avatar Jun 27 '17 08:06 edwardcho

Please take a look at this example: https://github.com/Microsoft/CNTK/tree/master/Examples/Image/FeatureExtraction

cha-zhang avatar Jun 29 '17 03:06 cha-zhang

Hello cha-zhang, According to your comments, I am testing my code. BTW, what is node_name ?? (ex. in sample_code, node_name is "z.x".) I inputed "output" as node_name. Right??

And, As you know in sample (BN_Inception / GoogLeNet / xxx_ImageNet.py ) the model is saved in this code. Can I use this model in your sample code ?? (FeatureExtraction.py)

... # perform model training if profiler_dir: start_profiler(profiler_dir, True)

for epoch in range(max_epochs):       # loop over epochs
    sample_count = 0
    while sample_count < epoch_size:  # loop over minibatches in the epoch
        data = train_source.next_minibatch(min(minibatch_size, epoch_size-sample_count), input_map=input_map) # fetch minibatch.
        trainer.train_minibatch(data)                                   # update model with it
        sample_count += trainer.previous_minibatch_sample_count         # count samples processed so far
        progress_printer.update_with_trainer(trainer, with_metric=True) # log progress
    progress_printer.epoch_summary(with_metric=True)
    network['output'].save(os.path.join(model_path, "BN-Inception_IRIS_IPP_{}.model".format(epoch)))
    enable_profiler() # begin to collect profiler data after first epoch

if profiler_dir:
    stop_profiler()

...

edwardcho avatar Jun 29 '17 07:06 edwardcho

Take a look at the layers.Label at the Python API for CNTK. If you have a label layer or your layer has a name, then that would be your node_name.

For example, I am using a sequence to sequence model and before decoding I have a layer called encoded_h. After I load the model, I can use the following program to look at the output.

model = C.Function.load(model_path) output_nodes = C.combine([model.encoded_h.owner]) mb = valid_reader.next_minibatch(1) output = output_nodes.eval(mb[valid_reader.streams.features])

meijiesky avatar Jun 29 '17 08:06 meijiesky

Sorry for my poor knowledge. Honesty, I can not understand your approach.. Anyway, this is my code abut creating network.

In this code, what is node_name??

Create the network.

def create_bn_inception():

# Input variables denoting the features and label data
feature_var = input_variable((NUM_CHANNELS, IMAGE_HEIGHT, IMAGE_WIDTH))
label_var = input_variable((NUM_CLASSES))

bn_time_const = 4096
z = bn_inception_model(feature_var, NUM_CLASSES, bn_time_const)

# loss and metric
ce  = cross_entropy_with_softmax(z, label_var)
pe  = classification_error(z, label_var)
pe5 = classification_error(z, label_var, topN=5)

log_number_of_parameters(z)
print()

return {
    'feature': feature_var,
    'label'  : label_var,
    'ce'     : ce,
    'pe'     : pe,
    'pe5'    : pe5,
    'output' : z
}

edwardcho avatar Jun 29 '17 08:06 edwardcho

I tested this for getting node-name in my model. loaded_model = load_model(model_file) print ( get_node_outputs(loaded_model) )

What is node name for above operation??

[Output('Block52522_Output_0', [#], [13]), Output('Block51807_Output_0', [#], [1024 x 1 x 1]), Output('Splice51787_Output_0', [#], [1024 x 7 x 7]), Output('ReLU43345_Output_0', [#], [352 x 7 x 7]), Output('Block42721_Output_0', [#], [352 x 7 x 7]), Output('Block42079_Output_0', [#], [352 x 7 x 7]), Output('Splice42058_Output_0', [#], [1024 x 7 x 7]), Output('ReLU34500_Output_0', [#], [352 x 7 x 7]), Output('Block33944_Output_0', [#], [352 x 7 x 7]), Output('Block33370_Output_0', [#], [352 x 7 x 7]), Output('Splice33349_Output_0', [#], [1056 x 7 x 7]), Output('ReLU29620_Output_0', [#], [192 x 7 x 7]), Output('Block29105_Output_0', [#], [192 x 7 x 7]), Output('Block28572_Output_0', [#], [192 x 7 x 7]), Output('ReLU28551_Output_0', [#], [128 x 14 x 14]), Output('Block28045_Output_0', [#], [128 x 14 x 14]), Output('Block27521_Output_0', [#], [128 x 14 x 14]), Output('Splice27500_Output_0', [#], [608 x 14 x 14]), Output('ReLU21476_Output_0', [#], [96 x 14 x 14]), Output('Block21038_Output_0', [#], [96 x 14 x 14]), Output('Block20582_Output_0', [#], [96 x 14 x 14]), Output('Splice20561_Output_0', [#], [608 x 14 x 14]), Output('ReLU15421_Output_0', [#], [160 x 14 x 14]), Output('Block15051_Output_0', [#], [160 x 14 x 14]), Output('Block14663_Output_0', [#], [160 x 14 x 14]), Output('Splice14642_Output_0', [#], [576 x 14 x 14]), Output('ReLU10386_Output_0', [#], [192 x 14 x 14]), Output('Block10084_Output_0', [#], [192 x 14 x 14]), Output('Block9764_Output_0', [#], [192 x 14 x 14]), Output('Splice9743_Output_0', [#], [576 x 14 x 14]), Output('ReLU6371_Output_0', [#], [224 x 14 x 14]), Output('Block6137_Output_0', [#], [224 x 14 x 14]), Output('Block5885_Output_0', [#], [224 x 14 x 14]), Output('Splice5864_Output_0', [#], [576 x 14 x 14]), Output('ReLU4389_Output_0', [#], [160 x 14 x 14]), Output('Block4196_Output_0', [#], [160 x 14 x 14]), Output('Block3985_Output_0', [#], [160 x 14 x 14]), Output('ReLU3964_Output_0', [#], [128 x 28 x 28]), Output('Block3780_Output_0', [#], [128 x 28 x 28]), Output('Block3578_Output_0', [#], [128 x 28 x 28]), Output('Splice3557_Output_0', [#], [320 x 28 x 28]), Output('ReLU1719_Output_0', [#], [64 x 28 x 28]), Output('Block1603_Output_0', [#], [64 x 28 x 28]), Output('Block1469_Output_0', [#], [64 x 28 x 28]), Output('Splice1448_Output_0', [#], [256 x 28 x 28]), Output('ReLU494_Output_0', [#], [64 x 28 x 28]), Output('Block446_Output_0', [#], [64 x 28 x 28]), Output('Block380_Output_0', [#], [64 x 28 x 28]), Output('Block322_Output_0', [#], [192 x 28 x 28]), Output('ReLU302_Output_0', [#], [192 x 56 x 56]), Output('Block267_Output_0', [#], [192 x 56 x 56]), Output('Block214_Output_0', [#], [192 x 56 x 56]), Output('ReLU193_Output_0', [#], [64 x 56 x 56]), Output('Block167_Output_0', [#], [64 x 56 x 56]), Output('Block123_Output_0', [#], [64 x 56 x 56]), Output('Block87_Output_0', [#], [64 x 56 x 56]), Output('ReLU67_Output_0', [#], [64 x 112 x 112]), Output('Block54_Output_0', [#], [64 x 112 x 112]), Output('Block23_Output_0', [#], [64 x 112 x 112]), Output('ReLU782_Output_0', [#], [64 x 28 x 28]), Output('Block725_Output_0', [#], [64 x 28 x 28]), Output('Block650_Output_0', [#], [64 x 28 x 28]), Output('ReLU629_Output_0', [#], [64 x 28 x 28]), Output('Block581_Output_0', [#], [64 x 28 x 28]), Output('Block515_Output_0', [#], [64 x 28 x 28]), Output('ReLU1241_Output_0', [#], [96 x 28 x 28]), Output('Block1175_Output_0', [#], [96 x 28 x 28]), Output('Block1091_Output_0', [#], [96 x 28 x 28]), Output('ReLU1070_Output_0', [#], [96 x 28 x 28]), Output('Block1013_Output_0', [#], [96 x 28 x 28]), Output('Block938_Output_0', [#], [96 x 28 x 28]), Output('ReLU917_Output_0', [#], [64 x 28 x 28]), Output('Block869_Output_0', [#], [64 x 28 x 28]), Output('Block803_Output_0', [#], [64 x 28 x 28]), Output('ReLU1445_Output_0', [#], [32 x 28 x 28]), Output('Block1393_Output_0', [#], [32 x 28 x 28]), Output('Block1323_Output_0', [#], [32 x 28 x 28]), Output('Block1261_Output_0', [#], [192 x 28 x 28]), Output('ReLU2279_Output_0', [#], [96 x 28 x 28]), Output('Block2154_Output_0', [#], [96 x 28 x 28]), Output('Block2011_Output_0', [#], [96 x 28 x 28]), Output('ReLU1990_Output_0', [#], [64 x 28 x 28]), Output('Block1874_Output_0', [#], [64 x 28 x 28]), Output('Block1740_Output_0', [#], [64 x 28 x 28]), Output('ReLU3146_Output_0', [#], [96 x 28 x 28]), Output('Block3012_Output_0', [#], [96 x 28 x 28]), Output('Block2860_Output_0', [#], [96 x 28 x 28]), Output('ReLU2839_Output_0', [#], [96 x 28 x 28]), Output('Block2714_Output_0', [#], [96 x 28 x 28]), Output('Block2571_Output_0', [#], [96 x 28 x 28]), Output('ReLU2550_Output_0', [#], [64 x 28 x 28]), Output('Block2434_Output_0', [#], [64 x 28 x 28]), Output('Block2300_Output_0', [#], [64 x 28 x 28]), Output('ReLU3554_Output_0', [#], [64 x 28 x 28]), Output('Block3434_Output_0', [#], [64 x 28 x 28]), Output('Block3296_Output_0', [#], [64 x 28 x 28]), Output('Block3166_Output_0', [#], [256 x 28 x 28]), Output('ReLU5664_Output_0', [#], [96 x 14 x 14]), Output('Block5462_Output_0', [#], [96 x 14 x 14]), Output('Block5242_Output_0', [#], [96 x 14 x 14]), Output('ReLU5221_Output_0', [#], [96 x 28 x 28]), Output('Block5028_Output_0', [#], [96 x 28 x 28]), Output('Block4817_Output_0', [#], [96 x 28 x 28]), Output('ReLU4796_Output_0', [#], [64 x 28 x 28]), Output('Block4612_Output_0', [#], [64 x 28 x 28]), Output('Block4410_Output_0', [#], [64 x 28 x 28]), Output('Block5684_Output_0', [#], [320 x 14 x 14]), Output('ReLU7403_Output_0', [#], [96 x 14 x 14]), Output('Block7160_Output_0', [#], [96 x 14 x 14]), Output('Block6899_Output_0', [#], [96 x 14 x 14]), Output('ReLU6878_Output_0', [#], [64 x 14 x 14]), Output('Block6644_Output_0', [#], [64 x 14 x 14]), Output('Block6392_Output_0', [#], [64 x 14 x 14]), Output('ReLU8978_Output_0', [#], [128 x 14 x 14]), Output('Block8726_Output_0', [#], [128 x 14 x 14]), Output('Block8456_Output_0', [#], [128 x 14 x 14]), Output('ReLU8435_Output_0', [#], [128 x 14 x 14]), Output('Block8192_Output_0', [#], [128 x 14 x 14]), Output('Block7931_Output_0', [#], [128 x 14 x 14]), Output('ReLU7910_Output_0', [#], [96 x 14 x 14]), Output('Block7676_Output_0', [#], [96 x 14 x 14]), Output('Block7424_Output_0', [#], [96 x 14 x 14]), Output('ReLU9740_Output_0', [#], [128 x 14 x 14]), Output('Block9502_Output_0', [#], [128 x 14 x 14]), Output('Block9246_Output_0', [#], [128 x 14 x 14]), Output('Block8998_Output_0', [#], [576 x 14 x 14]), Output('ReLU11690_Output_0', [#], [128 x 14 x 14]), Output('Block11379_Output_0', [#], [128 x 14 x 14]), Output('Block11050_Output_0', [#], [128 x 14 x 14]), Output('ReLU11029_Output_0', [#], [96 x 14 x 14]), Output('Block10727_Output_0', [#], [96 x 14 x 14]), Output('Block10407_Output_0', [#], [96 x 14 x 14]), Output('ReLU13673_Output_0', [#], [128 x 14 x 14]), Output('Block13353_Output_0', [#], [128 x 14 x 14]), Output('Block13015_Output_0', [#], [128 x 14 x 14]), Output('ReLU12994_Output_0', [#], [128 x 14 x 14]), Output('Block12683_Output_0', [#], [128 x 14 x 14]), Output('Block12354_Output_0', [#], [128 x 14 x 14]), Output('ReLU12333_Output_0', [#], [96 x 14 x 14]), Output('Block12031_Output_0', [#], [96 x 14 x 14]), Output('Block11711_Output_0', [#], [96 x 14 x 14]), Output('ReLU14639_Output_0', [#], [128 x 14 x 14]), Output('Block14333_Output_0', [#], [128 x 14 x 14]), Output('Block14009_Output_0', [#], [128 x 14 x 14]), Output('Block13693_Output_0', [#], [576 x 14 x 14]), Output('ReLU16997_Output_0', [#], [160 x 14 x 14]), Output('Block16618_Output_0', [#], [160 x 14 x 14]), Output('Block16221_Output_0', [#], [160 x 14 x 14]), Output('ReLU16200_Output_0', [#], [128 x 14 x 14]), Output('Block15830_Output_0', [#], [128 x 14 x 14]), Output('Block15442_Output_0', [#], [128 x 14 x 14]), Output('ReLU19388_Output_0', [#], [160 x 14 x 14]), Output('Block19000_Output_0', [#], [160 x 14 x 14]), Output('Block18594_Output_0', [#], [160 x 14 x 14]), Output('ReLU18573_Output_0', [#], [160 x 14 x 14]), Output('Block18194_Output_0', [#], [160 x 14 x 14]), Output('Block17797_Output_0', [#], [160 x 14 x 14]), Output('ReLU17776_Output_0', [#], [128 x 14 x 14]), Output('Block17406_Output_0', [#], [128 x 14 x 14]), Output('Block17018_Output_0', [#], [128 x 14 x 14]), Output('ReLU20558_Output_0', [#], [128 x 14 x 14]), Output('Block20184_Output_0', [#], [128 x 14 x 14]), Output('Block19792_Output_0', [#], [128 x 14 x 14]), Output('Block19408_Output_0', [#], [576 x 14 x 14]), Output('ReLU23324_Output_0', [#], [192 x 14 x 14]), Output('Block22877_Output_0', [#], [192 x 14 x 14]), Output('Block22412_Output_0', [#], [192 x 14 x 14]), Output('ReLU22391_Output_0', [#], [128 x 14 x 14]), Output('Block21953_Output_0', [#], [128 x 14 x 14]), Output('Block21497_Output_0', [#], [128 x 14 x 14]), Output('ReLU26123_Output_0', [#], [192 x 14 x 14]), Output('Block25667_Output_0', [#], [192 x 14 x 14]), Output('Block25193_Output_0', [#], [192 x 14 x 14]), Output('ReLU25172_Output_0', [#], [192 x 14 x 14]), Output('Block24725_Output_0', [#], [192 x 14 x 14]), Output('Block24260_Output_0', [#], [192 x 14 x 14]), Output('ReLU24239_Output_0', [#], [160 x 14 x 14]), Output('Block23801_Output_0', [#], [160 x 14 x 14]), Output('Block23345_Output_0', [#], [160 x 14 x 14]), Output('ReLU27497_Output_0', [#], [128 x 14 x 14]), Output('Block27055_Output_0', [#], [128 x 14 x 14]), Output('Block26595_Output_0', [#], [128 x 14 x 14]), Output('Block26143_Output_0', [#], [608 x 14 x 14]), Output('ReLU32827_Output_0', [#], [256 x 7 x 7]), Output('Block32303_Output_0', [#], [256 x 7 x 7]), Output('Block31761_Output_0', [#], [256 x 7 x 7]), Output('ReLU31740_Output_0', [#], [256 x 14 x 14]), Output('Block31225_Output_0', [#], [256 x 14 x 14]), Output('Block30692_Output_0', [#], [256 x 14 x 14]), Output('ReLU30671_Output_0', [#], [192 x 14 x 14]), Output('Block30165_Output_0', [#], [192 x 14 x 14]), Output('Block29641_Output_0', [#], [192 x 14 x 14]), Output('Block32847_Output_0', [#], [608 x 7 x 7]), Output('ReLU36820_Output_0', [#], [320 x 7 x 7]), Output('Block36255_Output_0', [#], [320 x 7 x 7]), Output('Block35672_Output_0', [#], [320 x 7 x 7]), Output('ReLU35651_Output_0', [#], [192 x 7 x 7]), Output('Block35095_Output_0', [#], [192 x 7 x 7]), Output('Block34521_Output_0', [#], [192 x 7 x 7]), Output('ReLU40327_Output_0', [#], [224 x 7 x 7]), Output('Block39753_Output_0', [#], [224 x 7 x 7]), Output('Block39161_Output_0', [#], [224 x 7 x 7]), Output('ReLU39140_Output_0', [#], [224 x 7 x 7]), Output('Block38575_Output_0', [#], [224 x 7 x 7]), Output('Block37992_Output_0', [#], [224 x 7 x 7]), Output('ReLU37971_Output_0', [#], [160 x 7 x 7]), Output('Block37415_Output_0', [#], [160 x 7 x 7]), Output('Block36841_Output_0', [#], [160 x 7 x 7]), Output('ReLU42055_Output_0', [#], [128 x 7 x 7]), Output('Block41495_Output_0', [#], [128 x 7 x 7]), Output('Block40917_Output_0', [#], [128 x 7 x 7]), Output('Block40347_Output_0', [#], [1056 x 7 x 7]), Output('ReLU45937_Output_0', [#], [320 x 7 x 7]), Output('Block45304_Output_0', [#], [320 x 7 x 7]), Output('Block44653_Output_0', [#], [320 x 7 x 7]), Output('ReLU44632_Output_0', [#], [192 x 7 x 7]), Output('Block44008_Output_0', [#], [192 x 7 x 7]), Output('Block43366_Output_0', [#], [192 x 7 x 7]), Output('ReLU49852_Output_0', [#], [224 x 7 x 7]), Output('Block49210_Output_0', [#], [224 x 7 x 7]), Output('Block48550_Output_0', [#], [224 x 7 x 7]), Output('ReLU48529_Output_0', [#], [224 x 7 x 7]), Output('Block47896_Output_0', [#], [224 x 7 x 7]), Output('Block47245_Output_0', [#], [224 x 7 x 7]), Output('ReLU47224_Output_0', [#], [192 x 7 x 7]), Output('Block46600_Output_0', [#], [192 x 7 x 7]), Output('Block45958_Output_0', [#], [192 x 7 x 7]), Output('ReLU51784_Output_0', [#], [128 x 7 x 7]), Output('Block51156_Output_0', [#], [128 x 7 x 7]), Output('Block50510_Output_0', [#], [128 x 7 x 7]), Output('Block49872_Output_0', [#], [1024 x 7 x 7])]

edwardcho avatar Jun 29 '17 11:06 edwardcho

And Then, I inputed "Block52522_Output_0" as node-name.

loaded_model  = load_model(model_file)

print ("loaded_model :", loaded_model)
node_in_graph = loaded_model.find_by_name(node_name)
print ("node_in_graph :", node_in_graph)
output_nodes  = combine([node_in_graph.owner])

I met this error. loaded_model : Composite(Tensor[3,224,224]) -> Tensor[13] node_in_graph : None // WHY????? Traceback (most recent call last): File "FeatureExtraction.py", line 75, in eval_and_write(model_file, node_name, output_file, minibatch_source, num_objects=5) File "FeatureExtraction.py", line 28, in eval_and_write output_nodes = combine([node_in_graph.owner]) AttributeError: 'NoneType' object has no attribute 'owner'

edwardcho avatar Jun 29 '17 11:06 edwardcho

you may need to look at the function bn_inception_model Where is this Python program?

meijiesky avatar Jun 29 '17 14:06 meijiesky

"bn_inception_model" function is in other python file. (This is example code of CNTK.) (Examples/Image/Classification/GoogLeNet/BN-Inception/Python)

And, this is "bn_inception_model" function in python file. ... def bn_inception_model(input, labelDim, bnTimeConst):

# 224 x 224 x 3
conv1 = conv_bn_relu_layer(input, 64, (7,7), (2,2), True, bnTimeConst)
# 112 x 112 x 64
pool1 = MaxPooling(filter_shape=(3,3), strides=(2,2), pad=True)(conv1)
# 56 x 56 x 64
conv2a = conv_bn_relu_layer(pool1, 64, (1,1), (1,1), True, bnTimeConst)
# 56 x 56 x 64
conv2b = conv_bn_relu_layer(conv2a, 192, (3,3), (1,1), True, bnTimeConst)
# 56 x 56 x 192
pool2 = MaxPooling(filter_shape=(3,3), strides=(2,2), pad=True)(conv2b)

# Inception Blocks
# 28 x 28 x 192
inception3a = inception_block_with_avgpool(pool2, 64, 64, 64, 64, 96, 32, bnTimeConst)
# 28 x 28 x 256
inception3b = inception_block_with_avgpool(inception3a, 64, 64, 96, 64, 96, 64, bnTimeConst)
# 28 x 28 x 320
inception3c = inception_block_pass_through(inception3b, 0, 128, 160, 64, 96, 0, bnTimeConst)
# 14 x 14 x 576
inception4a = inception_block_with_avgpool(inception3c, 224, 64, 96, 96, 128, 128, bnTimeConst)
# 14 x 14 x 576
inception4b = inception_block_with_avgpool(inception4a, 192, 96, 128, 96, 128, 128, bnTimeConst)
# 14 x 14 x 576
inception4c = inception_block_with_avgpool(inception4b, 160, 128, 160, 128, 160, 128, bnTimeConst)
# 14 x 14 x 576
inception4d = inception_block_with_avgpool(inception4c, 96, 128, 192, 160, 192, 128, bnTimeConst)
# 14 x 14 x 576
inception4e = inception_block_pass_through(inception4d, 0, 128, 192, 192, 256, 0, bnTimeConst)
# 7 x 7 x 1024
inception5a = inception_block_with_avgpool(inception4e, 352, 192, 320, 160, 224, 128, bnTimeConst)
# 7 x 7 x 1024
inception5b = inception_block_with_maxpool(inception5a, 352, 192, 320, 192, 224, 128, bnTimeConst)

# Global Average
# 7 x 7 x 1024
pool3 = AveragePooling(filter_shape=(7,7))(inception5b)
# 1 x 1 x 1024
z = Dense(labelDim, init=he_normal())(pool3)

return z

In this code, How to find node name??

edwardcho avatar Jun 29 '17 23:06 edwardcho

I think all the variable names on the left can be the node_name, such as conv1/pool1/.../z.

If you still get error, you can add a Label layer or give a name to that layer before training.

meijiesky avatar Jun 30 '17 03:06 meijiesky

Yes. Thanks. If so, after set name of all layers, I should do use name of the "Dense" layer as node_name. Right?? (the last layer of "bn_inception_model" is Dense).

edwardcho avatar Jun 30 '17 08:06 edwardcho

It works?

If you want to name the layer, look at the API and you can find that the following definition on the Dense function.

Dense(shape, activation=identity, init=glorot_uniform(), input_rank=None, map_rank=None, bias=True, init_bias=0, name='')

Therefore, you can name your layer in the argument. For example, Dense(labelDim, init=he_normal(), name = "last_layer")

Then, you can use last_layer as your node_name

meijiesky avatar Jun 30 '17 11:06 meijiesky

Hello meijiesky, Thank you for your help. After set "last_layer" as layer-name of "Dense" layer, I re-try it. It works.. Thanks.. So I am re-trying my case.

BTW, I have one question. When I do not set layer-name of "Dense" layer, the name of the layer is set automatically. At this time, I think that I can use the name of this layer. BUT the error was occurred in this case. (Then, I set layer name of "Dense" layer manually, => in this case, normally) I can not understand differences of two cases.

edwardcho avatar Jul 03 '17 01:07 edwardcho

Hey edwardcho,

I don't think the name of the layer will be set automatically if you don't set the layer-name of the 'Dense' layer. I suppose you have to name it or have a label layer right after it so that you can have access to the output of that layer.

meijiesky avatar Jul 03 '17 07:07 meijiesky

Hello meijiesky, Sorry for my response delay. I can understand your help but I have one question. If so, I don't set the layer name of all layers, the name of layers (at my previous article, for example. [Output('Block52522_Output_0', [#], [13]), Output('Block51807_Output_0', [#], [1024 x 1 x 1]), Output('Splice51787_Output_0', [#], [1024 x 7 x 7]), Output('ReLU43345_Output_0', [#], [352 x 7 x 7]), Output('Block42721_Output_0', [#], [352 x 7 x 7]), Output('Block42079_Output_0', [#], [352 x 7 x 7]), Output('Splice42058_Output_0', [#], [1024 x 7 x 7]), Output('ReLU34500_Output_0', [#], [352 x 7 x 7]), ....] => What is 'Block52522_Output_0' ?? ( I think that this name('Block52522_Output_0') is set automatically as layer-name because I don't set it ) So I can not understand this problem clearly.

edwardcho avatar Jul 10 '17 21:07 edwardcho

Ohh I see. I guess you are right. I am not sure about this. Have you tried to access the layer with the name ('Block52522_Output_0') ?

meijiesky avatar Jul 11 '17 06:07 meijiesky

I already had tried it. When I used 'Block52522_Output_0' as layer-name (this name was setted automatically) , I met error. But, when I set layer-name of 'Dense' manually, my logic works. I wonder what the two cases are different.

edwardcho avatar Jul 11 '17 07:07 edwardcho

Hello meijiesky. Thank you for your help. Anyway I got prediction result of test-data at my-model. I used training and predicted on python-code.

Then, I tried to get prediction on C# code using my-model (this model was generated python code already.) I had used this code on Github. ... https://github.com/Microsoft/CNTK/blob/master/Examples/Evaluation/CNTKLibraryCSEvalCPUOnlyExamples/CNTKLibraryCSEvalExamples.cs

BTW, I could not same predicted result on C# code. Why do different these results?? (the predicted result C#-code and python-code.)

I saw c# code of above-site. ... Function rootFunc = Function.Load(modelFilePath, device); .. Then I was python code in my code. ... loaded_model = load_model(model_file) node_in_graph = loaded_model.find_by_name(node_name) output_nodes = combine([node_in_graph.owner]) ..

I checked some difference in two codes. 1. find_by_name after loaded model. 2. combine.

So, I tried some cases for predicted code on C#-code. (the original code of C# is code of above-site). ... // Case 1 <<- my first test Function rootFunc = Function.Load(modelFilePath, device); Function interLayerFunc = rootFunc.FindByName("last_layer"); var modelFunc = Function.Combine(interLayerFunc.Outputs);

            // Case 2    <<- my second test
            Function rootFunc = Function.Load(modelFilePath, device);
            Function interLayerFunc = rootFunc.FindByName("last_layer");
            var modelFunc = Function.AsComposite(interLayerFunc);

            // Case 3     <<- my second test
            Function rootFunc = Function.Load(modelFilePath, device);
            Function interLayerFunc = rootFunc.FindByName("last_layer");
            var modelFunc = CNTKLib.AsComposite(interLayerFunc);

In these cases, I could not same predicted code with python-code. What should I do modify in my C# code.??

P.S. I want to use Softmax function in C# code for evaluation. How to use it??

edwardcho avatar Jul 12 '17 10:07 edwardcho

All the 3 cases have the same effect. Does the modelFunc has the same input/output variables (and their shapes) as those returned in python? Do you feed the same data to C# and Python? You probably need to do some image prepreocessing e.g. resize/HWC extraction before calling Evaluate() to make sure the input is what expected by the model.

zhouwangzw avatar Jul 12 '17 20:07 zhouwangzw

Hello zhouwangzw, Thank you for your reply, I had tested using two datasets. My testing way is as follows,

  1. training using python-code,
  2. I tried to get predicted result using C#-code. C#-code is the code of above-site.

I used two data-sets. (CIFAR-10 case that included cntk-sample and my data-set). DATASET #1: CIFAR-10 case that included cntk-sample : as you know, original image size is 40 * 40, then the preprocessing for training is crop and resize for images. (images are crop 32 * 32)

DATASET #2: my data-set : original image size is 256 * 256, then the preprocessing for training is crop and resize for images. (images are crop 224 * 224). I used python-code of Image-Net in cntk-sample-code.

Using DATASET #1 (CIFAR-10), training (in python-code) and evaluation (in C# code, using above c# code). => I got reasonable result as predicted result. (accuracy : 80% more) Using DATASET #2 (my data-set), training (in python-code) and evaluation (in C# code, using above c# code). => I could not get reasonable result as predicted result. (accuracy : less than 0.05%)

I can not understand this status. Why do predict normally in CIFAR-10 case?? (As you know, in CIFAR-10, image-preprocessing is in python-code for training, but, in C#-code, image-processing is not it.) and Why do not predict in my-data case??

edwardcho avatar Jul 12 '17 21:07 edwardcho

Have you get the same evaluation result in Python (using Function.Eval) with your own dataset and that in C#? If they are the same, the nyour probably need to re-train your model with your data set in order to improve accuracy. If they are different, then probably some image pre-processing is not matching the expcted input of the model. Are images in your data-set RGB images or gray-scale ones?

zhouwangzw avatar Jul 13 '17 09:07 zhouwangzw