在第一个时期,培训陷入困境。我正在使用22,000个训练示例来训练CNN模型。
Jupyter笔记本在第一个时期之后一直保持处理并且不运行任何东西,使用python 3.7在keras 2.2.4上运行代码。 这是我的CNN代码,它使用来自训练和测试文件夹的图像进行训练。但是,每当我尝试对其进行培训时,该程序似乎始终卡在时代1/10的位置上,我将其放置了一整夜,持续了8个小时,而且它完全没有进展,我可以尝试进行任何修复吗?
import numpy as np
from keras import layers
import pandas as pd
from tensorflow.keras.layers import Input, Dense, Activation, ZeroPadding2D, BatchNormalization,
Flatten, Conv2D
from tensorflow.keras.layers import AveragePooling2D, MaxPooling2D, Dropout, GlobalMaxPooling2D,
GlobalAveragePooling2D
from tensorflow.keras.models import Sequential
from tensorflow.keras import regularizers, optimizers
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import keras.backend as K
K.set_image_data_format('channels_last')
train_dir = r"D:\\Downloads\\cat vs dod\\PetImages\\train"
test_dir = r"D:\\Downloads\\cat vs dod\\PetImages\\test"
img_width, img_height = 300,281
input_shape = img_width, img_height, 3
train_samples = 22998
test_samples = 2002
epochs = 30
batch_size = 500
train_datagen = ImageDataGenerator(
rescale = 1. /255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(
rescale = 1. /255)
train_data = train_datagen.flow_from_directory(
train_dir,
target_size = (img_width, img_height),
batch_size = batch_size,
class_mode = 'binary')
test_data = test_datagen.flow_from_directory(
test_dir,
target_size = (img_width, img_height),
batch_size = batch_size,
class_mode = 'binary')
model = Sequential()
model.add(Conv2D(32, (7, 7), strides = (1, 1), input_shape = input_shape))
model.add(BatchNormalization(axis = 3))
model.add(Activation('relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (7, 7), strides = (1, 1)))
model.add(BatchNormalization(axis = 3))
model.add(Activation('relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss = 'binary_crossentropy',
optimizer = 'rmsprop',
metrics = ['accuracy'])
model.build(input_shape)
model.summary()
model.fit_generator(
train_data,
steps_per_epoch = train_samples//batch_size,
epochs = epochs,
validation_data = test_data,
verbose = 1,
validation_steps = test_samples//batch_size)
model.save_weights("D:\\Downloads\\cat vs dod\\simple_CNN.h5")