python-3.x - 从 Resnet 提取的特征不正确的问题
问题描述
重要编辑
好的,所以我对之前的问题做了一个完整的修改,但仍然是同样的问题,但现在代码更加简洁易读。我正在做的是使用 keras.preprocessing 图像库从文件中读取图像。然后使用 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。这不适用于 categorical_crossentropy,因为它需要标签的 [0,1] 格式。忘记了我的基本猫狗分类器。
推荐阅读
- reactjs - 用redux中的dispatch()和connect()解释这个问题?
- flutter - 何时使用 Provider.of
与消费者 在颤振中 - javascript - 有没有办法在 JavaScript 中生成一个随机数,然后用它来生成另一个随机数?
- ruby-on-rails - Ruby GraphQL 返回不同的类型
- ecmascript-6 - 在 ECMAScript 规范中的什么地方指定了等待表达式的返回值?
- laravel - 在 Laravel 中使用 isDirty
- macos - 来自 `SFSpeechRecognizer` `recognitionTask` 在约 30 秒标记处的不需要的回调
- reactjs - React - 在 PRODUCTION 构建中获取“.map 不是函数”,但在常规开发构建中没有?
- postgresql - 有没有办法将原始数据输入 PostgreSQL 以复制除某些列之外的行?
- python - 在 Python 中从 json 创建新数组