Hei, I got an error when running code for Alexnet feature extraction. I createalexnet.pb
file using this github link. I checked using Tensorboard and the graph went well.
嘿,我在运行Alexnet特性提取的代码时出错了。我createalexnet。使用这个github链接的pb文件。我用画板检查了一下,结果很好。
I want to use this model to extract feature from fc7/relu
and feed it to another model. I create the graph using this:
我想使用这个模型从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()
And then feed the input
and extract the fc7/relu
layer using this:
然后输入并提取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
But I got this error:
但我犯了一个错误:
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\
It seems I did wrong when feeding the graph. I see the graph using Tensorboard and it seems the placeholder dtype
is uint8
. How can I solve this?
我在给图表喂奶时好像做错了。我看到了使用Tensorboard的图形,似乎占位符dtype是uint8。我怎么解决这个问题?
Full error:
完整的错误:
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\
1 个解决方案
#1
1
This line:
这条线:
image_data = gfile.FastGFile(image_path, 'rb').read()
Is reading the file at image_path
as an array of bytes. However, what the input
placeholder expects is a four-dimensional array of type uint8
. Take a look, for example, at one of the next tutorials from the link you provided, 10 AlexNet Transfer Learning; the function get_batch
produces the batches using an additional graph an operations like tf.image.decode_jpeg
; then it gives the result of that graph as input to the main network graph.
以字节数组的形式读取image_path上的文件。然而,输入占位符所期望的是类型为uint8的四维数组。例如,看看您提供的链接的下一个教程,10 AlexNet Transfer Learning;函数get_batch使用一个额外的图来生成批次,操作类似tf.image.decode_jpeg;然后给出该图的结果作为主网络图的输入。
For example, you could have something like this (if all your images fit in memory, otherwise you'd have to batch them like in the tutorial):
例如,您可以有这样的东西(如果您的所有图像都符合内存,否则您将不得不像教程中那样对它们进行批处理):
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})
#1
1
This line:
这条线:
image_data = gfile.FastGFile(image_path, 'rb').read()
Is reading the file at image_path
as an array of bytes. However, what the input
placeholder expects is a four-dimensional array of type uint8
. Take a look, for example, at one of the next tutorials from the link you provided, 10 AlexNet Transfer Learning; the function get_batch
produces the batches using an additional graph an operations like tf.image.decode_jpeg
; then it gives the result of that graph as input to the main network graph.
以字节数组的形式读取image_path上的文件。然而,输入占位符所期望的是类型为uint8的四维数组。例如,看看您提供的链接的下一个教程,10 AlexNet Transfer Learning;函数get_batch使用一个额外的图来生成批次,操作类似tf.image.decode_jpeg;然后给出该图的结果作为主网络图的输入。
For example, you could have something like this (if all your images fit in memory, otherwise you'd have to batch them like in the tutorial):
例如,您可以有这样的东西(如果您的所有图像都符合内存,否则您将不得不像教程中那样对它们进行批处理):
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})