私はKerasとTensorflowを初めて使用しています。私は深い学習を使用して顔認識プロジェクトに取り組んでいます。このコード(出力はsoftmaxの層の出力)を使用して出力として件名のクラスラベルを取得していますが、精度は100クラスのカスタムデータセットの97.5%です。kerasとtensorflow backendを使用してnumpy配列として密集層の出力を取得するにはどうすればよいですか?
しかし、私は特徴ベクトル表現に興味があるので、ネットワークを介してテスト画像を渡して、softmax(最後の層)の前の活性化された密な層から出力を抽出したいと思います。私はKerasのドキュメントを参照しましたが、何も私のために動作するように見えませんでした。誰も助けてください密集層活性化からの出力を抽出し、numpy配列として保存する方法を助けてください?前もって感謝します。
class Faces:
@staticmethod
def build(width, height, depth, classes, weightsPath=None):
# initialize the model
model = Sequential()
model.add(Conv2D(100, (5, 5), padding="same",input_shape=(depth, height, width), data_format="channels_first"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2),data_format="channels_first"))
model.add(Conv2D(100, (5, 5), padding="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), data_format="channels_first"))
# 3 set of CONV => RELU => POOL
model.add(Conv2D(100, (5, 5), padding="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2),data_format="channels_first"))
# 4 set of CONV => RELU => POOL
model.add(Conv2D(50, (5, 5), padding="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2),data_format="channels_first"))
# 5 set of CONV => RELU => POOL
model.add(Conv2D(50, (5, 5), padding="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), data_format="channels_first"))
# 6 set of CONV => RELU => POOL
model.add(Conv2D(50, (5, 5), padding="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), data_format="channels_first"))
# set of FC => RELU layers
model.add(Flatten())
#model.add(Dense(classes))
#model.add(Activation("relu"))
# softmax classifier
model.add(Dense(classes))
model.add(Activation("softmax"))
return model
ap = argparse.ArgumentParser()
ap.add_argument("-l", "--load-model", type=int, default=-1,
help="(optional) whether or not pre-trained model should be loaded")
ap.add_argument("-w", "--weights", type=str,
help="(optional) path to weights file")
args = vars(ap.parse_args())
path = 'C:\\Users\\Project\\FaceGallery'
image_paths = [os.path.join(path, f) for f in os.listdir(path)]
images = []
labels = []
name_map = {}
demo = {}
nbr = 0
j = 0
for image_path in image_paths:
image_pil = Image.open(image_path).convert('L')
image = np.array(image_pil, 'uint8')
cv2.imshow("Image",image)
cv2.waitKey(5)
name = image_path.split("\\")[4][0:5]
print(name)
# Get the label of the image
if name in demo.keys():
pass
else:
demo[name] = j
j = j+1
nbr =demo[name]
name_map[nbr] = name
images.append(image)
labels.append(nbr)
print(name_map)
# Training and testing data split ratio = 60:40
(trainData, testData, trainLabels, testLabels) = train_test_split(images, labels, test_size=0.4)
trainLabels = np_utils.to_categorical(trainLabels, 100)
testLabels = np_utils.to_categorical(testLabels, 100)
trainData = np.asarray(trainData)
testData = np.asarray(testData)
trainData = trainData[:, np.newaxis, :, :]/255.0
testData = testData[:, np.newaxis, :, :]/255.0
opt = SGD(lr=0.01)
model = Faces.build(width=200, height=200, depth=1, classes=100,
weightsPath=args["weights"] if args["load_model"] > 0 else None)
model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
if args["load_model"] < 0:
model.fit(trainData, trainLabels, batch_size=10, epochs=300)
(loss, accuracy) = model.evaluate(testData, testLabels, batch_size=100, verbose=1)
print("Accuracy: {:.2f}%".format(accuracy * 100))
if args["save_model"] > 0:
model.save_weights(args["weights"], overwrite=True)
for i in np.arange(0, len(testLabels)):
probs = model.predict(testData[np.newaxis, i])
prediction = probs.argmax(axis=1)
image = (testData[i][0] * 255).astype("uint8")
name = "Subject " + str(prediction[0])
if prediction[0] in name_map:
name = name_map[prediction[0]]
cv2.putText(image, name, (5, 20), cv2.FONT_HERSHEY_PLAIN, 1.3, (255, 255, 255), 2)
print("Predicted: {}, Actual: {}".format(prediction[0], np.argmax(testLabels[i])))
cv2.imshow("Testing Face", image)
cv2.waitKey(1000)
を中間モデルを定義し、それを実行することができます
model.add(Dense(xx, name='my_dense'))
私はこの出力を得た: *テンソル( "dense_1/BiasAdd:0"、形状=( ?、442)、dtype = float32)* しかし、入力画像の特徴表現であるnumpy配列を出力する必要があります。 – TheBiometricsGuy
上記の例では、Xは数字の配列(つまり、実際のデータ)である必要があります。 keras model.predict()関数は、Tensorflowグラフを介してそのデータを処理し、numpyの配列を返します。 「Tensor」タイプは、グラフを作成するために使用される内部変数です。これはmodel.predict()の出力としては見られません。 – bivouac0