def learning(train_set, dataset_path, lbl_train, neural_nets, nn_for_learn, indexes, debug=False): if nn_for_learn[NET_12]: if debug: print("First network learning") for i in range(0, indexes[NET_12]): if debug: print(i) all_images, all_labels, coordinates = prepare_images.prepare(dataset_path + train_set[i].decode('utf8'), lbl_train[i], debug=debug) if debug: print("Image prepared") images = all_images[all_labels == 1] labels = np.ones(images.shape[0]) neg_size = int(labels.shape[0] * NEGATIVE_MULTIPLIER) neg_indexes = np.random.choice(np.arange(all_images.shape[0] * all_images.shape[1]), neg_size, replace=False) neg_indexes = np.unravel_index(neg_indexes, (all_images.shape[0], all_images.shape[1])) neg_labels = all_labels[neg_indexes] neg_images = all_images[neg_indexes] images = np.concatenate((images, neg_images)) labels = np.concatenate((labels, neg_labels)) if debug: print("images.shape, labels.shape") print(images.shape, labels.shape) neural_nets[NET_12].learning(dataset=convert48to12(images), labels=labels, debug_print=debug, n_epochs=5) if nn_for_learn[NET_24]: if debug: print("Second network learning") for i in range(indexes[NET_24]): all_images, all_labels, coordinates = prepare_images.prepare(dataset_path + train_set[i].decode('utf8'), lbl_train[i], debug=debug) predicted_labels = neural_nets[NET_12].predict(all_images) images = all_images[predicted_labels == 1] labels = all_labels[predicted_labels == 1] nn_for_learn[NET_24].learning(dataset=convert48to24(images), labels=labels, debug_print=debug, n_epochs=10) if nn_for_learn[NET_48]: if debug: print("Second network learning") for i in range(indexes[NET_48]): all_images, all_labels, coordinates = prepare_images.prepare(dataset_path + train_set[i].decode('utf8'), lbl_train[i], debug=debug) predicted_labels = neural_nets[NET_12].predict(all_images) images = all_images[predicted_labels == 1] predicted_labels = neural_nets[NET_24].predict(images) images = all_images[predicted_labels == 1] labels = all_labels[predicted_labels == 1] nn_for_learn[NET_48].learning(dataset=convert48to24(images), labels=labels, debug_print=debug, n_epochs=10)
def testing_results(neural_nets, nn_params, nn_for_test, test_set_without_negatives, numbers_of_test_imgs=10): first_net, second_net = neural_nets[:2] first_net.load_params(nn_params[0]) if nn_for_test[1]: second_net.load_params(nn_params[1]) print("Testing...") test_img = np.array(list(test_set_without_negatives.keys())[:numbers_of_test_imgs]) lbl_test = np.array([test_set_without_negatives.get(key).get_coordinates() for key in test_img]) tp_all = np.zeros(numbers_of_test_imgs) tn_all = np.zeros(numbers_of_test_imgs) fp_all = np.zeros(numbers_of_test_imgs) fn_all = np.zeros(numbers_of_test_imgs) f1_score_all = np.zeros(numbers_of_test_imgs) tn_percent_all = np.zeros(numbers_of_test_imgs) for i in range(test_img.shape[0]): imgs, lbls, coords = prepare_images.prepare(DATASET_PATH + test_img[i].decode('utf8'), lbl_test[i]) y_pred = np.zeros_like(lbls) for j in range(imgs.shape[0]): # TODO добавить nms в цепочку y_pred[j] = first_net.predict(nn.convert48to24(imgs[j])) if nn_for_test[1]: tmp = imgs[y_pred == 1] y_pred[y_pred == 1] = second_net.predict(tmp) tmp = lbls - y_pred tp = np.sum((y_pred == 1) & (lbls == 1)) tn = np.sum((y_pred == 0) & (lbls == 0)) fp = np.sum(tmp == -1) fn = np.sum(tmp == 1) f1_score = 2 * tp / (2 * tp + fn + fp) tp_all[i] = tp tn_all[i] = tn fp_all[i] = fp fn_all[i] = fn f1_score_all[i] = f1_score print(" f1 score = {}, true positive = {}, true negative = {}, false positive = {}, false negative = {}" .format(f1_score, tp, tn, fp, fn)) tn_percent = tn / (tn + fp) * 100 tn_percent_all[i] = tn_percent print("True negative percent from all negatives = {}".format(tn_percent)) tmp = np.arange(lbls.shape[0] * lbls.shape[1]).reshape(lbls.shape) tmp = tmp[y_pred == 1] rects = [coords.get(key, None) for key in tmp] prepare_images.save_img_with_rectangles(DATASET_PATH, test_img[i].decode('utf8'), rects) # prepare_images.show_rectangles(dataset_path + test_img[i].decode('utf8'), rects, show_type='opencv') # rects = prepare_images.nms(rects, 0.3) # prepare_images.show_rectangles(dataset_path + test_img[i].decode('utf8'), rects, show_type='opencv') print("f1 score = {}, true positive = {}, true negative = {}, false positive = {}, false negative = {}" .format(f1_score_all.mean(), tp_all.mean(), tn_all.mean(), fp_all.mean(), fn_all.mean())) print("True negative percent from all negatives = {}".format(tn_percent_all.mean())) return tp_all, tn_all, fp_all, fn_all, f1_score_all, tn_percent_all