TensorFlow期望全局“平均”池2D“1”输入具有形状(1,1,2048),但得到的数组具有形状(7,7,2048)

2024-09-28 05:27:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我对TensorFlow和图像分类比较陌生,所以我可能缺少关键知识,这可能是我面临这个问题的原因。在

为了对犬种进行图像分类,我在TensorFlow中建立了一个ResNet50模型,并使用ImageNet库成功地训练了一个能够检测各种犬种的神经网络。在

现在我想把一只狗的随机图像传递给我的模型,让它输出它认为的狗的品种。但是,当我运行这个函数dog_breed_predictor("dogImages/dogImages/valid/016.Beagle/Beagle_01126.jpg")时,当它试图执行Resnet50_model.predict(bottleneck_feature)行时,我得到了一个错误expected global_average_pooling2d_1_input to have shape (1, 1, 2048) but got array with shape (7, 7, 2048),我不知道如何解决这个问题。在

这是密码。我已经提供了我认为与问题相关的所有信息。在

#import tensorflow as tf, etc.

from sklearn.datasets import load_files
np_utils = tf.keras.utils

# define function to load train, test, and validation datasets
def load_dataset(path):
    data = load_files(path)
    dog_files = np.array(data['filenames'])
    dog_targets = np_utils.to_categorical(np.array(data['target']), 133)
    return dog_files, dog_targets

# load train, test, and validation datasets
train_files, train_targets = load_dataset('dogImages/dogImages/train')
valid_files, valid_targets = load_dataset('dogImages/dogImages/valid')
test_files, test_targets = load_dataset('dogImages/dogImages/test')

#define Resnet50 model
Resnet50_model = ResNet50(weights="imagenet")

def path_to_tensor(img_path):
    #loads RGB image as PIL.Image.Image type
    img = image.load_img(img_path, target_size=(224, 224))
    #convert PIL.Image.Image type to 3D tensor with shape (224, 224, 3)
    x = image.img_to_array(img)
    #convert 3D tensor into 4D tensor with shape (1, 224, 224, 3)
    return np.expand_dims(x, axis=0)

from keras.applications.resnet50 import preprocess_input, decode_predictions

def ResNet50_predict_labels(img_path):
    #returns prediction vector for image located at img_path
    img = preprocess_input(path_to_tensor(img_path))
    return np.argmax(Resnet50_model.predict(img))

###returns True if a dog is detected in the image stored at img_path
def dog_detector(img_path):
    prediction = ResNet50_predict_labels(img_path)
    return ((prediction <= 268) & (prediction >= 151))

###Obtain bottleneck features from another pre-trained CNN
bottleneck_features = np.load("bottleneck_features/DogResnet50Data.npz")
train_DogResnet50 = bottleneck_features["train"]
valid_DogResnet50 = bottleneck_features["valid"]
test_DogResnet50 = bottleneck_features["test"]

###Define your architecture
Resnet50_model = tf.keras.Sequential()
Resnet50_model.add(tf.keras.layers.GlobalAveragePooling2D(input_shape=train_DogResnet50.shape[1:]))
Resnet50_model.add(tf.contrib.keras.layers.Dense(133, activation="softmax"))

Resnet50_model.summary()

###Compile the model
Resnet50_model.compile(loss="categorical_crossentropy", optimizer="rmsprop", metrics=["accuracy"])
###Train the model
checkpointer = tf.keras.callbacks.ModelCheckpoint(filepath="saved_models/weights.best.ResNet50.hdf5",
                                                 verbose=1, save_best_only=True)

Resnet50_model.fit(train_DogResnet50, train_targets,
                  validation_data=(valid_DogResnet50, valid_targets),
                  epochs=20, batch_size=20, callbacks=[checkpointer])

###Load the model weights with the best validation loss.
Resnet50_model.load_weights("saved_models/weights.best.ResNet50.hdf5")

###Calculate classification accuracy on the test dataset
Resnet50_predictions = [np.argmax(Resnet50_model.predict(np.expand_dims(feature, axis=0))) for feature in test_DogResnet50]

#Report test accuracy
test_accuracy = 100*np.sum(np.array(Resnet50_predictions)==np.argmax(test_targets, axis=1))/len(Resnet50_predictions)
print("Test accuracy: %.4f%%" % test_accuracy)

from extract_bottleneck_features import * #separate .py file

def dog_breed(img_path):
    #extract bottleneck features
    bottleneck_feature = extract_Resnet50(path_to_tensor(img_path))
    #obtain predicted vector
    predicted_vector = Resnet50_model.predict(bottleneck_feature)
    #return dog breed that is predicted by the model
    return dog_names[np.argmax(predicted_vector)]

def dog_breed_predictor(img_path):
    #determine the predicted dog breed
    breed = dog_breed(img_path)
    #display the image
    img = cv2.imread(img_path)
    cv_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    plt.imshow(cv_rgb)
    plt.show()
    #display relevant predictor result
    if dog_detector(img_path):
        print("This is a dog and its breed is: " + str(breed))
    elif face_detector(img_path):
        print("This is a human but it looks like a: " + str(breed))
    else:
        print("I don't know what this is.")

dog_breed_predictor("dogImages/dogImages/valid/016.Beagle/Beagle_01126.jpg") #shape error occurs here

提取瓶颈的函数_功能.py公司名称:

^{pr2}$

Tags: thetopathtestimgmodelnpload
2条回答

首先有两个模型,命名为Resnet50_model。一个是ResNet50()将图像分类为dog,另一个是您设置的

###Define your architecture
Resnet50_model = tf.keras.Sequential()
Resnet50_model.add(tf.keras.layers.GlobalAveragePooling2D(input_shape=train_DogResnet50.shape[1:]))
Resnet50_model.add(tf.contrib.keras.layers.Dense(133, activation="softmax"))

正在从ResNet50()开始分类为狗种,您应该将其重命名。在

从维度为(1,12048)的bottleneck_features/DogResnet50Data.npz加载的数据,而从维度为(12048)的extract_Resnet50(path_to_tensor(img_path))中获得的数据可以编辑为

^{pr2}$

或者您可以使用

def paths_to_tensor(img_paths):
    list_of_tensors = [path_to_tensor(img_path) for img_path in tqdm(img_paths)]
    return np.vstack(list_of_tensors)

def extract_Resnet50(tensor):
    from keras.applications.resnet50 import ResNet50, preprocess_input
    ResNet50(weights='imagenet', input_shape=(224,224,3), pooling='avg', include_top=False).predict(preprocess_input(tensor))

train_Resnet50 = extract_Resnet50(paths_to_tensor(train_files))[:,np.newaxis,np.newaxis,:]
valid_Resnet50 = extract_Resnet50(paths_to_tensor(valid_files))[:,np.newaxis,np.newaxis,:]
test_Resnet50 = extract_Resnet50(paths_to_tensor(test_files))[:,np.newaxis,np.newaxis,:]

def Resnet50_predict_breed(img_path):
    # extract bottleneck features
    bottleneck_feature = np.expand_dims(np.expand_dims(
        extract_Resnet50(path_to_tensor(img_path)), axis=0), axis=0)
    ...

为什么不在ResNet调用中使用pooling == 'avg'pooling == 'max'和{}作为参数。它会照顾你的池层。使用这些参数调用函数后,可以删除池层。 从source code

if include_top:
    x = Flatten()(x)
    x = Dense(classes, activation='softmax', name='fc1000')(x)
else:
    if pooling == 'avg':
        x = GlobalAveragePooling2D()(x)
    elif pooling == 'max':
        x = GlobalMaxPooling2D()(x)

相关问题 更多 >

    热门问题