0

我基本上去了 CNTK 网站并提取了一些代码来获取图像的特征向量(最后一层之前的一层)。

这就是我现在所拥有的,它是一个 iPython Notebook

# importing library
import os,sys
import numpy as np
import cntk as C
from cntk import load_model, combine
import cntk.io.transforms as xforms
from cntk.logging import graph
from cntk.logging.graph import get_node_outputs
import zipfile
import shutil

try:
    from urllib.request import urlretrieve 
except ImportError: 
    from urllib import urlretrieve

def download_grocery_data():
    if not os.path.exists(os.path.join("Grocery", "testImages")):
        filename = os.path.join("Grocery.zip")
        if not os.path.exists(filename):
            url = "https://www.cntk.ai/DataSets/Grocery/Grocery.zip"
            print('Downloading data from ' + url + '...')
            urlretrieve(url, filename)

        try:
            print('Extracting ' + filename + '...')
            with zipfile.ZipFile(filename) as myzip:
                myzip.extractall()
#             if platform != "win32":
                testfile  = os.path.join("Grocery", "test.txt")
                unixfile = os.path.join("Grocery", "test_unix.txt")
                out = open(unixfile, 'w')
                with open(testfile) as f:
                    for line in f:
                        out.write(line.replace('\\', '/'))
                out.close()
                shutil.move(unixfile, testfile)
        finally:
            os.remove(filename)
        print('Done.')
    else:
        print('Data already available at ' + '/Grocery')


download_grocery_data()


try:
    from urllib.request import urlretrieve 
except ImportError: 
    from urllib import urlretrieve

# Add models here like this: (category, model_name, model_url)
models = (('Image Classification', 'AlexNet_ImageNet_CNTK', 'https://www.cntk.ai/Models/CNTK_Pretrained/AlexNet_ImageNet_CNTK.model'),
          ('Image Classification', 'AlexNet_ImageNet_Caffe', 'https://www.cntk.ai/Models/Caffe_Converted/AlexNet_ImageNet_Caffe.model'),
          ('Image Classification', 'InceptionV3_ImageNet_CNTK', 'https://www.cntk.ai/Models/CNTK_Pretrained/InceptionV3_ImageNet_CNTK.model'),
          ('Image Classification', 'BNInception_ImageNet_Caffe', 'https://www.cntk.ai/Models/Caffe_Converted/BNInception_ImageNet_Caffe.model'),
          ('Image Classification', 'ResNet18_ImageNet_CNTK', 'https://www.cntk.ai/Models/CNTK_Pretrained/ResNet18_ImageNet_CNTK.model'),
          ('Image Classification', 'ResNet34_ImageNet_CNTK', 'https://www.cntk.ai/Models/CNTK_Pretrained/ResNet34_ImageNet_CNTK.model'),
          ('Image Classification', 'ResNet50_ImageNet_CNTK', 'https://www.cntk.ai/Models/CNTK_Pretrained/ResNet50_ImageNet_CNTK.model'),
          ('Image Classification', 'ResNet20_CIFAR10_CNTK', 'https://www.cntk.ai/Models/CNTK_Pretrained/ResNet20_CIFAR10_CNTK.model'),
          ('Image Classification', 'ResNet110_CIFAR10_CNTK', 'https://www.cntk.ai/Models/CNTK_Pretrained/ResNet110_CIFAR10_CNTK.model'),
          ('Image Classification', 'ResNet50_ImageNet_Caffe', 'https://www.cntk.ai/Models/Caffe_Converted/ResNet50_ImageNet_Caffe.model'),
          ('Image Classification', 'ResNet101_ImageNet_Caffe', 'https://www.cntk.ai/Models/Caffe_Converted/ResNet101_ImageNet_Caffe.model'),
          ('Image Classification', 'ResNet152_ImageNet_Caffe', 'https://www.cntk.ai/Models/Caffe_Converted/ResNet152_ImageNet_Caffe.model'),
          ('Image Classification', 'VGG16_ImageNet_Caffe', 'https://www.cntk.ai/Models/Caffe_Converted/VGG16_ImageNet_Caffe.model'),
          ('Image Classification', 'VGG19_ImageNet_Caffe', 'https://www.cntk.ai/Models/Caffe_Converted/VGG19_ImageNet_Caffe.model'),
          ('Image Object Detection', 'Fast-RCNN_grocery100', 'https://www.cntk.ai/Models/FRCN_Grocery/Fast-RCNN_grocery100.model'),
          ('Image Object Detection', 'Fast-RCNN_Pascal', 'https://www.cntk.ai/Models/FRCN_Pascal/Fast-RCNN.model'))

def download_model(model_file_name, model_url):
#     model_dir = os.path.dirname(os.path.abspath(__file__))
#     filename = os.path.join(model_dir, model_file_name)
    filename = os.path.join(model_file_name)

    if not os.path.exists(filename):
        print('Downloading model from ' + model_url + ', may take a while...')
        urlretrieve(model_url, filename)
        print('Saved model as ' + filename)
    else:
        print('CNTK model already available at ' + filename)

def download_model_by_name(model_name):
    if model_name.endswith('.model'):
        model_name = model_name[:-6]

    model = next((x for x in models if x[1]==model_name), None)
    if model is None:
        print("ERROR: Unknown model name '%s'." % model_name)
        list_available_models()
    else:
        download_model(model_name + '.model', model[2])

def list_available_models():
    print("\nAvailable models (for more information see Readme.md):")
    max_cat = max(len(x[1]) for x in models)
    max_name = max(len(x[1]) for x in models)
    print("{:<{width}}   {}".format('Model name', 'Category', width=max_name))
    print("{:-<{width}}   {:-<{width_cat}}".format('', '', width=max_name, width_cat=max_cat))
    for model in sorted(models):
        print("{:<{width}}   {}".format(model[1], model[0], width=max_name))

sys.path.append(os.path.join( "..", "DataSets", "Grocery"))

# from install_grocery import download_grocery_data
download_grocery_data()

sys.path.append(os.path.join("..", "..", "..", "PretrainedModels"))

# from download_model import download_model_by_name
download_model_by_name("ResNet18_ImageNet_CNTK")


def create_mb_source(image_height, image_width, num_channels, map_file):
    transforms = [xforms.scale(width=image_width, height=image_height, channels=num_channels, interpolations='linear')]
    return C.io.MinibatchSource(
        C.io.ImageDeserializer(map_file, C.io.StreamDefs(
            features=C.io.StreamDef(field='image', transforms=transforms),
            labels=C.io.StreamDef(field='label', shape=1000))),
        randomize=False)

def eval_and_write(model_file, node_name, output_file, minibatch_source, num_objects):
    # load model and pick desired node as output
    loaded_model  = load_model(model_file)
    node_in_graph = loaded_model.find_by_name(node_name)
    output_nodes  = combine([node_in_graph.owner])

    # evaluate model and get desired node output
    print("Evaluating model for output node %s" % node_name)
    features_si = minibatch_source['features']
    with open(output_file, 'wb') as results_file:
        for i in range(0, num_objects):
            mb = minibatch_source.next_minibatch(1)
            output = output_nodes.eval(mb[features_si])

            # write results to file
            out_values = output[0].flatten()
            np.savetxt(results_file, out_values[np.newaxis], fmt="%.6f")

    def main():
        # define location of model and data and check existence
        model_file  = os.path.join("ResNet18_ImageNet_CNTK.model")
        print(model_file)


        map_file = os.path.join("Grocery", "test.txt")
        print(map_file)


    #     os.chdir(os.path.join("..", "DataSets", "Grocery"))

        if not (os.path.exists(model_file)):
            print("model bhetena")

        if not (os.path.exists(map_file)):
            print("map file bhetena")

        if not (os.path.exists(model_file) and os.path.exists(map_file)):
            print("Please run 'python install_data_and_model.py' first to get the required data and model.")
            exit(0)

        # create minibatch source
        image_height = 224
        image_width  = 224
        num_channels = 3
        minibatch_source = create_mb_source(image_height, image_width, num_channels, map_file)

        # use this to print all node names of the model (and knowledge of the model to pick the correct one)
        # node_outputs = get_node_outputs(load_model(model_file))
        # for out in node_outputs: print("{0} {1}".format(out.name, out.shape))

        # use this to get 1000 class predictions (not yet softmaxed!)
    #     node_name = "z"
    #     output_file = os.path.join(base_folder, "predOutput.txt")
    #     output_file = os.path.join("predOutput.txt")

        # use this to get 512 features from the last but one layer of ResNet_18
        node_name = "z.x"
    #     output_file = os.path.join(base_folder, "layerOutput.txt")
        output_file = os.path.join("layerOutput.txt")



    # evaluate model and write out the desired layer output
    eval_and_write(model_file, node_name, output_file, minibatch_source, num_objects=5)

    print("Done. Wrote output to %s" % output_file)

main()

此时它的作用是下载模型,下载测试图像,但模型无法读取图像。

这是我得到的错误,

ResNet18_ImageNet_CNTK.model
Grocery\test.txt
Evaluating model for output node z.x
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-5-283fca1e6e4a> in <module>()
     80     print("Done. Wrote output to %s" % output_file)
     81 
---> 82 main()

<ipython-input-5-283fca1e6e4a> in main()
     76 
     77     # evaluate model and write out the desired layer output
---> 78     eval_and_write(model_file, node_name, output_file, minibatch_source, num_objects=5)
     79 
     80     print("Done. Wrote output to %s" % output_file)

<ipython-input-5-283fca1e6e4a> in eval_and_write(model_file, node_name, output_file, minibatch_source, num_objects)
     18     with open(output_file, 'wb') as results_file:
     19         for i in range(0, num_objects):
---> 20             mb = minibatch_source.next_minibatch(1)
     21             output = output_nodes.eval(mb[features_si])
     22 

c:\users\t540p\appdata\local\programs\python\python35\lib\site-packages\cntk\internal\swig_helper.py in wrapper(*args, **kwds)
     67     @wraps(f)
     68     def wrapper(*args, **kwds):
---> 69         result = f(*args, **kwds)
     70         map_if_possible(result)
     71         return result

c:\users\t540p\appdata\local\programs\python\python35\lib\site-packages\cntk\io\__init__.py in next_minibatch(self, minibatch_size_in_samples, input_map, device, num_data_partitions, partition_index)
    327                                             minibatch_size_in_samples,
    328                                             num_data_partitions,
--> 329                                             partition_index, device)
    330 
    331         if not mb:

c:\users\t540p\appdata\local\programs\python\python35\lib\site-packages\cntk\cntk_py.py in get_next_minibatch(self, *args)
   2967 
   2968     def get_next_minibatch(self, *args):
-> 2969         return _cntk_py.MinibatchSource_get_next_minibatch(self, *args)
   2970 MinibatchSource_swigregister = _cntk_py.MinibatchSource_swigregister
   2971 MinibatchSource_swigregister(MinibatchSource)

RuntimeError: Cannot open file 'testImages/WIN_20160803_11_28_42_Pro.jpg'

[CALL STACK]
    > Microsoft::MSR::CNTK::IDataReader::  SupportsDistributedMBRead
    - CreateCompositeDataReader (x3)
    - vcomp_fork (x3)
    - vcomp_atomic_div_r8
    - vcomp_fork
    - CreateCompositeDataReader (x4)
    - CNTK::Dictionary::  ~Dictionary (x3)

这是错误的屏幕截图,

在此处输入图像描述

4

1 回答 1

0

一种解决方案是在 map_file 中使用绝对路径(请参阅 create_mb_source 函数)。另一种解决方案是使用三点表示法指定相对于 map_file 路径的路径:“.../path/relative/to/path/of/map_file”

于 2018-02-04T06:15:29.153 回答