[ad8447]: / (1) PyTorch_HistoNet / util / dbToDataStore.py

Download this file

140 lines (114 with data), 4.1 kB

  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
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import os
import matplotlib.pyplot as plt
from util.pause import pause
import csv
import torch
from torchvision import datasets
class ImageFolderWithPaths(datasets.ImageFolder):
"""Custom dataset that includes image file paths. Extends
torchvision.datasets.ImageFolder
"""
def __init__(self, image_pathP, transformP, classesP, filenamesP):
super(ImageFolderWithPaths, self).__init__(root=image_pathP, transform=transformP)
self.data = datasets.ImageFolder(image_pathP, transformP)
self.classVec = classesP
self.fileNameVec = filenamesP
# override the __getitem__ method. this is the method that dataloader calls
def __getitem__(self, index):
# this is what ImageFolder normally returns
im, dumTarget = super(ImageFolderWithPaths, self).__getitem__(index)
# the image file path
path = self.imgs[index][0]
#labelsTens = list()
#for i, (s_path) in enumerate(path):
# label
classV = ()
dir, filename = os.path.split(path)
indexL = self.fileNameVec.index(filename)
classV = self.classVec[indexL]
#labelsTens.append(classV)
#print(index)
#print(path)
#print(torch.tensor(classV))
#pause()
# make a new tuple that includes original and the path
#tuple_with_path = (original_tuple + (path,))
#return tuple_with_path
return im, dumTarget, (path), (torch.tensor(classV))
def getClass(columnNames, rowF, hierarNum, classesADP):
classes = list()
for className in classesADP[hierarNum]['classesNames']:
classes.append(rowF[columnNames.index(className)])
#print(classes)
#classOne = torch.max(classes, 1)
# cast
classesInt = [int(i) for i in classes]
return classesInt
def getAllClassesVec(hierarNum, classesADP, csvFileFull, log):
# open csv
allClasses = list()
allFileNames = list()
with open(csvFileFull) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
columnNames = row
else:
#print(row)
fileName = row[0]
classVec = getClass(columnNames, row, hierarNum, classesADP)
allClasses.append(classVec)
allFileNames.append(fileName)
#print(classVec)
#pause()
line_count += 1
print('Processed {line_count} lines.')
return allClasses, allFileNames, columnNames
def dbToDataStore(dirIn, dirOut, extOrig, extNew, log):
dLabel = 'dummyLabel'
# display
if log:
print("Transforming DB...")
# dummy dir
dirOutLabel = os.path.join(dirOut, dLabel)
# create directory if not present
if not os.path.exists(dirOutLabel):
os.mkdir(dirOutLabel)
# transform db
for i, name in enumerate(os.listdir(dirIn)):
if name.endswith(extOrig):
# display
if log and (i % 1000 == 0):
print("\tProcessing: " + name)
# read name
pre, ext = os.path.splitext(name)
# newname
newName = pre + '.' + extNew
newPath = os.path.join(dirOutLabel, newName)
# if already present skip
if os.path.exists(newPath):
continue
# read
img = plt.imread(os.path.join(dirIn, name))
# display
"""
print(newName)
plt.imshow(img)
plt.show()
pause()
"""
# write
plt.imsave(newPath, img)
#pause()
print()
return dLabel
def extractLabels(dirDbPart, fileNameVec, classVec):
classVall = list()
fileNameVecAll = list()
for i, name in enumerate(os.listdir(dirDbPart)):
indexL = fileNameVec.index(name)
fileNameVecAll.append(name)
classV = classVec[indexL]
classVall.append(classV)
return classVall, fileNameVecAll