gpt4 book ai didi

python - 值错误: invalid literal for int() with base 10 on Alexnet

转载 作者:行者123 更新时间:2023-11-30 09:48:37 25 4
gpt4 key购买 nike

Hei,我在运行 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()

然后输入输入并使用以下命令提取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 查看该图,占位符 dtype 似乎是 uint8。我该如何解决这个问题?

完整错误:

  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})

关于python - 值错误: invalid literal for int() with base 10 on Alexnet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49027781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com