Почему этот встроенный l oop не работает в python? - PullRequest
0 голосов
/ 11 февраля 2020

Я пытаюсь разделить свой набор данных на обучение и тестирование в соотношении 80:20. У моего продукта есть разные оценки, и в каждой папке оценок есть куча файлов. В конечном результате должно быть две папки Training и Testing , каждая из которых имеет все оценки в виде подпапок и пропорциональное количество файлов.

Я хотел бы понять, почему последняя пока l oop не работает.

# Split Data set into testing and training

train_ratio = 80
test_ratio = 20

# For each grade , determine no of training and testing images.
for grade_type in GRADES:
    grade_folder_path = os.path.join(prep_folder, grade_type)
    img_count_grade_folder = len(os.listdir(grade_folder_path))
    train_count: int = round(img_count_grade_folder * train_ratio / 100)
    test_count : int = img_count_grade_folder - train_count
    print(grade_type, "Total images :", img_count_grade_folder, "Train Images: ", train_count, "Test images: ",
          test_count)

    # Create Train and Test folders. 

    train_grade_type = os.path.join(prep_folder, "Train", "", grade_type, "")
    os.makedirs(train_grade_type, exist_ok=True)
    test_grade_type = os.path.join(prep_folder, "Test", "", grade_type, "")
    os.makedirs(test_grade_type, exist_ok=True)

    # Copy Data to Train and Test folders. 
    b : int = 0
    while b < train_count:
        print(b)
        images = os.listdir(grade_folder_path)
        src_img = images[b]
        src_img_path = os.path.join(grade_folder_path, src_img)
        train_target = os.path.join(train_grade_type)
        train_target_img = os.path.join(train_target, src_img)
        print("train_", src_img_path, train_target)
        # b = b+1
        if os.path.isfile(train_target_img):
            print("skipping", src_img)
        else:
            try:
                shutil.copy(src_img_path, train_target)

            except:
                print("error copying to train target", train_target)
            else:
                print(src_img, "Copy successful !")

        b = b + 1
        print("next", b)
        if b >= train_count:
            continue

    c : int = train_count
    while c < test_count:
        print(c)
        images = os.listdir(grade_folder_path)
        src_img = images[c]
        src_img_path = os.path.join(grade_folder_path, src_img)
        test_target = os.path.join(test_grade_type)
        test_target_img = os.path.join(test_target, src_img)
        print("Test_", src_img_path, test_target)
        # c = c+1
        if os.path.isfile(test_target_img):
            print("skipping", src_img)
        else:
            try:
                shutil.copy(src_img_path, test_target)

            except:
                print("error copying to test target", test_target)
            else:
                print(src_img, "Copy successful !")
        c = c + 1
        print("next", c)
        if c == test_count:
            break

1 Ответ

0 голосов
/ 11 февраля 2020

Поскольку train_count больше test_count, а начальное значение c равно: c : int = train_count, тогда оно не удовлетворяет условию while l oop. Вот почему пока l oop не работает.

...