Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.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'预测单个图像;图像数据发生器_Python_Tensorflow_Keras_Deep Learning - Fatal编程技术网

Python 用Keras'预测单个图像;图像数据发生器

Python 用Keras'预测单个图像;图像数据发生器,python,tensorflow,keras,deep-learning,Python,Tensorflow,Keras,Deep Learning,我对深度学习非常陌生,所以请原谅我这个可能很简单的问题 我训练了一个网络在阳性和阴性之间进行分类。为了简化图像生成和拟合过程,我使用了ImageDataGenerator和fit_generator函数,如下所示: 将tensorflow导入为tf 从tensorflow.keras.preprocessing.image导入ImageDataGenerator #简化模型 模型=tf.keras.models.Sequential([ Conv2D(16,(3,3),激活='relu',输入形

我对深度学习非常陌生,所以请原谅我这个可能很简单的问题

我训练了一个网络在
阳性
阴性
之间进行分类。为了简化图像生成和拟合过程,我使用了
ImageDataGenerator
fit_generator
函数,如下所示:

将tensorflow导入为tf
从tensorflow.keras.preprocessing.image导入ImageDataGenerator
#简化模型
模型=tf.keras.models.Sequential([
Conv2D(16,(3,3),激活='relu',输入形状=(12,12,3)),
tf.keras.layers.MaxPoolig2D(2,2),
tf.keras.layers.flatte(),
tf.keras.layers.Dense(128,活化='relu'),
致密层(1,激活='s形')
])
#图像导入,用于“验证\u生成器”
列车\数据发生器=图像数据发生器(重缩放=1./255)
train_generator=来自目录的train_datagen.flow_(
“/培训/”,
目标_大小=(12,12),
批次大小=128,
class_mode='binary')
#编撰
model.compile(loss='binary\u crossentropy',
优化器='adam',
指标=['acc'])
#配件,用于张力板“历史=型号。配件\u gen…”
型号:安装发电机(列发电机、,
每个历元的步数=8,
纪元=50,
verbose=1,
验证数据=验证生成器,
验证步骤=8,
回调=[tb])#标准张力板
我想使用我的模型预测单个图像(导入为
numpy数组
),如下所示:

image='singleimportedimagewithshape(12,12,3)'
模型预测(图像)
但是,我唯一得到的是一条错误消息,说明矩阵大小不兼容。我在我的
validation\u generator
上尝试了
model.predict\u generator()
,它可以工作,但这不是一个单独的图像


提前感谢。

如果要对单个图像进行预测,请执行以下操作:

image = np.random.rand(12, 12, 3) # single imported image with shape (12, 12, 3)
image = np.expand_dims(image, axis=0) # image shape is (1, 12, 12, 3)
model.predict(image)

换句话说,您的模型仍然希望输入形状为
(None,12,12,3)
。因此,在进行预测之前,请将图像的尺寸扩展为单个图像的一批。

效果不错!非常感谢:)10分钟后我会接受的。不用担心。很高兴我能帮忙!回答得很好。这也帮助了我目前的项目。!