首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >给出每个集的图像文件名列表,将大型数据集拆分到train/valid/test目录?

给出每个集的图像文件名列表,将大型数据集拆分到train/valid/test目录?
EN

Stack Overflow用户
提问于 2019-02-23 12:49:37
回答 1查看 1.1K关注 0票数 1

我正在尝试将一个大型数据集从Food101数据集中拆分成训练/有效/测试集,用于图像分类

数据集的结构如下所示,它将所有图像放在一个文件夹中

代码语言:javascript
复制
'',
'Structure:',
'----------',
'pec/',
'    images/',
'        <class_name>/',
'            <image_id>.jpg',
'    meta/',
'        classes.txt',
'        labels.txt',
'        test.json',
'        test.txt',
'        train.json',
'        train.txt',
'',
'All images can be found in the "images" folder and are organized per class. All',
'image ids are unique and correspond to the foodspotting.com review ids. 
'',
'The test/train splitting used in the experiment of our paper can be found in',
'the "meta" directory.', (edited) ```



I want to divide images dataset to train/valid/test  with the list of filenames given in train.txt and test.txt, which author used 

列车形状,有效,测试列表:(101,600),(101,150),25250

在colab中,我运行以下代码

代码语言:javascript
复制
for x in range(train.shape[0]):
    for y in range(train.shape[1]):

     temp = train[x,y] + ".jpg"

     foldername = temp.split('/')[0]

     !mv /content/food-101/images/$temp /content/food101/train/$foldername/

通过在列表中获取文件名来运行嵌套循环来单独移动图像,由于总共有100100个图像,因此创建文件夹花费了永远的时间,

我有一个训练/有效和测试集的文件名列表,但是如何将它们放入文件夹,以便我们可以将其以pytorch图像文件夹格式提供给图像分类器(我的意思是,训练/有效/测试集是三个不同的文件夹,每个文件夹都有每个类的子文件夹)。

如果有人知道怎么做,请告诉我,我真的需要你的帮助,谢谢:微笑:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-23 16:38:59

似乎我的解决方案一直都错了,我不需要移动镜像,我只需要通过os模块将镜像的路径更改为所需格式。

下面是执行此操作的代码。假设您有有效列表中的文件名列表

代码语言:javascript
复制
#for valid set 

v = valid.reshape(15150,)

or_fpath = '/content/food-101/images/' #path of original folder
cp_fpath = '/content/food101/valid/'   #path of destination folder

for y in tqdm(v):

 foldername = y.split('/')[0]

 img = y.split('/')[1] +'.jpg'

 ip_path = or_fpath+foldername
 op_path = cp_fpath+foldername

 if not os.path.exists(op_path):
   os.mkdir(op_path)

 os.rename(os.path.join(ip_path, img), os.path.join(op_path, img))

谢谢!

注意:如果你有更好的答案,请分享谢谢

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54838339

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档