{"id":18260749,"url":"https://github.com/bdevans/bionet","last_synced_at":"2025-09-21T16:32:49.333Z","repository":{"id":140055650,"uuid":"335355365","full_name":"bdevans/BioNet","owner":"bdevans","description":"Deep Neural Networks with Bio-inspired Convolutions","archived":false,"fork":false,"pushed_at":"2022-02-03T17:57:36.000Z","size":272,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-18T11:47:02.397Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bdevans.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":"CITATION.cff","codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-02-02T16:37:47.000Z","updated_at":"2024-11-05T09:25:24.000Z","dependencies_parsed_at":null,"dependency_job_id":"fd195c93-0823-4f0b-a870-7f663fa47f17","html_url":"https://github.com/bdevans/BioNet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bdevans%2FBioNet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bdevans%2FBioNet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bdevans%2FBioNet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bdevans%2FBioNet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bdevans","download_url":"https://codeload.github.com/bdevans/BioNet/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233773578,"owners_count":18728045,"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":[],"created_at":"2024-11-05T10:48:47.851Z","updated_at":"2025-09-21T16:32:43.989Z","avatar_url":"https://github.com/bdevans.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BioNet\nDeep Convolutional Neural Networks with bio-inspired filters. \n\n1. Clone this [BioNet repository](https://github.com/bdevans/BioNet)\n2. Clone the [CIFAR-10G](https://github.com/bdevans/CIFAR-10G) generalisation test set\n3. Set your `project_dir` in the notebook and pass your `data_dir` (`ln -s /shared/data/ data`) which contains the image sets\n\nExpected directory structure\n----------------------------\n\n```\n.\n├── bionet\n│   ├── config.py\n│   ├── explain.py\n│   ├── __init__.py\n│   ├── plots.py\n│   └── preparation.py\n├── data\n│   ├── CIFAR-10G\n│   ├── ecoset\n│   └── ecoset-cifar10\n├── logs\n├── models\n├── notebooks\n├── results\n├── scripts\n├── model.py\n└── README.md\n```\n\nTraining and testing the model\n------------------------------\n\nThe main script to handle training and testing is `model.py` in the project's root directory. This script is called to both train and test the models. If saved weights files are found in the `models` directory, training will be skipped (unless the `clean` flag is passed) and the code will proceed to testing. \n\n### Arguments and usage\n\n```\nusage: model.py [-h] [--convolution CONVOLUTION] [--base BASE] [--pretrain]\n                [--architecture ARCHITECTURE] [--interpolation INTERPOLATION]\n                [--optimizer {SGD,RMSprop,Adagrad,Adadelta,Adam,Adamax,Nadam}]\n                [--lr LR] [--decay DECAY] [--use_initializer]\n                [--internal_noise INTERNAL_NOISE] [--trial TRIAL]\n                [--label LABEL] [--seed SEED] [-t] [--recalculate_statistics]\n                [--epochs EPOCHS] [--batch BATCH] [--image_path IMAGE_PATH]\n                [--train_image_path TRAIN_IMAGE_PATH] [--test_generalisation]\n                [--invert_test_images INVERT_TEST_IMAGES]\n                [--test_perturbations] [--data_augmentation]\n                [--extra_augmentation] [-c] [--skip_test] [-l] [--save_images]\n                [-p] [--gpu GPU] [--project_dir PROJECT_DIR] [-v VERBOSE]\n\noptional arguments:\n  -h, --help            show this help message and exit\n  --convolution CONVOLUTION\n                        Name of convolutional filter to use\n  --base BASE           Name of model to use\n  --pretrain            Flag to use pretrained ImageNet weights in the model\n  --architecture ARCHITECTURE\n                        Parameter file (JSON) to load\n  --interpolation INTERPOLATION\n                        Method to interpolate the images when upscaling.\n                        Default: 0 (\"nearest\" i.e. no interpolation)\n  --optimizer {SGD,RMSprop,Adagrad,Adadelta,Adam,Adamax,Nadam}\n                        Name of optimizer to use: https://keras.io/optimizers/\n  --lr LR, --learning_rate LR\n                        Learning rate for training\n  --decay DECAY         Optimizer decay for training\n  --use_initializer     Flag to use the weight initializer (then freeze\n                        weights) for the Gabor filters\n  --internal_noise INTERNAL_NOISE\n                        Standard deviation for adding a Gaussian noise layer\n                        after the first convolutional layer\n  --trial TRIAL         Trial number for labeling different runs of the same\n                        model\n  --label LABEL         For labeling different runs of the same model\n  --seed SEED           Random seed to use\n  -t, --train           Flag to train the model\n  --recalculate_statistics\n                        Flag to recalculate normalisation statistics over the\n                        training set\n  --epochs EPOCHS       Number of epochs to train model\n  --batch BATCH         Size of mini-batches passed to the network\n  --image_path IMAGE_PATH\n                        Path to image files to load\n  --train_image_path TRAIN_IMAGE_PATH\n                        Path to training image files to load\n  --test_generalisation\n                        Flag to test the model on sets of untrained images\n  --invert_test_images INVERT_TEST_IMAGES\n                        Flag to invert the luminance of the test images\n  --test_perturbations  Flag to test the model on perturbed images\n  --data_augmentation   Flag to train the model with data augmentation\n  --extra_augmentation  Flag to train the model with additional data\n                        augmentation\n  -c, --clean           Flag to retrain model\n  --skip_test           Flag to skip testing the model\n  -l, --log             Flag to log training data\n  --save_images         Flag to save preprocessed (perturbed) test images\n  -p, --save_predictions\n                        Flag to save category predictions\n  --gpu GPU             GPU ID to run on\n  --project_dir PROJECT_DIR\n                        Path to the root project directory\n  -v VERBOSE, --verbose VERBOSE\n                        Verbosity level\n```\n\nTo train and test the models, the code below may be used and adapted as required.\n\n```python\nimport os\nimport sys\nimport pprint\nimport subprocess\nimport random\nfrom tqdm.notebook import tqdm\nimport tensorflow as tf\nimport tensorflow.keras\nimport tensorflow.keras.backend\nproject_root_dir = \"/home/jovyan/work/BioNet\"  # Change as necessary\nprint(f\"Project directory: {project_root_dir}\\n\")\nsys.path.append(project_root_dir)\nprint(\"\\nTensorFlow:\", tf.__version__)\nprint(f\"Channel ordering: {tf.keras.backend.image_data_format()}\")  # TensorFlow: Channels last order.\ngpus = tf.config.experimental.list_physical_devices('GPU')\n# gpus = tf.config.list_physical_devices('GPU')\npprint.pprint(gpus)\nlabel = \"paper\"\nimage_path = ''  # Empty string defaults to CIFAR-10\n# image_path = '/shared/data/ecoset-cifar10'\nconvolutions = ['Original', 'Low-pass', 'DoG', 'Gabor', 'Combined-trim']\nbases = ['ALL-CNN', 'VGG-16', 'VGG-19', 'ResNet']\nseed = 0\nstart_trial = 1\nnum_trials = 5\ntrials = range(start_trial, start_trial+num_trials)\ntrain = True\npretrain = False\nclean = False\nepochs = 100\noptimizer = \"RMSprop\"\nlr = 1e-4\nuse_initializer = True\ndata_augmentation = True\nextra_augmentation = False\ninternal_noise = 0\nskip_test = False\nsave_images = False\nsave_predictions = True\ntest_generalisation = True\ntest_perturbations = True\ninterpolation = 4  # Lanczos\nrecalculate_statistics = False\nverbose = 0\nhalt_on_error = False\ngpu = 1\n######################################\nscript = os.path.join(project_root_dir, \"model.py\")\nflags = ['--log']\nif train:\n    flags.append('-t')\nif clean:\n    flags.append('-c')\nif use_initializer:\n    flags.append('--use_initializer')\nif data_augmentation:\n    flags.append('--data_augmentation')\nif extra_augmentation:\n    flags.append('--extra_augmentation')\nif skip_test:\n    flags.append('--skip_test')\nif recalculate_statistics:\n    flags.append('--recalculate_statistics')\nif save_predictions:\n    flags.append('--save_predictions')\noptional_args = []\nif image_path:\n    optional_args.extend(['--image_path', str(image_path)])\nif test_perturbations:\n    optional_args.append('--test_perturbations')\nif test_generalisation:\n    optional_args.append('--test_generalisation')\nif pretrain:\n    optional_args.append('--pretrain')\nif internal_noise:\n    optional_args.extend(['--internal_noise', str(internal_noise)])\nif interpolation:\n    optional_args.extend(['--interpolation', str(interpolation)])\nif verbose:\n    optional_args.extend(['--verbose', str(verbose)])\ncount = 1\nfor trial in tqdm(trials, desc='Trial'):\n    if seed is None:\n        seed = random.randrange(2**32)\n    for base in tqdm(bases, desc='Model Base', leave=False):\n        for conv in tqdm(convolutions, desc='Convolution', leave=False):\n            cmd = [script, *flags]\n            if save_images and count == 1:\n                cmd.append('--save_images')\n            cmd.extend(['--convolution', conv, '--base', base, '--label', label,\n                        '--trial', str(trial), '--seed', str(seed),\n                        '--optimizer', optimizer, '--lr', str(lr),\n                        '--epochs', str(epochs), '--gpu', str(gpu)])\n            cmd.extend(optional_args)\n            completed = subprocess.run(cmd, shell=False, capture_output=True, text=True)\n            if completed.returncode != 0:\n                print(completed.stdout)\n                print(completed.stderr)\n            count += 1\nf'Finished job \"{label}\"!'\n```\n\nNotes\n-----\n\n`rsync -vzhrLKe ssh --progress user@host:/storage/models/paper /shared/data/`\n\nCreate symlinks to consolidate simulations\n\n`find response -maxdepth 1 -mindepth 1 -type d -exec ln -s ../'{}' paper/ \\;`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbdevans%2Fbionet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbdevans%2Fbionet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbdevans%2Fbionet/lists"}