|
| 1 | +import os |
| 2 | +import random as python_random |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +import tensorflow as tf |
| 6 | +import tensorflow_datasets as tfds |
| 7 | +from tensorflow.keras.models import load_model |
| 8 | + |
| 9 | +from gradient_accumulator import GradientAccumulateModel |
| 10 | +from gradient_accumulator.layers import AccumBatchNormalization |
| 11 | +from gradient_accumulator.utils import replace_batchnorm_layers |
| 12 | + |
| 13 | +from .utils import gray2rgb |
| 14 | +from .utils import normalize_img |
| 15 | +from .utils import reset |
| 16 | +from .utils import resizeImage |
| 17 | + |
| 18 | + |
| 19 | +def test_swap_layer( |
| 20 | + custom_bn: bool = True, bs: int = 100, accum_steps: int = 1, epochs: int = 1 |
| 21 | +): |
| 22 | + # load dataset |
| 23 | + (ds_train, ds_test), ds_info = tfds.load( |
| 24 | + "mnist", |
| 25 | + split=["train", "test"], |
| 26 | + shuffle_files=True, |
| 27 | + as_supervised=True, |
| 28 | + with_info=True, |
| 29 | + ) |
| 30 | + |
| 31 | + # build train pipeline |
| 32 | + ds_train = ds_train.map(normalize_img) |
| 33 | + ds_train = ds_train.map(gray2rgb) |
| 34 | + ds_train = ds_train.map(resizeImage) |
| 35 | + ds_train = ds_train.shuffle(ds_info.splits["train"].num_examples) |
| 36 | + ds_train = ds_train.batch(bs) |
| 37 | + ds_train = ds_train.prefetch(1) |
| 38 | + |
| 39 | + # build test pipeline |
| 40 | + ds_test = ds_test.map(normalize_img) |
| 41 | + ds_test = ds_test.map(gray2rgb) |
| 42 | + ds_test = ds_test.map(resizeImage) |
| 43 | + ds_test = ds_test.batch(bs) |
| 44 | + ds_test = ds_test.prefetch(1) |
| 45 | + |
| 46 | + # create model |
| 47 | + base_model = tf.keras.applications.MobileNetV2(input_shape=(32, 32, 3), weights="imagenet", include_top=False) |
| 48 | + base_model = replace_batchnorm_layers(base_model, accum_steps=accum_steps) |
| 49 | + |
| 50 | + input_ = tf.keras.layers.Input(shape=(32, 32, 3)) |
| 51 | + x = base_model(input_) |
| 52 | + x = tf.keras.layers.Dense(10, activation="softmax")(x) |
| 53 | + model = tf.keras.Model(inputs=input_, outputs=x) |
| 54 | + |
| 55 | + # wrap model to use gradient accumulation |
| 56 | + if accum_steps > 1: |
| 57 | + model = GradientAccumulateModel( |
| 58 | + accum_steps=accum_steps, inputs=model.input, outputs=model.output |
| 59 | + ) |
| 60 | + |
| 61 | + # compile model |
| 62 | + model.compile( |
| 63 | + optimizer=tf.keras.optimizers.SGD(1e-2), |
| 64 | + loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), |
| 65 | + metrics=[tf.keras.metrics.SparseCategoricalAccuracy()], |
| 66 | + ) |
| 67 | + |
| 68 | + # train model |
| 69 | + model.fit( |
| 70 | + ds_train, |
| 71 | + epochs=epochs, |
| 72 | + validation_data=ds_test, |
| 73 | + steps_per_epoch=4, |
| 74 | + validation_steps=4, |
| 75 | + ) |
| 76 | + |
| 77 | + model.save("./trained_model") |
| 78 | + |
| 79 | + # load trained model and test |
| 80 | + del model |
| 81 | + trained_model = load_model("./trained_model", compile=True) |
| 82 | + |
| 83 | + result = trained_model.evaluate(ds_test, verbose=1) |
| 84 | + print(result) |
| 85 | + return result |
0 commit comments