简体   繁体   中英

Why does this inline loop not work in python?

I am trying to split my dataset into Training and testing in the ratio 80:20. There are different grades of my product and each grade folder has a bunch of files. The final result should have two folders Training and Testing each having all the grades as subfolders and the proportionate amount of files.

I would like to understand why the last while loop is not working.

# 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

Because train_count is bigger than test_count , and initial value of c is: c : int = train_count then it not satisfy the while loop condition. That is why the while loop not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM