def test_model(net, name): print(' ------ testing ------') os.makedirs(V.experiments_folder + "/keras/" + name + '/Test/') data = Data(V.images_test_dir, V.labels_test_txt) images, labels = data.generator(4450).__next__() decoded_label = data.decode_labels(labels, depad=True) prediction = net.predict(images) argmax_prediction = data.pred2OneHot(prediction) decoded_prediction = data.decode_labels(argmax_prediction, depad=True) with open(V.experiments_folder + "/keras/" + name + '/Test/predictions.csv', 'w') as f: fieldnames = ['label', 'prediction', 'error'] writer = csv.DictWriter(f, fieldnames=fieldnames) writer.writeheader() for l, p in zip(decoded_label, decoded_prediction): writer.writerow({'label': l, 'prediction': p, 'error': CER(l, p)}) cross_val = net.evaluate(images, labels, verbose=False) label_error = [CER(l, p) for l, p in zip(decoded_label, decoded_prediction)] label_error_mean = np.sum(label_error) / len(label_error) word_error = [0 if cer == 0 else 1 for cer in label_error] word_error_mean = np.mean(word_error) with open(V.experiments_folder + "/keras/" + name + '/Test/loss.csv', 'w') as f: fieldnames = ['name', 'value'] writer = csv.DictWriter(f, fieldnames=fieldnames) writer.writeheader() writer.writerow({'name': 'cross-entropie', 'value': cross_val}) writer.writerow({'name': 'label error', 'value': label_error_mean}) writer.writerow({'name': 'word error', 'value': word_error_mean})
def see_animation(name): data = Data(V.images_train_dir, V.labels_train_txt) net = load_xp_model(name) net_attention = create_net_attention_maps(net, name) images, labels = data.generator(1).__next__() preds = data.decode_labels(data.pred2OneHot(net.predict(images))) atts = net_attention.predict(images) im_index = 0 image = images[im_index, :, :, 0] pred = preds[im_index] att = atts[im_index, :, :, 0] ratio = image.shape[0] / att.shape[0] list_images_att = [] for a in att: image_att = np.copy(image) for x in range(image.shape[0]): image_att[x, :] = image_att[x, :] * a[int(x / ratio)] list_images_att.append( [imshow(np.rot90(image_att, k=1), animated=True)]) list_images_att.append( [imshow(np.rot90(np.zeros((384, 28)), k=1), animated=True)]) fig = figure() ani = animation.ArtistAnimation(fig, list_images_att, interval=800, blit=True, repeat_delay=6e5) show()
def maps(name): data = Data(V.images_train_dir, V.labels_train_txt) net = load_xp_model(name) net_attention = create_net_attention_maps(net, name) images, labels = data.generator(10).__next__() preds = data.decode_labels(data.pred2OneHot(net.predict(images))) atts = net_attention.predict(images) panel = np.zeros((35, 35, 3), dtype=np.int) att = atts[0, :, :, 0] x = 4 y = 2 panel[x:x + att.shape[0], y:y + att.shape[1], 1] = att * 255 / np.max(att) panel = cv2.resize(panel, (500, 500)) cv2.imwrite('experiments/' + name + '/panel.jpg', panel) print('fin')