简体   繁体   中英

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

I am currently studying 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.

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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM