TensorFlow:卷积网络中的尺寸不兼容错误

阿森纳狂热者
import tensorflow as tf
import numpy as np
import scipy as sci
import cv2
import input_data_conv


# Parameters
learning_rate = 0.001
training_iters = 200000
batch_size = 64
display_step = 20
n_classes=101 # number of classes

#Input data and classes
global train_data,train_class,test_data,test_classs,train_i,test_i
test_i, train_i = 0,0
train_data=input_data_conv.train_list_file
train_class=input_data_conv.train_single_classes
test_data=input_data_conv.test_single_frames
test_classs=input_data_conv.test_single_classes


# Network Parameters
n_input = [227, 227, 3 ]# MNIST data input (img shape: 227*227*3)
dropout = 0.5 # Dropout, probability to keep units

# tf Graph input
x = tf.placeholder(tf.float32, [None, 227,227,3])
y = tf.placeholder(tf.float32, [None, n_classes])
keep_prob = tf.placeholder(tf.float32) # dropout (keep probability)


def init_weights(shape):
    return tf.Variable(tf.random_normal(shape, stddev=0.01))

def conv2d(name, l_input, w, b,s):
    return tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(l_input, w, strides=[1, s, s, 1], padding='SAME'),b), name=name)

def max_pool(name, l_input, k,s):
    return tf.nn.max_pool(l_input, ksize=[1, k, k, 1], strides=[1, s, s, 1], padding='SAME', name=name)

def norm(name, l_input, lsize):
    return tf.nn.lrn(l_input, lsize, bias=1.0, alpha=0.0001 / 9.0, beta=0.75, name=name)

def vgg_single_frame(_X, _weights, _biases, _dropout):
    # Reshape input picture
    _X = tf.reshape(_X, shape=[-1, 227, 227, 3])

    conv1 = conv2d('conv1', _X, _weights['wc1'], _biases['bc1'],s=2)
    pool1 = max_pool('pool1', conv1, k=3,s=2)
    norm1 = norm('norm1', pool1, lsize=5)

    conv2 = conv2d('conv2', norm1, _weights['wc2'], _biases['bc2'],s=2)
    pool2 = max_pool('pool2', conv2, k=3,s=2)
    norm2 = norm('norm2', pool2, lsize=5)


    conv3 = conv2d('conv3', norm2, _weights['wc3'], _biases['bc3'],s=2)
    conv4 = conv2d('conv4', conv3, _weights['wc4'], _biases['bc4'],s=2)
    conv5 = conv2d('conv4', conv4, _weights['wc5'], _biases['bc5'],s=2)
    pool5 = max_pool('pool5', conv5, k=3,s=2)

    # Fully connected layer
    dense1 = tf.reshape(norm3, [-1, _weights['wd1'].get_shape().as_list()[0]]) # Reshape conv3 output to fit dense layer input
    dense1 = tf.nn.relu(tf.matmul(dense1, _weights['wd1']) + _biases['bd1'], name='fc6') # Relu activation
    dense1 = tf.nn.dropout(dense1, _dropout)
    dense2 = tf.nn.relu(tf.matmul(dense1, _weights['wd2']) + _biases['bd2'], name='fc7') # Relu activation
    dense2 = tf.nn.dropout(dense2, _dropout)

    # Output, class prediction
    out = tf.matmul(dense2, _weights['out']) + _biases['out']
    return out

weights = {
    'wc1': tf.Variable(tf.random_normal([7, 7, 1, 96])), # 7x7 conv, 1 input, 96 outputs ,stride 2
    'wc2': tf.Variable(tf.random_normal([5, 5, 96, 384])), # 5x5 conv, 32 inputs, 64 outputs
    'wc3': tf.Variable(tf.random_normal([3, 3, 384, 512])),#s 2 ,p a
    'wc4': tf.Variable(tf.random_normal([3, 3, 512, 512])),#s 2, p 1
    'wc5': tf.Variable(tf.random_normal([3, 3, 512, 384])),#s 2, p 1
    'wd1': tf.Variable(tf.random_normal([7*7*64, 4096])), # fully connected, 7*7*64 inputs, 1024 outputs
    'wd2': tf.Variable(tf.random_normal([4096, 4096])), # fully connected, 7*7*64 inputs, 1024 outputs
    'out': tf.Variable(tf.random_normal([4096, n_classes])) # 1024 inputs, 10 outputs (class prediction)
}

biases = {
    'bc1': tf.Variable(tf.random_normal([96])),
    'bc2': tf.Variable(tf.random_normal([384])),
    'bc3': tf.Variable(tf.random_normal([512])),
    'bc4': tf.Variable(tf.random_normal([512])),
    'bc5': tf.Variable(tf.random_normal([384])),
    'bd1': tf.Variable(tf.random_normal([4096])),
    'bd2': tf.Variable(tf.random_normal([4096])),
    'out': tf.Variable(tf.random_normal([n_classes]))
}

def train_next_batch(batch_size):
    temp_data=np.ndarray(shape=(batch_size,227,227,3),dtype=float)
    temp_data=np.ndarray(shape=(batch_size,n_classes),dtype=float)
    for num,x in train_data[train_i:train_i+batch_size]:
        temp_data[num,:,:,:]=cv2.imread(x,1)


pred = vgg_single_frame(x, weights, biases, keep_prob)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
# Evaluate model
correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

# Initializing the variables
init = tf.initialize_all_variables()

with tf.Session() as sess:
    sess.run(init)
    step = 1
    # Keep training until reach max iterations
    while step * batch_size < training_iters:
        batch_xs, batch_ys = train_next_batch(batch_size)
        # Fit training using batch data
        sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys, keep_prob: dropout})
        if step % display_step == 0:
            # Calculate batch accuracy
            acc = sess.run(accuracy, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 1.})
            # Calculate batch loss
            loss = sess.run(cost, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 1.})
            print "Iter " + str(step*batch_size) + ", Minibatch Loss= " + "{:.6f}".format(loss) + ", Training Accuracy= " + "{:.5f}".format(acc)
        step += 1
    print "Optimization Finished!"
    # Calculate accuracy for 256 mnist test images
    print "Testing Accuracy:", sess.run(accuracy, feed_dict={x: mnist.test.images[:256], y: mnist.test.labels[:256], keep_prob: 1.})

我想运行上面的代码并将大小适中的图像馈[227, 227, 3]入此网络。但是,当我尝试建立网络时,出现以下错误:

I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcublas.so locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcudnn.so locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcufft.so locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcurand.so locally
Traceback (most recent call last):
  File "/home/anilil/projects/pycharm-community-5.0.4/helpers/pydev/pydevd.py", line 2411, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/home/anilil/projects/pycharm-community-5.0.4/helpers/pydev/pydevd.py", line 1802, in run
    launch(file, globals, locals)  # execute the script
  File "/media/anilil/Data/charm/Cnn/build_vgg_model.py", line 104, in <module>
    pred = vgg_single_frame(x, weights, biases, keep_prob)
  File "/media/anilil/Data/charm/Cnn/build_vgg_model.py", line 50, in vgg_single_frame
    conv1 = conv2d('conv1', _X, _weights['wc1'], _biases['bc1'],s=2)
  File "/media/anilil/Data/charm/Cnn/build_vgg_model.py", line 38, in conv2d
    return tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(l_input, w, strides=[1, s, s, 1], padding='SAME'),b), name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_nn_ops.py", line 211, in conv2d
    use_cudnn_on_gpu=use_cudnn_on_gpu, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/op_def_library.py", line 655, in apply_op
    op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2042, in create_op
    set_shapes_for_outputs(ret)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1528, in set_shapes_for_outputs
    shapes = shape_func(op)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/common_shapes.py", line 187, in conv2d_shape
    input_shape[3].assert_is_compatible_with(filter_shape[2])
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 94, in assert_is_compatible_with
    % (self, other))
ValueError: Dimensions Dimension(3) and Dimension(1) are not compatible

我认为weights变量weights['wc1']的形状错误,但是我不确定正确的变量是什么。

默里

问题是您的输入图像(中的_X)具有3个通道(大概是红色,绿色和蓝色),而用于图层conv1(中的卷积滤波器)的输入图像_weights['wc1']需要1个输入通道。

解决此问题的方法至少有两种:

  1. 重新定义_weights['wc1']以接受3个输入通道:

    weights = {
        'wc1': tf.Variable(tf.random_normal([7, 7, 3, 96])), # ...
        # ...
    }
    
  2. _X使用tf.image.rgb_to_grayscale()op将输入图像转换为具有1个输入通道

    _X = tf.image.rgb_to_grayscale(_X)
    

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

OpenCV 2.4.12中的不兼容错误

来自分类Dev

ArrayList中的类型不兼容错误,但都为String

来自分类Dev

Tensorflow尺寸在CNN中不兼容

来自分类Dev

本地类不兼容错误

来自分类Dev

Tensorflow - 使用 tf.losses.hinge_loss 导致形状不兼容错误

来自分类Dev

Matlab卷积(尺寸不匹配错误)

来自分类Dev

在Java中参数化可迭代包装器:类型不兼容错误

来自分类Dev

二叉树中的Java泛型,类型不兼容错误

来自分类Dev

R:VGAM中不兼容的尺寸错误vglm功能

来自分类Dev

仅更改方法时的本地类不兼容错误

来自分类Dev

Haskell版本的阴阳拼图:某种不兼容错误

来自分类Dev

播放2.3收到DefaultReads的不兼容错误

来自分类Dev

指针到指针的类型不兼容错误

来自分类Dev

控制台中的IE文档模式不兼容错误?

来自分类Dev

播放2.3收到DefaultReads的不兼容错误

来自分类Dev

查看表数据时出现不兼容错误消息

来自分类Dev

HSQLDB 2.3.4 及更高版本中是否存在客户端-服务器不兼容错误?

来自分类Dev

keras 中不兼容的输入数据错误,尺寸不匹配 ValueError

来自分类Dev

LSTM Keras层中的尺寸不兼容

来自分类Dev

调用java.util.ArrayList.toArray方法时,scala参数类型不兼容错误

来自分类Dev

现在在使用if else语句时出现不兼容错误

来自分类Dev

FragmentManager上的类型不兼容错误。尝试在动作栏按钮上启动片段单击

来自分类Dev

仅在Visual Studio中出现C ++向量迭代器不兼容错误

来自分类Dev

mplot3d python不兼容尺寸错误

来自分类Dev

与numpy数组不兼容的尺寸的python图错误

来自分类Dev

Swift-奇怪的编码兼容错误

来自分类Dev

寻找解决FIPS兼容错误的方法

来自分类Dev

可重用的Tensorflow卷积网络

来自分类Dev

R CVXR矩阵乘法%*%mul_dims_promote(lh_dim,rh_dim)中的错误:不兼容的尺寸

Related 相关文章

  1. 1

    OpenCV 2.4.12中的不兼容错误

  2. 2

    ArrayList中的类型不兼容错误,但都为String

  3. 3

    Tensorflow尺寸在CNN中不兼容

  4. 4

    本地类不兼容错误

  5. 5

    Tensorflow - 使用 tf.losses.hinge_loss 导致形状不兼容错误

  6. 6

    Matlab卷积(尺寸不匹配错误)

  7. 7

    在Java中参数化可迭代包装器:类型不兼容错误

  8. 8

    二叉树中的Java泛型,类型不兼容错误

  9. 9

    R:VGAM中不兼容的尺寸错误vglm功能

  10. 10

    仅更改方法时的本地类不兼容错误

  11. 11

    Haskell版本的阴阳拼图:某种不兼容错误

  12. 12

    播放2.3收到DefaultReads的不兼容错误

  13. 13

    指针到指针的类型不兼容错误

  14. 14

    控制台中的IE文档模式不兼容错误?

  15. 15

    播放2.3收到DefaultReads的不兼容错误

  16. 16

    查看表数据时出现不兼容错误消息

  17. 17

    HSQLDB 2.3.4 及更高版本中是否存在客户端-服务器不兼容错误?

  18. 18

    keras 中不兼容的输入数据错误,尺寸不匹配 ValueError

  19. 19

    LSTM Keras层中的尺寸不兼容

  20. 20

    调用java.util.ArrayList.toArray方法时,scala参数类型不兼容错误

  21. 21

    现在在使用if else语句时出现不兼容错误

  22. 22

    FragmentManager上的类型不兼容错误。尝试在动作栏按钮上启动片段单击

  23. 23

    仅在Visual Studio中出现C ++向量迭代器不兼容错误

  24. 24

    mplot3d python不兼容尺寸错误

  25. 25

    与numpy数组不兼容的尺寸的python图错误

  26. 26

    Swift-奇怪的编码兼容错误

  27. 27

    寻找解决FIPS兼容错误的方法

  28. 28

    可重用的Tensorflow卷积网络

  29. 29

    R CVXR矩阵乘法%*%mul_dims_promote(lh_dim,rh_dim)中的错误:不兼容的尺寸

热门标签

归档