From 4f3d070b2c70954352fb67fdc7e399dc9c81b71f Mon Sep 17 00:00:00 2001 From: dberth Date: Wed, 16 Dec 2020 12:19:32 -0800 Subject: [PATCH] Updated for TensorFlow 2.x --- ablation/ab_fixmatch_adam.py | 2 +- ablation/ab_fixmatch_anchoring.py | 2 +- ablation/ab_fixmatch_linearlr.py | 2 +- ablation/ab_fixmatch_meanteacher.py | 2 +- ablation/ab_fixmatch_meanteacher_train.py | 2 +- ablation/ab_fixmatch_momentum.py | 2 +- ablation/ab_fixmatch_ra.py | 2 +- ablation/ab_fixmatch_sharpen.py | 2 +- ablation/ab_fixmatch_sharpen_conditional_consistency.py | 2 +- fixmatch.py | 2 +- fully_supervised/fs_baseline.py | 2 +- fully_supervised/fs_mixup.py | 2 +- fully_supervised/lib/train.py | 2 +- ict.py | 2 +- imagenet/augment/augment_ops.py | 3 +-- imagenet/augment/ct_augment.py | 3 +-- imagenet/augment/rand_augment.py | 2 +- imagenet/augment/weak_augment.py | 2 +- imagenet/datasets/imagenet.py | 7 +++---- imagenet/fixmatch.py | 4 +--- imagenet/models/resnet50_model.py | 2 +- imagenet/prep_imagenet_data/save_sharded_data.py | 7 ++----- imagenet/supervised.py | 4 +--- imagenet/training.py | 4 +--- imagenet/utils/ema.py | 2 +- imagenet/utils/learning_rate.py | 2 +- libml/augment.py | 2 +- libml/data.py | 2 +- libml/layers.py | 2 +- libml/models.py | 2 +- libml/train.py | 2 +- libml/utils.py | 3 ++- mean_teacher.py | 2 +- mixmatch.py | 2 +- mixup.py | 2 +- pi_model.py | 2 +- pseudo_label.py | 2 +- remixmatch_no_cta.py | 2 +- requirements.txt | 2 +- scripts/aggregate_accuracy.py | 2 +- scripts/check_split.py | 2 +- scripts/create_datasets.py | 2 +- scripts/create_split.py | 2 +- scripts/create_unlabeled.py | 2 +- scripts/extract_accuracy.py | 2 +- scripts/inspect_dataset.py | 2 +- third_party/auto_augment/custom_ops.py | 2 +- third_party/auto_augment/shake_drop.py | 2 +- third_party/auto_augment/shake_shake.py | 2 +- third_party/auto_augment/wrn.py | 2 +- third_party/vat_utils.py | 2 +- uda.py | 2 +- vat.py | 2 +- 53 files changed, 57 insertions(+), 68 deletions(-) diff --git a/ablation/ab_fixmatch_adam.py b/ablation/ab_fixmatch_adam.py index 8666f3c..e154c79 100644 --- a/ablation/ab_fixmatch_adam.py +++ b/ablation/ab_fixmatch_adam.py @@ -15,7 +15,7 @@ import os import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags diff --git a/ablation/ab_fixmatch_anchoring.py b/ablation/ab_fixmatch_anchoring.py index d6468ea..686b7c0 100644 --- a/ablation/ab_fixmatch_anchoring.py +++ b/ablation/ab_fixmatch_anchoring.py @@ -15,7 +15,7 @@ import os import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags diff --git a/ablation/ab_fixmatch_linearlr.py b/ablation/ab_fixmatch_linearlr.py index 8fcbd43..5c58f64 100644 --- a/ablation/ab_fixmatch_linearlr.py +++ b/ablation/ab_fixmatch_linearlr.py @@ -14,7 +14,7 @@ import functools import os -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags diff --git a/ablation/ab_fixmatch_meanteacher.py b/ablation/ab_fixmatch_meanteacher.py index 0717a62..53b5c8d 100644 --- a/ablation/ab_fixmatch_meanteacher.py +++ b/ablation/ab_fixmatch_meanteacher.py @@ -15,7 +15,7 @@ import os import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags diff --git a/ablation/ab_fixmatch_meanteacher_train.py b/ablation/ab_fixmatch_meanteacher_train.py index 830b458..88fbb19 100644 --- a/ablation/ab_fixmatch_meanteacher_train.py +++ b/ablation/ab_fixmatch_meanteacher_train.py @@ -15,7 +15,7 @@ import os import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags diff --git a/ablation/ab_fixmatch_momentum.py b/ablation/ab_fixmatch_momentum.py index a041db3..1a632fd 100644 --- a/ablation/ab_fixmatch_momentum.py +++ b/ablation/ab_fixmatch_momentum.py @@ -15,7 +15,7 @@ import os import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags diff --git a/ablation/ab_fixmatch_ra.py b/ablation/ab_fixmatch_ra.py index a7815c8..d61d73d 100644 --- a/ablation/ab_fixmatch_ra.py +++ b/ablation/ab_fixmatch_ra.py @@ -15,7 +15,7 @@ import os import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags from tqdm import trange diff --git a/ablation/ab_fixmatch_sharpen.py b/ablation/ab_fixmatch_sharpen.py index cb826bc..27ab277 100644 --- a/ablation/ab_fixmatch_sharpen.py +++ b/ablation/ab_fixmatch_sharpen.py @@ -15,7 +15,7 @@ import os import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags diff --git a/ablation/ab_fixmatch_sharpen_conditional_consistency.py b/ablation/ab_fixmatch_sharpen_conditional_consistency.py index b236728..0ca22cf 100644 --- a/ablation/ab_fixmatch_sharpen_conditional_consistency.py +++ b/ablation/ab_fixmatch_sharpen_conditional_consistency.py @@ -15,7 +15,7 @@ import os import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags diff --git a/fixmatch.py b/fixmatch.py index ac3c80a..bc5e474 100644 --- a/fixmatch.py +++ b/fixmatch.py @@ -15,7 +15,7 @@ import os import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags from tqdm import trange diff --git a/fully_supervised/fs_baseline.py b/fully_supervised/fs_baseline.py index 8e97a22..f37c5b4 100644 --- a/fully_supervised/fs_baseline.py +++ b/fully_supervised/fs_baseline.py @@ -17,7 +17,7 @@ import functools import os -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags diff --git a/fully_supervised/fs_mixup.py b/fully_supervised/fs_mixup.py index 672dd80..ab0c2ae 100644 --- a/fully_supervised/fs_mixup.py +++ b/fully_supervised/fs_mixup.py @@ -16,7 +16,7 @@ import os -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags diff --git a/fully_supervised/lib/train.py b/fully_supervised/lib/train.py index 6d5151e..f9ed7a9 100644 --- a/fully_supervised/lib/train.py +++ b/fully_supervised/lib/train.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import flags from tqdm import trange diff --git a/ict.py b/ict.py index c52b570..fe00987 100644 --- a/ict.py +++ b/ict.py @@ -19,7 +19,7 @@ import functools import os -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags diff --git a/imagenet/augment/augment_ops.py b/imagenet/augment/augment_ops.py index db66245..3e6c902 100755 --- a/imagenet/augment/augment_ops.py +++ b/imagenet/augment/augment_ops.py @@ -15,10 +15,9 @@ import math -import tensorflow as tf +import tensorflow.compat.v1 as tf import tensorflow_addons as tfa - # Default replace value REPLACE_VALUE = 128 diff --git a/imagenet/augment/ct_augment.py b/imagenet/augment/ct_augment.py index 0a32dbc..29665ba 100755 --- a/imagenet/augment/ct_augment.py +++ b/imagenet/augment/ct_augment.py @@ -13,11 +13,10 @@ # limitations under the License. """Control theory augment.""" -import tensorflow as tf +import tensorflow.compat.v1 as tf from augment import augment_ops - IMAGENET_AUG_OPS = [ 'AutoContrastBlend', 'Blur', diff --git a/imagenet/augment/rand_augment.py b/imagenet/augment/rand_augment.py index 7f14019..60d7424 100755 --- a/imagenet/augment/rand_augment.py +++ b/imagenet/augment/rand_augment.py @@ -13,7 +13,7 @@ # limitations under the License. """Random augment.""" -import tensorflow as tf +import tensorflow.compat.v1 as tf from augment import augment_ops diff --git a/imagenet/augment/weak_augment.py b/imagenet/augment/weak_augment.py index 02b835d..0ff5134 100755 --- a/imagenet/augment/weak_augment.py +++ b/imagenet/augment/weak_augment.py @@ -13,7 +13,7 @@ # limitations under the License. """Various weak agumentations.""" -import tensorflow as tf +import tensorflow.compat.v1 as tf def flip_augmentation(image): diff --git a/imagenet/datasets/imagenet.py b/imagenet/datasets/imagenet.py index 0008668..3eb19de 100755 --- a/imagenet/datasets/imagenet.py +++ b/imagenet/datasets/imagenet.py @@ -14,12 +14,11 @@ """Efficient ImageNet input pipeline using tf.data.Dataset.""" import os -from absl import flags - -import tensorflow as tf +import tensorflow.compat.v1 as tf +from absl import flags -IMAGENET_BUFFER_SIZE = 16 * 1024 * 1024 # 16 MiB +IMAGENET_BUFFER_SIZE = 16 * 1024 * 1024 # 16 MiB IMAGENET_FETCH_CYCLE_LENGTH = 16 IMAGENET_SHUFFLE_BUFFER_SIZE = 1024 IMAGENET_PREPROCESSING_THREADS = 2 diff --git a/imagenet/fixmatch.py b/imagenet/fixmatch.py index 7bfbc2e..91cf6e6 100755 --- a/imagenet/fixmatch.py +++ b/imagenet/fixmatch.py @@ -13,13 +13,12 @@ # limitations under the License. """Fixmatch semi-supervised training code.""" +import tensorflow.compat.v1 as tf from absl import app from absl import flags from absl import logging from easydict import EasyDict -import tensorflow as tf - import training from augment.augment import create_ssl_augmenter from datasets import datasets @@ -27,7 +26,6 @@ from utils import ema from utils.learning_rate import ConfigurableLearningRateSchedule - FLAGS = flags.FLAGS diff --git a/imagenet/models/resnet50_model.py b/imagenet/models/resnet50_model.py index c3f6439..1b0c300 100755 --- a/imagenet/models/resnet50_model.py +++ b/imagenet/models/resnet50_model.py @@ -19,7 +19,7 @@ - http://torch.ch/blog/2016/02/04/resnets.html """ -import tensorflow as tf +import tensorflow.compat.v1 as tf backend = tf.keras.backend diff --git a/imagenet/prep_imagenet_data/save_sharded_data.py b/imagenet/prep_imagenet_data/save_sharded_data.py index 625d7f6..bbd2f9a 100755 --- a/imagenet/prep_imagenet_data/save_sharded_data.py +++ b/imagenet/prep_imagenet_data/save_sharded_data.py @@ -17,15 +17,12 @@ import os import time +import apache_beam as beam +import tensorflow.compat.v1 as tf from absl import app from absl import flags - -import apache_beam as beam from apache_beam.options.pipeline_options import PipelineOptions -import tensorflow as tf - - FLAGS = flags.FLAGS flags.DEFINE_string('input_dir', None, 'Directory with input data.') diff --git a/imagenet/supervised.py b/imagenet/supervised.py index 72f8e1c..92783a4 100755 --- a/imagenet/supervised.py +++ b/imagenet/supervised.py @@ -13,12 +13,11 @@ # limitations under the License. """Supervised training code.""" +import tensorflow.compat.v1 as tf from absl import app from absl import flags from absl import logging -import tensorflow as tf - import training from augment.augment import create_augmenter from datasets import datasets @@ -26,7 +25,6 @@ from utils import ema from utils.learning_rate import ConfigurableLearningRateSchedule - FLAGS = flags.FLAGS diff --git a/imagenet/training.py b/imagenet/training.py index f034cae..76c4333 100755 --- a/imagenet/training.py +++ b/imagenet/training.py @@ -20,13 +20,11 @@ import os import time +import tensorflow.compat.v1 as tf from absl import flags from absl import logging from easydict import EasyDict -import tensorflow as tf - - flags.DEFINE_string( 'tpu', None, diff --git a/imagenet/utils/ema.py b/imagenet/utils/ema.py index eb4fdbc..38ffa4b 100755 --- a/imagenet/utils/ema.py +++ b/imagenet/utils/ema.py @@ -13,7 +13,7 @@ # limitations under the License. """Helper functions for exponential moving average.""" -import tensorflow as tf +import tensorflow.compat.v1 as tf def assign_ema_vars_from_initial_values(ema_variables, initial_values): diff --git a/imagenet/utils/learning_rate.py b/imagenet/utils/learning_rate.py index 74f8498..cff510a 100755 --- a/imagenet/utils/learning_rate.py +++ b/imagenet/utils/learning_rate.py @@ -13,7 +13,7 @@ # limitations under the License. """Utils for learning rate schedule.""" -import tensorflow as tf +import tensorflow.compat.v1 as tf class ConfigurableLearningRateSchedule( diff --git a/libml/augment.py b/libml/augment.py index b02c344..a92ba04 100644 --- a/libml/augment.py +++ b/libml/augment.py @@ -20,7 +20,7 @@ import random import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import flags from libml import utils, ctaugment diff --git a/libml/data.py b/libml/data.py index 187c7bf..dac2c28 100644 --- a/libml/data.py +++ b/libml/data.py @@ -19,7 +19,7 @@ import os import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags from tqdm import tqdm diff --git a/libml/layers.py b/libml/layers.py index e7415ed..3a97dc2 100644 --- a/libml/layers.py +++ b/libml/layers.py @@ -16,7 +16,7 @@ import numbers import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf from libml.data import DataSets diff --git a/libml/models.py b/libml/models.py index 73059e6..59dcc17 100644 --- a/libml/models.py +++ b/libml/models.py @@ -15,7 +15,7 @@ import functools import itertools -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import flags from libml import layers diff --git a/libml/train.py b/libml/train.py index 2b3760a..7bce017 100644 --- a/libml/train.py +++ b/libml/train.py @@ -18,7 +18,7 @@ import shutil import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import flags from tqdm import trange, tqdm diff --git a/libml/utils.py b/libml/utils.py index 3701623..acb65ac 100644 --- a/libml/utils.py +++ b/libml/utils.py @@ -17,7 +17,7 @@ import re import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import flags, logging from tensorflow.python.client import device_lib @@ -49,6 +49,7 @@ def setup_main(): def setup_tf(): os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' logging.set_verbosity(logging.ERROR) + tf.disable_v2_behavior() def smart_shape(x): diff --git a/mean_teacher.py b/mean_teacher.py index 509aa20..3cb01ab 100644 --- a/mean_teacher.py +++ b/mean_teacher.py @@ -21,7 +21,7 @@ import os import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags diff --git a/mixmatch.py b/mixmatch.py index 23b5fe9..0cb9496 100644 --- a/mixmatch.py +++ b/mixmatch.py @@ -22,7 +22,7 @@ import os import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags diff --git a/mixup.py b/mixup.py index a3761f0..c068e7c 100644 --- a/mixup.py +++ b/mixup.py @@ -18,7 +18,7 @@ import functools import os -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags diff --git a/pi_model.py b/pi_model.py index faaad14..d65bdbb 100644 --- a/pi_model.py +++ b/pi_model.py @@ -20,7 +20,7 @@ import os import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags diff --git a/pseudo_label.py b/pseudo_label.py index 0738f3e..49d5407 100644 --- a/pseudo_label.py +++ b/pseudo_label.py @@ -20,7 +20,7 @@ import os import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags diff --git a/remixmatch_no_cta.py b/remixmatch_no_cta.py index 35bcc75..6b48dc8 100644 --- a/remixmatch_no_cta.py +++ b/remixmatch_no_cta.py @@ -16,7 +16,7 @@ import os import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags diff --git a/requirements.txt b/requirements.txt index 72ff8b6..85288b2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,7 +2,7 @@ absl-py easydict cython numpy -tensorflow-gpu==1.14.0 +tensorflow-gpu>=2.0.0 tqdm scipy pillow diff --git a/scripts/aggregate_accuracy.py b/scripts/aggregate_accuracy.py index 2b6743e..9868e6f 100755 --- a/scripts/aggregate_accuracy.py +++ b/scripts/aggregate_accuracy.py @@ -20,7 +20,7 @@ import sys import threading -import tensorflow as tf +import tensorflow.compat.v1 as tf import tqdm from absl import app from absl import flags diff --git a/scripts/check_split.py b/scripts/check_split.py index 00111a9..4c4cc35 100755 --- a/scripts/check_split.py +++ b/scripts/check_split.py @@ -22,7 +22,7 @@ import hashlib import os -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags from tqdm import trange diff --git a/scripts/create_datasets.py b/scripts/create_datasets.py index 622a62c..934d991 100755 --- a/scripts/create_datasets.py +++ b/scripts/create_datasets.py @@ -26,7 +26,7 @@ import numpy as np import scipy.io -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from tqdm import trange diff --git a/scripts/create_split.py b/scripts/create_split.py index dc018a6..c01cd5c 100755 --- a/scripts/create_split.py +++ b/scripts/create_split.py @@ -22,7 +22,7 @@ from collections import defaultdict import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags from tqdm import trange, tqdm diff --git a/scripts/create_unlabeled.py b/scripts/create_unlabeled.py index 291833b..22f2dca 100755 --- a/scripts/create_unlabeled.py +++ b/scripts/create_unlabeled.py @@ -22,7 +22,7 @@ from collections import defaultdict import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from tqdm import trange, tqdm diff --git a/scripts/extract_accuracy.py b/scripts/extract_accuracy.py index abe7138..a585f20 100755 --- a/scripts/extract_accuracy.py +++ b/scripts/extract_accuracy.py @@ -23,7 +23,7 @@ import os.path import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags diff --git a/scripts/inspect_dataset.py b/scripts/inspect_dataset.py index 0e7a754..f168a99 100755 --- a/scripts/inspect_dataset.py +++ b/scripts/inspect_dataset.py @@ -18,7 +18,7 @@ """ import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags from tqdm import trange diff --git a/third_party/auto_augment/custom_ops.py b/third_party/auto_augment/custom_ops.py index 133cf79..eee2e0f 100644 --- a/third_party/auto_augment/custom_ops.py +++ b/third_party/auto_augment/custom_ops.py @@ -24,7 +24,7 @@ from __future__ import print_function import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf arg_scope = tf.contrib.framework.arg_scope FLAGS = tf.flags.FLAGS diff --git a/third_party/auto_augment/shake_drop.py b/third_party/auto_augment/shake_drop.py index 0a21b50..f2eb47c 100644 --- a/third_party/auto_augment/shake_drop.py +++ b/third_party/auto_augment/shake_drop.py @@ -22,7 +22,7 @@ import math -import tensorflow as tf +import tensorflow.compat.v1 as tf import third_party.auto_augment.custom_ops as ops diff --git a/third_party/auto_augment/shake_shake.py b/third_party/auto_augment/shake_shake.py index 171185a..2c76596 100644 --- a/third_party/auto_augment/shake_shake.py +++ b/third_party/auto_augment/shake_shake.py @@ -20,7 +20,7 @@ from __future__ import division from __future__ import print_function -import tensorflow as tf +import tensorflow.compat.v1 as tf import third_party.auto_augment.custom_ops as ops diff --git a/third_party/auto_augment/wrn.py b/third_party/auto_augment/wrn.py index 03d315d..2c00c77 100644 --- a/third_party/auto_augment/wrn.py +++ b/third_party/auto_augment/wrn.py @@ -19,7 +19,7 @@ from __future__ import print_function import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf import third_party.auto_augment.custom_ops as ops diff --git a/third_party/vat_utils.py b/third_party/vat_utils.py index 5b82e12..925bf30 100644 --- a/third_party/vat_utils.py +++ b/third_party/vat_utils.py @@ -1,6 +1,6 @@ """Utilities derived from the VAT code.""" -import tensorflow as tf +import tensorflow.compat.v1 as tf def generate_perturbation(x, logit, forward, epsilon, xi=1e-6): diff --git a/uda.py b/uda.py index 90e3848..ecd821a 100644 --- a/uda.py +++ b/uda.py @@ -19,7 +19,7 @@ import os import numpy as np -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags from tqdm import trange diff --git a/vat.py b/vat.py index 275512f..ef7d09a 100644 --- a/vat.py +++ b/vat.py @@ -19,7 +19,7 @@ import functools import os -import tensorflow as tf +import tensorflow.compat.v1 as tf from absl import app from absl import flags