[英]Keras Error Logits and Labels must have the same shape ((None, 17, 17, 1) vs (None, 1))
I coded a horse and human classifier as a beginner我作为初学者编写了一个马和人分类器
# 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)
but when executed run with these warnings但是执行时会出现这些警告
2020-10-26 15:36:58.620164: W tensorflow/core/kernels/data/generator_dataset_op.cc:103] Error occurred when finalizing GeneratorDataset iterator: Cancelled: Operation was cancelled
2020-10-26 15:36:58.620164: W tensorflow/core/kernels/data/generator_dataset_op.cc:103] 完成 GeneratorDataset 迭代器时发生错误:已取消:操作已取消
Figures now render in the Plots pane by default.
默认情况下,图形现在呈现在“绘图”窗格中。 To make them also appear inline in the Console, uncheck "Mute Inline Plotting" under the Plots pane options menu.
要使它们也内联显示在控制台中,请取消选中“绘图”窗格选项菜单下的“静音内联绘图”。
Well as one can see there is no accuracy
, loss
, validation_loss
or validation_accuracy
values and the above msg was logged for all 100 epochs, Why is it so正如人们所看到的,没有
accuracy
、 loss
、 validation_loss
或validation_accuracy
值,并且上面的消息被记录了所有 100 个时期,为什么会这样
I think you forgot to flatten your output tensor of the layer tf.keras.layers.MaxPool2D(2,2)
.我想你忘了展平
tf.keras.layers.MaxPool2D(2,2)
层的 output 张量。 Just add a Flatten
layer and hopefully it will work.只需添加一个
Flatten
层,希望它会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.