dataset_loader.py 文件源码

python
阅读 34 收藏 0 点赞 0 评论 0

项目:R-CNN_LIGHT 作者: YeongHyeon 项目源码 文件源码
def imagelist_to_dataset(image_dir, image_lists, imsize=28):
    master_key, sub_key = key_from_dictionary(image_lists)

    print("\n***** Make image list *****")
    result_dir = "dataset/"
    if not os.path.exists(result_dir):
        os.makedirs(result_dir)
    else:
        shutil.rmtree(result_dir)
        os.makedirs(result_dir)

    x_train = []
    t_train = np.empty((0), int)
    x_test = []
    t_test = np.empty((0), int)
    x_valid = []
    t_valid = np.empty((0), int)
    for key_i in [0, 1, 3]:
        if key_i == 0:
            result_name = "train"
        elif key_i == 1:
            result_name = "test"
        else:
            result_name = "valid"
        sys.stdout.write(" Make \'"+result_name+" list\'...")
        # m: class
        for m in master_key:

                for i in range(len(image_lists[m][sub_key[key_i]])):
                    # m: category
                    # image_lists[m][sub_key[key_i]][i]: image name
                    image_path = "./"+image_dir+"/"+m+"/"+image_lists[m][sub_key[key_i]][i]
                    # Read jpg images and resizing it.
                    origin_image = cv2.imread(image_path)
                    resized_image = cv2.resize(origin_image, (imsize, imsize))
                    grayscale_image = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY)

                    image_save(result_dir+"origin/"+result_name+"/", image_lists[m][sub_key[key_i]][i], origin_image)
                    image_save(result_dir+"resize/"+result_name+"/", image_lists[m][sub_key[key_i]][i], resized_image)
                    image_save(result_dir+"gray/"+result_name+"/", image_lists[m][sub_key[key_i]][i], grayscale_image)

                    if key_i == 0:
                        x_train.append(resized_image)
                        t_train = np.append(t_train, np.array([int(np.asfarray(m))]), axis=0)
                    elif key_i == 1:
                        x_test.append(resized_image)
                        t_test = np.append(t_test, np.array([int(np.asfarray(m))]), axis=0)
                    else:
                        x_valid.append(resized_image)
                        t_valid = np.append(t_valid, np.array([int(np.asfarray(m))]), axis=0)

        print(" complete.")
    #print(" x_train shape: " + str(np.array(x_train).shape))
    #print(" t_train shape: " + str(np.array(t_train).shape))
    #print(" x_test shape: " + str(np.array(x_test).shape))
    #print(" t_test shape: " + str(np.array(t_test).shape))
    x_train = np.asarray(x_train)
    t_train = np.asarray(t_train)
    x_test = np.asarray(x_test)
    t_test = np.asarray(t_test)
    return (x_train, t_train), (x_test, t_test), len(master_key)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号