我一直在使用著名的 dog-vs-cats kaggle 数据集,并试图提出我自己的 CNN 模型。在image_dataset_from_directory将数据集配置到两个分别包含猫和狗图像的文件夹中后,我是使用该方法导入数据集的新手。
这是模型的代码。
from keras.models import Sequential
from keras.layers import Conv2D,MaxPooling2D,\
Dropout,Flatten,Dense,Activation,\
BatchNormalization
model=Sequential()
model.add(Conv2D(32,(3,3),activation='relu',input_shape=(128,128,3)))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(64,(3,3),activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(128,(3,3),activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512,activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.5))
model.add(Dense(2,activation='sigmoid'))
model.compile(loss = 'binary_crossentropy',
optimizer='adam',metrics=['accuracy'])
Run Code Online (Sandbox Code Playgroud)
这是数据集的代码:
Dataset = tf.keras.preprocessing.image_dataset_from_directory(
TRAIN_DIR,
labels="inferred",
label_mode="binary",
class_names=None,
color_mode="rgb",
batch_size=32,
image_size=(128, 128),
shuffle=True,
seed=None,
validation_split=None,
subset=None,
interpolation="bilinear",
follow_links=False,
)
Run Code Online (Sandbox Code Playgroud)
在运行 fit 函数来训练我的 CNN 之后。我看到了这个错误:
ValueError: in user code:
C:\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\training.py:806 train_function *
return step_function(self, iterator)
C:\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\training.py:796 step_function **
outputs = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Google Colab 中的 TPU 创建和训练我的 CNN 模型。我打算用它来对狗和猫进行分类。该模型使用 GPU/CPU 运行时运行,但在 TPU 运行时运行时遇到问题。这是创建模型的代码。
我使用 flow_from_directory() 函数输入我的数据集,这是它的代码
train_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
MAIN_DIR,
target_size = (128,128),
batch_size = 50,
class_mode = 'binary'
)
Run Code Online (Sandbox Code Playgroud)
def create_model():
model=Sequential()
model.add(Conv2D(32,(3,3),activation='relu',input_shape=(128,128,3)))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(64,(3,3),activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(128,(3,3),activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512,activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.5))
model.add(Dense(2,activation='softmax'))
return model
Run Code Online (Sandbox Code Playgroud)
这是用于在 google Colab 上启动 TPU 的代码
tf.keras.backend.clear_session()
resolver = tf.distribute.cluster_resolver.TPUClusterResolver('grpc://' + os.environ['COLAB_TPU_ADDR'])
tf.config.experimental_connect_to_cluster(resolver)
# This is the TPU initialization code that has to be at the …Run Code Online (Sandbox Code Playgroud) machine-learning keras tensorflow google-colaboratory google-cloud-tpu