我正在使用Python。我在Keras中创建了一个模型,在那里我拍摄了VGG-16并删除了最上面的几层。然后,我插入了一个?x1x100图层。最终预测是具有6个标记的多标记预测。 为此,我将输出1x100张量发送到一个定制函数中,该函数将其转换为图形模型空间,然后将其传递给Random-Forest,然后重新解释为多标签预测。我使用从模型的早期版本中提取的?x1x100向量进行了测试,该向量不包括增强功能和改进功能。
但是,我需要对1x100向量进行改进,因为我实际上是在提取它们以比较图像。因此,我需要该模型能够与包含的自定义图形模型一起进行训练。
我遇到的问题是,我需要先将Keras模型的输出转换为numpy数组,然后再将其发送到分类器模型中,该模型已在我的介绍之外进行了训练。我的设置方法如下,但收到错误消息。
graph_builder = LabelCooccurrenceGraphBuilder(weighted = True, include_self_edges = False)
# Set up the ensemble metaclassifier
openne_line_params = dict(batch_size=1000, order=3)
embedder = OpenNetworkEmbedder(graph_builder, 'LINE', dimension=5*train_labels.shape[1], aggregation_function = 'add', normalize_weights=True, param_dict = openne_line_params)
# Define using parameters
reg = RandomForestRegressor(max_depth=max_depth, max_features=max_features, min_samples_split=min_samples_split, min_samples_leaf=min_samples_leaf, bootstrap=bootstrap, n_estimators=n_estimators)
# Create classifier using those values
classifier = EmbeddingClassifier( embedder=embedder, regressor=reg, classifier=MLkNN(k=5), require_dense=[False, False])
# Fit to the train set
classifier.fit(feature_array_train, train_labels)
# Define custom loss and metric functions
def hamming_dist(classifier, sess):
def hamm_dist(y_true, y_pred):
with sess.as_default():
feature_vectors = y_pred.eval()
pred_labels = classifier.predict(feature_vectors).toarray()
# Convert the predictions into tensors
y_true = tf.cast(y_true,dtype=tf.float32)
y_pred = tf.cast(pred_labels,dtype=tf.float32)
return K.mean(K.sum(K.abs(y_true-y_pred),axis=1))
return hamm_dist
def manual_binary_crossentropy(classifier, sess):
def manual_bin_ce(y_true, y_pred):
with sess.as_default():
feature_vectors = y_pred.eval()
# Evaluate predicted labels through classifier
pred_labels = classifier.predict(feature_vectors).toarray()
# Convert the predictions into tensors
y_true = tf.cast(y_true,dtype=tf.float32)
y_pred = tf.cast(pred_labels,dtype=tf.float32)
return losses.binary_crossentropy(y_true, y_pred, from_logits=False, label_smoothing=0)
return manual_bin_ce
# Define model modified from VGG-16
sess = K.get_session()
with sess.as_default():
model = VGG16(weights = "imagenet", include_top=False, input_shape = img_shape)
# Add our custom output layers
x = model.output
x = Flatten()(x)
x = Dense(5000, activation="relu")(x)
# Note predictions here are fed into graph model later
predictions = Dense(100, activation="relu")(x)
# Define model
model=Model(input=model.input,output=predictions)
opt=optimizers.Adam(lr=learning_rate, beta_1=0.9)
model.compile(loss=[manual_binary_crossentropy(classifier=classifier, sess=sess)], optimizer=opt, metrics=[hamming_dist(classifier=classifier, sess=sess)])
运行此错误时,收到的错误如下:
tensorflow.python.framework.errors_impl.InvalidArgumentError:2根 发现错误。
(0)无效的参数:必须输入占位符张量的值 dtype浮点型且形状为[?,100,100,3] [[{{node input_1}}]]的'input_1'
(1)无效的参数:必须输入占位符张量的值 'input_1',其dtype类型为float,形状为[?,100,100,3] [[{{node input_1}}]]> [[dense_2 / Relu / _189]]
0次成功操作。忽略0个派生错误。
我什至会以正确的方式这样做吗?我在网上看过,但没有人试图做同样的事情。您的任何想法都会对您有所帮助。谢谢。