Ejemplo n.º 1
0
def main():
    if len(sys.argv) < 5:
        print('Please pass <TensorFlow record path> <Number of classes> <"cpu"/"gpu"> <batch_size>')
        exit(0)
    imagePath = sys.argv[1]
    numClasses = int(sys.argv[2])
    if(sys.argv[3] == "cpu"):
        raliCPU = True
    else:
        raliCPU = False
    bs = int(sys.argv[4])
    nt = 1
    di = 0
    cropSize = 320
    TFRecordReaderType = 1
    featureKeyMap = {
        'image/encoded': 'image/encoded',
        'image/class/label': 'image/object/class/label',
        'image/class/text': 'image/object/class/text',
        'image/object/bbox/xmin': 'image/object/bbox/xmin',
        'image/object/bbox/ymin': 'image/object/bbox/ymin',
        'image/object/bbox/xmax': 'image/object/bbox/xmax',
        'image/object/bbox/ymax': 'image/object/bbox/ymax',
        'image/filename': 'image/filename'
    }

    pipe = HybridPipe(feature_key_map=featureKeyMap, tfrecordreader_type=TFRecordReaderType,
                      batch_size=bs, num_threads=nt, device_id=di, data_dir=imagePath, crop=cropSize, rali_cpu=raliCPU)
    pipe.build()

    imageIterator = RALIIterator(pipe)

    for i, (images_array, bboxes_array, labels_array, num_bboxes_array) in enumerate(imageIterator, 0):
        images_array = np.transpose(images_array, [0, 2, 3, 1])
        print("RALI augmentation pipeline - Processing batch %d....." % i)

        for element in list(range(bs)):
            print("Processing image %d....." % element)
            features_dict = {
                "image": images_array[element],
                "true_image_shape": np.array([len(images_array[element]), len(images_array[element, 0]), len(images_array[element, 0, 0])])
            }
            labels_dict = {
                "num_groundtruth_boxes": num_bboxes_array[element],
                "groundtruth_boxes": bboxes_array[element],
                "groundtruth_classes": get_onehot(labels_array[element], numClasses),
                "groundtruth_weights": get_weights(num_bboxes_array[element])
            }
            processed_tensors = (features_dict, labels_dict)
            print("\nPROCESSED_TENSORS:\n", processed_tensors)
        print("\n\nPrinted first batch with", (bs), "images!")
        break
    imageIterator.reset()
def main():
    if len(sys.argv) < 4:
        print('Please pass the TensorFlowrecord  cpu/gpu batch_size')
        exit(0)
    imagePath = sys.argv[1]
    if (sys.argv[2] == "cpu"):
        raliCPU = True
    else:
        raliCPU = False
    bs = int(sys.argv[3])
    nt = 1
    di = 0
    cropSize = 224
    TFRecordReaderType = 0
    featureKeyMap = {
        'image/encoded': 'image/encoded',
        'image/class/label': 'image/class/label',
        'image/filename': 'image/filename'
    }

    pipe = HybridPipe(feature_key_map=featureKeyMap,
                      tfrecordreader_type=TFRecordReaderType,
                      batch_size=bs,
                      num_threads=nt,
                      device_id=di,
                      data_dir=imagePath,
                      crop=cropSize,
                      rali_cpu=raliCPU)
    pipe.build()

    imageIterator = RALIIterator(pipe)
    for i, (images_array, labels_array) in enumerate(imageIterator, 0):
        print("\nIMAGES ARRAY:\n", images_array)
        print("\nLABELS ARRAY:\n", labels_array)
Ejemplo n.º 3
0
def main():

    global NUM_CLASSES
    global LEARNING_RATE
    global NUM_TRAIN_STEPS
    global TRAIN_BATCH_SIZE
    global EVAL_EVERY
    global DATASET_DOWNLOAD_AND_PREPROCESS
    global TRAIN_RECORDS_DIR
    global VAL_RECORDS_DIR

    if DATASET_DOWNLOAD_AND_PREPROCESS == True:
        download_images()

    print(
        "\n-----------------------------------------------------------------------------------------"
    )
    print('TF records (train) are located in %s' % TRAIN_RECORDS_DIR)
    print('TF records (val) are located in %s' % VAL_RECORDS_DIR)
    print(
        "-----------------------------------------------------------------------------------------\n"
    )

    train_graph = tf.Graph()
    with train_graph.as_default():
        image_module = hub.Module(
            'https://tfhub.dev/google/imagenet/mobilenet_v2_035_128/feature_vector/2'
        )
        image_size = hub.get_expected_image_size(image_module)
        decoded_images = tf.placeholder(tf.float32,
                                        shape=[None, None, None, None])
        features = image_module(decoded_images)
        logits = create_model(features)
        labels = tf.placeholder(tf.float32, [None, NUM_CLASSES])
        cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(
            logits=logits, labels=labels)
        cross_entropy_mean = tf.reduce_mean(cross_entropy)
        optimizer = tf.train.GradientDescentOptimizer(
            learning_rate=LEARNING_RATE)
        train_op = optimizer.minimize(loss=cross_entropy_mean)
        probabilities = tf.nn.softmax(logits)
        prediction = tf.argmax(probabilities, 1)
        correct_label = tf.argmax(labels, 1)
        correct_prediction = tf.equal(prediction, tf.argmax(labels, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    crop_size = tuple(image_size)
    TFRecordReaderType = 0
    featureKeyMap = {
        'image/encoded': 'image/encoded',
        'image/class/label': 'image/object/class/label',
        'image/filename': 'image/filename'
    }

    trainPipe = HybridPipe(feature_key_map=featureKeyMap,
                           tfrecordreader_type=TFRecordReaderType,
                           batch_size=TRAIN_BATCH_SIZE,
                           num_threads=1,
                           device_id=0,
                           data_dir=TRAIN_RECORDS_DIR,
                           crop=crop_size,
                           rali_cpu=RUN_ON_HOST)
    valPipe = HybridPipe(feature_key_map=featureKeyMap,
                         tfrecordreader_type=TFRecordReaderType,
                         batch_size=TRAIN_BATCH_SIZE,
                         num_threads=1,
                         device_id=0,
                         data_dir=VAL_RECORDS_DIR,
                         crop=crop_size,
                         rali_cpu=RUN_ON_HOST)
    trainPipe.build()
    valPipe.build()
    trainIterator = RALIIterator(trainPipe)
    valIterator = RALIIterator(valPipe)

    i = 0
    with tf.Session(graph=train_graph) as sess:
        sess.run(tf.global_variables_initializer())
        while i < NUM_TRAIN_STEPS:

            for t, (train_image_ndArray,
                    train_label_ndArray) in enumerate(trainIterator, 0):
                train_image_ndArray_transposed = np.transpose(
                    train_image_ndArray, [0, 2, 3, 1])
                train_label_one_hot_list = get_label_one_hot(
                    train_label_ndArray)
                train_loss, _, train_accuracy = sess.run(
                    [cross_entropy_mean, train_op, accuracy],
                    feed_dict={
                        decoded_images: train_image_ndArray_transposed,
                        labels: train_label_one_hot_list
                    })
                print(
                    "Step :: %s\tTrain Loss :: %.2f\tTrain Accuracy :: %.2f%%\t"
                    % (i, train_loss, (train_accuracy * 100)))
                is_final_step = (i == (NUM_TRAIN_STEPS - 1))
                if i % EVAL_EVERY == 0 or is_final_step:
                    mean_acc = 0
                    mean_loss = 0
                    print(
                        "\n\n-------------------------------------------------------------------------------- BEGIN VALIDATION --------------------------------------------------------------------------------"
                    )
                    for j, (val_image_ndArray,
                            val_label_ndArray) in enumerate(valIterator, 0):
                        val_image_ndArray_transposed = np.transpose(
                            val_image_ndArray, [0, 2, 3, 1])
                        val_label_one_hot_list = get_label_one_hot(
                            val_label_ndArray)
                        val_loss, val_accuracy, val_prediction, val_target, correct_predicate = sess.run(
                            [
                                cross_entropy_mean, accuracy, prediction,
                                correct_label, correct_prediction
                            ],
                            feed_dict={
                                decoded_images: val_image_ndArray_transposed,
                                labels: val_label_one_hot_list
                            })
                        mean_acc += val_accuracy
                        mean_loss += val_loss
                        num_correct_predicate = 0
                        for predicate in correct_predicate:
                            if predicate == True:
                                num_correct_predicate += 1
                        print(
                            "Step :: %s\tTarget :: %s\tPrediction :: %s\tCorrect Predictions :: %s/%s\tValidation Loss :: %.2f\tValidation Accuracy :: %.2f%%\t"
                            % (j, val_target, val_prediction,
                               num_correct_predicate, len(correct_predicate),
                               val_loss, (val_accuracy * 100)))
                    mean_acc = (mean_acc * 100) / j
                    print(
                        "\nSUMMARY:\nMean Loss :: %.2f\tMean Accuracy :: %.2f%%"
                        % (mean_loss, mean_acc))
                    print(
                        "\n-------------------------------------------------------------------------------- END VALIDATION --------------------------------------------------------------------------------\n\n"
                    )

                i = i + 1
                if i >= NUM_TRAIN_STEPS:
                    break
Ejemplo n.º 4
0
def train_mnist_rali(data_path, _rali_cpu, batch_size):

    # setup keep_prob
    input_X = tf.placeholder('float32', shape=(batch_size, 784))
    labels = tf.placeholder('float32', shape=(batch_size, 10))
    logits = neural_net(input_X)
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
        labels=labels, logits=logits),
                          name="loss")
    optimizer = tf.train.AdamOptimizer().minimize(cost)
    train_prediction = tf.nn.softmax(logits)
    accuracy = compute_accuracy(train_prediction, labels)
    #correct_label = tf.argmax(labels, 1)
    num_epochs = 10
    crop_size = 28
    TFRecordReaderType = 0
    featureKeyMap = {
        'image/encoded': 'image_raw',
        'image/class/label': 'label',
        'image/filename': ''
    }

    trainPipe = HybridPipe(feature_key_map=featureKeyMap,
                           tfrecordreader_type=TFRecordReaderType,
                           batch_size=batch_size,
                           num_threads=1,
                           device_id=0,
                           data_dir=data_path + "/train",
                           crop=crop_size,
                           rali_cpu=_rali_cpu)
    valPipe = HybridPipe(feature_key_map=featureKeyMap,
                         tfrecordreader_type=TFRecordReaderType,
                         batch_size=batch_size,
                         num_threads=1,
                         device_id=0,
                         data_dir=data_path + "/val",
                         crop=crop_size,
                         rali_cpu=_rali_cpu)
    trainPipe.build()
    valPipe.build()
    trainIterator = RALIIterator(trainPipe)
    valIterator = RALIIterator(valPipe)

    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())
        for epoch in range(num_epochs):
            print('\n\n----------------------------Training Model for Epoch: ',
                  epoch, "-----------------------------------------------")
            epoch_loss = 0
            train_accuracy = 0
            for i, (image_train, label_train) in enumerate(trainIterator, 0):
                image_train_res = image_train.reshape(batch_size, 784)
                train_label_one_hot_list = get_label_one_hot(label_train)
                _, c, tacc = sess.run([optimizer, cost, accuracy],
                                      feed_dict={
                                          input_X: image_train_res,
                                          labels: train_label_one_hot_list
                                      })
                epoch_loss += c
                train_accuracy += tacc

            print('Epoch', epoch, 'completed out of', num_epochs, 'loss:',
                  epoch_loss, 'accuracy:', (train_accuracy * 100) / i,
                  'count :', i)
            #run evaluation for every epoch
            mean_acc = 0
            print(
                "\n\n----------------------------Evaluating Model ---------------------------------------------------------------"
            )
            for j, (val_image_ndArray,
                    val_label_ndArray) in enumerate(valIterator, 0):
                #val_image_ndArray_transposed = np.transpose(val_image_ndArray, [0, 2, 3, 1])
                val_image_ndArray_res = val_image_ndArray.reshape(
                    batch_size, 784)
                val_label_one_hot_list = get_label_one_hot(val_label_ndArray)
                val_accuracy = sess.run(
                    accuracy,
                    #[optimizer, accuracy, prediction, correct_label, correct_pred],
                    feed_dict={
                        input_X: val_image_ndArray_res,
                        labels: val_label_one_hot_list
                    })
                mean_acc += val_accuracy
                #mean_loss = mean_loss + val_loss
                #num_correct_predicate = 0
                #for predicate in correct_predicate:
                #	if predicate == True:
                #		num_correct_predicate += 1
                #print ("Step :: %s\tTarget :: %s\tPrediction :: %s\tCorrect Predictions :: %s/%s\tValidation Loss :: %.2f\tValidation Accuracy :: %.2f%%\t" % (j, val_target, val_prediction, num_correct_predicate, len(correct_predicate), val_loss, (val_accuracy * 100)))
            mean_acc = (mean_acc * 100) / j
            #mean_loss = (mean_loss * 100)/ j
            print("\nSUMMARY:\nMean Accuracy :: %.2f%% count: %d" %
                  (mean_acc, j))