Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ata格式)或(32224224)(使用“通道优先”数据格式)。它应该有3个输入通道,宽度和高度不应小于197。例如,(200,200,3)将是一个有效值_Python_Tensorflow_Keras_Resnet_Imagenet - Fatal编程技术网

Python ata格式)或(32224224)(使用“通道优先”数据格式)。它应该有3个输入通道,宽度和高度不应小于197。例如,(200,200,3)将是一个有效值

Python ata格式)或(32224224)(使用“通道优先”数据格式)。它应该有3个输入通道,宽度和高度不应小于197。例如,(200,200,3)将是一个有效值,python,tensorflow,keras,resnet,imagenet,Python,Tensorflow,Keras,Resnet,Imagenet,我的猜测是,由于您将include_top指定为False,网络定义将输入填充为比224x224更大的形状,因此,当您提取特征时,您最终得到的是特征映射,而不是特征向量(这是导致错误的原因) 只需按以下方式指定并输入_形状: return ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3)).predict(preprocess_inpu

我的猜测是,由于您将
include_top
指定为
False
,网络定义将输入填充为比224x224更大的形状,因此,当您提取特征时,您最终得到的是特征映射,而不是特征向量(这是导致错误的原因)

只需按以下方式指定并输入_形状:

return ResNet50(weights='imagenet',
                include_top=False,
                input_shape=(224, 224, 3)).predict(preprocess_input(tensor))
的文档介绍了构造函数参数
input\u shape
(重点是我的):

输入形状:可选形状元组,仅当include\u top为False时指定(否则输入形状必须为(224,224,3)(使用“channels\u last”数据格式)或(3,224,224)(使用“channels\u first”数据格式)。它应该正好有3个输入通道,宽度和高度不应小于197。例如。(200,200,3)将是一个有效值

我的猜测是,由于您将
include_top
指定为
False
,网络定义将输入填充为比224x224更大的形状,因此,当您提取特征时,您最终得到的是特征映射,而不是特征向量(这是导致错误的原因)

只需按以下方式指定并输入_形状:

return ResNet50(weights='imagenet',
                include_top=False,
                input_shape=(224, 224, 3)).predict(preprocess_input(tensor))
多亏了的帮助,我解决了这个问题。问题确实出在
ResNet50
pool
层上

上面脚本中的以下代码:

return ResNet50(weights='imagenet',
                include_top=False).predict(preprocess_input(tensor))
返回一个
(1,7,7,2048)
(当然,我不完全理解为什么)。为了避免这个问题,我在参数
pooling=“avg”
中添加了如下内容:

return ResNet50(weights='imagenet',
                include_top=False,
                pooling="avg").predict(preprocess_input(tensor))
相反,它返回一个
(12048)
(同样,我不知道为什么

但是,模型仍然需要一个4-D形状。为了解决这个问题,我在我的
dog\u breed()
函数中添加了以下代码:

print(bottleneck_feature.shape) #returns (1, 2048)
bottleneck_feature = np.expand_dims(bottleneck_feature, axis=0)
bottleneck_feature = np.expand_dims(bottleneck_feature, axis=0)
bottleneck_feature = np.expand_dims(bottleneck_feature, axis=0)
print(bottleneck_feature.shape) #returns (1, 1, 1, 1, 2048) - yes a 5D shape, not 4.
这将返回一个
(1,1,1,1,2048)
。出于某种原因,当我只添加了两个维度时,模型仍然抱怨它是一个3D形状,但当我添加了第三个维度时,它停止了(这很奇怪,我想了解更多关于为什么会这样)

因此,总的来说,我的
dog\u breed()
函数来自:

def dog_breed(img_path):
    #extract bottleneck features
    bottleneck_feature = extract_Resnet50(path_to_tensor(img_path))
    #obtain predicted vector
    predicted_vector = Resnet50_model.predict(bottleneck_feature) #shape error occurs here
    #return dog breed that is predicted by the model
    return dog_names[np.argmax(predicted_vector)]
为此:

def dog_breed(img_path):
    #extract bottleneck features
    bottleneck_feature = extract_Resnet50(path_to_tensor(img_path))
    print(bottleneck_feature.shape) #returns (1, 2048)
    bottleneck_feature = np.expand_dims(bottleneck_feature, axis=0)
    bottleneck_feature = np.expand_dims(bottleneck_feature, axis=0)
    bottleneck_feature = np.expand_dims(bottleneck_feature, axis=0)
    print(bottleneck_feature.shape) #returns (1, 1, 1, 1, 2048) - yes a 5D shape, not 4.
    #obtain predicted vector
    predicted_vector = Resnet50_model.predict(bottleneck_feature) #shape error occurs here
    #return dog breed that is predicted by the model
    return dog_names[np.argmax(predicted_vector)]
在确保参数
pooling=“avg”
添加到我对
ResNet50

的调用时,多亏了的帮助,我解决了这个问题。问题确实出在
ResNet50
层上

上面脚本中的以下代码:

return ResNet50(weights='imagenet',
                include_top=False).predict(preprocess_input(tensor))
返回一个
(1,7,7,2048)
(当然,我不完全理解为什么)。为了避免这个问题,我在参数
pooling=“avg”
中添加了如下内容:

return ResNet50(weights='imagenet',
                include_top=False,
                pooling="avg").predict(preprocess_input(tensor))
相反,它返回一个
(12048)
(同样,我不知道为什么

但是,模型仍然需要一个4-D形状。为了解决这个问题,我在我的
dog\u breed()
函数中添加了以下代码:

print(bottleneck_feature.shape) #returns (1, 2048)
bottleneck_feature = np.expand_dims(bottleneck_feature, axis=0)
bottleneck_feature = np.expand_dims(bottleneck_feature, axis=0)
bottleneck_feature = np.expand_dims(bottleneck_feature, axis=0)
print(bottleneck_feature.shape) #returns (1, 1, 1, 1, 2048) - yes a 5D shape, not 4.
这将返回一个
(1,1,1,1,2048)
。出于某种原因,当我只添加了两个维度时,模型仍然抱怨它是一个3D形状,但当我添加了第三个维度时,它停止了(这很奇怪,我想了解更多关于为什么会这样)

因此,总的来说,我的
dog\u breed()
函数来自:

def dog_breed(img_path):
    #extract bottleneck features
    bottleneck_feature = extract_Resnet50(path_to_tensor(img_path))
    #obtain predicted vector
    predicted_vector = Resnet50_model.predict(bottleneck_feature) #shape error occurs here
    #return dog breed that is predicted by the model
    return dog_names[np.argmax(predicted_vector)]
为此:

def dog_breed(img_path):
    #extract bottleneck features
    bottleneck_feature = extract_Resnet50(path_to_tensor(img_path))
    print(bottleneck_feature.shape) #returns (1, 2048)
    bottleneck_feature = np.expand_dims(bottleneck_feature, axis=0)
    bottleneck_feature = np.expand_dims(bottleneck_feature, axis=0)
    bottleneck_feature = np.expand_dims(bottleneck_feature, axis=0)
    print(bottleneck_feature.shape) #returns (1, 1, 1, 1, 2048) - yes a 5D shape, not 4.
    #obtain predicted vector
    predicted_vector = Resnet50_model.predict(bottleneck_feature) #shape error occurs here
    #return dog breed that is predicted by the model
    return dog_names[np.argmax(predicted_vector)]

同时确保参数
pooling=“avg”
被添加到我对
ResNet50
的调用中。不幸的是,仍然是相同的错误!这是不幸的。然而,问题应该在输入+特征提取部分,因为很明显,平均池层的预期输入比池层的预期输入大。池层可能就是其中之一r本身不是必需的。在文档中,检查另一个可选参数
pooling
,并尝试使用pooling=None,检查在这种情况下extacted特性的输出形状实际上,当包含
input\u shape
时,错误似乎发生了变化。检查输入时,错误现在是
错误:预期的input_3具有形状(244,244,3),但获得具有形状(224,224,3)的数组
。有什么想法吗?我已经想出来了!我将创建一个答案。不幸的是,这没有什么区别,仍然是相同的错误!这是不幸的。但是,问题应该在输入+特征提取部分,因为很明显,平均池层的预期输入比池层的预期输入大。池可能就是其中之一层本身,这不是必需的。在文档中,检查其他可选参数
pooling
,并尝试使用pooling=None,检查在这种情况下extacted特性的输出形状实际上,当包含
input\u shape
时,错误似乎发生了变化。检查输入时,错误现在是
错误:预期输入_3使其具有形状(244,244,3),但获得具有形状(224,224,3)的数组
。有什么想法吗?我已经想出来了!我会给出一个答案。我喜欢这个解决方案,但任何人都可以提供更多关于更改原因和维度原因的信息?我也有同样的问题,但因为我的维度有4个,所以我删除了一个
瓶颈特征=np。展开维度(瓶颈特征,轴=0)
最后一段代码中的一行,效果很好!谢谢,这真的很有帮助!为了稍微清理一下方法,我最后重写了我的代码:
predicted_vector=Resnet50_model.predict(np.expand_dims(np.expand_dims(瓶颈特征,axis=0,axis=0))
我喜欢这个解决方案,但任何人都可以提供更多关于更改原因和维度原因的信息?我也有同样的问题,但因为我的有4个维度,所以我删除了一个