{"id":37163441,"url":"https://github.com/joaopauloschuler/k-neural-api","last_synced_at":"2026-01-14T19:26:29.812Z","repository":{"id":37397016,"uuid":"221215803","full_name":"joaopauloschuler/k-neural-api","owner":"joaopauloschuler","description":"K-CAI NEURAL API - Keras based neural network API that will allow you to create parameter-efficient, memory-efficient, flops-efficient multipath models with new layer types. There are plenty of examples and documentation.","archived":false,"fork":false,"pushed_at":"2024-08-16T19:06:32.000Z","size":16044,"stargazers_count":136,"open_issues_count":4,"forks_count":104,"subscribers_count":28,"default_branch":"master","last_synced_at":"2025-05-17T09:47:45.668Z","etag":null,"topics":["keras-classification-models","keras-deep-dream","keras-deep-learning","keras-examples","keras-generators","keras-image-augmentation","keras-image-classifier","keras-implementations","keras-layer","keras-models","keras-neural-network","keras-neural-networks","keras-tensorflow","keras-tutorial","keras-tutorials","keras-visualization","machine-learning-api","parameter-efficient","parameter-efficient-learning"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joaopauloschuler.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-11-12T12:44:50.000Z","updated_at":"2025-04-14T12:52:53.000Z","dependencies_parsed_at":"2023-01-30T00:45:52.552Z","dependency_job_id":"2a0a0a11-4ada-486b-b7b5-e7afe3dec982","html_url":"https://github.com/joaopauloschuler/k-neural-api","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/joaopauloschuler/k-neural-api","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joaopauloschuler%2Fk-neural-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joaopauloschuler%2Fk-neural-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joaopauloschuler%2Fk-neural-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joaopauloschuler%2Fk-neural-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joaopauloschuler","download_url":"https://codeload.github.com/joaopauloschuler/k-neural-api/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joaopauloschuler%2Fk-neural-api/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28432598,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T18:57:19.464Z","status":"ssl_error","status_checked_at":"2026-01-14T18:52:48.501Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["keras-classification-models","keras-deep-dream","keras-deep-learning","keras-examples","keras-generators","keras-image-augmentation","keras-image-classifier","keras-implementations","keras-layer","keras-models","keras-neural-network","keras-neural-networks","keras-tensorflow","keras-tutorial","keras-tutorials","keras-visualization","machine-learning-api","parameter-efficient","parameter-efficient-learning"],"created_at":"2026-01-14T19:26:28.929Z","updated_at":"2026-01-14T19:26:29.795Z","avatar_url":"https://github.com/joaopauloschuler.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# K-CAI NEURAL API [![VERSION](https://img.shields.io/github/v/release/joaopauloschuler/k-neural-api)](https://github.com/joaopauloschuler/k-neural-api/releases)[![DOI](https://zenodo.org/badge/221215803.svg)](https://zenodo.org/badge/latestdoi/221215803)\n\u003cimg align=\"right\" src=\"docs/cai.png\" height=\"192\"\u003e\n\nK-CAI NEURAL API is a Keras based neural network API that allows you to:\n* Create parameter-efficient neural networks: [V1](https://github.com/joaopauloschuler/kEffNetV1) and [V2](https://github.com/joaopauloschuler/kEffNetV2).\n* Create [noise-resistant neural networks](https://github.com/joaopauloschuler/two-path-noise-lab-plant-disease) for image classification and [achieve state-of-the-art classification accuracy](https://github.com/joaopauloschuler/two-branch-plant-disease).\n* Use an extremely well tested data augmentation wrapper for image classification (see `cai.util.create_image_generator` below).\n* Add [non-standard layers](https://github.com/joaopauloschuler/k-neural-api#new-layers) to your neural network.\n* Visualize [first layer filters](https://github.com/joaopauloschuler/k-neural-api#first-layer-filters), [activation maps](https://github.com/joaopauloschuler/k-neural-api#activation-maps), [heatmaps](https://github.com/joaopauloschuler/k-neural-api#heatmaps) ([see example](https://colab.research.google.com/github/joaopauloschuler/k-neural-api/blob/master/examples/jupyter/activation_map_heatmap_with_cifar10.ipynb)) and [gradient ascent](https://github.com/joaopauloschuler/k-neural-api#gradient-ascent--deep-dream) ([see example](https://colab.research.google.com/github/joaopauloschuler/k-neural-api/blob/master/examples/jupyter/cai_gradient_ascent.ipynb)).\n* [Prototype convolutional neural networks faster](https://github.com/joaopauloschuler/k-neural-api#quick-start-with-image-classification-on-your-own-web-browser) ([see example](https://colab.research.google.com/github/joaopauloschuler/k-neural-api/blob/master/examples/jupyter/simple_image_classification_with_any_dataset.ipynb)).\n* Save a [Tensorflow dataset](https://www.tensorflow.org/datasets) for image classification into a local folder structure: `cai.datasets.save_tfds_in_format`. See [example](https://colab.research.google.com/github/joaopauloschuler/k-neural-api/blob/master/examples/jupyter/k_cai_tfds_example.ipynb).\n\nThis project is a subproject from a bigger and older project called [CAI](https://sourceforge.net/projects/cai/) and is sister to the [free pascal](https://www.freepascal.org/) based [CAI NEURAL API](https://github.com/joaopauloschuler/neural-api/).\n\n## Prerequisites\nAll you need is [Keras](https://keras.io/), [python](https://www.python.org/) and [pip](https://pypi.org/project/pip/). Alternatively, if you prefer running on your [web browser](https://colab.research.google.com/github/joaopauloschuler/k-neural-api/blob/master/examples/jupyter/simple_image_classification_with_any_dataset.ipynb) without installing any software on your computer, you can run it on [Google Colab](https://colab.research.google.com/github/joaopauloschuler/k-neural-api/blob/master/examples/jupyter/simple_image_classification_with_any_dataset.ipynb). [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/joaopauloschuler/k-neural-api/blob/master/examples/jupyter/simple_image_classification_with_any_dataset.ipynb)\n\n### Quick Start with Image Classification on Your Own Web Browser\nFor a quick start, you can try the [Simple Image Classification with any Dataset](https://colab.research.google.com/github/joaopauloschuler/k-neural-api/blob/master/examples/jupyter/simple_image_classification_with_any_dataset.ipynb) example. This example shows how to create a model and train it with a dataset passed as parameter. Feel free to modify the parameters and to add/remove neural layers directly from your browser. [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/joaopauloschuler/k-neural-api/blob/master/examples/jupyter/simple_image_classification_with_any_dataset.ipynb)\n\n## Installation\n### Via Shell\nInstalling via shell is very simple:\n```\ngit clone https://github.com/joaopauloschuler/k-neural-api.git k\ncd k \u0026\u0026 pip install .\n```\n### Installing on Google Colab\nPlace this on the top of your Google Colab Jupyter Notebook:\n```\nimport os\n\nif not os.path.isdir('k'):\n  !git clone https://github.com/joaopauloschuler/k-neural-api.git k\nelse:\n  !cd k \u0026\u0026 git pull\n\n!cd k \u0026\u0026 pip install .\n```\n\n## Documentation\nThe documentation is composed by **examples** and **PyDoc**.\n\n### Image Classification Examples\nThese examples show how to train a neural network for the task of [image classification](https://towardsdatascience.com/wtf-is-image-classification-8e78a8235acb). Most examples train a neural network with the [CIFAR-10](https://en.wikipedia.org/wiki/CIFAR-10) or [CIFAR-100](https://en.wikipedia.org/wiki/CIFAR-10) datasets.\n* [Simple Image Classification with any Dataset](https://github.com/joaopauloschuler/k-neural-api/blob/master/examples/jupyter/simple_image_classification_with_any_dataset.ipynb): this example shows how to create a model and train it with a dataset passed as parameter. [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/joaopauloschuler/k-neural-api/blob/master/examples/jupyter/simple_image_classification_with_any_dataset.ipynb)\n* [DenseNet BC L40 with CIFAR-10](https://github.com/joaopauloschuler/k-neural-api/blob/master/examples/jupyter/cai_densenet_bc_l40_with_cifar_10.ipynb): this example shows how to create a densenet model with `cai.densenet.simple_densenet` and easily train it with `cai.datasets.train_model_on_cifar10`. [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/joaopauloschuler/k-neural-api/blob/master/examples/jupyter/cai_densenet_bc_l40_with_cifar_10.ipynb)\n* [DenseNet BC L40 with CIFAR-100](https://github.com/joaopauloschuler/k-neural-api/blob/master/examples/jupyter/cai_densenet_bc_l40_with_cifar_100.ipynb): this example shows how to create a densenet model with `cai.densenet.simple_densenet` and easily train it with `cai.datasets.train_model_on_dataset`. [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/joaopauloschuler/k-neural-api/blob/master/examples/jupyter/cai_densenet_bc_l40_with_cifar_100.ipynb)\n* [Experiment your own DenseNet Architecture](https://github.com/joaopauloschuler/k-neural-api/blob/master/examples/jupyter/densenet_with_cifar.ipynb): this example allows you to experiment your own DenseNet settings. [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/joaopauloschuler/k-neural-api/blob/master/examples/jupyter/densenet_with_cifar.ipynb)\n* [Saving a TensorFlow dataset into png files](https://colab.research.google.com/github/joaopauloschuler/k-neural-api/blob/master/examples/jupyter/k_cai_tfds_example.ipynb) so you can use the dataset with Keras image generator.\n\n### Advanced Image Classification Examples\nThese papers show how to create parameter-efficient models (source code is available):\n* [An Enhanced Scheme for Reducing the Complexity of Pointwise Convolutions in CNNs for Image Classification Based on Interleaved Grouped Filters without Divisibility Constraints](https://github.com/joaopauloschuler/kEffNetV2).\n* [Grouped Pointwise Convolutions Reduce Parameters in Convolutional Neural Networks](https://github.com/joaopauloschuler/kEffNetV1).\n* [Color-aware two-branch DCNN for efficient plant disease classification](https://github.com/joaopauloschuler/two-branch-plant-disease).\n* [Grouped Pointwise Convolutions Significantly Reduces Parameters in EfficientNet](https://github.com/joaopauloschuler/kEffNet).\n* [Making plant disease classification noise resistant](https://github.com/joaopauloschuler/two-path-noise-lab-plant-disease).\n\n### First Layer Filters\nThe [Heatmap and Activation Map with CIFAR-10](https://github.com/joaopauloschuler/k-neural-api/blob/master/examples/jupyter/activation_map_heatmap_with_cifar10.ipynb) example shows how to quickly display heatmaps (CAM), activation maps and first layer filters/patterns.\n\nThese are filter examples:\n\n\u003cp\u003e\u003cimg src=\"docs/cai-filters.png\"\u003e\u003c/img\u003e\u003c/p\u003e\n\nAbove image has been created with a code similar to this:\n\n```\nweights = model.get_layer('layer_name').get_weights()[0]\nneuron_patterns = cai.util.show_neuronal_patterns(weights, NumRows = 8, NumCols = 8, ForceCellMax = True)\n...\nplt.imshow(neuron_patterns, interpolation='nearest', aspect='equal')\n```\n\n### Activation Maps\nThese are activation map examples:\n\n\u003cp\u003e\u003cimg src=\"docs/cai-activations.png\"\u003e\u003c/img\u003e\u003c/p\u003e\n\nThe above shown activation maps have been created with a code similar to this:\n\n```\nconv_output = cai.models.PartialModelPredict(InputImage, model, 'layer_name', False)\n...\nactivation_maps = cai.util.slice_3d_into_2d(aImage=conv_output[0], NumRows=8, NumCols=8, ForceCellMax=True);\n...\nplt.imshow(activation_maps, interpolation='nearest', aspect='equal')\n```\n\n### Heatmaps\nThe following image shows a car (left - input sample), its heatmap (center) and both added together (right).\n\n\u003cp\u003e\u003cimg src=\"docs/cai-heatmap.png\"\u003e\u003c/img\u003e\u003c/p\u003e\n\nHeatmaps can be produced following this example:\n\n```\nheat_map = cai.models.calculate_heat_map_from_dense_and_avgpool(InputImage, image_class, model, pOutputLayerName='last_conv_layer', pDenseLayerName='dense')\n```\n\n### Gradient Ascent \u0026 Deep Dream\nWith **cai.gradientascent.run_gradient_ascent_octaves**, you can easily run gradient ascent to create Deep Dream like images:\n```\nbase_model = tf.keras.applications.InceptionV3(include_top=False, weights='imagenet')\npmodel = cai.models.CreatePartialModel(base_model, 'mixed3')\nnew_img = cai.gradientascent.run_gradient_ascent_octaves(img=original_img, partial_model=pmodel, low_range=-4, high_range=1)\nplt.figure(figsize = (16, 16))\nplt.imshow(new_img, interpolation='nearest', aspect='equal')\nplt.show()\n```\n\n\u003cp\u003e\u003cimg src=\"docs/park-ga.jpg\"\u003e\u003c/img\u003e\u003c/p\u003e\n\nAbove image was generated from:\n\n\u003cp\u003e\u003cimg src=\"https://github.com/joaopauloschuler/neural-api/blob/master/docs/park.jpg?raw=true\" width=714px\u003e\u003c/img\u003e\u003c/p\u003e\n\nThere is a ready to use example: [Gradient Ascent / Deep Dream Example](https://github.com/joaopauloschuler/k-neural-api/blob/master/examples/jupyter/cai_gradient_ascent.ipynb). [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/joaopauloschuler/k-neural-api/blob/master/examples/jupyter/cai_gradient_ascent.ipynb)\n\n### PyDoc\nAfter installing K-CAI, you can find documentation with:\n```\npython -m pydoc cai.datasets\npython -m pydoc cai.densenet\npython -m pydoc cai.layers\npython -m pydoc cai.models\npython -m pydoc cai.util\n```\n\n### Scientific Research\nThese papers were made with K-CAI API:\n* [An Enhanced Scheme for Reducing the Complexity of Pointwise Convolutions in CNNs for Image Classification Based on Interleaved Grouped Filters without Divisibility Constraints](https://www.researchgate.net/publication/363413038_An_Enhanced_Scheme_for_Reducing_the_Complexity_of_Pointwise_Convolutions_in_CNNs_for_Image_Classification_Based_on_Interleaved_Grouped_Filters_without_Divisibility_Constraints).\n* [Grouped Pointwise Convolutions Reduce Parameters in Convolutional Neural Networks](https://www.researchgate.net/publication/360226228_Grouped_Pointwise_Convolutions_Reduce_Parameters_in_Convolutional_Neural_Networks).\n* [Grouped Pointwise Convolutions Significantly Reduces Parameters in EfficientNet](https://www.researchgate.net/publication/355214501_Grouped_Pointwise_Convolutions_Significantly_Reduces_Parameters_in_EfficientNet).\n* [Reliable Deep Learning Plant Leaf Disease Classification Based on Light-Chroma Separated Branches](https://www.researchgate.net/publication/355215213_Reliable_Deep_Learning_Plant_Leaf_Disease_Classification_Based_on_Light-Chroma_Separated_Branches).\n* [Color-aware two-branch DCNN for efficient plant disease classification](https://www.researchgate.net/publication/361511874_Color-Aware_Two-Branch_DCNN_for_Efficient_Plant_Disease_Classification).\n\n## Feature List\n* A number of new layer types (see below).\n* `cai.util.create_image_generator`: this wrapper has extremely well tested default parameters for image classification data augmentation. For you to get a better image classification accuracy might be just a case of replacing your current data augmentation generator by this one. Give it a go!\n* `cai.util.create_image_generator_no_augmentation`: image generator for test datasets.\n* `cai.densenet.simple_densenet`: simple way to create DenseNet models. See [example](https://github.com/joaopauloschuler/k-neural-api/blob/master/examples/jupyter/cai_densenet_bc_l40_with_cifar_10.ipynb).\n* `cai.datasets.load_hyperspectral_matlab_image`: downloads (if required) and loads hyperspectral image from a matlab file. This function has been tested with [AVIRIS](http://www.ehu.eus/ccwintco/index.php/Hyperspectral_Remote_Sensing_Scenes) and ROSIS sensor data stored as a matlab files.\n* `cai.models.calculate_heat_map_from_dense_and_avgpool`: calculates a class activation mapping (CAM) inspired on the paper [Learning Deep Features for Discriminative Localization](https://arxiv.org/abs/1512.04150) (see example below).\n* `cai.util.show_neuronal_patterns`: creates an array for visualizing first layer neuronal filters/patterns (see example below).\n* `cai.models.CreatePartialModel(pModel, pOutputLayerName, hasGlobalAvg=False)`: creates a partial model up to the layer name defined in pOutputLayerName.\n* `cai.models.CreatePartialModelCopyingChannels(pModel, pOutputLayerName, pChannelStart, pChannelCount)`: creates a partial model up to the layer name defined in pOutputLayerName and then copies channels starting from pChannelStart with pChannelCount channels.\n* `cai.models.CreatePartialModelFromChannel(pModel, pOutputLayerName, pChannelIdx)`: creates a partial model up to the layer name defined in pOutputLayerName and then copies the channel at index pChannelIdx. Use it in combination with `cai.gradientascent.run_gradient_ascent_octaves` to run gradient ascent from a specific channel or neuron.\n* `cai.models.CreatePartialModelWithSoftMax(pModel, pOutputLayerName, numClasses, newLayerName='k_probs')`: creates a partial model up to the layer name defined in pOutputLayerName and then adds a dense layer with softmax. This method was built to be used for image classification with transfer learning.\n* `cai.gradientascent.run_gradient_ascent_octaves`: allows visualizing patterns recognized by inner neuronal layers. See [example](https://colab.research.google.com/github/joaopauloschuler/k-neural-api/blob/master/examples/jupyter/cai_gradient_ascent.ipynb). Use it in combination with `cai.models.CreatePartialModel`, `cai.models.CreatePartialModelCopyingChannels` or `cai.models.CreatePartialModelFromChannel`.\n* `cai.datasets.save_tfds_in_format`: saves a TensorFlow dataset as image files. Classes are folders. See [example](https://colab.research.google.com/github/joaopauloschuler/k-neural-api/blob/master/examples/jupyter/k_cai_tfds_example.ipynb).\n* `cai.datasets.load_images_from_folders`: practical way to load small datasets into memory. It supports smart resizing, LAB color encoding and bipolar inputs.\n\n## New Layers\n* `cai.layers.ConcatNegation`: concatenates the input with its negation (input tensor multiplied by -1).\n* `cai.layers.CopyChannels`: copies a subset of the input channels.\n* `cai.layers.EnforceEvenChannelCount`: enforces that the number of channels is even (divisible by 2).\n* `cai.layers.FitChannelCountTo`: forces the number of channels to fit a specific number of channels. The new number of channels must be bigger than the number of input channels. The number of channels is fitted by concatenating copies of existing channels.\n* `cai.layers.GlobalAverageMaxPooling2D`: adds both global Average and Max poolings. `cai.layers.GlobalAverageMaxPooling2D` speeds up training when used as a replacement for standard average pooling and max pooling.\n* `cai.layers.InterleaveChannels`: interleaves channels stepping according to the number passed as parameter.\n* `cai.layers.kPointwiseConv2D`: parameter-efficient pointwise convolution as shown in the papers [Grouped Pointwise Convolutions Reduce Parameters in Convolutional Neural Networks](https://github.com/joaopauloschuler/kEffNetV1) and [An Enhanced Scheme for Reducing the Complexity of Pointwise Convolutions in CNNs for Image Classification Based on Interleaved Grouped Filters without Divisibility Constraints](https://github.com/joaopauloschuler/kEffNetV2).\n* `cai.layers.Negate`: negates (multiplies by -1) the input tensor.\n* `cai.layers.SumIntoHalfChannels`: divedes channels into 2 halfs and then sums both halfs. This results into an output with the half of the input channels.\n\n## Give this Project a Star\nThis project is an open source project. If you like what you see, please give it a star on github.\n\n## Citing this API\nYou can cite this API in BibTeX format with:\n```\n@software{k_cai_neural_api_2021_5810092,\n  author       = {Joao Paulo Schwarz Schuler},\n  title        = {K-CAI NEURAL API},\n  month        = dec,\n  year         = 2021,\n  publisher    = {Zenodo},\n  doi          = {10.5281/zenodo.5810092},\n  url          = {https://doi.org/10.5281/zenodo.5810092}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoaopauloschuler%2Fk-neural-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoaopauloschuler%2Fk-neural-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoaopauloschuler%2Fk-neural-api/lists"}