-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathpython_data_layer.py
More file actions
164 lines (136 loc) · 5.48 KB
/
Copy pathpython_data_layer.py
File metadata and controls
164 lines (136 loc) · 5.48 KB
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
import numpy as np
import sys
import cv2
import caffe
import os
import random
import time
from pandas.io.parsers import read_csv
import matplotlib.pyplot as plt
datadir = 'data/CelebA/'
mean_file = 'model/resnet_50/ResNet_mean.binaryproto'
proto_data = open(mean_file, "rb").read()
a = caffe.io.caffe_pb2.BlobProto.FromString(proto_data)
mean = caffe.io.blobproto_to_array(a)[0]
selected_attr = np.zeros(40)
for attr_index in range(40):
selected_attr[attr_index] = attr_index
def pre_process(color_img):
resized_img = cv2.resize(color_img, (224, 224))
return np.transpose(resized_img, (2, 0, 1)) - mean
def showImage(img,points=None, bbox=None):
if points is not None:
for i in range(0,points.shape[0]/2):
cv2.circle(img,(int(round(points[i*2])),int(points[i*2+1])),1,(0,0,255),2)
if bbox is not None:
cv2.rectangle(img, (int(bbox[0]), int(bbox[2])), (int(bbox[1]), int(bbox[3])), (0,0,255), 2)
plt.figure()
plt.imshow(img)
plt.show()
print 'here'
class ValLayer(caffe.Layer):
attri_num = 40
"""
Compute the Euclidean Loss in the same manner as the C++ EuclideanLossLayer
to demonstrate the class interface for developing layers in Python.
"""
total_namelist = []
attri_array =[]
# landmark_array =[]
img_num = 0
target_height = 224
target_witdh = 224
# mean = []
batch = 20
imgset = 'align_val'
def setup(self, bottom, top):
filename = os.path.join(datadir, self.imgset, self.imgset + 'list.txt')
attrfile = os.path.join(datadir, self.imgset, self.imgset + '_attr.txt')
det_df = read_csv(filename,sep=' ',header=None)
self.total_namelist = det_df[det_df.columns[0]].values
self.img_num = len(self.total_namelist)
self.attri_array = np.zeros((self.img_num, self.attri_num))
attr_f = open(attrfile, 'r')
attr_line = attr_f.readline().strip().split()
i = 0
while attr_line:
for j in range(0, self.attri_num):
value = int(float(attr_line[j + 1]))
assert value == 1 or value == -1
self.attri_array[i, j] = value
attr_line = attr_f.readline().strip().split()
i += 1
top[0].reshape(self.batch,3,self.target_height, self.target_witdh)
top[1].reshape(self.batch, len(selected_attr))
def reshape(self, bottom, top):
pass
def forward(self, bottom, top):
for i in range(self.batch):
# print i
idx = random.randint(0,self.img_num-1)
im = cv2.imread(self.total_namelist[idx])
if im is not None:
top[0].data[i] = pre_process(im)
for k in range(0, len(selected_attr)):
top[1].data[i][k] = self.attri_array[idx][int(selected_attr[k])]
def backward(self, top, propagate_down, bottom):
pass
class JointAttributeLayer(caffe.Layer):
attri_num = 40
point_num = 5
"""
Compute the Euclidean Loss in the same manner as the C++ EuclideanLossLayer
to demonstrate the class interface for developing layers in Python.
"""
train_total_namelist = []
train_attri_array = []
train_img_num = 0
val_total_namelist = []
val_attri_array =[]
val_img_num = 0
target_height = 224
target_witdh = 224
batch = 10
train_imgset = 'align_train'
val_imgset = 'align_val'
def do_setup(self, imgset, attri_num):
filename = os.path.join(datadir, imgset, imgset + 'list.txt')
attrfile = os.path.join(datadir, imgset, imgset + '_attr.txt')
det_df = read_csv(filename,sep=' ', header=None)
total_namelist = det_df[det_df.columns[0]].values
img_num = len(total_namelist)
attri_array = np.zeros((img_num, attri_num))
attr_f = open(attrfile, 'r')
attr_line = attr_f.readline().strip().split()
i = 0
while attr_line:
for j in range(0, attri_num):
value = int(float(attr_line[j + 1]))
assert value == 1 or value == -1
attri_array[i, j] = value
attr_line = attr_f.readline().strip().split()
i += 1
return total_namelist, img_num, attri_array
def setup(self, bottom, top):
self.train_total_namelist, self.train_img_num, self.train_attri_array = self.do_setup(self.train_imgset, self.attri_num)
self.val_total_namelist, self.val_img_num, self.val_attri_array = self.do_setup(self.val_imgset, self.attri_num)
top[0].reshape(2 * self.batch,3,self.target_height,self.target_witdh)
top[1].reshape(2 * self.batch, len(selected_attr))
def reshape(self, bottom, top):
pass
def do_forward(self, top, begin_index, total_namelist, img_num, attri_array):
for i in range(self.batch):
# print i
idx = random.randint(0, img_num-1)
im = cv2.imread(total_namelist[idx])
if im is not None:
top[0].data[i + begin_index] = pre_process(im)
for k in range(0, len(selected_attr)):
top[1].data[i + begin_index][k] = attri_array[idx][int(selected_attr[k])]
def forward(self, bottom, top):
self.do_forward(top, 0, self.train_total_namelist, self.train_img_num,
self.train_attri_array)
self.do_forward(top, self.batch, self.val_total_namelist, self.val_img_num,
self.val_attri_array)
def backward(self, top, propagate_down, bottom):
pass