我们修复我们的图像尺寸、批量大小,和纪元,并编码我们的分类的类标签。TensorFlow 2.0 于 2019 年三月发布,这个练习是尝试它的完美理由。
import tensorflow as tf -
# Load the TensorBoard notebook extension (optional) %load_ext tensorboard.notebook -
tf.random.set_seed(42) tf.__version__ -
# Output '2.0.0-alpha0'
深度学习训练
在模型训练阶段,我们将构建三个深度训练模型,使用我们的训练集训练,使用验证数据比较它们的性能。然后,我们保存这些模型并在之后的模型评估阶段使用它们。
模型 1:从头开始的 CNN
我们的第一个疟疾检测模型将从头开始构建和训练一个基础的 CNN。首先,让我们定义我们的模型架构,
inp = tf.keras.layers.Input(shape=INPUT_SHAPE) -
conv1 = tf.keras.layers.Conv2D(32, kernel_size=(3, 3), activation='relu', padding='same')(inp) pool1 = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(conv1) conv2 = tf.keras.layers.Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same')(pool1) pool2 = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(conv2) conv3 = tf.keras.layers.Conv2D(128, kernel_size=(3, 3), activation='relu', padding='same')(pool2) pool3 = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(conv3) -
flat = tf.keras.layers.Flatten()(pool3) -
hidden1 = tf.keras.layers.Dense(512, activation='relu')(flat) drop1 = tf.keras.layers.Dropout(rate=0.3)(hidden1) hidden2 = tf.keras.layers.Dense(512, activation='relu')(drop1) drop2 = tf.keras.layers.Dropout(rate=0.3)(hidden2) -
out = tf.keras.layers.Dense(1, activation='sigmoid')(drop2) -
model = tf.keras.Model(inputs=inp, outputs=out) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) model.summary() -
-
# Output Model: "model" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= input_1 (InputLayer) [(None, 125, 125, 3)] 0 _________________________________________________________________ conv2d (Conv2D) (None, 125, 125, 32) 896 _________________________________________________________________ max_pooling2d (MaxPooling2D) (None, 62, 62, 32) 0 _________________________________________________________________ conv2d_1 (Conv2D) (None, 62, 62, 64) 18496 _________________________________________________________________ ... ... _________________________________________________________________ dense_1 (Dense) (None, 512) 262656 _________________________________________________________________ dropout_1 (Dropout) (None, 512) 0 _________________________________________________________________ dense_2 (Dense) (None, 1) 513 ================================================================= Total params: 15,102,529 Trainable params: 15,102,529 Non-trainable params: 0 _________________________________________________________________
(编辑:ASP站长网)
|