Skip to content

Instantly share code, notes, and snippets.

@salihkaragoz
Last active December 15, 2017 11:41
Show Gist options
  • Save salihkaragoz/45a6861a04963036055cf0ed29c9771f to your computer and use it in GitHub Desktop.
Save salihkaragoz/45a6861a04963036055cf0ed29c9771f to your computer and use it in GitHub Desktop.
Training
import numpy as np
from pycocotools.coco import COCO
import os
import math
import keras
from keras.models import Sequential, Model
from keras.layers import Dense, Activation, Input, Flatten, Dropout
from keras.utils import plot_model
import cv2
import matplotlib.pyplot as plt
def bbox_generator():
weights = np.zeros((90, 140, 17))
output = np.zeros((90, 140, 17))
coco = COCO(os.path.join('', 'annotations', 'person_keypoints_val2017.json'))
thres = 0.05
ann_ids = coco.getAnnIds()
while 1:
#for i in range(1875):
# if i%125==0:
# print "i = " + str(i)
# yield
print "listen up"
for i in range(len(ann_ids)):
print i
ann_data = coco.loadAnns(2203726)[0]
# ann_ids[i]
bbox = ann_data['bbox']
x0 = int(bbox[0])
y0 = int(bbox[1])
x = float(bbox[2])
y = float(bbox[3])
xscale = 90.0 / math.ceil(x)
yscale = 140.0 / math.ceil(y)
kpx = ann_data['keypoints'][0::3]
kpy = ann_data['keypoints'][1::3]
kpv = ann_data['keypoints'][2::3]
for xy in range(17):
if kpv[xy] > 0:
a = int(round(kpx[xy] - x0) * xscale)
b = int(round(kpy[xy] - y0) * yscale)
if a >= 90 and b >= 140:
weights[89][139][xy] = 1
continue
if a >= 90:
weights[89][b][xy] = 1
if b >= 140:
weights[a][139][xy] = 1
else:
output[a, b, xy] = 1
img_id = ann_data['image_id']
img_data = coco.loadImgs(img_id)[0]
ann_data = coco.loadAnns(coco.getAnnIds(img_data['id']))
for a in range(len(ann_data)):
kpx = ann_data[a]['keypoints'][0::3]
kpy = ann_data[a]['keypoints'][1::3]
kpv = ann_data[a]['keypoints'][2::3]
for x in range(17):
if kpv[x] > 0:
if (kpx[x] > bbox[0] - bbox[2] * thres and kpx[x] < bbox[0] + bbox[2] * (1 + thres)):
if (kpy[x] > bbox[1] - bbox[3] * thres and kpy[x] < bbox[1] + bbox[3] * (1 + thres)):
a = int(round(kpx[x] - x0) * xscale)
b = int(round(kpy[x] - y0) * yscale)
#print(ann_data[i]['image_id'])
if a >= 90 and b >= 140:
weights[89][139][x] = 1
print(x0, xscale, bbox[2], a, b)
continue
if a >= 90:
weights[89][b][x] = 1
if b >= 140:
weights[a][139][x] = 1
if a < 90 and b < 140:
weights[a][b][x] = 1
yield np.expand_dims(weights,axis=0), np.expand_dims(output,axis=0)
#model = Sequential()
#inputs = Input(shape=(214200))
visible = Input(shape=(90,140,17))
#flatten = Flatten()(visible)
hidden1 = Dense(17, activation='relu')(visible)
#flatten2 = Flatten()(hidden1)
#output = Dense(107100, activation='relu')(flatten2)
model = Model(inputs=visible, outputs=hidden1)
# Summarize Layers
print(model.summary())
# Plot Graph
plot_model(model, to_file='test.png')
model.compile(loss='categorical_crossentropy', optimizer='sgd')
model.fit_generator(bbox_generator(), samples_per_epoch = 6000, nb_epoch=10, verbose=2,callbacks=[], validation_data=None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment