Keras错误日志和标签的形状必须相同((None,17,17,1)vs(None,1))

时间:2020-10-26 09:22:15

标签: python python-3.x tensorflow keras binary

我将马和人类分类器编码为初学者

# dependencies
import os
import zipfile
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import RMSprop
import matplotlib.pyplot as plt

# Extracting ZipFiles
zip_path1 = r'C:/Users/91736/Documents/DEEP LEARNING PRACTICE/Week 1/files/horse-or-human.zip'
zip_ref1 = zipfile.ZipFile(zip_path1 , 'r')
zip_ref1.extractall(r'C:/Users/91736/Documents/DEEP LEARNING PRACTICE/Week 1/horse-or-human')
zip_ref1.close()

zip_path2 = r'C:/Users/91736/Documents/DEEP LEARNING PRACTICE/Week 1/files/validation-horse-or-human.zip'
zip_ref2 = zipfile.ZipFile(zip_path2 , 'r')
zip_ref2.extractall(r'C:/Users/91736/Documents/DEEP LEARNING PRACTICE/Week 1/validation-horse-or-human')
zip_ref2.close()


# setting up local dir
train_base_dir = r'C:/Users/91736/Documents/DEEP LEARNING PRACTICE/Week 1/horse-or-human'
valid_base_dir = r'C:/Users/91736/Documents/DEEP LEARNING PRACTICE/Week 1/validation-horse-or-human'

# setting up train and test dir
train_horse_dir = os.path.join(train_base_dir , 'horses')
train_human_dir = os.path.join(train_base_dir , 'humans')


valid_horse_dir = os.path.join(valid_base_dir ,'horses')
valid_human_dir = os.path.join(valid_base_dir , 'humans')


# defining model

model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(filters = 32 ,
                           kernel_size= (3,3) ,
                           input_shape = (150, 150,3),
                           activation = 'relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Conv2D(64 , (3,3) , activation = 'relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Conv2D(128 , (3,3) , activation = 'relu'),
    tf.keras.layers.MaxPool2D(2,2),
    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(512 , activation ='relu'),
    tf.keras.layers.Dense(1 , activation = 'sigmoid')    
    ])

model.compile(loss = 'binary_crossentropy' , optimizer=RMSprop(lr = 0.001) , metrics = ['accuracy'])
model.summary()

# defining augmentation
train_datgen = ImageDataGenerator(rescale = 1./255 ,
                                  rotation_range=40,
                                  width_shift_range= 0.2,
                                  height_shift_range= 0.2,
                                  shear_range= 0.2,
                                  zoom_range = 0.2,
                                  horizontal_flip = True,
                                  fill_mode= 'nearest')

valid_datagen = ImageDataGenerator(rescale = 1./255)




# calling geenrators
train_gen = train_datgen.flow_from_directory(train_base_dir,
                                       target_size = (150, 150),
                                       batch_size = 20,
                                       class_mode = 'binary')

valid_gen = valid_datagen.flow_from_directory(valid_base_dir,
                                        target_size = (150, 150),
                                        batch_size = 20,
                                        class_mode = 'binary')
                                        

history = model.fit_generator(train_gen,
                    validation_data= valid_gen,
                    epochs = 100 ,
                    steps_per_epoch= 10, 
                    validation_steps = 10,
                    verbose = 1)

但是在执行时带有这些警告

2020-10-26 15:36:58.620164:W tensorflow / core / kernels / data / generator_dataset_op.cc:103]错误 最终确定GeneratorDataset迭代器时发生:已取消: 操作被取消

现在默认情况下,“图形”窗格中将渲染图形。使它们也出现 在控制台中内联,请取消选中“静音内联绘图” 绘图窗格选项菜单。

我们可以看到没有accuracylossvalidation_lossvalidation_accuracy的值,并且上面的味精记录了所有100个时期,为什么会这样

1 个答案:

答案 0 :(得分:0)

我认为您忘记展平tf.keras.layers.MaxPool2D(2,2)层的输出张量。只需添加一个Flatten层,希望它会起作用。