Не хватает ОЗУ в ноутбуке Jupyter или ядре Kaggle при загрузке пакетов данных изображений - PullRequest
0 голосов
/ 06 апреля 2020

Когда я звоню print_test_accuracy(), у меня всегда заканчивается ОЗУ, и мой jupyter перестает отвечать на запросы, а ядро ​​kaggle перезапускается. Я уже пытался загружать свои изображения партиями с помощью функции ниже, но все же столкнулся с этой проблемой. Нужно ли каким-то образом выгружать пакеты данных из ОЗУ после их передачи в tf.Session().run()?

test_batch_size = 64
def load_image_batch(image_ids):
    imgs = []
    for image_id in image_ids:
        file_path = image_id + ".jpg"
        img = cv2.imread(IMG_PATH + file_path)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        imgs.append(img)
    image_batch = np.ndarray(shape=(len(imgs), img_height, img_width, 3),dtype = np.float32)
    for i in range(len(imgs)):
        image_batch[i] = imgs[i]

    return image_batch

def print_test_accuracy():

    # Number of images in the test-set.
    num_test = x_test.shape[0]

    # Allocate an array for the predicted classes which
    # will be calculated in batches and filled into this array.
    cls_pred = np.zeros(shape=num_test, dtype=np.int)

    # Now calculate the predicted classes for the batches.
    # We will just iterate through all the batches.
    # There might be a more clever and Pythonic way of doing this.

    # The starting index for the next batch is denoted i.
    i = 0

    while i < num_test:
        # The ending index for the next batch is denoted j.
        j = min(i + test_batch_size, num_test)

        # Get the images from the test-set between index i and j.
        image_ids = df_test['image_id'][i:j]
        images = load_image_batch(image_ids)

        # Get the associated labels.
        labels = y_test[i:j]

        # Create a feed-dict with these images and labels.
        feed_dict = {x: images, y_true: labels}

        # Calculate the predicted class using TensorFlow.
        cls_pred[i:j] = session.run(y_pred_cls, feed_dict=feed_dict)

        # Set the start-index for the next batch to the
        # end-index of the current batch.
        i = j

    # Convenience variable for the true class-numbers of the test-set.
    cls_true = y_test_cls 

    # Create a boolean array whether each image is correctly classified.
    correct = (cls_true == cls_pred)

    # Calculate the number of correctly classified images.
    # When summing a boolean array, False means 0 and True means 1.
    # correct_sum = correct.sum()
    correct_sum = sum(1 for boo in correct if boo == True)

    # Classification accuracy is the number of correctly classified
    # images divided by the total number of images in the test-set.
    acc = float(correct_sum) / num_test

    # Print the accuracy.
    msg = "Accuracy on Test-Set: {0:.1%} ({1} / {2})"
    print(msg.format(acc, correct_sum, num_test))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...