关于使用keras进行多标签分类

时间:2017-05-25 09:03:00

标签: tensorflow keras

我尝试训练多标签分类器,我在输出层使用sigmoid单位然后使用“binary_crossentrpy”丢失。目前的问题是训练和测试的结果是理想的,损失和准确度的值都很大。但是当我使用model.predict()预测标签时,输出与真实标签值不匹配。如何更改代码来解决它? 训练集和测试集的形状为(-1,1,300,300),目标标签的形状为(-1,478),总共有478个。 我的完整代码:

import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Activation, Convolution2D, MaxPooling2D, Flatten, Dropout
from keras.optimizers import Adam

X = np.load('./data/X_train.npy')
y = np.load('./data/Y_train.npy')
X_train, y_train = X[:2000], y[:2000]
X_test, y_test = X[2000:], y[2000:]

model = Sequential()

model.add(Convolution2D(nb_filter=32, nb_row=5, nb_col=5, padding='same', input_shape=(1, 300, 300)))
model.add(Activation('relu'))

model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))

model.add(Dropout(0.25))

model.add(Convolution2D(64, 5, 5, padding='same'))
model.add(Activation('relu'))

model.add(MaxPooling2D(pool_size=(2, 2), border_mode='same'))

model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))

model.add(Dropout(0.5))

model.add(Dense(478))
model.add(Activation('sigmoid'))

model.compile(optimizer=Adam(lr=1e-4), loss='binary_crossentropy', metrics=['accuracy'])

print('Training ------------')
model.fit(X_train, y_train, epochs=5, batch_size=300, validation_data=(X_test, y_test), verbose=1)

model.save('model.h5')

你能帮我找一个解决方案吗?谢谢!

1 个答案:

答案 0 :(得分:0)

您是否尝试根据阈值过滤值?

pred = model.predict(x_test)
pred[pred>=0.5] = 1
pred[pred<0.5] = 0
print(pred[0:5])