-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1c3ea77
Showing
10 changed files
with
3,135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
# Created by https://www.toptal.com/developers/gitignore/api/python,macos,linux,windows | ||
# Edit at https://www.toptal.com/developers/gitignore?templates=python,macos,linux,windows | ||
|
||
### Linux ### | ||
*~ | ||
|
||
# temporary files which can be created if a process still has a handle open of a deleted file | ||
.fuse_hidden* | ||
|
||
# KDE directory preferences | ||
.directory | ||
|
||
# Linux trash folder which might appear on any partition or disk | ||
.Trash-* | ||
|
||
# .nfs files are created when an open file is removed but is still being accessed | ||
.nfs* | ||
|
||
### macOS ### | ||
# General | ||
.DS_Store | ||
.AppleDouble | ||
.LSOverride | ||
|
||
# Icon must end with two \r | ||
Icon | ||
|
||
|
||
# Thumbnails | ||
._* | ||
|
||
# Files that might appear in the root of a volume | ||
.DocumentRevisions-V100 | ||
.fseventsd | ||
.Spotlight-V100 | ||
.TemporaryItems | ||
.Trashes | ||
.VolumeIcon.icns | ||
.com.apple.timemachine.donotpresent | ||
|
||
# Directories potentially created on remote AFP share | ||
.AppleDB | ||
.AppleDesktop | ||
Network Trash Folder | ||
Temporary Items | ||
.apdisk | ||
|
||
### macOS Patch ### | ||
# iCloud generated files | ||
*.icloud | ||
|
||
### Python ### | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
cover/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
db.sqlite3-journal | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
.pybuilder/ | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# IPython | ||
profile_default/ | ||
ipython_config.py | ||
|
||
# pyenv | ||
# For a library or package, you might want to ignore these files since the code is | ||
# intended to run in multiple environments; otherwise, check them in: | ||
# .python-version | ||
|
||
# pipenv | ||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
# However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
# having no cross-platform support, pipenv may install dependencies that don't work, or not | ||
# install all needed dependencies. | ||
#Pipfile.lock | ||
|
||
# poetry | ||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. | ||
# This is especially recommended for binary packages to ensure reproducibility, and is more | ||
# commonly ignored for libraries. | ||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control | ||
#poetry.lock | ||
|
||
# pdm | ||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. | ||
#pdm.lock | ||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it | ||
# in version control. | ||
# https://pdm.fming.dev/#use-with-ide | ||
.pdm.toml | ||
|
||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm | ||
__pypackages__/ | ||
|
||
# Celery stuff | ||
celerybeat-schedule | ||
celerybeat.pid | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
# pytype static type analyzer | ||
.pytype/ | ||
|
||
# Cython debug symbols | ||
cython_debug/ | ||
|
||
# PyCharm | ||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can | ||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore | ||
# and can be added to the global gitignore or merged into this file. For a more nuclear | ||
# option (not recommended) you can uncomment the following to ignore the entire idea folder. | ||
#.idea/ | ||
|
||
### Python Patch ### | ||
# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration | ||
poetry.toml | ||
|
||
# ruff | ||
.ruff_cache/ | ||
|
||
# LSP config files | ||
pyrightconfig.json | ||
|
||
### Windows ### | ||
# Windows thumbnail cache files | ||
Thumbs.db | ||
Thumbs.db:encryptable | ||
ehthumbs.db | ||
ehthumbs_vista.db | ||
|
||
# Dump file | ||
*.stackdump | ||
|
||
# Folder config file | ||
[Dd]esktop.ini | ||
|
||
# Recycle Bin used on file shares | ||
$RECYCLE.BIN/ | ||
|
||
# Windows Installer files | ||
*.cab | ||
*.msi | ||
*.msix | ||
*.msm | ||
*.msp | ||
|
||
# Windows shortcuts | ||
*.lnk | ||
|
||
# End of https://www.toptal.com/developers/gitignore/api/python,macos,linux,windows | ||
|
||
test_images/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Multilayer Authenticity Identifier (MAI) | ||
|
||
MAI is a research project that attempts to train a CNN model to identify synthetic AI images. | ||
|
||
## Why? | ||
|
||
i am bored. | ||
|
||
## Architecture | ||
|
||
nothing is set in stone, but at the moment, MAI is a simple CNN model that looks like this: | ||
|
||
1. 16-channel, 3x3 convolution layer -> 2x2 max pooling -> relu activation | ||
2. 32-channel, 3x3 convolution layer -> 2x2 max pooling -> relu activation | ||
3. 64-channel, 3x3 convolution layer -> 2x2 max pooling -> relu activation | ||
4. 40,000-neuron layer -> relu -> 120-neuron layer -> relu -> 30 -> 1 | ||
|
||
the model expects a 200x200 image as an input and outputs a score, with 1 being that the input image is absolutely synthetic, and 0 being that it is absolutely authentic. | ||
|
||
[BCEWithLogitLoss](https://pytorch.org/docs/stable/generated/torch.nn.BCEWithLogitsLoss.html) is used as the loss fn, and [RMSprop](https://pytorch.org/docs/stable/generated/torch.optim.RMSprop.html) as the optimizer. | ||
|
||
## Datasets | ||
|
||
MAI has been trained on the following datatsets: | ||
|
||
- [poloclub/diffusiondb](https://huggingface.co/datasets/poloclub/diffusiondb) | ||
- [nlphuji/flickr30k](https://huggingface.co/datasets/nlphuji/flickr30k) | ||
- [keremberke/painting-style-classification](https://huggingface.co/datasets/keremberke/painting-style-classification) | ||
- [animelover/scenery-images](https://huggingface.co/datasets/animelover/scenery-images) | ||
- [nanxstats/movie-poster-5k](https://huggingface.co/datasets/nanxstats/movie-poster-5k) | ||
- [Alphonsce/metal_album_covers](https://huggingface.co/datasets/Alphonsce/metal_album_covers) | ||
|
||
## How to train? | ||
|
||
make sure to have [poetry](https://python-poetry.org) installed. | ||
|
||
clone the project, and run: | ||
|
||
``` | ||
poetry install | ||
``` | ||
|
||
open a shell in the venv created by poetry: | ||
|
||
``` | ||
poetry shell | ||
``` | ||
|
||
run `train.py` to train the model. make sure cuda is available as a cuda-enabled gpu is used to accelerate training. for each epoch, if the validation loss is less than the last epoch, the model is saved locally. you can customize the location easily in `train.py`. | ||
|
||
## How to run inference? | ||
|
||
run `inference.py` instead. place your test images in `test_images/` directory, and don't forget to reference the images in `inference.py`. | ||
|
||
## More on modal.com | ||
|
||
i am using (https://modal.com) to run the training and inference, but u can get rid of the modal.com glue pretty easily. you should first remove the decorators above the functions, then at where the functions are invoked, remove `.remote()` and instead invoke the function directly. remove `app` and `vol` variables as well. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import albumentations as a | ||
import numpy as np | ||
from albumentations.pytorch import ToTensorV2 | ||
from hyperparams import CROP_SIZE | ||
|
||
|
||
preprocess_training = a.Compose( | ||
[ | ||
a.augmentations.PadIfNeeded(min_width=CROP_SIZE, min_height=CROP_SIZE), | ||
a.RandomCrop(width=CROP_SIZE, height=CROP_SIZE), | ||
a.Flip(p=0.5), | ||
a.RandomRotate90(p=0.5), | ||
a.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), | ||
ToTensorV2(), | ||
] | ||
) | ||
preprocess_validation = a.Compose( | ||
[ | ||
a.augmentations.PadIfNeeded(min_width=CROP_SIZE, min_height=CROP_SIZE), | ||
a.CenterCrop(width=CROP_SIZE, height=CROP_SIZE), | ||
a.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), | ||
ToTensorV2(), | ||
] | ||
) | ||
|
||
|
||
def transform_training(example): | ||
transformed = [] | ||
for pil_image in example["image"]: | ||
array = np.array(pil_image.convert("RGB")) | ||
# check if image is in (height, width, channel) shape | ||
# if not, do a transpose | ||
if array.shape[-1] != 3: | ||
array = np.transpose(array, (1, 2, 0)) | ||
img = preprocess_training(image=array)["image"] | ||
transformed.append(img) | ||
example["pixel_values"] = transformed | ||
return example | ||
|
||
|
||
def transform_validation(example): | ||
transformed = [] | ||
for pil_image in example["image"]: | ||
array = np.array(pil_image.convert("RGB")) | ||
if array.shape[-1] != 3: | ||
array = np.transpose(array, (1, 2, 0)) | ||
img = preprocess_validation(image=array)["image"] | ||
transformed.append(img) | ||
example["pixel_values"] = transformed | ||
return example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
FILTER_COUNT = 32 | ||
KERNEL_SIZE = 2 | ||
CROP_SIZE = 200 | ||
BATCH_SIZE = 4 | ||
EPOCHS = 15 |
Oops, something went wrong.