-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathops.py
84 lines (76 loc) · 2.04 KB
/
ops.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
"""
This code include some of the methods used in y-DCGAN
"""
import numpy as np
import tensorflow as tf
from PIL import Image
import scipy.misc
import os
def saveImg(array, name):
scipy.misc.imsave(name + '.pgm', array)
def norm(a):
min=257
max=-1
for i in range(256):
for j in range(256):
if(a[i][j]<min):
min=a[i][j]
if(a[i][j]>max):
max=a[i][j]
for i in range(256):
for j in range(256):
a[i][j]=(a[i][j]-min)/max
a[i][j]= a[i][j] * 256
return a
def cross_entropy(x, y):
return tf.nn.sigmoid_cross_entropy_with_logits(logits=x, labels=y)
def get_sparsity(pro):
p=0.0001
p_=tf.reduce_mean(pro, axis=0)
spar=p*tf.log(p/(p_+1e-10)) + (1-p)*tf.log((1-p)/(1-p_))
return tf.reduce_mean(spar)
def get_label(type):
if(type):
return np.array([[1], [1], [1], [1], [1],[1], [1], [1], [1], [1]], dtype=np.float)
else:
return np.array([[0],[0],[0],[0],[0],[0],[0],[0],[0],[0]], dtype=np.float)
def get_test_data(path):
os.chdir(path)
img_names=[('%d.pgm'%i) for i in range(50000, 56000, 3)]
list=[]
for img_name in img_names:
img=Image.open(img_name)
a=np.array(img)
list.append(a.tolist())
return np.array(list, dtype=np.float)
def get_trn_data(path):
tmp=os.getcwd()
os.chdir(path)
img_names=[('%d.pgm'%i) for i in range(50000)]
list=[]
cnt = 0
for img_name in img_names:
cnt+=1
if(cnt%5000==0):
print(cnt)
img=Image.open(img_name)
a=np.array(img)
list.append(a.tolist())
ary=np.array(list, dtype=np.float)
ary=ary.reshape([-1,256,256,1])
os.chdir(tmp)
return ary
class batch_norm(object):
def __init__(self, epsilon=1e-5, momentum = 0.9, name="batch_norm"):
with tf.variable_scope(name):
self.epsilon = epsilon
self.momentum = momentum
self.name = name
def __call__(self, x, train=True):
return tf.contrib.layers.batch_norm(x,
decay=self.momentum,
updates_collections=None,
epsilon=self.epsilon,
scale=True,
is_training=train,
scope=self.name)