Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Lint

on:
push:
branches: [ main, walid-branch, cnn ]
pull_request:
branches: [ main, walid-branch, cnn ]

jobs:
lint:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'

- name: Install linting dependencies
run: |
python -m pip install --upgrade pip
pip install black flake8 isort

- name: Check code formatting with Black
run: |
black --check --diff models/ utils/ tests/

- name: Check import sorting with isort
run: |
isort --check-only --diff models/ utils/ tests/

- name: Lint with flake8
run: |
# Stop the build if there are Python syntax errors or undefined names
flake8 models/ utils/ tests/ --count --select=E9,F63,F7,F82 --show-source --statistics
# Exit-zero treats all errors as warnings. Max line length set to 100
flake8 models/ utils/ tests/ --count --max-line-length=100 --statistics --extend-ignore=E203,W503
40 changes: 40 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Tests

on:
push:
branches: [ main, walid-branch, cnn ]
pull_request:
branches: [ main, walid-branch, cnn ]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.11', '3.12']

steps:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pytest pytest-cov

- name: Run tests with pytest
run: |
pytest tests/ -v --cov=models --cov=utils --cov-report=term-missing

- name: Upload coverage reports
if: matrix.python-version == '3.12'
uses: codecov/codecov-action@v4
with:
fail_ci_if_error: false
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ wheels/
computer-vision/

*.png
.DS_Store

.coverage
45 changes: 45 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@


.PHONY: help install install-dev test test-cov lint format format-check type-check train clean pre-commit all

help:
@echo "Available commands:"
@echo " make install Install production dependencies"
@echo " make install-dev Install development dependencies"
@echo " make test Run tests ith coverage report"
@echo " make lint Run linting (flake8)"
@echo " make format Format code with black and isort"
@echo " make train Train model (use SCRIPT=path/to/script.py)"
@echo " make type-check Run type checking with mypy"
@echo " make clean Remove build artifacts and cache files"
@echo " make all Run format, lint, type-check, and test"

install:
pip install -r requirements.txt

install-dev:
pip install -r requirements-dev.txt

test:
pytest --cov --cov-report=html --cov-report=term

lint:
flake8 models utils tests

format:
black models utils tests
isort models utils tests

type-check:
mypy models utils


clean:
find . -type f -name '*.pyc' -delete
find . -type d -name '__pycache__' -delete
find . -type d -name '*.egg-info' -exec rm -rf {} +
find . -type d -name '.pytest_cache' -exec rm -rf {} +
find . -type d -name '.mypy_cache' -exec rm -rf {} +
rm -rf build dist htmlcov .coverage

all: format lint type-check test
143 changes: 143 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# Computer Vision Project

![Tests](https://github.com/alexisvannson/computer-vision/actions/workflows/test.yml/badge.svg)
![Lint](https://github.com/alexisvannson/computer-vision/actions/workflows/lint.yml/badge.svg)
![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)
![Python](https://img.shields.io/badge/python-3.12+-blue.svg)

A PyTorch-based computer vision project implementing various neural network architectures including CNNs, MLPs, and exploring advanced topics like attention mechanisms, transfer learning, and generative models.

## Features

- **Flexible CNN Implementation**: Convolutional Neural Network for image classification tasks
- **Customizable MLP**: Multi-layer Perceptron with configurable hidden layers, activations, and normalization
- **Training Utilities**: Modular training pipeline for model development
- **CI/CD Integration**: Automated testing and linting with GitHub Actions
- **Code Quality**: Enforced code formatting with Black, import sorting with isort, and type checking with mypy

## Project Structure

```
computer-vision/
├── models/ # Neural network architectures
│ ├── CNN.py # Convolutional Neural Network
│ └── MLP.py # Multi-layer Perceptron
├── utils/ # Training and utility functions
│ └── train_model.py # Model training utilities
├── tests/ # Unit and integration tests
│ ├── test_models.py
│ └── test_training.py
├── data/ # Dataset directory
│ ├── train/
│ ├── test/
│ └── train_labels.csv
├── notebooks/ # Jupyter notebooks for experiments
├── .github/workflows/ # CI/CD configuration
│ ├── test.yml # Automated testing
│ └── lint.yml # Code quality checks
└── Makefile # Development commands
```

## Installation

### Prerequisites

- Python 3.12+
- pip or uv package manager

### Basic Installation

```bash
# Clone the repository
git clone <repository-url>
cd computer-vision

# Install dependencies
pip install -r requirements.txt
```

### Development Installation

```bash
# Install development dependencies (includes testing and linting tools)
make install-dev
# or
pip install -r requirements-dev.txt
```

## Usage


## Development

### Available Make Commands

```bash
# Install development dependencies
make install-dev

# Run tests
make test

# Run tests with coverage report
make test-cov

# Lint code
make lint

# Format code
make format

# Check formatting without changes
make format-check

# Type checking
make type-check

# Train a model
make train SCRIPT=your_script.py

# Clean build artifacts
make clean
```

### Code Quality

This project enforces code quality through:

- **Black**: Code formatting (line length: 100)
- **isort**: Import sorting
- **flake8**: Linting
- **mypy**: Static type checking
- **pytest**: Unit and integration testing

### Running Tests

```bash
# Run all tests
make test

# Run tests with coverage
make test-cov

# Run specific test file
pytest tests/test_models.py
```

## CI/CD

The project includes GitHub Actions workflows for:

- **Automated Testing**: Runs on every push and pull request
- **Code Linting**: Ensures code quality standards
- **Type Checking**: Validates type annotations


### Code Style

- Follow PEP 8 guidelines
- Use type hints for function arguments and return values
- Write docstrings for classes and functions
- Maintain test coverage for new features


3 changes: 3 additions & 0 deletions gameplan.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ Generative Adversarial Network (GAN)


Diffusion


DENO
58 changes: 2 additions & 56 deletions models/CNN.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import torch
import torch
import torch.nn as nn
import torch.nn.functional as F


class CNN(nn.Module):
def __init__(self):

"""
Flexible Convolutional Neural Network.
"""
Expand All @@ -21,61 +20,8 @@ def __init__(self):
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = torch.flatten(x, 1) # flatten all dimensions except batch
x = torch.flatten(x, 1) # flatten all dimensions except batch
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x


net = Net()



import torch.nn as nn
from torch import Tensor

# Definition of a MLP
class MLP(nn.Module):
def __init__(
self,
in_dim: int,
out_dim: int,
hidden_dim: int = 128,
hidden_layers: int = 2,
activation: str = "ReLU",
initializer: None | str = None,
norm_type: None | str = "LayerNorm",
):
"""
Flexible Multi-layer perceptron.
"""

super(MLP, self).__init__()
self.activation = getattr(nn, activation)()
if initializer is not None:
self.initializer = getattr(nn.init, initializer)
layers = [nn.Linear(in_dim, hidden_dim), self.activation]
for _ in range(hidden_layers - 1):
layers += [nn.Linear(hidden_dim, hidden_dim), self.activation]
layers.append(nn.Linear(hidden_dim, out_dim))

if norm_type is not None:
assert norm_type in [
"LayerNorm",
"BatchNorm1d",
]
norm_layer = getattr(nn, norm_type)
layers.append(norm_layer(out_dim))

self.model = nn.Sequential(*layers)

if initializer is not None:
params = self.model.parameters()
for param in params:
if param.requires_grad and len(param.shape) > 1:
self.initializer(param)

def forward(self, x: Tensor):
x = x.view(x.size(0), -1) # Flatten: (batch, channels*height*width)
return self.model(x.float())
3 changes: 2 additions & 1 deletion models/MLP.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import torch.nn as nn
from torch import Tensor


# Definition of a MLP
class MLP(nn.Module):
def __init__(
Expand Down Expand Up @@ -44,4 +45,4 @@ def __init__(

def forward(self, x: Tensor):
x = x.view(x.size(0), -1) # Flatten: (batch, channels*height*width)
return self.model(x.float())
return self.model(x.float())
Loading