-
Notifications
You must be signed in to change notification settings - Fork 0
/
ml_utils.py
112 lines (76 loc) · 2.8 KB
/
ml_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
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
from __future__ import print_function
from logging import exception
import cv2
import matplotlib.pyplot as plt
import numpy as np
import torch
import os
import shutil
from skimage.transform import resize as skimage_resize
from skimage import io
from sklearn.cluster import KMeans , SpectralClustering
from sklearn.mixture import GaussianMixture
def normalize_image(img_path) :
"""
[0-255] --> [0 - 1]
Neural networks process inputs using small weight values, and inputs with large integer values can disrupt or slow down the learning process.
As such it is good practice to normalize the pixel values so that each pixel value has a value between 0 and 1.
"""
try :
img = io.imread(img_path)
except:
img = img_path
img = img.astype('float32')
# normalize to the range 0-1
img /= 255.0
return img
def centerize_image(img_path , normalize_before = False) :
"""
centerizing and standartizing are used for training neural networks efficently
after you done center/standartize image you can not display it because of negative values
so reverse your operations to display again in range [0-1] / [0 - 255 ]
Centering, as the distribution of the pixel values is centered on the value of zero.
Centering can be performed before or after normalization.
Centering the pixels before normalizing will mean that the pixel values will be centered close to 0.5 and be in the range 0-1.
Centering after normalization will mean that the pixels will have positive and negative values [-0.5 , 0.5 ],
in which case images will not display correctly (e.g. pixels are expected to have value in the range 0-255 or 0-1).
"""
normalize_after = not normalize_before
try :
img = io.imread(img_path)
except:
img = img_path
img = img.astype('float32')
if normalize_before:
img /= 255.0
mean = img.mean()
img -= mean
if normalize_after:
img /= 255.0
return img
def standardize_image(img_path) :
try :
img = io.imread(img_path)
except:
img = img_path
img = img.astype('float32')
mean,std = img.mean() , img.std()
img -= mean
img /= std
return img
def K_means_sklearn(lines,n_clusters,show = True):
#prepare data
X = np.empty(shape=(0,2))
for item in lines:
first_point,second_point = item[:]
X = np.vstack([X,first_point,second_point])
assert X.shape[0] == len(lines) * 2 and X.shape[1] == 2
model = KMeans(n_clusters)
model.fit(X)
yhat = model.predict(X)
clusters = np.unique(yhat)
if show:
for cluster in clusters:
row_ix = np.where(yhat == cluster)
plt.scatter(X[row_ix, 0], X[row_ix, 1])
plt.show()