ValueError:在Alexnet上具有基数10的int()的无效文字

问题描述 投票:0回答:1

嘿,我在运行Alexnet特征提取代码时遇到错误。我使用这个github alexnet.pb创建了link文件。我使用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\
python tensorflow machine-learning deep-learning
1个回答
1
投票

这一行:

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})
© www.soinside.com 2019 - 2024. All rights reserved.