ValueError:两个形状中的尺寸0必须相等,但是对于'分配' (op:' Assign')输入形状:[5,5,3,30],[30,1,5,5]

时间:2017-07-15 19:17:00

标签: python-2.7 numpy deep-learning keras python-imaging-library

我正在研究简单的python代码,涉及使用keras使用mnist标准数据集进行深度学习。

这是我工作的代码:

import numpy
import cv2
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
from keras import backend as K
import sys
from keras.preprocessing.image import ImageDataGenerator

K.set_image_dim_ordering('th')

img_width, img_height =28,28

def larger_model():
    # create model
    model = Sequential()
    model.add(Conv2D(30, (5, 5), input_shape=(3, 28, 28), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Conv2D(15, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.2))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dense(50, activation='relu'))
    model.add(Dense(10, activation='softmax')) #num_classes=10
    # Compile model
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

img=cv2.imread(sys.argv[1])
img=cv2.resize(img,(img_width,img_height))
model=larger_model()

model.load_weights('./mnist.h5')

arr=numpy.array(img).reshape((3,img_width,img_height)).astype('float32') #

#arr=numpy.expand_dims(arr,axis=0)
arr/=255    
prediction=model.predict(arr)[0]          #

bestClass=''
bestConf= -1

for n in [0,1,2,3,4,5,6,7,8,9]:
    if prediction[n] > bestConf:
        bestClass = str[n]
        bestConf = prediction[n]

print " I think the digit is" + bestClass+ "with"+ str(bestConf*100) +"% confidence !"

这是出现的错误:

    Traceback (most recent call last):
  File "predict.py", line 39, in <module>
    model.load_weights('./mnist.h5')
  File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 720, in load_weights
    topology.load_weights_from_hdf5_group(f, layers)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 3048, in load_weights_from_hdf5_group
    K.batch_set_value(weight_value_tuples)
  File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 2183, in batch_set_value
    assign_op = x.assign(assign_placeholder)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variables.py", line 516, in assign
    return state_ops.assign(self._variable, value, use_locking=use_locking)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/state_ops.py", line 271, in assign
    validate_shape=validate_shape)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_state_ops.py", line 45, in assign
    use_locking=use_locking, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
    op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2508, in create_op
    set_shapes_for_outputs(ret)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1873, in set_shapes_for_outputs
    shapes = shape_func(op)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1823, in call_with_requiring
    return call_cpp_shape_fn(op, require_shape_fn=True)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn
    debug_python_shape_fn, require_shape_fn)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/common_shapes.py", line 676, in _call_cpp_shape_fn_impl
    raise ValueError(err.message)
ValueError: Dimension 0 in both shapes must be equal, but are 5 and 30 for 'Assign' (op: 'Assign') with input shapes: [5,5,3,30], [30,1,5,5].

请帮助我,因为我在这里被困了好多天。谢谢你!

0 个答案:

没有答案