问题获取ValueError: `decode_predictions` 期望一批预测

我从Github上获取了以下代码,用于运行预训练模型mobilenet_v2 https://github.com/vvigilante/mobilenet_v2_keras/blob/master/mobilenet_v2_keras.py,并尝试运行它。然而,在运行代码时遇到了一些问题。我尝试从Keras.applications.mobilentnetv2导入它,但问题并未解决。

from keras.applications.mobilenet_v2 import decode_predictions#from keras.applications.imagenet_utils import decode_predictionsfrom keras.applications.imagenet_utils import preprocess_inputdef relu6(x):  return K.relu(x, max_value=6)def _conv_block(inputs, filters, kernel, strides, use_bias=True):  channel_axis = 1 if K.image_data_format() == 'channels_first' else -1  if nlay < 0 or nlay > 16:    basename = 'conv_%d' % (nlay + 1)  else:    basename = 'expanded_conv_%d_expand' % nlay  x = Conv2D(filters, kernel, padding='same', strides=strides, name=basename, use_bias=use_bias) (inputs)  x = BatchNormalization(axis=channel_axis, name=basename + '_batch_normalization')(x)  return Activation(relu6, name=basename + '_activation')(x)def _bottleneck(inputs, filters, kernel, t, s, r=False): global nlay channel_axis = 1 if K.image_data_format() == 'channels_first' else -1 if t > 1:    tchannel = K.int_shape(inputs)[channel_axis] * t    x = _conv_block(inputs, tchannel, (1, 1), (1, 1), use_bias=False) else:    x = inputs x = DepthwiseConv2D(kernel, strides=(s, s), depth_multiplier=1, padding='same',                    name='expanded_conv_%d_depthwise' % nlay, use_bias=False)(x) x = BatchNormalization(axis=channel_axis, name='expanded_conv_%d_depthwise_batch_normalization' % nlay)(x) x = Activation(relu6, name='expanded_conv_%d_depthwise_activation' % nlay)(x) x = Conv2D(filters, (1, 1), strides=(1, 1), padding='same', name='expanded_conv_%d_project' % nlay, use_bias=False)(    x) x = BatchNormalization(axis=channel_axis, name='expanded_conv_%d_project_batch_normalization' % nlay)(x) if r:    x = add([x, inputs], name="expanded_conv_%d_add" % nlay) nlay += 1 return xdef _inverted_residual_block(inputs, filters, kernel, t, strides, n):  x = _bottleneck(inputs, filters, kernel, t, strides)  for i in range(1, n):    x = _bottleneck(x, filters, kernel, t, 1, True)  return xdef roundup(n):  x = (n + 6) // 8  return x * 8def MobileNetv2(input_shape, k, width_multiplier=1.0):  global nlay  nlay = -1  inputs = Input(shape=input_shape)  x = _conv_block(inputs, roundup(int(32 * width_multiplier)), (3, 3), strides=(2, 2), use_bias=False)  nlay += 1  fix = 0  if width_multiplier - 1.3 < 0.01:    fix = -2  x = _inverted_residual_block(x, roundup(int(16 * width_multiplier)), (3, 3), t=1, strides=1, n=1)  x = _inverted_residual_block(x, roundup(int(24 * width_multiplier)), (3, 3), t=6, strides=2, n=2)  x = _inverted_residual_block(x, roundup(int(32 * width_multiplier)), (3, 3), t=6, strides=2, n=3)  x = _inverted_residual_block(x, roundup(int(64 * width_multiplier) + fix), (3, 3), t=6, strides=2,   n=4)  x = _inverted_residual_block(x, roundup(int(96 * width_multiplier)), (3, 3), t=6, strides=1, n=3)  x = _inverted_residual_block(x, roundup(int(160 * width_multiplier)), (3, 3), t=6, strides=2, n=3)  x = _inverted_residual_block(x, roundup(int(320 * width_multiplier)), (3, 3), t=6, strides=1, n=1)  last_conv_size = max(1280, int(1280 * width_multiplier))  x = _conv_block(x, last_conv_size, (1, 1), strides=(1, 1), use_bias=False)  x = GlobalAveragePooling2D()(x)  x = Reshape((1, 1, last_conv_size))(x)  x = Dropout(0.3, name='Dropout')(x)  x = Conv2D(k, (1, 1), padding='same', name='logits', use_bias=True)(x)  x = Activation('softmax', name='softmax')(x)  output = Reshape((k,), name='out')(x)  model = Model(inputs, output)  plot_model(model, to_file='MobileNetv2.png', show_shapes=True) return modelif __name__ == '__main__':  model=MobileNetv2((224, 224, 3), 100)  img_path = 'elephant.jpg'  img = image.load_img(img_path, target_size=(224, 224))  x = image.img_to_array(img)  x = np.expand_dims(x, axis=0)  x = preprocess_input(x)  print('Input image shape:', x.shape)  preds = model.predict(x)  print('Predicted:', decode_predictions(preds))

错误

File "C:/Users/learn/PycharmProjects/mobilenet-v2.py", line 120, in <module>   print('Predicted:', decode_predictions(preds))File "C:\Users\learn\miniconda3\envs\tensorflow\lib\site- packages\keras\applications\__init__.py", line 28, in wrapperreturn base_fun(*args, **kwargs)File "C:\Users\learn\miniconda3\envs\tensorflow\lib\site- packages\keras\applications\imagenet_utils.py", line 14, in decode_predictions*args, **kwargs)File "C:\Users\learn\miniconda3\envs\tensorflow\lib\site- packages\keras_applications\imagenet_utils.py", line 222, in decode_predictions'Found array with shape: ' + str(preds.shape))ValueError: `decode_predictions` expects a batch of predictions (i.e. a 2D array of shape (samples, 1000)). Found array with shape: (1, 100)

回答:

此函数旨在将1,000个概率的向量转换为ImageNet数据集的一个类别,该数据集有1,000个类别。你的最终层有100个类别,因此函数感到困惑。你可以这样做:

model=MobileNetv2((224, 224, 3), 1000)

如果这符合你的任务需求。你想用这个函数做什么?

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注