我试图将大型数据集分为火车/有效/测试集从food101数据集中进行图像分类
和数据集的结构是这样的,并且在一个文件夹中具有所有图像
'',
'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中,我按照代码
运行
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 image映像文件夹格式中的图像分类器(我的意思是火车/有效/测试集是三个不同的文件夹每个文件夹都有每个类的子文件夹)
请告诉任何人是否知道该怎么做,请我在这里真的需要您的帮助,谢谢:
看来我对解决方案已经完全错误,我不需要移动图像,我需要更改的只是通过OS模块以所需格式的图像的路径
以下是这样做的代码。说您有有效列表中的文件名列表
#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))
谢谢!
注意:如果您有更好的答案,请分享感谢