{"id":16735460,"url":"https://github.com/thomasthaddeus/neuralnetworks","last_synced_at":"2025-03-15T20:46:13.158Z","repository":{"id":166612264,"uuid":"642113995","full_name":"thomasthaddeus/NeuralNetworks","owner":"thomasthaddeus","description":"Imagery pipeline utilizing neural networks","archived":false,"fork":false,"pushed_at":"2024-03-20T18:36:53.000Z","size":555,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-22T10:11:08.056Z","etag":null,"topics":["cnn-keras","greedy-algorithms","knn-regression","python","rnn-tensorflow"],"latest_commit_sha":null,"homepage":"http://thomasthaddeus.com/NeuralNetworks/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thomasthaddeus.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":".github/CODEOWNERS","security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-17T21:17:51.000Z","updated_at":"2024-03-17T07:24:34.000Z","dependencies_parsed_at":null,"dependency_job_id":"a73065d5-8a5c-4461-a7a5-bc7a03e1012f","html_url":"https://github.com/thomasthaddeus/NeuralNetworks","commit_stats":{"total_commits":55,"total_committers":4,"mean_commits":13.75,"dds":"0.19999999999999996","last_synced_commit":"65bb35f9f726aa54ac8943aeb3cfeb97bab1f42b"},"previous_names":["thomasthaddeus/neuralnetworks"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomasthaddeus%2FNeuralNetworks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomasthaddeus%2FNeuralNetworks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomasthaddeus%2FNeuralNetworks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomasthaddeus%2FNeuralNetworks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thomasthaddeus","download_url":"https://codeload.github.com/thomasthaddeus/NeuralNetworks/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243790950,"owners_count":20348379,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["cnn-keras","greedy-algorithms","knn-regression","python","rnn-tensorflow"],"created_at":"2024-10-13T00:06:01.474Z","updated_at":"2025-03-15T20:46:13.119Z","avatar_url":"https://github.com/thomasthaddeus.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Neural Networks\nPython program that implements a simple convolutional neural network (CNN) using the Keras library\n\n\u003c!-- BADGIE TIME --\u003e\n\n[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit)](https://github.com/pre-commit/pre-commit)\n[![code style: black](https://img.shields.io/badge/code_style-black-000000.svg)](https://github.com/psf/black)\n[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)\n\n\u003c!-- END BADGIE TIME --\u003e\n\n## REQUIREMENTS\n\n1. [Link to Requirements](./docs/requirements.md)\n2. [Data tree](./docs/tree.html)\n3. [Overview](./src/pipeline/overview.ipynb)\n\n## Data for testing\n\nTo be able to test these algorithms I used a dataset I found on Kaggle\nThe dataset is Located [Here]\n or you can download it using the following command:\n\n```bash\nkaggle datasets download -d puneet6060/intel-image-classification\n```\n\n## CNN\n\nEx. of a Python program that implements a simple convolutional neural network (CNN) using the Keras library\n\nIn this example, we're using the `Keras` library to define and train the CNN. \\\nLoad the MNIST dataset using the `mnist.load_data()` function from Keras.\nThen preprocess the data by normalizing the pixel values between **0 and 1**` and adding a channel dimension for grayscale images.\n\n1. The model consists of several layers:\n    - Two convolutional layers with max pooling\n    - Followed by a flattening layer\n    - Then two fully connected (dense) layers\n    - The final layer uses the softmax activation function to output probabilities for each class\n2. To use this code, you would need to load and preprocess your dataset accordingly.\n3. Replace the comments in the code with the necessary code for loading and preprocessing your data.\n4. Finally, you can train the model using the fit method and evaluate its performance using the evaluate method.\n\n```python\n# Load the dataset (assuming MNIST)\nfrom keras.datasets import mnist\nfrom keras.utils import to_categorical\n\n(X_train, y_train), (X_test, y_test) = mnist.load_data()\n\n# Preprocess the data\n# Normalize pixel values between 0 and 1\nX_train = X_train.astype('float32') / 255\nX_test = X_test.astype('float32') / 255\n\n# Add channel dimension for grayscale images\nX_train = np.expand_dims(X_train, axis=-1)\nX_test = np.expand_dims(X_test, axis=-1)\n\n# Convert class labels to one-hot encoded vectors\ny_train = to_categorical(y_train, num_classes=10)\ny_test = to_categorical(y_test, num_classes=10)\n\n# Train the model\nmodel.fit(X_train, y_train, epochs=10, batch_size=128, validation_data=(X_test, y_test))\n\n# Evaluate the model\nloss, accuracy = model.evaluate(X_test, y_test)\nprint(f'Test Loss: {loss:.4f}')\nprint(f'Test Accuracy: {accuracy:.4f}')\n```\n\nNext, we convert the class labels to one-hot encoded vectors using the to_categorical function from Keras. This step is necessary when dealing with multi-class classification problems.\nFinally, we train the model using the preprocessed training data and labels. The fit method is used to train the model for a specified number of epochs and a specified batch size. We also provide the test data and labels as the validation data to monitor the model's performance during training.\nAfter training, we evaluate the model on the test data using the evaluate method, and print the test loss and accuracy.\nRemember to ensure that you have the required dependencies installed and import the necessary libraries before running the code.\n\n```python\n# Load the dataset (assuming MNIST)\nfrom keras.datasets import mnist\nfrom keras.utils import to_categorical\n\n# Load the MNIST dataset\n(X_train, y_train), (X_test, y_test) = mnist.load_data()\n\n# Preprocess the data\n# Normalize pixel values between 0 and 1\nX_train = X_train.astype('float32') / 255\nX_test = X_test.astype('float32') / 255\n\n# Add channel dimension for grayscale images\nX_train = X_train.reshape(X_train.shape[0], 28, 28, 1)\nX_test = X_test.reshape(X_test.shape[0], 28, 28, 1)\n\n# Convert class labels to one-hot encoded vectors\ny_train = to_categorical(y_train, num_classes=10)\ny_test = to_categorical(y_test, num_classes=10)\n\n# Define the CNN model\nmodel = Sequential()\nmodel.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)))\nmodel.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Flatten())\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(10, activation='softmax'))\n\n# Compile the model\nmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n\n# Train the model\nmodel.fit(X_train, y_train, batch_size=128, epochs=10, verbose=1, validation_data=(X_test, y_test))\n\n# Evaluate the model\nloss, accuracy = model.evaluate(X_test, y_test, verbose=0)\nprint(f'Test Loss: {loss:.4f}')\nprint(f'Test Accuracy: {accuracy:.4f}')\n```\n\nReshaped the input data: The shape of the input data is modified to include the channel dimension explicitly. This is achieved by using the reshape method on the X_train and X_test arrays.\n\nModified the architecture: The architecture of the CNN model has been slightly modified. We added a second convolutional layer and changed the size of the fully connected layer to 128 units.\n\nAdded verbosity during training: We set the verbose argument to 1 during the training process. This allows us to see the progress and logs during the training.\n\nOther than these changes, the rest of the code remains the same. The dataset is loaded, preprocessed, and the model is compiled, trained, and evaluated in a similar manner.\n\n```python\nimport numpy as np\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense\nfrom keras.datasets import mnist\nfrom keras.utils import to_categorical\n\nclass CNNModel:\n    def __init__(self):\n        self.model = None\n\n    def build_model(self, input_shape, num_classes):\n        self.model = Sequential()\n        self.model.add(Conv2D(32, (3, 3), activation='relu', input_shape=input_shape))\n        self.model.add(MaxPooling2D((2, 2)))\n        self.model.add(Conv2D(64, (3, 3), activation='relu'))\n        self.model.add(MaxPooling2D((2, 2)))\n        self.model.add(Flatten())\n        self.model.add(Dense(64, activation='relu'))\n        self.model.add(Dense(num_classes, activation='softmax'))\n        self.model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n    def load_data(self):\n        (X_train, y_train), (X_test, y_test) = mnist.load_data()\n        X_train = X_train.astype('float32') / 255\n        X_test = X_test.astype('float32') / 255\n        X_train = np.expand_dims(X_train, axis=-1)\n        X_test = np.expand_dims(X_test, axis=-1)\n        y_train = to_categorical(y_train, num_classes=10)\n        y_test = to_categorical(y_test, num_classes=10)\n        return X_train, y_train, X_test, y_test\n\n    def train(self, X_train, y_train, X_test, y_test, epochs, batch_size):\n        self.model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, validation_data=(X_test, y_test))\n\n    def evaluate(self, X_test, y_test):\n        loss, accuracy = self.model.evaluate(X_test, y_test)\n        print(f'Test Loss: {loss:.4f}')\n        print(f'Test Accuracy: {accuracy:.4f}')\n\n    def predict(self, X):\n        return self.model.predict(X)\n\n# Example usage\ncnn = CNNModel()\ncnn.build_model(input_shape=(28, 28, 1), num_classes=10)\nX_train, y_train, X_test, y_test = cnn.load_data()\ncnn.train(X_train, y_train, X_test, y_test, epochs=10, batch_size=128)\ncnn.evaluate(X_test, y_test)\n```\n\nIn this example, we have encapsulated the CNN model and related operations within the CNNModel class. The class has methods for building the model, loading the data, training, evaluating, and making predictions.\n\nYou can create an instance of the CNNModel class and call its methods to perform the desired operations. The build_model method allows you to specify the input shape and the number of classes. The load_data method loads and preprocesses the MNIST dataset. The train method trains the model with the provided data, while the evaluate method evaluates the model's performance on the test data. Finally, the predict method can be used to make predictions on new data.\n\nThis class provides a convenient way to reuse the CNN model and its functions by simply creating an instance of the class and calling the appropriate methods with the desired values.\n\n[Here]:  \u003chttps://www.kaggle.com/datasets/puneet6060/intel-image-classification\u003e \"Intel Image Classification\"\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthomasthaddeus%2Fneuralnetworks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthomasthaddeus%2Fneuralnetworks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthomasthaddeus%2Fneuralnetworks/lists"}