如何使用彩色图像进行Tensorflow图像分类?

时间:2019-04-13 03:44:26

标签: python tensorflow keras

我正在关注Tensorflow image classification教程,该教程使用Fashion MNIST数据集。每个图像都是28x28灰度图像:

[GADMobileAds.sharedInstance startWithCompletionHandler:]

...在本教程的后面部分对其进行了规范化,并将其输入到Flatten层中。

df2 = df.assign(Roll=df.Roll.ffill(), GPA=df.GPA.bfill())

我有一个使用matplotlib导入的彩色PNG数据集,其形状为:

train_images[0].shape
(28, 28)

鉴于图像(28,28)与(400,400,3)的形状不同,如何在不转换为灰度的情况下使分类器使用它呢?

1 个答案:

答案 0 :(得分:0)

一个小例子:

model = keras.Sequential([
keras.layers.Flatten(input_shape=(400, 400, 3)),
keras.layers.Conv2D(32,kernel_size=3,strides=(1,1),activation='relu', padding='same'),
keras.layers.Conv2D(32,kernel_size=3,strides=(2,2),activation='relu', padding='same'),
keras.layers.Conv2D(64,kernel_size=3,strides=(1,1),activation='relu', padding='same'),
keras.layers.Conv2D(64,kernel_size=3,strides=(2,2),activation='relu', padding='same'),
keras.layers.Conv2D(128,kernel_size=3,strides=(1,1),activation='relu', padding='same'),
keras.layers.Conv2D(128,kernel_size=3,strides=(2,2),activation='relu', padding='same'),
keras.layers.Conv2D(256,kernel_size=3,strides=(1,1),activation='relu', padding='same'),
keras.layers.Conv2D(256,kernel_size=3,strides=(2,2),activation='relu', padding='same'),
keras.layers.GlobalAveragePooling2D(),
keras.layers.Dense(128, activation=tf.nn.relu),
keras.layers.Dense(10, activation=tf.nn.softmax)

])

或者您可以使用keras.applications中的预训练模型:

from keras.applications.resnet50 import ResNet50
base_model = ResNet50(weights='imagenet',include_top=False,input_shape=(400,400,3)) 
x=base_model.output
x=GlobalAveragePooling2D()(x)
x=Dense(10,activation='softmax')(x)
model=Model(base_model.input,x)