| 1 | """Utilities for ImageNet data preprocessing & prediction decoding. |
| 2 | """ |
| 3 | from __future__scip-python python python-stdlib 3.11 __future__/__init__: import absolute_importscip-python python python-stdlib 3.11 __future__/absolute_import.absolute_import. |
| 4 | from __future__scip-python python python-stdlib 3.11 __future__/__init__: import divisionscip-python python python-stdlib 3.11 __future__/division.division. |
| 5 | from __future__scip-python python python-stdlib 3.11 __future__/__init__: import print_functionscip-python python python-stdlib 3.11 __future__/print_function.print_function. |
| 6 | |
| 7 | import jsonscip-python python python-stdlib 3.11 json/__init__: |
| 8 | import warningsscip-python python python-stdlib 3.11 warnings/__init__: |
| 9 | import numpyscip-python python numpy 2.4.6 numpy/__init__: as np |
| 10 | |
| 11 | from . import get_submodules_from_kwargsscip-python python Keras_Applications 1.0.8 keras_applications/get_submodules_from_kwargs(). |
| 12 | |
| 13 | CLASS_INDEXscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/CLASS_INDEX. = None |
| 14 | CLASS_INDEX_PATHscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/CLASS_INDEX_PATH. = ('https://storage.googleapis.com/download.tensorflow.org/' |
| 15 | 'data/imagenet_class_index.json') |
| 16 | |
| 17 | # Global tensor of imagenet mean for preprocessing symbolic inputs |
| 18 | _IMAGENET_MEANscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_IMAGENET_MEAN. = None |
| 19 | |
| 20 | |
| 21 | def _preprocess_numpy_inputscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x), data_formatscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(data_format), modescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(mode), **kwargsscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(kwargs)): |
| 22 | """Preprocesses a Numpy array encoding a batch of images. |
| 23 | |
| 24 | # Arguments |
| 25 | x: Input array, 3D or 4D. |
| 26 | data_format: Data format of the image array. |
| 27 | mode: One of "caffe", "tf" or "torch". |
| 28 | - caffe: will convert the images from RGB to BGR, |
| 29 | then will zero-center each color channel with |
| 30 | respect to the ImageNet dataset, |
| 31 | without scaling. |
| 32 | - tf: will scale pixels between -1 and 1, |
| 33 | sample-wise. |
| 34 | - torch: will scale pixels between 0 and 1 and then |
| 35 | will normalize each channel with respect to the |
| 36 | ImageNet dataset. |
| 37 | |
| 38 | # Returns |
| 39 | Preprocessed Numpy array. |
| 40 | """ |
| 41 | backend, _, _, _ = get_submodules_from_kwargsscip-python python Keras_Applications 1.0.8 keras_applications/get_submodules_from_kwargs().(kwargsscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(kwargs)) |
| 42 | if not issubclass(xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x).dtype.type, np.floating): |
| 43 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x) = xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x).astype(backend.floatx(), copy=False) |
| 44 | |
| 45 | if modescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(mode) == 'tf': |
| 46 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x) /= 127.5 |
| 47 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x) -= 1. |
| 48 | return xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x) |
| 49 | |
| 50 | if modescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(mode) == 'torch': |
| 51 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x) /= 255. |
| 52 | mean = [0.485, 0.456, 0.406] |
| 53 | std = [0.229, 0.224, 0.225] |
| 54 | else: |
| 55 | if data_formatscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(data_format) == 'channels_first': |
| 56 | # 'RGB'->'BGR' |
| 57 | if xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x).ndim == 3: |
| 58 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x) = xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x)[::-1, ...] |
| 59 | else: |
| 60 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x) = xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x)[:, ::-1, ...] |
| 61 | else: |
| 62 | # 'RGB'->'BGR' |
| 63 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x) = xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x)[..., ::-1] |
| 64 | mean = [103.939, 116.779, 123.68] |
| 65 | std = None |
| 66 | |
| 67 | # Zero-center by mean pixel |
| 68 | if data_formatscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(data_format) == 'channels_first': |
| 69 | if xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x).ndim == 3: |
| 70 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x)[0, :, :] -= mean[0] |
| 71 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x)[1, :, :] -= mean[1] |
| 72 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x)[2, :, :] -= mean[2] |
| 73 | if std is not None: |
| 74 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x)[0, :, :] /= std[0] |
| 75 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x)[1, :, :] /= std[1] |
| 76 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x)[2, :, :] /= std[2] |
| 77 | else: |
| 78 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x)[:, 0, :, :] -= mean[0] |
| 79 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x)[:, 1, :, :] -= mean[1] |
| 80 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x)[:, 2, :, :] -= mean[2] |
| 81 | if std is not None: |
| 82 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x)[:, 0, :, :] /= std[0] |
| 83 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x)[:, 1, :, :] /= std[1] |
| 84 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x)[:, 2, :, :] /= std[2] |
| 85 | else: |
| 86 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x)[..., 0] -= mean[0] |
| 87 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x)[..., 1] -= mean[1] |
| 88 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x)[..., 2] -= mean[2] |
| 89 | if std is not None: |
| 90 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x)[..., 0] /= std[0] |
| 91 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x)[..., 1] /= std[1] |
| 92 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x)[..., 2] /= std[2] |
| 93 | return xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(x) |
| 94 | |
| 95 | |
| 96 | def _preprocess_symbolic_inputscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(x), data_formatscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(data_format), modescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(mode), **kwargsscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(kwargs)): |
| 97 | """Preprocesses a tensor encoding a batch of images. |
| 98 | |
| 99 | # Arguments |
| 100 | x: Input tensor, 3D or 4D. |
| 101 | data_format: Data format of the image tensor. |
| 102 | mode: One of "caffe", "tf" or "torch". |
| 103 | - caffe: will convert the images from RGB to BGR, |
| 104 | then will zero-center each color channel with |
| 105 | respect to the ImageNet dataset, |
| 106 | without scaling. |
| 107 | - tf: will scale pixels between -1 and 1, |
| 108 | sample-wise. |
| 109 | - torch: will scale pixels between 0 and 1 and then |
| 110 | will normalize each channel with respect to the |
| 111 | ImageNet dataset. |
| 112 | |
| 113 | # Returns |
| 114 | Preprocessed tensor. |
| 115 | """ |
| 116 | global _IMAGENET_MEANscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_IMAGENET_MEAN. |
| 117 | |
| 118 | backend, _, _, _ = get_submodules_from_kwargsscip-python python Keras_Applications 1.0.8 keras_applications/get_submodules_from_kwargs().(kwargsscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(kwargs)) |
| 119 | |
| 120 | if modescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(mode) == 'tf': |
| 121 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(x) /= 127.5 |
| 122 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(x) -= 1. |
| 123 | return xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(x) |
| 124 | |
| 125 | if modescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(mode) == 'torch': |
| 126 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(x) /= 255. |
| 127 | mean = [0.485, 0.456, 0.406] |
| 128 | std = [0.229, 0.224, 0.225] |
| 129 | else: |
| 130 | if data_formatscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(data_format) == 'channels_first': |
| 131 | # 'RGB'->'BGR' |
| 132 | if backend.ndim(xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(x)) == 3: |
| 133 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(x) = xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(x)[::-1, ...] |
| 134 | else: |
| 135 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(x) = xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(x)[:, ::-1, ...] |
| 136 | else: |
| 137 | # 'RGB'->'BGR' |
| 138 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(x) = xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(x)[..., ::-1] |
| 139 | mean = [103.939, 116.779, 123.68] |
| 140 | std = None |
| 141 | |
| 142 | if _IMAGENET_MEANscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_IMAGENET_MEAN. is None: |
| 143 | _IMAGENET_MEANscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_IMAGENET_MEAN. = backend.constant(-np.arrayscip-python python numpy 2.4.6 `numpy._core.multiarray`/array().(mean)) |
| 144 | |
| 145 | # Zero-center by mean pixel |
| 146 | if backend.dtype(xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(x)) != backend.dtype(_IMAGENET_MEANscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_IMAGENET_MEAN.): |
| 147 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(x) = backend.bias_add( |
| 148 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(x), backend.cast(_IMAGENET_MEANscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_IMAGENET_MEAN., backend.dtype(xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(x))), |
| 149 | data_format=data_formatscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(data_format)) |
| 150 | else: |
| 151 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(x) = backend.bias_add(xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(x), _IMAGENET_MEANscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_IMAGENET_MEAN., data_formatscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(data_format)) |
| 152 | if std is not None: |
| 153 | xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(x) /= std |
| 154 | return xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(x) |
| 155 | |
| 156 | |
| 157 | def preprocess_inputscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/preprocess_input().(xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/preprocess_input().(x), data_formatscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/preprocess_input().(data_format)=None, modescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/preprocess_input().(mode)='caffe', **kwargsscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/preprocess_input().(kwargs)): |
| 158 | """Preprocesses a tensor or Numpy array encoding a batch of images. |
| 159 | |
| 160 | # Arguments |
| 161 | x: Input Numpy or symbolic tensor, 3D or 4D. |
| 162 | The preprocessed data is written over the input data |
| 163 | if the data types are compatible. To avoid this |
| 164 | behaviour, `numpy.copy(x)` can be used. |
| 165 | data_format: Data format of the image tensor/array. |
| 166 | mode: One of "caffe", "tf" or "torch". |
| 167 | - caffe: will convert the images from RGB to BGR, |
| 168 | then will zero-center each color channel with |
| 169 | respect to the ImageNet dataset, |
| 170 | without scaling. |
| 171 | - tf: will scale pixels between -1 and 1, |
| 172 | sample-wise. |
| 173 | - torch: will scale pixels between 0 and 1 and then |
| 174 | will normalize each channel with respect to the |
| 175 | ImageNet dataset. |
| 176 | |
| 177 | # Returns |
| 178 | Preprocessed tensor or Numpy array. |
| 179 | |
| 180 | # Raises |
| 181 | ValueError: In case of unknown `data_format` argument. |
| 182 | """ |
| 183 | backend, _, _, _ = get_submodules_from_kwargsscip-python python Keras_Applications 1.0.8 keras_applications/get_submodules_from_kwargs().(kwargsscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/preprocess_input().(kwargs)) |
| 184 | |
| 185 | if data_formatscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/preprocess_input().(data_format) is None: |
| 186 | data_formatscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/preprocess_input().(data_format) = backend.image_data_format() |
| 187 | if data_formatscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/preprocess_input().(data_format) not in {'channels_first', 'channels_last'}: |
| 188 | raise ValueErrorscip-python python python-stdlib 3.11 builtins/ValueError#('Unknown data_format ' + strscip-python python python-stdlib 3.11 builtins/str#(data_formatscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/preprocess_input().(data_format))) |
| 189 | |
| 190 | if isinstance(xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/preprocess_input().(x), np.ndarray): |
| 191 | return _preprocess_numpy_inputscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/preprocess_input().(x), data_formatscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(data_format)=data_formatscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/preprocess_input().(data_format), |
| 192 | modescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_numpy_input().(mode)=modescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/preprocess_input().(mode), **kwargsscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/preprocess_input().(kwargs)) |
| 193 | else: |
| 194 | return _preprocess_symbolic_inputscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(xscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/preprocess_input().(x), data_formatscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(data_format)=data_formatscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/preprocess_input().(data_format), |
| 195 | modescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_preprocess_symbolic_input().(mode)=modescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/preprocess_input().(mode), **kwargsscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/preprocess_input().(kwargs)) |
| 196 | |
| 197 | |
| 198 | def decode_predictionsscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/decode_predictions().(predsscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/decode_predictions().(preds), topscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/decode_predictions().(top)=5, **kwargsscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/decode_predictions().(kwargs)): |
| 199 | """Decodes the prediction of an ImageNet model. |
| 200 | |
| 201 | # Arguments |
| 202 | preds: Numpy tensor encoding a batch of predictions. |
| 203 | top: Integer, how many top-guesses to return. |
| 204 | |
| 205 | # Returns |
| 206 | A list of lists of top class prediction tuples |
| 207 | `(class_name, class_description, score)`. |
| 208 | One list of tuples per sample in batch input. |
| 209 | |
| 210 | # Raises |
| 211 | ValueError: In case of invalid shape of the `pred` array |
| 212 | (must be 2D). |
| 213 | """ |
| 214 | global CLASS_INDEXscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/CLASS_INDEX. |
| 215 | |
| 216 | backend, _, _, keras_utils = get_submodules_from_kwargsscip-python python Keras_Applications 1.0.8 keras_applications/get_submodules_from_kwargs().(kwargsscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/decode_predictions().(kwargs)) |
| 217 | |
| 218 | if len(predsscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/decode_predictions().(preds).shape) != 2 or predsscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/decode_predictions().(preds).shape[1] != 1000: |
| 219 | raise ValueErrorscip-python python python-stdlib 3.11 builtins/ValueError#('`decode_predictions` expects ' |
| 220 | 'a batch of predictions ' |
| 221 | '(i.e. a 2D array of shape (samples, 1000)). ' |
| 222 | 'Found array with shape: ' + strscip-python python python-stdlib 3.11 builtins/str#(predsscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/decode_predictions().(preds).shape)) |
| 223 | if CLASS_INDEXscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/CLASS_INDEX. is None: |
| 224 | fpath = keras_utils.get_file( |
| 225 | 'imagenet_class_index.json', |
| 226 | CLASS_INDEX_PATHscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/CLASS_INDEX_PATH., |
| 227 | cache_subdir='models', |
| 228 | file_hash='c2c37ea517e94d9795004a39431a14cb') |
| 229 | with openscip-python python python-stdlib 3.11 builtins/open().(fpath) as f: |
| 230 | CLASS_INDEXscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/CLASS_INDEX. = jsonscip-python python python-stdlib 3.11 json/__init__:.loadscip-python python python-stdlib 3.11 json/load().(f) |
| 231 | results = [] |
| 232 | for pred in predsscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/decode_predictions().(preds): |
| 233 | top_indices = pred.argsort()[-topscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/decode_predictions().(top):][::-1] |
| 234 | result = [tuplescip-python python python-stdlib 3.11 builtins/tuple#(CLASS_INDEXscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/CLASS_INDEX.[strscip-python python python-stdlib 3.11 builtins/str#(i)]) + (pred[i],) for i in top_indices] |
| 235 | result.sortscip-python python python-stdlib 3.11 builtins/list#sort().(keyscip-python python python-stdlib 3.11 builtins/list#sort().(key)=lambda x: x[2], reversescip-python python python-stdlib 3.11 builtins/list#sort().(reverse)=True) |
| 236 | results.appendscip-python python python-stdlib 3.11 builtins/list#append().(result) |
| 237 | return results |
| 238 | |
| 239 | |
| 240 | def _obtain_input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape), |
| 241 | default_sizescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(default_size), |
| 242 | min_sizescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(min_size), |
| 243 | data_formatscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(data_format), |
| 244 | require_flattenscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(require_flatten), |
| 245 | weightsscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(weights)=None): |
| 246 | """Internal utility to compute/validate a model's input shape. |
| 247 | |
| 248 | # Arguments |
| 249 | input_shape: Either None (will return the default network input shape), |
| 250 | or a user-provided shape to be validated. |
| 251 | default_size: Default input width/height for the model. |
| 252 | min_size: Minimum input width/height accepted by the model. |
| 253 | data_format: Image data format to use. |
| 254 | require_flatten: Whether the model is expected to |
| 255 | be linked to a classifier via a Flatten layer. |
| 256 | weights: One of `None` (random initialization) |
| 257 | or 'imagenet' (pre-training on ImageNet). |
| 258 | If weights='imagenet' input channels must be equal to 3. |
| 259 | |
| 260 | # Returns |
| 261 | An integer shape tuple (may include None entries). |
| 262 | |
| 263 | # Raises |
| 264 | ValueError: In case of invalid argument values. |
| 265 | """ |
| 266 | if weightsscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(weights) != 'imagenet' and input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape) and len(input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape)) == 3: |
| 267 | if data_formatscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(data_format) == 'channels_first': |
| 268 | if input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape)[0] not in {1, 3}: |
| 269 | warningsscip-python python python-stdlib 3.11 warnings/__init__:.warnscip-python python python-stdlib 3.11 _warnings/warn().( |
| 270 | 'This model usually expects 1 or 3 input channels. ' |
| 271 | 'However, it was passed an input_shape with ' + |
| 272 | strscip-python python python-stdlib 3.11 builtins/str#(input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape)[0]) + ' input channels.') |
| 273 | default_shape = (input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape)[0], default_sizescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(default_size), default_sizescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(default_size)) |
| 274 | else: |
| 275 | if input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape)[-1] not in {1, 3}: |
| 276 | warningsscip-python python python-stdlib 3.11 warnings/__init__:.warnscip-python python python-stdlib 3.11 _warnings/warn().( |
| 277 | 'This model usually expects 1 or 3 input channels. ' |
| 278 | 'However, it was passed an input_shape with ' + |
| 279 | strscip-python python python-stdlib 3.11 builtins/str#(input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape)[-1]) + ' input channels.') |
| 280 | default_shape = (default_sizescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(default_size), default_sizescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(default_size), input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape)[-1]) |
| 281 | else: |
| 282 | if data_formatscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(data_format) == 'channels_first': |
| 283 | default_shape = (3, default_sizescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(default_size), default_sizescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(default_size)) |
| 284 | else: |
| 285 | default_shape = (default_sizescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(default_size), default_sizescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(default_size), 3) |
| 286 | if weightsscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(weights) == 'imagenet' and require_flattenscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(require_flatten): |
| 287 | if input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape) is not None: |
| 288 | if input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape) != default_shape: |
| 289 | raise ValueErrorscip-python python python-stdlib 3.11 builtins/ValueError#('When setting `include_top=True` ' |
| 290 | 'and loading `imagenet` weights, ' |
| 291 | '`input_shape` should be ' + |
| 292 | strscip-python python python-stdlib 3.11 builtins/str#(default_shape) + '.') |
| 293 | return default_shape |
| 294 | if input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape): |
| 295 | if data_formatscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(data_format) == 'channels_first': |
| 296 | if input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape) is not None: |
| 297 | if len(input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape)) != 3: |
| 298 | raise ValueErrorscip-python python python-stdlib 3.11 builtins/ValueError#( |
| 299 | '`input_shape` must be a tuple of three integers.') |
| 300 | if input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape)[0] != 3 and weightsscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(weights) == 'imagenet': |
| 301 | raise ValueErrorscip-python python python-stdlib 3.11 builtins/ValueError#('The input must have 3 channels; got ' |
| 302 | '`input_shape=' + strscip-python python python-stdlib 3.11 builtins/str#(input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape)) + '`') |
| 303 | if ((input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape)[1] is not None and input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape)[1] < min_sizescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(min_size)) or |
| 304 | (input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape)[2] is not None and input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape)[2] < min_sizescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(min_size))): |
| 305 | raise ValueErrorscip-python python python-stdlib 3.11 builtins/ValueError#('Input size must be at least ' + |
| 306 | strscip-python python python-stdlib 3.11 builtins/str#(min_sizescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(min_size)) + 'x' + strscip-python python python-stdlib 3.11 builtins/str#(min_sizescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(min_size)) + |
| 307 | '; got `input_shape=' + |
| 308 | strscip-python python python-stdlib 3.11 builtins/str#(input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape)) + '`') |
| 309 | else: |
| 310 | if input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape) is not None: |
| 311 | if len(input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape)) != 3: |
| 312 | raise ValueErrorscip-python python python-stdlib 3.11 builtins/ValueError#( |
| 313 | '`input_shape` must be a tuple of three integers.') |
| 314 | if input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape)[-1] != 3 and weightsscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(weights) == 'imagenet': |
| 315 | raise ValueErrorscip-python python python-stdlib 3.11 builtins/ValueError#('The input must have 3 channels; got ' |
| 316 | '`input_shape=' + strscip-python python python-stdlib 3.11 builtins/str#(input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape)) + '`') |
| 317 | if ((input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape)[0] is not None and input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape)[0] < min_sizescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(min_size)) or |
| 318 | (input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape)[1] is not None and input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape)[1] < min_sizescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(min_size))): |
| 319 | raise ValueErrorscip-python python python-stdlib 3.11 builtins/ValueError#('Input size must be at least ' + |
| 320 | strscip-python python python-stdlib 3.11 builtins/str#(min_sizescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(min_size)) + 'x' + strscip-python python python-stdlib 3.11 builtins/str#(min_sizescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(min_size)) + |
| 321 | '; got `input_shape=' + |
| 322 | strscip-python python python-stdlib 3.11 builtins/str#(input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape)) + '`') |
| 323 | else: |
| 324 | if require_flattenscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(require_flatten): |
| 325 | input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape) = default_shape |
| 326 | else: |
| 327 | if data_formatscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(data_format) == 'channels_first': |
| 328 | input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape) = (3, None, None) |
| 329 | else: |
| 330 | input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape) = (None, None, 3) |
| 331 | if require_flattenscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(require_flatten): |
| 332 | if None in input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape): |
| 333 | raise ValueErrorscip-python python python-stdlib 3.11 builtins/ValueError#('If `include_top` is True, ' |
| 334 | 'you should specify a static `input_shape`. ' |
| 335 | 'Got `input_shape=' + strscip-python python python-stdlib 3.11 builtins/str#(input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape)) + '`') |
| 336 | return input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shape) |