数据集: visual_domain_decathlon/cifar100
Config description: Data based on "CIFAR-100", with images resized isotropically to have a shorter size of 72 pixels
train: 40000 张图片
test: 10000 张图片
validation: 10000 张图片
类别数为 100
训练的时候采纳 180 x 180 x 3
其中 NASNetMobile 非凡一些,须要 resize 成 224 x 224 x 3
第一阶段,咱们利用在 ImageNet 上做过预训练的模型来做 feature extraction,意思就是要 freeze 预训练模型的卷积局部,而后只训练新增加的 top-classifier,训练后果如下图所示
此处咱们能够看到,val_acc 最高的是 ResNet50,值为 0.7421,其实最高的是 ResNet101,然而思考到计算量,咱们取 ResNet50。不过这里比拟神奇的是 ResNet50 的 val_acc 居然是最高的,猜想是数据集的分辨率大小问题,毕竟咱们此次的工作,原始图像分辨率只有 72 x 72 x 3。
咱们粘贴一下第一阶段的代码
rand_aug = iaa.RandAugment(n=3, m=7)def augment(images): # Input to `augment()` is a TensorFlow tensor which # is not supported by `imgaug`. This is why we first # convert it to its `numpy` variant. images = tf.cast(images, tf.uint8) return rand_aug(images=images.numpy())AUTOTUNE = tf.data.AUTOTUNEtrain_ds = train_ds.shuffle(buffer_size=len(train_ds)).cache().batch(batch_size).map( lambda x, y: (tf.py_function(augment, [x], [tf.float32])[0], y), num_parallel_calls=AUTOTUNE).prefetch( buffer_size=AUTOTUNE)val_ds = val_ds.cache().batch(batch_size).prefetch(buffer_size=AUTOTUNE)preprocess_input = tf.keras.applications.resnet.preprocess_inputbase_model = tf.keras.applications.ResNet101(input_shape=img_size, include_top=False, weights='imagenet')
这里,我没有粘贴全副的代码,如果须要查看源码,请到这里: https://github.com/MaoXianXin...
如上图所示,咱们须要 checkout 对应的分支。
基于此,咱们对 ResNet50 和 InceptionResNetV2 别离做了 fine-tune,后果如下所示
此处未对第一阶段的所有模型做 fine-tune,从上图能够发现,还是 ResNet50 的 val_acc 略高,不过到这里为止,咱们在 visual_domain_decathlon/cifar100 上的 val_acc 还是低了些,只有 0.8041,须要做改良。
preprocess_input = tf.keras.applications.resnet50.preprocess_inputbase_model = tf.keras.applications.ResNet50(input_shape=img_size, include_top=False, weights='imagenet')base_model.trainable = True# Let's take a look to see how many layers are in the base modelprint("Number of layers in the base model: ", len(base_model.layers))# Fine-tune from this layer onwardsfine_tune_at = 120# Freeze all the layers before the `fine_tune_at` layerfor layer in base_model.layers[:fine_tune_at]: layer.trainable = Falseinputs = tf.keras.Input(shape=img_size)x = data_augmentation(inputs)x = preprocess_input(x)x = base_model(x, training=False)x = tf.keras.layers.GlobalAveragePooling2D()(x)x = tf.keras.layers.Dropout(0.2)(x)outputs = tf.keras.layers.Dense(num_classes)(x)model = tf.keras.Model(inputs, outputs)model.load_weights('./save_model/my_model_1')print(model.summary())
最初上一下 fine-tune 阶段的代码,这里须要留神的是,不同模型,网络层数不一样,所以 fine_tune_at 这个参数咱们须要看状况而定,还有就是加载模型的地址不要搞错。