我必须构建一个采用形状输入的分类器:[batch,frames,height,width,channel]。 这里的帧数会有所不同,所有维度都是固定的。
这是我的网络:
height = 90
width = 120
channel = 3
num_classes = 20
batch = 2
def my_model(frames, data, label):
XR = tf.reshape(data, [batch, frames, height*width*channel])
h = rnn_layer(XR)
print("h: ", h)
prediction = Dense(output_dim=num_classes, activation='softmax', kernel_regularizer=l2(alpha))(h)
print("logits: ", prediction)
with tf.name_scope('Loss'):
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=label))
print("cost: ", cost)
with tf.name_scope('Optimizer'):
optimizer = tf.train.AdamOptimizer(learning_rate=0.0001)
train_op = optimizer.minimize(cost)
print("train_op: ", train_op)
with tf.name_scope('Accuracy'):
# Test model
correct_prediction = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y, 1))
# Calculate accuracy
accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
return accuracy
你可以忽略rnn_layer(), Dense()
及其参数,因为它们是RNN和完全连接层的实现,并且工作正常。
with tf.Session() as sess:
with tf.variable_scope("frames_1") as scope:
tf.global_variables_initializer().run()
frames = 2
X = tf.placeholder('float', [None, frames, height, width, channel], name='InputData')
Y = tf.placeholder('float', [None, num_classes], name='LabelData')
input_data, label_data = video_frames_5d_train[0:batch, 0:frames], category_label_train_one_not[0:batch]
result = my_model(frames, X, Y)
print("result frames_1: ", result)
scope.reuse_variables()
acc = sess.run(result, feed_dict={X: input_data, Y: label_data})
print("Accuracy: ", acc)
with tf.variable_scope("frames_2", reuse=tf.AUTO_REUSE):
frames = 5
X = tf.placeholder(tf.float32, [None, frames, height, width, channel], name='InputData')
Y = tf.placeholder(tf.float32, [None, num_classes], name='LabelData')
input_data, label_data = video_frames_5d_train[0:batch, 0:frames], category_label_train_one_not[0:batch]
result = my_model(frames, X, Y)
print("result frames_2: ", result)
acc = sess.run(result, feed_dict={X: input_data, Y: label_data})
print("Accuracy: ", acc)
在上面的代码中,frames
的值会有所不同,其余值相同。我想为它们分别优化frames
和计算accuracy
,所以我使用的是变量得分。此处video_frames_5d_train
包含数据。
这是我执行时打印的内容:
h: Tensor("frames_1/tt_lstm_1/TensorArrayReadV3:0", shape=(2, 625), dtype=float32)
logits: Tensor("frames_1/dense_1/Softmax:0", shape=(2, 20), dtype=float32)
cost: Tensor("frames_1/Loss/Mean:0", shape=(), dtype=float32)
c:\anaconda2\envs\py3\lib\site-packages\ipykernel_launcher.py:26: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(units=20, activation="softmax", kernel_regularizer=<keras.reg...)`
train_op: name: "frames_1/Optimizer/Adam"
op: "NoOp"
input: "^frames_1/Optimizer/Adam/update_frames_1/tt_lstm_1/kernel/ApplyAdam"
input: "^frames_1/Optimizer/Adam/update_frames_1/tt_lstm_1/bias/ApplyAdam"
input: "^frames_1/Optimizer/Adam/update_frames_1/tt_lstm_1/recurrent_kernel/ApplyAdam"
input: "^frames_1/Optimizer/Adam/update_frames_1/dense_1/kernel/ApplyAdam"
input: "^frames_1/Optimizer/Adam/update_frames_1/dense_1/bias/ApplyAdam"
input: "^frames_1/Optimizer/Adam/Assign"
input: "^frames_1/Optimizer/Adam/Assign_1"
result frames_1: Tensor("frames_1/Accuracy/Mean:0", shape=(), dtype=float32)
所以,它成功执行到print("result frames_1: ", result)
,但后来我收到错误:
FailedPreconditionError: Attempting to use uninitialized value frames_1/dense_1/kernel
[[Node: frames_1/dense_1/kernel/read = Identity[T=DT_FLOAT, _class=["loc:@frames_1/dense_1/kernel"], _device="/job:localhost/replica:0/task:0/device:GPU:0"](frames_1/dense_1/kernel)]]
[[Node: frames_1/Accuracy/Mean/_13 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_317_frames_1/Accuracy/Mean", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
最后,我想为不同的输入尺寸创建一个模型并对其进行优化并分别跟踪精度。有办法吗?
由于