python - 使用opencv和tensorflow时如何修复 "ValueError: An initializer for variable conv2d/kernel of is required"

标签 python opencv tensorflow machine-learning conv-neural-network

我正在编写一个应该使用 tensorflow 和 opencv 的程序 使用卷积神经网络执行手语识别。 我使用了 MNIST 分类器的示例代码,可以找到 here和 我试图以这样的方式改变它,我可以使用 opencv 来加载训练 图像,然后将相机捕获作为 CNN 的输入。 现在,我在训练模型时遇到了问题 出错了:

ValueError:需要变量 conv2d/kernel 的初始值设定项

可以找到完整的错误日志here

正在使用的框架版本:

  • Tensorflow r 1.7.1
  • OpenCV 4.0.0
  • python 3.6.5
  • NumPy 1.14.2

可以看到应该为网络准备训练数据的代码 在第一个代码片段中。它只是读取一堆不同的 jpg 照片 手势,调整这些图像的大小并将其放入 numpy 数组中

def prepareTrainingData(trainingLetterMaxId, training_image_size):
    training_images = []
    training_labels = []
    for letter in training_letters:
        for i in range(0, trainingLetterMaxId):
            read_image = cv2.imread('/home/radkye/Documents/ASLRecognizer/images/'
            + letter + '/' + letter + '_' + str(i) + '.jpg', 0)
            resized = np.array(cv2.resize(read_image, (training_image_size, training_image_size)))
            flattened = resized.ravel()
            image = tf.cast(flattened, tf.float32)
            training_images.append(image)
            net_output = np.zeros(len(training_letters))
            net_output[letters_to_indices_map[letter]] = 1
            training_labels.append(net_output)

    result = np.array(training_images)
    labels_result = np.array(training_labels)
    return result, labels_result

training_data, training_labels = prepareTrainingData(100, 60)
train_labels_int = np.asarray(training_labels, dtype=np.int32)

mnist_classifier = tf.estimator.Estimator(
    model_fn=cnn.cnn_model_fn,
    model_dir="/home/radkye/Documents/studia/ASLRecognizer_AutoTestVersion/asl_cnn_model")

tensors_to_log = {"probabilities": "softmax_tensor"}
logging_hook = tf.train.LoggingTensorHook(
    tensors=tensors_to_log, every_n_iter=50)

train_input_fn = tf.estimator.inputs.numpy_input_fn(
    x={"x": training_data},
    y=train_labels_int,
    batch_size=3600,
    num_epochs=None,
    shuffle=True)

mnist_classifier.train(
    input_fn=train_input_fn,
    steps=20000,
    hooks=[logging_hook])

cnn_model_fn 定义为:

def cnn_model_fn(features, labels, mode):

    input_layer = tf.reshape(features["x"], [-1, 60, 60, 1])

    conv1 = tf.layers.conv2d(
        inputs=input_layer,
        filters=64,
        kernel_size=[5, 5],
        padding="same",
        activation=tf.nn.relu)

    pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)

    conv2 = tf.layers.conv2d(
        inputs=pool1,
        filters=64,
        kernel_size=[5, 5],
        padding="same",
        activation=tf.nn.relu)

    pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)
    pool2_flat = tf.reshape(pool2, [-1, 12 * 12 * 64])
    dense = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu)

    dropout = tf.layers.dropout(
        inputs=dense, rate=0.4, training=mode == tf.estimator.ModeKeys.TRAIN)

    logits = tf.layers.dense(inputs=dropout, units=24)

拜托,有人可以帮助我识别数据可能存在的问题吗 我传递给 CNN 模型的结构?问题可能出在我准备训练数据的方式上,这可以在第一个代码片段中看到。 我对 tensorflow 还不是很熟练。

否则,也许有人有任何使用 opencv 和 tensorflow 来创建 CNN 的教程或示例。我没找到东西 我需要这种方式。

如有任何帮助,我将不胜感激。 提前谢谢你。

最佳答案

[-1, 12 * 12 * 64] - 给定 padding 和 maxpool 层,这应该是 [-1, 15 * 15 * 64],因为60/2/2 = 15

也就是说,我不确定这是实际问题还是唯一问题,因为我没有办法重现您的问题。

关于python - 使用opencv和tensorflow时如何修复 "ValueError: An initializer for variable conv2d/kernel of is required",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50314634/

相关文章:

java - 断言失败。 convertPointsFromHomogenous

python - 具有噪声损失函数的 SciPy 优化

python - Tensorflow:无法共享密集/内核 - ValueError:尝试共享变量密集/内核,但指定形状 (100, 160) 并找到形状 (9, 100)

tensorflow - 训练 CNN 后,其输出是否应该是确定性的?

python - 检索 Python 服务器端设置的 cookie

python - 等待子进程的最后一行

c++ - cvMat图像在OpenCV中的显示

python - 如何通过列表枚举第一个 "index"报告为 1? ( python 2.4)

jquery - 如何访问views.py中ajax调用发送的数据 python django

android - opencv人脸检测示例