diff --git a/.gitignore b/.gitignore index 99c0f9693..c229d37d0 100644 --- a/.gitignore +++ b/.gitignore @@ -92,3 +92,12 @@ ENV/ # PyCharm project setting .idea + +# log +logs/ + +# model +models/ + +# vscode +.vscode diff --git a/requirements.txt b/requirements.txt index b7418c9ac..ed5641ad1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,9 +1,46 @@ -tensorflow==1.7 -scipy -scikit-learn -opencv-python -h5py -matplotlib -Pillow -requests -psutil +absl-py==0.11.0 +astunparse==1.6.3 +cachetools==4.2.1 +certifi==2020.12.5 +chardet==4.0.0 +cycler==0.10.0 +flatbuffers==1.12 +gast==0.3.3 +google-auth==1.24.0 +google-auth-oauthlib==0.4.2 +google-pasta==0.2.0 +grpcio==1.32.0 +h5py==2.10.0 +idna==2.10 +joblib==1.0.0 +Keras-Preprocessing==1.1.2 +kiwisolver==1.3.1 +Markdown==3.3.3 +matplotlib==3.3.4 +numpy==1.19.5 +oauthlib==3.1.0 +opencv-python==4.5.1.48 +opt-einsum==3.3.0 +Pillow==8.1.0 +protobuf==3.14.0 +pyasn1==0.4.8 +pyasn1-modules==0.2.8 +pyparsing==2.4.7 +python-dateutil==2.8.1 +requests==2.25.1 +requests-oauthlib==1.3.0 +rsa==4.7 +scikit-learn==0.24.1 +scipy==1.6.0 +six==1.15.0 +tensorboard==2.4.1 +tensorboard-plugin-wit==1.8.0 +tensorflow-gpu==2.4.1 +tensorflow-estimator==2.4.0 +termcolor==1.1.0 +tf-slim==1.1.0 +threadpoolctl==2.1.0 +typing-extensions==3.7.4.3 +urllib3==1.26.3 +Werkzeug==1.0.1 +wrapt==1.12.1 diff --git a/src/align/align_dataset_mtcnn.py b/src/align/align_dataset_mtcnn.py index 7d5e735e6..0307e9f74 100644 --- a/src/align/align_dataset_mtcnn.py +++ b/src/align/align_dataset_mtcnn.py @@ -35,6 +35,7 @@ import align.detect_face import random from time import sleep +from PIL import Image def main(args): sleep(random.random()) @@ -49,8 +50,8 @@ def main(args): print('Creating networks and loading parameters') with tf.Graph().as_default(): - gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction) - sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) + gpu_options = tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction) + sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) with sess.as_default(): pnet, rnet, onet = align.detect_face.create_mtcnn(sess, None) @@ -80,7 +81,7 @@ def main(args): print(image_path) if not os.path.exists(output_filename): try: - img = misc.imread(image_path) + img = np.array(Image.open(image_path)) except (IOError, ValueError, IndexError) as e: errorMessage = '{}: {}'.format(image_path, e) print(errorMessage) @@ -121,14 +122,15 @@ def main(args): bb[2] = np.minimum(det[2]+args.margin/2, img_size[1]) bb[3] = np.minimum(det[3]+args.margin/2, img_size[0]) cropped = img[bb[1]:bb[3],bb[0]:bb[2],:] - scaled = misc.imresize(cropped, (args.image_size, args.image_size), interp='bilinear') + cropped = Image.fromarray(np.uint8(cropped)) + scaled = cropped.resize((args.image_size, args.image_size), Image.ANTIALIAS) nrof_successfully_aligned += 1 filename_base, file_extension = os.path.splitext(output_filename) if args.detect_multiple_faces: output_filename_n = "{}_{}{}".format(filename_base, i, file_extension) else: output_filename_n = "{}{}".format(filename_base, file_extension) - misc.imsave(output_filename_n, scaled) + scaled.save(output_filename_n) text_file.write('%s %d %d %d %d\n' % (output_filename_n, bb[0], bb[1], bb[2], bb[3])) else: print('Unable to align "%s"' % image_path) @@ -150,7 +152,7 @@ def parse_arguments(argv): parser.add_argument('--random_order', help='Shuffles the order of images to enable alignment using multiple processes.', action='store_true') parser.add_argument('--gpu_memory_fraction', type=float, - help='Upper bound on the amount of GPU memory that will be used by the process.', default=1.0) + help='Upper bound on the amount of GPU memory that will be used by the process.', default=0.8) parser.add_argument('--detect_multiple_faces', type=bool, help='Detect and align multiple faces per image.', default=False) return parser.parse_args(argv) diff --git a/src/align/detect_face.py b/src/align/detect_face.py index 7f98ca7fb..df1d58e51 100644 --- a/src/align/detect_face.py +++ b/src/align/detect_face.py @@ -82,13 +82,13 @@ def load(self, data_path, session, ignore_missing=False): session: The current TensorFlow session ignore_missing: If true, serialized weights for missing layers are ignored. """ - data_dict = np.load(data_path, encoding='latin1').item() #pylint: disable=no-member + data_dict = np.load(data_path, encoding='latin1', allow_pickle=True).item() #pylint: disable=no-member for op_name in data_dict: - with tf.variable_scope(op_name, reuse=True): + with tf.compat.v1.variable_scope(op_name, reuse=True): for param_name, data in iteritems(data_dict[op_name]): try: - var = tf.get_variable(param_name) + var = tf.compat.v1.get_variable(param_name) session.run(var.assign(data)) except ValueError: if not ignore_missing: @@ -122,7 +122,7 @@ def get_unique_name(self, prefix): def make_var(self, name, shape): """Creates a new TensorFlow variable.""" - return tf.get_variable(name, shape, trainable=self.trainable) + return tf.compat.v1.get_variable(name, shape, trainable=self.trainable) def validate_padding(self, padding): """Verifies that the padding is one of the supported ones.""" @@ -150,7 +150,7 @@ def conv(self, assert c_o % group == 0 # Convolution for a given input and kernel convolve = lambda i, k: tf.nn.conv2d(i, k, [1, s_h, s_w, 1], padding=padding) - with tf.variable_scope(name) as scope: + with tf.compat.v1.variable_scope(name) as scope: kernel = self.make_var('weights', shape=[k_h, k_w, c_i // group, c_o]) # This is the common-case. Convolve the input without any further complications. output = convolve(inp, kernel) @@ -165,7 +165,7 @@ def conv(self, @layer def prelu(self, inp, name): - with tf.variable_scope(name): + with tf.compat.v1.variable_scope(name): i = int(inp.get_shape()[-1]) alpha = self.make_var('alpha', shape=(i,)) output = tf.nn.relu(inp) + tf.multiply(alpha, -tf.nn.relu(-inp)) @@ -182,7 +182,7 @@ def max_pool(self, inp, k_h, k_w, s_h, s_w, name, padding='SAME'): @layer def fc(self, inp, num_out, name, relu=True): - with tf.variable_scope(name): + with tf.compat.v1.variable_scope(name): input_shape = inp.get_shape() if input_shape.ndims == 4: # The input is spatial. Vectorize it first. @@ -191,10 +191,10 @@ def fc(self, inp, num_out, name, relu=True): dim *= int(d) feed_in = tf.reshape(inp, [-1, dim]) else: - feed_in, dim = (inp, input_shape[-1].value) + feed_in, dim = (inp, input_shape.as_list()[-1]) weights = self.make_var('weights', shape=[dim, num_out]) biases = self.make_var('biases', [num_out]) - op = tf.nn.relu_layer if relu else tf.nn.xw_plus_b + op = tf.nn.relu_layer if relu else tf.compat.v1.nn.xw_plus_b fc = op(feed_in, weights, biases, name=name) return fc @@ -210,7 +210,7 @@ def softmax(self, target, axis, name=None): max_axis = tf.reduce_max(target, axis, keepdims=True) target_exp = tf.exp(target-max_axis) normalize = tf.reduce_sum(target_exp, axis, keepdims=True) - softmax = tf.div(target_exp, normalize, name) + softmax = tf.compat.v1.div(target_exp, normalize, name) return softmax class PNet(Network): @@ -277,16 +277,16 @@ def create_mtcnn(sess, model_path): if not model_path: model_path,_ = os.path.split(os.path.realpath(__file__)) - with tf.variable_scope('pnet'): - data = tf.placeholder(tf.float32, (None,None,None,3), 'input') + with tf.compat.v1.variable_scope('pnet'): + data = tf.compat.v1.placeholder(tf.float32, (None,None,None,3), 'input') pnet = PNet({'data':data}) pnet.load(os.path.join(model_path, 'det1.npy'), sess) - with tf.variable_scope('rnet'): - data = tf.placeholder(tf.float32, (None,24,24,3), 'input') + with tf.compat.v1.variable_scope('rnet'): + data = tf.compat.v1.placeholder(tf.float32, (None,24,24,3), 'input') rnet = RNet({'data':data}) rnet.load(os.path.join(model_path, 'det2.npy'), sess) - with tf.variable_scope('onet'): - data = tf.placeholder(tf.float32, (None,48,48,3), 'input') + with tf.compat.v1.variable_scope('onet'): + data = tf.compat.v1.placeholder(tf.float32, (None,48,48,3), 'input') onet = ONet({'data':data}) onet.load(os.path.join(model_path, 'det3.npy'), sess) @@ -708,7 +708,7 @@ def nms(boxes, threshold, method): w = np.maximum(0.0, xx2-xx1+1) h = np.maximum(0.0, yy2-yy1+1) inter = w * h - if method is 'Min': + if method == 'Min': o = inter / np.minimum(area[i], area[idx]) else: o = inter / (area[i] + area[idx] - inter) diff --git a/src/classifier.py b/src/classifier.py index 749db4d6b..844b55e78 100644 --- a/src/classifier.py +++ b/src/classifier.py @@ -40,7 +40,7 @@ def main(args): with tf.Graph().as_default(): - with tf.Session() as sess: + with tf.compat.v1.Session() as sess: np.random.seed(seed=args.seed) @@ -69,9 +69,9 @@ def main(args): facenet.load_model(args.model) # Get input and output tensors - images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0") - embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0") - phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0") + images_placeholder = tf.compat.v1.get_default_graph().get_tensor_by_name("input:0") + embeddings = tf.compat.v1.get_default_graph().get_tensor_by_name("embeddings:0") + phase_train_placeholder = tf.compat.v1.get_default_graph().get_tensor_by_name("phase_train:0") embedding_size = embeddings.get_shape()[1] # Run forward pass to calculate embeddings diff --git a/src/facenet.py b/src/facenet.py index 0e056765a..4019d8977 100644 --- a/src/facenet.py +++ b/src/facenet.py @@ -40,6 +40,7 @@ from tensorflow.python.platform import gfile import math from six import iteritems +from PIL import Image def triplet_loss(anchor, positive, negative, alpha): """Calculate the triplet loss according to the FaceNet paper @@ -52,9 +53,9 @@ def triplet_loss(anchor, positive, negative, alpha): Returns: the triplet loss according to the FaceNet paper as a float tensor. """ - with tf.variable_scope('triplet_loss'): - pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), 1) - neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), 1) + with tf.compat.v1.variable_scope('triplet_loss'): + pos_dist = tf.compat.v1.reduce_sum(tf.square(tf.subtract(anchor, positive)), 1) + neg_dist = tf.compat.v1.reduce_sum(tf.square(tf.subtract(anchor, negative)), 1) basic_loss = tf.add(tf.subtract(pos_dist,neg_dist), alpha) loss = tf.reduce_mean(tf.maximum(basic_loss, 0.0), 0) @@ -66,12 +67,12 @@ def center_loss(features, label, alfa, nrof_classes): (http://ydwen.github.io/papers/WenECCV16.pdf) """ nrof_features = features.get_shape()[1] - centers = tf.get_variable('centers', [nrof_classes, nrof_features], dtype=tf.float32, + centers = tf.compat.v1.get_variable('centers', [nrof_classes, nrof_features], dtype=tf.float32, initializer=tf.constant_initializer(0), trainable=False) label = tf.reshape(label, [-1]) centers_batch = tf.gather(centers, label) diff = (1 - alfa) * (centers_batch - features) - centers = tf.scatter_sub(centers, label, diff) + centers = tf.compat.v1.scatter_sub(centers, label, diff) with tf.control_dependencies([centers]): loss = tf.reduce_mean(tf.square(features - centers_batch)) return loss, centers @@ -106,20 +107,20 @@ def create_input_pipeline(input_queue, image_size, nrof_preprocess_threads, batc filenames, label, control = input_queue.dequeue() images = [] for filename in tf.unstack(filenames): - file_contents = tf.read_file(filename) + file_contents = tf.io.read_file(filename) image = tf.image.decode_image(file_contents, 3) image = tf.cond(get_control_flag(control[0], RANDOM_ROTATE), - lambda:tf.py_func(random_rotate_image, [image], tf.uint8), + lambda:tf.py_function(random_rotate_image, [image], tf.uint8), lambda:tf.identity(image)) image = tf.cond(get_control_flag(control[0], RANDOM_CROP), - lambda:tf.random_crop(image, image_size + (3,)), - lambda:tf.image.resize_image_with_crop_or_pad(image, image_size[0], image_size[1])) + lambda:tf.image.random_crop(image, image_size + (3,)), + lambda:tf.image.resize_with_crop_or_pad(image, image_size[0], image_size[1])) image = tf.cond(get_control_flag(control[0], RANDOM_FLIP), lambda:tf.image.random_flip_left_right(image), lambda:tf.identity(image)) image = tf.cond(get_control_flag(control[0], FIXED_STANDARDIZATION), lambda:(tf.cast(image, tf.float32) - 127.5)/128.0, - lambda:tf.image.per_image_standardization(image)) + lambda:tf.cast(tf.image.per_image_standardization(image), tf.float32)) image = tf.cond(get_control_flag(control[0], FLIP), lambda:tf.image.flip_left_right(image), lambda:tf.identity(image)) @@ -128,7 +129,7 @@ def create_input_pipeline(input_queue, image_size, nrof_preprocess_threads, batc images.append(image) images_and_labels_list.append([images, label]) - image_batch, label_batch = tf.train.batch_join( + image_batch, label_batch = tf.compat.v1.train.batch_join( images_and_labels_list, batch_size=batch_size_placeholder, shapes=[image_size + (3,), ()], enqueue_many=True, capacity=4 * nrof_preprocess_threads * 100, @@ -137,7 +138,7 @@ def create_input_pipeline(input_queue, image_size, nrof_preprocess_threads, batc return image_batch, label_batch def get_control_flag(control, field): - return tf.equal(tf.mod(tf.floor_div(control, field), 2), 1) + return tf.equal(tf.math.floormod(tf.math.floordiv(control, field), 2), 1) def _add_loss_summaries(total_loss): """Add summaries for losses. @@ -152,7 +153,7 @@ def _add_loss_summaries(total_loss): """ # Compute the moving average of all individual losses and the total loss. loss_averages = tf.train.ExponentialMovingAverage(0.9, name='avg') - losses = tf.get_collection('losses') + losses = tf.compat.v1.get_collection('losses') loss_averages_op = loss_averages.apply(losses + [total_loss]) # Attach a scalar summmary to all individual losses and the total loss; do the @@ -172,15 +173,15 @@ def train(total_loss, global_step, optimizer, learning_rate, moving_average_deca # Compute gradients. with tf.control_dependencies([loss_averages_op]): if optimizer=='ADAGRAD': - opt = tf.train.AdagradOptimizer(learning_rate) + opt = tf.compat.v1.train.AdagradOptimizer(learning_rate) elif optimizer=='ADADELTA': - opt = tf.train.AdadeltaOptimizer(learning_rate, rho=0.9, epsilon=1e-6) + opt = tf.compat.v1.train.AdadeltaOptimizer(learning_rate, rho=0.9, epsilon=1e-6) elif optimizer=='ADAM': - opt = tf.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999, epsilon=0.1) + opt = tf.compat.v1.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999, epsilon=0.1) elif optimizer=='RMSPROP': - opt = tf.train.RMSPropOptimizer(learning_rate, decay=0.9, momentum=0.9, epsilon=1.0) + opt = tf.compat.v1.train.RMSPropOptimizer(learning_rate, decay=0.9, momentum=0.9, epsilon=1.0) elif optimizer=='MOM': - opt = tf.train.MomentumOptimizer(learning_rate, 0.9, use_nesterov=True) + opt = tf.compat.v1.train.MomentumOptimizer(learning_rate, 0.9, use_nesterov=True) else: raise ValueError('Invalid optimization algorithm') @@ -191,7 +192,7 @@ def train(total_loss, global_step, optimizer, learning_rate, moving_average_deca # Add histograms for trainable variables. if log_histograms: - for var in tf.trainable_variables(): + for var in tf.compat.v1.trainable_variables(): tf.summary.histogram(var.op.name, var) # Add histograms for gradients. @@ -203,7 +204,7 @@ def train(total_loss, global_step, optimizer, learning_rate, moving_average_deca # Track the moving averages of all trainable variables. variable_averages = tf.train.ExponentialMovingAverage( moving_average_decay, global_step) - variables_averages_op = variable_averages.apply(tf.trainable_variables()) + variables_averages_op = variable_averages.apply(tf.compat.v1.trainable_variables()) with tf.control_dependencies([apply_gradient_op, variables_averages_op]): train_op = tf.no_op(name='train') @@ -244,7 +245,7 @@ def load_data(image_paths, do_random_crop, do_random_flip, image_size, do_prewhi nrof_samples = len(image_paths) images = np.zeros((nrof_samples, image_size, image_size, 3)) for i in range(nrof_samples): - img = misc.imread(image_paths[i]) + img = np.array(Image.open(image_paths[i])) if img.ndim == 2: img = to_rgb(img) if do_prewhiten: @@ -368,7 +369,7 @@ def load_model(model, input_map=None): if (os.path.isfile(model_exp)): print('Model filename: %s' % model_exp) with gfile.FastGFile(model_exp,'rb') as f: - graph_def = tf.GraphDef() + graph_def = tf.compat.v1.GraphDef() graph_def.ParseFromString(f.read()) tf.import_graph_def(graph_def, input_map=input_map, name='') else: @@ -379,7 +380,7 @@ def load_model(model, input_map=None): print('Checkpoint file: %s' % ckpt_file) saver = tf.train.import_meta_graph(os.path.join(model_exp, meta_file), input_map=input_map) - saver.restore(tf.get_default_session(), os.path.join(model_exp, ckpt_file)) + saver.restore(tf.compat.v1.get_default_session(), os.path.join(model_exp, ckpt_file)) def get_model_filenames(model_dir): files = os.listdir(model_dir) diff --git a/src/models/inception_resnet_v1.py b/src/models/inception_resnet_v1.py index 475e81bb4..b1df50436 100644 --- a/src/models/inception_resnet_v1.py +++ b/src/models/inception_resnet_v1.py @@ -24,18 +24,18 @@ from __future__ import print_function import tensorflow as tf -import tensorflow.contrib.slim as slim +import tf_slim as slim # Inception-Resnet-A def block35(net, scale=1.0, activation_fn=tf.nn.relu, scope=None, reuse=None): """Builds the 35x35 resnet block.""" - with tf.variable_scope(scope, 'Block35', [net], reuse=reuse): - with tf.variable_scope('Branch_0'): + with tf.compat.v1.variable_scope(scope, 'Block35', [net], reuse=reuse): + with tf.compat.v1.variable_scope('Branch_0'): tower_conv = slim.conv2d(net, 32, 1, scope='Conv2d_1x1') - with tf.variable_scope('Branch_1'): + with tf.compat.v1.variable_scope('Branch_1'): tower_conv1_0 = slim.conv2d(net, 32, 1, scope='Conv2d_0a_1x1') tower_conv1_1 = slim.conv2d(tower_conv1_0, 32, 3, scope='Conv2d_0b_3x3') - with tf.variable_scope('Branch_2'): + with tf.compat.v1.variable_scope('Branch_2'): tower_conv2_0 = slim.conv2d(net, 32, 1, scope='Conv2d_0a_1x1') tower_conv2_1 = slim.conv2d(tower_conv2_0, 32, 3, scope='Conv2d_0b_3x3') tower_conv2_2 = slim.conv2d(tower_conv2_1, 32, 3, scope='Conv2d_0c_3x3') @@ -50,10 +50,10 @@ def block35(net, scale=1.0, activation_fn=tf.nn.relu, scope=None, reuse=None): # Inception-Resnet-B def block17(net, scale=1.0, activation_fn=tf.nn.relu, scope=None, reuse=None): """Builds the 17x17 resnet block.""" - with tf.variable_scope(scope, 'Block17', [net], reuse=reuse): - with tf.variable_scope('Branch_0'): + with tf.compat.v1.variable_scope(scope, 'Block17', [net], reuse=reuse): + with tf.compat.v1.variable_scope('Branch_0'): tower_conv = slim.conv2d(net, 128, 1, scope='Conv2d_1x1') - with tf.variable_scope('Branch_1'): + with tf.compat.v1.variable_scope('Branch_1'): tower_conv1_0 = slim.conv2d(net, 128, 1, scope='Conv2d_0a_1x1') tower_conv1_1 = slim.conv2d(tower_conv1_0, 128, [1, 7], scope='Conv2d_0b_1x7') @@ -71,10 +71,10 @@ def block17(net, scale=1.0, activation_fn=tf.nn.relu, scope=None, reuse=None): # Inception-Resnet-C def block8(net, scale=1.0, activation_fn=tf.nn.relu, scope=None, reuse=None): """Builds the 8x8 resnet block.""" - with tf.variable_scope(scope, 'Block8', [net], reuse=reuse): - with tf.variable_scope('Branch_0'): + with tf.compat.v1.variable_scope(scope, 'Block8', [net], reuse=reuse): + with tf.compat.v1.variable_scope('Branch_0'): tower_conv = slim.conv2d(net, 192, 1, scope='Conv2d_1x1') - with tf.variable_scope('Branch_1'): + with tf.compat.v1.variable_scope('Branch_1'): tower_conv1_0 = slim.conv2d(net, 192, 1, scope='Conv2d_0a_1x1') tower_conv1_1 = slim.conv2d(tower_conv1_0, 192, [1, 3], scope='Conv2d_0b_1x3') @@ -89,38 +89,38 @@ def block8(net, scale=1.0, activation_fn=tf.nn.relu, scope=None, reuse=None): return net def reduction_a(net, k, l, m, n): - with tf.variable_scope('Branch_0'): + with tf.compat.v1.variable_scope('Branch_0'): tower_conv = slim.conv2d(net, n, 3, stride=2, padding='VALID', scope='Conv2d_1a_3x3') - with tf.variable_scope('Branch_1'): + with tf.compat.v1.variable_scope('Branch_1'): tower_conv1_0 = slim.conv2d(net, k, 1, scope='Conv2d_0a_1x1') tower_conv1_1 = slim.conv2d(tower_conv1_0, l, 3, scope='Conv2d_0b_3x3') tower_conv1_2 = slim.conv2d(tower_conv1_1, m, 3, stride=2, padding='VALID', scope='Conv2d_1a_3x3') - with tf.variable_scope('Branch_2'): + with tf.compat.v1.variable_scope('Branch_2'): tower_pool = slim.max_pool2d(net, 3, stride=2, padding='VALID', scope='MaxPool_1a_3x3') net = tf.concat([tower_conv, tower_conv1_2, tower_pool], 3) return net def reduction_b(net): - with tf.variable_scope('Branch_0'): + with tf.compat.v1.variable_scope('Branch_0'): tower_conv = slim.conv2d(net, 256, 1, scope='Conv2d_0a_1x1') tower_conv_1 = slim.conv2d(tower_conv, 384, 3, stride=2, padding='VALID', scope='Conv2d_1a_3x3') - with tf.variable_scope('Branch_1'): + with tf.compat.v1.variable_scope('Branch_1'): tower_conv1 = slim.conv2d(net, 256, 1, scope='Conv2d_0a_1x1') tower_conv1_1 = slim.conv2d(tower_conv1, 256, 3, stride=2, padding='VALID', scope='Conv2d_1a_3x3') - with tf.variable_scope('Branch_2'): + with tf.compat.v1.variable_scope('Branch_2'): tower_conv2 = slim.conv2d(net, 256, 1, scope='Conv2d_0a_1x1') tower_conv2_1 = slim.conv2d(tower_conv2, 256, 3, scope='Conv2d_0b_3x3') tower_conv2_2 = slim.conv2d(tower_conv2_1, 256, 3, stride=2, padding='VALID', scope='Conv2d_1a_3x3') - with tf.variable_scope('Branch_3'): + with tf.compat.v1.variable_scope('Branch_3'): tower_pool = slim.max_pool2d(net, 3, stride=2, padding='VALID', scope='MaxPool_1a_3x3') net = tf.concat([tower_conv_1, tower_conv1_1, @@ -137,7 +137,7 @@ def inference(images, keep_probability, phase_train=True, # force in-place updates of mean and variance estimates 'updates_collections': None, # Moving averages ends up in the trainable variables collection - 'variables_collections': [ tf.GraphKeys.TRAINABLE_VARIABLES ], + 'variables_collections': [ tf.compat.v1.GraphKeys.TRAINABLE_VARIABLES ], } with slim.arg_scope([slim.conv2d, slim.fully_connected], @@ -169,7 +169,7 @@ def inception_resnet_v1(inputs, is_training=True, """ end_points = {} - with tf.variable_scope(scope, 'InceptionResnetV1', [inputs], reuse=reuse): + with tf.compat.v1.variable_scope(scope, 'InceptionResnetV1', [inputs], reuse=reuse): with slim.arg_scope([slim.batch_norm, slim.dropout], is_training=is_training): with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d], @@ -208,7 +208,7 @@ def inception_resnet_v1(inputs, is_training=True, end_points['Mixed_5a'] = net # Reduction-A - with tf.variable_scope('Mixed_6a'): + with tf.compat.v1.variable_scope('Mixed_6a'): net = reduction_a(net, 192, 192, 256, 384) end_points['Mixed_6a'] = net @@ -217,7 +217,7 @@ def inception_resnet_v1(inputs, is_training=True, end_points['Mixed_6b'] = net # Reduction-B - with tf.variable_scope('Mixed_7a'): + with tf.compat.v1.variable_scope('Mixed_7a'): net = reduction_b(net) end_points['Mixed_7a'] = net @@ -228,7 +228,7 @@ def inception_resnet_v1(inputs, is_training=True, net = block8(net, activation_fn=None) end_points['Mixed_8b'] = net - with tf.variable_scope('Logits'): + with tf.compat.v1.variable_scope('Logits'): end_points['PrePool'] = net #pylint: disable=no-member net = slim.avg_pool2d(net, net.get_shape()[1:3], padding='VALID', diff --git a/src/train_softmax.py b/src/train_softmax.py index 6b0b28b58..bf712d6f2 100644 --- a/src/train_softmax.py +++ b/src/train_softmax.py @@ -39,7 +39,7 @@ import lfw import h5py import math -import tensorflow.contrib.slim as slim +import tf_slim as slim from tensorflow.python.ops import data_flow_ops from tensorflow.python.framework import ops from tensorflow.python.ops import array_ops @@ -95,7 +95,7 @@ def main(args): lfw_paths, actual_issame = lfw.get_paths(os.path.expanduser(args.lfw_dir), pairs) with tf.Graph().as_default(): - tf.set_random_seed(args.seed) + tf.compat.v1.set_random_seed(args.seed) global_step = tf.Variable(0, trainable=False) # Get a list of image paths and their labels @@ -107,17 +107,17 @@ def main(args): # Create a queue that produces indices into the image_list and label_list labels = ops.convert_to_tensor(label_list, dtype=tf.int32) range_size = array_ops.shape(labels)[0] - index_queue = tf.train.range_input_producer(range_size, num_epochs=None, + index_queue = tf.compat.v1.train.range_input_producer(range_size, num_epochs=None, shuffle=True, seed=None, capacity=32) index_dequeue_op = index_queue.dequeue_many(args.batch_size*args.epoch_size, 'index_dequeue') - learning_rate_placeholder = tf.placeholder(tf.float32, name='learning_rate') - batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size') - phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train') - image_paths_placeholder = tf.placeholder(tf.string, shape=(None,1), name='image_paths') - labels_placeholder = tf.placeholder(tf.int32, shape=(None,1), name='labels') - control_placeholder = tf.placeholder(tf.int32, shape=(None,1), name='control') + learning_rate_placeholder = tf.compat.v1.placeholder(tf.float32, name='learning_rate') + batch_size_placeholder = tf.compat.v1.placeholder(tf.int32, name='batch_size') + phase_train_placeholder = tf.compat.v1.placeholder(tf.bool, name='phase_train') + image_paths_placeholder = tf.compat.v1.placeholder(tf.string, shape=(None,1), name='image_paths') + labels_placeholder = tf.compat.v1.placeholder(tf.int32, shape=(None,1), name='labels') + control_placeholder = tf.compat.v1.placeholder(tf.int32, shape=(None,1), name='control') nrof_preprocess_threads = 4 input_queue = data_flow_ops.FIFOQueue(capacity=2000000, @@ -152,48 +152,48 @@ def main(args): # Norm for the prelogits eps = 1e-4 - prelogits_norm = tf.reduce_mean(tf.norm(tf.abs(prelogits)+eps, ord=args.prelogits_norm_p, axis=1)) - tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, prelogits_norm * args.prelogits_norm_loss_factor) + prelogits_norm = tf.reduce_mean(input_tensor=tf.norm(tensor=tf.abs(prelogits)+eps, ord=args.prelogits_norm_p, axis=1)) + tf.compat.v1.add_to_collection(tf.compat.v1.GraphKeys.REGULARIZATION_LOSSES, prelogits_norm * args.prelogits_norm_loss_factor) # Add center loss prelogits_center_loss, _ = facenet.center_loss(prelogits, label_batch, args.center_loss_alfa, nrof_classes) - tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, prelogits_center_loss * args.center_loss_factor) + tf.compat.v1.add_to_collection(tf.compat.v1.GraphKeys.REGULARIZATION_LOSSES, prelogits_center_loss * args.center_loss_factor) - learning_rate = tf.train.exponential_decay(learning_rate_placeholder, global_step, + learning_rate = tf.compat.v1.train.exponential_decay(learning_rate_placeholder, global_step, args.learning_rate_decay_epochs*args.epoch_size, args.learning_rate_decay_factor, staircase=True) - tf.summary.scalar('learning_rate', learning_rate) + tf.compat.v1.summary.scalar('learning_rate', learning_rate) # Calculate the average cross entropy loss across the batch cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits( labels=label_batch, logits=logits, name='cross_entropy_per_example') - cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy') - tf.add_to_collection('losses', cross_entropy_mean) + cross_entropy_mean = tf.reduce_mean(input_tensor=cross_entropy, name='cross_entropy') + tf.compat.v1.add_to_collection('losses', cross_entropy_mean) - correct_prediction = tf.cast(tf.equal(tf.argmax(logits, 1), tf.cast(label_batch, tf.int64)), tf.float32) - accuracy = tf.reduce_mean(correct_prediction) + correct_prediction = tf.cast(tf.equal(tf.argmax(input=logits, axis=1), tf.cast(label_batch, tf.int64)), tf.float32) + accuracy = tf.reduce_mean(input_tensor=correct_prediction) # Calculate the total losses - regularization_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES) + regularization_losses = tf.compat.v1.get_collection(tf.compat.v1.GraphKeys.REGULARIZATION_LOSSES) total_loss = tf.add_n([cross_entropy_mean] + regularization_losses, name='total_loss') # Build a Graph that trains the model with one batch of examples and updates the model parameters train_op = facenet.train(total_loss, global_step, args.optimizer, - learning_rate, args.moving_average_decay, tf.global_variables(), args.log_histograms) + learning_rate, args.moving_average_decay, tf.compat.v1.global_variables(), args.log_histograms) # Create a saver - saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=3) + saver = tf.compat.v1.train.Saver(tf.compat.v1.trainable_variables(), max_to_keep=3) # Build the summary operation based on the TF collection of Summaries. - summary_op = tf.summary.merge_all() + summary_op = tf.compat.v1.summary.merge_all() # Start running operations on the Graph. - gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction) - sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) - sess.run(tf.global_variables_initializer()) - sess.run(tf.local_variables_initializer()) - summary_writer = tf.summary.FileWriter(log_dir, sess.graph) + gpu_options = tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction) + sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) + sess.run(tf.compat.v1.global_variables_initializer()) + sess.run(tf.compat.v1.local_variables_initializer()) + summary_writer = tf.compat.v1.summary.FileWriter(log_dir, sess.graph) coord = tf.train.Coordinator() - tf.train.start_queue_runners(coord=coord, sess=sess) + tf.compat.v1.train.start_queue_runners(coord=coord, sess=sess) with sess.as_default(): @@ -257,7 +257,7 @@ def main(args): print('Saving statistics') with h5py.File(stat_file_name, 'w') as f: - for key, value in stat.iteritems(): + for key, value in stat.items(): f.create_dataset(key, data=value) return model_dir @@ -347,7 +347,7 @@ def train(args, sess, epoch, image_list, label_list, index_dequeue_op, enqueue_o batch_number += 1 train_time += duration # Add validation loss and accuracy to summary - summary = tf.Summary() + summary = tf.compat.v1.Summary() #pylint: disable=maybe-no-member summary.value.add(tag='time/total', simple_value=train_time) summary_writer.add_summary(summary, global_step=step_) @@ -443,7 +443,7 @@ def evaluate(sess, enqueue_op, image_paths_placeholder, labels_placeholder, phas print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' % (val, val_std, far)) lfw_time = time.time() - start_time # Add validation loss and accuracy to summary - summary = tf.Summary() + summary = tf.compat.v1.Summary() #pylint: disable=maybe-no-member summary.value.add(tag='lfw/accuracy', simple_value=np.mean(accuracy)) summary.value.add(tag='lfw/val_rate', simple_value=val) @@ -470,7 +470,7 @@ def save_variables_and_metagraph(sess, saver, summary_writer, model_dir, model_n saver.export_meta_graph(metagraph_filename) save_time_metagraph = time.time() - start_time print('Metagraph saved in %.2f seconds' % save_time_metagraph) - summary = tf.Summary() + summary = tf.compat.v1.Summary() #pylint: disable=maybe-no-member summary.value.add(tag='time/save_variables', simple_value=save_time_variables) summary.value.add(tag='time/save_metagraph', simple_value=save_time_metagraph)