【发布时间】:2021-11-26 08:45:09
【问题描述】:
从 xcption 模型中提取特征 我正在使用 cnn 和 lstm 开发图像标题生成器,并且我分享了从网站获得的代码。我试图解决它,但是当我对代码进行更改时,我在其他部分出现错误,所以任何人都可以告诉我如何解决这个问题
def extract_features(directory):
model = Xception( include_top=False, pooling='avg' )
features = {}
for img in tqdm(os.walk(directory)):
filename = directory + "/" + img
image = Image.open(filename)
image = image.resize((299,299))
image = np.expand_dims(image, axis=0)
#image = preprocess_input(image)
image = image/127.5
image = image - 1.0
feature = model.predict(image)
features[img] = feature
return features
#2048 feature vector
features = extract_features(dataset_images)
dump(features, open("features.p","wb"))
错误
TypeError Traceback (most recent call last)
<ipython-input-8-5f1d4e4e3211> in <module>()
16
17 #2048 feature vector
---> 18 features = extract_features(dataset_images)
19 dump(features, open("features.p","wb"))
<ipython-input-8-5f1d4e4e3211> in extract_features(directory)
3 features = {}
4 for img in tqdm(os.walk(directory)):
----> 5 filename = directory + "/" + img
6 image = Image.open(filename)
7 image = image.resize((299,299))
TypeError: can only concatenate str (not "tuple") to str
【问题讨论】:
-
os.listdir()返回文件和目录。所以你正在尝试使用Image.open打开一个目录 -
是的,我想打开一个图像数据集。我是否需要使用其他代码来打开目录?实际上,我将 'os.listdir()' 更改为 'os.walk' 并解决了 ' IsADirectory ' 错误,但我遇到了其他错误。
-
顺便说一句,你的新错误是什么?
-
我已经更新了我所做的更改后出现的新错误。你能查一下吗,我卡在那里了
标签: python image google-colaboratory feature-extraction