-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathutils.py
84 lines (73 loc) · 2.59 KB
/
utils.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
"""
Some useful functions are defined in this file
Author: Kaihua Tang
"""
from scipy import misc
import scipy.io as scio
import tensorflow as tf
import numpy as np
import time
import math
import random
def random_mini_batches(totalSize, mini_batch_size = 64, random = True):
"""
totalSize : total num of train image
mini_batch_size : mini batch size
return a set of arrays that contains the index from 1 to totalSize, each array is mini_batch_size
"""
np.random.seed(int(time.time()))
m = totalSize # number of training examples
mini_batches = []
if(random):
permutation = list(np.random.permutation(m))
else:
permutation = list(range(m))
num_complete_minibatches = math.floor(m/mini_batch_size) # number of mini batches of size mini_batch_size in your partitionning
for k in range(0, num_complete_minibatches):
mini_batches.append(permutation[k * mini_batch_size : (k + 1) * mini_batch_size])
if m % mini_batch_size != 0:
mini_batches.append(permutation[(k + 1) * mini_batch_size :])
return mini_batches
def load_all_image(nameList, h, w, c, parentPath, create_npy = False):
"""
Load all image data in advance
nameList: name of image we need to load
"""
all_size = len(nameList)
all_data = np.zeros((all_size, h, w, c), dtype = "uint8")
for i in range(all_size):
tmp_img = load_images(parentPath + nameList[i])
all_data[i,:,:,:] = tmp_img[:,:,:]
if(create_npy):
np.save('./1200_data.npy',all_data)
return all_data
def get_minibatch(indexList, labelList, h, w, c, n, allImage, is_sparse = False):
"""
Load one batch images.
indexList: (size, 1).
nameList: (totalSize, string).
labelList: (totalSize, int)
h, w, c: height, width, channel
n: number of labels
"""
m_size = len(indexList)
batch_X = np.ndarray([m_size, h, w, c])
if(is_sparse):
batch_Y = np.zeros((m_size), dtype = 'int64')
else:
batch_Y = np.zeros((m_size, n))
#print(paths)
for i in range(m_size):
batch_X[i,:,:,:] = allImage[indexList[i],:,:,:]
if(is_sparse):
batch_Y[i] = labelList[indexList[i]] - 1
else:
batch_Y[i, :] = [1 if j == (labelList[indexList[i]] - 1) else 0 for j in range(n)]
return batch_X, batch_Y
def load_images(path):
"""
Load multiple images.
:param paths: The image paths.
"""
img = misc.imread(path, mode="RGB").astype(float)
return img