def train(learn_rate, report_steps, batch_size, initial_weights=None):
    """
    Train the network.

    The function operates interactively: Progress is reported on stdout, and
    training ceases upon `KeyboardInterrupt` at which point the learned weights
    are saved to `weights.npz`, and also returned.

    :param learn_rate:
        Learning rate to use.

    :param report_steps:
        Every `report_steps` batches a progress report is printed.

    :param batch_size:
        The size of the batches used for training.

    :param initial_weights:
        (Optional.) Weights to initialize the network with.

    :return:
        The learned network weights.

    """
    x, y, params = model.get_training_model()

    y_ = tf.placeholder(tf.float32,
                        [None, common.LENGTH * len(common.CHARS) + 1])

    digits_loss, presence_loss, loss = get_loss(y, y_)
    train_step = tf.train.AdamOptimizer(learn_rate).minimize(loss)

    best = tf.argmax(
        tf.reshape(y[:, 1:],
                   [-1, common.LENGTH, len(common.CHARS)]), 2)
    correct = tf.argmax(
        tf.reshape(y_[:, 1:],
                   [-1, common.LENGTH, len(common.CHARS)]), 2)

    if initial_weights is not None:
        assert len(params) == len(initial_weights)
        assign_ops = [w.assign(v) for w, v in zip(params, initial_weights)]

    init = tf.initialize_all_variables()

    def vec_to_plate(v):
        return "".join(common.CHARS[i] for i in v)

    def do_report():
        r = sess.run([
            best, correct,
            tf.greater(y[:, 0], 0), y_[:, 0], digits_loss, presence_loss, loss
        ],
                     feed_dict={
                         x: test_xs,
                         y_: test_ys
                     })
        num_correct = numpy.sum(
            numpy.logical_or(numpy.all(r[0] == r[1], axis=1),
                             numpy.logical_and(r[2] < 0.5, r[3] < 0.5)))
        r_short = (r[0][:190], r[1][:190], r[2][:190], r[3][:190])
        for b, c, pb, pc in zip(*r_short):
            print("{} {} <-> {} {}".format(vec_to_plate(c), pc,
                                           vec_to_plate(b), float(pb)))
        num_p_correct = numpy.sum(r[2] == r[3])

        print(("B{:3d} {:2.02f}% {:02.02f}% loss: {} "
               "(digits: {}, presence: {}) |{}|").format(
                   batch_idx, 100. * num_correct / (len(r[0])),
                   100. * num_p_correct / len(r[2]), r[6], r[4], r[5],
                   "".join("X "[numpy.array_equal(b, c) or (not pb and not pc)]
                           for b, c, pb, pc in zip(*r_short))))

    def do_batch():
        sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
        if batch_idx % report_steps == 0:
            do_report()

    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.95)
    with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
        sess.run(init)
        if initial_weights is not None:
            sess.run(assign_ops)

        test_xs, test_ys = unzip(list(read_data("test/*.png"))[:50])

        try:
            last_batch_idx = 0
            last_batch_time = time.time()
            batch_iter = enumerate(read_batches(batch_size))
            for batch_idx, (batch_xs, batch_ys) in batch_iter:
                do_batch()
                if batch_idx % report_steps == 0:
                    batch_time = time.time()
                    if last_batch_idx != batch_idx:
                        print("time for 60 batches {}".format(
                            60 * (last_batch_time - batch_time) /
                            (last_batch_idx - batch_idx)))
                        last_batch_idx = batch_idx
                        last_batch_time = batch_time

        except KeyboardInterrupt:
            last_weights = [p.eval() for p in params]
            numpy.savez("weights.npz", *last_weights)
            return last_weights
示例#2
0
            # Builds the graph
            input_placeholder = tf.placeholder(tf.float32, [
                embedding_batch_size, sequence_length, height, width, channels
            ])
            embeddings = create_id3_embedding(
                preprocess(input_placeholder, (224, 224)))

            reference_embeddings_placeholder = tf.placeholder(
                tf.float32, [sequences, 400])
            generated_embeddings_placeholder = tf.placeholder(
                tf.float32, [sequences, 400])

            fvd = calculate_fvd(reference_embeddings_placeholder,
                                generated_embeddings_placeholder)

            gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.3)
            with tf.Session(config=tf.ConfigProto(
                    gpu_options=gpu_options)) as sess:
                sess.run(tf.global_variables_initializer())
                sess.run(tf.tables_initializer())

                # Computes the embeddings
                reference_embeddings = extract_embeddings(
                    embeddings,
                    input_placeholder,
                    reference,
                    sess,
                    batch_size=embedding_batch_size)
                generated_embeddings = extract_embeddings(
                    embeddings,
                    input_placeholder,
示例#3
0
    def __call__(self, reference_dataloader, generated_dataloader) -> float:
        '''
        Computes the FVD between the reference and the generated observations

        :param reference_dataloader: dataloader for reference observations
        :param generated_dataloader: dataloader for generated observations
        :return: The FVD between the two distributions
        '''

        # Multiples of 16 must be fes to the embeddings network
        embedding_batch_size = 16

        # Extracts information about the batch
        sample_observations = next(iter(reference_dataloader)).to_tuple()[0]
        _, sequence_length, channels, height, width = list(
            sample_observations.size())

        with tf.Graph().as_default():

            # Builds the embedding computation part of the graph
            input_placeholder = tf.placeholder(tf.float32, [
                embedding_batch_size, sequence_length, height, width, channels
            ])

            embeddings = create_id3_embedding(
                preprocess(input_placeholder, (224, 224)))

            # Computes all embeddings
            gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.3)
            with tf.Session(config=tf.ConfigProto(
                    gpu_options=gpu_options)) as sess:
                sess.run(tf.global_variables_initializer())
                sess.run(tf.tables_initializer())

                reference_embeddings = self.extract_all_embeddings(
                    reference_dataloader, embeddings, input_placeholder, sess,
                    embedding_batch_size)
                generated_embeddings = self.extract_all_embeddings(
                    generated_dataloader, embeddings, input_placeholder, sess,
                    embedding_batch_size)

                if reference_embeddings.shape[0] != generated_embeddings.shape[
                        0]:
                    raise Exception(
                        f"Size of reference ({reference_embeddings.shape[0]}) and generated embeddings ({generated_embeddings.shape[0]}) differ"
                    )

            sequences = reference_embeddings.shape[0]

            # Builds the fvd computation part of the graph
            reference_embeddings_placeholder = tf.placeholder(
                tf.float32, [sequences, 400])
            generated_embeddings_placeholder = tf.placeholder(
                tf.float32, [sequences, 400])

            fvd = calculate_fvd(reference_embeddings_placeholder,
                                generated_embeddings_placeholder)

            # Computes the fvd
            gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.3)
            with tf.Session(config=tf.ConfigProto(
                    gpu_options=gpu_options)) as sess:
                sess.run(tf.global_variables_initializer())
                sess.run(tf.tables_initializer())

                # Computes the fvd
                fvd_np = sess.run(fvd,
                                  feed_dict={
                                      reference_embeddings_placeholder:
                                      reference_embeddings,
                                      generated_embeddings_placeholder:
                                      generated_embeddings
                                  })

                return float(fvd_np)
def transfrom_data(NUM_TEST_PER_EPOCH):
    encoded_data = []
    original_data = []
    train_loader = get_data(BATCH_SIZE)
    trainiter = iter(train_loader)
    for i in range(NUM_ITERATION):
        features2, labels = next(trainiter)
        features2 = np.array(
            tf.keras.applications.resnet.preprocess_input(
                features2.data.numpy().transpose(0, 2, 3, 1) * 255))
        features = np.zeros(
            [len(features2), img_size[0], img_size[1], img_size[2]])
        features[:, :, :, 0] = (features2[:, :, :, 2])
        features = np.clip(features + 123.68, 0, 255) / 255
        original_images = np.copy(features)
        for k in range(len(features)):
            features[k] = add_gaussian_noise(features[k],
                                             sd=np.random.uniform(
                                                 NOISE_STD_RANGE[0],
                                                 NOISE_STD_RANGE[1]))
        encoded_data.append(np.copy(features))
        original_data.append(np.copy(original_images))

    gpu_options = tf.GPUOptions(allow_growth=True,
                                visible_device_list=str(GPU_ID))
    config = tf.ConfigProto(gpu_options=gpu_options)
    gx1 = tf.Graph()
    with gx1.as_default():
        with tf.Session(config=config) as sess1:
            sargan_model1 = SARGAN(img_size, BATCH_SIZE, img_channel=1)
            sargan_saver1 = tf.train.Saver()
            sargan_saver1 = tf.train.import_meta_graph(trained_model_path1 +
                                                       '/sargan_mnist.meta')
            sargan_saver1.restore(
                sess1, tf.train.latest_checkpoint(trained_model_path1))
            for ibatch in range(NUM_TEST_PER_EPOCH):
                processed_batch = sess1.run(sargan_model1.gen_img,
                                            feed_dict={
                                                sargan_model1.image:
                                                encoded_data[ibatch],
                                                sargan_model1.cond:
                                                encoded_data[ibatch]
                                            })
                encoded_data[ibatch] = (np.copy(processed_batch))

    gx2 = tf.Graph()
    with gx2.as_default():
        with tf.Session(config=config) as sess2:
            sargan_model2 = SARGAN(img_size, BATCH_SIZE, img_channel=1)
            sargan_saver2 = tf.train.Saver()
            sargan_saver2 = tf.train.import_meta_graph(trained_model_path2 +
                                                       '/sargan_mnist.meta')
            sargan_saver2.restore(
                sess2, tf.train.latest_checkpoint(trained_model_path2))
            for ibatch in range(NUM_TEST_PER_EPOCH):
                processed_batch = sess2.run(sargan_model2.gen_img,
                                            feed_dict={
                                                sargan_model2.image:
                                                encoded_data[ibatch],
                                                sargan_model2.cond:
                                                encoded_data[ibatch]
                                            })
                encoded_data[ibatch] = (np.copy(processed_batch))

    gx3 = tf.Graph()
    with gx3.as_default():
        with tf.Session(config=config) as sess3:
            sargan_model3 = SARGAN(img_size, BATCH_SIZE, img_channel=1)
            sargan_saver3 = tf.train.Saver()
            sargan_saver3 = tf.train.import_meta_graph(trained_model_path3 +
                                                       '/sargan_mnist.meta')
            sargan_saver3.restore(
                sess3, tf.train.latest_checkpoint(trained_model_path3))
            for ibatch in range(NUM_TEST_PER_EPOCH):
                processed_batch = sess3.run(sargan_model3.gen_img,
                                            feed_dict={
                                                sargan_model3.image:
                                                encoded_data[ibatch],
                                                sargan_model3.cond:
                                                encoded_data[ibatch]
                                            })
                encoded_data[ibatch] = (np.copy(processed_batch))

    return encoded_data, original_data
def Recognize(idList):
    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,
                                                log_device_placement=False))
        with sess.as_default():
            pnet, rnet, onet = detect_face.create_mtcnn(sess, npy)
            minsize = 30  # minimum size of face
            threshold = [0.7, 0.8, 0.8]  # three steps's threshold
            factor = 0.709  # scale factor
            margin = 44
            batch_size = 100  #1000
            image_size = 182
            input_image_size = 160
            HumanNames = os.listdir(train_img)
            HumanNames.sort()
            print('Loading Model')
            facenet.load_model(modeldir)
            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")
            embedding_size = embeddings.get_shape()[1]
            classifier_filename_exp = os.path.expanduser(classifier_filename)
            with open(classifier_filename_exp, 'rb') as infile:
                (model, class_names) = pickle.load(infile, encoding='latin1')

            video_capture = cv2.VideoCapture(video)
            print('Start Recognition')
            while True:
                ret, frame = video_capture.read()
                #frame = cv2.resize(frame, (0,0), fx=0.5, fy=0.5)    #resize frame (optional)
                timer = time.time()
                if frame.ndim == 2:
                    frame = facenet.to_rgb(frame)
                bounding_boxes, _ = detect_face.detect_face(
                    frame, minsize, pnet, rnet, onet, threshold, factor)
                faceNum = bounding_boxes.shape[0]
                if faceNum > 0:
                    det = bounding_boxes[:, 0:4]
                    img_size = np.asarray(frame.shape)[0:2]
                    cropped = []
                    scaled = []
                    scaled_reshape = []
                    for i in range(faceNum):
                        emb_array = np.zeros((1, embedding_size))
                        xmin = int(det[i][0])
                        ymin = int(det[i][1])
                        xmax = int(det[i][2])
                        ymax = int(det[i][3])
                        try:
                            # inner exception
                            if xmin <= 0 or ymin <= 0 or xmax >= len(
                                    frame[0]) or ymax >= len(frame):
                                print('Face is very close!')
                                continue
                            cropped.append(frame[ymin:ymax, xmin:xmax, :])
                            cropped[i] = facenet.flip(cropped[i], False)
                            scaled.append(
                                np.array(
                                    Image.fromarray(cropped[i]).resize(
                                        (image_size, image_size))))
                            scaled[i] = cv2.resize(
                                scaled[i],
                                (input_image_size, input_image_size),
                                interpolation=cv2.INTER_CUBIC)
                            scaled[i] = facenet.prewhiten(scaled[i])
                            scaled_reshape.append(scaled[i].reshape(
                                -1, input_image_size, input_image_size, 3))
                            feed_dict = {
                                images_placeholder: scaled_reshape[i],
                                phase_train_placeholder: False
                            }
                            emb_array[0, :] = sess.run(embeddings,
                                                       feed_dict=feed_dict)
                            predictions = model.predict_proba(emb_array)
                            best_class_indices = np.argmax(predictions, axis=1)
                            best_class_probabilities = predictions[
                                np.arange(len(best_class_indices)),
                                best_class_indices]
                            if best_class_probabilities > 0.87:
                                cv2.rectangle(frame, (xmin, ymin),
                                              (xmax, ymax), (0, 255, 0),
                                              2)  #boxing face
                                for H_i in HumanNames:
                                    if HumanNames[
                                            best_class_indices[0]] == H_i:
                                        result_ids = HumanNames[
                                            best_class_indices[0]]

                                        result_names = "?"
                                        profile = GetProfile(result_ids)
                                        if (profile != None):
                                            result_names = profile[1]
                                            if int(result_ids) not in idList:
                                                idList.append(int(result_ids))

                                        print(
                                            "Predictions : [ name: {} , accuracy: {:.3f} ]"
                                            .format(
                                                HumanNames[
                                                    best_class_indices[0]],
                                                best_class_probabilities[0]))
                                        cv2.rectangle(frame, (xmin, ymin - 20),
                                                      (xmax, ymin - 2),
                                                      (0, 255, 255), -1)
                                        cv2.putText(
                                            frame,
                                            result_names, (xmin, ymin - 5),
                                            cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                            1, (0, 0, 0),
                                            thickness=1,
                                            lineType=1)

                            else:
                                cv2.rectangle(frame, (xmin, ymin),
                                              (xmax, ymax), (0, 255, 0), 2)
                                cv2.rectangle(frame, (xmin, ymin - 20),
                                              (xmax, ymin - 2), (0, 255, 255),
                                              -1)
                                cv2.putText(frame,
                                            "?", (xmin, ymin - 5),
                                            cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                            1, (0, 0, 0),
                                            thickness=1,
                                            lineType=1)
                        except:

                            print("error")

                endtimer = time.time()
                fps = 1 / (endtimer - timer)
                cv2.rectangle(frame, (15, 30), (135, 60), (0, 255, 255), -1)
                cv2.putText(frame, "fps: {:.2f}".format(fps), (20, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2)
                cv2.imshow('Face Recognition', frame)
                key = cv2.waitKey(1)
                if key == 113:  # "q"
                    break
            video_capture.release()
            cv2.destroyAllWindows()
 def __init__(self, net_factory, model_path):
     #create a graph
     graph = tf.Graph()
     with graph.as_default():
         #define tensor and op in graph(-1,1)
         self.image_op = tf.placeholder(tf.float32, name='input_image')
         self.width_op = tf.placeholder(tf.int32, name='image_width')
         self.height_op = tf.placeholder(tf.int32, name='image_height')
         image_reshape = tf.reshape(self.image_op, [1, self.height_op, self.width_op, 3])
         #self.cls_prob batch*2
         #self.bbox_pred batch*4
         #construct model here
         #self.cls_prob, self.bbox_pred = net_factory(image_reshape, training=False)
         #contains landmark
         self.cls_prob, self.bbox_pred, _ = net_factory(image_reshape, training=False)
         
         #allow 
         self.sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, gpu_options=tf.GPUOptions(allow_growth=True)))
         saver = tf.train.Saver()
         #check whether the dictionary is valid
         model_dict = '/'.join(model_path.split('/')[:-1])
         ckpt = tf.train.get_checkpoint_state(model_dict)
         print(model_path)
         readstate = ckpt and ckpt.model_checkpoint_path
         assert  readstate, "the params dictionary is not valid"
         print("restore models' param")
         saver.restore(self.sess, model_path)
示例#7
0
def initialize_globals():
    c = AttrDict()

    # Set default dropout rates
    if FLAGS.dropout_rate2 < 0:
        FLAGS.dropout_rate2 = FLAGS.dropout_rate
    if FLAGS.dropout_rate3 < 0:
        FLAGS.dropout_rate3 = FLAGS.dropout_rate
    if FLAGS.dropout_rate6 < 0:
        FLAGS.dropout_rate6 = FLAGS.dropout_rate

    # Set default checkpoint dir
    if not FLAGS.checkpoint_dir:
        FLAGS.checkpoint_dir = xdg.save_data_path(
            os.path.join('deepspeech', 'checkpoints'))

    if FLAGS.load not in ['last', 'best', 'init', 'auto']:
        FLAGS.load = 'auto'

    # Set default summary dir
    if not FLAGS.summary_dir:
        FLAGS.summary_dir = xdg.save_data_path(
            os.path.join('deepspeech', 'summaries'))

    # Standard session configuration that'll be used for all new sessions.
    c.session_config = tfv1.ConfigProto(
        allow_soft_placement=True,
        log_device_placement=FLAGS.log_placement,
        inter_op_parallelism_threads=FLAGS.inter_op_parallelism_threads,
        intra_op_parallelism_threads=FLAGS.intra_op_parallelism_threads,
        gpu_options=tfv1.GPUOptions(allow_growth=FLAGS.use_allow_growth))

    # CPU device
    c.cpu_device = '/cpu:0'

    # Available GPU devices
    c.available_devices = get_available_gpus(c.session_config)
    print("*******device**********", c.available_devices)

    # If there is no GPU available, we fall back to CPU based operation
    if not c.available_devices:
        c.available_devices = [c.cpu_device]

    if FLAGS.utf8:
        c.alphabet = UTF8Alphabet()
    else:
        c.alphabet = Alphabet(os.path.abspath(FLAGS.alphabet_config_path))

    # Geometric Constants
    # ===================

    # For an explanation of the meaning of the geometric constants, please refer to
    # doc/Geometry.md

    # Number of MFCC features
    c.n_input = 13  # TODO: Determine this programmatically from the sample rate

    # The number of frames in the context
    c.n_context = 10  # TODO: Determine the optimal value using a validation data set

    # Number of units in hidden layers
    c.n_hidden = FLAGS.n_hidden

    c.n_hidden_1 = c.n_hidden

    c.n_hidden_2 = c.n_hidden

    c.n_hidden_5 = c.n_hidden

    # LSTM cell state dimension
    c.n_cell_dim = c.n_hidden

    # The number of units in the third layer, which feeds in to the LSTM
    c.n_hidden_3 = c.n_cell_dim

    # Units in the sixth layer = number of characters in the target language plus one
    c.n_hidden_6 = c.alphabet.size() + 1  # +1 for CTC blank label

    # Size of audio window in samples
    if (FLAGS.feature_win_len * FLAGS.audio_sample_rate) % 1000 != 0:
        log_error(
            '--feature_win_len value ({}) in milliseconds ({}) multiplied '
            'by --audio_sample_rate value ({}) must be an integer value. Adjust '
            'your --feature_win_len value or resample your audio accordingly.'
            ''.format(FLAGS.feature_win_len, FLAGS.feature_win_len / 1000,
                      FLAGS.audio_sample_rate))
        sys.exit(1)

    c.audio_window_samples = FLAGS.audio_sample_rate * (FLAGS.feature_win_len /
                                                        1000)

    # Stride for feature computations in samples
    if (FLAGS.feature_win_step * FLAGS.audio_sample_rate) % 1000 != 0:
        log_error(
            '--feature_win_step value ({}) in milliseconds ({}) multiplied '
            'by --audio_sample_rate value ({}) must be an integer value. Adjust '
            'your --feature_win_step value or resample your audio accordingly.'
            ''.format(FLAGS.feature_win_step, FLAGS.feature_win_step / 1000,
                      FLAGS.audio_sample_rate))
        sys.exit(1)

    c.audio_step_samples = FLAGS.audio_sample_rate * (FLAGS.feature_win_step /
                                                      1000)

    if FLAGS.one_shot_infer:
        if not os.path.exists(FLAGS.one_shot_infer):
            log_error(
                'Path specified in --one_shot_infer is not a valid file.')
            sys.exit(1)

    ConfigSingleton._config = c  # pylint: disable=protected-access
示例#8
0
    def collect_data(self):
        output_dir = os.path.expanduser(self.output_datadir)
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)

        dataset = facenet.get_dataset(self.input_datadir)
        with tf.Graph().as_default():
            gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)
            sess = tf.Session(config=tf.ConfigProto(
                gpu_options=gpu_options, log_device_placement=False))
            with sess.as_default():
                pnet, rnet, onet = detect_face.create_mtcnn(sess, './npy')

        minsize = 20  # minimum size of face
        threshold = [0.6, 0.7, 0.7]  # three steps's threshold
        factor = 0.709  # scale factor
        margin = 44
        image_size = 182

        # Add a random key to the filename to allow alignment using multiple processes
        random_key = np.random.randint(0, high=99999)
        bounding_boxes_filename = os.path.join(
            output_dir, 'bounding_boxes_%05d.txt' % random_key)

        with open(bounding_boxes_filename, "w") as text_file:
            nrof_images_total = 0
            nrof_successfully_aligned = 0
            for cls in dataset:
                output_class_dir = os.path.join(output_dir, cls.name)
                if not os.path.exists(output_class_dir):
                    os.makedirs(output_class_dir)
                for image_path in cls.image_paths:
                    nrof_images_total += 1
                    filename = os.path.splitext(
                        os.path.split(image_path)[1])[0]
                    output_filename = os.path.join(output_class_dir,
                                                   filename + '.png')
                    print("Image: %s" % image_path)
                    if not os.path.exists(output_filename):
                        try:
                            img = misc.imread(image_path)
                        except (IOError, ValueError, IndexError) as e:
                            errorMessage = '{}: {}'.format(image_path, e)
                            print(errorMessage)
                        else:
                            if img.ndim < 2:
                                print('Unable to align "%s"' % image_path)
                                text_file.write('%s\n' % (output_filename))
                                continue
                            if img.ndim == 2:
                                img = facenet.to_rgb(img)
                                print('to_rgb data dimension: ', img.ndim)
                            img = img[:, :, 0:3]

                            bounding_boxes, _ = detect_face.detect_face(
                                img, minsize, pnet, rnet, onet, threshold,
                                factor)
                            nrof_faces = bounding_boxes.shape[0]
                            print('No of Detected Face: %d' % nrof_faces)
                            if nrof_faces > 0:
                                det = bounding_boxes[:, 0:4]
                                img_size = np.asarray(img.shape)[0:2]
                                if nrof_faces > 1:
                                    bounding_box_size = (
                                        det[:, 2] - det[:, 0]) * (det[:, 3] -
                                                                  det[:, 1])
                                    img_center = img_size / 2
                                    offsets = np.vstack([
                                        (det[:, 0] + det[:, 2]) / 2 -
                                        img_center[1],
                                        (det[:, 1] + det[:, 3]) / 2 -
                                        img_center[0]
                                    ])
                                    offset_dist_squared = np.sum(
                                        np.power(offsets, 2.0), 0)
                                    index = np.argmax(
                                        bounding_box_size -
                                        offset_dist_squared * 2.0
                                    )  # some extra weight on the centering
                                    det = det[index, :]
                                det = np.squeeze(det)
                                bb_temp = np.zeros(4, dtype=np.int32)

                                bb_temp[0] = det[0]
                                bb_temp[1] = det[1]
                                bb_temp[2] = det[2]
                                bb_temp[3] = det[3]

                                cropped_temp = img[bb_temp[1]:bb_temp[3],
                                                   bb_temp[0]:bb_temp[2], :]
                                scaled_temp = misc.imresize(
                                    cropped_temp, (image_size, image_size),
                                    interp='bilinear')

                                nrof_successfully_aligned += 1
                                misc.imsave(output_filename, scaled_temp)
                                text_file.write(
                                    '%s %d %d %d %d\n' %
                                    (output_filename, bb_temp[0], bb_temp[1],
                                     bb_temp[2], bb_temp[3]))
                            else:
                                print('Unable to align "%s"' % image_path)
                                text_file.write('%s\n' % (output_filename))

        return (nrof_images_total, nrof_successfully_aligned)
示例#9
0
    def export(self,
               output_dir: Text,
               tflite_path: Text = None,
               tensorrt: Text = None):
        """Export a saved model, frozen graph, and potential tflite/tensorrt model.

    Args:
      output_dir: the output folder for saved model.
      tflite_path: the path for saved tflite file.
      tensorrt: If not None, must be {'FP32', 'FP16', 'INT8'}.
    """
        signitures = self.signitures
        signature_def_map = {
            'serving_default':
            tf.saved_model.predict_signature_def(
                {signitures['image_arrays'].name: signitures['image_arrays']},
                {signitures['prediction'].name: signitures['prediction']}),
        }
        b = tf.saved_model.Builder(output_dir)
        b.add_meta_graph_and_variables(self.sess,
                                       tags=['serve'],
                                       signature_def_map=signature_def_map,
                                       assets_collection=tf.get_collection(
                                           tf.GraphKeys.ASSET_FILEPATHS),
                                       clear_devices=True)
        b.save()
        logging.info('Model saved at %s', output_dir)

        # also save freeze pb file.
        graphdef = self.freeze()
        pb_path = os.path.join(output_dir, self.model_name + '_frozen.pb')
        tf.io.gfile.GFile(pb_path, 'wb').write(graphdef.SerializeToString())
        logging.info('Frozen graph saved at %s', pb_path)

        if tflite_path:
            height, width = utils.parse_image_size(self.params['image_size'])
            input_name = signitures['image_arrays'].op.name
            input_shapes = {input_name: [None, height, width, 3]}
            converter = tf.lite.TFLiteConverter.from_saved_model(
                output_dir,
                input_arrays=[input_name],
                input_shapes=input_shapes,
                output_arrays=[signitures['prediction'].op.name])
            converter.target_spec.supported_ops = [
                tf.lite.OpsSet.TFLITE_BUILTINS
            ]
            tflite_model = converter.convert()

            tf.io.gfile.GFile(tflite_path, 'wb').write(tflite_model)
            logging.info('TFLite is saved at %s', tflite_path)

        if tensorrt:
            from tensorflow.python.compiler.tensorrt import trt  # pylint: disable=g-direct-tensorflow-import,g-import-not-at-top
            sess_config = tf.ConfigProto(gpu_options=tf.GPUOptions(
                allow_growth=True))
            trt_path = os.path.join(output_dir, 'tensorrt_' + tensorrt.lower())
            trt.create_inference_graph(None,
                                       None,
                                       precision_mode=tensorrt,
                                       input_saved_model_dir=output_dir,
                                       output_saved_model_dir=trt_path,
                                       session_config=sess_config)
            logging.info('TensorRT model is saved at %s', trt_path)
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--path',
                        help='Path of the video you want to test on.',
                        default=0)
    args = parser.parse_args()

    MINSIZE = 20
    THRESHOLD = [0.6, 0.7, 0.7]
    FACTOR = 0.709
    IMAGE_SIZE = 182
    INPUT_IMAGE_SIZE = 160
    CLASSIFIER_PATH = 'Models/facemodel_rework.pkl'
    VIDEO_PATH = args.path
    FACENET_MODEL_PATH = 'Models/20180402-114759.pb'

    # Load The Custom Classifier
    with open(CLASSIFIER_PATH, 'rb') as file:
        model, class_names = pickle.load(file)
    print("Custom Classifier, Successfully loaded")

    with tf.Graph().as_default():

        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,
                                                log_device_placement=False))

        with sess.as_default():

            # Load the model
            print('Loading feature extraction model')
            facenet.load_model(FACENET_MODEL_PATH)

            # 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")
            embedding_size = embeddings.get_shape()[1]

            pnet, rnet, onet = align.detect_face.create_mtcnn(sess, "align/")

            people_detected = set()
            person_detected = collections.Counter()

            cap = VideoStream(src=0).start()

            while (True):
                frame = cap.read()
                frame = imutils.resize(frame, width=800)
                frame = cv2.flip(frame, 1)

                bounding_boxes, _ = align.detect_face.detect_face(
                    frame, MINSIZE, pnet, rnet, onet, THRESHOLD, FACTOR)

                faces_found = bounding_boxes.shape[0]
                try:
                    # if faces_found > 1:
                    #     cv2.putText(frame, "Only one face", (0, 100), cv2.FONT_HERSHEY_COMPLEX_SMALL,
                    #                 1, (255, 255, 255), thickness=1, lineType=2)
                    if faces_found > 0:
                        det = bounding_boxes[:, 0:4]
                        bb = np.zeros((faces_found, 4), dtype=np.int32)
                        for i in range(faces_found):
                            bb[i][0] = det[i][0]
                            bb[i][1] = det[i][1]
                            bb[i][2] = det[i][2]
                            bb[i][3] = det[i][3]
                            print(bb[i][3] - bb[i][1])
                            print(frame.shape[0])
                            print((bb[i][3] - bb[i][1]) / frame.shape[0])
                            if (bb[i][3] - bb[i][1]) / frame.shape[0] > 0.25:
                                cropped = frame[bb[i][1]:bb[i][3],
                                                bb[i][0]:bb[i][2], :]
                                scaled = cv2.resize(
                                    cropped,
                                    (INPUT_IMAGE_SIZE, INPUT_IMAGE_SIZE),
                                    interpolation=cv2.INTER_CUBIC)
                                scaled = facenet.prewhiten(scaled)
                                scaled_reshape = scaled.reshape(
                                    -1, INPUT_IMAGE_SIZE, INPUT_IMAGE_SIZE, 3)
                                feed_dict = {
                                    images_placeholder: scaled_reshape,
                                    phase_train_placeholder: False
                                }
                                emb_array = sess.run(embeddings,
                                                     feed_dict=feed_dict)

                                predictions = model.predict_proba(emb_array)
                                best_class_indices = np.argmax(predictions,
                                                               axis=1)
                                best_class_probabilities = predictions[
                                    np.arange(len(best_class_indices)),
                                    best_class_indices]
                                best_name = class_names[best_class_indices[0]]
                                print("Name: {}, Probability: {}".format(
                                    best_name, best_class_probabilities))
                                cv2.rectangle(frame, (bb[i][0], bb[i][1]),
                                              (bb[i][2], bb[i][3]),
                                              (0, 255, 0), 2)
                                text_x = bb[i][0]
                                text_y = bb[i][3] + 20

                                if best_class_probabilities > 0.07:

                                    name = class_names[best_class_indices[0]]
                                    cv2.putText(frame,
                                                name, (text_x, text_y),
                                                cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                                1, (0, 255, 0),
                                                thickness=1,
                                                lineType=2)
                                    cv2.putText(
                                        frame,
                                        str(
                                            round(best_class_probabilities[0],
                                                  3)), (text_x, text_y + 17),
                                        cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                        1, (0, 255, 0),
                                        thickness=1,
                                        lineType=2)
                                    person_detected[best_name] += 1
                                    print(person_detected)

                                    print("most_common",
                                          person_detected.most_common(1)[0][0])
                                    if person_detected.most_common(
                                            1)[0][1] > 50:
                                        person_detected = collections.Counter()

                                    #print("best_detect",best_detect)
                                else:
                                    name = "Unknown"
                                    cv2.putText(frame,
                                                name, (text_x, text_y),
                                                cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                                1, (0, 255, 0),
                                                thickness=1,
                                                lineType=2)
                                    cv2.putText(
                                        frame,
                                        str(
                                            round(best_class_probabilities[0],
                                                  3)), (text_x, text_y + 17),
                                        cv2.FONT_HERSHEY_COMPLEX_SMALL,
                                        1, (0, 255, 0),
                                        thickness=1,
                                        lineType=2)

                except:
                    pass

                cv2.imshow('Face Recognition', frame)
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break

            cap.release()
            cv2.destroyAllWindows()
示例#11
0
    def train(self):

        start_time = time.time()

        # Config GPU options
        if self.FLAGS.per_process_gpu_memory_fraction == 0.0:
            gpu_options = tf.GPUOptions(allow_growth=True)
        elif self.FLAGS.per_process_gpu_memory_fraction == 1.0:
            gpu_options = tf.GPUOptions()

        else:
            gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=self.
                                        FLAGS.per_process_gpu_memory_fraction,
                                        allow_growth=True)

        os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
        os.environ['CUDA_VISIBLE_DEVICES'] = self.FLAGS.cuda_visible_devices

        self.sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
        if not tf.test.gpu_device_name():
            self.logger.warning("No GPU is found")
        else:
            self.logger.info(tf.test.gpu_device_name())

        global_step_lr = tf.Variable(0, trainable=False)
        lr1 = tf.train.exponential_decay(
            learning_rate=self.FLAGS.learning_rate,
            global_step=global_step_lr,
            decay_steps=1000,
            decay_rate=0.995,
            staircase=True)
        lr2 = tf.train.exponential_decay(learning_rate=0.001,
                                         global_step=global_step_lr,
                                         decay_steps=1000,
                                         decay_rate=0.995,
                                         staircase=True)

        with self.sess.as_default():

            if self.FLAGS.experiment_type == "MSRTP":
                self.model = MSRTP(self.FLAGS, self.emb, self.sess)

            self.logger.info('Init finish.\tCost time: %.2fs' %
                             (time.time() - start_time))

            #AUC暂时不看
            # test_auc = self.model.metrics(sess=self.sess,
            #                               batch_data=self.test_set,
            #                               global_step=self.global_step,
            #                               name='test auc')

            # Eval init AUC
            # self.logger.info('Init AUC: %.4f' % test_auc)

            test_start = time.time()
            self.hr_1, self.ndcg_1, self.hr_5, self.ndcg_5, self.hr_10, self.ndcg_10, self.hr_30, self.ndcg_30, self.hr_50, self.ndcg_50 = \
                0,0,0,0,0,0,0,0,0,0
            self.mse = 1000000
            self.best_result_hr = []
            self.best_result_ndcg = []

            def eval_mse():

                max_step = 0.
                mse_lst = []

                for step_i, batch_data in DataInput(
                        self.test_set, self.FLAGS.test_batch_size):
                    max_step = 1 + max_step

                    step_mse = self.model.metrics_mse(
                        sess=self.sess,
                        batch_data=batch_data,
                        global_step=self.global_step,
                        topk=self.FLAGS.top_k)
                    mse_lst.extend(list(step_mse[0]))

                mse_val = np.mean(mse_lst)

                if mse_val < self.mse:
                    self.mse = mse_val

                print('----test mse: %.5f-----' % mse_val)
                print('----MIN mse: %.5f-----' % (self.mse))

            def eval_topk():
                sum_hr_1, sum_ndcg_1, sum_hr_5, sum_ndcg_5, sum_hr_10, sum_ndcg_10, sum_hr_30, sum_ndcg_30, sum_hr_50, sum_ndcg_50 = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                result_list_hr_all = []
                result_list_ndcg_all = []

                max_step = 0

                for step_i, batch_data in DataInput(
                        self.test_set, self.FLAGS.test_batch_size):
                    max_step = 1 + max_step
                    if self.FLAGS.experiment_type == "NARM" or \
                            self.FLAGS.experiment_type == "NARM+" or \
                            self.FLAGS.experiment_type == "NARM++": # or \
                        #self.FLAGS.experiment_type == "MTAM" :
                        hr_1, ndcg_1, hr_5, ndcg_5, hr_10, ndcg_10,  hr_30, ndcg_30, hr_50, ndcg_50, \
                        result_list_hr, result_list_ndcg= \
                                                          self.model.metrics_topK_concat(sess=self.sess,
                                                                                         batch_data=batch_data,
                                                                                         global_step=self.global_step,
                                                                                         topk=self.FLAGS.top_k)
                    else:
                        hr_1, ndcg_1, hr_5, ndcg_5, hr_10, ndcg_10,  hr_30, ndcg_30, hr_50, ndcg_50, \
                        result_list_hr, result_list_ndcg = \
                                                          self.model.metrics_topK(sess=self.sess,
                                                                                  batch_data=batch_data,
                                                                                  global_step=self.global_step,
                                                                                  topk=self.FLAGS.top_k)
                    sum_hr_1 = sum_hr_1 + hr_1
                    sum_ndcg_1 = sum_ndcg_1 + ndcg_1
                    sum_hr_5 = sum_hr_5 + hr_5
                    sum_ndcg_5 = sum_ndcg_5 + ndcg_5
                    sum_hr_10 = sum_hr_10 + hr_10
                    sum_ndcg_10 = sum_ndcg_10 + ndcg_10
                    sum_hr_30 = sum_hr_30 + hr_30
                    sum_ndcg_30 = sum_ndcg_30 + ndcg_30
                    sum_hr_50 = sum_hr_50 + hr_50
                    sum_ndcg_50 = sum_ndcg_50 + ndcg_50
                    result_list_hr_all = result_list_hr_all + result_list_hr
                    result_list_ndcg_all = result_list_ndcg_all + result_list_ndcg

                sum_hr_1 /= max_step
                sum_ndcg_1 /= max_step
                sum_hr_5 /= max_step
                sum_ndcg_5 /= max_step
                sum_hr_10 /= max_step
                sum_ndcg_10 /= max_step
                sum_hr_30 /= max_step
                sum_ndcg_30 /= max_step
                sum_hr_50 /= max_step
                sum_ndcg_50 /= max_step

                if sum_hr_1 > self.hr_1 and sum_ndcg_1 > self.ndcg_1:
                    self.hr_1, self.ndcg_1 = sum_hr_1, sum_ndcg_1
                if sum_hr_5 > self.hr_5 and sum_ndcg_5 > self.ndcg_5:
                    self.hr_5, self.ndcg_5 = sum_hr_5, sum_ndcg_5
                if sum_hr_10 > self.hr_10 and sum_ndcg_10 > self.ndcg_10:
                    self.hr_10, self.ndcg_10 = sum_hr_10, sum_ndcg_10
                    self.best_result_hr = result_list_hr_all
                    self.best_result_ndcg = result_list_ndcg_all

                if sum_hr_30 > self.hr_30 and sum_ndcg_30 > self.ndcg_30:
                    self.hr_30, self.ndcg_30 = sum_hr_30, sum_ndcg_30
                if sum_hr_50 > self.hr_50 and sum_ndcg_50 > self.ndcg_50:
                    self.hr_50, self.ndcg_50 = sum_hr_50, sum_ndcg_50

                def summery(k, hr, ndcg):
                    tag_recall = 'recall@' + str(k)
                    tag_ndcg = 'ndgc@' + str(k)
                    summary_recall_rate = tf.Summary(value=[
                        tf.Summary.Value(tag=tag_recall, simple_value=hr)
                    ])
                    self.model.train_writer.add_summary(
                        summary_recall_rate, global_step=self.global_step)
                    summary_avg_ndcg = tf.Summary(value=[
                        tf.Summary.Value(tag=tag_ndcg, simple_value=ndcg)
                    ])
                    self.model.train_writer.add_summary(
                        summary_avg_ndcg, global_step=self.global_step)
                    self.logger.info(
                        'Test recall rate @ %d : %.4f   ndcg @ %d: %.4f' %
                        (k, hr, k, ndcg))

                summery(1, sum_hr_1, sum_ndcg_1)
                summery(5, sum_hr_5, sum_ndcg_5)
                summery(10, sum_hr_10, sum_ndcg_10)
                summery(30, sum_hr_30, sum_ndcg_30)
                summery(50, sum_hr_50, sum_ndcg_50)

            eval_topk()
            eval_mse()
            self.logger.info('End test. \tTest Cost time: %.2fs' %
                             (time.time() - test_start))

            # Start training

            self.logger.info(
                'Training....\tmax_epochs:%d\tepoch_size:%d' %
                (self.FLAGS.max_epochs, self.FLAGS.train_batch_size))
            start_time, avg_loss, self.best_auc, self.best_recall, self.best_ndcg = time.time(
            ), 0.0, 0.0, 0.0, 0.0
            loss_origin = []
            loss_time = []
            for epoch in range(self.FLAGS.max_epochs):
                #if epoch > 2:
                #lr = lr/1.5

                random.shuffle(self.train_set)
                self.logger.info('tain_set:%d' % len(self.train_set))
                epoch_start_time = time.time()
                learning_rate = self.FLAGS.learning_rate

                for step_i, train_batch_data in DataInput(
                        self.train_set, self.FLAGS.train_batch_size):
                    try:
                        #print(self.sess.run(global_step_lr))
                        if learning_rate > 0.001:
                            learning_rate = self.sess.run(
                                lr1,
                                feed_dict={global_step_lr: self.global_step})
                        else:
                            learning_rate = self.sess.run(
                                lr2,
                                feed_dict={global_step_lr: self.global_step})
                        #print(learning_rate)
                        add_summary = bool(self.global_step %
                                           self.FLAGS.display_freq == 0)
                        step_loss, step_loss_origin, step_loss_time, merge = self.model.train(
                            self.sess, train_batch_data, learning_rate,
                            add_summary, self.global_step, epoch)

                        self.sess.graph.finalize()

                        self.model.train_writer.add_summary(
                            merge, self.global_step)
                        avg_loss = avg_loss + step_loss
                        loss_origin.extend(step_loss_origin)
                        loss_time.extend(step_loss_time)
                        self.global_step = self.global_step + 1
                        self.one_epoch_step = self.one_epoch_step + 1

                        #evaluate for eval steps
                        if self.global_step % self.FLAGS.eval_freq == 0:
                            print(learning_rate)
                            self.logger.info("Epoch step is " +
                                             str(self.one_epoch_step))
                            self.logger.info("Global step is " +
                                             str(self.global_step))
                            self.logger.info("Train_loss is " +
                                             str(avg_loss /
                                                 self.FLAGS.eval_freq))
                            self.logger.info(
                                "Cross Entropy Loss is " +
                                str(np.mean(np.array(loss_origin))))
                            self.logger.info("Time Loss is " +
                                             str(np.mean(np.array(loss_time))))
                            # train_auc = self.model.metrics(sess=self.sess, batch_data=train_batch_data,
                            #                               global_step=self.global_step,name='train auc')
                            # self.logger.info('Batch Train AUC: %.4f' % train_auc)
                            # self.test_auc = self.model.metrics(sess=self.sess, batch_data=self.test_set,
                            #                               global_step=self.global_step,name='test auc')
                            # self.logger.info('Test AUC: %.4f' % self.test_auc)

                            eval_topk()
                            eval_mse()
                            avg_loss = 0
                            loss_origin = []
                            loss_time = []
                            loss_reconsume = []

                            self.save_model()
                            if self.FLAGS.draw_pic == True:
                                self.save_fig()

                    except Exception as e:
                        self.logger.info("Error!!!!!!!!!!!!")
                        self.logger.info(e)
                        traceback.print_exc()

                self.logger.info('one epoch Cost time: %.2f' %
                                 (time.time() - epoch_start_time))
                self.logger.info("Epoch step is " + str(self.one_epoch_step))
                self.logger.info("Global step is " + str(self.global_step))
                self.logger.info("Train_loss is " + str(step_loss))
                self.logger.info("Cross Entropy Loss is " +
                                 str(np.mean(np.array(loss_origin))))
                self.logger.info("Time Loss is " +
                                 str(np.mean(np.array(loss_time))))
                eval_topk()
                eval_mse()
                with open('best_result_hr_' + self.FLAGS.version, 'w+') as f:
                    f.write(str(self.best_result_hr))
                with open('best_result_ndcg' + self.FLAGS.version, 'w+') as f:
                    f.write(str(self.best_result_ndcg))
                self.logger.info('Max recall rate @ 1: %.4f   ndcg @ 1: %.4f' %
                                 (self.hr_1, self.ndcg_1))
                self.logger.info('Max recall rate @ 5: %.4f   ndcg @ 5: %.4f' %
                                 (self.hr_5, self.ndcg_5))
                self.logger.info(
                    'Max recall rate @ 10: %.4f   ndcg @ 10: %.4f' %
                    (self.hr_10, self.ndcg_10))
                self.logger.info(
                    'Max recall rate @ 30: %.4f   ndcg @ 30: %.4f' %
                    (self.hr_30, self.ndcg_30))
                self.logger.info(
                    'Max recall rate @ 50: %.4f   ndcg @ 50: %.4f' %
                    (self.hr_50, self.ndcg_50))

                self.one_epoch_step = 0
                #if self.global_step > 1000:
                #lr = lr / 2
                #if lr < 0.0005:
                #lr = lr * 0.99
                #elif self.FLAGS.type == "tmall":
                #lr = lr * 0.5
                #else:
                #lr = lr * 0.98

                self.logger.info('Epoch %d DONE\tCost time: %.2f' %
                                 (self.now_epoch, time.time() - start_time))

                self.now_epoch = self.now_epoch + 1
                self.one_epoch_step = 0

        self.model.save(self.sess, self.global_step)
        self.logger.info('best test_auc: ' + str(self.best_auc))
        self.logger.info('best recall: ' + str(self.best_recall))

        self.logger.info('Finished')
示例#12
0
    def set_threshold(self):
        dataset = facenet.get_dataset(self.input_datadir)
        with tf.Graph().as_default():
            gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5)
            sess = tf.Session(config=tf.ConfigProto(
                gpu_options=gpu_options, log_device_placement=False))
            with sess.as_default():
                pnet, rnet, onet = detect_face.create_mtcnn(
                    sess, './Model/data')

            image_size = 180
            input_image_size = 160

            facenet.load_model(self.model)

            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")
            embedding_size = embeddings.get_shape()[1]
            emb_array = np.zeros((1, embedding_size))

            self.classifier_exp = os.path.expanduser(self.classifier)
            with open(self.classifier_exp, 'rb') as infile:
                (model, class_names) = pickle.load(infile)

            threshold_dict = {}

            index = 0
            for data in dataset:
                total = 0
                for path in data.image_paths:
                    frame = cv2.imread(path, 0)
                    if frame.ndim == 2:
                        frame = facenet.to_rgb(frame)
                    cropped = []
                    scaled = []
                    scaled_reshape = []
                    cropped = facenet.flip(frame, False)
                    scaled.append(
                        cv2.resize(cropped, (image_size, image_size),
                                   interpolation=cv2.INTER_LINEAR))
                    scaled = cv2.resize(scaled[0],
                                        (input_image_size, input_image_size),
                                        interpolation=cv2.INTER_CUBIC)
                    scaled = facenet.prewhiten(scaled)
                    scaled_reshape.append(
                        scaled.reshape(-1, input_image_size, input_image_size,
                                       3))
                    feed_dict = {
                        images_placeholder: scaled_reshape[0],
                        phase_train_placeholder: False
                    }
                    emb_array[0, :] = sess.run(embeddings, feed_dict=feed_dict)

                    predictions = model.predict_proba(emb_array)
                    best_class_indices = np.argmax(predictions, axis=1)

                    best_class_probabilities = predictions[
                        np.arange(len(best_class_indices)), best_class_indices]
                    if best_class_indices[0] != index:
                        total -= predictions[0][best_class_indices[0]]
                    else:
                        total += predictions[0][index]
                try:
                    threshold_dict[str(
                        data.name)] = total / len(data.image_paths)
                except ZeroDivisionError:
                    pass
                index += 1
        try:
            with open('./Model/prediction_threshold/threshold.pickle',
                      'wb') as handle:
                pickle.dump(threshold_dict,
                            handle,
                            protocol=pickle.HIGHEST_PROTOCOL)
        except FileNotFoundError:
            os.mkdir('./Model/prediction_threshold')
            with open('./Model/prediction_threshold/threshold.pickle',
                      'wb') as handle:
                pickle.dump(threshold_dict,
                            handle,
                            protocol=pickle.HIGHEST_PROTOCOL)
示例#13
0
    def benchmark(self, ckpt_dir, outer_steps=100, inner_steps=1000):
        """Run repeatedly on dummy data to benchmark inference."""
        # Turn off Grappler optimizations.
        options = {"disable_meta_optimizer": True}
        tf.config.optimizer.set_experimental_options(options)

        # Create the model outside the loop body.
        hparams = registry.hparams(self.hparams_set)
        hparams_lib.add_problem_hparams(hparams, self.problem_name)
        model_cls = registry.model(self.model_name)
        model = model_cls(hparams, tf.estimator.ModeKeys.EVAL)

        # Run only the model body (no data pipeline) on device.
        feature_shape = [
            hparams.batch_size, 3 * self.image_size * self.image_size
        ]
        features = {"targets": tf.zeros(feature_shape, dtype=tf.int32)}

        # Call the model once to initialize the variables. Note that
        # this should never execute.
        with tf.variable_scope(self.model_name) as vso:
            transformed_features = model.bottom(features)
            with tf.variable_scope("body") as vsi:
                body_out = model.body(transformed_features)
            logits = model.top(body_out, features)
            model.loss(logits, features)

        def call_model(features):
            with tf.variable_scope(vso, reuse=tf.AUTO_REUSE):
                transformed_features = model.bottom(features)
                with tf.variable_scope(vsi, reuse=tf.AUTO_REUSE):
                    body_out = model.body(transformed_features)
                logits = model.top(body_out, features)
                return model.loss(logits, features)

        # Run the function body in a loop to amortize session overhead.
        loop_index = tf.zeros([], dtype=tf.int32)
        initial_loss = (tf.zeros([]), tf.zeros([]))

        def loop_cond(idx, _):
            return tf.less(idx, tf.constant(inner_steps, dtype=tf.int32))

        def loop_body(idx, _):
            return idx + 1, call_model(features)

        benchmark_op = tf.while_loop(loop_cond,
                                     loop_body, [loop_index, initial_loss],
                                     parallel_iterations=1,
                                     back_prop=False)

        session_config = tf.ConfigProto(gpu_options=tf.GPUOptions(
            allow_growth=False, per_process_gpu_memory_fraction=0.95))
        run_metadata = tf.RunMetadata()
        with tf.Session(config=session_config) as sess:
            self.restore_model(sess, ckpt_dir)
            tps = []
            for idx in range(outer_steps):
                start_time = time.time()
                sess.run(benchmark_op, run_metadata=run_metadata)
                elapsed_time = time.time() - start_time
                tps.append(inner_steps * hparams.batch_size * (64 * 64 * 3) /
                           elapsed_time)
                logging.error("Iterations %d processed %f TPS.", idx, tps[-1])
            # Skip the first iteration where all the setup and allocation happens.
            tps = np.asarray(tps[1:])
            logging.error("Mean/Std/Max/Min throughput = %f / %f / %f / %f",
                          np.mean(tps), np.std(tps), tps.max(), tps.min())
示例#14
0
            self.train()
            time.sleep(0.01)


if __name__ == '__main__':
    FPS = 60
    # For stats
    ep_rewards = [-200]

    # For more repetitive results
    random.seed(1)
    np.random.seed(1)
    tf.set_random_seed(1)

    # Memory fraction, used mostly when trai8ning multiple agents
    gpu_options = tf.GPUOptions(
        per_process_gpu_memory_fraction=MEMORY_FRACTION)
    backend.set_session(
        tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)))

    # Create models folder
    if not os.path.isdir('models'):
        os.makedirs('models')

    # Create agent and environment
    agent = DQNAgent()
    env = CarEnv()

    # Start training thread and wait for training to be initialized
    trainer_thread = Thread(target=agent.train_in_loop, daemon=True)
    trainer_thread.start()
    while not agent.training_initialized:
def main(args):
    sleep(random.random())
    output_dir = os.path.expanduser(args.output_dir)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    # Store some git revision info in a text file in the log directory
    src_path,_ = os.path.split(os.path.realpath(__file__))
    facenet.store_revision_info(src_path, output_dir, ' '.join(sys.argv))
    dataset = facenet.get_dataset(args.input_dir)
    
    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))
        with sess.as_default():
            pnet, rnet, onet = align.detect_face.create_mtcnn(sess, None)
    
    minsize = 20 # minimum size of face
    threshold = [ 0.6, 0.7, 0.7 ]  # three steps's threshold
    factor = 0.709 # scale factor

    # Add a random key to the filename to allow alignment using multiple processes
    random_key = np.random.randint(0, high=99999)
    bounding_boxes_filename = os.path.join(output_dir, 'bounding_boxes_%05d.txt' % random_key)
    
    with open(bounding_boxes_filename, "w") as text_file:
        nrof_images_total = 0
        nrof_successfully_aligned = 0
        if args.random_order:
            random.shuffle(dataset)
        for cls in dataset:
            output_class_dir = os.path.join(output_dir, cls.name)
            if not os.path.exists(output_class_dir):
                os.makedirs(output_class_dir)
                if args.random_order:
                    random.shuffle(cls.image_paths)
            for image_path in cls.image_paths:
                nrof_images_total += 1
                filename = os.path.splitext(os.path.split(image_path)[1])[0]
                output_filename = os.path.join(output_class_dir, filename+'.png')
                print(image_path)
                if not os.path.exists(output_filename):
                    try:
                        img = misc.imread(image_path)
                    except (IOError, ValueError, IndexError) as e:
                        errorMessage = '{}: {}'.format(image_path, e)
                        print(errorMessage)
                    else:
                        if img.ndim<2:
                            print('Unable to align "%s"' % image_path)
                            text_file.write('%s\n' % (output_filename))
                            continue
                        if img.ndim == 2:
                            img = facenet.to_rgb(img)
                        img = img[:,:,0:3]
    
                        bounding_boxes, _ = align.detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
                        nrof_faces = bounding_boxes.shape[0]
                        if nrof_faces>0:
                            det = bounding_boxes[:,0:4]
                            det_arr = []
                            img_size = np.asarray(img.shape)[0:2]
                            if nrof_faces>1:
                                if args.detect_multiple_faces:
                                    for i in range(nrof_faces):
                                        det_arr.append(np.squeeze(det[i]))
                                else:
                                    bounding_box_size = (det[:,2]-det[:,0])*(det[:,3]-det[:,1])
                                    img_center = img_size / 2
                                    offsets = np.vstack([ (det[:,0]+det[:,2])/2-img_center[1], (det[:,1]+det[:,3])/2-img_center[0] ])
                                    offset_dist_squared = np.sum(np.power(offsets,2.0),0)
                                    index = np.argmax(bounding_box_size-offset_dist_squared*2.0) # some extra weight on the centering
                                    det_arr.append(det[index,:])
                            else:
                                det_arr.append(np.squeeze(det))

                            for i, det in enumerate(det_arr):
                                det = np.squeeze(det)
                                bb = np.zeros(4, dtype=np.int32)
                                bb[0] = np.maximum(det[0]-args.margin/2, 0)
                                bb[1] = np.maximum(det[1]-args.margin/2, 0)
                                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')
                                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)
                                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)
                            text_file.write('%s\n' % (output_filename))
                            
    print('Total number of images: %d' % nrof_images_total)
    print('Number of successfully aligned images: %d' % nrof_successfully_aligned)
示例#16
0
def set_session(device_count=None, seed=0):
    gpu_options = tf.GPUOptions(allow_growth=True)
    config = tf.ConfigProto(gpu_options=gpu_options, device_count=device_count)
示例#17
0
def tf_execute_helper(data_cache, epoch, partitions, checkpoint_path, input_paths,
                      input_fn_string, model_fn_string, train_fn_string, mst, train=True):
    """

    :param data_cache:
    :param epoch:
    :param partitions:
    :param checkpoint_path:
    :param input_paths:
    :param input_fn_string:
    :param model_fn_string:
    :param train_fn_string:
    :param mst:
    :param train:
    :return:
    """
    begin_time = time.time()

    tf.reset_default_graph()

    input_fn = dill.loads(base64.b64decode(input_fn_string))
    model_fn = dill.loads(base64.b64decode(model_fn_string))
    train_fn = dill.loads(base64.b64decode(train_fn_string))

    losses = []
    errors = []
    message = ""
    for partition, input_path in zip(partitions, input_paths):
        tf.reset_default_graph()

        if input_path in data_cache:
            # already cached
            data = data_cache[input_path]
        else:
            data = input_fn(input_path)
            data_cache[input_path] = data

        opt, loss, error = model_fn(data, mst)
        train_step = opt.minimize(loss, colocate_gradients_with_ops=True)
        saver = tf.train.Saver()
        gpu_options = tf.GPUOptions(allow_growth = True)
        sess = tf.Session(
            config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=False, gpu_options=gpu_options))

        if os.path.exists(checkpoint_path + ".index"):
            saver.restore(sess, checkpoint_path)
        else:
            sess.run(tf.global_variables_initializer())

        message += "\nEPOCH: %d, PARTITION: %d, Model Building and Session Initialization Time: %d\n" % (
            epoch, partition, time.time() - begin_time)

        time_begin = time.time()
        loss_val, error_val = train_fn(sess, train_step, loss, error, train=train)
        losses.append(loss_val)
        errors.append(error_val)

        elapsed_time = time.time() - time_begin
        mode = "TRAIN" if train else "VALID"
        message += "EPOCH: %d, PARTITION: %d, %s LOSS: %f, ERROR: %f, Time: %f\n" % (epoch, partition, mode, loss_val, error_val, elapsed_time)

        if train:
            begin_time = time.time()
            saver.save(sess, checkpoint_path)
            message += "EPOCH: %d, PARTITION: %d, Checkpoint Save Time: %d\n" % (epoch, partition, time.time() - begin_time)

        sess.close()

    return {'loss': losses, 'error': errors, 'message': message[1:]}
示例#18
0
def main(_):
  tf.logging.set_verbosity(tf.logging.INFO)

  # If we have ps_hosts, then we'll assume that this is going to be a
  # distributed training run.  Configure the cluster appropriately.  Otherwise,
  # we just do everything in-process.
  if FLAGS.ps_hosts:
    cluster = tf.train.ClusterSpec({
        'ps': FLAGS.ps_hosts.split(','),
        'worker': FLAGS.worker_hosts.split(','),
    })

    if FLAGS.job_name == 'ps':
      # Ignore the GPU if we're the parameter server. This let's the PS run on
      # the same machine as a worker.
      config = tf.ConfigProto(device_count={'GPU': 0})
    elif FLAGS.job_name == 'worker':
      config = tf.ConfigProto(gpu_options=tf.GPUOptions(
          visible_device_list='%d' % FLAGS.gpu_device,
          allow_growth=True))
    else:
      raise ValueError('unknown job name "%s"' % FLAGS.job_name)

    server = tf.train.Server(
        cluster,
        job_name=FLAGS.job_name,
        task_index=FLAGS.task_index,
        config=config)

    if FLAGS.job_name == 'ps':
      return server.join()

    device_setter = tf.train.replica_device_setter(
        worker_device='/job:worker/task:%d' % FLAGS.task_index,
        cluster=cluster)

  else:
    server = None
    device_setter = tf.train.replica_device_setter(0)

  # Build the graph.
  with tf.Graph().as_default():
    with tf.device(device_setter):
      model = Model(FLAGS.input_base_path, FLAGS)

      # If an eval path is present, then create eval operators and set up scalar
      # summaries to report on the results.  Run the evals on the CPU since
      # the analogy eval requires a fairly enormous tensor to be allocated to
      # do the nearest neighbor search.
      if FLAGS.eval_base_path:
        wordsim_filenames = glob.glob(
            os.path.join(FLAGS.eval_base_path, '*.ws.tab'))

        for filename in wordsim_filenames:
          name = os.path.basename(filename).split('.')[0]
          with tf.device(tf.DeviceSpec(device_type='CPU')):
            op = model.wordsim_eval_op(filename, FLAGS.eval_word_prefix)
            tf.summary.scalar(name, op)

        analogy_filenames = glob.glob(
            os.path.join(FLAGS.eval_base_path, '*.an.tab'))

        for filename in analogy_filenames:
          name = os.path.basename(filename).split('.')[0]
          with tf.device(tf.DeviceSpec(device_type='CPU')):
            op = model.analogy_eval_op(filename)
            tf.summary.scalar(name, op)

      tf.summary.scalar('loss', model.loss_op)
      tf.summary.scalar('l2_loss', model.l2_loss_op)
      tf.summary.scalar('sigmoid_loss', model.sigmoid_loss_op)
      tf.summary.scalar('vector_regul_loss', model.vector_regul_loss_op)

    # Train on, soldier.
    supervisor = tf.train.Supervisor(
        logdir=FLAGS.output_base_path,
        is_chief=(FLAGS.task_index == 0),
        save_summaries_secs=60,
        recovery_wait_secs=5)

    max_step = FLAGS.num_epochs * model.steps_per_epoch
    master = server.target if server else ''
    with supervisor.managed_session(master) as session:
      local_step = 0
      global_step = session.run(model.global_step)
      while not supervisor.should_stop() and global_step < max_step:
        global_step, loss, _ = session.run([
            model.global_step, model.loss_op, model.train_op])

        if not np.isfinite(loss):
          raise ValueError('non-finite cost at step %d' % global_step)

        local_step += 1
        if local_step % 10 == 0:
          tf.logging.info(
              'local_step=%d global_step=%d loss=%.1f, %.1f%% complete',
              local_step, global_step, loss, 100.0 * global_step / max_step)

      if FLAGS.task_index == 0:
        supervisor.saver.save(
            session, supervisor.save_path, global_step=global_step)

        model.write_embeddings(FLAGS, session)
示例#19
0
def main(args):

    img_mean = np.array([134.10714722, 102.52040863, 87.15436554])
    img_stddev = np.sqrt(np.array([3941.30175781, 2856.94287109, 2519.35791016]))

    vae_def = importlib.import_module(args.vae_def)
    vae = vae_def.Vae(args.latent_var_size)
    gen_image_size = vae.get_image_size()

    with tf.Graph().as_default():
        tf.set_random_seed(args.seed)

        images = tf.placeholder(tf.float32, shape=(None,gen_image_size,gen_image_size,3), name='input')

        # Normalize
        images_norm = (images-img_mean) / img_stddev

        # Resize to appropriate size for the encoder
        images_norm_resize = tf.image.resize_images(images_norm, (gen_image_size,gen_image_size))

        # Create encoder network
        mean, log_variance = vae.encoder(images_norm_resize, True)

        epsilon = tf.random_normal((tf.shape(mean)[0], args.latent_var_size))
        std = tf.exp(log_variance/2)
        latent_var = mean + epsilon * std

        # Create decoder
        reconstructed_norm = vae.decoder(latent_var, False)

        # Un-normalize
        reconstructed = (reconstructed_norm*img_stddev) + img_mean

        # Create a saver
        saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=3)

        # Start running operations on the Graph
        gpu_memory_fraction = 1.0
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=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())
        coord = tf.train.Coordinator()
        tf.train.start_queue_runners(coord=coord, sess=sess)


        with sess.as_default():

            vae_checkpoint = os.path.expanduser(args.vae_checkpoint)
            print('Restoring VAE checkpoint: %s' % vae_checkpoint)
            saver.restore(sess, vae_checkpoint)

            filename = os.path.expanduser(args.attributes_filename)
            with h5py.File(filename,'r') as f:
                latent_vars = np.array(f.get('latent_vars'))
                attributes = np.array(f.get('attributes'))
                #fields = np.array(f.get('fields'))
                attribute_vectors = np.array(f.get('attribute_vectors'))

            # Reconstruct faces while adding varying amount of the selected attribute vector
            attribute_index = 31 # 31: 'Smiling'
            image_indices = [8,11,13,18,19,26,31,39,47,54,56,57,58,59,60,73]
            nrof_images = len(image_indices)
            nrof_interp_steps = 10
            sweep_latent_var = np.zeros((nrof_interp_steps*nrof_images, args.latent_var_size), np.float32)
            for j in range(nrof_images):
                image_index = image_indices[j]
                idx = np.argwhere(attributes[:,attribute_index]==-1)[image_index,0]
                for i in range(nrof_interp_steps):
                    sweep_latent_var[i+nrof_interp_steps*j,:] = latent_vars[idx,:] + 5.0*i/nrof_interp_steps*attribute_vectors[attribute_index,:]

            recon = sess.run(reconstructed, feed_dict={latent_var:sweep_latent_var})

            img = facenet.put_images_on_grid(recon, shape=(nrof_interp_steps*2,int(math.ceil(nrof_images/2))))

            image_filename = os.path.expanduser(args.output_image_filename)
            print('Writing generated image to %s' % image_filename)
            misc.imsave(image_filename, img)
示例#20
0
import numpy as np
import tensorflow.compat.v1 as tf

import vgg16
import tmp_model
import time

img1 = tmp_model.load_image("./test_img/tiger.jpeg")
img2 = tmp_model.load_image("./test_img/puzzle.jpeg")

batch1 = img1.reshape((1, 128, 128, 3))
batch2 = img2.reshape((1, 128, 128, 3))

batch = np.concatenate((batch1, batch2), 0)

with tf.Session(config=tf.ConfigProto(gpu_options=(tf.GPUOptions(
        per_process_gpu_memory_fraction=0.7)))) as sess:
    images = tf.placeholder("float", [2, 128, 128, 3], name="pc_img")
    feed_dict = {images: batch}
    noise = np.random.random([2, 128, 128, 3])

    vgg = vgg16.Vgg16("/tmp/vgg16/vgg16.npy")
    with tf.name_scope("content_vgg"):
        vgg.build(images)

    t1 = time.time() * 1000
    prob = sess.run(vgg.prob, feed_dict={images: noise})
    prob2 = sess.run(vgg.prob, feed_dict={images: batch})
    print(prob)
    print(prob2)
    t2 = time.time() * 1000
    print(t2 - t1)
示例#21
0
def main(args):
    model = SARGAN(img_size, BATCH_SIZE, img_channel=img_size[2])
    with tf.variable_scope("d_opt", reuse=tf.AUTO_REUSE):
        d_opt = tf.train.AdamOptimizer(learning_rate=LEARNING_RATE).minimize(
            model.d_loss, var_list=model.d_vars)
    with tf.variable_scope("g_opt", reuse=tf.AUTO_REUSE):
        g_opt = tf.train.AdamOptimizer(learning_rate=LEARNING_RATE).minimize(
            model.g_loss, var_list=model.g_vars)
    if (retrain == 0):
        saver = tf.train.Saver(max_to_keep=20)
    else:
        saver = tf.train.import_meta_graph(trained_model_path +
                                           '/sargan_mnist.meta')

    gpu_options = tf.GPUOptions(allow_growth=True,
                                visible_device_list=str(GPU_ID))
    config = tf.ConfigProto(gpu_options=gpu_options)

    progress_bar = tqdm(range(MAX_EPOCH), unit="epoch")
    #list of loss values each item is the loss value of one ieteration
    train_d_loss_values = []
    train_g_loss_values = []

    #test_imgs, test_classes = get_data(test_filename)
    #imgs, classes = get_data(train_filename)
    with tf.Session(config=config) as sess:
        if (retrain == 0):
            sess.run(tf.global_variables_initializer())
        else:
            sess.run(tf.global_variables_initializer())
            saver.restore(sess, tf.train.latest_checkpoint(trained_model_path))
        #test_copies = test_imgs.astype('float32')
        for epoch in progress_bar:
            NUM_TEST_PER_EPOCH = 1
            counter = 0
            epoch_start_time = time.time()
            encoded_data, original_data = transfrom_data(NUM_TEST_PER_EPOCH)
            #shuffle(copies)
            #divide the images into equal sized batches
            #image_batches = np.array(list(chunks(copies, BATCH_SIZE)))
            for i in range(NUM_ITERATION):
                #getting a batch from the training data
                #one_batch_of_imgs = image_batches[i]
                #copy the batch
                features = original_data[i]
                #corrupt the images
                corrupted_batch = encoded_data[i]
                _, m = sess.run([d_opt, model.d_loss],
                                feed_dict={
                                    model.image: features,
                                    model.cond: corrupted_batch
                                })
                _, M = sess.run([g_opt, model.g_loss],
                                feed_dict={
                                    model.image: features,
                                    model.cond: corrupted_batch
                                })
                train_d_loss_values.append(m)
                train_g_loss_values.append(M)
                #print some notifications
                counter += 1
                if counter % 25 == 0:
                    print("\rEpoch [%d], Iteration [%d]: time: %4.4f, d_loss: %.8f, g_loss: %.8f" \
                      % (epoch, counter, time.time() - epoch_start_time, m, M))

            # save the trained network
            if epoch % SAVE_EVERY_EPOCH == 0:
                save_path = saver.save(sess,
                                       (trained_model_path + "/sargan_mnist"))
                print("\n\nModel saved in file: %s\n\n" % save_path)
示例#22
0
import numpy as np
import matplotlib.pyplot as plt
dataset = BinaryDbReader(mode='training',
                         batch_size=8,
                         shuffle=True,
                         hand_crop=True,
                         use_wrist_coord=False,
                         sigma=10,
                         coord_uv_noise=False,
                         crop_center_noise=False,
                         crop_offset_noise=False,
                         crop_scale_noise=False)
data = dataset.get()
print(data)
# Start TF
gpu_options = tf.GPUOptions(allow_growth=True, )
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
tf.train.start_queue_runners(sess=sess)
sess.run(tf.global_variables_initializer())
while 1:
    d = sess.run(data)
    # print(d)
    hm_sum = np.sum(d["scoremap"][0], axis=2)
    plt.figure(figsize=(12, 12))
    plt.subplot(121)
    plt.imshow(hm_sum)
    plt.subplot(122)
    img = d["image_crop"][0] + 0.5
    img[:, :, 0] += hm_sum
    plt.imshow(img)
    plt.show()
    x_tf = tf.identity(x_in)

    model = nn_dict[model_type](is_train, hps)
    cleverhans_model = CallableModelWrapper(forward_pass, 'logits')

    # Separate forward pass graph for Cleverhans wrapper (for C&W and PGD attacks) placed on the last GPU
    logits = forward_pass(x_tf)

    # Error rate
    incorrect_prediction = tf.not_equal(tf.argmax(logits, 1),
                                        tf.argmax(y_in, 1))
    err_rate = tf.reduce_mean(tf.cast(incorrect_prediction, tf.float32))

    # GPU settings
    gpu_options = tf.GPUOptions(visible_device_list=str(hps.gpus)[1:-1],
                                per_process_gpu_memory_fraction=hps.gpu_memory)
    config = tf.ConfigProto(allow_soft_placement=True, gpu_options=gpu_options)

# ---------  Pytorch part for the Kolter-Wong model ----------
device = torch.device('cuda:' + str(hps.gpus[-1]))
torch.cuda.set_device(hps.gpus[-1])

model_torch = kolter_wong.models.select_model(hps.nn_type, hps.n_in,
                                              hps.n_out).to(device)
for var in model_torch.parameters():
    var.requires_grad = False
# ----------      end      ----------

with tf.Session(graph=graph, config=config) as sess:
    with graph.as_default(), tf.device('/gpu:0'):
        pgd_ae_tensor = ae.pgd_attack(x_tf, y_in, cleverhans_model, hps.p, eps,
示例#24
0
def initialize_globals():
    c = AttrDict()

    # Augmentations
    c.augmentations = parse_augmentations(FLAGS.augment)
    if len(c.augmentations
           ) > 0 and FLAGS.feature_cache and FLAGS.cache_for_epochs == 0:
        log_warn(
            'Due to current feature-cache settings the exact same sample augmentations of the first '
            'epoch will be repeated on all following epochs. This could lead to unintended over-fitting. '
            'You could use --cache_for_epochs <n_epochs> to invalidate the cache after a given number of epochs.'
        )

    # Caching
    if FLAGS.cache_for_epochs == 1:
        log_warn(
            '--cache_for_epochs == 1 is (re-)creating the feature cache on every epoch but will never use it.'
        )

    # Read-buffer
    FLAGS.read_buffer = parse_file_size(FLAGS.read_buffer)

    # Set default dropout rates
    if FLAGS.dropout_rate2 < 0:
        FLAGS.dropout_rate2 = FLAGS.dropout_rate
    if FLAGS.dropout_rate3 < 0:
        FLAGS.dropout_rate3 = FLAGS.dropout_rate
    if FLAGS.dropout_rate6 < 0:
        FLAGS.dropout_rate6 = FLAGS.dropout_rate

    # Set default checkpoint dir
    if not FLAGS.checkpoint_dir:
        FLAGS.checkpoint_dir = xdg.save_data_path(
            os.path.join('deepspeech', 'checkpoints'))

    if FLAGS.load_train not in ['last', 'best', 'init', 'auto']:
        FLAGS.load_train = 'auto'

    if FLAGS.load_evaluate not in ['last', 'best', 'auto']:
        FLAGS.load_evaluate = 'auto'

    # Set default summary dir
    if not FLAGS.summary_dir:
        FLAGS.summary_dir = xdg.save_data_path(
            os.path.join('deepspeech', 'summaries'))

    # Standard session configuration that'll be used for all new sessions.
    c.session_config = tfv1.ConfigProto(
        allow_soft_placement=True,
        log_device_placement=FLAGS.log_placement,
        inter_op_parallelism_threads=FLAGS.inter_op_parallelism_threads,
        intra_op_parallelism_threads=FLAGS.intra_op_parallelism_threads,
        gpu_options=tfv1.GPUOptions(allow_growth=FLAGS.use_allow_growth))

    # CPU device
    c.cpu_device = '/cpu:0'

    # Available GPU devices
    c.available_devices = get_available_gpus(c.session_config)

    # If there is no GPU available, we fall back to CPU based operation
    if not c.available_devices:
        c.available_devices = [c.cpu_device]

    if FLAGS.bytes_output_mode:
        c.alphabet = UTF8Alphabet()
    else:
        c.alphabet = Alphabet(os.path.abspath(FLAGS.alphabet_config_path))

    # Geometric Constants
    # ===================

    # For an explanation of the meaning of the geometric constants, please refer to
    # doc/Geometry.md

    # Number of MFCC features
    c.n_input = 26  # TODO: Determine this programmatically from the sample rate

    # The number of frames in the context
    c.n_context = 9  # TODO: Determine the optimal value using a validation data set

    # Number of units in hidden layers
    c.n_hidden = FLAGS.n_hidden

    c.n_hidden_1 = c.n_hidden

    c.n_hidden_2 = c.n_hidden

    c.n_hidden_5 = c.n_hidden

    # LSTM cell state dimension
    c.n_cell_dim = c.n_hidden

    # The number of units in the third layer, which feeds in to the LSTM
    c.n_hidden_3 = c.n_cell_dim

    # Units in the sixth layer = number of characters in the target language plus one
    c.n_hidden_6 = c.alphabet.GetSize() + 1  # +1 for CTC blank label

    # Size of audio window in samples
    if (FLAGS.feature_win_len * FLAGS.audio_sample_rate) % 1000 != 0:
        log_error(
            '--feature_win_len value ({}) in milliseconds ({}) multiplied '
            'by --audio_sample_rate value ({}) must be an integer value. Adjust '
            'your --feature_win_len value or resample your audio accordingly.'
            ''.format(FLAGS.feature_win_len, FLAGS.feature_win_len / 1000,
                      FLAGS.audio_sample_rate))
        sys.exit(1)

    c.audio_window_samples = FLAGS.audio_sample_rate * (FLAGS.feature_win_len /
                                                        1000)

    # Stride for feature computations in samples
    if (FLAGS.feature_win_step * FLAGS.audio_sample_rate) % 1000 != 0:
        log_error(
            '--feature_win_step value ({}) in milliseconds ({}) multiplied '
            'by --audio_sample_rate value ({}) must be an integer value. Adjust '
            'your --feature_win_step value or resample your audio accordingly.'
            ''.format(FLAGS.feature_win_step, FLAGS.feature_win_step / 1000,
                      FLAGS.audio_sample_rate))
        sys.exit(1)

    c.audio_step_samples = FLAGS.audio_sample_rate * (FLAGS.feature_win_step /
                                                      1000)

    if FLAGS.one_shot_infer:
        if not path_exists_remote(FLAGS.one_shot_infer):
            log_error(
                'Path specified in --one_shot_infer is not a valid file.')
            sys.exit(1)

    if FLAGS.train_cudnn and FLAGS.load_cudnn:
        log_error('Trying to use --train_cudnn, but --load_cudnn '
                  'was also specified. The --load_cudnn flag is only '
                  'needed when converting a CuDNN RNN checkpoint to '
                  'a CPU-capable graph. If your system is capable of '
                  'using CuDNN RNN, you can just specify the CuDNN RNN '
                  'checkpoint normally with --save_checkpoint_dir.')
        sys.exit(1)

    # If separate save and load flags were not specified, default to load and save
    # from the same dir.
    if not FLAGS.save_checkpoint_dir:
        FLAGS.save_checkpoint_dir = FLAGS.checkpoint_dir

    if not FLAGS.load_checkpoint_dir:
        FLAGS.load_checkpoint_dir = FLAGS.checkpoint_dir

    ConfigSingleton._config = c  # pylint: disable=protected-access
示例#25
0
 def __init__(self,
              hidden_size=100,
              out_size=100,
              batch_size=300,
              n_node=None,
              lr=None,
              l2=None,
              step=1,
              decay=None,
              lr_dc=0.1,
              nonhybrid=False):
     super(GGNN, self).__init__(hidden_size, out_size, batch_size,
                                nonhybrid)
     self.embedding = tf.get_variable(
         shape=[n_node, hidden_size],
         name='embedding',
         dtype=tf.float32,
         initializer=tf.random_uniform_initializer(-self.stdv, self.stdv))
     self.adj_in = tf.placeholder(dtype=tf.float32,
                                  shape=[self.batch_size, None, None])
     self.adj_out = tf.placeholder(dtype=tf.float32,
                                   shape=[self.batch_size, None, None])
     self.n_node = n_node
     self.L2 = l2
     self.step = step
     self.nonhybrid = nonhybrid
     self.W_in = tf.get_variable('W_in',
                                 shape=[self.out_size, self.out_size],
                                 dtype=tf.float32,
                                 initializer=tf.random_uniform_initializer(
                                     -self.stdv, self.stdv))
     self.b_in = tf.get_variable('b_in', [self.out_size],
                                 dtype=tf.float32,
                                 initializer=tf.random_uniform_initializer(
                                     -self.stdv, self.stdv))
     self.W_out = tf.get_variable('W_out', [self.out_size, self.out_size],
                                  dtype=tf.float32,
                                  initializer=tf.random_uniform_initializer(
                                      -self.stdv, self.stdv))
     self.b_out = tf.get_variable('b_out', [self.out_size],
                                  dtype=tf.float32,
                                  initializer=tf.random_uniform_initializer(
                                      -self.stdv, self.stdv))
     with tf.variable_scope('ggnn_model', reuse=None):
         self.loss_train, _ = self.forward(self.ggnn())
     with tf.variable_scope('ggnn_model', reuse=True):
         self.loss_test, self.score_test = self.forward(self.ggnn(),
                                                        train=False)
     self.global_step = tf.Variable(0)
     self.learning_rate = tf.train.exponential_decay(
         lr,
         global_step=self.global_step,
         decay_steps=decay,
         decay_rate=lr_dc,
         staircase=True)
     self.opt = tf.train.AdamOptimizer(self.learning_rate).minimize(
         self.loss_train, global_step=self.global_step)
     gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.8)
     config = tf.ConfigProto(gpu_options=gpu_options)
     config.gpu_options.allow_growth = True
     self.sess = tf.Session(config=config)
     self.sess.run(tf.global_variables_initializer())
示例#26
0
    # But many TF deprecation warnings in 1.14.0, e.g.:
    # "The name tf.GPUOptions is deprecated. Please use tf.compat.v1.GPUOptions
    # instead". See tf_export.py
    if g.tf_ver >= parse_version('1.14.0'):
        import tensorflow.compat.v1 as tf
    else:
        import tensorflow as tf
    # TODO(KGF): above, builder.py (bug workaround), mpi_launch_tensorflow.py,
    # and runner.py are the only files that import tensorflow directly

    from keras.backend.tensorflow_backend import set_session
    # KGF: next 3 lines dump many TensorFlow diagnostics to stderr.
    # All MPI ranks first "Successfully opened dynamic library libcuda"
    # then, one by one: ID GPU, libcudart, libcublas, libcufft, ...
    # Finally, "Device interconnect StreamExecutor with strength 1 edge matrix"
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.95,
                                allow_growth=True)
    config = tf.ConfigProto(gpu_options=gpu_options)
    set_session(tf.Session(config=config))
    g.flush_all_inorder()
else:
    os.environ['KERAS_BACKEND'] = 'theano'
    base_compile_dir = '{}/tmp/{}-{}'.format(conf['paths']['output_path'],
                                             socket.gethostname(),
                                             g.task_index)
    os.environ['THEANO_FLAGS'] = (
        'device=gpu{},floatX=float32,base_compiledir={}'.format(
            g.MY_GPU, base_compile_dir))  # ,mode=NanGuardMode'
    # import theano
    # import keras
for i in range(g.num_workers):
    g.comm.Barrier()
示例#27
0
    def __call__(self, reference_observations: np.ndarray,
                 generated_observations: np.ndarray) -> float:
        '''
        Computes the FVD between the reference and the generated observations

        :param reference_observations: (bs, observations_count, channels, height, width) tensor with reference observations
        :param generated_observations: (bs, observations_count, channels, height, width) tensor with generated observations
        :return: The FVD between the two distributions
        '''

        # Multiples of 16 must be fes to the embeddings network
        embedding_batch_size = 16

        # Puts the observations in the expected range [0, 255]
        reference_observations = reference_observations * 255
        generated_observations = generated_observations * 255

        # Converts dimensions to tensorflow format by moving channels
        reference_observations = np.moveaxis(reference_observations, 2, -1)
        generated_observations = np.moveaxis(generated_observations, 2, -1)

        # Cuts the sequences to multiples of the batch size
        reference_observations = cut_to_multiple_size(reference_observations,
                                                      embedding_batch_size)
        generated_observations = cut_to_multiple_size(generated_observations,
                                                      embedding_batch_size)

        sequences, sequence_length, height, width, channels = reference_observations.shape
        with tf.Graph().as_default():

            # Builds the graph
            input_placeholder = tf.placeholder(tf.float32, [
                embedding_batch_size, sequence_length, height, width, channels
            ])
            embeddings = create_id3_embedding(
                preprocess(input_placeholder, (224, 224)))

            reference_embeddings_placeholder = tf.placeholder(
                tf.float32, [sequences, 400])
            generated_embeddings_placeholder = tf.placeholder(
                tf.float32, [sequences, 400])

            fvd = calculate_fvd(reference_embeddings_placeholder,
                                generated_embeddings_placeholder)

            gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.3)
            with tf.Session(config=tf.ConfigProto(
                    gpu_options=gpu_options)) as sess:
                sess.run(tf.global_variables_initializer())
                sess.run(tf.tables_initializer())

                # Computes the embeddings
                reference_embeddings = extract_embeddings(
                    embeddings,
                    input_placeholder,
                    reference_observations,
                    sess,
                    batch_size=embedding_batch_size)
                generated_embeddings = extract_embeddings(
                    embeddings,
                    input_placeholder,
                    generated_observations,
                    sess,
                    batch_size=embedding_batch_size)

                # Computes the fvd
                fvd_np = sess.run(fvd,
                                  feed_dict={
                                      reference_embeddings_placeholder:
                                      reference_embeddings,
                                      generated_embeddings_placeholder:
                                      generated_embeddings
                                  })

                return float(fvd_np)
示例#28
0
    def train(self):

        start_time = time.time()

        # Config GPU options
        if self.FLAGS.per_process_gpu_memory_fraction == 0.0:
            gpu_options = tf.GPUOptions(allow_growth=True)
        elif self.FLAGS.per_process_gpu_memory_fraction == 1.0:
            gpu_options = tf.GPUOptions()

        else:
            gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=self.
                                        FLAGS.per_process_gpu_memory_fraction,
                                        allow_growth=True)

        os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
        os.environ['CUDA_VISIBLE_DEVICES'] = self.FLAGS.cuda_visible_devices

        self.sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
        if not tf.test.gpu_device_name():
            self.logger.warning("No GPU is found")
        else:
            self.logger.info(tf.test.gpu_device_name())

        global_step_lr = tf.Variable(0, trainable=False)
        lr1 = tf.train.exponential_decay(
            learning_rate=self.FLAGS.learning_rate,
            global_step=global_step_lr,
            decay_steps=1000,
            decay_rate=0.995,
            staircase=True)
        lr2 = tf.train.exponential_decay(learning_rate=0.001,
                                         global_step=global_step_lr,
                                         decay_steps=1000,
                                         decay_rate=0.995,
                                         staircase=True)

        with self.sess.as_default():

            if self.FLAGS.experiment_type == "SR_GNN":
                self.model = SR_GNN(self.FLAGS, self.emb, self.sess)
            if self.FLAGS.experiment_type == "GC_SAN":
                self.model = GC_SAN(self.FLAGS, self.emb, self.sess)

            elif self.FLAGS.experiment_type == "GNN_T_Att":
                self.model = GNN_T_Att(self.FLAGS, self.emb, self.sess)
            elif self.FLAGS.experiment_type == "TimeAwareSR_GNN":
                self.model = TimeAwareSR_GNN(self.FLAGS, self.emb, self.sess)
            elif self.FLAGS.experiment_type == "GatedGrnnRec":
                self.model = GatedGrnnRec(self.FLAGS, self.emb, self.sess)
            elif self.FLAGS.experiment_type == "OrderedGatedGrnnRec":
                self.model = OrderedGatedGrnnRec(self.FLAGS, self.emb,
                                                 self.sess)
            elif self.FLAGS.experiment_type == "ModifiedGatedGrnnRec":
                self.model = ModifiedGatedGrnnRec(self.FLAGS, self.emb,
                                                  self.sess)
            elif self.FLAGS.experiment_type == "FGNN":
                self.model = FGNN(self.FLAGS, self.emb, self.sess)
            self.logger.info('Init finish.\tCost time: %.2fs' %
                             (time.time() - start_time))

            #AUC暂时不看
            # test_auc = self.model.metrics(sess=self.sess,
            #                               batch_data=self.test_set,
            #                               global_step=self.global_step,
            #                               name='test auc')

            # Eval init AUC
            # self.logger.info('Init AUC: %.4f' % test_auc)

            test_start = time.time()
            self.hr_1, self.ndcg_1, self.hr_5, self.ndcg_5, self.hr_10, self.ndcg_10, self.hr_20, self.ndcg_20, self.hr_50, self.ndcg_50 = \
                0,0,0,0,0,0,0,0,0,0
            self.best_dev_hr_10 = 0
            self.best_dev_ndcg_10 = 0
            self.best_result_hr = []
            self.best_result_ndcg = []

            self.max_stay_count = 0
            self.last_dev_hr_10 = 0
            self.last_dev_ndcg_10 = 0

            def eval_topk():

                dev_sum_hr_1, dev_sum_ndcg_1, dev_sum_mrr_1, dev_sum_hr_5, dev_sum_ndcg_5, dev_sum_mrr_5, \
                dev_sum_hr_10, dev_sum_ndcg_10, dev_sum_mrr_10, dev_sum_hr_20, dev_sum_ndcg_20, dev_sum_mrr_20, \
                dev_sum_hr_50, dev_sum_ndcg_50, dev_sum_mrr_50 = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                result_list_hr_all = []
                result_list_ndcg_all = []

                max_step = 0

                for step_i, batch_data in DataInput(
                        self.dev_set, self.FLAGS.test_batch_size):
                    max_step = 1 + max_step
                    if self.FLAGS.experiment_type == "NARM" or \
                            self.FLAGS.experiment_type == "NARM+" or \
                            self.FLAGS.experiment_type == "NARM++" or \
                            self.FLAGS.experiment_type == "SR_GNN":
                        hr_1, ndcg_1, mrr_1, hr_5, ndcg_5, mrr_5, \
                        hr_10, ndcg_10, mrr_10, hr_20, ndcg_20, mrr_20, \
                        hr_50, ndcg_50, mrr_50, \
                        result_list_hr, result_list_ndcg = \
                            self.model.metrics_topK_concat(sess=self.sess,
                                                           batch_data=batch_data,
                                                           global_step=self.global_step,
                                                           topk=self.FLAGS.top_k)
                    else:
                        hr_1, ndcg_1, mrr_1, hr_5, ndcg_5, mrr_5, \
                        hr_10, ndcg_10, mrr_10, hr_20, ndcg_20, mrr_20, \
                        hr_50, ndcg_50, mrr_50, \
                        result_list_hr, result_list_ndcg = \
                            self.model.metrics_topK(sess=self.sess,
                                                    batch_data=batch_data,
                                                    global_step=self.global_step,
                                                    topk=self.FLAGS.top_k)
                    dev_sum_hr_1 = dev_sum_hr_1 + hr_1
                    dev_sum_ndcg_1 = dev_sum_ndcg_1 + ndcg_1
                    dev_sum_mrr_1 = dev_sum_mrr_1 + mrr_1
                    dev_sum_hr_5 = dev_sum_hr_5 + hr_5
                    dev_sum_ndcg_5 = dev_sum_ndcg_5 + ndcg_5
                    dev_sum_mrr_5 = dev_sum_mrr_5 + mrr_5
                    dev_sum_hr_10 = dev_sum_hr_10 + hr_10
                    dev_sum_ndcg_10 = dev_sum_ndcg_10 + ndcg_10
                    dev_sum_mrr_10 = dev_sum_mrr_10 + mrr_10
                    dev_sum_hr_20 = dev_sum_hr_20 + hr_20
                    dev_sum_ndcg_20 = dev_sum_ndcg_20 + ndcg_20
                    dev_sum_mrr_20 = dev_sum_mrr_20 + mrr_20
                    dev_sum_hr_50 = dev_sum_hr_50 + hr_50
                    dev_sum_ndcg_50 = dev_sum_ndcg_50 + ndcg_50
                    dev_sum_mrr_50 = dev_sum_mrr_50 + mrr_50

                dev_sum_hr_1 /= max_step
                dev_sum_ndcg_1 /= max_step
                dev_sum_mrr_1 /= max_step
                dev_sum_hr_5 /= max_step
                dev_sum_ndcg_5 /= max_step
                dev_sum_mrr_5 /= max_step
                dev_sum_hr_10 /= max_step
                dev_sum_ndcg_10 /= max_step
                dev_sum_mrr_10 /= max_step
                dev_sum_hr_20 /= max_step
                dev_sum_ndcg_20 /= max_step
                dev_sum_mrr_20 /= max_step
                dev_sum_hr_50 /= max_step
                dev_sum_ndcg_50 /= max_step
                dev_sum_mrr_50 /= max_step

                sum_hr_1, sum_ndcg_1, sum_mrr_1, sum_hr_5, sum_ndcg_5, sum_mrr_5, \
                sum_hr_10, sum_ndcg_10, sum_mrr_10, sum_hr_20, sum_ndcg_20, sum_mrr_20, \
                sum_hr_50, sum_ndcg_50, sum_mrr_50 = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                result_list_hr_all = []
                result_list_ndcg_all = []

                max_step = 0

                for step_i, batch_data in DataInput(
                        self.test_set, self.FLAGS.test_batch_size):
                    max_step = 1 + max_step
                    if self.FLAGS.experiment_type == "NARM" or \
                            self.FLAGS.experiment_type == "NARM+" or \
                            self.FLAGS.experiment_type == "NARM++" or \
                            self.FLAGS.experiment_type == "SR_GNN":
                        hr_1, ndcg_1, mrr_1, hr_5, ndcg_5, mrr_5, \
                        hr_10, ndcg_10, mrr_10, hr_20, ndcg_20, mrr_20, \
                        hr_50, ndcg_50, mrr_50, \
                        result_list_hr, result_list_ndcg = \
                            self.model.metrics_topK_concat(sess=self.sess,
                                                           batch_data=batch_data,
                                                           global_step=self.global_step,
                                                           topk=self.FLAGS.top_k)
                    else:
                        hr_1, ndcg_1, mrr_1, hr_5, ndcg_5, mrr_5, \
                        hr_10, ndcg_10, mrr_10, hr_20, ndcg_20, mrr_20, \
                        hr_50, ndcg_50, mrr_50, \
                        result_list_hr, result_list_ndcg = \
                            self.model.metrics_topK(sess=self.sess,
                                                    batch_data=batch_data,
                                                    global_step=self.global_step,
                                                    topk=self.FLAGS.top_k)
                    sum_hr_1 = sum_hr_1 + hr_1
                    sum_ndcg_1 = sum_ndcg_1 + ndcg_1
                    sum_mrr_1 = sum_mrr_1 + mrr_1
                    sum_hr_5 = sum_hr_5 + hr_5
                    sum_ndcg_5 = sum_ndcg_5 + ndcg_5
                    sum_mrr_5 = sum_mrr_5 + mrr_5

                    sum_hr_10 = sum_hr_10 + hr_10
                    sum_ndcg_10 = sum_ndcg_10 + ndcg_10
                    sum_mrr_10 = sum_mrr_10 + mrr_10
                    sum_hr_20 = sum_hr_20 + hr_20
                    sum_ndcg_20 = sum_ndcg_20 + ndcg_20
                    sum_mrr_20 = sum_mrr_20 + mrr_20

                    sum_hr_50 = sum_hr_50 + hr_50
                    sum_ndcg_50 = sum_ndcg_50 + ndcg_50
                    sum_mrr_50 = sum_mrr_50 + mrr_50
                    result_list_hr_all = result_list_hr_all + result_list_hr
                    result_list_ndcg_all = result_list_ndcg_all + result_list_ndcg

                sum_hr_1 /= max_step
                sum_ndcg_1 /= max_step
                sum_mrr_1 /= max_step
                sum_hr_5 /= max_step
                sum_ndcg_5 /= max_step
                sum_mrr_5 /= max_step
                sum_hr_10 /= max_step
                sum_ndcg_10 /= max_step
                sum_mrr_10 /= max_step
                sum_hr_20 /= max_step
                sum_ndcg_20 /= max_step
                sum_mrr_20 /= max_step
                sum_hr_50 /= max_step
                sum_ndcg_50 /= max_step
                sum_mrr_50 /= max_step

                if dev_sum_hr_10 > self.best_dev_hr_10 and dev_sum_ndcg_10 > self.best_dev_ndcg_10:
                    self.best_dev_hr_10 = dev_sum_hr_10
                    self.best_dev_ndcg_10 = dev_sum_ndcg_10

                    self.hr_1, self.ndcg_1, self.mrr_1 = sum_hr_1, sum_ndcg_1, sum_mrr_1
                    self.hr_5, self.ndcg_5, self.mrr_5 = sum_hr_5, sum_ndcg_5, sum_mrr_5
                    self.hr_10, self.ndcg_10, self.mrr_10 = sum_hr_10, sum_ndcg_10, sum_mrr_10
                    self.best_result_hr = result_list_hr_all
                    self.best_result_ndcg = result_list_ndcg_all
                    self.hr_20, self.ndcg_20, self.mrr_20 = sum_hr_20, sum_ndcg_20, sum_mrr_20
                    self.hr_50, self.ndcg_50, self.mrr_50 = sum_hr_50, sum_ndcg_50, sum_mrr_50

                def dev_log(k, hr, ndcg, mrr):

                    self.logger.info(
                        'Dev recall rate @ %d : %.4f   ndcg @ %d: %.4f' %
                        (k, hr, k, ndcg))

                dev_log(1, dev_sum_hr_1, dev_sum_ndcg_1, dev_sum_mrr_1)
                dev_log(5, dev_sum_hr_5, dev_sum_ndcg_5, dev_sum_mrr_5)
                dev_log(10, dev_sum_hr_10, dev_sum_ndcg_10, dev_sum_mrr_10)
                dev_log(20, dev_sum_hr_20, dev_sum_ndcg_20, dev_sum_mrr_20)
                dev_log(50, dev_sum_hr_50, dev_sum_ndcg_50, dev_sum_mrr_50)

                def summery(k, hr, ndcg, mrr):
                    tag_recall = 'test recall@' + str(k)
                    tag_ndcg = 'test ndgc@' + str(k)
                    summary_recall_rate = tf.Summary(value=[
                        tf.Summary.Value(tag=tag_recall, simple_value=hr)
                    ])
                    self.model.train_writer.add_summary(
                        summary_recall_rate, global_step=self.global_step)
                    summary_avg_ndcg = tf.Summary(value=[
                        tf.Summary.Value(tag=tag_ndcg, simple_value=ndcg)
                    ])
                    self.model.train_writer.add_summary(
                        summary_avg_ndcg, global_step=self.global_step)
                    self.logger.info(
                        'Test recall rate @ %d : %.4f   ndcg @ %d: %.4f  mrr @ %d: %.4f'
                        % (k, hr, k, ndcg, k, mrr))

                summery(1, sum_hr_1, sum_ndcg_1, sum_mrr_1)
                summery(5, sum_hr_5, sum_ndcg_5, sum_mrr_5)
                summery(10, sum_hr_10, sum_ndcg_10, sum_mrr_10)
                summery(20, sum_hr_20, sum_ndcg_20, sum_mrr_20)
                summery(50, sum_hr_50, sum_ndcg_50, sum_mrr_50)

            eval_topk()
            self.logger.info('End test. \tTest Cost time: %.2fs' %
                             (time.time() - test_start))

            # Start training

            self.logger.info(
                'Training....\tmax_epochs:%d\tepoch_size:%d' %
                (self.FLAGS.max_epochs, self.FLAGS.train_batch_size))
            start_time, avg_loss, self.best_auc, self.best_recall, self.best_ndcg = time.time(
            ), 0.0, 0.0, 0.0, 0.0
            for epoch in range(self.FLAGS.max_epochs):
                #if epoch > 2:
                #lr = lr/1.5

                random.shuffle(self.train_set)
                self.logger.info('tain_set:%d' % len(self.train_set))
                epoch_start_time = time.time()
                learning_rate = self.FLAGS.learning_rate

                for step_i, train_batch_data in DataInput(
                        self.train_set, self.FLAGS.train_batch_size):

                    # try:

                    #print(self.sess.run(global_step_lr))
                    if learning_rate > 0.001:
                        learning_rate = self.sess.run(
                            lr1, feed_dict={global_step_lr: self.global_step})
                    else:
                        learning_rate = self.sess.run(
                            lr2, feed_dict={global_step_lr: self.global_step})
                    #print(learning_rate)
                    add_summary = bool(self.global_step %
                                       self.FLAGS.display_freq == 0)
                    step_loss, merge = self.model.train(
                        self.sess, train_batch_data, learning_rate,
                        add_summary, self.global_step, epoch)

                    self.sess.graph.finalize()
                    self.model.train_writer.add_summary(
                        merge, self.global_step)
                    avg_loss = avg_loss + step_loss
                    self.global_step = self.global_step + 1
                    self.one_epoch_step = self.one_epoch_step + 1

                    #evaluate for eval steps
                    if self.global_step % self.FLAGS.eval_freq == 0:
                        print(learning_rate)
                        self.logger.info("Epoch step is " +
                                         str(self.one_epoch_step))
                        self.logger.info("Global step is " +
                                         str(self.global_step))
                        self.logger.info("Train_loss is " +
                                         str(avg_loss / self.FLAGS.eval_freq))
                        # train_auc = self.model.metrics(sess=self.sess, batch_data=train_batch_data,
                        #                               global_step=self.global_step,name='train auc')
                        # self.logger.info('Batch Train AUC: %.4f' % train_auc)
                        # self.test_auc = self.model.metrics(sess=self.sess, batch_data=self.test_set,
                        #                               global_step=self.global_step,name='test auc')
                        # self.logger.info('Test AUC: %.4f' % self.test_auc)

                        eval_topk()
                        avg_loss = 0

                        self.save_model()
                        if self.FLAGS.draw_pic == True:
                            self.save_fig()

                    # except Exception as e:
                    #     self.logger.info("Error!!!!!!!!!!!!")
                    #     self.logger.info(e)
                    #     traceback.print_exc()

                self.logger.info('one epoch Cost time: %.2f' %
                                 (time.time() - epoch_start_time))
                self.logger.info("Epoch step is " + str(self.one_epoch_step))
                self.logger.info("Global step is " + str(self.global_step))
                self.logger.info("Train_loss is " + str(step_loss))

                eval_topk()
                with open('best_result_hr_' + self.FLAGS.version, 'w+') as f:
                    f.write(str(self.best_result_hr))
                with open('best_result_ndcg' + self.FLAGS.version, 'w+') as f:
                    f.write(str(self.best_result_ndcg))
                self.logger.info('Max recall rate @ 1: %.4f   ndcg @ 1: %.4f' %
                                 (self.hr_1, self.ndcg_1))
                self.logger.info('Max recall rate @ 5: %.4f   ndcg @ 5: %.4f' %
                                 (self.hr_5, self.ndcg_5))
                self.logger.info(
                    'Max recall rate @ 10: %.4f   ndcg @ 10: %.4f' %
                    (self.hr_10, self.ndcg_10))
                self.logger.info(
                    'Max recall rate @ 20: %.4f   ndcg @ 20: %.4f' %
                    (self.hr_20, self.ndcg_20))
                self.logger.info(
                    'Max recall rate @ 50: %.4f   ndcg @ 50: %.4f' %
                    (self.hr_50, self.ndcg_50))
                if self.best_dev_hr_10 == self.last_dev_hr_10 and self.best_dev_ndcg_10 == self.last_dev_ndcg_10:
                    self.max_stay_count += 1
                else:
                    self.last_dev_hr_10 = self.best_dev_hr_10
                    self.last_dev_ndcg_10 = self.best_dev_ndcg_10
                    self.max_stay_count = 0
                if self.max_stay_count > 5:
                    break

                self.one_epoch_step = 0
                #if self.global_step > 1000:
                #lr = lr / 2
                #if lr < 0.0005:
                #lr = lr * 0.99
                #elif self.FLAGS.type == "tmall":
                #lr = lr * 0.5
                #else:
                #lr = lr * 0.98

                self.logger.info('Epoch %d DONE\tCost time: %.2f' %
                                 (self.now_epoch, time.time() - start_time))

                self.now_epoch = self.now_epoch + 1
                self.one_epoch_step = 0

        # self.model.save(self.sess,self.global_step)
        self.logger.info('best test_auc: ' + str(self.best_auc))
        self.logger.info('best recall: ' + str(self.best_recall))

        self.logger.info('Finished')
示例#29
0
def main():
    tf.disable_eager_execution()
    
    workspace_path = os.environ.get('AE_WORKSPACE_PATH')

    if workspace_path is None:
        print('Please define a workspace path:\n')
        print('export AE_WORKSPACE_PATH=/path/to/workspace\n')
        exit(-1)

    gentle_stop = np.array((1,), dtype=np.bool)
    gentle_stop[0] = False
    def on_ctrl_c(signal, frame):
        gentle_stop[0] = True
    signal.signal(signal.SIGINT, on_ctrl_c)

    parser = argparse.ArgumentParser()
    parser.add_argument("experiment_name")
    parser.add_argument("-d", action='store_true', default=False)
    parser.add_argument("-gen", action='store_true', default=False)
    arguments = parser.parse_args()

    full_name = arguments.experiment_name.split('/')

    experiment_name = full_name.pop()
    experiment_group = full_name.pop() if len(full_name) > 0 else ''

    debug_mode = arguments.d
    generate_data = arguments.gen

    cfg_file_path = u.get_config_file_path(workspace_path, experiment_name, experiment_group)
    log_dir = u.get_log_dir(workspace_path, experiment_name, experiment_group)
    checkpoint_file = u.get_checkpoint_basefilename(log_dir)
    ckpt_dir = u.get_checkpoint_dir(log_dir)
    train_fig_dir = u.get_train_fig_dir(log_dir)
    dataset_path = u.get_dataset_path(workspace_path)

    if not os.path.exists(cfg_file_path):
        print('Could not find config file:\n')
        print('{}\n'.format(cfg_file_path))
        exit(-1)

    if not os.path.exists(ckpt_dir):
        os.makedirs(ckpt_dir)
    if not os.path.exists(train_fig_dir):
        os.makedirs(train_fig_dir)
    if not os.path.exists(dataset_path):
        os.makedirs(dataset_path)

    args = configparser.ConfigParser()
    args.read(cfg_file_path)

    shutil.copy2(cfg_file_path, log_dir)

    with tf.variable_scope(experiment_name):
        dataset = factory.build_dataset(dataset_path, args)
        queue = factory.build_queue(dataset, args)
        encoder = factory.build_encoder(queue.x, args, is_training=True)
        decoder = factory.build_decoder(queue.y, encoder, args, is_training=True)
        ae = factory.build_ae(encoder, decoder, args)
        codebook = factory.build_codebook(encoder, dataset, args)
        train_op = factory.build_train_op(ae, args)
        saver = tf.train.Saver(save_relative_paths=True)

    num_iter = args.getint('Training', 'NUM_ITER') if not debug_mode else 100000
    save_interval = args.getint('Training', 'SAVE_INTERVAL')
    model_type = args.get('Dataset', 'MODEL')

    if model_type=='dsprites':
        dataset.get_sprite_training_images(args)
    else:
        dataset.get_training_images(dataset_path, args)
        dataset.load_bg_images(dataset_path)

    if generate_data:
        print('finished generating synthetic training data for ' + experiment_name)
        print('exiting...')
        exit()

    widgets = ['Training: ', progressbar.Percentage(),
         ' ', progressbar.Bar(),
         ' ', progressbar.Counter(), ' / %s' % num_iter,
         ' ', progressbar.ETA(), ' ']
    bar = progressbar.ProgressBar(maxval=num_iter,widgets=widgets)


    gpu_options = tf.GPUOptions(allow_growth=True, per_process_gpu_memory_fraction=0.8)
    config = tf.ConfigProto(gpu_options=gpu_options)

    with tf.Session(config=config) as sess:

        chkpt = tf.train.get_checkpoint_state(ckpt_dir)
        if chkpt and chkpt.model_checkpoint_path:
            saver.restore(sess, chkpt.model_checkpoint_path)
        else:
            sess.run(tf.global_variables_initializer())

        merged_loss_summary = tf.summary.merge_all()
        summary_writer = tf.summary.FileWriter(ckpt_dir, sess.graph)


        if not debug_mode:
            print('Training with %s model' % args.get('Dataset','MODEL'), os.path.basename(args.get('Paths','MODEL_PATH')))
            bar.start()

        queue.start(sess)
        for i in range(ae.global_step.eval(), num_iter):
            if not debug_mode:
                sess.run(train_op)
                if i % 10 == 0:
                    loss = sess.run(merged_loss_summary)
                    summary_writer.add_summary(loss, i)

                bar.update(i)
                if (i+1) % save_interval == 0:
                    saver.save(sess, checkpoint_file, global_step=ae.global_step)

                    this_x, this_y = sess.run([queue.x, queue.y])
                    reconstr_train = sess.run(decoder.x,feed_dict={queue.x:this_x})
                    train_imgs = np.hstack(( u.tiles(this_x, 4, 4), u.tiles(reconstr_train, 4,4),u.tiles(this_y, 4, 4)))
                    cv2.imwrite(os.path.join(train_fig_dir,'training_images_%s.png' % i), train_imgs*255)
            else:

                this_x, this_y = sess.run([queue.x, queue.y])
                reconstr_train = sess.run(decoder.x,feed_dict={queue.x:this_x})
                cv2.imshow('sample batch', np.hstack(( u.tiles(this_x, 3, 3), u.tiles(reconstr_train, 3,3),u.tiles(this_y, 3, 3))) )
                k = cv2.waitKey(0)
                if k == 27:
                    break

            if gentle_stop[0]:
                break

        queue.stop(sess)
        if not debug_mode:
            bar.finish()
        if not gentle_stop[0] and not debug_mode:
            print('To create the embedding run:\n')
            print('ae_embed {}\n'.format(full_name))
示例#30
0
    def __init__(self,
                 N,
                 rw_len,
                 walk_generator,
                 generator_layers=[40],
                 discriminator_layers=[30],
                 W_down_generator_size=128,
                 W_down_discriminator_size=128,
                 batch_size=128,
                 noise_dim=16,
                 noise_type="Gaussian",
                 learning_rate=0.0003,
                 disc_iters=3,
                 wasserstein_penalty=10,
                 l2_penalty_generator=1e-7,
                 l2_penalty_discriminator=5e-5,
                 temp_start=5.0,
                 min_temperature=0.5,
                 temperature_decay=1 - 5e-5,
                 seed=15,
                 gpu_id=0,
                 use_gumbel=True,
                 legacy_generator=False):
        """
        Initialize NetGAN.

        Parameters
        ----------
        N: int
           Number of nodes in the graph to generate.
        rw_len: int
                Length of random walks to generate.
        walk_generator: function
                        Function that generates a single random walk and takes no arguments.
        generator_layers: list of integers, default: [40], i.e. a single layer with 40 units.
                          The layer sizes of the generator LSTM layers
        discriminator_layers: list of integers, default: [30], i.e. a single layer with 30 units.
                              The sizes of the discriminator LSTM layers
        W_down_generator_size: int, default: 128
                               The size of the weight matrix W_down of the generator. See our paper for details.
        W_down_discriminator_size: int, default: 128
                                   The size of the weight matrix W_down of the discriminator. See our paper for details.
        batch_size: int, default: 128
                    The batch size.
        noise_dim: int, default: 16
                   The dimension of the random noise that is used as input to the generator.
        noise_type: str in ["Gaussian", "Uniform], default: "Gaussian"
                    The noise type to feed into the generator.
        learning_rate: float, default: 0.0003
                       The learning rate.
        disc_iters: int, default: 3
                    The number of discriminator iterations per generator training iteration.
        wasserstein_penalty: float, default: 10
                             The Wasserstein gradient penalty applied to the discriminator. See the Wasserstein GAN
                             paper for details.
        l2_penalty_generator: float, default: 1e-7
                                L2 penalty on the generator weights.
        l2_penalty_discriminator: float, default: 5e-5
                                    L2 penalty on the discriminator weights.
        temp_start: float, default: 5.0
                    The initial temperature for the Gumbel softmax.
        min_temperature: float, default: 0.5
                         The minimal temperature for the Gumbel softmax.
        temperature_decay: float, default: 1-5e-5
                           After each evaluation, the current temperature is updated as
                           current_temp := max(temperature_decay*current_temp, min_temperature)
        seed: int, default: 15
              Random seed.
        gpu_id: int or None, default: 0
                The ID of the GPU to be used for training. If None, CPU only.
        use_gumbel: bool, default: True
                Use the Gumbel softmax trick.
        
        legacy_generator: bool, default: False
            If True, the hidden and cell states of the generator LSTM are initialized by two separate feed-forward networks. 
            If False (recommended), the hidden layer is shared, which has less parameters and performs just as good.
        
        """

        self.params = {
            'noise_dim': noise_dim,
            'noise_type': noise_type,
            'Generator_Layers': generator_layers,
            'Discriminator_Layers': discriminator_layers,
            'W_Down_Generator_size': W_down_generator_size,
            'W_Down_Discriminator_size': W_down_discriminator_size,
            'l2_penalty_generator': l2_penalty_generator,
            'l2_penalty_discriminator': l2_penalty_discriminator,
            'learning_rate': learning_rate,
            'batch_size': batch_size,
            'Wasserstein_penalty': wasserstein_penalty,
            'temp_start': temp_start,
            'min_temperature': min_temperature,
            'temperature_decay': temperature_decay,
            'disc_iters': disc_iters,
            'use_gumbel': use_gumbel,
            'legacy_generator': legacy_generator
        }

        assert rw_len > 1, "Random walk length must be > 1."

        tf.set_random_seed(seed)
        #tf.random.set_seed(seed)

        self.N = N
        self.rw_len = rw_len

        self.noise_dim = self.params['noise_dim']
        self.G_layers = self.params['Generator_Layers']
        self.D_layers = self.params['Discriminator_Layers']
        self.tau = tf.placeholder(1.0, shape=(), name="temperature")

        # W_down and W_up for generator and discriminator
        self.W_down_generator = tf.get_variable(
            'Generator.W_Down',
            shape=[self.N, self.params['W_Down_Generator_size']],
            dtype=tf.float32,
            initializer=tf.glorot_uniform_initializer())

        self.W_down_discriminator = tf.get_variable(
            'Discriminator.W_Down',
            shape=[self.N, self.params['W_Down_Discriminator_size']],
            dtype=tf.float32,
            initializer=tf.glorot_uniform_initializer())

        self.W_up = tf.get_variable(
            "Generator.W_up",
            shape=[self.G_layers[-1], self.N],
            dtype=tf.float32,
            initializer=tf.glorot_uniform_initializer())

        self.b_W_up = tf.get_variable("Generator.W_up_bias",
                                      dtype=tf.float32,
                                      initializer=tf.zeros_initializer,
                                      shape=self.N)

        self.generator_function = self.generator_recurrent
        self.discriminator_function = self.discriminator_recurrent

        self.fake_inputs = self.generator_function(self.params['batch_size'],
                                                   reuse=False,
                                                   gumbel=use_gumbel,
                                                   legacy=legacy_generator)
        self.fake_inputs_discrete = self.generate_discrete(
            self.params['batch_size'],
            reuse=True,
            gumbel=use_gumbel,
            legacy=legacy_generator)

        # Pre-fetch real random walks
        dataset = tf.data.Dataset.from_generator(
            walk_generator, tf.int32, [self.params['batch_size'], self.rw_len])
        #dataset_batch = dataset.prefetch(2).batch(self.params['batch_size'])
        dataset_batch = dataset.prefetch(100)
        batch_iterator = dataset_batch.make_one_shot_iterator()
        real_data = batch_iterator.get_next()

        self.real_inputs_discrete = real_data
        self.real_inputs = tf.one_hot(self.real_inputs_discrete, self.N)

        self.disc_real = self.discriminator_function(self.real_inputs)
        self.disc_fake = self.discriminator_function(self.fake_inputs,
                                                     reuse=True)

        self.disc_cost = tf.reduce_mean(self.disc_fake) - tf.reduce_mean(
            self.disc_real)
        self.gen_cost = -tf.reduce_mean(self.disc_fake)

        # WGAN lipschitz-penalty
        alpha = tf.random_uniform(shape=[self.params['batch_size'], 1, 1],
                                  minval=0.,
                                  maxval=1.)

        self.differences = self.fake_inputs - self.real_inputs
        self.interpolates = self.real_inputs + (alpha * self.differences)
        self.gradients = tf.gradients(
            self.discriminator_function(self.interpolates, reuse=True),
            self.interpolates)[0]
        self.slopes = tf.sqrt(
            tf.reduce_sum(tf.square(self.gradients), reduction_indices=[1, 2]))
        self.gradient_penalty = tf.reduce_mean((self.slopes - 1.)**2)
        self.disc_cost += self.params[
            'Wasserstein_penalty'] * self.gradient_penalty

        # weight regularization; we omit W_down from regularization
        self.disc_l2_loss = tf.add_n([
            tf.nn.l2_loss(v) for v in tf.trainable_variables()
            if 'Disc' in v.name and not 'W_down' in v.name
        ]) * self.params['l2_penalty_discriminator']
        self.disc_cost += self.disc_l2_loss

        # weight regularization; we omit  W_down from regularization
        self.gen_l2_loss = tf.add_n([
            tf.nn.l2_loss(v) for v in tf.trainable_variables()
            if 'Gen' in v.name and not 'W_down' in v.name
        ]) * self.params['l2_penalty_generator']
        self.gen_cost += self.gen_l2_loss

        self.gen_params = [
            v for v in tf.trainable_variables() if 'Generator' in v.name
        ]
        self.disc_params = [
            v for v in tf.trainable_variables() if 'Discriminator' in v.name
        ]

        self.gen_train_op = tf.train.AdamOptimizer(
            learning_rate=self.params['learning_rate'], beta1=0.5,
            beta2=0.9).minimize(self.gen_cost, var_list=self.gen_params)
        self.disc_train_op = tf.train.AdamOptimizer(
            learning_rate=self.params['learning_rate'], beta1=0.5,
            beta2=0.9).minimize(self.disc_cost, var_list=self.disc_params)

        if gpu_id is None:
            config = tf.ConfigProto(device_count={'GPU': 0})
        else:
            gpu_options = tf.GPUOptions(
                visible_device_list='{}'.format(gpu_id), allow_growth=True)
            config = tf.ConfigProto(gpu_options=gpu_options)

        self.session = tf.InteractiveSession(config=config)
        self.init_op = tf.global_variables_initializer()