简体   繁体   English

ValueError:两个形状中的尺寸2必须相等,但分别为3和32

[英]ValueError: Dimension 2 in both shapes must be equal, but are 3 and 32

I am currently studying Tensorflow. 我目前正在研究Tensorflow。 I used a pre-trained model for prediction through the Django app, But during the prediction, I got the error, Please help me to resolve the error. 我通过Django应用程序使用了预先训练的模型进行预测,但是在预测过程中出现错误,请帮助我解决错误。

def alpha_to_color(image, color=(255, 255, 255)):
        x = np.array(image)
        r, g, b, a = np.rollaxis(x, axis=-1)
        r[a == 0] = color[0]
        g[a == 0] = color[1]
        b[a == 0] = color[2]
        x = np.dstack([r, g, b, a])
        return Image.fromarray(x, 'RGBA')

    def preprocess(data):
        # dimensions of our images.
        img_width, img_height = 250, 250
        dataUrlPattern = re.compile('data:image/(png|jpeg);base64,(.*)$')
        imgb64 = dataUrlPattern.match(data).group(2)
        if imgb64 is not None and len(imgb64) > 0:
            data= base64.b64decode(imgb64)
            im1 = Image.open(BytesIO(data))
            im1 = alpha_to_color(im1)
            im1=im1.convert('RGB')
        im1= im1.resize((250,250))
        print("[INFO] loading and preprocessing image...")
        image = img_to_array(im1)
        image = image.reshape((1,) + image.shape)  # this is a Numpy array with shape (1, 3, 250,250)
        test_ob = ImageDataGenerator(rescale=1./255)
        X=[]
        for batch in test_ob.flow(image, batch_size=1):
            X= batch
            break
        return X

    def build_model():
        model = Sequential()
        model.add(Conv2D(32, (3, 3), input_shape=(250, 250, 3)))
        model.add(Activation('relu'))
        model.add(MaxPooling2D(pool_size=(2, 2)))

        model.add(Conv2D(32, (3, 3)))
        model.add(Activation('relu'))
        model.add(MaxPooling2D(pool_size=(2, 2)))

        model.add(Conv2D(64, (3, 3)))
        model.add(Activation('relu'))
        model.add(MaxPooling2D(pool_size=(2, 2)))

        model.add(Flatten())
        model.add(Dense(64))
        model.add(Activation('relu'))
        #model.add(Dropout(0.5))
        model.add(Dense(250))
        model.add(Activation('sigmoid'))

        model.compile(loss='categorical_crossentropy',
                      optimizer='adam',
                      metrics=['accuracy'])
        module_dir = os.path.dirname(__file__)  # get current directory
        file_path = os.path.join(module_dir, 'bestWeight.hdf5')
        model.load_weights(file_path)
        return model
    def load_labels():
        module_dir = os.path.dirname(__file__)  # get current directory
        file_path = os.path.join(module_dir, 'labels.csv')
        df = pd.read_csv(file_path,
            header=0)
        target_names = df['Category'].tolist()
        return target_names

    def predict_labels(data):
        model = build_model()
        image = preprocess(data)
        target_names = load_labels()
        encoder = LabelEncoder()
        encoder.fit(target_names)
        pL = model.predict(image)
        prob = model.predict_proba(image)

        p= np.argsort(pL, axis=1)
        n1 = (p[:,-4:]) #gives top 5 labels
        pL_names = (encoder.inverse_transform(n1))
        pL_names = pL_names[0]

        p= np.sort(prob, axis=1)
        convertperc = [stats.percentileofscore(p[0], a, 'rank') for a in p[0]]
        n = (convertperc[-4:]) #gives top 5 probabilities perc
        prob_values = (p[:,-4:])
        prob_single_values = prob_values[0]
        return zip(pL_names,n,prob_single_values)

The code give this error 代码给出了这个错误

ValueError: Dimension 2 in both shapes must be equal, but are 3 and 32. Shapes are [3,3,3,32] and [3,3,32,3]. for 'Assign' (op: 'Assign') with input shapes: [3,3,3,32], [3,3,32,3].

This error occurs when running the line for cross_entropy. 在为cross_entropy行时,会发生此错误。 I don't understand why this is happing, if you need any more information I would be happy to give it to you. 我不明白为什么会这样,如果您需要更多信息,我很乐意为您提供。 Here is my compilation log 这是我的编译日志

Traceback (most recent call last):
  File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1576, in _create_c_op
    c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Dimension 2 in both shapes must be equal, but are 3 and 32. Shapes are [3,3,3,32] and [3,3,32,3]. for 'Assign' (op: 'Assign') with input shapes: [3,3,3,32], [3,3,32,3].

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Users\RAHKARP\Desktop\webApplication\sketchPad\views.py", line 148, in recognizeSketch
    result = predict_labels(data)
  File "C:\Users\RAHKARP\Desktop\webApplication\sketchPad\views.py", line 113, in predict_labels
    model = build_model()
  File "C:\Users\RAHKARP\Desktop\webApplication\sketchPad\views.py", line 99, in build_model
    model.load_weights(file_path)
  File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\keras\engine\network.py", line 1161, in load_weights
    f, self.layers, reshape=reshape)
  File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\keras\engine\saving.py", line 928, in load_weights_from_hdf5_group
    K.batch_set_value(weight_value_tuples)
  File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 2435, in batch_set_value
    assign_op = x.assign(assign_placeholder)
  File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\tensorflow\python\ops\variables.py", line 645, in assign
    return state_ops.assign(self._variable, value, use_locking=use_locking)
  File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\tensorflow\python\ops\state_ops.py", line 216, in assign
    validate_shape=validate_shape)
  File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_state_ops.py", line 63, in assign
    use_locking=use_locking, name=name)
  File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\tensorflow\python\util\deprecation.py", line 454, in new_func
    return func(*args, **kwargs)
  File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 3155, in create_op
    op_def=op_def)
  File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1731, in __init__
    control_input_ops)
  File "C:\Users\RAHKARP\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1579, in _create_c_op
    raise ValueError(str(e))
ValueError: Dimension 2 in both shapes must be equal, but are 3 and 32. Shapes are [3,3,3,32] and [3,3,32,3]. for 'Assign' (op: 'Assign') with input shapes: [3,3,3,32], [3,3,32,3].

Can you send a minimal example of your code with error which can be executed on our side? 您是否可以发送一个代码示例,该示例带有错误,可以在我们这边执行? It would be very helpful. 这将非常有帮助。 I suppose that error is in wrong channel order. 我认为该错误是错误的频道顺序。 You generate batch with following shape: 您生成具有以下形状的批处理:

image = image.reshape((1,) + image.shape)  # shape = (1, 3, 250,250)

In keras channels should be a last dimension: (1, 250, 250, 3) 在keras中,通道应为最后尺寸:(1,250,250,3)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果 ValueError: x 和 y 必须具有相同的第一维,但形状为 (32,) 和 (31, 5),我该怎么办? - What do I do if ValueError: x and y must have same first dimension, but have shapes (32,) and (31, 5)? ValueError:尺寸必须相等,输入形状 - ValueError: Dimensions must be equal, with input shapes ValueError:形状 (32, 5, 5) 和 (32, 2) 不兼容 - ValueError: Shapes (32, 5, 5) and (32, 2) are incompatible Matplotlib 'ValueError: x 和 y 必须具有相同的第一维,但具有形状 (20,) 和 (1,)' - Matplotlib 'ValueError: x and y must have same first dimension, but have shapes (20,) and (1,)' ValueError: x 和 y 必须具有相同的第一维,但具有形状 (2140699,) 和 (4281398,) - ValueError: x and y must have same first dimension, but have shapes (2140699,) and (4281398,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 (165,) 和 (166,) - ValueError: x and y must have same first dimension, but have shapes (165,) and (166,) ValueError: x 和 y 必须具有相同的第一个维度,但具有形状 (99955,) 和 (15000,) - ValueError: x and y must have same first dimension, but have shapes (99955,) and (15000,) ValueError:尺寸必须相等,但对于'MatMul_1'(op:'MatMul'),输入形状为784和500:[?,784],[500,500] - ValueError: Dimensions must be equal, but are 784 and 500 for 'MatMul_1' (op: 'MatMul') with input shapes: [?,784], [500,500] ValueError:尺寸必须相等,但对于具有输入形状的“mul_18”(操作:“Mul”)为 2 和 80:[?,?,?,5,2], [?,?,?,5,80] - ValueError: Dimensions must be equal, but are 2 and 80 for 'mul_18' (op: 'Mul') with input shapes: [?,?,?,5,2], [?,?,?,5,80] ValueError:尺寸必须相等,但输入形状为 100 和 19:[?,100], [?,100,19] - ValueError: Dimensions must be equal, but are 100 and 19 with input shapes: [?,100], [?,100,19]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM