python - 从 Google Drive 读取到 Google Colab 后,图像数据以不同/随机顺序存储在数组中
问题描述
我正在尝试使用 Google Colab 使用 U-net 进行图像分割过程。我可以将图像数据集从 Drive 读取到 Colab 并保存在一个数组中。仅供参考:我的 Google Drive 中有一个文件夹,其中包含所有训练数据,其中包含 2 个子文件夹(分别为 Image 和 Mask)。
现在,当我使用“plt.show”检查图像时读取并调整图像和蒙版的大小后,我注意到图像编号的顺序存在差异。例如,当我随机选择第 10 张图片时,该图片与谷歌驱动器中的第 10 张图片不匹配。更糟糕的是,我的面具得到了完全不同的图像,这使我的图像和面具不同(主要问题!!)。
有没有人遇到过类似的情况?知道如何解决这个问题吗?
解决方案
我遇到了这个问题。从 google 驱动器将图像从目录导入到 google colab 似乎随机导入图像。
因此,我首先检查了此代码以确认我的理论。
inside = os.listdir('/content/gdrive/MyDrive/files/')
for i in range(20):
print(inside[i])
这给出了输出:
15588_KateOMara_32_f.jpg
15658_KatharineRoss_68_f.jpg
15741_MaryTamm_40_f.jpg
15661_KatharineRoss_72_f.jpg
15621_KateOMara_70_f.jpg
15646_KatharineRoss_46_f.jpg
15851_StВphaneAudran_22_f.jpg
15810_SarahDouglas_61_f.jpg
15486_JeanetteMacDonald_46_f.jpg
15831_StefaniePowers_56_f.jpg
15670_KathrynGrayson_26_f.jpg
15539_JulieBishop_36_f.jpg
15696_KathrynGrayson_75_f.jpg
15738_MaryTamm_33_f.jpg
15853_StВphaneAudran_24_f.jpg
15665_KathrynGrayson_21_f.jpg
15815_StefaniePowers_24_f.jpg
15748_MaryTamm_51_f.jpg
15759_PamelaSueMartin_26_f.jpg
15799_SarahDouglas_43_f.jpg
我正在使用
os.listdir(self.directory)
它返回指定路径中所有文件和目录的列表。所以我只是用
sorted()
功能对列表进行排序,这解决了问题。
sorted_dir = sorted(os.listdir('/content/gdrive/MyDrive/files/'))
for i in range(20):
print(sorted_dir[i])
输出:
0_MariaCallas_35_f.jpg
10000_GlennClose_62_f.jpg
10001_GoldieHawn_23_f.jpg
10002_GoldieHawn_24_f.jpg
10003_GoldieHawn_24_f.jpg
10004_GoldieHawn_27_f.jpg
10005_GoldieHawn_28_f.jpg
10006_GoldieHawn_29_f.jpg
10007_GoldieHawn_30_f.jpg
10008_GoldieHawn_31_f.jpg
10009_GoldieHawn_35_f.jpg
1000_StephenHawking_1_m.jpg
10010_GoldieHawn_35_f.jpg
10011_GoldieHawn_37_f.jpg
10012_GoldieHawn_39_f.jpg
10013_GoldieHawn_44_f.jpg
10014_GoldieHawn_45_f.jpg
10015_GoldieHawn_45_f.jpg
10016_GoldieHawn_50_f.jpg
10017_GoldieHawn_51_f.jpg
前:
for i, file in enumerate(os.listdir(self.directory)):
file_labels = parse('{}_{person}_{age}_{gender}.jpg', file)
后:
for i, file in enumerate(sorted(os.listdir(self.directory))):
file_labels = parse('{}_{person}_{age}_{gender}.jpg', file)
推荐阅读
- angular - Angular 服务将字符串从 App 组件共享到其他组件
- date - 使用 ARRAYFORMULA 插入新行时谷歌表格日期重新格式化
- python - 有什么方法可以在 Django 中为我的 sqlite3 数据库设置密码?
- asp.net-core - 如何从 asp.net core 3 Identity 中删除 UserName 字段
- html - 如何从网站获取 HTML 代码并将其转换为 VB 字符串?
- javascript - 可以在没有 ajaxSetup 的情况下预定义 ajax 参数吗?
- json - 在 Woocommerce Rest API 上遇到 JSON 问题
- mysql - MySQL中的特定时间触发器
- sql - 无法为数据库“tempdb”中的对象“dbo.SORT 临时运行存储:142590947360768”分配空间,因为“PRIMARY”
- php - echo Recursiveiteratoriterator 输出作为数组中的键