Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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 3.x 从Resnet提取的特征形状不正确的问题_Python 3.x_Keras_Deep Learning_Computer Vision - Fatal编程技术网

Python 3.x 从Resnet提取的特征形状不正确的问题

Python 3.x 从Resnet提取的特征形状不正确的问题,python-3.x,keras,deep-learning,computer-vision,Python 3.x,Keras,Deep Learning,Computer Vision,重要编辑 好的,我对前面的问题做了一个完整的修改,但仍然是同一个问题,但是现在代码更加简洁易读。我正在做的是使用keras.preprocessing image lib从文件中读取图像。然后使用keras img_to_arrar函数将其转换为数组。我将其解析为三个数组:锚数组、图像数组和标签数组。然后,我通过我的模型将其输入,这给了我一个奇怪的反馈: Error when checking target: expected Act_3 to have shape (2,) but got a

重要编辑

好的,我对前面的问题做了一个完整的修改,但仍然是同一个问题,但是现在代码更加简洁易读。我正在做的是使用keras.preprocessing image lib从文件中读取图像。然后使用keras img_to_arrar函数将其转换为数组。我将其解析为三个数组:锚数组、图像数组和标签数组。然后,我通过我的模型将其输入,这给了我一个奇怪的反馈:

Error when checking target: expected Act_3 to have shape (2,) but got array with shape (1,)
为什么它会从形状2下降到形状1看起来它丢失了所有的数据

以下是完整的代码:

def read_in_images(array):
    input_1_array = []
    input_2_array = []
    labels = []
    for item in array:
      a = item[0]
      i = item[1]
      l = item[2]
      img_a = image.load_img(a, target_size=(224, 224))
      img_i = image.load_img(i, target_size=(224, 224))
      a_a = image.img_to_array(img_a)
      i_a = image.img_to_array(img_i)
      input_1_array.append(a_a)
      input_2_array.append(i_a)
      labels.append(l)
    return np.array(input_1_array), np.array(input_2_array), np.array(labels)

train_x1, train_x2, train_y = read_in_images(sm_train)
val_x1, val_x2, val_y = read_in_images(sm_val)
test_x1, test_x2, test_y = read_in_images(sm_test)
print(train_x1.shape) # give (50, 224, 224, 3)
print(val_x1.shape) # gives (15, 224, 224, 3)
print(test_x1.shape) # (30, 224, 224, 3) which is what I want

resnet_model = resnet50.ResNet50(weights="imagenet", include_top=True)
input_1 = Input(shape=(224,224,3))
input_2 = Input(shape=(224,224,3))

proccess_1 = resnet_model(input_1)
proccess_2 = resnet_model(input_2)
merged = Concatenate(axis=-1)([proccess_1, proccess_2])

fc1 = Dense(512, kernel_initializer="glorot_uniform", name="Den_1")(merged)
fc1 = Dropout(0.2)(fc1)
fc1 = Activation("relu", name = "Act_1")(fc1)

fc2 = Dense(128, kernel_initializer="glorot_uniform", name="Den_2")(fc1)
fc2 = Dropout(0.2)(fc2)
fc2 = Activation("relu", name = "Act_2")(fc2)

pred = Dense(2, kernel_initializer="glorot_uniform", name="Den_3")(fc2)
pred = Activation("softmax", name = "Act_3")(pred)
model = Model(inputs=[input_1, input_2], outputs=pred)
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])

history = model.fit([train_x1, train_x2], train_y,
      batch_size=32,
      epochs=10,
      verbose = 1,
      validation_data=([val_x1, val_x2], val_y))

我发现了这个新版本的问题所在。我没有以[0,1]格式制作标签,因为它是0或1。这不适用于分类交叉熵,因为它需要标签的[0,1]格式。忘了我的基本猫狗分类器

我发现了这个新版本的问题所在。我没有以[0,1]格式制作标签,因为它是0或1。这不适用于分类交叉熵,因为它需要标签的[0,1]格式。忘了我的基本猫狗分类器

从您的代码示例中,我看不出resnet是如何进入图片的。我错过什么了吗?它似乎没有连接到其他设置。为什么要使用make_数据方法??我认为这是一个问题。您可以直接将图像发送到resnet,如有必要,可以使用“重塑”和“展开尺寸”来对齐尺寸。resnet用于在“创建”功能方法中创建功能。我重述了这个问题,以便现在可以很容易地看到它是如何工作的。但是,仍然存在一些问题。从您的代码示例中,我看不出resnet是如何进入图片的。我错过什么了吗?它似乎没有连接到其他设置。为什么要使用make_数据方法??我认为这是一个问题。您可以直接将图像发送到resnet,如有必要,可以使用“重塑”和“展开尺寸”来对齐尺寸。resnet用于在“创建”功能方法中创建功能。我重述了这个问题,以便现在可以很容易地看到它是如何工作的。但仍然存在一些问题。