Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

ValueError: Error when checking input: expected input_1 to have shape (3, 224, 224) but got array with shape (3, 3, 224) #167

Open
Nagaraj4896 opened this issue Jan 29, 2020 · 2 comments

Comments

@Nagaraj4896
Copy link

Nagaraj4896 commented Jan 29, 2020

_________________________________ test_resnet __________________________________
[gw0] linux -- Python 3.6.7 /home/ironman/anaconda3/envs/Tf_Cv_Ker/bin/python3

def test_resnet():
    app = random.choice(RESNET_LIST)
    module = keras_applications.resnet
    last_dim = 2048
  _test_application_basic(app, module=module)

test_check.py:197:


test_check.py:86: in wrapper
output = func(*args, **kwargs)
test_check.py:150: in _test_application_basic
lambda: app(weights='imagenet'), module.preprocess_input)
test_check.py:140: in _get_output_shape
return (model.output_shape, model.predict(x))
/home/ironman/anaconda3/envs/Tf_Cv_Ker/lib/python3.6/site-packages/keras/engine/training.py:1441: in predict
x, _, _ = self._standardize_user_data(x)
/home/ironman/anaconda3/envs/Tf_Cv_Ker/lib/python3.6/site-packages/keras/engine/training.py:579: in _standardize_user_data
exception_prefix='input')


data = [array([[[[ 6.4060997e+01, 9.9060997e+01, 1.2106100e+02, ...,
1.1406100e+02, 1.1406100e+02, 1.1406100e+...6.9680000e+01, -6.6800003e+00, ...,
3.4320000e+01, 2.6320000e+01, 5.3320000e+01]]]],
dtype=float32)]
names = ['input_1'], shapes = [(None, 3, 224, 224)], check_batch_axis = False
exception_prefix = 'input'

def standardize_input_data(data,
                           names,
                           shapes=None,
                           check_batch_axis=True,
                           exception_prefix=''):
    """Normalizes inputs and targets provided by users.

    Users may pass data as a list of arrays, dictionary of arrays,
    or as a single array. We normalize this to an ordered list of
    arrays (same order as `names`), while checking that the provided
    arrays have shapes that match the network's expectations.

    # Arguments
        data: User-provided input data (polymorphic).
        names: List of expected array names.
        shapes: Optional list of expected array shapes.
        check_batch_axis: Boolean; whether to check that
            the batch axis of the arrays matches the expected
            value found in `shapes`.
        exception_prefix: String prefix used for exception formatting.

    # Returns
        List of standardized input arrays (one array per model input).

    # Raises
        ValueError: in case of improperly formatted user-provided data.
    """
    if not names:
        if data is not None and hasattr(data, '__len__') and len(data):
            raise ValueError('Error when checking model ' +
                             exception_prefix + ': '
                             'expected no data, but got:', data)
        return []
    if data is None:
        return [None for _ in range(len(names))]

    if isinstance(data, dict):
        try:
            data = [
                data[x].values
                if data[x].__class__.__name__ == 'DataFrame' else data[x]
                for x in names
            ]
        except KeyError as e:
            raise ValueError('No data provided for "' + e.args[0] +
                             '". Need data '
                             'for each key in: ' + str(names))
    elif isinstance(data, list):
        if isinstance(data[0], list):
            data = [np.asarray(d) for d in data]
        elif len(names) == 1 and isinstance(data[0], (float, int)):
            data = [np.asarray(data)]
        else:
            data = [
                x.values if x.__class__.__name__ == 'DataFrame'
                else x for x in data
            ]
    else:
        data = data.values if data.__class__.__name__ == 'DataFrame' else data
        data = [data]
    data = [standardize_single_array(x) for x in data]

    if len(data) != len(names):
        if data and hasattr(data[0], 'shape'):
            raise ValueError(
                'Error when checking model ' + exception_prefix +
                ': the list of Numpy arrays that you are passing to '
                'your model is not the size the model expected. '
                'Expected to see ' + str(len(names)) + ' array(s), '
                'but instead got the following list of ' +
                str(len(data)) + ' arrays: ' + str(data)[:200] + '...')
        elif len(names) > 1:
            raise ValueError(
                'Error when checking model ' + exception_prefix +
                ': you are passing a list as input to your model, '
                'but the model expects a list of ' + str(len(names)) +
                ' Numpy arrays instead. '
                'The list you passed was: ' + str(data)[:200])
        elif len(data) == 1 and not hasattr(data[0], 'shape'):
            raise TypeError('Error when checking model ' + exception_prefix +
                            ': data should be a Numpy array, or list/dict of '
                            'Numpy arrays. Found: ' + str(data)[:200] + '...')
        elif len(names) == 1:
            data = [np.asarray(data)]

    # Check shapes compatibility.
    if shapes:
        for i in range(len(names)):
            if shapes[i] is not None and not K.is_tensor(data[i]):
                data_shape = data[i].shape
                shape = shapes[i]
                if data[i].ndim != len(shape):
                    raise ValueError(
                        'Error when checking ' + exception_prefix +
                        ': expected ' + names[i] + ' to have ' +
                        str(len(shape)) + ' dimensions, but got array '
                        'with shape ' + str(data_shape))
                if not check_batch_axis:
                    data_shape = data_shape[1:]
                    shape = shape[1:]
                for dim, ref_dim in zip(data_shape, shape):
                    if ref_dim != dim and ref_dim:
                        raise ValueError(
                            'Error when checking ' + exception_prefix +
                            ': expected ' + names[i] + ' to have shape ' +
                            str(shape) + ' but got array with shape ' +
                          str(data_shape))

E ValueError: Error when checking input: expected input_1 to have shape (3, 224, 224) but got array with shape (3, 3, 224)

/home/ironman/anaconda3/envs/Tf_Cv_Ker/lib/python3.6/site-packages/keras/engine/training_utils.py:145: ValueError

@Nagaraj4896 Nagaraj4896 changed the title ValueError while running applications_test.py ValueError: Error when checking input: expected input_1 to have shape (3, 224, 224) but got array with shape (3, 3, 224) Jan 29, 2020
@taehoonlee
Copy link
Contributor

@Nagaraj4896, The error is not reproducible. The test _test_application_basic(app, module=module) has been checked in Travis. Are you using tests/data/elephant.jpg?

@Nagaraj4896
Copy link
Author

@taehoonlee yes i am using tests/data/elephant.jpg only.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants