嘿,运行用于Alexnet功能提取的代码时出现错误。我使用这个github link创建alexnet.pb文件。我使用Tensorboard进行了检查,该图运行良好。

我想使用此模型从fc7/relu中提取特征并将其提供给另一个模型。我使用以下方法创建图形:

data = 0

model_dir = 'model'
images_dir = 'images_alexnet/train/' + str(data) + '/'
list_images = [images_dir+f for f in os.listdir(images_dir) if re.search('jpeg|JPEG', f)]
list_images.sort()

def create_graph():
    with gfile.FastGFile(os.path.join(model_dir, 'alexnet.pb'), 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        _ = tf.import_graph_def(graph_def, name='')

create_graph()


然后输入input并使用以下命令提取fc7/relu层:

def extract_features(image_paths, verbose=False):
    feature_dimension = 4096
    features = np.empty((len(image_paths), feature_dimension))

    with tf.Session() as sess:
        flattened_tensor = sess.graph.get_tensor_by_name('fc7/relu:0')

        for i, image_path in enumerate(image_paths):
            if verbose:
                print('Processing %s...' % (image_path))

            if not gfile.Exists(image_path):
                tf.logging.fatal('File does not exist %s', image)

            image_data = gfile.FastGFile(image_path, 'rb').read()
            feature = sess.run(flattened_tensor, {'input:0': image_data})
            features[i, :] = np.squeeze(feature)

    return features


但是我得到了这个错误:

ValueError: invalid literal for int() with base 10: b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\x08\x06\x06\x07\x06\x05\x08\x07\x07\x07\t\t\x08\n\x0c\x14\r\x0c\x0b\x0b\x0c\x19\x12\x13\x0f\x14\x1d\x1a\x1f\x1e\


喂图时似乎做错了。我使用Tensorboard看到该图,并且占位符dtypeuint8。我该如何解决?

完整错误:

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Hermon Jay/Documents/Python/diabetic_retinopathy_temp6_transfer_learning/feature_extraction_alexnet.py", line 49, in <module>
    features = extract_features(list_images)

  File "C:/Users/Hermon Jay/Documents/Python/diabetic_retinopathy_temp6_transfer_learning/feature_extraction_alexnet.py", line 44, in extract_features
    feature = sess.run(flattened_tensor, {'input:0': image_data})

  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 889, in run
    run_metadata_ptr)

  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1089, in _run
    np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)

  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\numeric.py", line 531, in asarray
    return array(a, dtype, copy=False, order=order)

ValueError: invalid literal for int() with base 10: b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\x08\x06\x06\x07\x06\x05\x08\x07\x07\x07\t\t\x08\n\x0c\x14\r\x0c\x0b\x0b\x0c\x19\x12\x13\x0f\x14\x1d\x1a\x1f\x1e\

最佳答案

这行:

image_data = gfile.FastGFile(image_path, 'rb').read()


正在读取image_path处的文件作为字节数组。但是,input占位符期望的是类型为uint8的三维数组。例如,从您提供的链接10 AlexNet Transfer Learning中查看下一个教程之一。函数get_batch使用附加图和tf.image.decode_jpeg之类的操作来生成批处理;然后将该图的结果作为主网络图的输入。

例如,您可能会有类似的内容(如果所有图像都适合存储在内存中,否则您必须像本教程中那样对它们进行批处理):

def read_images(image_paths):
    with tf.Graph().as_default(), tf.Session() as sess:
        file_name = tf.placeholder(tf.string)
        jpeg_data = tf.read_file(jpeg_name)
        decoded_image = tf.image.decode_jpeg(jpeg_data, channels=3)
        images = []
        for path in image_paths:
            images.append(sess.run(decoded_image, feed_dict={file_name: path}))
        return images

def extract_features(image_paths):
    images = read_images(image_paths)
    with tf.Session() as sess:
        flattened_tensor = sess.graph.get_tensor_by_name('fc7/relu:0')
        return sess.run(flattened_tensor, {'input:0': images})

10-08 00:25