无法挤压dim [1],预期尺寸为1,得到14 [[{{nodemetric_2 / acc / Squeeze}}]]

时间:2019-08-25 12:17:42

标签: tensorflow

我已经在python中编写了用于图像分类的代码,但是在编写代码后,出现了此错误,并且在图像数据集中,存在最小尺寸的图像。 655x53,我的任务是将细胞图像分为14类。共有6889张训练集图像和3771张测试/验证集图像

import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_dir = '/vista/train1'
validation_dir = '/vista/test1'

train_data = ImageDataGenerator(rescale = 1/255,
                            rotation_range = 20,
                            width_shift_range = 0.2,
                            height_shift_range = 0.2,
                            zoom_range = 0.2,
                            horizontal_flip = True,
                            fill_mode = 'nearest')

validation_data = ImageDataGenerator(rescale = 1/255)

train_gen = train_data.flow_from_directory(train_dir,
                                        batch_size = 30,
                                        class_mode = 'categorical',
                                        target_size = (400,200))

validation_gen = validation_data.flow_from_directory(validation_dir,
                                                 batch_size = 30,
                                                 class_mode = 
'categorical',
                                                 target_size = (400,200))

model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(64,(3,3),activation = 'relu',input_shape = 
(400,200,3)),
    tf.keras.layers.MaxPooling2D(2,2),

    tf.keras.layers.Conv2D(128,(3,3),activation = 'relu'),
    tf.keras.layers.MaxPooling2D(2,2),

    tf.keras.layers.Conv2D(128,(3,3),activation = 'relu'),
    tf.keras.layers.MaxPooling2D(2,2),

    tf.keras.layers.Conv2D(128,(3,3),activation = 'relu'),
    tf.keras.layers.MaxPooling2D(2,2),

    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(256,activation = 'relu'),
    tf.keras.layers.Dropout(0.20),
    tf.keras.layers.Dense(14,activation = 'softmax')
    ])

model.compile(optimizer = 'Adam',
          loss = 'sparse_categorical_crossentropy',
          metrics = ['acc'])

df = int(3711/30)
ef = int(6889/30)

history = model.fit_generator(train_gen,
                          steps_per_epoch = df,
                          epochs = 20,
                          validation_data = validation_gen,
          `enter code here`                validation_steps = ef,
                          verbose =2)

model.save_weights('/vista',overwrite=True,save_format = '.h5')

0 个答案:

没有答案