为什么特征向量在Keras VGG16模型的输出中有很多零值?

时间:2018-11-21 20:54:47

标签: python tensorflow keras deep-learning feature-extraction

我正在尝试使用以下代码从Keras中的VGG16模型的最后一层提取特征:

Sub ForwardEmail(myEmail As Outlook.MailItem) 'subroutine called from Outlook rule, when new incoming email message arrives
Const PR_SMTP_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x0076001E"
Set objSMTPMail = CreateObject("CDO.Message") 'needed to send SMTP mail
Set objConf = CreateObject("CDO.Configuration") 'needed for SMTP configuration

Set objFlds = objConf.Fields 'used for SMTP configuration

'Set various parameters and properties of CDO object

objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2     
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtpout.test.com" 'define SMTP server
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 'default port for email

objFlds.Update

objSMTPMail.Configuration = objConf

If myEmail.SenderEmailType = "EX" Then
  objSMTPMail.From = myEmail.Sender.GetExchangeUser.PrimarySmtpAddress
Else
  objSMTPMail.From = myEmail.SenderEmailAddress 'takes email address from   the original email and uses it in the new SMTP email
 objAttachments = myEmail.Attachments  ' I believe this is how to get the attachments

End If

objSMTPMail.Subject = myEmail.Subject 'use the subject from the original email message for the SMTP message
objSMTPMail.HTMLBody = myEmail.HTMLBody 'myEmail.HTMLBody is necessary to retain Electronic Inquiry Form formatting
objSMTPMail.To = "nobody@test.com"
objSMTPMail.AddAttachment objAttachments ' tried to add attachment
'send the SMTP message via the SMTP server
objSMTPMail.Send




'Set all objects to nothing after sending the email

Set objFlds = Nothing
Set objConf = Nothing
Set objSMTPMail = Nothing

End Sub
我认为,由于relu层,

特征变量应该是特征向量,但它有很多零。例如,在Matlab中,提取的特征向量似乎同时具有正值和负值,我如何在keras模型中得到相同的结果?

matlab代码为:

from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as np

model = VGG16(weights='imagenet', include_top=True )


img_path = 'E:\project\KERAS DEEP\poodle.png'
img = image.load_img(img_path, target_size=(224, 224))
img_data = image.img_to_array(img)
img_data = np.expand_dims(img_data, axis=0)
img_data = preprocess_input(img_data)
model.summary()
model.layers.pop();
model.outputs = [model.layers[-1].output]
model.layers[-1].outbound_nodes = []
feature = model.predict(img_data)[0]

两个输出向量im=imread('poodle.png'); im=imresize(im,[224,224]); net=vgg16; trainingFeatures = activations(net, im, 'fc7', ... 'OutputAs', 'rows'); feature如下(左侧是python输出,右侧是Matlab的输出

Samples from the Keras output enter image description here

这是经过测试的图像:

enter image description here

1 个答案:

答案 0 :(得分:0)

在我自己的自定义网络中,我发现无论输入图像如何,矢量中的同一位置通常都出现零,并且这些零维的数量与丢失保持概率有关。

所以我推断出辍学原因。较低的遗漏保持概率可能会导致较少的零,因为需要更多的非零特征来确保重要的特征通过遗漏阶段。

关于辍学为何导致零的原因,我不确定。看看训练期间数字是否变化会很有趣。