-
Notifications
You must be signed in to change notification settings - Fork 4
/
convertToNPY.py
171 lines (127 loc) · 6.57 KB
/
convertToNPY.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
#Convert the input datasets to a numpy array to make access easier later
#Additionally it can iterate through the original dataset carrying out random operations including:
#-flipping
#-rotation
#-color transformations
import os
import sys
import random
import warnings
import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tqdm import tqdm
from itertools import chain
from skimage.io import imread, imshow, imread_collection, concatenate_images
from skimage.transform import resize
from skimage.morphology import label
# Set some parameters
IMG_WIDTH = 384
IMG_HEIGHT = 384
IMG_CHANNELS = 3
TRAIN_PATH = 'stage1_train/'
augment = True
normalize = True
warnings.filterwarnings('ignore', category=UserWarning, module='skimage')
seed = 42
random.seed = seed
np.random.seed = seed
# Get train and test IDs
train_ids = next(os.walk(TRAIN_PATH))[1]
# Get and resize train images and masks
X_train = []
Y_train = []
print('Getting and resizing train images and masks ... ')
sys.stdout.flush()
i=0
for n, id_ in tqdm(enumerate(train_ids), total=len(train_ids)):
#acquire original image and resize so all images match
path = TRAIN_PATH + id_
imgOrig = imread(path + '/images/' + id_ + '.png')[:,:,:IMG_CHANNELS]
img = resize(imgOrig, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)
#acquire original mask and resize so all images match
mask = np.zeros((IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.bool)
for mask_file in next(os.walk(path + '/masks/'))[2]:
mask_ = imread(path + '/masks/' + mask_file)
if mask_.ndim==2:
mask_ = np.expand_dims(resize(mask_, (IMG_HEIGHT, IMG_WIDTH), mode='constant',preserve_range=True), axis=-1)
else:
mask_=mask_[:,:,0]
mask_ = np.expand_dims(resize(mask_, (IMG_HEIGHT, IMG_WIDTH), mode='constant',preserve_range=True), axis=-1)
mask_=mask_>250
mask = np.maximum(mask, mask_)
img=img.astype(np.uint8)
mask=mask.astype(np.bool)
if np.count_nonzero(mask)==0:
print('no mask! '+str(id_))
continue
#plt.imsave('test/test'+str(id_)+'.png',np.squeeze(mask))
#plt.clf()
X_train.append(img)
Y_train.append(mask)
if augment == True:
#create alternative mask/image pairs out of the larger training examples
if imgOrig.shape[0]>IMG_HEIGHT or imgOrig.shape[0]>IMG_WIDTH:
for i in range(math.ceil(imgOrig.shape[0]/IMG_HEIGHT)):
for j in range(math.ceil(imgOrig.shape[1]/IMG_HEIGHT)):
#print (imgOrig.shape[0]/IMG_HEIGHT)
#print (imgOrig.shape[1]/IMG_HEIGHT)
if i+1==math.ceil(imgOrig.shape[0]/IMG_HEIGHT) or j+1==math.ceil(imgOrig.shape[1]/IMG_HEIGHT):
continue
subImg=imgOrig[i*IMG_HEIGHT:(i+1)*IMG_HEIGHT,j*IMG_WIDTH:(j+1)*IMG_WIDTH,:]
subImg = resize(subImg, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)
subMask = np.zeros((IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.bool)
for mask_file in next(os.walk(path + '/masks/'))[2]:
mask_ = imread(path + '/masks/' + mask_file)
mask_ = mask_[i*IMG_HEIGHT:(i+1)*IMG_HEIGHT,j*IMG_WIDTH:(j+1)*IMG_WIDTH]
if mask_.ndim==2:
mask_ = np.expand_dims(resize(mask_, (IMG_HEIGHT, IMG_WIDTH), mode='constant',preserve_range=True), axis=-1)
else:
mask_=mask_[:,:,0]
mask_ = np.expand_dims(resize(mask_, (IMG_HEIGHT, IMG_WIDTH), mode='constant',preserve_range=True), axis=-1)
mask_=mask_>250
subMask = np.maximum(subMask, mask_)
subImg=subImg.astype(np.uint8)
subMask=subMask.astype(np.bool)
if np.count_nonzero(subMask)==0:
print('no mask! '+str(id_))
continue
X_train.append(subImg)
Y_train.append(subMask)
#plt.imsave('test/mask'+str(id_)+'_'+str(i)+'_'+str(j)+'.png',np.squeeze(subMask))
#plt.imsave('test/img'+str(id_)+'_'+str(i)+'_'+str(j)+'.png',subImg)
#plt.clf()
# for j in range(0, 10):
# cloneImg=img
# cloneMask=mask
# #randomly flip x axis
# if random() > 0.5:
# cloneImg=np.flip(cloneImg, 0)
# cloneMask=np.flip(cloneMas, 0)
# #randomly flip y axis
# if random() > 0.5:
# cloneImg=np.flip(cloneImg, 1)
# cloneMask=np.flip(cloneMask, 1)
# #random rotation
# if random() > 0.5:
# rotAngle=360*random()
# cloneImg=rotate(cloneImg, rotAngle)
# cloneMask=rotate(cloneMask, rotAngle)
# #randomly shift color palette
# if random() > 0.5:
# cloneImg=rgb2hsv(cloneImg)
# #randomly shift color palette
# if random() > 0.5:
# cloneImg=hsv2rgb(cloneImg)
# X_train.append(cloneImg)
# Y_train.append(cloneMask)
i=i+1
if normalize == True:
for i in range(len(X_train)):
#print (X_train[i].shape)
div = X_train[i].max(axis=tuple(np.arange(1,len(X_train[i].shape))), keepdims=True)
div[div < 0.01*X_train[i].mean()] = 1. # protect against too small pixel intensities
X_train[i] = X_train[i].astype(np.float32)/div
np.save('inputImagesHRes.npy',np.stack(X_train))
np.save('inputMaskHRes.npy',np.stack(Y_train))