forked from gadami01/ColorAwakers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit_data.py
50 lines (40 loc) · 1.84 KB
/
split_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import os
import shutil
import xlsxwriter
import numpy as np
import random
import os
import cv2
# Define the base path for the source dataset
train_path = "C:/Users/George/Desktop/imagenet-mini/train"
val_path = "C:/Users/George/Desktop/imagenet-mini/val"
# Define the base path for the destination folders
dest_path = "C:/Users/George/Desktop/imagenet-mini-split-128"
train_dest_path = "C:/Users/George/Desktop/imagenet-mini-split-128/train"
val_dest_path = "C:/Users/George/Desktop/imagenet-mini-split-128/val"
test_dest_path = "C:/Users/George/Desktop/imagenet-mini-split-128/test"
os.makedirs(dest_path, exist_ok=True)
os.makedirs(train_dest_path, exist_ok=True)
os.makedirs(val_dest_path, exist_ok=True)
os.makedirs(test_dest_path, exist_ok=True)
trainingCounter = 0
validationCounter = 0
testCounter = 0
for imgDir in os.listdir(train_path):
for img in os.listdir(train_path + "/" + imgDir):
img = cv2.imread(train_path + "/" + imgDir + "/" + img)
img = cv2.resize(img, (128, 128), interpolation=cv2.INTER_LINEAR)
cv2.imwrite(os.path.join(train_dest_path, str(trainingCounter) + '.JPEG'), img)
trainingCounter += 1
for imgDir in os.listdir(val_path):
for img in os.listdir(val_path + "/" + imgDir):
if random.random() >= 0.5:
img = cv2.imread(val_path + "/" + imgDir + "/" + img)
img = cv2.resize(img, (128, 128), interpolation=cv2.INTER_LINEAR)
cv2.imwrite(os.path.join(val_dest_path, str(validationCounter) + '.JPEG'), img)
validationCounter += 1
else:
img = cv2.imread(val_path + "/" + imgDir + "/" + img)
img = cv2.resize(img, (128, 128), interpolation=cv2.INTER_LINEAR)
cv2.imwrite(os.path.join(test_dest_path, str(testCounter) + '.JPEG'), img)
testCounter += 1