def train_body(): train_images = help_functions.load_all_images( config.train_body_folder, preprocess=help_functions.prepare_body) # Hyper-parameters people_count = 8 iterations = 60000 checkpoint = 20 save_checkpoint = 10000 backend.clear_session() model = get_body_model((config.body_image_resize[1], config.body_image_resize[0], 3)) f = open(os.path.join('model_history', 'body_perf.txt'), 'a') # print(model.summary()) for i in range(1, iterations+1): inputs, targets = get_image_pairs(train_images, people_count) (loss, acc) = model.train_on_batch(inputs, targets) if i % checkpoint == 0: logging.info('Iteration: {}'.format(i)) logging.info('Loss: {}'.format(loss)) logging.info('Accuracy: {}'.format(acc)) f.write(str(i) + ' ' + str(loss) + ' ' + str(acc) + '\n') if i % save_checkpoint == 0: model.save_weights(os.path.join('model_history', 'base_body_weights_it{}.h5'.format(i))) model.save_weights(os.path.join('model_history', 'base_body_weights.h5')) f.close()
def test_body(model_file, image_folder=config.test_body_folder): test_images = help_functions.load_all_images( image_folder, preprocess=help_functions.prepare_body) model = get_body_model( (config.body_image_resize[1], config.body_image_resize[0], 3)) model.load_weights(filepath=model_file) print('body') rates = np.array([0, 0, 0, 0]) for i in range(100): inputs, targets = train_help_functions.get_image_pairs(test_images, 10) predicted = model.predict_on_batch(inputs) rates += calc_rates(predicted, targets) print_rates(rates)
def test_body_oneshot(model_file, iterations=10, versus=4): test_images = help_functions.load_all_images( config.test_body_folder, preprocess=help_functions.prepare_body) model = get_body_model( (config.body_image_resize[1], config.body_image_resize[0], 3)) model.load_weights(filepath=model_file) matched = 0 for i in range(iterations): inputs, targets = train_help_functions.get_oneshot_pair( test_images, versus) if targets.shape[0] == 0: continue result = model.predict_on_batch(inputs) matched += np.argmax(result) == np.argmax(targets) print('Oneshot body:', float(matched) / float(iterations), 'vs', versus)
def improve_bodies(): # same person in different folders might have different ID, causing false negatives if we connected them train_images = [] # load images from each folder for group_name in config.improve_camera_groups: images = help_functions.load_all_images( os.path.join(config.improve_folder, group_name, 'bodies'), file_type='.jpg', preprocess=help_functions.prepare_body) train_images.append(images) # hyper-parameters people_count = 8 iterations = 8000 checkpoint = 2 save_checkpoint = 5000 model = siamese_network.get_body_model( (config.body_image_resize[1], config.body_image_resize[0], 3)) # are we improving base model of already improved model? # load weight for model we are improving if config.learning_start: model.load_weights(filepath=config.base_body_model) elif config.learning_improving: model.load_weights(filepath=config.improved_body_model) f = open(os.path.join('model_history', 'body_improve_perf.txt'), 'a') logging.info('IMPROVING: Starting to improve model for bodies') for i in range(1, iterations + 1): inputs, targets = get_image_pairs( train_images[np.random.randint(0, len(train_images))], people_count) (loss, acc) = model.train_on_batch(inputs, targets) if i % checkpoint == 0: logging.info('Iteration: {}'.format(i)) logging.info('Loss: {}'.format(loss)) logging.info('Accuracy: {}'.format(acc)) f.write(str(i) + ' ' + str(loss) + ' ' + str(acc) + '\n') if i % save_checkpoint == 0: model.save_weights(os.path.join('model_history', str(i) + 'FBI.h5')) f.flush() model.save_weights(config.improved_body_model) f.close()
import numpy as np import logging import cv2 import siamese_network import config body_model = siamese_network.get_body_model() face_model = siamese_network.get_face_model() if config.learning_start: body_model.load_weights(filepath=config.base_body_model) face_model.load_weights(filepath=config.base_face_model) elif config.learning_improving or config.production: body_model.load_weights(filepath=config.improved_body_model) face_model.load_weights(filepath=config.improved_face_model) def confirm_match(candidate_track, same_track): while True: face1 = candidate_track.get_face_images() face2 = same_track.get_face_images() body1 = candidate_track.get_body_images() body2 = same_track.get_body_images() showed = False if body1.shape[0] > 0 and body2.shape[0] > 0: body1 = body1[np.random.randint(0, body1.shape[0])] body2 = body2[np.random.randint(0, body2.shape[0])] cv2.imshow('candidate', body1.astype(np.uint8)) cv2.imshow('matched to', body2.astype(np.uint8))