def character_classification(): print('Loading data...') x, y = load_data_chars() print('Processing data..') print('Training data shape: ', x.shape) print('Test data shape: ', y.shape) plots.plot_filters(x[0]) SVM.svm(x, y) Naive_Bayes.naive_bayes(x, y) KNN.knn(x, y) CNN.fit_cnn(x, y, trials=1, network_type='simple')
ax[0, 0].axis('off') ax[0, 0].set_title('Image', fontsize=10) ax[0, 1].imshow(add_edge_detection_filter(img), cmap='gray') ax[0, 1].axis('off') ax[0, 1].set_title('Edge detection filter', fontsize=10) ax[0, 2].imshow(add_otsu_filter(img), cmap='gray') ax[0, 2].axis('off') ax[0, 2].set_title('Otsu filter', fontsize=10) ax[1, 0].imshow(add_gaussian_filter(img), cmap='gray') ax[1, 0].axis('off') ax[1, 0].set_title('Gaussian filter', fontsize=10) ax[1, 1].imshow(add_bilateral_filter(img), cmap='gray') ax[1, 1].axis('off') ax[1, 1].set_title('Bilateral filter', fontsize=10) ax[1, 2].imshow(add_median_filter(img), cmap='gray') ax[1, 2].axis('off') ax[1, 2].set_title('Median filter', fontsize=10) fig.tight_layout() plt.show() if __name__ == '__main__': x, y = load_data_chars() plot_filters(x[2])
print('=== Convolution Neural Network ===') test_accuracy = np.zeros(trials) running_time = np.zeros(trials) x = scale_input(x) x = grey_scale(x) x = add_dimension(x) x, y = add_pictures_without_chars(x, y) y = to_categorical(y, int(np.max(y) + 1)) for i in range(trials): print('Training network ', i + 1) start = time.time() random_state = 100 + i x_train, x_test, y_train, y_test = train_test_split( x, y, test_size=0.2, random_state=random_state, stratify=y) network = CNN(num_classes=len(y[0]), sample=x_train[0], network_type=network_type) network.train_generator(x_train, y_train, model_weights) test_accuracy[i] = network.test(x_test, y_test, model_weights) running_time[i] = time.time() - start print('Running time: ', running_time[i]) print('Average test accuracy over ', trials, ' trials: ', np.mean(test_accuracy)) print('Average running time over ', trials, ' trials: ', np.mean(running_time)) if __name__ == "__main__": img, target = load_data_chars() fit_cnn(img, target, trials=1)