/
Tools.py
47 lines (40 loc) · 1.39 KB
/
Tools.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
import numpy as np
import torch
import random
import math
from scipy import ndimage
def DeviceInitialization(GPUNum):
if torch.cuda.is_available():
torch.backends.cudnn.deterministic = True
device = torch.device(GPUNum)
else:
device = torch.device('cpu')
random.seed(2021)
np.random.seed(2021)
torch.manual_seed(2021)
return device
def DataReading(path, set, fracTrain, fracTest):
dataType = np.genfromtxt(path + set + '.txt', dtype=str)
IDs = []
labels = []
for ii in range(len(dataType)):
type = dataType[ii]
data = np.genfromtxt(path + 'Csvs\\' + type + '.txt', dtype=str)
for jj in range(len(data)):
tempID = type + '\\' + data[jj]
tempLabel = ii
IDs.append(tempID)
labels.append(tempLabel)
shuffleIdx = np.arange(len(IDs))
shuffleRng = np.random.RandomState(2022)
shuffleRng.shuffle(shuffleIdx)
IDs = np.array(IDs)[shuffleIdx]
labels = np.array(labels)[shuffleIdx]
trainNum = math.ceil(fracTrain * len(IDs) / 100)
testNum = math.ceil(fracTest * len(IDs) / 100)
testNum = len(IDs) - testNum
trainIdxIDs = IDs[:trainNum]
trainIdxLabels = labels[:trainNum]
testIdxIDs = IDs[testNum:]
testIdxLabels = labels[testNum:]
return trainIdxIDs, trainIdxLabels, testIdxIDs, testIdxLabels