forked from shoaibahmed/FCN-TensorFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputReader.py
More file actions
176 lines (149 loc) · 5.86 KB
/
inputReader.py
File metadata and controls
176 lines (149 loc) · 5.86 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import os
import numpy as np
import skimage
import skimage.io
import skimage.transform
class InputReader:
def __init__(self, options):
self.options = options
# Reads pathes of images together with their labels
self.imageList = self.readImageNames(self.options.trainFileName)
self.imageListTest = self.readImageNames(self.options.testFileName)
# Shuffle the image list if not random sampling at each stage
if not self.options.randomFetch:
np.random.shuffle(self.imageList)
self.currentIndex = 0
self.totalEpochs = 0
self.totalImages = len(self.imageList)
self.totalImagesTest = len(self.imageListTest)
self.imgShape = [self.options.imageHeight, self.options.imageWidth, self.options.imageChannels]
self.maskShape = [self.options.imageHeight, self.options.imageWidth]
def readImageNames(self, imageListFile):
"""Reads a .txt file containing pathes and labeles
Args:
imageListFile: a .txt file with one /path/to/image per line
Returns:
List with all fileNames in file imageListFile
"""
f = open(imageListFile, 'r')
fileNames = []
for line in f:
# Get file name
fileNames.append(line.strip())
return fileNames
def readImagesFromDisk(self, fileNames, readMask = True):
"""Consumes a list of filenames and returns image with mask
Args:
fileNames: List of image files
Returns:
Two 4-D numpy arrays: The input images as well as well their corresponding binary mask
"""
images = []
masks = []
for i in xrange(0, len(fileNames)):
maskImageName = fileNames[i]
# maskImageName = maskImageName[:-15] + '_mask/mask' + maskImageName[-9:]
lastSlashIndex = maskImageName[::-1].index('/')
imageName = maskImageName[-lastSlashIndex:]
if "-" not in imageName:
maskImageName = maskImageName[:-lastSlashIndex] + 'mask' + maskImageName[-9:]
else:
maskImageName = maskImageName[:-lastSlashIndex] + 'mask' + maskImageName[-11:-6] + maskImageName[-4:]
if self.options.verbose > 1:
print ("Image: %s" % fileNames[i])
print ("Mask: %s" % maskImageName)
# Read image
img = skimage.io.imread(fileNames[i])
if img.shape != self.imgShape:
img = skimage.transform.resize(img, self.imgShape, preserve_range=True)
# skimage.io.imsave('./resizedIm/' + imageName, img)
images.append(img)
# Read mask
if readMask:
mask = skimage.io.imread(maskImageName)
# Convert the mask to [H, W, options.numClasses]
if mask.shape != self.maskShape:
mask = skimage.transform.resize(mask, self.maskShape, preserve_range=True)
backgroundClass = (mask == 0).astype(np.uint8)
foregroundClass = (mask > 0).astype(np.uint8)
mask = np.stack([backgroundClass, foregroundClass], axis=2)
masks.append(mask)
# Convert list to ndarray
images = np.array(images)
masks = np.array(masks)
return images, masks
def getTrainBatch(self):
"""Returns training images and masks in batch
Args:
None
Returns:
Two 4-D numpy arrays: training images and masks in batch.
"""
if self.totalEpochs >= self.options.trainingEpochs:
return None, None
endIndex = self.currentIndex + self.options.batchSize
if self.options.randomFetch:
# Randomly fetch any images
self.indices = np.random.choice(self.totalImages, self.options.batchSize)
else:
# Fetch the next sequence of images
self.indices = np.arange(self.currentIndex, endIndex)
if endIndex > self.totalImages:
# Replace the indices which overshot with 0
self.indices[self.indices >= self.totalImages] = np.arange(0, np.sum(self.indices >= self.totalImages))
imageBatch, maskBatch = self.readImagesFromDisk([self.imageList[index] for index in self.indices])
self.currentIndex = endIndex
if self.currentIndex > self.totalImages:
print ("Training epochs completed: %f" % (self.totalEpochs + (float(self.currentIndex) / self.totalImages)))
self.currentIndex = self.currentIndex - self.totalImages
self.totalEpochs = self.totalEpochs + 1
# Shuffle the image list if not random sampling at each stage
if not self.options.randomFetch:
np.random.shuffle(self.imageList)
return imageBatch, maskBatch
def getTestBatch(self, readMask = True):
"""Returns testing images and masks in batch
Args:
None
Returns:
Two 4-D numpy arrays: test images and masks in batch.
"""
# Optional Image and Label Batching
self.indices = np.random.choice(self.totalImagesTest, self.options.batchSize)
imageBatch, maskBatch = self.readImagesFromDisk([self.imageListTest[index] for index in self.indices], readMask=readMask)
return imageBatch, maskBatch
def restoreCheckpoint(self, numSteps):
"""Restores current index and epochs using numSteps
Args:
numSteps: Number of batches processed
Returns:
None
"""
processedImages = numSteps * self.options.batchSize
self.totalEpochs = processedImages / self.totalImages
self.currentIndex = processedImages % self.totalImages
def saveLastBatchResults(self, outputImages, isTrain=True):
"""Saves the results of last retrieved image batch
Args:
outputImages: 4D Numpy array [batchSize, H, W, numClasses]
isTrain: If the last batch was training batch
Returns:
None
"""
if isTrain:
imageNames = [self.imageList[index] for index in self.indices]
else:
imageNames = [self.imageListTest[index] for index in self.indices]
# Iterate over each image name and save the results
for i in xrange(0, self.options.batchSize):
imageName = imageNames[i].split('/')
imageName = imageName[-1]
if isTrain:
imageName = self.options.imagesOutputDirectory + '/' + 'train_' + imageName[:-4] + '_prob' + imageName[-4:]
else:
imageName = self.options.imagesOutputDirectory + '/' + 'test_' + imageName[:-4] + '_prob' + imageName[-4:]
# print(imageName)
# Save foreground probability
im = np.squeeze(outputImages[i, :, :, 1] * 255)
im = im.astype(np.uint8) # Convert image from float to unit8 for saving
skimage.io.imsave(imageName, im)