将比图像更多的信息添加到 keras 中的图像分类器 [英] Adding More information than a image to an image classifier in keras

查看:36
本文介绍了将比图像更多的信息添加到 keras 中的图像分类器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 keras 制作一个图像分类器来预测乳腺癌病例,直到这里我遇到了一些麻烦,因为它不是一个简单的"classifire,我无法使用 keras 的传统方法来做到这一点.所以我试图添加更多的信息而不仅仅是一个图像,比如一个数组 [图像、年龄、密度].我是新手,所以我不知道是否有可能或如何寻找正确的方法来做到这一点,我正在尝试这样做:

i am trying to make an image classifier with keras to predict cases of breast câncer, i had some troubles until here because it's not a "simple" classifire, i couldn't do this with the traditional methods with keras. So i am trying to add more information than just a image, like an array [image, age, density]. I am new at this so i dont know if it's possible or how to search for the rigth way to do this, i am trying to do this this way:

X = np.array(X) \\ the image

y = np.array(y) \\ an array with 1 or 0 (cancer or not)

z = np.array(z) \\ the density of the breast

\ al 数组对应于相同的情况,如 X[0] 是图像 y[0] 如果这种情况是否是癌症和 z[0]这种情况下的密度

\ al the arrays correspond to the same case like X[0] is the image y[0] if this case is cancer or not and z[0] the density os this case

model = Sequential()

model.add(Conv2D(64,(3,3), activation = 'relu'))
model.add(MaxPooling2D((2,2)))

model.add(Conv2D(64,(3,3), activation = 'relu'))
model.add(MaxPooling2D((2,2)))

model.add(Flatten())

model.add(Dense(128, input_shape = X.shape[1:],activation = 'relu'))
model.add(Dense(2,activation = 'softmax')) 
model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])


model.fit(X,z,y, epochs = 20) // i actually don't know how to give the information to the model

这是错误:

具有多个元素的数组的真值是不明确的.使用 a.any()a.all()

The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

推荐答案

Fit 不带这样的参数.如果您查看 函数定义,则输入第一个参数,第二个是目标预测,第三个是批量大小.

Fit does not take parameter like that. If you look at the function definition the first parameter is input, second is target prediction, and the third is batch size.

您应该做的是连接 X 和 z(以及任何其他信息),因为它们都是输入数据.例如

What you should do is concatenate X and z (and any other info), as they are both input data. e.g.

Xz = np.concatenate((X, z[..., np.newaxis]), axis=-1)

注意:z[..., np.newaxis] 取一个大小为 [H, W] 的数组并使其成为 [H, W,1] 这样你就可以把它和 X 连接起来,我假设它是一个形状为 [H,W,3] 的 RGB 图像.如果是灰度,就忽略这个,简单使用z.

Note: z[..., np.newaxis] takes an array with size [H, W] and makes it [H, W, 1] so that you can concatenate it with X, which I assume is an RGB image with shape [H,W,3]. If it is greyscale, just ignore this and simple use z.

最终您想要的是具有维度 [H,W,C] 的输入,其中 C 是所有数据的维度,例如[红、绿、蓝、年龄、密度等].在网络设计中,将年龄等非图像信息注入网络的最后一层(例如 Dense(128))可能更有意义

Utimately what you want is the input to have dimension [H,W,C] where C is the dimension of all the data, e.g. [red, green, blue, age, density, etc]. It might make more sense in the network design to inject non-image information, like age, in at the final layers of the network (e.g. into Dense(128))

这篇关于将比图像更多的信息添加到 keras 中的图像分类器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆