def append_dmap(self, annos, container, length): k = util.get_key_hmap((length, length), annos, self.patch, self.patch_l // 2) tmp, tmp_re = util.get_dir_hmap((length, length), annos, self.ones, self.limbs, self.patch_l // 2) d = util.weight_dir_hmap(k, tmp, tmp_re, self.limbs) container.append(d)
def next_batch(self): start = self.index end = self.index + self.batch_size if end >= self.volumn: end = self.volumn self.index = 0 else: self.index = end data_batch = self.data[start:end] img = [] keypoint_hmap = [] direction_hmap = [] for piece in data_batch: tmp = misc.imread(self.img_dir + piece['image_id'] + '.jpg') tmp, rate, left, top = \ util.resize(tmp, self.length) annos = np.array(list(piece['keypoint_annotations'].values()), dtype=np.float16) annos[:, ::3] = annos[:, ::3] * rate - left annos[:, 1::3] = annos[:, 1::3] * rate - top annos = annos.astype(np.int16) img.append(tmp) tmp = misc.imresize(tmp, (self.short, self.short)) # annos = np.round(annos * self.short / self.length).astype(np.int8) rate = self.short / self.length annos[:, ::3] = annos[:, ::3] * rate annos[:, 1::3] = annos[:, 1::3] * rate annos = np.round(annos).astype(np.int8) # print(annos) kmap = util.get_key_hmap(\ tmp.shape, annos, self.patch, self.patch_l//2) keypoint_hmap.append(kmap) tmp_dmap, tmp_dmap_re = util.get_dir_hmap(\ tmp.shape, annos, self.ones, self.limbs, self.patch_l//2) dmap = util.weight_dir_hmap(kmap, tmp_dmap, tmp_dmap_re, self.limbs) # use forward only dmap = dmap[:, :, len(self.limbs) * 2:] direction_hmap.append(dmap) img = np.array(img, dtype=np.float32) img -= self.mean img /= self.var keypoint_hmap = np.array(keypoint_hmap, dtype=np.float32) direction_hmap = np.array(direction_hmap, dtype=np.float32) return img, keypoint_hmap, direction_hmap
def next_batch(self): start = self.index end = self.index + self.batch_size if end >= self.volumn: end = self.volumn self.index = 0 else: self.index = end data_batch = self.data[start:end] img = [] keypoint_hmap = [] affinity_hmap = [] names = [] for piece in data_batch: tmp = misc.imread(self.img_dir + piece['image_id'] + '.jpg') names.append(piece['image_id']) tmp, rate, left, top = \ util.resize(tmp, self.length) annos = np.array(list(piece['keypoint_annotations'].values()), dtype=np.float16) annos[:, ::3] = annos[:, ::3] * rate - left annos[:, 1::3] = annos[:, 1::3] * rate - top annos = annos.astype(np.int16) img.append(tmp) tmp = misc.imresize(tmp, (self.short, self.short)) rate = self.short / self.length annos[:, ::3] = annos[:, ::3] * rate annos[:, 1::3] = annos[:, 1::3] * rate annos = np.round(annos).astype(np.int8) keypoint_hmap.append( util.get_key_hmap(tmp.shape, annos, self.patch, self.patch_l // 2, strict=True)) affinity_hmap.append( util.get_aff_hmap(tmp.shape, annos, self.limbs, strict=True)) img = np.array(img, dtype=np.float32) img -= self.mean img /= self.var keypoint_hmap = np.array(keypoint_hmap, dtype=np.float32) affinity_hmap = np.array(affinity_hmap, dtype=np.float32) return img, keypoint_hmap, affinity_hmap, names
def next_batch(self): start = self.index end = self.index + self.batch_size if end >= self.volumn: end = self.volumn self.index = 0 else: self.index = end data_batch = self.data[start:end] img = [] keypoint_hmap = [] direction_hmap = [] names = [] lables = [] link_kmaps = [] for piece in data_batch: tmp = misc.imread(self.img_dir + piece['image_id'] + '.jpg') names.append(piece['image_id']) # resize the img and annotation to 368x368 tmp, rate, left, top = \ util.resize(tmp, self.length) annos = np.array(list(piece['keypoint_annotations'].values()), dtype=np.float16) annos[:, ::3] = annos[:, ::3] * rate - left annos[:, 1::3] = annos[:, 1::3] * rate - top annos = annos.astype(np.int16) img.append(tmp) # resize the img and annotations to 46x46 tmp = misc.imresize(tmp, (self.short, self.short)) rate = self.short / self.length annos[:, ::3] = annos[:, ::3] * rate annos[:, 1::3] = annos[:, 1::3] * rate annos = np.round(annos).astype(np.int8) kmap = util.get_key_hmap(\ tmp.shape, annos, self.patch, self.patch_l//2) keypoint_hmap.append(kmap) human_num = len(annos) failed = True # since many img contains only one people, we manually # reduce the possibility to get the positive sample random.shuffle(self.limb_index) if np.random.random_sample() < 0.15 or human_num == 1: # extract from the same people for limb in self.limb_index: start = self.limbs[limb][0] end = self.limbs[limb][1] human = annos[np.random.randint(human_num)] start_x = human[start * 3] start_y = human[start * 3 + 1] start_v = human[start * 3 + 2] end_x = human[end * 3] end_y = human[end * 3 + 1] end_v = human[end * 3 + 2] if util.validate(start_x, start_y, start_v, self.short, self.short) \ and util.validate(end_x, end_y, end_v, self.short, self.short): kmap_start = util.get_single_kmap( (self.short, self.short), start_x, start_y, self.patch_l // 2, self.patch) kmap_end = util.get_single_kmap( (self.short, self.short), end_x, end_y, self.patch_l // 2, self.patch) link_kmaps.append( np.stack((kmap_start, kmap_end), axis=-1)) failed = False break if failed: link_kmaps.append(np.zeros((self.short, self.short, 2))) print('no validate limb in %s (same)' % piece['image_id']) lables.append(1) else: # extract from two different humen for limb in self.limb_index: start = self.limbs[limb][0] end = self.limbs[limb][1] index1 = np.random.randint(human_num) human1 = annos[index1] start_x = human1[start * 3] start_y = human1[start * 3 + 1] start_v = human1[start * 3 + 2] index2 = np.random.randint(human_num) human2 = annos[index2] end_x = human2[end * 3] end_y = human2[end * 3 + 1] end_v = human2[end * 3 + 2] if util.validate(start_x, start_y, start_v, self.short, self.short) \ and util.validate(end_x, end_y, end_v, self.short, self.short) \ and index1 != index2: kmap_start = util.get_single_kmap( (self.short, self.short), start_x, start_y, self.patch_l // 2, self.patch) kmap_end = util.get_single_kmap( (self.short, self.short), end_x, end_y, self.patch_l // 2, self.patch) link_kmaps.append( np.stack((kmap_start, kmap_end), axis=-1)) lables.append(0) failed = False break if failed: link_kmaps.append(np.zeros((self.short, self.short, 2))) print('no validate limb in %s (differen)' % piece['image_id']) lables.append(0) tmp_dmap, tmp_dmap_re = util.get_dir_hmap(\ tmp.shape, annos, self.ones, self.limbs, self.patch_l//2) dmap = util.weight_dir_hmap(kmap, tmp_dmap, tmp_dmap_re, self.limbs) direction_hmap.append(dmap) img = np.array(img, dtype=np.float32) img -= self.mean img /= self.var keypoint_hmap = np.array(keypoint_hmap, dtype=np.float32) direction_hmap = np.array(direction_hmap, dtype=np.float32) lables = np.array(lables, dtype=np.float32) link_kmaps = np.array(link_kmaps, dtype=np.float32) return img, keypoint_hmap, direction_hmap, names, lables, link_kmaps
for k in range(14): if k in h.keys(): tmp.append(int(round(h[k][0] / rate))) tmp.append(int(round(h[k][1] / rate))) tmp.append(1) else: tmp.append(0) tmp.append(0) tmp.append(0) ret['keypoint_annotations']['human%d'%(cnt+1)] = tmp return ret if __name__ == '__main__': dmap = np.load('dmap.npy') kmap = util.get_kmap_from_dmap(dmap, util.get_limbs()) humans = reconstruct(dmap, kmap, 5) annos = format(humans, 'sample', 0.05958549222797927461139896373057) # print(annos) for i in range(len(annos['keypoint_annotations'])): k_rev = util.get_key_hmap((772, 950), [annos['keypoint_annotations']['human%d'%(i+1)]], util.get_patch(40,32), r=20) src = misc.imread('ffa97d027dfc2f2fc62692a035535579c5be74e0.jpg') util.cover_key_map(src, k_rev) misc.imsave('vis_anno_%d.jpg'%i, src) # outstander, layer = find_outstander(kmap, 10) # print(layer) # print(outstander)
batch_k, batch_a = sess.run([kmaps, amaps], feed_dict={inflow: imgs}) batch_k = batch_k[-1] batch_a = batch_a[-1] k = util.concat_maps(batch_k, lefts, tops, 8) a = util.concat_maps(batch_a, lefts, tops, 8) humans = inf_util_ka.reconstruct(a, k, 10) annos = inf_util_ka.format(humans, name.split('.')[0], rate) result.append(annos) if vis_anno: for human, ann in annos['keypoint_annotations'].items(): k_rev = util.get_key_hmap((h, w), [ann], util.get_patch(40, 64), r=20) src = misc.imread(test_path + name) util.cover_key_map(src, k_rev) misc.imsave('%s_%s.jpg' % (annos['image_id'], human), src) if save: np.save(annos['image_id'] + '_k.npy', k) np.save(annos['image_id'] + '_a.npy', a) toc = time.time() cnt += 1 interval = toc - tic elapse += interval if cnt == 2: