Skip to content

Commit

Permalink
u
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseYang committed Apr 13, 2018
1 parent 78d042c commit 033ccdc
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cfgs/config.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .config_normal import cfg
from .config_debug_1600_vgg19 import cfg
80 changes: 80 additions & 0 deletions cfgs/config_debug_1600_vgg19.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from easydict import EasyDict as edict

_ = cfg = edict()
_.train_ann = 'coco/annotations/person_keypoints_train2017.json'
_.val_ann = 'coco/annotations/person_keypoints_val2017.json'


_.train_images_dir = 'coco/train2017'
_.val_images_dir = 'coco/val2017'
_.test_images_dir = 'coco/test2017'

_.train_masks_dir = 'coco/train2017_masks'
_.val_masks_dir = 'coco/val2017_masks'

_.train_labels_dir = 'coco/train2017_labels'
_.val_labels_dir = 'coco/val2017_labels'

_.img_y = 368
_.img_x = 368

# whether skip a person if the distance to existing person is too small
_.skip_adj = False

_.augmentation = True
_.backbone_grad_scale = 0.25

_.debug = True
_.debug_sample_num = 1600

# the default oder in coco annotation is:
# 0:nose 1:left_eye 2:right_eye 3:left_ear
# 4:right_ear 5:left_shoulder 6:right_shoulder 7:left_elbow
# 8:right_elbow 9:left_wrist 10:right_wrist 11:left_hip
# 12:right_hip 13:left_knee 14:right_knee 15:left_ankle
# 16:right_ankle
_.from_body_part = [0, 5, 6, 8, 10, 5, 7, 9, 12, 14, 16, 11, 13, 15, 2, 1, 4, 3]
_.to_body_part = [0, 6, 6, 8, 10, 5, 7, 9, 12, 14, 16, 11, 13, 15, 2, 1, 4, 3]
# the oder after adjustment is:
# 0:nose 1:neck 2:right_shoulder 3:right_elbow
# 4:right_wrist 5:left_shoulder 6:left_elbow 7:left_wrist
# 8:right_hip 9:right_knee 10:right_anckle 11:left_hip
# 12:left_knee 13:left_ankle 14:right_eye 15:left_eye
# 16:right_ear 17:left_ear

# the limbs include:
# 0:neck-->right_hip 1:right_hip-->right_knee 2:right_knee-->right_ankle 3:neck-->left_hip
# 4:left_hip-->left_knee 5:left_knee-->left_ankle 6:neck-->right_shoulder 7:right_shoulder-->right_elbow
# 8:right_elbow-->right_wrist 9:right_shoulder-->right_ear 10:neck-->left_shoulder 11:left_shoulder-->left_elbow
# 12:left_elbow-->left_wrist 13:left_shoulder-->left_ear 14:neck-->nose 15:nose-->right_eye
# 16:nose-->left_eye 17:right_eye-->right_ear 18:left_eye-->left_ear
_.limb_from = [1, 8, 9, 1, 11, 12, 1, 2, 3, 2, 1, 5, 6, 5, 1, 0, 0, 14, 15]
_.limb_to = [8, 9, 10, 11, 12, 13, 2, 3, 4, 16, 5, 6, 7, 17, 0, 14, 15, 16, 17]

_.limb_seq = []
_.map_idx = []
for idx in range(len(_.limb_from)):
_.limb_seq.append([_.limb_from[idx], _.limb_to[idx]])
_.map_idx.append([2 * idx, 2 * idx + 1])

_.stride = 8

_.sigma = 7

_.grid_y = int(_.img_y / _.stride)
_.grid_x = int(_.img_x / _.stride)

_.thre = 1
_.thre1 = 0.1
_.thre2 = 0.05

_.ch_heats = 18 + 1 # 18个parts 第19个(遍历不会执行到)表示是非人体区域
_.ch_vectors = 38
_.stages = 6

_.scale_search = [0.5, 1, 1.5, 2]
_.pad_value = 128

_.base_lr = 1e-5
_.momentum = 0.9
_.weight_decay = 5e-4
4 changes: 2 additions & 2 deletions modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# @layer_register(log_shape=True)
def VGGBlock_official(l):
with argscope(Conv2D, kernel_shape=3, nl=tf.nn.relu):
with argscope(Conv2D, kernel_shape=3, W_init=tf.random_normal_initializer(stddev=0.01), nl=tf.nn.relu):
l = (LinearWrap(l)
.Conv2D('conv1_1', 64)
.Conv2D('conv1_2', 64)
Expand All @@ -28,7 +28,7 @@ def VGGBlock_official(l):

# @layer_register(log_shape=True)
def VGGBlock_ours(l):
with argscope(Conv2D, kernel_shape=3, nl=tf.nn.relu):
with argscope(Conv2D, kernel_shape=3, W_init=tf.random_normal_initializer(stddev=0.01), nl=tf.nn.relu):
l = (LinearWrap(l)
.Conv2D('conv1_1', 64)
.Conv2D('conv1_2', 64)
Expand Down
11 changes: 5 additions & 6 deletions predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def predict(args):
output_names = ['heatmaps', 'pafs'])
predict_func = OfflinePredictor(predict_config)

img = cv2.imread(args.input_path)
img_bgr = cv2.imread(args.input_path)
img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)

h, w, _ = img.shape

Expand Down Expand Up @@ -74,7 +75,7 @@ def predict(args):

raw_heatmap_shown = np.maximum(0, heatmap_avg[:, :, 0:1] * 255)
heatmap_shown = cv2.applyColorMap(raw_heatmap_shown.astype(np.uint8), cv2.COLORMAP_JET)
img_with_heatmap = cv2.addWeighted(heatmap_shown, 0.5, img, 0.5, 0)
img_with_heatmap = cv2.addWeighted(heatmap_shown, 0.5, img_bgr, 0.5, 0)

cv2.imwrite('heatmap_shown.jpg', img_with_heatmap)

Expand Down Expand Up @@ -296,10 +297,8 @@ def predict(args):

if __name__ == '__main__':

# img_id = 196283
# img_id = 163640
img_id = 785
img_path = os.path.join('coco/val2017', '%012d.jpg' % img_id)
img_id = 524311
img_path = os.path.join('coco/train2017', '%012d.jpg' % img_id)

parser = argparse.ArgumentParser()
parser.add_argument('--model_path', help='path of model', required = True)
Expand Down
7 changes: 1 addition & 6 deletions reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def get_data(self):
# read img, mask, and label data
img_path = os.path.join(self.images_dir, '%012d.jpg' % img_id)
img = cv2.imread(img_path)
img = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

mask_path = os.path.join(self.masks_dir, "mask_miss_%012d.png" % img_id)
mask = cv2.imread(mask_path, 0)
Expand Down Expand Up @@ -358,8 +358,3 @@ def reset_state(self):

g = ds.get_data()
sample = next(g)
'''
for i in g:
img, heatmaps, pafs, mask_all = i
print(mask_all.shape)
'''
15 changes: 8 additions & 7 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from tensorpack.tfutils import optimizer, gradproc

from cfgs.config import cfg
from modules import VGGBlock_ours as VGGBlock, Stage1Block, StageTBlock
from modules import VGGBlock_official as VGGBlock, Stage1Block, StageTBlock
from reader import Data

def apply_mask(t, mask):
Expand All @@ -26,7 +26,8 @@ def image_preprocess(image, bgr=True):
if bgr == False:
mean = mean[::-1]
image_mean = tf.constant(mean, dtype=tf.float32)
image = image - image_mean
image = (image - image_mean) / (255 / 2)
# image = image - image_mean
return image

class Model(ModelDesc):
Expand Down Expand Up @@ -101,7 +102,7 @@ def _build_graph(self, inputs):
wd_cost = tf.constant(0.0)
loss1_total = tf.add_n(loss1_list)
loss2_total = tf.add_n(loss2_list)
loss_total = loss1 + loss2
loss_total = loss1_total + loss2_total
self.cost = tf.add_n([loss_total, wd_cost], name='cost')


Expand All @@ -113,9 +114,9 @@ def _build_graph(self, inputs):

add_moving_summary(self.cost)
for idx, loss1 in enumerate(loss1_list):
add_moving_summary(tf.identity(loss1_list[idx], name='stage%d_L1_loss' % idx))
add_moving_summary(tf.identity(loss1_list[idx], name='stage%d_L1_loss' % (idx+1)))
for idx, loss2 in enumerate(loss2_list):
add_moving_summary(tf.identity(loss2_list[idx], name='stage%d_L2_loss' % idx))
add_moving_summary(tf.identity(loss2_list[idx], name='stage%d_L2_loss' % (idx+1)))
add_moving_summary(tf.identity(loss1_total, name = 'L1_loss'))
add_moving_summary(tf.identity(loss2_total, name = 'L2_loss'))

Expand Down Expand Up @@ -181,8 +182,8 @@ def get_config(args):
dataflow = ds_train,
callbacks = [
ModelSaver(),
PeriodicTrigger(InferenceRunner(ds_val, [ScalarStats('cost')]),
every_k_epochs=3),
# PeriodicTrigger(InferenceRunner(ds_val, [ScalarStats('cost')]),
# every_k_epochs=3),
HumanHyperParamSetter('learning_rate'),
],
model = Model(),
Expand Down

0 comments on commit 033ccdc

Please sign in to comment.