【发布时间】:2021-02-08 13:09:21
【问题描述】:
作为初学者,我编写了马和人类分类器
# 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 迭代器时发生:已取消: 操作已取消
图形现在默认呈现在“绘图”窗格中。让它们也出现 inline 在控制台中,取消选中“Mute Inline Plotting”下的 绘图窗格选项菜单。
人们可以看到没有accuracy、loss、validation_loss 或validation_accuracy 值,并且所有 100 个时期都记录了上述消息,为什么会这样
【问题讨论】:
-
你需要对最后一个 Conv2D 和最后一个密集层进行一些展平操作
标签: python python-3.x tensorflow keras binary