Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Keras VGG16修正模型每次都给出相同的预测_Python_Tensorflow_Keras_Deep Learning_Conv Neural Network - Fatal编程技术网

Python Keras VGG16修正模型每次都给出相同的预测

Python Keras VGG16修正模型每次都给出相同的预测,python,tensorflow,keras,deep-learning,conv-neural-network,Python,Tensorflow,Keras,Deep Learning,Conv Neural Network,我采用了内置的keras.applications.vgg16.vgg16(weights='imagenet',include_top=True,input_shape=(224224,3))模型,并通过包括如下所示的全局平均池层,对PASCAL VOC 2012数据集进行了迁移学习,共有20个类: def VGG16_modified(): base_model = vgg16.VGG16(include_top=True,weights='imagenet',input_shape

我采用了内置的keras.applications.vgg16.vgg16(weights='imagenet',include_top=True,input_shape=(224224,3))模型,并通过包括如下所示的全局平均池层,对PASCAL VOC 2012数据集进行了迁移学习,共有20个类:

def VGG16_modified():
    base_model = vgg16.VGG16(include_top=True,weights='imagenet',input_shape=(224,224,3))
    print(base_model.summary())
    x = base_model.get_layer('block5_pool').output
    x = (GlobalAveragePooling2D())(x)
    predictions = Dense(20,activation='sigmoid')(x)

    final_model = Model(input = base_model.input, output = predictions)
    print(final_model.get_weights())
    return final_model
 def get_CAM(model,img):
        model = load_model(model)
        im = image.load_img(img,target_size=(224,224))
        im = image.img_to_array(im)
        im = np.expand_dims(im,axis=0)
        class_weights = model.layers[-1].get_weights()[0]
        final_conv_layer = model.get_layer('block5_pool')
        cam_model = Model(inputs = model.input,outputs=(final_conv_layer.output,model.layers[-1].output))
        conv_outputs, predictions = cam_model.predict(im)
        conv_outputs = np.squeeze(conv_outputs)
        prediction = np.argmax(predictions)
        print(predictions)
        print(prediction)
        print(conv_outputs)
        print(conv_outputs.shape)
        class_weights = class_weights[:,prediction]
        mat_for_mult = scipy.ndimage.zoom(conv_outputs,(32,32,1),order=1)
        final_output = np.dot(mat_for_mult.reshape((224*224, 512)),class_weights).reshape((224,224))
        print(final_output)
        return final_output
现在,我想拿一张纸上的班级激活图。为此,我的代码如下所示:

def VGG16_modified():
    base_model = vgg16.VGG16(include_top=True,weights='imagenet',input_shape=(224,224,3))
    print(base_model.summary())
    x = base_model.get_layer('block5_pool').output
    x = (GlobalAveragePooling2D())(x)
    predictions = Dense(20,activation='sigmoid')(x)

    final_model = Model(input = base_model.input, output = predictions)
    print(final_model.get_weights())
    return final_model
 def get_CAM(model,img):
        model = load_model(model)
        im = image.load_img(img,target_size=(224,224))
        im = image.img_to_array(im)
        im = np.expand_dims(im,axis=0)
        class_weights = model.layers[-1].get_weights()[0]
        final_conv_layer = model.get_layer('block5_pool')
        cam_model = Model(inputs = model.input,outputs=(final_conv_layer.output,model.layers[-1].output))
        conv_outputs, predictions = cam_model.predict(im)
        conv_outputs = np.squeeze(conv_outputs)
        prediction = np.argmax(predictions)
        print(predictions)
        print(prediction)
        print(conv_outputs)
        print(conv_outputs.shape)
        class_weights = class_weights[:,prediction]
        mat_for_mult = scipy.ndimage.zoom(conv_outputs,(32,32,1),order=1)
        final_output = np.dot(mat_for_mult.reshape((224*224, 512)),class_weights).reshape((224,224))
        print(final_output)
        return final_output

但是,cam\u model.predict(im)总是为所有图像提供相同的类。我不确定我错在哪里了。由于pascal voc 2012包含多标签图像,我在修改的_vgg16的最后一层中使用了“sigmoid”,而不是“softmax”。你能告诉我哪里出了问题吗

PASCAL VOC类间图像分布不均衡

数据集中的大多数图像都是针对人物的,这导致了数据分布的不平衡,并造成了对人物类别的偏见

为了避免这种情况,可以使用加权类的方法,这有助于消除类之间的不平衡


有关如何为不平衡类设置类权重的更多信息,请参阅。

这是否也发生在培训期间?PASCAL VOC具有多标签图像。我注意到大约50%的图片有“Person”类标签,现在所有的图片都被归类为“Person”。只是想知道这是否是问题所在。如果是这样,我们如何解决这个问题。因为您使用的是sigmoid,所以每个类都应该是独立的。我注意到您使用的是
np.argmax(预测)
,如果在最后一层上应用softmax,这将是有意义的。但是,对于多标签任务,通常我会根据每个类的阈值预测多个类。@zihaozhihao,是的,我看到了,我曾想过更改为阈值,但我对这个问题感到震惊。“人”类在这里的概率总是最高的。这个不平衡的问题可以在训练中得到缓解,比如引入类权重。