def apply_inception(x, in_d, out_d):
    """ This function implements the one inception layer with reduced dimensionality """
    d_1x1 = 32
    conv1x1 = conv_layer(x, 1, in_d, out_d, True)
    conv2 = conv_layer(x, 1, in_d, d_1x1, True)
    conv3 = conv_layer(x, 1, in_d, d_1x1, True)
    maxpool = maxpool_layer(x, 3)
    conv_maxpool = conv_layer(maxpool, 1, in_d, out_d, False)
    conv3x3 = conv_layer(conv2, 3, d_1x1, int(out_d // 2), False)
    conv3x3 = conv_layer(conv3x3, 1, int(out_d // 2), out_d, False)
    conv5x5 = conv_layer(conv3, 5, d_1x1, int(out_d // 2), False)
    conv5x5 = conv_layer(conv5x5, 1, int(out_d // 2), out_d, False)
    # return tf.nn.leaky_relu(tf.concat([conv1x1, conv3x3, conv5x5, conv_maxpool], 3), name=name)
    return tf.nn.leaky_relu(
        tf.concat([conv1x1, conv3x3, conv5x5, conv_maxpool], 3))


if __name__ == '__main__':
    fg_bg = cv2.createBackgroundSubtractorMOG2()
    IMAGE_SIZE = (12, 8)

    # path_gen = os_utils.iterate_data(cs.BASE_DATA_PATH + cs.DATA_TRAIN_VIDEOS, ".mp4")
    #
    # for path in path_gen:
    #     write_videos(path, cs.DATA_TRAIN_VIDEOS, cs.DATA_BG_TRAIN_VIDEO)

    path_gen = os_utils.iterate_test_data(
        cs.BASE_DATA_PATH + cs.DATA_TEST_VIDEOS, ".mp4")
    for path in path_gen:
        write_videos(path, cs.DATA_TEST_VIDEOS, cs.DATA_BG_TEST_VIDEO)
def test():
    encoder_logs_path = cs.BASE_LOG_PATH + cs.MODEL_CONV_AE_1
    bi_lstm_logs_path = cs.BASE_LOG_PATH + cs.MODEL_BI_LSTM
    path_generator = os_utils.iterate_test_data(cs.BASE_DATA_PATH + cs.DATA_BG_TEST_VIDEO, "mp4")

    graph = tf.Graph()
    accuracy_1 = 0
    accuracy_3 = 0
    accuracy_5 = 0

    with graph.as_default():
        rnn = Bi_LSTM(lstm_size=128, batch_len=BATCH_SIZE, output_nodes=14, keep_prob=0.0, learning_rate=0.001)
        rnn.build_model()
        stage_1_ip, stage_2_ip = get_encoded_embeddings(encoder_logs_path)
        prediction = tf.nn.softmax(rnn.predictions)
        saver = tf.train.Saver()

    label_encoder, num_classes = get_label_enocder(path_generator)
    path_generator = os_utils.iterate_test_data(cs.BASE_DATA_PATH + cs.DATA_BG_TEST_VIDEO, "mp4")

    with tf.Session(graph=graph) as sess:
        saver.restore(sess, tf.train.latest_checkpoint(bi_lstm_logs_path))
        state_fw = sess.run(rnn.initial_state_fw)
        state_bw = sess.run(rnn.initial_state_bw)
        loop_count = 0
        for video_path in path_generator:
            # print(video_path)
            batch_x = get_batch(video_path, True)
            batch_y = get_target_name(video_path)

            encoded_batch = sess.run(stage_2_ip, feed_dict={stage_1_ip: batch_x})
            encoded_batch = encoded_batch.reshape((1, encoded_batch.shape[0], encoded_batch.shape[1]))

            feed = {rnn.inputs_: encoded_batch,
                    rnn.targets_: label_encoder.transform([batch_y]),
                    rnn.keep_prob: 0.5,
                    rnn.initial_state_fw: state_fw,
                    rnn.initial_state_bw: state_bw}

            probabilities_1, probabilities_3, probabilities_5 = sess.run([tf.nn.top_k(prediction, k=1),
                                                                          tf.nn.top_k(prediction, k=3),
                                                                          tf.nn.top_k(prediction, k=5)],
                                                                         feed_dict=feed)

            print(probabilities_1[1][0])
            print(probabilities_3[1][0])
            print(probabilities_5[1][0])
            print(batch_y - 1)

            if batch_y - 1 in probabilities_1[1][0]:
                accuracy_1 += 1
                print("accuracy_1 =", accuracy_1)

            if batch_y - 1 in probabilities_3[1][0]:
                accuracy_3 += 1
                print("accuracy_3 =", accuracy_3)

            if batch_y - 1 in probabilities_5[1][0]:
                accuracy_5 += 1
                print("accuracy_5 =", accuracy_5)
            loop_count += 1

            print("==============================", loop_count, "=================================")

    print(accuracy_1, 100 * accuracy_1 / 280)
    print(accuracy_3, 100 * accuracy_3 / 280)
    print(accuracy_5, 100 * accuracy_5 / 280)
Exemple #3
0
def train():
    """ This function builds the graph and performs the training """

    epochs = 150  # epochs: Number of iterations for which training will be performed
    loading = False  # loading : flag for loading an already trained model
    logs_path = cs.BASE_LOG_PATH + cs.MODEL_CONV_AE_1  # logs_path : path to store checkpoint and summary events
    tf.reset_default_graph()
    cae = ConVAE()
    cae.build_model()
    merged_summary_op = write_summaries(cae)
    sess = tf.Session()
    saver = tf.train.Saver(max_to_keep=10)

    # =======================================================================
    # If loading flag is true then load the latest model form the logs_path
    # =======================================================================
    if loading:
        sess.run(tf.global_variables_initializer())
        saver.restore(sess, tf.train.latest_checkpoint(logs_path))
        latest_checkpoint_path = tf.train.latest_checkpoint(logs_path)
        checkpoint_number = latest_checkpoint_path.split(".")[0]
        checkpoint_number = int(checkpoint_number.split("_")[-1])
        print("loading checkpoint_number =", checkpoint_number)

    else:
        sess.run(tf.global_variables_initializer())
        checkpoint_number = 0

    summary_writer = tf.summary.FileWriter(logs_path, graph=sess.graph)
    summary_writer.add_graph(sess.graph)

    loop_counter = 1

    for e in tqdm(range(checkpoint_number, checkpoint_number + epochs)):
        print()
        path_generator = os_utils.iterate_data(
            cs.BASE_DATA_PATH + cs.DATA_BG_TRAIN_VIDEO, "mp4")
        batch_counter = 0
        start_time = time.time()

        for video_path in path_generator:
            # ======================================
            # get batches to feed into the network
            # ======================================
            batch_x = get_batch(video_path)

            if batch_x is None:
                continue

            else:
                print("video_path", video_path)
                print("video number =", batch_counter, "..... batch_x.shape",
                      batch_x.shape, " loop_counter =",
                      checkpoint_number + loop_counter)

                batch_loss, _, summary = sess.run(
                    [cae.loss, cae.opt, merged_summary_op],
                    feed_dict={
                        cae.inputs_: batch_x,
                        cae.targets_: batch_x.copy()
                    })

            # ==============================
            # Write logs at every iteration
            # ==============================
            summary_writer.add_summary(summary,
                                       checkpoint_number + loop_counter)

            print("Epoch: {}/{}...".format(e + 1 - checkpoint_number, epochs),
                  "Training loss: {:.4f}".format(batch_loss))

            # if batch_counter % 2 == 0:
            #     print("saving the model at epoch", checkpoint_number + loop_counter)
            #     saver.save(sess, os.path.join(logs_path, 'encoder_epoch_number_{}.ckpt'
            #                                   .format(checkpoint_number + loop_counter)))

            batch_counter += 1
            loop_counter += 1
            if batch_counter == 420:
                end_time = time.time()
                print(
                    "=============================================================================================="
                )
                print("Epoch Number", e, "has ended in", end_time - start_time,
                      "seconds for", batch_counter, "videos")
                print(
                    "=============================================================================================="
                )

                # break

        if e % 10 == 0:
            print("################################################")
            print("saving the model at epoch",
                  checkpoint_number + loop_counter)
            print("################################################")

            saver.save(
                sess,
                os.path.join(
                    logs_path,
                    'encoder_epoch_number_{}.ckpt'.format(checkpoint_number +
                                                          loop_counter)))
    # =========================
    # Freeze the session graph
    # =========================
    cae.process_node_names()
    utility.freeze_model(sess, logs_path,
                         tf.train.latest_checkpoint(logs_path), cae,
                         "encoder_train.pb", cs.ENCODER1_FREEZED_PB_NAME)

    print(
        "Run the command line:\n--> tensorboard --logdir={}".format(logs_path),
        "\nThen open http://0.0.0.0:6006/ into your web browser")

    path_generator = os_utils.iterate_test_data(
        cs.BASE_DATA_PATH + cs.DATA_BG_TRAIN_VIDEO, "mp4")

    # ==============================================================
    # Now testing the performance of our model on an unknown data
    # ==============================================================
    for video_path in path_generator:
        test_frame = get_batch(video_path)

        if test_frame is not None:

            test_frame = test_frame[20:40, :, :, :]
            reconstructed = sess.run(cae.decoded,
                                     feed_dict={cae.inputs_: test_frame})
            display_reconstruction_results(test_frame, reconstructed)

            break

    sess.close()