-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdata_input.py
175 lines (147 loc) · 5.79 KB
/
data_input.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
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
#SYSTEM IMPORTS
#print "Importing dependencies..."
import glob
import os
import argparse
import gzip
import tarfile
import urllib
from os import listdir
from os.path import isfile, join
import scipy.misc
import tensorflow as tf
import numpy as np
from tensorflow.python.framework import dtypes
import base
import matplotlib.pyplot as plt
import cv2
from imageflow import convert_images
#-------------------------------------------------
print "Locating dataset..."
#Read images into arrays
#Sat images = Input data
# The dataset has 10 classes, representing the digits 0 through 9.
#NUM_CLASSES = 10
# The images are always 1500x1500 pixels.
IMAGE_SIZE = 750
NUM_CHANNELS = 3
NUM_CLASSES = 2
IMAGE_PIXELS = IMAGE_SIZE * IMAGE_SIZE * NUM_CHANNELS
RESIZE_FACTOR = 10
INTERP = 'nearest'
class DataSet(object):
def __init__(self, images, labels, fake_data=False, one_hot=False):#, dtype=dtypes.float32):
#dtype = dtypes.as_dtype(dtype).base_dtype
if fake_data:
self._num_examples = 10000
else:
assert images.shape[0] == labels.shape[0], (
"images.shape: %s labels.shape: %s" % (images.shape,
labels.shape))
self._num_examples = images.shape[0]
# Convert shape from [num examples, rows, columns, depth]
# to [num examples, rows*columns*depth] (assuming depth == 3)
#assert images.shape[3] == 1
#images = images.reshape(images.shape[0],images.shape[1] * images.shape[2] * images.shape[3])
# Convert from [0, 255] -> [0.0, 1.0].
images = np.multiply(images, 1.0/255.0)
images -= np.mean(images)
images = images.astype(np.float32)
# normalize labels to values [0,1] for binary classification
labels = np.multiply(labels, 1.0/255.0)
labels = labels.astype(np.float32)
self._images = images
self._labels = labels
self._epochs_completed = 0
self._index_in_epoch = 0
@property
def images(self):
return self._images
@property
def labels(self):
return self._labels
@property
def num_examples(self):
return self._num_examples
@property
def epochs_completed(self):
return self._epochs_completed
def next_batch(self, batch_size, fake_data=False):
"""Return the next `batch_size` examples from this data set."""
if fake_data:
fake_image = [1.0 for _ in xrange(245025)]
fake_label = 0
return [fake_image for _ in xrange(batch_size)], [
fake_label for _ in xrange(batch_size)]
start = self._index_in_epoch
self._index_in_epoch += batch_size
if self._index_in_epoch > self._num_examples:
# Finished epoch
self._epochs_completed += 1
# Shuffle the data
perm = np.arange(self._num_examples)
np.random.shuffle(perm)
self._images = self._images[perm]
self._labels = self._labels[perm]
# Start next epoch
start = 0
self._index_in_epoch = batch_size
assert batch_size <= self._num_examples
end = self._index_in_epoch
return self._images[start:end], self._labels[start:end]
def read_data_sets(train_dir, fake_data=False, one_hot=False):
class DataSets(object):
pass
# Satellite image inputs for testing
mypath='/data/mass_roads_batch/test/sat'# % data_type
if os.path.exists(mypath):
print ("Found datasets \n Loading files...")
else:
print ("Data not found!")
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f))]
sat_images_test = np.empty([len(onlyfiles),IMAGE_SIZE,IMAGE_SIZE, NUM_CHANNELS])
for n in range(0, len(onlyfiles)):
sat_images_test[n] = cv2.imread(join(mypath, onlyfiles[n]))
print ("reading test satellite image: %d of %d" % (n+1, len(onlyfiles)))
# Satellite image MAPS (Labels) for testing
mypath='/data/mass_roads_batch/test/map'# % data_type
if os.path.exists(mypath):
print ("Found datasets \n Loading files...")
else:
print ("Data not found!")
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f))]
#map_images_test = np.empty([len(onlyfiles),IMAGE_SIZE,IMAGE_SIZE, NUM_CHANNELS])
map_images_test = np.empty([len(onlyfiles),IMAGE_SIZE,IMAGE_SIZE])
for n in range(0, len(onlyfiles)):
map_images_test[n] = cv2.cvtColor(cv2.imread(join(mypath, onlyfiles[n])), cv2.COLOR_RGB2GRAY)
print ("reading test satellite map: %d of %d" % (n+1, len(onlyfiles)))
# Satellite image inputs for training
mypath='/data/mass_roads_batch/train/sat'# % data_type
if os.path.exists(mypath):
print ("Found datasets \n Loading files...")
else:
print ("Data not found!")
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f))]
sat_images_train = np.empty([len(onlyfiles),IMAGE_SIZE,IMAGE_SIZE, NUM_CHANNELS])
for n in range(0, len(onlyfiles)):
sat_images_train[n] = cv2.imread(join(mypath, onlyfiles[n]))
print ("reading training satellite image: %d of %d" % (n+1, len(onlyfiles)))
# Satellite image MAPS (Labels) for training
mypath='/data/mass_roads_batch/train/map'# % data_type
if os.path.exists(mypath):
print ("Found datasets \n Loading files...")
else:
print ("Data not found!")
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f))]
map_images_train = np.empty([len(onlyfiles),IMAGE_SIZE,IMAGE_SIZE])
for n in range(0, len(onlyfiles)):
map_images_train[n] = cv2.cvtColor(cv2.imread(join(mypath, onlyfiles[n])), cv2.COLOR_RGB2GRAY)
print ("reading training satellite image: %d of %d" % (n+1, len(onlyfiles)))
train_images = sat_images_train
train_labels = map_images_train
test_images = sat_images_test
test_labels = map_images_test
train = DataSet(train_images, train_labels)#, dtype=dtype)
test = DataSet(test_images, test_labels)#, dtype=dtype)
print ("Finished importing data. Returning data to conv_net_test.py..")
return base.DataSets(train=train, test=test)