├── .gitignore ├── LICENSE ├── Makefile.mixup2 ├── Makefile.predict ├── Makefile.train ├── Makefile.train2 ├── README.md ├── data └── data_prep.py ├── tars ├── __init__.py ├── bninception.py ├── inceptionresnetv2.py ├── inceptionv4.py ├── nasnet.py ├── resnext.py ├── resnext_features │ ├── __init__.py │ ├── resnext101_32x4d_features.py │ └── resnext101_64x4d_features.py ├── tars_data_loaders.py ├── tars_model.py ├── tars_training.py ├── utils.py └── vggm.py ├── tars_ensemble.py ├── tars_mixup_train.py ├── tars_predict.py └── tars_train.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | #log files 103 | logs/ 104 | #models 105 | models/ 106 | -------------------------------------------------------------------------------- /Makefile.mixup2: -------------------------------------------------------------------------------- 1 | python: 2 | #python tars_mixup_train.py -idl "data/training_data" -mo "resnet18" -ep 200 -pt 3 | #python tars_mixup_train.py -idl "data/training_data" -mo "resnet34" -ep 200 -pt 4 | #python tars_mixup_train.py -idl "data/training_data" -mo "resnet50" -ep 200 -pt 5 | #python tars_mixup_train.py -idl "data/training_data" -mo "resnet101" -ep 200 -pt 6 | #python tars_mixup_train.py -idl "data/training_data" -mo "resnet152" -ep 200 -pt 7 | #python tars_mixup_train.py -idl "data/training_data" -mo "densenet121" -ep 200 -pt 8 | #python tars_mixup_train.py -idl "data/training_data" -mo "densenet169" -ep 200 -pt 9 | #python tars_mixup_train.py -idl "data/training_data" -mo "densenet201" -ep 200 -pt 10 | #python tars_mixup_train.py -idl "data/training_data" -mo "squeezenet1_0" -ep 200 -pt 11 | #python tars_mixup_train.py -idl "data/training_data" -mo "squeezenet1_1" -ep 200 -pt 12 | python tars_mixup_train.py -idl "data/training_data" -mo "inception_v3" -ep 200 -pt -is 299 13 | python tars_mixup_train.py -idl "data/training_data" -mo "vgg11" -ep 200 -pt 14 | python tars_mixup_train.py -idl "data/training_data" -mo "vgg13" -ep 200 -pt 15 | python tars_mixup_train.py -idl "data/training_data" -mo "vgg16" -ep 200 -pt 16 | python tars_mixup_train.py -idl "data/training_data" -mo "vgg19" -ep 200 -pt 17 | python tars_mixup_train.py -idl "data/training_data" -mo "vgg11_bn" -ep 200 -pt 18 | python tars_mixup_train.py -idl "data/training_data" -mo "vgg13_bn" -ep 200 -pt 19 | python tars_mixup_train.py -idl "data/training_data" -mo "vgg16_bn" -ep 200 -pt 20 | python tars_mixup_train.py -idl "data/training_data" -mo "vgg19_bn" -ep 200 -pt 21 | python tars_mixup_train.py -idl "data/training_data" -mo "alexnet" -ep 200 -pt 22 | python tars_mixup_train.py -idl "data/training_data" -mo "inceptionresnetv2" -ep 200 -pt -is 299 -b 16 23 | python tars_mixup_train.py -idl "data/training_data" -mo "inceptionv4" -ep 200 -pt -is 299 -b 16 24 | python tars_mixup_train.py -idl "data/training_data" -mo "bninception" -ep 200 -pt -b 16 25 | python tars_mixup_train.py -idl "data/training_data" -mo "inception_v3" -ep 200 -pt -is 299 26 | python tars_mixup_train.py -idl "data/training_data" -mo "resnext101_64x4d" -ep 200 -pt -b 16 27 | python tars_mixup_train.py -idl "data/training_data" -mo "resnext101_32x4d" -ep 200 -pt -b 16 28 | python tars_mixup_train.py -idl "data/training_data" -mo "nasnetalarge" -ep 200 -pt -is 331 -b 8 29 | -------------------------------------------------------------------------------- /Makefile.predict: -------------------------------------------------------------------------------- 1 | python: 2 | #python tars_predict.py -idl "data/training_data" -ll "models/resnet18_False_freeze_True_freeze_initial_layer.pth" -mo "resnet18" -nc 12 3 | #python tars_predict.py -idl "data/training_data" -ll "models/resnet18_True_freeze_True_freeze_initial_layer.pth" -mo "resnet18" -nc 12 4 | #python tars_predict.py -idl "data/training_data" -ll "models/resnet34_False_freeze_True_freeze_initial_layer.pth" -mo "resnet34" -nc 12 5 | #python tars_predict.py -idl "data/training_data" -ll "models/resnet34_True_freeze_True_freeze_initial_layer.pth" -mo "resnet34" -nc 12 6 | #python tars_predict.py -idl "data/training_data" -ll "models/resnet50_False_freeze_True_freeze_initial_layer.pth" -mo "resnet50" -nc 12 7 | #python tars_predict.py -idl "data/training_data" -ll "models/resnet50_True_freeze_True_freeze_initial_layer.pth" -mo "resnet50" -nc 12 8 | #python tars_predict.py -idl "data/training_data" -ll "models/resnet101_False_freeze_True_freeze_initial_layer.pth" -mo "resnet101" -nc 12 9 | #python tars_predict.py -idl "data/training_data" -ll "models/resnet101_True_freeze_True_freeze_initial_layer.pth" -mo "resnet101" -nc 12 10 | #python tars_predict.py -idl "data/training_data" -ll "models/resnet152_False_freeze_True_freeze_initial_layer.pth" -mo "resnet152" -nc 12 11 | #python tars_predict.py -idl "data/training_data" -ll "models/resnet152_True_freeze_True_freeze_initial_layer.pth" -mo "resnet152" -nc 12 12 | #python tars_predict.py -idl "data/training_data" -ll "models/densenet121_False_freeze_True_freeze_initial_layer.pth" -mo "densenet121" -nc 12 13 | #python tars_predict.py -idl "data/training_data" -ll "models/densenet121_True_freeze_True_freeze_initial_layer.pth" -mo "densenet121" -nc 12 14 | #python tars_predict.py -idl "data/training_data" -ll "models/densenet169_False_freeze_True_freeze_initial_layer.pth" -mo "densenet169" -nc 12 15 | #python tars_predict.py -idl "data/training_data" -ll "models/densenet169_True_freeze_True_freeze_initial_layer.pth" -mo "densenet169" -nc 12 16 | #python tars_predict.py -idl "data/training_data" -ll "models/densenet201_False_freeze_True_freeze_initial_layer.pth" -mo "densenet201" -nc 12 17 | #python tars_predict.py -idl "data/training_data" -ll "models/densenet201_True_freeze_True_freeze_initial_layer.pth" -mo "densenet201" -nc 12 18 | #python tars_predict.py -idl "data/training_data" -ll "models/squeezenet1_0_False_freeze_True_freeze_initial_layer.pth" -mo "squeezenet1_0" -nc 12 19 | #python tars_predict.py -idl "data/training_data" -ll "models/squeezenet1_0_True_freeze_True_freeze_initial_layer.pth" -mo "squeezenet1_0" -nc 12 20 | #python tars_predict.py -idl "data/training_data" -ll "models/squeezenet1_1_False_freeze_True_freeze_initial_layer.pth" -mo "squeezenet1_1" -nc 12 21 | #python tars_predict.py -idl "data/training_data" -ll "models/squeezenet1_1_True_freeze_True_freeze_initial_layer.pth" -mo "squeezenet1_1" -nc 12 22 | #python tars_predict.py -idl "data/training_data" -ll "models/inception_v3_False_freeze_True_freeze_initial_layer.pth" -mo "inception_v3" -nc 12 -is 299 23 | #python tars_predict.py -idl "data/training_data" -ll "models/inception_v3_True_freeze_True_freeze_initial_layer.pth" -mo "inception_v3" -nc 12 -is 299 24 | #python tars_predict.py -idl "data/training_data" -ll "models/vgg11_False_freeze_True_freeze_initial_layer.pth" -mo "vgg11" -nc 12 25 | #python tars_predict.py -idl "data/training_data" -ll "models/vgg11_True_freeze_True_freeze_initial_layer.pth" -mo "vgg11" -nc 12 26 | #python tars_predict.py -idl "data/training_data" -ll "models/vgg13_False_freeze_True_freeze_initial_layer.pth" -mo "vgg13" -nc 12 27 | #python tars_predict.py -idl "data/training_data" -ll "models/vgg13_True_freeze_True_freeze_initial_layer.pth" -mo "vgg13" -nc 12 28 | #python tars_predict.py -idl "data/training_data" -ll "models/vgg16_False_freeze_True_freeze_initial_layer.pth" -mo "vgg16" -nc 12 29 | #python tars_predict.py -idl "data/training_data" -ll "models/vgg16_True_freeze_True_freeze_initial_layer.pth" -mo "vgg16" -nc 12 30 | #python tars_predict.py -idl "data/training_data" -ll "models/vgg19_False_freeze_True_freeze_initial_layer.pth" -mo "vgg19" -nc 12 31 | #python tars_predict.py -idl "data/training_data" -ll "models/vgg19_True_freeze_True_freeze_initial_layer.pth" -mo "vgg19" -nc 12 32 | #python tars_predict.py -idl "data/training_data" -ll "models/vgg11_bn_False_freeze_True_freeze_initial_layer.pth" -mo "vgg11_bn" -nc 12 33 | #python tars_predict.py -idl "data/training_data" -ll "models/vgg11_bn_True_freeze_True_freeze_initial_layer.pth" -mo "vgg11_bn" -nc 12 34 | #python tars_predict.py -idl "data/training_data" -ll "models/vgg13_bn_False_freeze_True_freeze_initial_layer.pth" -mo "vgg13_bn" -nc 12 35 | #python tars_predict.py -idl "data/training_data" -ll "models/vgg13_bn_True_freeze_True_freeze_initial_layer.pth" -mo "vgg13_bn" -nc 12 36 | #python tars_predict.py -idl "data/training_data" -ll "models/vgg16_bn_False_freeze_True_freeze_initial_layer.pth" -mo "vgg16_bn" -nc 12 37 | #python tars_predict.py -idl "data/training_data" -ll "models/vgg16_bn_True_freeze_True_freeze_initial_layer.pth" -mo "vgg16_bn" -nc 12 38 | #python tars_predict.py -idl "data/training_data" -ll "models/vgg19_bn_False_freeze_True_freeze_initial_layer.pth" -mo "vgg19_bn" -nc 12 39 | #python tars_predict.py -idl "data/training_data" -ll "models/vgg19_bn_True_freeze_True_freeze_initial_layer.pth" -mo "vgg19_bn" -nc 12 40 | #python tars_predict.py -idl "data/training_data" -ll "models/alexnet_False_freeze_True_freeze_initial_layer.pth" -mo "alexnet" -nc 12 41 | #python tars_predict.py -idl "data/training_data" -ll "models/alexnet_True_freeze_True_freeze_initial_layer.pth" -mo "alexnet" -nc 12 42 | #python tars_predict.py -idl "data/training_data" -ll "models/resnext101_64x4d_False_freeze_True_freeze_initial_layer.pth" -mo "resnext101_64x4d" -nc 12 43 | #python tars_predict.py -idl "data/training_data" -ll "models/resnext101_64x4d_True_freeze_True_freeze_initial_layer.pth" -mo "resnext101_64x4d" -nc 12 44 | #python tars_predict.py -idl "data/training_data" -ll "models/resnext101_32x4d_False_freeze_True_freeze_initial_layer.pth" -mo "resnext101_32x4d" -nc 12 45 | #python tars_predict.py -idl "data/training_data" -ll "models/resnext101_32x4d_True_freeze_True_freeze_initial_layer.pth" -mo "resnext101_32x4d" -nc 12 46 | #python tars_predict.py -idl "data/training_data" -ll "models/nasnetalarge_False_freeze_True_freeze_initial_layer.pth" -mo "nasnetalarge" -nc 12 -is 331 -gi 47 | #python tars_predict.py -idl "data/training_data" -ll "models/nasnetalarge_True_freeze_True_freeze_initial_layer.pth" -mo "nasnetalarge" -nc 12 -is 331 -gi 48 | #python tars_predict.py -idl "data/training_data" -ll "models/inceptionresnetv2_False_freeze_True_freeze_initial_layer.pth" -mo "inceptionresnetv2" -nc 12 -is 299 -gi 49 | #python tars_predict.py -idl "data/training_data" -ll "models/inceptionresnetv2_True_freeze_True_freeze_initial_layer.pth" -mo "inceptionresnetv2" -nc 12 -is 299 -gi 50 | #python tars_predict.py -idl "data/training_data" -ll "models/inceptionv4_False_freeze_True_freeze_initial_layer.pth" -mo "inceptionv4" -nc 12 -is 299 -gi 51 | #python tars_predict.py -idl "data/training_data" -ll "models/inceptionv4_True_freeze_True_freeze_initial_layer.pth" -mo "inceptionv4" -nc 12 -is 299 -gi 52 | python tars_predict.py -idl "data/training_data" -ll "models/bninception_False_freeze_True_freeze_initial_layer.pth" -mo "bninception" -nc 12 -gi 53 | python tars_predict.py -idl "data/training_data" -ll "models/bninception_True_freeze_True_freeze_initial_layer.pth" -mo "bninception" -nc 12 -gi 54 | -------------------------------------------------------------------------------- /Makefile.train: -------------------------------------------------------------------------------- 1 | DATA_LOC=-idl "/data1/public_datasets/plant_seedlings/training_data" 2 | MIXUP=-mx 3 | MIXUP_ALPHA=-mxal 0.1 4 | SAVE_LOC="models/" 5 | NUM_EPOCHS=-ep 100 6 | 7 | all: resnets densenets squeezenets inceptions_v3 vggs_base vggs_bn alexnets inceptionresnets resnext inceptions_bn nasnets 8 | echo "Training all nets are done" 9 | 10 | resnets: 11 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "resnet18" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 12 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "resnet18" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 13 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "resnet34" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 14 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "resnet34" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 15 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "resnet50" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 16 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "resnet50" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 17 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "resnet101" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 18 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "resnet101" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 19 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "resnet152" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 20 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "resnet152" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 21 | densenets: 22 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "densenet121" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 23 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "densenet121" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 24 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "densenet169" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 25 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "densenet169" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 26 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "densenet201" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 27 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "densenet201" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 28 | squeezenets: 29 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "squeezenet1_0" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 30 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "squeezenet1_0" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 31 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "squeezenet1_1" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 32 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "squeezenet1_1" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 33 | inceptions_v3: 34 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "inception_v3" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) -is 299 35 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "inception_v3" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) -is 299 36 | vggs_base: 37 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "vgg11" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 38 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "vgg11" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 39 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "vgg13" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 40 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "vgg13" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 41 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "vgg16" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 42 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "vgg16" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 43 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "vgg19" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 44 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "vgg19" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 45 | vggs_bn: 46 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "vgg11_bn" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 47 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "vgg11_bn" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 48 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "vgg13_bn" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 49 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "vgg13_bn" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 50 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "vgg16_bn" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 51 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "vgg16_bn" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 52 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "vgg19_bn" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 53 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "vgg19_bn" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 54 | alexnets: 55 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "alexnet" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 56 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "alexnet" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) 57 | inceptionresnets: 58 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "inceptionresnetv2" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) -is 299 -b 16 59 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "inceptionresnetv2" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) -is 299 -b 16 60 | inceptions_v4: 61 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "inceptionv4" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) -is 299 -b 16 62 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "inceptionv4" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) -is 299 -b 16 63 | inceptions_bn: 64 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "bninception" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) -b 16 65 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "bninception" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) -b 16 66 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "inception_v3" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) -is 299 67 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "inception_v3" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) -is 299 68 | resnext: 69 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "resnext101_64x4d" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) -b 16 70 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "resnext101_64x4d" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) -b 16 71 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "resnext101_32x4d" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) -b 16 72 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "resnext101_32x4d" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) -b 16 73 | nasnets: 74 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "nasnetalarge" $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) -is 331 -b 8 75 | python tars_train.py $(DATA_LOC) -sl $(SAVE_LOC) -mo "nasnetalarge" -f $(NUM_EPOCHS) $(MIXUP) $(MIXUP_ALPHA) -is 331 -b 8 76 | -------------------------------------------------------------------------------- /Makefile.train2: -------------------------------------------------------------------------------- 1 | python: 2 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "resnet18" -ep 1 3 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "resnet18" -f -ep 1 4 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "resnet34" -ep 1 5 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "resnet34" -f -ep 1 6 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "resnet50" -ep 1 7 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "resnet50" -f -ep 1 8 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "resnet101" -ep 1 9 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "resnet101" -f -ep 1 10 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "resnet152" -ep 1 11 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "resnet152" -f -ep 1 12 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "densenet121" -ep 1 13 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "densenet121" -f -ep 1 14 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "densenet169" -ep 1 15 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "densenet169" -f -ep 1 16 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "densenet201" -ep 1 17 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "densenet201" -f -ep 1 18 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "squeezenet1_0" -ep 1 19 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "squeezenet1_0" -f -ep 1 20 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "squeezenet1_1" -ep 1 21 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "squeezenet1_1" -f -ep 1 22 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "vgg11" -ep 1 23 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "vgg11" -f -ep 1 24 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "vgg13" -ep 1 25 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "vgg13" -f -ep 1 26 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "vgg16" -ep 1 27 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "vgg16" -f -ep 1 28 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "vgg19" -ep 1 29 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "vgg19" -f -ep 1 30 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "vgg11_bn" -ep 1 31 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "vgg11_bn" -f -ep 1 32 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "vgg13_bn" -ep 1 33 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "vgg13_bn" -f -ep 1 34 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "vgg16_bn" -ep 1 35 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "vgg16_bn" -f -ep 1 36 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "vgg19_bn" -ep 1 37 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "vgg19_bn" -f -ep 1 38 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "alexnet" -ep 1 39 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "alexnet" -f -ep 1 40 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "resnext101_64x4d" -ep 1 -b 16 41 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "resnext101_64x4d" -f -ep 1 -b 16 42 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "resnext101_32x4d" -ep 1 -b 16 43 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "resnext101_32x4d" -f -ep 1 -b 16 44 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "nasnetalarge" -ep 1 -is 331 -b 8 45 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "nasnetalarge" -f -ep 1 -is 331 -b 8 46 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "inceptionresnetv2" -ep 1 -is 299 47 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "inceptionresnetv2" -f -ep 1 -is 299 48 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "inceptionv4" -ep 1 -is 299 49 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "inceptionv4" -f -ep 1 -is 299 50 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "bninception" -ep 1 51 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "bninception" -f -ep 1 52 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "inception_v3" -ep 1 -is 299 53 | python tars_train.py -idl "data/training_data" -sl "models/" -mo "inception_v3" -f -ep 1 -is 299 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## This is an experimental setup to build code base for pytorch. Its main is to experiment faster using transfer learning on all available pre-trained models. 2 | 3 | ### Dataset: Plant Seedlings classification 4 | 5 | Classes present: 6 | ----------------- 7 | - Black-grass 8 | - Charlock 9 | - Cleavers 10 | - Common Chickweed 11 | - Common wheat 12 | - Fat Hen 13 | - Loose Silky-bent 14 | - Maize 15 | - Scentless Mayweed 16 | - Shepherds Purse 17 | - Small-flowered Cranesbill 18 | - Sugar beet 19 | 20 | #### Run the following commands 21 | To train the models: 22 | ``` 23 | make -f Makefile.train 24 | ``` 25 | 26 | To test the models: 27 | ``` 28 | make -f Makefile.predict 29 | ``` 30 | 31 | 32 | ## Results 33 | total number of train images: 4268 34 | total number of val images: 482 35 | total number of test images: 794 36 | 37 | 38 | # Results with Full-Agumentation strategy: 39 | 40 | ## Trained the networks in three methods: 41 | - Full Finetuning 42 | - Freeze first few layers 43 | 44 | ## Case-1 - Finetuning entire network 45 | Models | Train Accuracy_score | Val Accuracy_score | 46 | ------------- | :------------------: | --------------------: | 47 | resnet18 | 0.92783 | 0.93153 | 48 | resnet34 | 0.9522 | 0.94190 | 49 | resnet50 | 0.95665 | 0.94398 | 50 | resnet101 | 0.96696 | 0.96265 | 51 | resnet152 | 0.96555 | 0.95643 | 52 | squeezenet1_0 | 0.94329 | 0.92738 | 53 | squeezenet1_1 | 0.93955 | 0.93153 | 54 | densenet121 | 0.95243 | 0.9336 | 55 | densenet169 | 0.96626 | 0.93983 | 56 | densenet201 | 0.96063 | 0.95020 | 57 | inception_v3 | 0.94212 | 0.93568 | 58 | vgg11 | 0.93814 | 0.93153 | 59 | vgg13 | 0.94493 | 0.94190 | 60 | vgg16 | 0.95665 | 0.93568 | 61 | vgg19 | 0.95009 | 0.93775 | 62 | vgg11_bn | 0.94142 | 0.93775 | 63 | vgg13_bn | 0.94423 | 0.92738 | 64 | vgg16_bn | 0.94634 | 0.94190 | 65 | vgg19_bn | 0.94915 | 0.93360 | 66 | alexnet | 0.91260 | 0.90456 | 67 | resnext101_64x4d | 0.98055 | 0.96887 | 68 | resnext101_32x4d | 0.98172 | 0.96887 | 69 | nasnetalarge | 0.96907 | 0.96265 | 70 | inceptionresnetv2| 0.96134 | 0.95435 | 71 | inceptionv4 | 0.96930 | 0.96473 | 72 | 73 | 74 | ## Case-2 - Freezed first few layers(look at code) 75 | Models | Train Accuracy_score | Val Accuracy_score | 76 | ------------- | :------------------: | ----------------------: 77 | resnet18 | 0.9196 | 0.91493 | 78 | resnet34 | 0.94845 | 0.93983 | 79 | resnet50 | 0.9564 | 0.93983 | 80 | resnet101 | 0.96790 | 0.96265 | 81 | resnet152 | 0.96508 | 0.95643 | 82 | squeezenet1_0 | 0.94048 | 0.92738 | 83 | squeezenet1_1 | 0.93088 | 0.92116 | 84 | densenet121 | 0.95173 | 0.95228 | 85 | densenet169 | 0.96087 | 0.94813 | 86 | densenet201 | 0.95384 | 0.95020 | 87 | inception_v3 | 0.94025 | 0.93775 | 88 | vgg11 | 0.93697 | 0.92946 | 89 | vgg13 | 0.93533 | 0.92323 | 90 | vgg16 | 0.94821 | 0.93983 | 91 | vgg19 | 0.95243 | 0.94190 | 92 | vgg11_bn | 0.93416 | 0.92738 | 93 | vgg13_bn | 0.93322 | 0.92323 | 94 | vgg16_bn | 0.94728 | 0.93775 | 95 | vgg19_bn | 0.94798 | 0.94190 | 96 | alexnet | 0.89784 | 0.88589 | 97 | resnext101_64x4d | 0.98617 | 0.96887 | 98 | resnext101_32x4d | 0.98195 | 0.96473 | 99 | nasnetalarge | 0.95970 | 0.96265 | 100 | inceptionresnetv2| 0.95103 | 0.94813 | 101 | inceptionv4 | 0.96251 | 0.93775 | 102 | 103 | 104 | # Results with Basic Agumentation 105 | 106 | ## Case-1 (Freezed all layers except last one) 107 | Models | Train Accuracy_score | Val Accuracy_score | 108 | ------------- | :------------------: | --------------------: | 109 | resnet18 | 0.77553 | 0.75518 | 110 | resnet152 | 0.82778 | 0.81535 | 111 | resnet101 | 0.82333 | 0.80290 | 112 | resnet50 | 0.79943 | 0.78630 | 113 | resnet34 | 0.78655 | 0.74688 | 114 | squeezenet1_0 | 0.91447 | 0.87966 | 115 | squeezenet1_1 | 0.90089 | 0.87344 | 116 | densenet121 | 0.80880 | 0.81120 | 117 | densenet169 | 0.84746 | 0.82987 | 118 | densenet201 | 0.86621 | 0.86514 | 119 | inception_v3 | 0.76101 | 0.74688 | 120 | vgg11 | 0.78209 | 0.78008 | 121 | vgg13 | 0.75960 | 0.72821 | 122 | vgg16 | 0.77038 | 0.71576 | 123 | vgg19 | 0.71204 | 0.64522 | 124 | vgg11_bn | 0.76522 | 0.74481 | 125 | vgg13_bn | 0.76241 | 0.76348 | 126 | vgg16_bn | 0.76265 | 0.75726 | 127 | vgg19_bn | 0.75773 | 0.73858 | 128 | alexnet | 0.83153 | 0.76348 | 129 | 130 | 131 | ## Case-2 Finetuning the entire network 132 | Models | Train Accuracy_score | Val Accuracy_score | 133 | ------------- | :------------------: | ----------------------: 134 | resnet18 | 0.98477 | 0.96058 | 135 | resnet152 | 0.99273 | 0.97717 | 136 | resnet101 | 0.99367 | 0.97717 | 137 | resnet50 | 0.99133 | 0.97510 | 138 | resnet34 | 0.98969 | 0.97095 | 139 | squeezenet1_0 | 0.96274 | 0.94190 | 140 | squeezenet1_1 | 0.96485 | 0.92738 | 141 | densenet121 | 0.99086 | 0.96887 | 142 | densenet169 | 0.99507 | 0.97510 | 143 | densenet201 | 0.99390 | 0.97717 | 144 | inception_v3 | 0.98898 | 0.97302 | 145 | vgg11 | 0.98031 | 0.95020 | 146 | vgg13 | 0.98078 | 0.95643 | 147 | vgg16 | 0.98266 | 0.95435 | 148 | vgg19 | 0.98430 | 0.95643 | 149 | vgg11_bn | 0.98570 | 0.96265 | 150 | vgg13_bn | 0.98687 | 0.97095 | 151 | vgg16_bn | 0.99179 | 0.96680 | 152 | vgg19_bn | 0.99297 | 0.96680 | 153 | alexnet | 0.95970 | 0.92946 | 154 | 155 | 156 | ## Submissions: 157 | - densenet201 LB - to 97.22 158 | - ensemble1 - mode of all best performing models LB - 97.32 159 | 160 | ## TO_DO 161 | 1) Adding mixup strategy to all the networks 162 | 2) Ensembling model outputs 163 | 3) Model stacking 164 | 4) Extracting bottleneck features and using 165 | - ML to train the model 166 | - Visualization using T-sne 167 | 5) Solve issue with bninception(Model is not training) 168 | 6) Train Vggm network 169 | -------------------------------------------------------------------------------- /data/data_prep.py: -------------------------------------------------------------------------------- 1 | """ Data Preparation 2 | 3 | Split the data into training and validation 4 | """ 5 | import glob, cv2, os 6 | from shutil import copyfile 7 | from sklearn.model_selection import train_test_split 8 | from tqdm import tqdm 9 | 10 | 11 | def split_train_val(input_folder, create_folder, train_folder_name, val_folder_name, test_size=0.1, input_image_format=".png"): 12 | """ 13 | Given Input folder which contains folders of images with different classes, This will create a folder (create_folder) and divide the dataset into train and val according to the test_size and copy them to train_folder_name and val_folder_name which are inside the create folder. 14 | 15 | Mention input_image_format and test_size if the defaults doesn't work for you. 16 | 17 | """ 18 | if not os.path.exists(create_folder): 19 | os.makedirs(create_folder) 20 | 21 | files = glob.glob(input_folder+"/*/") 22 | files = [glob.glob(i+"/*.png") for i in files] 23 | 24 | print("files in each folder", [len(i) for i in files]) 25 | print("Names of the files", [files[i][0].rsplit("/")[-2] for i in range(len(files))]) 26 | 27 | for i in tqdm(range(len(files))): 28 | x = files[i] 29 | train, test = train_test_split(x, test_size=0.1, random_state = 42) 30 | train_loc = train[0].rsplit("/")[1] 31 | train_loc = create_folder+"/"+train_folder_name+"/"+train_loc 32 | if not os.path.exists(train_loc): 33 | os.makedirs(train_loc) 34 | for j in train: 35 | copyfile(j, train_loc+"/"+j.rsplit("/")[2]) 36 | test_loc = test[0].rsplit("/")[1] 37 | test_loc = create_folder+"/"+val_folder_name+"/"+test_loc 38 | if not os.path.exists(test_loc): 39 | os.makedirs(test_loc) 40 | for m in test: 41 | copyfile(m, test_loc+"/"+m.rsplit("/")[2]) 42 | 43 | files = glob.glob(create_folder+"/"+train_folder_name+"/*/") 44 | files = [glob.glob(i+"/*"+input_image_format) for i in files] 45 | print("total number of files in train folder: {}".format(sum([len(i) for i in files]))) 46 | 47 | files = glob.glob(create_folder+"/"+val_folder_name+"/*/") 48 | files = [glob.glob(i+"/*"+input_image_format) for i in files] 49 | print("total number of files in val folder: {}".format(sum([len(i) for i in files]))) 50 | 51 | 52 | if __name__ == '__main__': 53 | split_train_val("train", "training_data", "train", "val") 54 | -------------------------------------------------------------------------------- /tars/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prakashjayy/pytorch_classifiers/ed1fc54c427f3fa52b5fa58bd013018d6ad40eff/tars/__init__.py -------------------------------------------------------------------------------- /tars/inceptionresnetv2.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.utils.model_zoo as model_zoo 4 | import os 5 | import sys 6 | 7 | __all__ = ['InceptionResNetV2', 'inceptionresnetv2'] 8 | 9 | pretrained_settings = { 10 | 'inceptionresnetv2': { 11 | 'imagenet': { 12 | 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/inceptionresnetv2-520b38e4.pth', 13 | 'input_space': 'RGB', 14 | 'input_size': [3, 299, 299], 15 | 'input_range': [0, 1], 16 | 'mean': [0.5, 0.5, 0.5], 17 | 'std': [0.5, 0.5, 0.5], 18 | 'num_classes': 1000 19 | }, 20 | 'imagenet+background': { 21 | 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/inceptionresnetv2-520b38e4.pth', 22 | 'input_space': 'RGB', 23 | 'input_size': [3, 299, 299], 24 | 'input_range': [0, 1], 25 | 'mean': [0.5, 0.5, 0.5], 26 | 'std': [0.5, 0.5, 0.5], 27 | 'num_classes': 1001 28 | } 29 | } 30 | } 31 | 32 | 33 | class BasicConv2d(nn.Module): 34 | 35 | def __init__(self, in_planes, out_planes, kernel_size, stride, padding=0): 36 | super(BasicConv2d, self).__init__() 37 | self.conv = nn.Conv2d(in_planes, out_planes, 38 | kernel_size=kernel_size, stride=stride, 39 | padding=padding, bias=False) # verify bias false 40 | self.bn = nn.BatchNorm2d(out_planes, 41 | eps=0.001, # value found in tensorflow 42 | momentum=0.1, # default pytorch value 43 | affine=True) 44 | self.relu = nn.ReLU(inplace=False) 45 | 46 | def forward(self, x): 47 | x = self.conv(x) 48 | x = self.bn(x) 49 | x = self.relu(x) 50 | return x 51 | 52 | 53 | class Mixed_5b(nn.Module): 54 | 55 | def __init__(self): 56 | super(Mixed_5b, self).__init__() 57 | 58 | self.branch0 = BasicConv2d(192, 96, kernel_size=1, stride=1) 59 | 60 | self.branch1 = nn.Sequential( 61 | BasicConv2d(192, 48, kernel_size=1, stride=1), 62 | BasicConv2d(48, 64, kernel_size=5, stride=1, padding=2) 63 | ) 64 | 65 | self.branch2 = nn.Sequential( 66 | BasicConv2d(192, 64, kernel_size=1, stride=1), 67 | BasicConv2d(64, 96, kernel_size=3, stride=1, padding=1), 68 | BasicConv2d(96, 96, kernel_size=3, stride=1, padding=1) 69 | ) 70 | 71 | self.branch3 = nn.Sequential( 72 | nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False), 73 | BasicConv2d(192, 64, kernel_size=1, stride=1) 74 | ) 75 | 76 | def forward(self, x): 77 | x0 = self.branch0(x) 78 | x1 = self.branch1(x) 79 | x2 = self.branch2(x) 80 | x3 = self.branch3(x) 81 | out = torch.cat((x0, x1, x2, x3), 1) 82 | return out 83 | 84 | 85 | class Block35(nn.Module): 86 | 87 | def __init__(self, scale=1.0): 88 | super(Block35, self).__init__() 89 | 90 | self.scale = scale 91 | 92 | self.branch0 = BasicConv2d(320, 32, kernel_size=1, stride=1) 93 | 94 | self.branch1 = nn.Sequential( 95 | BasicConv2d(320, 32, kernel_size=1, stride=1), 96 | BasicConv2d(32, 32, kernel_size=3, stride=1, padding=1) 97 | ) 98 | 99 | self.branch2 = nn.Sequential( 100 | BasicConv2d(320, 32, kernel_size=1, stride=1), 101 | BasicConv2d(32, 48, kernel_size=3, stride=1, padding=1), 102 | BasicConv2d(48, 64, kernel_size=3, stride=1, padding=1) 103 | ) 104 | 105 | self.conv2d = nn.Conv2d(128, 320, kernel_size=1, stride=1) 106 | self.relu = nn.ReLU(inplace=False) 107 | 108 | def forward(self, x): 109 | x0 = self.branch0(x) 110 | x1 = self.branch1(x) 111 | x2 = self.branch2(x) 112 | out = torch.cat((x0, x1, x2), 1) 113 | out = self.conv2d(out) 114 | out = out * self.scale + x 115 | out = self.relu(out) 116 | return out 117 | 118 | 119 | class Mixed_6a(nn.Module): 120 | 121 | def __init__(self): 122 | super(Mixed_6a, self).__init__() 123 | 124 | self.branch0 = BasicConv2d(320, 384, kernel_size=3, stride=2) 125 | 126 | self.branch1 = nn.Sequential( 127 | BasicConv2d(320, 256, kernel_size=1, stride=1), 128 | BasicConv2d(256, 256, kernel_size=3, stride=1, padding=1), 129 | BasicConv2d(256, 384, kernel_size=3, stride=2) 130 | ) 131 | 132 | self.branch2 = nn.MaxPool2d(3, stride=2) 133 | 134 | def forward(self, x): 135 | x0 = self.branch0(x) 136 | x1 = self.branch1(x) 137 | x2 = self.branch2(x) 138 | out = torch.cat((x0, x1, x2), 1) 139 | return out 140 | 141 | 142 | class Block17(nn.Module): 143 | 144 | def __init__(self, scale=1.0): 145 | super(Block17, self).__init__() 146 | 147 | self.scale = scale 148 | 149 | self.branch0 = BasicConv2d(1088, 192, kernel_size=1, stride=1) 150 | 151 | self.branch1 = nn.Sequential( 152 | BasicConv2d(1088, 128, kernel_size=1, stride=1), 153 | BasicConv2d(128, 160, kernel_size=(1,7), stride=1, padding=(0,3)), 154 | BasicConv2d(160, 192, kernel_size=(7,1), stride=1, padding=(3,0)) 155 | ) 156 | 157 | self.conv2d = nn.Conv2d(384, 1088, kernel_size=1, stride=1) 158 | self.relu = nn.ReLU(inplace=False) 159 | 160 | def forward(self, x): 161 | x0 = self.branch0(x) 162 | x1 = self.branch1(x) 163 | out = torch.cat((x0, x1), 1) 164 | out = self.conv2d(out) 165 | out = out * self.scale + x 166 | out = self.relu(out) 167 | return out 168 | 169 | 170 | class Mixed_7a(nn.Module): 171 | 172 | def __init__(self): 173 | super(Mixed_7a, self).__init__() 174 | 175 | self.branch0 = nn.Sequential( 176 | BasicConv2d(1088, 256, kernel_size=1, stride=1), 177 | BasicConv2d(256, 384, kernel_size=3, stride=2) 178 | ) 179 | 180 | self.branch1 = nn.Sequential( 181 | BasicConv2d(1088, 256, kernel_size=1, stride=1), 182 | BasicConv2d(256, 288, kernel_size=3, stride=2) 183 | ) 184 | 185 | self.branch2 = nn.Sequential( 186 | BasicConv2d(1088, 256, kernel_size=1, stride=1), 187 | BasicConv2d(256, 288, kernel_size=3, stride=1, padding=1), 188 | BasicConv2d(288, 320, kernel_size=3, stride=2) 189 | ) 190 | 191 | self.branch3 = nn.MaxPool2d(3, stride=2) 192 | 193 | def forward(self, x): 194 | x0 = self.branch0(x) 195 | x1 = self.branch1(x) 196 | x2 = self.branch2(x) 197 | x3 = self.branch3(x) 198 | out = torch.cat((x0, x1, x2, x3), 1) 199 | return out 200 | 201 | 202 | class Block8(nn.Module): 203 | 204 | def __init__(self, scale=1.0, noReLU=False): 205 | super(Block8, self).__init__() 206 | 207 | self.scale = scale 208 | self.noReLU = noReLU 209 | 210 | self.branch0 = BasicConv2d(2080, 192, kernel_size=1, stride=1) 211 | 212 | self.branch1 = nn.Sequential( 213 | BasicConv2d(2080, 192, kernel_size=1, stride=1), 214 | BasicConv2d(192, 224, kernel_size=(1,3), stride=1, padding=(0,1)), 215 | BasicConv2d(224, 256, kernel_size=(3,1), stride=1, padding=(1,0)) 216 | ) 217 | 218 | self.conv2d = nn.Conv2d(448, 2080, kernel_size=1, stride=1) 219 | if not self.noReLU: 220 | self.relu = nn.ReLU(inplace=False) 221 | 222 | def forward(self, x): 223 | x0 = self.branch0(x) 224 | x1 = self.branch1(x) 225 | out = torch.cat((x0, x1), 1) 226 | out = self.conv2d(out) 227 | out = out * self.scale + x 228 | if not self.noReLU: 229 | out = self.relu(out) 230 | return out 231 | 232 | 233 | class InceptionResNetV2(nn.Module): 234 | 235 | def __init__(self, num_classes=1001): 236 | super(InceptionResNetV2, self).__init__() 237 | # Special attributs 238 | self.input_space = None 239 | self.input_size = (299, 299, 3) 240 | self.mean = None 241 | self.std = None 242 | # Modules 243 | self.conv2d_1a = BasicConv2d(3, 32, kernel_size=3, stride=2) 244 | self.conv2d_2a = BasicConv2d(32, 32, kernel_size=3, stride=1) 245 | self.conv2d_2b = BasicConv2d(32, 64, kernel_size=3, stride=1, padding=1) 246 | self.maxpool_3a = nn.MaxPool2d(3, stride=2) 247 | self.conv2d_3b = BasicConv2d(64, 80, kernel_size=1, stride=1) 248 | self.conv2d_4a = BasicConv2d(80, 192, kernel_size=3, stride=1) 249 | self.maxpool_5a = nn.MaxPool2d(3, stride=2) 250 | self.mixed_5b = Mixed_5b() 251 | self.repeat = nn.Sequential( 252 | Block35(scale=0.17), 253 | Block35(scale=0.17), 254 | Block35(scale=0.17), 255 | Block35(scale=0.17), 256 | Block35(scale=0.17), 257 | Block35(scale=0.17), 258 | Block35(scale=0.17), 259 | Block35(scale=0.17), 260 | Block35(scale=0.17), 261 | Block35(scale=0.17) 262 | ) 263 | self.mixed_6a = Mixed_6a() 264 | self.repeat_1 = nn.Sequential( 265 | Block17(scale=0.10), 266 | Block17(scale=0.10), 267 | Block17(scale=0.10), 268 | Block17(scale=0.10), 269 | Block17(scale=0.10), 270 | Block17(scale=0.10), 271 | Block17(scale=0.10), 272 | Block17(scale=0.10), 273 | Block17(scale=0.10), 274 | Block17(scale=0.10), 275 | Block17(scale=0.10), 276 | Block17(scale=0.10), 277 | Block17(scale=0.10), 278 | Block17(scale=0.10), 279 | Block17(scale=0.10), 280 | Block17(scale=0.10), 281 | Block17(scale=0.10), 282 | Block17(scale=0.10), 283 | Block17(scale=0.10), 284 | Block17(scale=0.10) 285 | ) 286 | self.mixed_7a = Mixed_7a() 287 | self.repeat_2 = nn.Sequential( 288 | Block8(scale=0.20), 289 | Block8(scale=0.20), 290 | Block8(scale=0.20), 291 | Block8(scale=0.20), 292 | Block8(scale=0.20), 293 | Block8(scale=0.20), 294 | Block8(scale=0.20), 295 | Block8(scale=0.20), 296 | Block8(scale=0.20) 297 | ) 298 | self.block8 = Block8(noReLU=True) 299 | self.conv2d_7b = BasicConv2d(2080, 1536, kernel_size=1, stride=1) 300 | self.avgpool_1a = nn.AvgPool2d(8, count_include_pad=False) 301 | self.last_linear = nn.Linear(1536, num_classes) 302 | 303 | def features(self, input): 304 | x = self.conv2d_1a(input) 305 | x = self.conv2d_2a(x) 306 | x = self.conv2d_2b(x) 307 | x = self.maxpool_3a(x) 308 | x = self.conv2d_3b(x) 309 | x = self.conv2d_4a(x) 310 | x = self.maxpool_5a(x) 311 | x = self.mixed_5b(x) 312 | x = self.repeat(x) 313 | x = self.mixed_6a(x) 314 | x = self.repeat_1(x) 315 | x = self.mixed_7a(x) 316 | x = self.repeat_2(x) 317 | x = self.block8(x) 318 | x = self.conv2d_7b(x) 319 | return x 320 | 321 | def logits(self, features): 322 | x = self.avgpool_1a(features) 323 | x = x.view(x.size(0), -1) 324 | x = self.last_linear(x) 325 | return x 326 | 327 | def forward(self, input): 328 | x = self.features(input) 329 | x = self.logits(x) 330 | return x 331 | 332 | def inceptionresnetv2(num_classes=1001, pretrained='imagenet'): 333 | r"""InceptionResNetV2 model architecture from the 334 | `"InceptionV4, Inception-ResNet..." `_ paper. 335 | """ 336 | if pretrained: 337 | settings = pretrained_settings['inceptionresnetv2'][pretrained] 338 | assert num_classes == settings['num_classes'], \ 339 | "num_classes should be {}, but is {}".format(settings['num_classes'], num_classes) 340 | 341 | # both 'imagenet'&'imagenet+background' are loaded from same parameters 342 | model = InceptionResNetV2(num_classes=1001) 343 | model.load_state_dict(model_zoo.load_url(settings['url'])) 344 | 345 | if pretrained == 'imagenet': 346 | new_last_linear = nn.Linear(1536, 1000) 347 | new_last_linear.weight.data = model.last_linear.weight.data[1:] 348 | new_last_linear.bias.data = model.last_linear.bias.data[1:] 349 | model.last_linear = new_last_linear 350 | 351 | model.input_space = settings['input_space'] 352 | model.input_size = settings['input_size'] 353 | model.input_range = settings['input_range'] 354 | 355 | model.mean = settings['mean'] 356 | model.std = settings['std'] 357 | else: 358 | model = InceptionResNetV2(num_classes=num_classes) 359 | return model 360 | 361 | ''' 362 | TEST 363 | Run this code with: 364 | ``` 365 | cd $HOME/pretrained-models.pytorch 366 | python -m pretrainedmodels.inceptionresnetv2 367 | ``` 368 | ''' 369 | if __name__ == '__main__': 370 | 371 | assert inceptionresnetv2(num_classes=10, pretrained=None) 372 | print('success') 373 | assert inceptionresnetv2(num_classes=1000, pretrained='imagenet') 374 | print('success') 375 | assert inceptionresnetv2(num_classes=1001, pretrained='imagenet+background') 376 | print('success') 377 | 378 | # fail 379 | assert inceptionresnetv2(num_classes=1001, pretrained='imagenet') -------------------------------------------------------------------------------- /tars/inceptionv4.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.utils.model_zoo as model_zoo 4 | import os 5 | import sys 6 | 7 | __all__ = ['InceptionV4', 'inceptionv4'] 8 | 9 | pretrained_settings = { 10 | 'inceptionv4': { 11 | 'imagenet': { 12 | 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/inceptionv4-8e4777a0.pth', 13 | 'input_space': 'RGB', 14 | 'input_size': [3, 299, 299], 15 | 'input_range': [0, 1], 16 | 'mean': [0.5, 0.5, 0.5], 17 | 'std': [0.5, 0.5, 0.5], 18 | 'num_classes': 1000 19 | }, 20 | 'imagenet+background': { 21 | 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/inceptionv4-8e4777a0.pth', 22 | 'input_space': 'RGB', 23 | 'input_size': [3, 299, 299], 24 | 'input_range': [0, 1], 25 | 'mean': [0.5, 0.5, 0.5], 26 | 'std': [0.5, 0.5, 0.5], 27 | 'num_classes': 1001 28 | } 29 | } 30 | } 31 | 32 | 33 | class BasicConv2d(nn.Module): 34 | 35 | def __init__(self, in_planes, out_planes, kernel_size, stride, padding=0): 36 | super(BasicConv2d, self).__init__() 37 | self.conv = nn.Conv2d(in_planes, out_planes, 38 | kernel_size=kernel_size, stride=stride, 39 | padding=padding, bias=False) # verify bias false 40 | self.bn = nn.BatchNorm2d(out_planes, 41 | eps=0.001, # value found in tensorflow 42 | momentum=0.1, # default pytorch value 43 | affine=True) 44 | self.relu = nn.ReLU(inplace=True) 45 | 46 | def forward(self, x): 47 | x = self.conv(x) 48 | x = self.bn(x) 49 | x = self.relu(x) 50 | return x 51 | 52 | 53 | class Mixed_3a(nn.Module): 54 | 55 | def __init__(self): 56 | super(Mixed_3a, self).__init__() 57 | self.maxpool = nn.MaxPool2d(3, stride=2) 58 | self.conv = BasicConv2d(64, 96, kernel_size=3, stride=2) 59 | 60 | def forward(self, x): 61 | x0 = self.maxpool(x) 62 | x1 = self.conv(x) 63 | out = torch.cat((x0, x1), 1) 64 | return out 65 | 66 | 67 | class Mixed_4a(nn.Module): 68 | 69 | def __init__(self): 70 | super(Mixed_4a, self).__init__() 71 | 72 | self.branch0 = nn.Sequential( 73 | BasicConv2d(160, 64, kernel_size=1, stride=1), 74 | BasicConv2d(64, 96, kernel_size=3, stride=1) 75 | ) 76 | 77 | self.branch1 = nn.Sequential( 78 | BasicConv2d(160, 64, kernel_size=1, stride=1), 79 | BasicConv2d(64, 64, kernel_size=(1,7), stride=1, padding=(0,3)), 80 | BasicConv2d(64, 64, kernel_size=(7,1), stride=1, padding=(3,0)), 81 | BasicConv2d(64, 96, kernel_size=(3,3), stride=1) 82 | ) 83 | 84 | def forward(self, x): 85 | x0 = self.branch0(x) 86 | x1 = self.branch1(x) 87 | out = torch.cat((x0, x1), 1) 88 | return out 89 | 90 | 91 | class Mixed_5a(nn.Module): 92 | 93 | def __init__(self): 94 | super(Mixed_5a, self).__init__() 95 | self.conv = BasicConv2d(192, 192, kernel_size=3, stride=2) 96 | self.maxpool = nn.MaxPool2d(3, stride=2) 97 | 98 | def forward(self, x): 99 | x0 = self.conv(x) 100 | x1 = self.maxpool(x) 101 | out = torch.cat((x0, x1), 1) 102 | return out 103 | 104 | 105 | class Inception_A(nn.Module): 106 | 107 | def __init__(self): 108 | super(Inception_A, self).__init__() 109 | self.branch0 = BasicConv2d(384, 96, kernel_size=1, stride=1) 110 | 111 | self.branch1 = nn.Sequential( 112 | BasicConv2d(384, 64, kernel_size=1, stride=1), 113 | BasicConv2d(64, 96, kernel_size=3, stride=1, padding=1) 114 | ) 115 | 116 | self.branch2 = nn.Sequential( 117 | BasicConv2d(384, 64, kernel_size=1, stride=1), 118 | BasicConv2d(64, 96, kernel_size=3, stride=1, padding=1), 119 | BasicConv2d(96, 96, kernel_size=3, stride=1, padding=1) 120 | ) 121 | 122 | self.branch3 = nn.Sequential( 123 | nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False), 124 | BasicConv2d(384, 96, kernel_size=1, stride=1) 125 | ) 126 | 127 | def forward(self, x): 128 | x0 = self.branch0(x) 129 | x1 = self.branch1(x) 130 | x2 = self.branch2(x) 131 | x3 = self.branch3(x) 132 | out = torch.cat((x0, x1, x2, x3), 1) 133 | return out 134 | 135 | 136 | class Reduction_A(nn.Module): 137 | 138 | def __init__(self): 139 | super(Reduction_A, self).__init__() 140 | self.branch0 = BasicConv2d(384, 384, kernel_size=3, stride=2) 141 | 142 | self.branch1 = nn.Sequential( 143 | BasicConv2d(384, 192, kernel_size=1, stride=1), 144 | BasicConv2d(192, 224, kernel_size=3, stride=1, padding=1), 145 | BasicConv2d(224, 256, kernel_size=3, stride=2) 146 | ) 147 | 148 | self.branch2 = nn.MaxPool2d(3, stride=2) 149 | 150 | def forward(self, x): 151 | x0 = self.branch0(x) 152 | x1 = self.branch1(x) 153 | x2 = self.branch2(x) 154 | out = torch.cat((x0, x1, x2), 1) 155 | return out 156 | 157 | 158 | class Inception_B(nn.Module): 159 | 160 | def __init__(self): 161 | super(Inception_B, self).__init__() 162 | self.branch0 = BasicConv2d(1024, 384, kernel_size=1, stride=1) 163 | 164 | self.branch1 = nn.Sequential( 165 | BasicConv2d(1024, 192, kernel_size=1, stride=1), 166 | BasicConv2d(192, 224, kernel_size=(1,7), stride=1, padding=(0,3)), 167 | BasicConv2d(224, 256, kernel_size=(7,1), stride=1, padding=(3,0)) 168 | ) 169 | 170 | self.branch2 = nn.Sequential( 171 | BasicConv2d(1024, 192, kernel_size=1, stride=1), 172 | BasicConv2d(192, 192, kernel_size=(7,1), stride=1, padding=(3,0)), 173 | BasicConv2d(192, 224, kernel_size=(1,7), stride=1, padding=(0,3)), 174 | BasicConv2d(224, 224, kernel_size=(7,1), stride=1, padding=(3,0)), 175 | BasicConv2d(224, 256, kernel_size=(1,7), stride=1, padding=(0,3)) 176 | ) 177 | 178 | self.branch3 = nn.Sequential( 179 | nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False), 180 | BasicConv2d(1024, 128, kernel_size=1, stride=1) 181 | ) 182 | 183 | def forward(self, x): 184 | x0 = self.branch0(x) 185 | x1 = self.branch1(x) 186 | x2 = self.branch2(x) 187 | x3 = self.branch3(x) 188 | out = torch.cat((x0, x1, x2, x3), 1) 189 | return out 190 | 191 | 192 | class Reduction_B(nn.Module): 193 | 194 | def __init__(self): 195 | super(Reduction_B, self).__init__() 196 | 197 | self.branch0 = nn.Sequential( 198 | BasicConv2d(1024, 192, kernel_size=1, stride=1), 199 | BasicConv2d(192, 192, kernel_size=3, stride=2) 200 | ) 201 | 202 | self.branch1 = nn.Sequential( 203 | BasicConv2d(1024, 256, kernel_size=1, stride=1), 204 | BasicConv2d(256, 256, kernel_size=(1,7), stride=1, padding=(0,3)), 205 | BasicConv2d(256, 320, kernel_size=(7,1), stride=1, padding=(3,0)), 206 | BasicConv2d(320, 320, kernel_size=3, stride=2) 207 | ) 208 | 209 | self.branch2 = nn.MaxPool2d(3, stride=2) 210 | 211 | def forward(self, x): 212 | x0 = self.branch0(x) 213 | x1 = self.branch1(x) 214 | x2 = self.branch2(x) 215 | out = torch.cat((x0, x1, x2), 1) 216 | return out 217 | 218 | 219 | class Inception_C(nn.Module): 220 | 221 | def __init__(self): 222 | super(Inception_C, self).__init__() 223 | 224 | self.branch0 = BasicConv2d(1536, 256, kernel_size=1, stride=1) 225 | 226 | self.branch1_0 = BasicConv2d(1536, 384, kernel_size=1, stride=1) 227 | self.branch1_1a = BasicConv2d(384, 256, kernel_size=(1,3), stride=1, padding=(0,1)) 228 | self.branch1_1b = BasicConv2d(384, 256, kernel_size=(3,1), stride=1, padding=(1,0)) 229 | 230 | self.branch2_0 = BasicConv2d(1536, 384, kernel_size=1, stride=1) 231 | self.branch2_1 = BasicConv2d(384, 448, kernel_size=(3,1), stride=1, padding=(1,0)) 232 | self.branch2_2 = BasicConv2d(448, 512, kernel_size=(1,3), stride=1, padding=(0,1)) 233 | self.branch2_3a = BasicConv2d(512, 256, kernel_size=(1,3), stride=1, padding=(0,1)) 234 | self.branch2_3b = BasicConv2d(512, 256, kernel_size=(3,1), stride=1, padding=(1,0)) 235 | 236 | self.branch3 = nn.Sequential( 237 | nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False), 238 | BasicConv2d(1536, 256, kernel_size=1, stride=1) 239 | ) 240 | 241 | def forward(self, x): 242 | x0 = self.branch0(x) 243 | 244 | x1_0 = self.branch1_0(x) 245 | x1_1a = self.branch1_1a(x1_0) 246 | x1_1b = self.branch1_1b(x1_0) 247 | x1 = torch.cat((x1_1a, x1_1b), 1) 248 | 249 | x2_0 = self.branch2_0(x) 250 | x2_1 = self.branch2_1(x2_0) 251 | x2_2 = self.branch2_2(x2_1) 252 | x2_3a = self.branch2_3a(x2_2) 253 | x2_3b = self.branch2_3b(x2_2) 254 | x2 = torch.cat((x2_3a, x2_3b), 1) 255 | 256 | x3 = self.branch3(x) 257 | 258 | out = torch.cat((x0, x1, x2, x3), 1) 259 | return out 260 | 261 | 262 | class InceptionV4(nn.Module): 263 | 264 | def __init__(self, num_classes=1001): 265 | super(InceptionV4, self).__init__() 266 | # Special attributs 267 | self.input_space = None 268 | self.input_size = (299, 299, 3) 269 | self.mean = None 270 | self.std = None 271 | # Modules 272 | self.features = nn.Sequential( 273 | BasicConv2d(3, 32, kernel_size=3, stride=2), 274 | BasicConv2d(32, 32, kernel_size=3, stride=1), 275 | BasicConv2d(32, 64, kernel_size=3, stride=1, padding=1), 276 | Mixed_3a(), 277 | Mixed_4a(), 278 | Mixed_5a(), 279 | Inception_A(), 280 | Inception_A(), 281 | Inception_A(), 282 | Inception_A(), 283 | Reduction_A(), # Mixed_6a 284 | Inception_B(), 285 | Inception_B(), 286 | Inception_B(), 287 | Inception_B(), 288 | Inception_B(), 289 | Inception_B(), 290 | Inception_B(), 291 | Reduction_B(), # Mixed_7a 292 | Inception_C(), 293 | Inception_C(), 294 | Inception_C() 295 | ) 296 | self.avg_pool = nn.AvgPool2d(8, count_include_pad=False) 297 | self.last_linear = nn.Linear(1536, num_classes) 298 | 299 | def logits(self, features): 300 | x = self.avg_pool(features) 301 | x = x.view(x.size(0), -1) 302 | x = self.last_linear(x) 303 | return x 304 | 305 | def forward(self, input): 306 | x = self.features(input) 307 | x = self.logits(x) 308 | return x 309 | 310 | 311 | def inceptionv4(num_classes=1001, pretrained='imagenet'): 312 | if pretrained: 313 | settings = pretrained_settings['inceptionv4'][pretrained] 314 | assert num_classes == settings['num_classes'], \ 315 | "num_classes should be {}, but is {}".format(settings['num_classes'], num_classes) 316 | 317 | # both 'imagenet'&'imagenet+background' are loaded from same parameters 318 | model = InceptionV4(num_classes=1001) 319 | model.load_state_dict(model_zoo.load_url(settings['url'])) 320 | 321 | if pretrained == 'imagenet': 322 | new_last_linear = nn.Linear(1536, 1000) 323 | new_last_linear.weight.data = model.last_linear.weight.data[1:] 324 | new_last_linear.bias.data = model.last_linear.bias.data[1:] 325 | model.last_linear = new_last_linear 326 | 327 | model.input_space = settings['input_space'] 328 | model.input_size = settings['input_size'] 329 | model.input_range = settings['input_range'] 330 | model.mean = settings['mean'] 331 | model.std = settings['std'] 332 | else: 333 | model = InceptionV4(num_classes=num_classes) 334 | return model 335 | 336 | 337 | ''' 338 | TEST 339 | Run this code with: 340 | ``` 341 | cd $HOME/pretrained-models.pytorch 342 | python -m pretrainedmodels.inceptionv4 343 | ``` 344 | ''' 345 | if __name__ == '__main__': 346 | 347 | assert inceptionv4(num_classes=10, pretrained=None) 348 | print('success') 349 | assert inceptionv4(num_classes=1000, pretrained='imagenet') 350 | print('success') 351 | assert inceptionv4(num_classes=1001, pretrained='imagenet+background') 352 | print('success') 353 | 354 | # fail 355 | assert inceptionv4(num_classes=1001, pretrained='imagenet') -------------------------------------------------------------------------------- /tars/nasnet.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.nn.functional as F 4 | import torch.utils.model_zoo as model_zoo 5 | from torch.autograd import Variable 6 | 7 | pretrained_settings = { 8 | 'nasnetalarge': { 9 | 'imagenet': { 10 | 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/nasnetalarge-a1897284.pth', 11 | 'input_space': 'RGB', 12 | 'input_size': [3, 331, 331], # resize 354 13 | 'input_range': [0, 1], 14 | 'mean': [0.5, 0.5, 0.5], 15 | 'std': [0.5, 0.5, 0.5], 16 | 'num_classes': 1000 17 | }, 18 | 'imagenet+background': { 19 | 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/nasnetalarge-a1897284.pth', 20 | 'input_space': 'RGB', 21 | 'input_size': [3, 331, 331], # resize 354 22 | 'input_range': [0, 1], 23 | 'mean': [0.5, 0.5, 0.5], 24 | 'std': [0.5, 0.5, 0.5], 25 | 'num_classes': 1001 26 | } 27 | } 28 | } 29 | 30 | class MaxPoolPad(nn.Module): 31 | 32 | def __init__(self): 33 | super(MaxPoolPad, self).__init__() 34 | self.pad = nn.ZeroPad2d((1, 0, 1, 0)) 35 | self.pool = nn.MaxPool2d(3, stride=2, padding=1) 36 | 37 | def forward(self, x): 38 | x = self.pad(x) 39 | x = self.pool(x) 40 | x = x[:, :, 1:, 1:] 41 | return x 42 | 43 | 44 | class AvgPoolPad(nn.Module): 45 | 46 | def __init__(self, stride=2, padding=1): 47 | super(AvgPoolPad, self).__init__() 48 | self.pad = nn.ZeroPad2d((1, 0, 1, 0)) 49 | self.pool = nn.AvgPool2d(3, stride=stride, padding=padding, count_include_pad=False) 50 | 51 | def forward(self, x): 52 | x = self.pad(x) 53 | x = self.pool(x) 54 | x = x[:, :, 1:, 1:] 55 | return x 56 | 57 | 58 | class SeparableConv2d(nn.Module): 59 | 60 | def __init__(self, in_channels, out_channels, dw_kernel, dw_stride, dw_padding, bias=False): 61 | super(SeparableConv2d, self).__init__() 62 | self.depthwise_conv2d = nn.Conv2d(in_channels, in_channels, dw_kernel, 63 | stride=dw_stride, 64 | padding=dw_padding, 65 | bias=bias, 66 | groups=in_channels) 67 | self.pointwise_conv2d = nn.Conv2d(in_channels, out_channels, 1, stride=1, bias=bias) 68 | 69 | def forward(self, x): 70 | x = self.depthwise_conv2d(x) 71 | x = self.pointwise_conv2d(x) 72 | return x 73 | 74 | 75 | class BranchSeparables(nn.Module): 76 | 77 | def __init__(self, in_channels, out_channels, kernel_size, stride, padding, bias=False): 78 | super(BranchSeparables, self).__init__() 79 | self.relu = nn.ReLU() 80 | self.separable_1 = SeparableConv2d(in_channels, in_channels, kernel_size, stride, padding, bias=bias) 81 | self.bn_sep_1 = nn.BatchNorm2d(in_channels, eps=0.001, momentum=0.1, affine=True) 82 | self.relu1 = nn.ReLU() 83 | self.separable_2 = SeparableConv2d(in_channels, out_channels, kernel_size, 1, padding, bias=bias) 84 | self.bn_sep_2 = nn.BatchNorm2d(out_channels, eps=0.001, momentum=0.1, affine=True) 85 | 86 | def forward(self, x): 87 | x = self.relu(x) 88 | x = self.separable_1(x) 89 | x = self.bn_sep_1(x) 90 | x = self.relu1(x) 91 | x = self.separable_2(x) 92 | x = self.bn_sep_2(x) 93 | return x 94 | 95 | 96 | class BranchSeparablesStem(nn.Module): 97 | 98 | def __init__(self, in_channels, out_channels, kernel_size, stride, padding, bias=False): 99 | super(BranchSeparablesStem, self).__init__() 100 | self.relu = nn.ReLU() 101 | self.separable_1 = SeparableConv2d(in_channels, out_channels, kernel_size, stride, padding, bias=bias) 102 | self.bn_sep_1 = nn.BatchNorm2d(out_channels, eps=0.001, momentum=0.1, affine=True) 103 | self.relu1 = nn.ReLU() 104 | self.separable_2 = SeparableConv2d(out_channels, out_channels, kernel_size, 1, padding, bias=bias) 105 | self.bn_sep_2 = nn.BatchNorm2d(out_channels, eps=0.001, momentum=0.1, affine=True) 106 | 107 | def forward(self, x): 108 | x = self.relu(x) 109 | x = self.separable_1(x) 110 | x = self.bn_sep_1(x) 111 | x = self.relu1(x) 112 | x = self.separable_2(x) 113 | x = self.bn_sep_2(x) 114 | return x 115 | 116 | 117 | class BranchSeparablesReduction(BranchSeparables): 118 | 119 | def __init__(self, in_channels, out_channels, kernel_size, stride, padding, z_padding=1, bias=False): 120 | BranchSeparables.__init__(self, in_channels, out_channels, kernel_size, stride, padding, bias) 121 | self.padding = nn.ZeroPad2d((z_padding, 0, z_padding, 0)) 122 | 123 | def forward(self, x): 124 | x = self.relu(x) 125 | x = self.padding(x) 126 | x = self.separable_1(x) 127 | x = x[:, :, 1:, 1:].contiguous() 128 | x = self.bn_sep_1(x) 129 | x = self.relu1(x) 130 | x = self.separable_2(x) 131 | x = self.bn_sep_2(x) 132 | return x 133 | 134 | 135 | class CellStem0(nn.Module): 136 | 137 | def __init__(self): 138 | super(CellStem0, self).__init__() 139 | self.conv_1x1 = nn.Sequential() 140 | self.conv_1x1.add_module('relu', nn.ReLU()) 141 | self.conv_1x1.add_module('conv', nn.Conv2d(96, 42, 1, stride=1, bias=False)) 142 | self.conv_1x1.add_module('bn', nn.BatchNorm2d(42, eps=0.001, momentum=0.1, affine=True)) 143 | 144 | self.comb_iter_0_left = BranchSeparables(42, 42, 5, 2, 2) 145 | self.comb_iter_0_right = BranchSeparablesStem(96, 42, 7, 2, 3, bias=False) 146 | 147 | self.comb_iter_1_left = nn.MaxPool2d(3, stride=2, padding=1) 148 | self.comb_iter_1_right = BranchSeparablesStem(96, 42, 7, 2, 3, bias=False) 149 | 150 | self.comb_iter_2_left = nn.AvgPool2d(3, stride=2, padding=1, count_include_pad=False) 151 | self.comb_iter_2_right = BranchSeparablesStem(96, 42, 5, 2, 2, bias=False) 152 | 153 | self.comb_iter_3_right = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False) 154 | 155 | self.comb_iter_4_left = BranchSeparables(42, 42, 3, 1, 1, bias=False) 156 | self.comb_iter_4_right = nn.MaxPool2d(3, stride=2, padding=1) 157 | 158 | def forward(self, x): 159 | x1 = self.conv_1x1(x) 160 | 161 | x_comb_iter_0_left = self.comb_iter_0_left(x1) 162 | x_comb_iter_0_right = self.comb_iter_0_right(x) 163 | x_comb_iter_0 = x_comb_iter_0_left + x_comb_iter_0_right 164 | 165 | x_comb_iter_1_left = self.comb_iter_1_left(x1) 166 | x_comb_iter_1_right = self.comb_iter_1_right(x) 167 | x_comb_iter_1 = x_comb_iter_1_left + x_comb_iter_1_right 168 | 169 | x_comb_iter_2_left = self.comb_iter_2_left(x1) 170 | x_comb_iter_2_right = self.comb_iter_2_right(x) 171 | x_comb_iter_2 = x_comb_iter_2_left + x_comb_iter_2_right 172 | 173 | x_comb_iter_3_right = self.comb_iter_3_right(x_comb_iter_0) 174 | x_comb_iter_3 = x_comb_iter_3_right + x_comb_iter_1 175 | 176 | x_comb_iter_4_left = self.comb_iter_4_left(x_comb_iter_0) 177 | x_comb_iter_4_right = self.comb_iter_4_right(x1) 178 | x_comb_iter_4 = x_comb_iter_4_left + x_comb_iter_4_right 179 | 180 | x_out = torch.cat([x_comb_iter_1, x_comb_iter_2, x_comb_iter_3, x_comb_iter_4], 1) 181 | return x_out 182 | 183 | 184 | class CellStem1(nn.Module): 185 | 186 | def __init__(self): 187 | super(CellStem1, self).__init__() 188 | self.conv_1x1 = nn.Sequential() 189 | self.conv_1x1.add_module('relu', nn.ReLU()) 190 | self.conv_1x1.add_module('conv', nn.Conv2d(168, 84, 1, stride=1, bias=False)) 191 | self.conv_1x1.add_module('bn', nn.BatchNorm2d(84, eps=0.001, momentum=0.1, affine=True)) 192 | 193 | self.relu = nn.ReLU() 194 | self.path_1 = nn.Sequential() 195 | self.path_1.add_module('avgpool', nn.AvgPool2d(1, stride=2, count_include_pad=False)) 196 | self.path_1.add_module('conv', nn.Conv2d(96, 42, 1, stride=1, bias=False)) 197 | self.path_2 = nn.ModuleList() 198 | self.path_2.add_module('pad', nn.ZeroPad2d((0, 1, 0, 1))) 199 | self.path_2.add_module('avgpool', nn.AvgPool2d(1, stride=2, count_include_pad=False)) 200 | self.path_2.add_module('conv', nn.Conv2d(96, 42, 1, stride=1, bias=False)) 201 | 202 | self.final_path_bn = nn.BatchNorm2d(84, eps=0.001, momentum=0.1, affine=True) 203 | 204 | self.comb_iter_0_left = BranchSeparables(84, 84, 5, 2, 2, bias=False) 205 | self.comb_iter_0_right = BranchSeparables(84, 84, 7, 2, 3, bias=False) 206 | 207 | self.comb_iter_1_left = nn.MaxPool2d(3, stride=2, padding=1) 208 | self.comb_iter_1_right = BranchSeparables(84, 84, 7, 2, 3, bias=False) 209 | 210 | self.comb_iter_2_left = nn.AvgPool2d(3, stride=2, padding=1, count_include_pad=False) 211 | self.comb_iter_2_right = BranchSeparables(84, 84, 5, 2, 2, bias=False) 212 | 213 | self.comb_iter_3_right = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False) 214 | 215 | self.comb_iter_4_left = BranchSeparables(84, 84, 3, 1, 1, bias=False) 216 | self.comb_iter_4_right = nn.MaxPool2d(3, stride=2, padding=1) 217 | 218 | def forward(self, x_conv0, x_stem_0): 219 | x_left = self.conv_1x1(x_stem_0) 220 | 221 | x_relu = self.relu(x_conv0) 222 | # path 1 223 | x_path1 = self.path_1(x_relu) 224 | # path 2 225 | x_path2 = self.path_2.pad(x_relu) 226 | x_path2 = x_path2[:, :, 1:, 1:] 227 | x_path2 = self.path_2.avgpool(x_path2) 228 | x_path2 = self.path_2.conv(x_path2) 229 | # final path 230 | x_right = self.final_path_bn(torch.cat([x_path1, x_path2], 1)) 231 | 232 | x_comb_iter_0_left = self.comb_iter_0_left(x_left) 233 | x_comb_iter_0_right = self.comb_iter_0_right(x_right) 234 | x_comb_iter_0 = x_comb_iter_0_left + x_comb_iter_0_right 235 | 236 | x_comb_iter_1_left = self.comb_iter_1_left(x_left) 237 | x_comb_iter_1_right = self.comb_iter_1_right(x_right) 238 | x_comb_iter_1 = x_comb_iter_1_left + x_comb_iter_1_right 239 | 240 | x_comb_iter_2_left = self.comb_iter_2_left(x_left) 241 | x_comb_iter_2_right = self.comb_iter_2_right(x_right) 242 | x_comb_iter_2 = x_comb_iter_2_left + x_comb_iter_2_right 243 | 244 | x_comb_iter_3_right = self.comb_iter_3_right(x_comb_iter_0) 245 | x_comb_iter_3 = x_comb_iter_3_right + x_comb_iter_1 246 | 247 | x_comb_iter_4_left = self.comb_iter_4_left(x_comb_iter_0) 248 | x_comb_iter_4_right = self.comb_iter_4_right(x_left) 249 | x_comb_iter_4 = x_comb_iter_4_left + x_comb_iter_4_right 250 | 251 | x_out = torch.cat([x_comb_iter_1, x_comb_iter_2, x_comb_iter_3, x_comb_iter_4], 1) 252 | return x_out 253 | 254 | 255 | class FirstCell(nn.Module): 256 | 257 | def __init__(self, in_channels_left, out_channels_left, in_channels_right, out_channels_right): 258 | super(FirstCell, self).__init__() 259 | self.conv_1x1 = nn.Sequential() 260 | self.conv_1x1.add_module('relu', nn.ReLU()) 261 | self.conv_1x1.add_module('conv', nn.Conv2d(in_channels_right, out_channels_right, 1, stride=1, bias=False)) 262 | self.conv_1x1.add_module('bn', nn.BatchNorm2d(out_channels_right, eps=0.001, momentum=0.1, affine=True)) 263 | 264 | self.relu = nn.ReLU() 265 | self.path_1 = nn.Sequential() 266 | self.path_1.add_module('avgpool', nn.AvgPool2d(1, stride=2, count_include_pad=False)) 267 | self.path_1.add_module('conv', nn.Conv2d(in_channels_left, out_channels_left, 1, stride=1, bias=False)) 268 | self.path_2 = nn.ModuleList() 269 | self.path_2.add_module('pad', nn.ZeroPad2d((0, 1, 0, 1))) 270 | self.path_2.add_module('avgpool', nn.AvgPool2d(1, stride=2, count_include_pad=False)) 271 | self.path_2.add_module('conv', nn.Conv2d(in_channels_left, out_channels_left, 1, stride=1, bias=False)) 272 | 273 | self.final_path_bn = nn.BatchNorm2d(out_channels_left * 2, eps=0.001, momentum=0.1, affine=True) 274 | 275 | self.comb_iter_0_left = BranchSeparables(out_channels_right, out_channels_right, 5, 1, 2, bias=False) 276 | self.comb_iter_0_right = BranchSeparables(out_channels_right, out_channels_right, 3, 1, 1, bias=False) 277 | 278 | self.comb_iter_1_left = BranchSeparables(out_channels_right, out_channels_right, 5, 1, 2, bias=False) 279 | self.comb_iter_1_right = BranchSeparables(out_channels_right, out_channels_right, 3, 1, 1, bias=False) 280 | 281 | self.comb_iter_2_left = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False) 282 | 283 | self.comb_iter_3_left = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False) 284 | self.comb_iter_3_right = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False) 285 | 286 | self.comb_iter_4_left = BranchSeparables(out_channels_right, out_channels_right, 3, 1, 1, bias=False) 287 | 288 | def forward(self, x, x_prev): 289 | x_relu = self.relu(x_prev) 290 | # path 1 291 | x_path1 = self.path_1(x_relu) 292 | # path 2 293 | x_path2 = self.path_2.pad(x_relu) 294 | x_path2 = x_path2[:, :, 1:, 1:] 295 | x_path2 = self.path_2.avgpool(x_path2) 296 | x_path2 = self.path_2.conv(x_path2) 297 | # final path 298 | x_left = self.final_path_bn(torch.cat([x_path1, x_path2], 1)) 299 | 300 | x_right = self.conv_1x1(x) 301 | 302 | x_comb_iter_0_left = self.comb_iter_0_left(x_right) 303 | x_comb_iter_0_right = self.comb_iter_0_right(x_left) 304 | x_comb_iter_0 = x_comb_iter_0_left + x_comb_iter_0_right 305 | 306 | x_comb_iter_1_left = self.comb_iter_1_left(x_left) 307 | x_comb_iter_1_right = self.comb_iter_1_right(x_left) 308 | x_comb_iter_1 = x_comb_iter_1_left + x_comb_iter_1_right 309 | 310 | x_comb_iter_2_left = self.comb_iter_2_left(x_right) 311 | x_comb_iter_2 = x_comb_iter_2_left + x_left 312 | 313 | x_comb_iter_3_left = self.comb_iter_3_left(x_left) 314 | x_comb_iter_3_right = self.comb_iter_3_right(x_left) 315 | x_comb_iter_3 = x_comb_iter_3_left + x_comb_iter_3_right 316 | 317 | x_comb_iter_4_left = self.comb_iter_4_left(x_right) 318 | x_comb_iter_4 = x_comb_iter_4_left + x_right 319 | 320 | x_out = torch.cat([x_left, x_comb_iter_0, x_comb_iter_1, x_comb_iter_2, x_comb_iter_3, x_comb_iter_4], 1) 321 | return x_out 322 | 323 | 324 | class NormalCell(nn.Module): 325 | 326 | def __init__(self, in_channels_left, out_channels_left, in_channels_right, out_channels_right): 327 | super(NormalCell, self).__init__() 328 | self.conv_prev_1x1 = nn.Sequential() 329 | self.conv_prev_1x1.add_module('relu', nn.ReLU()) 330 | self.conv_prev_1x1.add_module('conv', nn.Conv2d(in_channels_left, out_channels_left, 1, stride=1, bias=False)) 331 | self.conv_prev_1x1.add_module('bn', nn.BatchNorm2d(out_channels_left, eps=0.001, momentum=0.1, affine=True)) 332 | 333 | self.conv_1x1 = nn.Sequential() 334 | self.conv_1x1.add_module('relu', nn.ReLU()) 335 | self.conv_1x1.add_module('conv', nn.Conv2d(in_channels_right, out_channels_right, 1, stride=1, bias=False)) 336 | self.conv_1x1.add_module('bn', nn.BatchNorm2d(out_channels_right, eps=0.001, momentum=0.1, affine=True)) 337 | 338 | self.comb_iter_0_left = BranchSeparables(out_channels_right, out_channels_right, 5, 1, 2, bias=False) 339 | self.comb_iter_0_right = BranchSeparables(out_channels_left, out_channels_left, 3, 1, 1, bias=False) 340 | 341 | self.comb_iter_1_left = BranchSeparables(out_channels_left, out_channels_left, 5, 1, 2, bias=False) 342 | self.comb_iter_1_right = BranchSeparables(out_channels_left, out_channels_left, 3, 1, 1, bias=False) 343 | 344 | self.comb_iter_2_left = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False) 345 | 346 | self.comb_iter_3_left = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False) 347 | self.comb_iter_3_right = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False) 348 | 349 | self.comb_iter_4_left = BranchSeparables(out_channels_right, out_channels_right, 3, 1, 1, bias=False) 350 | 351 | def forward(self, x, x_prev): 352 | x_left = self.conv_prev_1x1(x_prev) 353 | x_right = self.conv_1x1(x) 354 | 355 | x_comb_iter_0_left = self.comb_iter_0_left(x_right) 356 | x_comb_iter_0_right = self.comb_iter_0_right(x_left) 357 | x_comb_iter_0 = x_comb_iter_0_left + x_comb_iter_0_right 358 | 359 | x_comb_iter_1_left = self.comb_iter_1_left(x_left) 360 | x_comb_iter_1_right = self.comb_iter_1_right(x_left) 361 | x_comb_iter_1 = x_comb_iter_1_left + x_comb_iter_1_right 362 | 363 | x_comb_iter_2_left = self.comb_iter_2_left(x_right) 364 | x_comb_iter_2 = x_comb_iter_2_left + x_left 365 | 366 | x_comb_iter_3_left = self.comb_iter_3_left(x_left) 367 | x_comb_iter_3_right = self.comb_iter_3_right(x_left) 368 | x_comb_iter_3 = x_comb_iter_3_left + x_comb_iter_3_right 369 | 370 | x_comb_iter_4_left = self.comb_iter_4_left(x_right) 371 | x_comb_iter_4 = x_comb_iter_4_left + x_right 372 | 373 | x_out = torch.cat([x_left, x_comb_iter_0, x_comb_iter_1, x_comb_iter_2, x_comb_iter_3, x_comb_iter_4], 1) 374 | return x_out 375 | 376 | 377 | class ReductionCell0(nn.Module): 378 | 379 | def __init__(self, in_channels_left, out_channels_left, in_channels_right, out_channels_right): 380 | super(ReductionCell0, self).__init__() 381 | self.conv_prev_1x1 = nn.Sequential() 382 | self.conv_prev_1x1.add_module('relu', nn.ReLU()) 383 | self.conv_prev_1x1.add_module('conv', nn.Conv2d(in_channels_left, out_channels_left, 1, stride=1, bias=False)) 384 | self.conv_prev_1x1.add_module('bn', nn.BatchNorm2d(out_channels_left, eps=0.001, momentum=0.1, affine=True)) 385 | 386 | self.conv_1x1 = nn.Sequential() 387 | self.conv_1x1.add_module('relu', nn.ReLU()) 388 | self.conv_1x1.add_module('conv', nn.Conv2d(in_channels_right, out_channels_right, 1, stride=1, bias=False)) 389 | self.conv_1x1.add_module('bn', nn.BatchNorm2d(out_channels_right, eps=0.001, momentum=0.1, affine=True)) 390 | 391 | self.comb_iter_0_left = BranchSeparablesReduction(out_channels_right, out_channels_right, 5, 2, 2, bias=False) 392 | self.comb_iter_0_right = BranchSeparablesReduction(out_channels_right, out_channels_right, 7, 2, 3, bias=False) 393 | 394 | self.comb_iter_1_left = MaxPoolPad() 395 | self.comb_iter_1_right = BranchSeparablesReduction(out_channels_right, out_channels_right, 7, 2, 3, bias=False) 396 | 397 | self.comb_iter_2_left = AvgPoolPad() 398 | self.comb_iter_2_right = BranchSeparablesReduction(out_channels_right, out_channels_right, 5, 2, 2, bias=False) 399 | 400 | self.comb_iter_3_right = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False) 401 | 402 | self.comb_iter_4_left = BranchSeparablesReduction(out_channels_right, out_channels_right, 3, 1, 1, bias=False) 403 | self.comb_iter_4_right = MaxPoolPad() 404 | 405 | def forward(self, x, x_prev): 406 | x_left = self.conv_prev_1x1(x_prev) 407 | x_right = self.conv_1x1(x) 408 | 409 | x_comb_iter_0_left = self.comb_iter_0_left(x_right) 410 | x_comb_iter_0_right = self.comb_iter_0_right(x_left) 411 | x_comb_iter_0 = x_comb_iter_0_left + x_comb_iter_0_right 412 | 413 | x_comb_iter_1_left = self.comb_iter_1_left(x_right) 414 | x_comb_iter_1_right = self.comb_iter_1_right(x_left) 415 | x_comb_iter_1 = x_comb_iter_1_left + x_comb_iter_1_right 416 | 417 | x_comb_iter_2_left = self.comb_iter_2_left(x_right) 418 | x_comb_iter_2_right = self.comb_iter_2_right(x_left) 419 | x_comb_iter_2 = x_comb_iter_2_left + x_comb_iter_2_right 420 | 421 | x_comb_iter_3_right = self.comb_iter_3_right(x_comb_iter_0) 422 | x_comb_iter_3 = x_comb_iter_3_right + x_comb_iter_1 423 | 424 | x_comb_iter_4_left = self.comb_iter_4_left(x_comb_iter_0) 425 | x_comb_iter_4_right = self.comb_iter_4_right(x_right) 426 | x_comb_iter_4 = x_comb_iter_4_left + x_comb_iter_4_right 427 | 428 | x_out = torch.cat([x_comb_iter_1, x_comb_iter_2, x_comb_iter_3, x_comb_iter_4], 1) 429 | return x_out 430 | 431 | 432 | class ReductionCell1(nn.Module): 433 | 434 | def __init__(self, in_channels_left, out_channels_left, in_channels_right, out_channels_right): 435 | super(ReductionCell1, self).__init__() 436 | self.conv_prev_1x1 = nn.Sequential() 437 | self.conv_prev_1x1.add_module('relu', nn.ReLU()) 438 | self.conv_prev_1x1.add_module('conv', nn.Conv2d(in_channels_left, out_channels_left, 1, stride=1, bias=False)) 439 | self.conv_prev_1x1.add_module('bn', nn.BatchNorm2d(out_channels_left, eps=0.001, momentum=0.1, affine=True)) 440 | 441 | self.conv_1x1 = nn.Sequential() 442 | self.conv_1x1.add_module('relu', nn.ReLU()) 443 | self.conv_1x1.add_module('conv', nn.Conv2d(in_channels_right, out_channels_right, 1, stride=1, bias=False)) 444 | self.conv_1x1.add_module('bn', nn.BatchNorm2d(out_channels_right, eps=0.001, momentum=0.1, affine=True)) 445 | 446 | self.comb_iter_0_left = BranchSeparables(out_channels_right, out_channels_right, 5, 2, 2, bias=False) 447 | self.comb_iter_0_right = BranchSeparables(out_channels_right, out_channels_right, 7, 2, 3, bias=False) 448 | 449 | self.comb_iter_1_left = nn.MaxPool2d(3, stride=2, padding=1) 450 | self.comb_iter_1_right = BranchSeparables(out_channels_right, out_channels_right, 7, 2, 3, bias=False) 451 | 452 | self.comb_iter_2_left = nn.AvgPool2d(3, stride=2, padding=1, count_include_pad=False) 453 | self.comb_iter_2_right = BranchSeparables(out_channels_right, out_channels_right, 5, 2, 2, bias=False) 454 | 455 | self.comb_iter_3_right = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False) 456 | 457 | self.comb_iter_4_left = BranchSeparables(out_channels_right, out_channels_right, 3, 1, 1, bias=False) 458 | self.comb_iter_4_right = nn.MaxPool2d(3, stride=2, padding=1) 459 | 460 | def forward(self, x, x_prev): 461 | x_left = self.conv_prev_1x1(x_prev) 462 | x_right = self.conv_1x1(x) 463 | 464 | x_comb_iter_0_left = self.comb_iter_0_left(x_right) 465 | x_comb_iter_0_right = self.comb_iter_0_right(x_left) 466 | x_comb_iter_0 = x_comb_iter_0_left + x_comb_iter_0_right 467 | 468 | x_comb_iter_1_left = self.comb_iter_1_left(x_right) 469 | x_comb_iter_1_right = self.comb_iter_1_right(x_left) 470 | x_comb_iter_1 = x_comb_iter_1_left + x_comb_iter_1_right 471 | 472 | x_comb_iter_2_left = self.comb_iter_2_left(x_right) 473 | x_comb_iter_2_right = self.comb_iter_2_right(x_left) 474 | x_comb_iter_2 = x_comb_iter_2_left + x_comb_iter_2_right 475 | 476 | x_comb_iter_3_right = self.comb_iter_3_right(x_comb_iter_0) 477 | x_comb_iter_3 = x_comb_iter_3_right + x_comb_iter_1 478 | 479 | x_comb_iter_4_left = self.comb_iter_4_left(x_comb_iter_0) 480 | x_comb_iter_4_right = self.comb_iter_4_right(x_right) 481 | x_comb_iter_4 = x_comb_iter_4_left + x_comb_iter_4_right 482 | 483 | x_out = torch.cat([x_comb_iter_1, x_comb_iter_2, x_comb_iter_3, x_comb_iter_4], 1) 484 | return x_out 485 | 486 | 487 | class NASNetALarge(nn.Module): 488 | 489 | def __init__(self, num_classes=1001): 490 | super(NASNetALarge, self).__init__() 491 | self.num_classes = num_classes 492 | 493 | self.conv0 = nn.Sequential() 494 | self.conv0.add_module('conv', nn.Conv2d(in_channels=3, out_channels=96, kernel_size=3, padding=0, stride=2, 495 | bias=False)) 496 | self.conv0.add_module('bn', nn.BatchNorm2d(96, eps=0.001, momentum=0.1, affine=True)) 497 | 498 | self.cell_stem_0 = CellStem0() 499 | self.cell_stem_1 = CellStem1() 500 | 501 | self.cell_0 = FirstCell(in_channels_left=168, out_channels_left=84, 502 | in_channels_right=336, out_channels_right=168) 503 | self.cell_1 = NormalCell(in_channels_left=336, out_channels_left=168, 504 | in_channels_right=1008, out_channels_right=168) 505 | self.cell_2 = NormalCell(in_channels_left=1008, out_channels_left=168, 506 | in_channels_right=1008, out_channels_right=168) 507 | self.cell_3 = NormalCell(in_channels_left=1008, out_channels_left=168, 508 | in_channels_right=1008, out_channels_right=168) 509 | self.cell_4 = NormalCell(in_channels_left=1008, out_channels_left=168, 510 | in_channels_right=1008, out_channels_right=168) 511 | self.cell_5 = NormalCell(in_channels_left=1008, out_channels_left=168, 512 | in_channels_right=1008, out_channels_right=168) 513 | 514 | self.reduction_cell_0 = ReductionCell0(in_channels_left=1008, out_channels_left=336, 515 | in_channels_right=1008, out_channels_right=336) 516 | 517 | self.cell_6 = FirstCell(in_channels_left=1008, out_channels_left=168, 518 | in_channels_right=1344, out_channels_right=336) 519 | self.cell_7 = NormalCell(in_channels_left=1344, out_channels_left=336, 520 | in_channels_right=2016, out_channels_right=336) 521 | self.cell_8 = NormalCell(in_channels_left=2016, out_channels_left=336, 522 | in_channels_right=2016, out_channels_right=336) 523 | self.cell_9 = NormalCell(in_channels_left=2016, out_channels_left=336, 524 | in_channels_right=2016, out_channels_right=336) 525 | self.cell_10 = NormalCell(in_channels_left=2016, out_channels_left=336, 526 | in_channels_right=2016, out_channels_right=336) 527 | self.cell_11 = NormalCell(in_channels_left=2016, out_channels_left=336, 528 | in_channels_right=2016, out_channels_right=336) 529 | 530 | self.reduction_cell_1 = ReductionCell1(in_channels_left=2016, out_channels_left=672, 531 | in_channels_right=2016, out_channels_right=672) 532 | 533 | self.cell_12 = FirstCell(in_channels_left=2016, out_channels_left=336, 534 | in_channels_right=2688, out_channels_right=672) 535 | self.cell_13 = NormalCell(in_channels_left=2688, out_channels_left=672, 536 | in_channels_right=4032, out_channels_right=672) 537 | self.cell_14 = NormalCell(in_channels_left=4032, out_channels_left=672, 538 | in_channels_right=4032, out_channels_right=672) 539 | self.cell_15 = NormalCell(in_channels_left=4032, out_channels_left=672, 540 | in_channels_right=4032, out_channels_right=672) 541 | self.cell_16 = NormalCell(in_channels_left=4032, out_channels_left=672, 542 | in_channels_right=4032, out_channels_right=672) 543 | self.cell_17 = NormalCell(in_channels_left=4032, out_channels_left=672, 544 | in_channels_right=4032, out_channels_right=672) 545 | 546 | self.relu = nn.ReLU() 547 | self.avg_pool = nn.AvgPool2d(11, stride=1, padding=0) 548 | self.dropout = nn.Dropout() 549 | self.last_linear = nn.Linear(4032, self.num_classes) 550 | 551 | def features(self, input): 552 | x_conv0 = self.conv0(input) 553 | x_stem_0 = self.cell_stem_0(x_conv0) 554 | x_stem_1 = self.cell_stem_1(x_conv0, x_stem_0) 555 | 556 | x_cell_0 = self.cell_0(x_stem_1, x_stem_0) 557 | x_cell_1 = self.cell_1(x_cell_0, x_stem_1) 558 | x_cell_2 = self.cell_2(x_cell_1, x_cell_0) 559 | x_cell_3 = self.cell_3(x_cell_2, x_cell_1) 560 | x_cell_4 = self.cell_4(x_cell_3, x_cell_2) 561 | x_cell_5 = self.cell_5(x_cell_4, x_cell_3) 562 | 563 | x_reduction_cell_0 = self.reduction_cell_0(x_cell_5, x_cell_4) 564 | 565 | x_cell_6 = self.cell_6(x_reduction_cell_0, x_cell_4) 566 | x_cell_7 = self.cell_7(x_cell_6, x_reduction_cell_0) 567 | x_cell_8 = self.cell_8(x_cell_7, x_cell_6) 568 | x_cell_9 = self.cell_9(x_cell_8, x_cell_7) 569 | x_cell_10 = self.cell_10(x_cell_9, x_cell_8) 570 | x_cell_11 = self.cell_11(x_cell_10, x_cell_9) 571 | 572 | x_reduction_cell_1 = self.reduction_cell_1(x_cell_11, x_cell_10) 573 | 574 | x_cell_12 = self.cell_12(x_reduction_cell_1, x_cell_10) 575 | x_cell_13 = self.cell_13(x_cell_12, x_reduction_cell_1) 576 | x_cell_14 = self.cell_14(x_cell_13, x_cell_12) 577 | x_cell_15 = self.cell_15(x_cell_14, x_cell_13) 578 | x_cell_16 = self.cell_16(x_cell_15, x_cell_14) 579 | x_cell_17 = self.cell_17(x_cell_16, x_cell_15) 580 | return x_cell_17 581 | 582 | def logits(self, features): 583 | x = self.relu(features) 584 | x = self.avg_pool(x) 585 | x = x.view(x.size(0), -1) 586 | x = self.dropout(x) 587 | x = self.last_linear(x) 588 | return x 589 | 590 | def forward(self, input): 591 | x = self.features(input) 592 | x = self.logits(x) 593 | return x 594 | 595 | 596 | def nasnetalarge(num_classes=1001, pretrained='imagenet'): 597 | r"""NASNetALarge model architecture from the 598 | `"NASNet" `_ paper. 599 | """ 600 | if pretrained: 601 | settings = pretrained_settings['nasnetalarge'][pretrained] 602 | assert num_classes == settings['num_classes'], \ 603 | "num_classes should be {}, but is {}".format(settings['num_classes'], num_classes) 604 | 605 | # both 'imagenet'&'imagenet+background' are loaded from same parameters 606 | model = NASNetALarge(num_classes=1001) 607 | model.load_state_dict(model_zoo.load_url(settings['url'])) 608 | 609 | if pretrained == 'imagenet': 610 | new_last_linear = nn.Linear(model.last_linear.in_features, 1000) 611 | new_last_linear.weight.data = model.last_linear.weight.data[1:] 612 | new_last_linear.bias.data = model.last_linear.bias.data[1:] 613 | model.last_linear = new_last_linear 614 | 615 | model.input_space = settings['input_space'] 616 | model.input_size = settings['input_size'] 617 | model.input_range = settings['input_range'] 618 | 619 | model.mean = settings['mean'] 620 | model.std = settings['std'] 621 | else: 622 | model = NASNetALarge(num_classes=num_classes) 623 | return model 624 | 625 | 626 | 627 | if __name__ == "__main__": 628 | 629 | model = NasNetALarge() 630 | 631 | input = Variable(torch.randn(2,3,331,331)) 632 | output = model(input) 633 | print(output.size()) 634 | -------------------------------------------------------------------------------- /tars/resnext.py: -------------------------------------------------------------------------------- 1 | import os 2 | import torch 3 | import torch.nn as nn 4 | import torch.utils.model_zoo as model_zoo 5 | from .resnext_features import resnext101_32x4d_features 6 | from .resnext_features import resnext101_64x4d_features 7 | 8 | __all__ = ['ResNeXt101_32x4d', 'resnext101_32x4d', 9 | 'ResNeXt101_64x4d', 'resnext101_64x4d'] 10 | 11 | pretrained_settings = { 12 | 'resnext101_32x4d': { 13 | 'imagenet': { 14 | 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/resnext101_32x4d-29e315fa.pth', 15 | 'input_space': 'RGB', 16 | 'input_size': [3, 224, 224], 17 | 'input_range': [0, 1], 18 | 'mean': [0.485, 0.456, 0.406], 19 | 'std': [0.229, 0.224, 0.225], 20 | 'num_classes': 1000 21 | } 22 | }, 23 | 'resnext101_64x4d': { 24 | 'imagenet': { 25 | 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/resnext101_64x4d-e77a0586.pth', 26 | 'input_space': 'RGB', 27 | 'input_size': [3, 224, 224], 28 | 'input_range': [0, 1], 29 | 'mean': [0.485, 0.456, 0.406], 30 | 'std': [0.229, 0.224, 0.225], 31 | 'num_classes': 1000 32 | } 33 | } 34 | } 35 | 36 | class ResNeXt101_32x4d(nn.Module): 37 | 38 | def __init__(self, num_classes=1000): 39 | super(ResNeXt101_32x4d, self).__init__() 40 | self.num_classes = num_classes 41 | self.features = resnext101_32x4d_features 42 | self.avg_pool = nn.AvgPool2d((7, 7), (1, 1)) 43 | self.last_linear = nn.Linear(2048, num_classes) 44 | 45 | def logits(self, input): 46 | x = self.avg_pool(input) 47 | x = x.view(x.size(0), -1) 48 | x = self.last_linear(x) 49 | return x 50 | 51 | def forward(self, input): 52 | x = self.features(input) 53 | x = self.logits(x) 54 | return x 55 | 56 | 57 | class ResNeXt101_64x4d(nn.Module): 58 | 59 | def __init__(self, num_classes=1000): 60 | super(ResNeXt101_64x4d, self).__init__() 61 | self.num_classes = num_classes 62 | self.features = resnext101_64x4d_features 63 | self.avg_pool = nn.AvgPool2d((7, 7), (1, 1)) 64 | self.last_linear = nn.Linear(2048, num_classes) 65 | 66 | def logits(self, input): 67 | x = self.avg_pool(input) 68 | x = x.view(x.size(0), -1) 69 | x = self.last_linear(x) 70 | return x 71 | 72 | def forward(self, input): 73 | x = self.features(input) 74 | x = self.logits(x) 75 | return x 76 | 77 | 78 | def resnext101_32x4d(num_classes=1000, pretrained='imagenet'): 79 | model = ResNeXt101_32x4d(num_classes=num_classes) 80 | if pretrained is not None: 81 | settings = pretrained_settings['resnext101_32x4d'][pretrained] 82 | assert num_classes == settings['num_classes'], \ 83 | "num_classes should be {}, but is {}".format(settings['num_classes'], num_classes) 84 | model.load_state_dict(model_zoo.load_url(settings['url'])) 85 | model.input_space = settings['input_space'] 86 | model.input_size = settings['input_size'] 87 | model.input_range = settings['input_range'] 88 | model.mean = settings['mean'] 89 | model.std = settings['std'] 90 | return model 91 | 92 | def resnext101_64x4d(num_classes=1000, pretrained='imagenet'): 93 | model = ResNeXt101_64x4d(num_classes=num_classes) 94 | if pretrained is not None: 95 | settings = pretrained_settings['resnext101_64x4d'][pretrained] 96 | assert num_classes == settings['num_classes'], \ 97 | "num_classes should be {}, but is {}".format(settings['num_classes'], num_classes) 98 | model.load_state_dict(model_zoo.load_url(settings['url'])) 99 | model.input_space = settings['input_space'] 100 | model.input_size = settings['input_size'] 101 | model.input_range = settings['input_range'] 102 | model.mean = settings['mean'] 103 | model.std = settings['std'] 104 | return model 105 | -------------------------------------------------------------------------------- /tars/resnext_features/__init__.py: -------------------------------------------------------------------------------- 1 | from .resnext101_32x4d_features import resnext101_32x4d_features 2 | from .resnext101_64x4d_features import resnext101_64x4d_features -------------------------------------------------------------------------------- /tars/resnext_features/resnext101_32x4d_features.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | from torch.autograd import Variable 4 | from functools import reduce 5 | 6 | class LambdaBase(nn.Sequential): 7 | def __init__(self, fn, *args): 8 | super(LambdaBase, self).__init__(*args) 9 | self.lambda_func = fn 10 | 11 | def forward_prepare(self, input): 12 | output = [] 13 | for module in self._modules.values(): 14 | output.append(module(input)) 15 | return output if output else input 16 | 17 | class Lambda(LambdaBase): 18 | def forward(self, input): 19 | return self.lambda_func(self.forward_prepare(input)) 20 | 21 | class LambdaMap(LambdaBase): 22 | def forward(self, input): 23 | return list(map(self.lambda_func,self.forward_prepare(input))) 24 | 25 | class LambdaReduce(LambdaBase): 26 | def forward(self, input): 27 | return reduce(self.lambda_func,self.forward_prepare(input)) 28 | 29 | resnext101_32x4d_features = nn.Sequential( # Sequential, 30 | nn.Conv2d(3,64,(7, 7),(2, 2),(3, 3),1,1,bias=False), 31 | nn.BatchNorm2d(64), 32 | nn.ReLU(), 33 | nn.MaxPool2d((3, 3),(2, 2),(1, 1)), 34 | nn.Sequential( # Sequential, 35 | nn.Sequential( # Sequential, 36 | LambdaMap(lambda x: x, # ConcatTable, 37 | nn.Sequential( # Sequential, 38 | nn.Sequential( # Sequential, 39 | nn.Conv2d(64,128,(1, 1),(1, 1),(0, 0),1,1,bias=False), 40 | nn.BatchNorm2d(128), 41 | nn.ReLU(), 42 | nn.Conv2d(128,128,(3, 3),(1, 1),(1, 1),1,32,bias=False), 43 | nn.BatchNorm2d(128), 44 | nn.ReLU(), 45 | ), 46 | nn.Conv2d(128,256,(1, 1),(1, 1),(0, 0),1,1,bias=False), 47 | nn.BatchNorm2d(256), 48 | ), 49 | nn.Sequential( # Sequential, 50 | nn.Conv2d(64,256,(1, 1),(1, 1),(0, 0),1,1,bias=False), 51 | nn.BatchNorm2d(256), 52 | ), 53 | ), 54 | LambdaReduce(lambda x,y: x+y), # CAddTable, 55 | nn.ReLU(), 56 | ), 57 | nn.Sequential( # Sequential, 58 | LambdaMap(lambda x: x, # ConcatTable, 59 | nn.Sequential( # Sequential, 60 | nn.Sequential( # Sequential, 61 | nn.Conv2d(256,128,(1, 1),(1, 1),(0, 0),1,1,bias=False), 62 | nn.BatchNorm2d(128), 63 | nn.ReLU(), 64 | nn.Conv2d(128,128,(3, 3),(1, 1),(1, 1),1,32,bias=False), 65 | nn.BatchNorm2d(128), 66 | nn.ReLU(), 67 | ), 68 | nn.Conv2d(128,256,(1, 1),(1, 1),(0, 0),1,1,bias=False), 69 | nn.BatchNorm2d(256), 70 | ), 71 | Lambda(lambda x: x), # Identity, 72 | ), 73 | LambdaReduce(lambda x,y: x+y), # CAddTable, 74 | nn.ReLU(), 75 | ), 76 | nn.Sequential( # Sequential, 77 | LambdaMap(lambda x: x, # ConcatTable, 78 | nn.Sequential( # Sequential, 79 | nn.Sequential( # Sequential, 80 | nn.Conv2d(256,128,(1, 1),(1, 1),(0, 0),1,1,bias=False), 81 | nn.BatchNorm2d(128), 82 | nn.ReLU(), 83 | nn.Conv2d(128,128,(3, 3),(1, 1),(1, 1),1,32,bias=False), 84 | nn.BatchNorm2d(128), 85 | nn.ReLU(), 86 | ), 87 | nn.Conv2d(128,256,(1, 1),(1, 1),(0, 0),1,1,bias=False), 88 | nn.BatchNorm2d(256), 89 | ), 90 | Lambda(lambda x: x), # Identity, 91 | ), 92 | LambdaReduce(lambda x,y: x+y), # CAddTable, 93 | nn.ReLU(), 94 | ), 95 | ), 96 | nn.Sequential( # Sequential, 97 | nn.Sequential( # Sequential, 98 | LambdaMap(lambda x: x, # ConcatTable, 99 | nn.Sequential( # Sequential, 100 | nn.Sequential( # Sequential, 101 | nn.Conv2d(256,256,(1, 1),(1, 1),(0, 0),1,1,bias=False), 102 | nn.BatchNorm2d(256), 103 | nn.ReLU(), 104 | nn.Conv2d(256,256,(3, 3),(2, 2),(1, 1),1,32,bias=False), 105 | nn.BatchNorm2d(256), 106 | nn.ReLU(), 107 | ), 108 | nn.Conv2d(256,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 109 | nn.BatchNorm2d(512), 110 | ), 111 | nn.Sequential( # Sequential, 112 | nn.Conv2d(256,512,(1, 1),(2, 2),(0, 0),1,1,bias=False), 113 | nn.BatchNorm2d(512), 114 | ), 115 | ), 116 | LambdaReduce(lambda x,y: x+y), # CAddTable, 117 | nn.ReLU(), 118 | ), 119 | nn.Sequential( # Sequential, 120 | LambdaMap(lambda x: x, # ConcatTable, 121 | nn.Sequential( # Sequential, 122 | nn.Sequential( # Sequential, 123 | nn.Conv2d(512,256,(1, 1),(1, 1),(0, 0),1,1,bias=False), 124 | nn.BatchNorm2d(256), 125 | nn.ReLU(), 126 | nn.Conv2d(256,256,(3, 3),(1, 1),(1, 1),1,32,bias=False), 127 | nn.BatchNorm2d(256), 128 | nn.ReLU(), 129 | ), 130 | nn.Conv2d(256,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 131 | nn.BatchNorm2d(512), 132 | ), 133 | Lambda(lambda x: x), # Identity, 134 | ), 135 | LambdaReduce(lambda x,y: x+y), # CAddTable, 136 | nn.ReLU(), 137 | ), 138 | nn.Sequential( # Sequential, 139 | LambdaMap(lambda x: x, # ConcatTable, 140 | nn.Sequential( # Sequential, 141 | nn.Sequential( # Sequential, 142 | nn.Conv2d(512,256,(1, 1),(1, 1),(0, 0),1,1,bias=False), 143 | nn.BatchNorm2d(256), 144 | nn.ReLU(), 145 | nn.Conv2d(256,256,(3, 3),(1, 1),(1, 1),1,32,bias=False), 146 | nn.BatchNorm2d(256), 147 | nn.ReLU(), 148 | ), 149 | nn.Conv2d(256,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 150 | nn.BatchNorm2d(512), 151 | ), 152 | Lambda(lambda x: x), # Identity, 153 | ), 154 | LambdaReduce(lambda x,y: x+y), # CAddTable, 155 | nn.ReLU(), 156 | ), 157 | nn.Sequential( # Sequential, 158 | LambdaMap(lambda x: x, # ConcatTable, 159 | nn.Sequential( # Sequential, 160 | nn.Sequential( # Sequential, 161 | nn.Conv2d(512,256,(1, 1),(1, 1),(0, 0),1,1,bias=False), 162 | nn.BatchNorm2d(256), 163 | nn.ReLU(), 164 | nn.Conv2d(256,256,(3, 3),(1, 1),(1, 1),1,32,bias=False), 165 | nn.BatchNorm2d(256), 166 | nn.ReLU(), 167 | ), 168 | nn.Conv2d(256,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 169 | nn.BatchNorm2d(512), 170 | ), 171 | Lambda(lambda x: x), # Identity, 172 | ), 173 | LambdaReduce(lambda x,y: x+y), # CAddTable, 174 | nn.ReLU(), 175 | ), 176 | ), 177 | nn.Sequential( # Sequential, 178 | nn.Sequential( # Sequential, 179 | LambdaMap(lambda x: x, # ConcatTable, 180 | nn.Sequential( # Sequential, 181 | nn.Sequential( # Sequential, 182 | nn.Conv2d(512,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 183 | nn.BatchNorm2d(512), 184 | nn.ReLU(), 185 | nn.Conv2d(512,512,(3, 3),(2, 2),(1, 1),1,32,bias=False), 186 | nn.BatchNorm2d(512), 187 | nn.ReLU(), 188 | ), 189 | nn.Conv2d(512,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 190 | nn.BatchNorm2d(1024), 191 | ), 192 | nn.Sequential( # Sequential, 193 | nn.Conv2d(512,1024,(1, 1),(2, 2),(0, 0),1,1,bias=False), 194 | nn.BatchNorm2d(1024), 195 | ), 196 | ), 197 | LambdaReduce(lambda x,y: x+y), # CAddTable, 198 | nn.ReLU(), 199 | ), 200 | nn.Sequential( # Sequential, 201 | LambdaMap(lambda x: x, # ConcatTable, 202 | nn.Sequential( # Sequential, 203 | nn.Sequential( # Sequential, 204 | nn.Conv2d(1024,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 205 | nn.BatchNorm2d(512), 206 | nn.ReLU(), 207 | nn.Conv2d(512,512,(3, 3),(1, 1),(1, 1),1,32,bias=False), 208 | nn.BatchNorm2d(512), 209 | nn.ReLU(), 210 | ), 211 | nn.Conv2d(512,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 212 | nn.BatchNorm2d(1024), 213 | ), 214 | Lambda(lambda x: x), # Identity, 215 | ), 216 | LambdaReduce(lambda x,y: x+y), # CAddTable, 217 | nn.ReLU(), 218 | ), 219 | nn.Sequential( # Sequential, 220 | LambdaMap(lambda x: x, # ConcatTable, 221 | nn.Sequential( # Sequential, 222 | nn.Sequential( # Sequential, 223 | nn.Conv2d(1024,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 224 | nn.BatchNorm2d(512), 225 | nn.ReLU(), 226 | nn.Conv2d(512,512,(3, 3),(1, 1),(1, 1),1,32,bias=False), 227 | nn.BatchNorm2d(512), 228 | nn.ReLU(), 229 | ), 230 | nn.Conv2d(512,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 231 | nn.BatchNorm2d(1024), 232 | ), 233 | Lambda(lambda x: x), # Identity, 234 | ), 235 | LambdaReduce(lambda x,y: x+y), # CAddTable, 236 | nn.ReLU(), 237 | ), 238 | nn.Sequential( # Sequential, 239 | LambdaMap(lambda x: x, # ConcatTable, 240 | nn.Sequential( # Sequential, 241 | nn.Sequential( # Sequential, 242 | nn.Conv2d(1024,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 243 | nn.BatchNorm2d(512), 244 | nn.ReLU(), 245 | nn.Conv2d(512,512,(3, 3),(1, 1),(1, 1),1,32,bias=False), 246 | nn.BatchNorm2d(512), 247 | nn.ReLU(), 248 | ), 249 | nn.Conv2d(512,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 250 | nn.BatchNorm2d(1024), 251 | ), 252 | Lambda(lambda x: x), # Identity, 253 | ), 254 | LambdaReduce(lambda x,y: x+y), # CAddTable, 255 | nn.ReLU(), 256 | ), 257 | nn.Sequential( # Sequential, 258 | LambdaMap(lambda x: x, # ConcatTable, 259 | nn.Sequential( # Sequential, 260 | nn.Sequential( # Sequential, 261 | nn.Conv2d(1024,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 262 | nn.BatchNorm2d(512), 263 | nn.ReLU(), 264 | nn.Conv2d(512,512,(3, 3),(1, 1),(1, 1),1,32,bias=False), 265 | nn.BatchNorm2d(512), 266 | nn.ReLU(), 267 | ), 268 | nn.Conv2d(512,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 269 | nn.BatchNorm2d(1024), 270 | ), 271 | Lambda(lambda x: x), # Identity, 272 | ), 273 | LambdaReduce(lambda x,y: x+y), # CAddTable, 274 | nn.ReLU(), 275 | ), 276 | nn.Sequential( # Sequential, 277 | LambdaMap(lambda x: x, # ConcatTable, 278 | nn.Sequential( # Sequential, 279 | nn.Sequential( # Sequential, 280 | nn.Conv2d(1024,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 281 | nn.BatchNorm2d(512), 282 | nn.ReLU(), 283 | nn.Conv2d(512,512,(3, 3),(1, 1),(1, 1),1,32,bias=False), 284 | nn.BatchNorm2d(512), 285 | nn.ReLU(), 286 | ), 287 | nn.Conv2d(512,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 288 | nn.BatchNorm2d(1024), 289 | ), 290 | Lambda(lambda x: x), # Identity, 291 | ), 292 | LambdaReduce(lambda x,y: x+y), # CAddTable, 293 | nn.ReLU(), 294 | ), 295 | nn.Sequential( # Sequential, 296 | LambdaMap(lambda x: x, # ConcatTable, 297 | nn.Sequential( # Sequential, 298 | nn.Sequential( # Sequential, 299 | nn.Conv2d(1024,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 300 | nn.BatchNorm2d(512), 301 | nn.ReLU(), 302 | nn.Conv2d(512,512,(3, 3),(1, 1),(1, 1),1,32,bias=False), 303 | nn.BatchNorm2d(512), 304 | nn.ReLU(), 305 | ), 306 | nn.Conv2d(512,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 307 | nn.BatchNorm2d(1024), 308 | ), 309 | Lambda(lambda x: x), # Identity, 310 | ), 311 | LambdaReduce(lambda x,y: x+y), # CAddTable, 312 | nn.ReLU(), 313 | ), 314 | nn.Sequential( # Sequential, 315 | LambdaMap(lambda x: x, # ConcatTable, 316 | nn.Sequential( # Sequential, 317 | nn.Sequential( # Sequential, 318 | nn.Conv2d(1024,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 319 | nn.BatchNorm2d(512), 320 | nn.ReLU(), 321 | nn.Conv2d(512,512,(3, 3),(1, 1),(1, 1),1,32,bias=False), 322 | nn.BatchNorm2d(512), 323 | nn.ReLU(), 324 | ), 325 | nn.Conv2d(512,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 326 | nn.BatchNorm2d(1024), 327 | ), 328 | Lambda(lambda x: x), # Identity, 329 | ), 330 | LambdaReduce(lambda x,y: x+y), # CAddTable, 331 | nn.ReLU(), 332 | ), 333 | nn.Sequential( # Sequential, 334 | LambdaMap(lambda x: x, # ConcatTable, 335 | nn.Sequential( # Sequential, 336 | nn.Sequential( # Sequential, 337 | nn.Conv2d(1024,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 338 | nn.BatchNorm2d(512), 339 | nn.ReLU(), 340 | nn.Conv2d(512,512,(3, 3),(1, 1),(1, 1),1,32,bias=False), 341 | nn.BatchNorm2d(512), 342 | nn.ReLU(), 343 | ), 344 | nn.Conv2d(512,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 345 | nn.BatchNorm2d(1024), 346 | ), 347 | Lambda(lambda x: x), # Identity, 348 | ), 349 | LambdaReduce(lambda x,y: x+y), # CAddTable, 350 | nn.ReLU(), 351 | ), 352 | nn.Sequential( # Sequential, 353 | LambdaMap(lambda x: x, # ConcatTable, 354 | nn.Sequential( # Sequential, 355 | nn.Sequential( # Sequential, 356 | nn.Conv2d(1024,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 357 | nn.BatchNorm2d(512), 358 | nn.ReLU(), 359 | nn.Conv2d(512,512,(3, 3),(1, 1),(1, 1),1,32,bias=False), 360 | nn.BatchNorm2d(512), 361 | nn.ReLU(), 362 | ), 363 | nn.Conv2d(512,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 364 | nn.BatchNorm2d(1024), 365 | ), 366 | Lambda(lambda x: x), # Identity, 367 | ), 368 | LambdaReduce(lambda x,y: x+y), # CAddTable, 369 | nn.ReLU(), 370 | ), 371 | nn.Sequential( # Sequential, 372 | LambdaMap(lambda x: x, # ConcatTable, 373 | nn.Sequential( # Sequential, 374 | nn.Sequential( # Sequential, 375 | nn.Conv2d(1024,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 376 | nn.BatchNorm2d(512), 377 | nn.ReLU(), 378 | nn.Conv2d(512,512,(3, 3),(1, 1),(1, 1),1,32,bias=False), 379 | nn.BatchNorm2d(512), 380 | nn.ReLU(), 381 | ), 382 | nn.Conv2d(512,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 383 | nn.BatchNorm2d(1024), 384 | ), 385 | Lambda(lambda x: x), # Identity, 386 | ), 387 | LambdaReduce(lambda x,y: x+y), # CAddTable, 388 | nn.ReLU(), 389 | ), 390 | nn.Sequential( # Sequential, 391 | LambdaMap(lambda x: x, # ConcatTable, 392 | nn.Sequential( # Sequential, 393 | nn.Sequential( # Sequential, 394 | nn.Conv2d(1024,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 395 | nn.BatchNorm2d(512), 396 | nn.ReLU(), 397 | nn.Conv2d(512,512,(3, 3),(1, 1),(1, 1),1,32,bias=False), 398 | nn.BatchNorm2d(512), 399 | nn.ReLU(), 400 | ), 401 | nn.Conv2d(512,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 402 | nn.BatchNorm2d(1024), 403 | ), 404 | Lambda(lambda x: x), # Identity, 405 | ), 406 | LambdaReduce(lambda x,y: x+y), # CAddTable, 407 | nn.ReLU(), 408 | ), 409 | nn.Sequential( # Sequential, 410 | LambdaMap(lambda x: x, # ConcatTable, 411 | nn.Sequential( # Sequential, 412 | nn.Sequential( # Sequential, 413 | nn.Conv2d(1024,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 414 | nn.BatchNorm2d(512), 415 | nn.ReLU(), 416 | nn.Conv2d(512,512,(3, 3),(1, 1),(1, 1),1,32,bias=False), 417 | nn.BatchNorm2d(512), 418 | nn.ReLU(), 419 | ), 420 | nn.Conv2d(512,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 421 | nn.BatchNorm2d(1024), 422 | ), 423 | Lambda(lambda x: x), # Identity, 424 | ), 425 | LambdaReduce(lambda x,y: x+y), # CAddTable, 426 | nn.ReLU(), 427 | ), 428 | nn.Sequential( # Sequential, 429 | LambdaMap(lambda x: x, # ConcatTable, 430 | nn.Sequential( # Sequential, 431 | nn.Sequential( # Sequential, 432 | nn.Conv2d(1024,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 433 | nn.BatchNorm2d(512), 434 | nn.ReLU(), 435 | nn.Conv2d(512,512,(3, 3),(1, 1),(1, 1),1,32,bias=False), 436 | nn.BatchNorm2d(512), 437 | nn.ReLU(), 438 | ), 439 | nn.Conv2d(512,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 440 | nn.BatchNorm2d(1024), 441 | ), 442 | Lambda(lambda x: x), # Identity, 443 | ), 444 | LambdaReduce(lambda x,y: x+y), # CAddTable, 445 | nn.ReLU(), 446 | ), 447 | nn.Sequential( # Sequential, 448 | LambdaMap(lambda x: x, # ConcatTable, 449 | nn.Sequential( # Sequential, 450 | nn.Sequential( # Sequential, 451 | nn.Conv2d(1024,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 452 | nn.BatchNorm2d(512), 453 | nn.ReLU(), 454 | nn.Conv2d(512,512,(3, 3),(1, 1),(1, 1),1,32,bias=False), 455 | nn.BatchNorm2d(512), 456 | nn.ReLU(), 457 | ), 458 | nn.Conv2d(512,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 459 | nn.BatchNorm2d(1024), 460 | ), 461 | Lambda(lambda x: x), # Identity, 462 | ), 463 | LambdaReduce(lambda x,y: x+y), # CAddTable, 464 | nn.ReLU(), 465 | ), 466 | nn.Sequential( # Sequential, 467 | LambdaMap(lambda x: x, # ConcatTable, 468 | nn.Sequential( # Sequential, 469 | nn.Sequential( # Sequential, 470 | nn.Conv2d(1024,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 471 | nn.BatchNorm2d(512), 472 | nn.ReLU(), 473 | nn.Conv2d(512,512,(3, 3),(1, 1),(1, 1),1,32,bias=False), 474 | nn.BatchNorm2d(512), 475 | nn.ReLU(), 476 | ), 477 | nn.Conv2d(512,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 478 | nn.BatchNorm2d(1024), 479 | ), 480 | Lambda(lambda x: x), # Identity, 481 | ), 482 | LambdaReduce(lambda x,y: x+y), # CAddTable, 483 | nn.ReLU(), 484 | ), 485 | nn.Sequential( # Sequential, 486 | LambdaMap(lambda x: x, # ConcatTable, 487 | nn.Sequential( # Sequential, 488 | nn.Sequential( # Sequential, 489 | nn.Conv2d(1024,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 490 | nn.BatchNorm2d(512), 491 | nn.ReLU(), 492 | nn.Conv2d(512,512,(3, 3),(1, 1),(1, 1),1,32,bias=False), 493 | nn.BatchNorm2d(512), 494 | nn.ReLU(), 495 | ), 496 | nn.Conv2d(512,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 497 | nn.BatchNorm2d(1024), 498 | ), 499 | Lambda(lambda x: x), # Identity, 500 | ), 501 | LambdaReduce(lambda x,y: x+y), # CAddTable, 502 | nn.ReLU(), 503 | ), 504 | nn.Sequential( # Sequential, 505 | LambdaMap(lambda x: x, # ConcatTable, 506 | nn.Sequential( # Sequential, 507 | nn.Sequential( # Sequential, 508 | nn.Conv2d(1024,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 509 | nn.BatchNorm2d(512), 510 | nn.ReLU(), 511 | nn.Conv2d(512,512,(3, 3),(1, 1),(1, 1),1,32,bias=False), 512 | nn.BatchNorm2d(512), 513 | nn.ReLU(), 514 | ), 515 | nn.Conv2d(512,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 516 | nn.BatchNorm2d(1024), 517 | ), 518 | Lambda(lambda x: x), # Identity, 519 | ), 520 | LambdaReduce(lambda x,y: x+y), # CAddTable, 521 | nn.ReLU(), 522 | ), 523 | nn.Sequential( # Sequential, 524 | LambdaMap(lambda x: x, # ConcatTable, 525 | nn.Sequential( # Sequential, 526 | nn.Sequential( # Sequential, 527 | nn.Conv2d(1024,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 528 | nn.BatchNorm2d(512), 529 | nn.ReLU(), 530 | nn.Conv2d(512,512,(3, 3),(1, 1),(1, 1),1,32,bias=False), 531 | nn.BatchNorm2d(512), 532 | nn.ReLU(), 533 | ), 534 | nn.Conv2d(512,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 535 | nn.BatchNorm2d(1024), 536 | ), 537 | Lambda(lambda x: x), # Identity, 538 | ), 539 | LambdaReduce(lambda x,y: x+y), # CAddTable, 540 | nn.ReLU(), 541 | ), 542 | nn.Sequential( # Sequential, 543 | LambdaMap(lambda x: x, # ConcatTable, 544 | nn.Sequential( # Sequential, 545 | nn.Sequential( # Sequential, 546 | nn.Conv2d(1024,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 547 | nn.BatchNorm2d(512), 548 | nn.ReLU(), 549 | nn.Conv2d(512,512,(3, 3),(1, 1),(1, 1),1,32,bias=False), 550 | nn.BatchNorm2d(512), 551 | nn.ReLU(), 552 | ), 553 | nn.Conv2d(512,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 554 | nn.BatchNorm2d(1024), 555 | ), 556 | Lambda(lambda x: x), # Identity, 557 | ), 558 | LambdaReduce(lambda x,y: x+y), # CAddTable, 559 | nn.ReLU(), 560 | ), 561 | nn.Sequential( # Sequential, 562 | LambdaMap(lambda x: x, # ConcatTable, 563 | nn.Sequential( # Sequential, 564 | nn.Sequential( # Sequential, 565 | nn.Conv2d(1024,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 566 | nn.BatchNorm2d(512), 567 | nn.ReLU(), 568 | nn.Conv2d(512,512,(3, 3),(1, 1),(1, 1),1,32,bias=False), 569 | nn.BatchNorm2d(512), 570 | nn.ReLU(), 571 | ), 572 | nn.Conv2d(512,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 573 | nn.BatchNorm2d(1024), 574 | ), 575 | Lambda(lambda x: x), # Identity, 576 | ), 577 | LambdaReduce(lambda x,y: x+y), # CAddTable, 578 | nn.ReLU(), 579 | ), 580 | nn.Sequential( # Sequential, 581 | LambdaMap(lambda x: x, # ConcatTable, 582 | nn.Sequential( # Sequential, 583 | nn.Sequential( # Sequential, 584 | nn.Conv2d(1024,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 585 | nn.BatchNorm2d(512), 586 | nn.ReLU(), 587 | nn.Conv2d(512,512,(3, 3),(1, 1),(1, 1),1,32,bias=False), 588 | nn.BatchNorm2d(512), 589 | nn.ReLU(), 590 | ), 591 | nn.Conv2d(512,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 592 | nn.BatchNorm2d(1024), 593 | ), 594 | Lambda(lambda x: x), # Identity, 595 | ), 596 | LambdaReduce(lambda x,y: x+y), # CAddTable, 597 | nn.ReLU(), 598 | ), 599 | nn.Sequential( # Sequential, 600 | LambdaMap(lambda x: x, # ConcatTable, 601 | nn.Sequential( # Sequential, 602 | nn.Sequential( # Sequential, 603 | nn.Conv2d(1024,512,(1, 1),(1, 1),(0, 0),1,1,bias=False), 604 | nn.BatchNorm2d(512), 605 | nn.ReLU(), 606 | nn.Conv2d(512,512,(3, 3),(1, 1),(1, 1),1,32,bias=False), 607 | nn.BatchNorm2d(512), 608 | nn.ReLU(), 609 | ), 610 | nn.Conv2d(512,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 611 | nn.BatchNorm2d(1024), 612 | ), 613 | Lambda(lambda x: x), # Identity, 614 | ), 615 | LambdaReduce(lambda x,y: x+y), # CAddTable, 616 | nn.ReLU(), 617 | ), 618 | ), 619 | nn.Sequential( # Sequential, 620 | nn.Sequential( # Sequential, 621 | LambdaMap(lambda x: x, # ConcatTable, 622 | nn.Sequential( # Sequential, 623 | nn.Sequential( # Sequential, 624 | nn.Conv2d(1024,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 625 | nn.BatchNorm2d(1024), 626 | nn.ReLU(), 627 | nn.Conv2d(1024,1024,(3, 3),(2, 2),(1, 1),1,32,bias=False), 628 | nn.BatchNorm2d(1024), 629 | nn.ReLU(), 630 | ), 631 | nn.Conv2d(1024,2048,(1, 1),(1, 1),(0, 0),1,1,bias=False), 632 | nn.BatchNorm2d(2048), 633 | ), 634 | nn.Sequential( # Sequential, 635 | nn.Conv2d(1024,2048,(1, 1),(2, 2),(0, 0),1,1,bias=False), 636 | nn.BatchNorm2d(2048), 637 | ), 638 | ), 639 | LambdaReduce(lambda x,y: x+y), # CAddTable, 640 | nn.ReLU(), 641 | ), 642 | nn.Sequential( # Sequential, 643 | LambdaMap(lambda x: x, # ConcatTable, 644 | nn.Sequential( # Sequential, 645 | nn.Sequential( # Sequential, 646 | nn.Conv2d(2048,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 647 | nn.BatchNorm2d(1024), 648 | nn.ReLU(), 649 | nn.Conv2d(1024,1024,(3, 3),(1, 1),(1, 1),1,32,bias=False), 650 | nn.BatchNorm2d(1024), 651 | nn.ReLU(), 652 | ), 653 | nn.Conv2d(1024,2048,(1, 1),(1, 1),(0, 0),1,1,bias=False), 654 | nn.BatchNorm2d(2048), 655 | ), 656 | Lambda(lambda x: x), # Identity, 657 | ), 658 | LambdaReduce(lambda x,y: x+y), # CAddTable, 659 | nn.ReLU(), 660 | ), 661 | nn.Sequential( # Sequential, 662 | LambdaMap(lambda x: x, # ConcatTable, 663 | nn.Sequential( # Sequential, 664 | nn.Sequential( # Sequential, 665 | nn.Conv2d(2048,1024,(1, 1),(1, 1),(0, 0),1,1,bias=False), 666 | nn.BatchNorm2d(1024), 667 | nn.ReLU(), 668 | nn.Conv2d(1024,1024,(3, 3),(1, 1),(1, 1),1,32,bias=False), 669 | nn.BatchNorm2d(1024), 670 | nn.ReLU(), 671 | ), 672 | nn.Conv2d(1024,2048,(1, 1),(1, 1),(0, 0),1,1,bias=False), 673 | nn.BatchNorm2d(2048), 674 | ), 675 | Lambda(lambda x: x), # Identity, 676 | ), 677 | LambdaReduce(lambda x,y: x+y), # CAddTable, 678 | nn.ReLU(), 679 | ), 680 | ) 681 | ) -------------------------------------------------------------------------------- /tars/resnext_features/resnext101_64x4d_features.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | from torch.autograd import Variable 4 | from functools import reduce 5 | 6 | class LambdaBase(nn.Sequential): 7 | def __init__(self, fn, *args): 8 | super(LambdaBase, self).__init__(*args) 9 | self.lambda_func = fn 10 | 11 | def forward_prepare(self, input): 12 | output = [] 13 | for module in self._modules.values(): 14 | output.append(module(input)) 15 | return output if output else input 16 | 17 | class Lambda(LambdaBase): 18 | def forward(self, input): 19 | return self.lambda_func(self.forward_prepare(input)) 20 | 21 | class LambdaMap(LambdaBase): 22 | def forward(self, input): 23 | return list(map(self.lambda_func,self.forward_prepare(input))) 24 | 25 | class LambdaReduce(LambdaBase): 26 | def forward(self, input): 27 | return reduce(self.lambda_func,self.forward_prepare(input)) 28 | 29 | resnext101_64x4d_features = nn.Sequential(#Sequential, 30 | nn.Conv2d(3, 64, (7, 7), (2, 2), (3, 3), 1, 1, bias = False), 31 | nn.BatchNorm2d(64), 32 | nn.ReLU(), 33 | nn.MaxPool2d((3, 3), (2, 2), (1, 1)), 34 | nn.Sequential(#Sequential, 35 | nn.Sequential(#Sequential, 36 | LambdaMap(lambda x: x, #ConcatTable, 37 | nn.Sequential(#Sequential, 38 | nn.Sequential(#Sequential, 39 | nn.Conv2d(64, 256, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 40 | nn.BatchNorm2d(256), 41 | nn.ReLU(), 42 | nn.Conv2d(256, 256, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 43 | nn.BatchNorm2d(256), 44 | nn.ReLU(), 45 | ), 46 | nn.Conv2d(256, 256, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 47 | nn.BatchNorm2d(256), 48 | ), 49 | nn.Sequential(#Sequential, 50 | nn.Conv2d(64, 256, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 51 | nn.BatchNorm2d(256), 52 | ), 53 | ), 54 | LambdaReduce(lambda x, y: x + y), #CAddTable, 55 | nn.ReLU(), 56 | ), 57 | nn.Sequential(#Sequential, 58 | LambdaMap(lambda x: x, #ConcatTable, 59 | nn.Sequential(#Sequential, 60 | nn.Sequential(#Sequential, 61 | nn.Conv2d(256, 256, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 62 | nn.BatchNorm2d(256), 63 | nn.ReLU(), 64 | nn.Conv2d(256, 256, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 65 | nn.BatchNorm2d(256), 66 | nn.ReLU(), 67 | ), 68 | nn.Conv2d(256, 256, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 69 | nn.BatchNorm2d(256), 70 | ), 71 | Lambda(lambda x: x), #Identity, 72 | ), 73 | LambdaReduce(lambda x, y: x + y), #CAddTable, 74 | nn.ReLU(), 75 | ), 76 | nn.Sequential(#Sequential, 77 | LambdaMap(lambda x: x, #ConcatTable, 78 | nn.Sequential(#Sequential, 79 | nn.Sequential(#Sequential, 80 | nn.Conv2d(256, 256, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 81 | nn.BatchNorm2d(256), 82 | nn.ReLU(), 83 | nn.Conv2d(256, 256, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 84 | nn.BatchNorm2d(256), 85 | nn.ReLU(), 86 | ), 87 | nn.Conv2d(256, 256, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 88 | nn.BatchNorm2d(256), 89 | ), 90 | Lambda(lambda x: x), #Identity, 91 | ), 92 | LambdaReduce(lambda x, y: x + y), #CAddTable, 93 | nn.ReLU(), 94 | ), 95 | ), 96 | nn.Sequential(#Sequential, 97 | nn.Sequential(#Sequential, 98 | LambdaMap(lambda x: x, #ConcatTable, 99 | nn.Sequential(#Sequential, 100 | nn.Sequential(#Sequential, 101 | nn.Conv2d(256, 512, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 102 | nn.BatchNorm2d(512), 103 | nn.ReLU(), 104 | nn.Conv2d(512, 512, (3, 3), (2, 2), (1, 1), 1, 64, bias = False), 105 | nn.BatchNorm2d(512), 106 | nn.ReLU(), 107 | ), 108 | nn.Conv2d(512, 512, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 109 | nn.BatchNorm2d(512), 110 | ), 111 | nn.Sequential(#Sequential, 112 | nn.Conv2d(256, 512, (1, 1), (2, 2), (0, 0), 1, 1, bias = False), 113 | nn.BatchNorm2d(512), 114 | ), 115 | ), 116 | LambdaReduce(lambda x, y: x + y), #CAddTable, 117 | nn.ReLU(), 118 | ), 119 | nn.Sequential(#Sequential, 120 | LambdaMap(lambda x: x, #ConcatTable, 121 | nn.Sequential(#Sequential, 122 | nn.Sequential(#Sequential, 123 | nn.Conv2d(512, 512, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 124 | nn.BatchNorm2d(512), 125 | nn.ReLU(), 126 | nn.Conv2d(512, 512, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 127 | nn.BatchNorm2d(512), 128 | nn.ReLU(), 129 | ), 130 | nn.Conv2d(512, 512, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 131 | nn.BatchNorm2d(512), 132 | ), 133 | Lambda(lambda x: x), #Identity, 134 | ), 135 | LambdaReduce(lambda x, y: x + y), #CAddTable, 136 | nn.ReLU(), 137 | ), 138 | nn.Sequential(#Sequential, 139 | LambdaMap(lambda x: x, #ConcatTable, 140 | nn.Sequential(#Sequential, 141 | nn.Sequential(#Sequential, 142 | nn.Conv2d(512, 512, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 143 | nn.BatchNorm2d(512), 144 | nn.ReLU(), 145 | nn.Conv2d(512, 512, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 146 | nn.BatchNorm2d(512), 147 | nn.ReLU(), 148 | ), 149 | nn.Conv2d(512, 512, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 150 | nn.BatchNorm2d(512), 151 | ), 152 | Lambda(lambda x: x), #Identity, 153 | ), 154 | LambdaReduce(lambda x, y: x + y), #CAddTable, 155 | nn.ReLU(), 156 | ), 157 | nn.Sequential(#Sequential, 158 | LambdaMap(lambda x: x, #ConcatTable, 159 | nn.Sequential(#Sequential, 160 | nn.Sequential(#Sequential, 161 | nn.Conv2d(512, 512, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 162 | nn.BatchNorm2d(512), 163 | nn.ReLU(), 164 | nn.Conv2d(512, 512, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 165 | nn.BatchNorm2d(512), 166 | nn.ReLU(), 167 | ), 168 | nn.Conv2d(512, 512, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 169 | nn.BatchNorm2d(512), 170 | ), 171 | Lambda(lambda x: x), #Identity, 172 | ), 173 | LambdaReduce(lambda x, y: x + y), #CAddTable, 174 | nn.ReLU(), 175 | ), 176 | ), 177 | nn.Sequential(#Sequential, 178 | nn.Sequential(#Sequential, 179 | LambdaMap(lambda x: x, #ConcatTable, 180 | nn.Sequential(#Sequential, 181 | nn.Sequential(#Sequential, 182 | nn.Conv2d(512, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 183 | nn.BatchNorm2d(1024), 184 | nn.ReLU(), 185 | nn.Conv2d(1024, 1024, (3, 3), (2, 2), (1, 1), 1, 64, bias = False), 186 | nn.BatchNorm2d(1024), 187 | nn.ReLU(), 188 | ), 189 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 190 | nn.BatchNorm2d(1024), 191 | ), 192 | nn.Sequential(#Sequential, 193 | nn.Conv2d(512, 1024, (1, 1), (2, 2), (0, 0), 1, 1, bias = False), 194 | nn.BatchNorm2d(1024), 195 | ), 196 | ), 197 | LambdaReduce(lambda x, y: x + y), #CAddTable, 198 | nn.ReLU(), 199 | ), 200 | nn.Sequential(#Sequential, 201 | LambdaMap(lambda x: x, #ConcatTable, 202 | nn.Sequential(#Sequential, 203 | nn.Sequential(#Sequential, 204 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 205 | nn.BatchNorm2d(1024), 206 | nn.ReLU(), 207 | nn.Conv2d(1024, 1024, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 208 | nn.BatchNorm2d(1024), 209 | nn.ReLU(), 210 | ), 211 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 212 | nn.BatchNorm2d(1024), 213 | ), 214 | Lambda(lambda x: x), #Identity, 215 | ), 216 | LambdaReduce(lambda x, y: x + y), #CAddTable, 217 | nn.ReLU(), 218 | ), 219 | nn.Sequential(#Sequential, 220 | LambdaMap(lambda x: x, #ConcatTable, 221 | nn.Sequential(#Sequential, 222 | nn.Sequential(#Sequential, 223 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 224 | nn.BatchNorm2d(1024), 225 | nn.ReLU(), 226 | nn.Conv2d(1024, 1024, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 227 | nn.BatchNorm2d(1024), 228 | nn.ReLU(), 229 | ), 230 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 231 | nn.BatchNorm2d(1024), 232 | ), 233 | Lambda(lambda x: x), #Identity, 234 | ), 235 | LambdaReduce(lambda x, y: x + y), #CAddTable, 236 | nn.ReLU(), 237 | ), 238 | nn.Sequential(#Sequential, 239 | LambdaMap(lambda x: x, #ConcatTable, 240 | nn.Sequential(#Sequential, 241 | nn.Sequential(#Sequential, 242 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 243 | nn.BatchNorm2d(1024), 244 | nn.ReLU(), 245 | nn.Conv2d(1024, 1024, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 246 | nn.BatchNorm2d(1024), 247 | nn.ReLU(), 248 | ), 249 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 250 | nn.BatchNorm2d(1024), 251 | ), 252 | Lambda(lambda x: x), #Identity, 253 | ), 254 | LambdaReduce(lambda x, y: x + y), #CAddTable, 255 | nn.ReLU(), 256 | ), 257 | nn.Sequential(#Sequential, 258 | LambdaMap(lambda x: x, #ConcatTable, 259 | nn.Sequential(#Sequential, 260 | nn.Sequential(#Sequential, 261 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 262 | nn.BatchNorm2d(1024), 263 | nn.ReLU(), 264 | nn.Conv2d(1024, 1024, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 265 | nn.BatchNorm2d(1024), 266 | nn.ReLU(), 267 | ), 268 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 269 | nn.BatchNorm2d(1024), 270 | ), 271 | Lambda(lambda x: x), #Identity, 272 | ), 273 | LambdaReduce(lambda x, y: x + y), #CAddTable, 274 | nn.ReLU(), 275 | ), 276 | nn.Sequential(#Sequential, 277 | LambdaMap(lambda x: x, #ConcatTable, 278 | nn.Sequential(#Sequential, 279 | nn.Sequential(#Sequential, 280 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 281 | nn.BatchNorm2d(1024), 282 | nn.ReLU(), 283 | nn.Conv2d(1024, 1024, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 284 | nn.BatchNorm2d(1024), 285 | nn.ReLU(), 286 | ), 287 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 288 | nn.BatchNorm2d(1024), 289 | ), 290 | Lambda(lambda x: x), #Identity, 291 | ), 292 | LambdaReduce(lambda x, y: x + y), #CAddTable, 293 | nn.ReLU(), 294 | ), 295 | nn.Sequential(#Sequential, 296 | LambdaMap(lambda x: x, #ConcatTable, 297 | nn.Sequential(#Sequential, 298 | nn.Sequential(#Sequential, 299 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 300 | nn.BatchNorm2d(1024), 301 | nn.ReLU(), 302 | nn.Conv2d(1024, 1024, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 303 | nn.BatchNorm2d(1024), 304 | nn.ReLU(), 305 | ), 306 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 307 | nn.BatchNorm2d(1024), 308 | ), 309 | Lambda(lambda x: x), #Identity, 310 | ), 311 | LambdaReduce(lambda x, y: x + y), #CAddTable, 312 | nn.ReLU(), 313 | ), 314 | nn.Sequential(#Sequential, 315 | LambdaMap(lambda x: x, #ConcatTable, 316 | nn.Sequential(#Sequential, 317 | nn.Sequential(#Sequential, 318 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 319 | nn.BatchNorm2d(1024), 320 | nn.ReLU(), 321 | nn.Conv2d(1024, 1024, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 322 | nn.BatchNorm2d(1024), 323 | nn.ReLU(), 324 | ), 325 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 326 | nn.BatchNorm2d(1024), 327 | ), 328 | Lambda(lambda x: x), #Identity, 329 | ), 330 | LambdaReduce(lambda x, y: x + y), #CAddTable, 331 | nn.ReLU(), 332 | ), 333 | nn.Sequential(#Sequential, 334 | LambdaMap(lambda x: x, #ConcatTable, 335 | nn.Sequential(#Sequential, 336 | nn.Sequential(#Sequential, 337 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 338 | nn.BatchNorm2d(1024), 339 | nn.ReLU(), 340 | nn.Conv2d(1024, 1024, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 341 | nn.BatchNorm2d(1024), 342 | nn.ReLU(), 343 | ), 344 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 345 | nn.BatchNorm2d(1024), 346 | ), 347 | Lambda(lambda x: x), #Identity, 348 | ), 349 | LambdaReduce(lambda x, y: x + y), #CAddTable, 350 | nn.ReLU(), 351 | ), 352 | nn.Sequential(#Sequential, 353 | LambdaMap(lambda x: x, #ConcatTable, 354 | nn.Sequential(#Sequential, 355 | nn.Sequential(#Sequential, 356 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 357 | nn.BatchNorm2d(1024), 358 | nn.ReLU(), 359 | nn.Conv2d(1024, 1024, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 360 | nn.BatchNorm2d(1024), 361 | nn.ReLU(), 362 | ), 363 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 364 | nn.BatchNorm2d(1024), 365 | ), 366 | Lambda(lambda x: x), #Identity, 367 | ), 368 | LambdaReduce(lambda x, y: x + y), #CAddTable, 369 | nn.ReLU(), 370 | ), 371 | nn.Sequential(#Sequential, 372 | LambdaMap(lambda x: x, #ConcatTable, 373 | nn.Sequential(#Sequential, 374 | nn.Sequential(#Sequential, 375 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 376 | nn.BatchNorm2d(1024), 377 | nn.ReLU(), 378 | nn.Conv2d(1024, 1024, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 379 | nn.BatchNorm2d(1024), 380 | nn.ReLU(), 381 | ), 382 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 383 | nn.BatchNorm2d(1024), 384 | ), 385 | Lambda(lambda x: x), #Identity, 386 | ), 387 | LambdaReduce(lambda x, y: x + y), #CAddTable, 388 | nn.ReLU(), 389 | ), 390 | nn.Sequential(#Sequential, 391 | LambdaMap(lambda x: x, #ConcatTable, 392 | nn.Sequential(#Sequential, 393 | nn.Sequential(#Sequential, 394 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 395 | nn.BatchNorm2d(1024), 396 | nn.ReLU(), 397 | nn.Conv2d(1024, 1024, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 398 | nn.BatchNorm2d(1024), 399 | nn.ReLU(), 400 | ), 401 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 402 | nn.BatchNorm2d(1024), 403 | ), 404 | Lambda(lambda x: x), #Identity, 405 | ), 406 | LambdaReduce(lambda x, y: x + y), #CAddTable, 407 | nn.ReLU(), 408 | ), 409 | nn.Sequential(#Sequential, 410 | LambdaMap(lambda x: x, #ConcatTable, 411 | nn.Sequential(#Sequential, 412 | nn.Sequential(#Sequential, 413 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 414 | nn.BatchNorm2d(1024), 415 | nn.ReLU(), 416 | nn.Conv2d(1024, 1024, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 417 | nn.BatchNorm2d(1024), 418 | nn.ReLU(), 419 | ), 420 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 421 | nn.BatchNorm2d(1024), 422 | ), 423 | Lambda(lambda x: x), #Identity, 424 | ), 425 | LambdaReduce(lambda x, y: x + y), #CAddTable, 426 | nn.ReLU(), 427 | ), 428 | nn.Sequential(#Sequential, 429 | LambdaMap(lambda x: x, #ConcatTable, 430 | nn.Sequential(#Sequential, 431 | nn.Sequential(#Sequential, 432 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 433 | nn.BatchNorm2d(1024), 434 | nn.ReLU(), 435 | nn.Conv2d(1024, 1024, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 436 | nn.BatchNorm2d(1024), 437 | nn.ReLU(), 438 | ), 439 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 440 | nn.BatchNorm2d(1024), 441 | ), 442 | Lambda(lambda x: x), #Identity, 443 | ), 444 | LambdaReduce(lambda x, y: x + y), #CAddTable, 445 | nn.ReLU(), 446 | ), 447 | nn.Sequential(#Sequential, 448 | LambdaMap(lambda x: x, #ConcatTable, 449 | nn.Sequential(#Sequential, 450 | nn.Sequential(#Sequential, 451 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 452 | nn.BatchNorm2d(1024), 453 | nn.ReLU(), 454 | nn.Conv2d(1024, 1024, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 455 | nn.BatchNorm2d(1024), 456 | nn.ReLU(), 457 | ), 458 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 459 | nn.BatchNorm2d(1024), 460 | ), 461 | Lambda(lambda x: x), #Identity, 462 | ), 463 | LambdaReduce(lambda x, y: x + y), #CAddTable, 464 | nn.ReLU(), 465 | ), 466 | nn.Sequential(#Sequential, 467 | LambdaMap(lambda x: x, #ConcatTable, 468 | nn.Sequential(#Sequential, 469 | nn.Sequential(#Sequential, 470 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 471 | nn.BatchNorm2d(1024), 472 | nn.ReLU(), 473 | nn.Conv2d(1024, 1024, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 474 | nn.BatchNorm2d(1024), 475 | nn.ReLU(), 476 | ), 477 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 478 | nn.BatchNorm2d(1024), 479 | ), 480 | Lambda(lambda x: x), #Identity, 481 | ), 482 | LambdaReduce(lambda x, y: x + y), #CAddTable, 483 | nn.ReLU(), 484 | ), 485 | nn.Sequential(#Sequential, 486 | LambdaMap(lambda x: x, #ConcatTable, 487 | nn.Sequential(#Sequential, 488 | nn.Sequential(#Sequential, 489 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 490 | nn.BatchNorm2d(1024), 491 | nn.ReLU(), 492 | nn.Conv2d(1024, 1024, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 493 | nn.BatchNorm2d(1024), 494 | nn.ReLU(), 495 | ), 496 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 497 | nn.BatchNorm2d(1024), 498 | ), 499 | Lambda(lambda x: x), #Identity, 500 | ), 501 | LambdaReduce(lambda x, y: x + y), #CAddTable, 502 | nn.ReLU(), 503 | ), 504 | nn.Sequential(#Sequential, 505 | LambdaMap(lambda x: x, #ConcatTable, 506 | nn.Sequential(#Sequential, 507 | nn.Sequential(#Sequential, 508 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 509 | nn.BatchNorm2d(1024), 510 | nn.ReLU(), 511 | nn.Conv2d(1024, 1024, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 512 | nn.BatchNorm2d(1024), 513 | nn.ReLU(), 514 | ), 515 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 516 | nn.BatchNorm2d(1024), 517 | ), 518 | Lambda(lambda x: x), #Identity, 519 | ), 520 | LambdaReduce(lambda x, y: x + y), #CAddTable, 521 | nn.ReLU(), 522 | ), 523 | nn.Sequential(#Sequential, 524 | LambdaMap(lambda x: x, #ConcatTable, 525 | nn.Sequential(#Sequential, 526 | nn.Sequential(#Sequential, 527 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 528 | nn.BatchNorm2d(1024), 529 | nn.ReLU(), 530 | nn.Conv2d(1024, 1024, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 531 | nn.BatchNorm2d(1024), 532 | nn.ReLU(), 533 | ), 534 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 535 | nn.BatchNorm2d(1024), 536 | ), 537 | Lambda(lambda x: x), #Identity, 538 | ), 539 | LambdaReduce(lambda x, y: x + y), #CAddTable, 540 | nn.ReLU(), 541 | ), 542 | nn.Sequential(#Sequential, 543 | LambdaMap(lambda x: x, #ConcatTable, 544 | nn.Sequential(#Sequential, 545 | nn.Sequential(#Sequential, 546 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 547 | nn.BatchNorm2d(1024), 548 | nn.ReLU(), 549 | nn.Conv2d(1024, 1024, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 550 | nn.BatchNorm2d(1024), 551 | nn.ReLU(), 552 | ), 553 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 554 | nn.BatchNorm2d(1024), 555 | ), 556 | Lambda(lambda x: x), #Identity, 557 | ), 558 | LambdaReduce(lambda x, y: x + y), #CAddTable, 559 | nn.ReLU(), 560 | ), 561 | nn.Sequential(#Sequential, 562 | LambdaMap(lambda x: x, #ConcatTable, 563 | nn.Sequential(#Sequential, 564 | nn.Sequential(#Sequential, 565 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 566 | nn.BatchNorm2d(1024), 567 | nn.ReLU(), 568 | nn.Conv2d(1024, 1024, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 569 | nn.BatchNorm2d(1024), 570 | nn.ReLU(), 571 | ), 572 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 573 | nn.BatchNorm2d(1024), 574 | ), 575 | Lambda(lambda x: x), #Identity, 576 | ), 577 | LambdaReduce(lambda x, y: x + y), #CAddTable, 578 | nn.ReLU(), 579 | ), 580 | nn.Sequential(#Sequential, 581 | LambdaMap(lambda x: x, #ConcatTable, 582 | nn.Sequential(#Sequential, 583 | nn.Sequential(#Sequential, 584 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 585 | nn.BatchNorm2d(1024), 586 | nn.ReLU(), 587 | nn.Conv2d(1024, 1024, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 588 | nn.BatchNorm2d(1024), 589 | nn.ReLU(), 590 | ), 591 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 592 | nn.BatchNorm2d(1024), 593 | ), 594 | Lambda(lambda x: x), #Identity, 595 | ), 596 | LambdaReduce(lambda x, y: x + y), #CAddTable, 597 | nn.ReLU(), 598 | ), 599 | nn.Sequential(#Sequential, 600 | LambdaMap(lambda x: x, #ConcatTable, 601 | nn.Sequential(#Sequential, 602 | nn.Sequential(#Sequential, 603 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 604 | nn.BatchNorm2d(1024), 605 | nn.ReLU(), 606 | nn.Conv2d(1024, 1024, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 607 | nn.BatchNorm2d(1024), 608 | nn.ReLU(), 609 | ), 610 | nn.Conv2d(1024, 1024, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 611 | nn.BatchNorm2d(1024), 612 | ), 613 | Lambda(lambda x: x), #Identity, 614 | ), 615 | LambdaReduce(lambda x, y: x + y), #CAddTable, 616 | nn.ReLU(), 617 | ), 618 | ), 619 | nn.Sequential(#Sequential, 620 | nn.Sequential(#Sequential, 621 | LambdaMap(lambda x: x, #ConcatTable, 622 | nn.Sequential(#Sequential, 623 | nn.Sequential(#Sequential, 624 | nn.Conv2d(1024, 2048, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 625 | nn.BatchNorm2d(2048), 626 | nn.ReLU(), 627 | nn.Conv2d(2048, 2048, (3, 3), (2, 2), (1, 1), 1, 64, bias = False), 628 | nn.BatchNorm2d(2048), 629 | nn.ReLU(), 630 | ), 631 | nn.Conv2d(2048, 2048, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 632 | nn.BatchNorm2d(2048), 633 | ), 634 | nn.Sequential(#Sequential, 635 | nn.Conv2d(1024, 2048, (1, 1), (2, 2), (0, 0), 1, 1, bias = False), 636 | nn.BatchNorm2d(2048), 637 | ), 638 | ), 639 | LambdaReduce(lambda x, y: x + y), #CAddTable, 640 | nn.ReLU(), 641 | ), 642 | nn.Sequential(#Sequential, 643 | LambdaMap(lambda x: x, #ConcatTable, 644 | nn.Sequential(#Sequential, 645 | nn.Sequential(#Sequential, 646 | nn.Conv2d(2048, 2048, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 647 | nn.BatchNorm2d(2048), 648 | nn.ReLU(), 649 | nn.Conv2d(2048, 2048, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 650 | nn.BatchNorm2d(2048), 651 | nn.ReLU(), 652 | ), 653 | nn.Conv2d(2048, 2048, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 654 | nn.BatchNorm2d(2048), 655 | ), 656 | Lambda(lambda x: x), #Identity, 657 | ), 658 | LambdaReduce(lambda x, y: x + y), #CAddTable, 659 | nn.ReLU(), 660 | ), 661 | nn.Sequential(#Sequential, 662 | LambdaMap(lambda x: x, #ConcatTable, 663 | nn.Sequential(#Sequential, 664 | nn.Sequential(#Sequential, 665 | nn.Conv2d(2048, 2048, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 666 | nn.BatchNorm2d(2048), 667 | nn.ReLU(), 668 | nn.Conv2d(2048, 2048, (3, 3), (1, 1), (1, 1), 1, 64, bias = False), 669 | nn.BatchNorm2d(2048), 670 | nn.ReLU(), 671 | ), 672 | nn.Conv2d(2048, 2048, (1, 1), (1, 1), (0, 0), 1, 1, bias = False), 673 | nn.BatchNorm2d(2048), 674 | ), 675 | Lambda(lambda x: x), #Identity, 676 | ), 677 | LambdaReduce(lambda x, y: x + y), #CAddTable, 678 | nn.ReLU(), 679 | ), 680 | ) 681 | ) -------------------------------------------------------------------------------- /tars/tars_data_loaders.py: -------------------------------------------------------------------------------- 1 | """ data loaders 2 | 3 | Given a input folder of train and valid, this should periodically generate batches of input images with labels 4 | 5 | Important Augmentations: https://github.com/pytorch/vision/blob/master/torchvision/transforms/transforms.py 6 | """ 7 | 8 | from __future__ import print_function, division 9 | 10 | import torch 11 | import torch.nn as nn 12 | import torch.optim as optim 13 | from torch.optim import lr_scheduler 14 | from torch.autograd import Variable 15 | import numpy as np 16 | import torchvision 17 | from torchvision import datasets, models, transforms 18 | import time 19 | import os 20 | from tars.utils import * 21 | 22 | def generate_data(data_dir, input_shape, name, batch_size=32): 23 | """ 24 | input_shape(scale, shape) 25 | """ 26 | if name in ["inceptionv4", "inceptionresnetv2", "inception_v3"]: 27 | scale = 360 28 | mean = [0.5, 0.5, 0.5] 29 | std = [0.5, 0.5, 0.5] 30 | 31 | elif name == "bninception": 32 | scale = 256 33 | mean = [104, 117, 128] 34 | std = [1, 1, 1] 35 | 36 | elif name == "vggm": 37 | scale = 256 38 | mean = [123.68, 116.779, 103.939] 39 | std = [1, 1, 1] 40 | 41 | elif name == "nasnetalarge": 42 | scale = 354 43 | mean = [0.5, 0.5, 0.5] 44 | std = [1, 1, 1] 45 | 46 | else: 47 | scale = 256 48 | mean = [0.485, 0.456, 0.406] 49 | std = [0.229, 0.224, 0.225] 50 | print("[Scale: {} , mean: {}, std: {}]".format(scale, mean, std)) 51 | if name == "bninception": 52 | print("[Using bninception Dataloader]") 53 | data_transforms = { 54 | 'train': transforms.Compose([ 55 | transforms.Resize(scale), 56 | transforms.RandomResizedCrop(input_shape), 57 | transforms.RandomHorizontalFlip(), 58 | transforms.RandomVerticalFlip(), 59 | transforms.RandomRotation(degrees=90), 60 | transforms.ToTensor(), 61 | ToSpaceBGR(True), 62 | ToRange255(True), 63 | transforms.Normalize(mean, std)]), 64 | 'val': transforms.Compose([ 65 | transforms.Resize(scale), 66 | transforms.CenterCrop(input_shape), 67 | transforms.ToTensor(), 68 | ToSpaceBGR(True), 69 | ToRange255(True), 70 | transforms.Normalize(mean, std)]),} 71 | else: 72 | data_transforms = { 73 | 'train': transforms.Compose([ 74 | transforms.Resize(scale), 75 | transforms.RandomResizedCrop(input_shape), 76 | transforms.RandomHorizontalFlip(), 77 | transforms.RandomVerticalFlip(), 78 | transforms.RandomRotation(degrees=90), 79 | transforms.ToTensor(), 80 | transforms.Normalize(mean, std)]), 81 | 'val': transforms.Compose([ 82 | transforms.Resize(scale), 83 | transforms.CenterCrop(input_shape), 84 | transforms.ToTensor(), 85 | transforms.Normalize(mean, std)]),} 86 | image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x), 87 | data_transforms[x]) for x in ['train', 'val']} 88 | dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=batch_size, 89 | shuffle=True, num_workers=4) for x in ['train', 'val']} 90 | dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'val']} 91 | class_names = image_datasets['train'].classes 92 | return dataloaders, dataset_sizes, class_names 93 | 94 | 95 | 96 | 97 | def generate_data_simple_agumentation(data_dir, input_shape, name, batch_size=32): 98 | """ 99 | input_shape(scale, shape) 100 | """ 101 | if name in ["inceptionv4", "inceptionresnetv2", "inception_v3"]: 102 | scale = 360 103 | mean = [0.5, 0.5, 0.5] 104 | std = [0.5, 0.5, 0.5] 105 | 106 | elif name == "bninception": 107 | scale = 256 108 | mean = [104, 117, 128] 109 | std = [1, 1, 1] 110 | 111 | elif name == "vggm": 112 | scale = 256 113 | mean = [123.68, 116.779, 103.939] 114 | std = [1, 1, 1] 115 | 116 | elif name == "nasnetalarge": 117 | scale = 354 118 | mean = [0.5, 0.5, 0.5] 119 | std = [1, 1, 1] 120 | 121 | else: 122 | scale = 256 123 | mean = [0.485, 0.456, 0.406] 124 | std = [0.229, 0.224, 0.225] 125 | print("[Scale: {} , mean: {}, std: {}]".format(scale, mean, std)) 126 | if name == "bninception": 127 | print("[Using bninception Dataloader]") 128 | data_transforms = { 129 | 'train': transforms.Compose([ 130 | transforms.Resize(scale), 131 | transforms.RandomResizedCrop(input_shape), 132 | transforms.RandomHorizontalFlip(), 133 | transforms.ToTensor(), 134 | ToSpaceBGR(True), 135 | ToRange255(True), 136 | transforms.Normalize(mean, std)]), 137 | 'val': transforms.Compose([ 138 | transforms.Resize(scale), 139 | transforms.CenterCrop(input_shape), 140 | transforms.ToTensor(), 141 | ToSpaceBGR(True), 142 | ToRange255(True), 143 | transforms.Normalize(mean, std)]),} 144 | else: 145 | data_transforms = { 146 | 'train': transforms.Compose([ 147 | transforms.Resize(scale), 148 | transforms.RandomResizedCrop(input_shape), 149 | transforms.RandomHorizontalFlip(), 150 | transforms.ToTensor(), 151 | transforms.Normalize(mean, std)]), 152 | 'val': transforms.Compose([ 153 | transforms.Resize(scale), 154 | transforms.CenterCrop(input_shape), 155 | transforms.ToTensor(), 156 | transforms.Normalize(mean, std)]),} 157 | image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x), 158 | data_transforms[x]) for x in ['train', 'val']} 159 | dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=batch_size, 160 | shuffle=True, num_workers=4) for x in ['train', 'val']} 161 | dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'val']} 162 | class_names = image_datasets['train'].classes 163 | return dataloaders, dataset_sizes, class_names 164 | 165 | 166 | def data_loader_predict(data_dir, input_shape, name): 167 | if name in ["inceptionv4", "inceptionresnetv2", "inception_v3"]: 168 | scale = 360 169 | mean = [0.5, 0.5, 0.5] 170 | std = [0.5, 0.5, 0.5] 171 | 172 | elif name == "bninception": 173 | scale = 256 174 | mean = [104, 117, 128] 175 | std = [1, 1, 1] 176 | 177 | elif name == "vggm": 178 | scale = 256 179 | mean = [123.68, 116.779, 103.939] 180 | std = [1, 1, 1] 181 | 182 | elif name == "nasnetalarge": 183 | scale = 354 184 | mean = [0.5, 0.5, 0.5] 185 | std = [1, 1, 1] 186 | 187 | else: 188 | scale = 256 189 | mean = [0.485, 0.456, 0.406] 190 | std = [0.229, 0.224, 0.225] 191 | print("[Scale: {} , mean: {}, std: {}]".format(scale, mean, std)) 192 | if name == "bninception": 193 | val = transforms.Compose([transforms.Scale(scale), 194 | transforms.TenCrop(input_shape), 195 | transforms.Lambda(lambda crops: torch.stack([transforms.ToTensor()(crop) for crop in crops])), 196 | transforms.Lambda(lambda bgr: torch.stack([ToSpaceBGR(True)(bgrformat) for bgrformat in bgr])), 197 | transforms.Lambda(lambda range255: torch.stack([ToRange255(True)(ranges) for ranges in range255])), 198 | transforms.Lambda(lambda normal: torch.stack([transforms.Normalize(mean, std)(normalize) for normalize in normal]))]) 199 | else: 200 | val = transforms.Compose([transforms.Scale(scale), 201 | transforms.TenCrop(input_shape), 202 | transforms.Lambda(lambda crops: torch.stack([transforms.ToTensor()(crop) for crop in crops])), 203 | transforms.Lambda(lambda normal: torch.stack([transforms.Normalize(mean, std)(normalize) for normalize in normal]))]) 204 | image_datasets = datasets.ImageFolder(data_dir, val) 205 | dataloaders = torch.utils.data.DataLoader(image_datasets, batch_size=1, 206 | shuffle=False, num_workers=1) 207 | return dataloaders, image_datasets 208 | -------------------------------------------------------------------------------- /tars/tars_model.py: -------------------------------------------------------------------------------- 1 | """ Models 2 | """ 3 | import torch 4 | import torchvision 5 | import torch.nn as nn 6 | import torch.nn.init as init 7 | 8 | import numpy as np 9 | 10 | from tars.nasnet import nasnetalarge 11 | from tars.resnext import resnext101_32x4d, resnext101_64x4d 12 | from tars.inceptionresnetv2 import inceptionresnetv2 13 | from tars.inceptionv4 import inceptionv4 14 | from tars.bninception import bninception 15 | 16 | """ 17 | resnet18, resnet34, resnet50, resnet101, resnet152 18 | densenet121, densenet161, densenet169, densenet201 19 | squeezenet1_0, squeezenet1_1 20 | alexnet, 21 | inception_v3, 22 | vgg11, vgg13, vgg16, vgg19 23 | vgg11_bn, vgg13_bn, vgg16_bn, vgg19_bn 24 | 25 | 26 | Its quite messed up or I messed it a bit. 27 | 28 | - If you want to freeze the layers by its name 29 | 30 | for name, params in model_conv.named_parameters(): 31 | if name =! "something": 32 | params.requires_grad=False 33 | 34 | - If you want to freeze the first few layers 35 | 36 | ``` 37 | model_ft = models.resnet50(pretrained=True) 38 | ct = 0 39 | for child in model_ft.children(): 40 | ct += 1 41 | if ct < 7: 42 | for param in child.parameters(): 43 | param.requires_grad = False 44 | ``` 45 | 46 | ct = [] 47 | for name, child in model_conv.named_children(): 48 | if "layer1" in ct: 49 | execute this 50 | ct.append(name) 51 | 52 | 53 | """ 54 | 55 | def all_pretrained_models(n_class, use_gpu=True, freeze_layers=False, freeze_initial_layers=False, name="resnet18", pretrained=True): 56 | if pretrained: 57 | weights = "imagenet" 58 | else: 59 | weights = False 60 | if name == "alexnet": 61 | print("[Building alexnet]") 62 | model_conv = torchvision.models.alexnet(pretrained=weights) 63 | elif name == "inception_v3": 64 | print("[Building inception_v3]") 65 | model_conv = torchvision.models.inception_v3(pretrained=weights) 66 | elif name == "inceptionresnetv2": 67 | print("[Building InceptionResnetV2]") 68 | model_conv = inceptionresnetv2(num_classes=1000, pretrained=weights) 69 | elif name == "inceptionv4": 70 | print("[Building Inceptionv4]") 71 | model_conv = inceptionv4(num_classes=1000, pretrained=weights) 72 | elif name == "bninception": 73 | print("[Building bninception]") 74 | model_conv = bninception(num_classes=1000, pretrained=weights) 75 | elif name == "resnet18": 76 | print("[Building resnet18]") 77 | model_conv = torchvision.models.resnet18(pretrained=weights) 78 | elif name == "resnet34": 79 | print("[Building resnet34]") 80 | model_conv = torchvision.models.resnet34(pretrained=weights) 81 | elif name == "resnet50": 82 | print("[Building resnet50]") 83 | model_conv = torchvision.models.resnet50(pretrained=weights) 84 | elif name == "resnet101": 85 | print("[Building resnet101]") 86 | model_conv = torchvision.models.resnet101(pretrained=weights) 87 | elif name == "resnet152": 88 | print("[Building resnet152]") 89 | model_conv = torchvision.models.resnet152(pretrained=weights) 90 | elif name == "densenet121": 91 | print("[Building densenet121]") 92 | model_conv = torchvision.models.densenet121(pretrained=weights) 93 | elif name == "densenet169": 94 | print("[Building densenet169]") 95 | model_conv = torchvision.models.densenet169(pretrained=weights) 96 | elif name == "densenet201": 97 | print("[Building densenet201]") 98 | model_conv = torchvision.models.densenet201(pretrained=weights) 99 | elif name == "squeezenet1_0": 100 | print("[Building squeezenet1_0]") 101 | model_conv = torchvision.models.squeezenet1_0(pretrained=weights) 102 | elif name == "squeezenet1_1": 103 | print("[Building squeezenet1_1]") 104 | model_conv = torchvision.models.squeezenet1_1(pretrained=weights) 105 | elif name == "vgg11": 106 | print("[Building vgg11]") 107 | model_conv = torchvision.models.vgg11(pretrained=weights) 108 | elif name == "vgg13": 109 | print("[Building vgg13]") 110 | model_conv = torchvision.models.vgg13(pretrained=weights) 111 | elif name == "vgg16": 112 | print("[Building vgg16]") 113 | model_conv = torchvision.models.vgg16(pretrained=weights) 114 | elif name == "vgg19": 115 | print("[Building vgg19]") 116 | model_conv = torchvision.models.vgg19(pretrained=weights) 117 | elif name == "vgg11_bn": 118 | print("[Building vgg11_bn]") 119 | model_conv = torchvision.models.vgg11_bn(pretrained=weights) 120 | elif name == "vgg13_bn": 121 | print("[Building vgg13_bn]") 122 | model_conv = torchvision.models.vgg13_bn(pretrained=weights) 123 | elif name == "vgg16_bn": 124 | print("[Building vgg16_bn]") 125 | model_conv = torchvision.models.vgg16_bn(pretrained=weights) 126 | elif name == "vgg19_bn": 127 | print("[Building vgg19_bn]") 128 | model_conv = torchvision.models.vgg19_bn(pretrained=weights) 129 | elif name == "nasnetalarge": 130 | print("[Building nasnetalarge]") 131 | model_conv = nasnetalarge(num_classes=1000, pretrained=weights) 132 | elif name == "resnext101_64x4d": 133 | print("[Building resnext101_64x4d]") 134 | model_conv = resnext101_64x4d(pretrained=weights) 135 | elif name == "resnext101_32x4d": 136 | print("[Building resnext101_32x4d]") 137 | model_conv = resnext101_32x4d(pretrained=weights) 138 | else: 139 | raise ValueError 140 | 141 | if not pretrained: 142 | print("[Initializing the weights randomly........]") 143 | if "densenet" in name: 144 | num_ftrs = model_conv.classifier.in_features 145 | model_conv.classifier = nn.Linear(num_ftrs, n_class) 146 | elif "squeezenet" in name: 147 | in_ftrs = model_conv.classifier[1].in_channels 148 | out_ftrs = model_conv.classifier[1].out_channels 149 | features = list(model_conv.classifier.children()) 150 | features[1] = nn.Conv2d(in_ftrs, n_class, kernel_size=(2, 2), stride=(1, 1)) 151 | features[3] = nn.AvgPool2d(12, stride=1) 152 | model_conv.classifier = nn.Sequential(*features) 153 | model_conv.num_classes = n_class 154 | elif "vgg" in name or "alexnet" in name: 155 | print("[Building VGG or Alexnet classifier]") 156 | num_ftrs = model_conv.classifier[6].in_features 157 | features = list(model_conv.classifier.children())[:-1] 158 | features.extend([nn.Linear(num_ftrs, n_class)]) 159 | model_conv.classifier = nn.Sequential(*features) 160 | elif "vgg" in name or "alexnet" in name: 161 | print("[Building VGG or Alexnet classifier]") 162 | num_ftrs = model_conv.classifier[6].in_features 163 | features = list(model_conv.classifier.children())[:-1] 164 | features.extend([nn.Linear(num_ftrs, n_class)]) 165 | model_conv.classifier = nn.Sequential(*features) 166 | elif "resnext" in name or "inceptionv4" in name or "inceptionresnetv2" in name or "bninception" in name or "nasnetalarge" in name: 167 | print("[Building {}]".format(name)) 168 | num_ftrs = model_conv.last_linear.in_features 169 | model_conv.last_linear = nn.Linear(num_ftrs, n_class) 170 | else: 171 | print("[Building inception_v3 or Resnet]") 172 | num_ftrs = model_conv.fc.in_features 173 | model_conv.fc = nn.Linear(num_ftrs, n_class) 174 | 175 | 176 | else: 177 | if freeze_layers: 178 | for i, param in model_conv.named_parameters(): 179 | param.requires_grad = False 180 | else: 181 | print("[All layers will be trained]") 182 | # Parameters of newly constructed modules have requires_grad=True by default 183 | if "densenet" in name: 184 | print("[Building Densenet]") 185 | num_ftrs = model_conv.classifier.in_features 186 | model_conv.classifier = nn.Linear(num_ftrs, n_class) 187 | if freeze_initial_layers: 188 | print("[Densenet: Freeezing layers only till denseblock1 including]") 189 | ct = [] 190 | for name, child in model_conv.features.named_children(): 191 | if "denseblock1" in ct: #freeze all layers from layer1 inclusive 192 | for params in child.parameters(): 193 | params.requires_grad = True 194 | ct.append(name) 195 | elif "squeezenet" in name: 196 | print("[Building Squeezenet]") 197 | in_ftrs = model_conv.classifier[1].in_channels 198 | out_ftrs = model_conv.classifier[1].out_channels 199 | features = list(model_conv.classifier.children()) 200 | features[1] = nn.Conv2d(in_ftrs, n_class, kernel_size=(2, 2), stride=(1, 1)) 201 | features[3] = nn.AvgPool2d(12, stride=1) 202 | model_conv.classifier = nn.Sequential(*features) 203 | model_conv.num_classes = n_class 204 | if freeze_layers: 205 | ct = [] 206 | print("[Squeezenet: Freeezing layers only till denseblock1 including]") 207 | for name, child in model_conv.features.named_children(): 208 | if "3" in ct: 209 | for params in child.parameters(): 210 | params.requires_grad = True 211 | ct.append(name) 212 | 213 | elif "vgg" in name or "alexnet" in name: 214 | print("[Building VGG or Alexnet classifier]") 215 | num_ftrs = model_conv.classifier[6].in_features 216 | features = list(model_conv.classifier.children())[:-1] 217 | features.extend([nn.Linear(num_ftrs, n_class)]) 218 | model_conv.classifier = nn.Sequential(*features) 219 | if freeze_layers: 220 | ct = [] 221 | print("[Alex or VGG: Freeezing layers only till denseblock1 including]") 222 | for name, child in model_conv.features.named_children(): 223 | if "5" in ct: 224 | for params in child.parameters(): 225 | params.requires_grad = True 226 | ct.append(name) 227 | 228 | elif "nasnetalarge" in name: 229 | print("[Building nasnetalarge]") 230 | num_ftrs = model_conv.last_linear.in_features 231 | model_conv.last_linear = nn.Linear(num_ftrs, n_class) 232 | if freeze_layers: 233 | ct=[] 234 | print("[nasnetalarge: Freezing layers till cell_5]") 235 | for name, child in model_conv.named_children(): 236 | if "cell_5" in ct: 237 | for params in child.parameters(): 238 | params.requires_grad=True 239 | ct.append(name) 240 | 241 | elif "resnext" in name: 242 | print("[Building resnext]") 243 | num_ftrs = model_conv.last_linear.in_features 244 | model_conv.last_linear = nn.Linear(num_ftrs, n_class) 245 | if freeze_layers: 246 | ct = [] 247 | for name, child in model_conv.features.named_children(): 248 | if "4" in ct: 249 | for param in child.parameters(): 250 | param.requires_grad = True 251 | ct.append(name) 252 | 253 | elif "inceptionresnetv2" in name: 254 | print("[Building inceptionresnetv2]") 255 | num_ftrs = model_conv.last_linear.in_features 256 | model_conv.last_linear = nn.Linear(num_ftrs, n_class) 257 | if freeze_layers: 258 | ct = [] 259 | print("[inceptionresnetv2: Freezing layers till maxpool_3a]") 260 | for name, child in model_conv.named_children(): 261 | if "maxpool_3a" in ct: 262 | for params in child.parameters(): 263 | params.requires_grad=True 264 | ct.append(name) 265 | 266 | elif "inceptionv4" in name: 267 | print("[Building inceptionv4]") 268 | num_ftrs = model_conv.last_linear.in_features 269 | model_conv.last_linear = nn.Linear(num_ftrs, n_class) 270 | if freeze_layers: 271 | ct = [] 272 | for name, child in model_conv.features.named_children(): 273 | if "4" in ct: 274 | for param in child.parameters(): 275 | param.requires_grad = True 276 | ct.append(name) 277 | 278 | elif "bninception" in name: 279 | print("[Building bninception]") 280 | num_ftrs = model_conv.last_linear.in_features 281 | model_conv.last_linear = nn.Linear(num_ftrs, n_class) 282 | if freeze_layers: 283 | ct = [] 284 | print("[bninception: Freezing layers till pool2_3x3_s2]") 285 | for name, child in model_conv.named_children(): 286 | if "pool2_3x3_s2" in ct: 287 | for params in child.parameters(): 288 | params.requires_grad=True 289 | ct.append(name) 290 | else: 291 | print("[Building inception_v3 or Resnet]") 292 | num_ftrs = model_conv.fc.in_features 293 | model_conv.fc = nn.Linear(num_ftrs, n_class) 294 | 295 | if freeze_initial_layers: 296 | if "resnet" in name: 297 | print("[Resnet: Freezing layers only till layer1 including]") 298 | ct = [] 299 | for name, child in model_conv.named_children(): 300 | if "layer1" in ct: 301 | for params in child.parameters(): 302 | params.requires_grad = True 303 | ct.append(name) 304 | else: 305 | print("[Inception: Freezing layers only till layer1 including]") 306 | ct = [] 307 | for name, child in model_conv.named_children(): 308 | if "Conv2d_4a_3x3" in ct: 309 | for params in child.parameters(): 310 | params.requires_grad = True 311 | ct.append(name) 312 | 313 | if use_gpu: 314 | model_conv = model_conv.cuda() 315 | else: 316 | model_conv = model_conv 317 | return model_conv 318 | -------------------------------------------------------------------------------- /tars/tars_training.py: -------------------------------------------------------------------------------- 1 | """ training functions 2 | """ 3 | 4 | import torch 5 | import torch.nn as nn 6 | import torch.optim as optim 7 | from torch.optim import lr_scheduler 8 | from torch.autograd import Variable 9 | import torchvision 10 | from torchvision import datasets, models, transforms 11 | 12 | import numpy as np 13 | import time 14 | import os 15 | from tqdm import tqdm 16 | from sklearn.metrics import accuracy_score 17 | import pandas as pd 18 | 19 | from tars.utils import * 20 | from tars.tars_data_loaders import * 21 | 22 | def train_model(model, dataloaders, dataset_sizes, criterion, optimizer, scheduler, use_gpu, num_epochs=25, mixup = False, alpha = 0.1): 23 | print("MIXUP".format(mixup)) 24 | since = time.time() 25 | 26 | best_model_wts = model.state_dict() 27 | best_acc = 0.0 28 | 29 | for epoch in range(num_epochs): 30 | print('Epoch {}/{}'.format(epoch, num_epochs - 1)) 31 | print('-' * 10) 32 | 33 | # Each epoch has a training and validation phase 34 | for phase in ['train', 'val']: 35 | if phase == 'train': 36 | scheduler.step() 37 | model.train(True) # Set model to training mode 38 | else: 39 | model.train(False) # Set model to evaluate mode 40 | 41 | running_loss = 0.0 42 | running_corrects = 0 43 | 44 | # Iterate over data. 45 | for data in tqdm(dataloaders[phase]): 46 | # get the inputs 47 | inputs, labels = data 48 | 49 | #augementation using mixup 50 | if phase == 'train' and mixup: 51 | inputs = mixup_batch(inputs, alpha) 52 | # wrap them in Variable 53 | if use_gpu: 54 | inputs = Variable(inputs.cuda()) 55 | labels = Variable(labels.cuda()) 56 | else: 57 | inputs, labels = Variable(inputs), Variable(labels) 58 | 59 | # zero the parameter gradients 60 | optimizer.zero_grad() 61 | 62 | # forward 63 | outputs = model(inputs) 64 | if type(outputs) == tuple: 65 | outputs, _ = outputs 66 | _, preds = torch.max(outputs.data, 1) 67 | loss = criterion(outputs, labels) 68 | 69 | # backward + optimize only if in training phase 70 | if phase == 'train': 71 | loss.backward() 72 | optimizer.step() 73 | 74 | # statistics 75 | running_loss += loss.data[0] 76 | running_corrects += torch.sum(preds == labels.data) 77 | 78 | epoch_loss = running_loss / dataset_sizes[phase] 79 | epoch_acc = running_corrects / dataset_sizes[phase] 80 | 81 | print('{} Loss: {:.4f} Acc: {:.4f}'.format( 82 | phase, epoch_loss, epoch_acc)) 83 | 84 | # deep copy the model 85 | if phase == 'val' and epoch_acc > best_acc: 86 | best_acc = epoch_acc 87 | best_model_wts = model.state_dict() 88 | 89 | print() 90 | 91 | time_elapsed = time.time() - since 92 | print('Training complete in {:.0f}m {:.0f}s'.format( 93 | time_elapsed // 60, time_elapsed % 60)) 94 | print('Best val Acc: {:4f}'.format(best_acc)) 95 | 96 | # load best model weights 97 | model.load_state_dict(best_model_wts) 98 | return model 99 | 100 | def model_evaluation(mode, model_conv, input_data_loc, input_shape, use_gpu, name): 101 | """A function which evaluates the model and outputs the csv of predictions of validation and train. 102 | 103 | mode: 104 | model_conv: 105 | input_data_loc: 106 | input_shape: 107 | use_gpu: 108 | 109 | Output: 110 | 1) Prints score of train and validation (Write it to a log file) 111 | """ 112 | print("[Evaluating the data in {}]".format(mode)) 113 | data_loc = os.path.join(input_data_loc, mode) 114 | 115 | print("[Building dataloaders]") 116 | dataloaders, image_datasets = data_loader_predict(data_loc, input_shape, name) 117 | class_to_idx = image_datasets.class_to_idx 118 | imgs = [i[0] for i in image_datasets.imgs] 119 | print("total number of {} images: {}".format(mode, len(imgs))) 120 | original, predicted, probs = [], [], [] 121 | for img, label in dataloaders: 122 | if use_gpu: 123 | inputs = Variable(img.cuda()) 124 | else: 125 | inputs = Variable(img) 126 | bs, ncrops, c, h, w = inputs.data.size() 127 | output = model_conv(inputs.view(-1, c, h, w)) # fuse batch size and ncrops 128 | if type(output) == tuple: 129 | output, _ = output 130 | else: 131 | output = output 132 | outputs = torch.stack([nn.Softmax(dim=0)(i) for i in output]) 133 | outputs = outputs.mean(0) 134 | _, preds = torch.max(outputs, 0) 135 | probs.append(outputs.data.cpu().numpy()) 136 | original.extend(label.numpy()) 137 | predicted.extend(preds.data.cpu().numpy()) 138 | print("Accuracy_score {} : {} ".format(mode, accuracy_score(original, predicted))) 139 | frame = pd.DataFrame(probs) 140 | frame.columns = ["class_{}".format(i) for i in frame.columns] 141 | frame["img_loc"] = imgs 142 | frame["original"] = original 143 | frame["predicted"] = predicted 144 | return frame, class_to_idx 145 | 146 | 147 | def model_evaluation_test(mode, model_conv, test_input_data_loc, input_shape, use_gpu, name): 148 | dataloaders, image_datasets = data_loader_predict(test_input_data_loc, input_shape, name) 149 | imgs =[i[0] for i in image_datasets.imgs] 150 | print("total number of {} images: {}".format(mode, len(imgs))) 151 | predicted, probs = [], [] 152 | for img, label in dataloaders: 153 | if use_gpu: 154 | inputs = Variable(img.cuda()) 155 | else: 156 | inputs = Variable(img) 157 | bs, ncrops, c, h, w = inputs.data.size() 158 | output = model_conv(inputs.view(-1, c, h, w)) # fuse batch size and ncrops 159 | if type(output) == tuple: 160 | output, _ = output 161 | else: 162 | output = output 163 | outputs = torch.stack([nn.Softmax(dim=0)(i) for i in output]) 164 | outputs = outputs.mean(0) 165 | _, preds = torch.max(outputs, 0) 166 | probs.append(outputs.data.cpu().numpy()) 167 | predicted.extend(preds.data.cpu().numpy()) 168 | frame = pd.DataFrame(probs) 169 | frame.columns = ["class_{}".format(i) for i in frame.columns] 170 | frame["img_loc"] = imgs 171 | frame["predicted"] = predicted 172 | return frame 173 | -------------------------------------------------------------------------------- /tars/utils.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | from torch.nn import functional as Func 4 | import torchvision.transforms as transforms 5 | from torch.autograd import Variable 6 | from PIL import Image 7 | import numpy as np 8 | 9 | class ToSpaceBGR(object): 10 | 11 | def __init__(self, is_bgr): 12 | self.is_bgr = is_bgr 13 | 14 | def __call__(self, tensor): 15 | if self.is_bgr: 16 | new_tensor = tensor.clone() 17 | new_tensor[0] = tensor[2] 18 | new_tensor[2] = tensor[0] 19 | tensor = new_tensor 20 | return tensor 21 | 22 | 23 | class ToRange255(object): 24 | 25 | def __init__(self, is_255): 26 | self.is_255 = is_255 27 | 28 | def __call__(self, tensor): 29 | if self.is_255: 30 | tensor.mul_(255) 31 | return tensor 32 | 33 | def mixup_batch(inp_batch, alpha): 34 | """ 35 | Applies mixup augementation to a batch 36 | :param input_batch: tensor with batchsize as first dim 37 | :param alpha: lamda drawn from beta(alpha+1, alpha) 38 | """ 39 | inp_clone = inp_batch.clone() 40 | #getting batch size 41 | batchsize = inp_batch.size()[0] 42 | 43 | #permute a clone 44 | perm = np.random.permutation(batchsize) 45 | for i in range(batchsize): 46 | inp_clone[i] = inp_batch[perm[i]] 47 | #generating different lambda for each sample 48 | #Refernced from http://www.inference.vc/mixup-data-dependent-data-augmentation/ 49 | lam = torch.Tensor(np.random.beta(alpha+1, alpha, batchsize)) 50 | lam = lam.view(-1,1,1,1) 51 | inp_mixup = lam * inp_batch + (1- lam) * inp_clone 52 | return inp_mixup 53 | 54 | def onehot(t, num_classes): 55 | """ 56 | convert index tensor into onehot tensor 57 | :param t: index tensor 58 | :param num_classes: number of classes 59 | """ 60 | assert isinstance(t, torch.LongTensor) 61 | return torch.zeros(t.size()[0], num_classes).scatter_(1, t.view(-1, 1), 1) 62 | 63 | 64 | def naive_cross_entropy_loss(output, target, size_average=True): 65 | """ 66 | was used in mixup 67 | in PyTorch's cross entropy, targets are expected to be labels 68 | so to predict probabilities this loss is needed 69 | suppose q is the target and p is the input 70 | loss(p, q) = -\sum_i q_i \log p_i 71 | """ 72 | if type(output) == tuple: 73 | output, _ = output 74 | if output.size() != target.size(): 75 | print(output.size(), target.size()) 76 | raise Exception("model not working") 77 | assert isinstance(output, Variable) and isinstance(target, Variable) 78 | output =-1 * torch.log(Func.softmax(output).clamp(1e-5, 1)) 79 | # input = input - torch.log(torch.sum(torch.exp(input), dim=1)).view(-1, 1) 80 | loss = torch.sum(output * target) 81 | 82 | return loss / output.size()[0] if size_average else loss 83 | -------------------------------------------------------------------------------- /tars/vggm.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | from torch.autograd import Variable 4 | #from torch.legacy import nn as nnl 5 | import torch.utils.model_zoo as model_zoo 6 | 7 | __all__ = ['vggm'] 8 | 9 | pretrained_settings = { 10 | 'vggm': { 11 | 'imagenet': { 12 | 'url': 'http://data.lip6.fr/cadene/pretrainedmodels/vggm-786f2434.pth', 13 | 'input_space': 'BGR', 14 | 'input_size': [3, 221, 221], 15 | 'input_range': [0, 255], 16 | 'mean': [123.68, 116.779, 103.939], 17 | 'std': [1, 1, 1], 18 | 'num_classes': 1000 19 | } 20 | } 21 | } 22 | 23 | class SpatialCrossMapLRN(nn.Module): 24 | def __init__(self, local_size=1, alpha=1.0, beta=0.75, k=1, ACROSS_CHANNELS=True): 25 | super(SpatialCrossMapLRN, self).__init__() 26 | self.ACROSS_CHANNELS = ACROSS_CHANNELS 27 | if ACROSS_CHANNELS: 28 | self.average=nn.AvgPool3d(kernel_size=(local_size, 1, 1), 29 | stride=1, 30 | padding=(int((local_size-1.0)/2), 0, 0)) 31 | else: 32 | self.average=nn.AvgPool2d(kernel_size=local_size, 33 | stride=1, 34 | padding=int((local_size-1.0)/2)) 35 | self.alpha = alpha 36 | self.beta = beta 37 | self.k = k 38 | 39 | def forward(self, x): 40 | if self.ACROSS_CHANNELS: 41 | div = x.pow(2).unsqueeze(1) 42 | div = self.average(div).squeeze(1) 43 | div = div.mul(self.alpha).add(self.k).pow(self.beta) 44 | else: 45 | div = x.pow(2) 46 | div = self.average(div) 47 | div = div.mul(self.alpha).add(self.k).pow(self.beta) 48 | x = x.div(div) 49 | return x 50 | 51 | class LambdaBase(nn.Sequential): 52 | def __init__(self, fn, *args): 53 | super(LambdaBase, self).__init__(*args) 54 | self.lambda_func = fn 55 | 56 | def forward_prepare(self, input): 57 | output = [] 58 | for module in self._modules.values(): 59 | output.append(module(input)) 60 | return output if output else input 61 | 62 | class Lambda(LambdaBase): 63 | def forward(self, input): 64 | return self.lambda_func(self.forward_prepare(input)) 65 | 66 | class VGGM(nn.Module): 67 | 68 | def __init__(self, num_classes=1000): 69 | super(VGGM, self).__init__() 70 | self.num_classes = num_classes 71 | self.features = nn.Sequential( 72 | nn.Conv2d(3,96,(7, 7),(2, 2)), 73 | nn.ReLU(), 74 | SpatialCrossMapLRN(5, 0.0005, 0.75, 2), 75 | nn.MaxPool2d((3, 3),(2, 2),(0, 0),ceil_mode=True), 76 | nn.Conv2d(96,256,(5, 5),(2, 2),(1, 1)), 77 | nn.ReLU(), 78 | SpatialCrossMapLRN(5, 0.0005, 0.75, 2), 79 | nn.MaxPool2d((3, 3),(2, 2),(0, 0),ceil_mode=True), 80 | nn.Conv2d(256,512,(3, 3),(1, 1),(1, 1)), 81 | nn.ReLU(), 82 | nn.Conv2d(512,512,(3, 3),(1, 1),(1, 1)), 83 | nn.ReLU(), 84 | nn.Conv2d(512,512,(3, 3),(1, 1),(1, 1)), 85 | nn.ReLU(), 86 | nn.MaxPool2d((3, 3),(2, 2),(0, 0),ceil_mode=True) 87 | ) 88 | self.classif = nn.Sequential( 89 | nn.Linear(18432,4096), 90 | nn.ReLU(), 91 | nn.Dropout(0.5), 92 | nn.Linear(4096,4096), 93 | nn.ReLU(), 94 | nn.Dropout(0.5), 95 | nn.Linear(4096,num_classes) 96 | ) 97 | 98 | def forward(self, x): 99 | x = self.features(x) 100 | x = x.view(x.size(0), -1) 101 | x = self.classif(x) 102 | return x 103 | 104 | def vggm(num_classes=1000, pretrained='imagenet'): 105 | if pretrained: 106 | settings = pretrained_settings['vggm'][pretrained] 107 | assert num_classes == settings['num_classes'], \ 108 | "num_classes should be {}, but is {}".format(settings['num_classes'], num_classes) 109 | 110 | model = VGGM(num_classes=1000) 111 | model.load_state_dict(model_zoo.load_url(settings['url'])) 112 | 113 | model.input_space = settings['input_space'] 114 | model.input_size = settings['input_size'] 115 | model.input_range = settings['input_range'] 116 | model.mean = settings['mean'] 117 | model.std = settings['std'] 118 | else: 119 | model = VGGM(num_classes=num_classes) 120 | return model -------------------------------------------------------------------------------- /tars_ensemble.py: -------------------------------------------------------------------------------- 1 | """ This repo will ensemble all the models in different ways 2 | 3 | We will be using the following ensembling strategies: 4 | - Select the model outputs you want to use. 5 | - Take Voting and choose the class with most number of Voting 6 | """ 7 | 8 | import pandas as pd 9 | 10 | 11 | models_used = ["resnet18", "resnet34", "resnet50", "resnet101", "resnet152", "densenet121", "densenet169", "densenet201", 12 | "squeezenet1_0", "squeezenet1_1", "inception_v3", "vgg11", "vgg13", "vgg16", "vgg19", "vgg11_bn", "vgg13_bn", "vgg16_bn", "vgg19_bn", "alexnet"] 13 | models_loc = ["submission/"+i+"_False_freeze.csv" for i in models_used] 14 | 15 | models_combined=pd.DataFrame() 16 | cols = ["file", "species"] 17 | for i in models_loc: 18 | f = pd.read_csv(i) 19 | f["file"] = f["img_loc"].apply(lambda x:x.rsplit("/")[-1]) 20 | f = f[["file", "label"]] 21 | models_combined["file"] = f["file"].values 22 | models_combined[i.rsplit("/")[-1].rsplit(".")[0]] = f["label"].values 23 | 24 | print(models_combined.shape) 25 | print(models_combined.head()) 26 | models_combined.to_csv("submissions/ensemble1_intermediate.csv", index=False) 27 | cols = list(models_combined.columns) 28 | cols.pop(0) 29 | models_combined["species"] = models_combined[cols].mode(axis=1) 30 | models_combined[["file", "species"]].to_csv("submissions/ensemble.csv", index=False) 31 | -------------------------------------------------------------------------------- /tars_mixup_train.py: -------------------------------------------------------------------------------- 1 | """ 2 | Some of the links which I found useful. 3 | https://discuss.pytorch.org/t/freeze-the-learnable-parameters-of-resnet-and-attach-it-to-a-new-network/949/9 4 | """ 5 | 6 | import torch 7 | import torch.nn as nn 8 | import torch.optim as optim 9 | from torch.optim import lr_scheduler 10 | import numpy as np 11 | import time 12 | import os 13 | import argparse 14 | 15 | from tars.tars_data_loaders import * 16 | from tars.tars_training import * 17 | from tars.tars_model import * 18 | 19 | parser = argparse.ArgumentParser(description='Training a pytorch model to classify different plants') 20 | parser.add_argument('-idl', '--input_data_loc', help='', default='data/training_data') 21 | parser.add_argument('-mo', '--model_name', default="resnet18") 22 | parser.add_argument('-f', '--freeze_layers', default=True, action='store_false', help='Bool type') 23 | parser.add_argument('-fi', '--freeze_initial_layers', default=True, action='store_false', help='Bool type') 24 | parser.add_argument('-ep', '--epochs', default=100, type=int) 25 | parser.add_argument('-b', '--batch_size', default=64, type=int) 26 | parser.add_argument('-is', '--input_shape', default=224, type=int) 27 | parser.add_argument('-sl', '--save_loc', default="models_mixup/" ) 28 | parser.add_argument("-g", '--use_gpu', default=True, action='store_false', help='Bool type gpu') 29 | parser.add_argument("-p", '--use_parallel', default=True, action='store_false', help='Bool type to use_parallel') 30 | parser.add_argument("-pt", '--pretrained', default=True, action='store_false', help='Weather to use pretrained model or not') 31 | parser.add_argument('-ml', '--mixup_lambda', default=0.2, type=float) 32 | parser.add_argument('-a', '--alpha', default=0.5, type=float) 33 | 34 | args = parser.parse_args() 35 | 36 | if not os.path.exists(args.save_loc): 37 | os.makedirs(args.save_loc) 38 | 39 | dataloaders, dataset_sizes, class_names = generate_data_simple_agumentation(args.input_data_loc, args.input_shape, args.model_name, batch_size=args.batch_size) 40 | print(class_names) 41 | 42 | print("[Load the model...]") 43 | # Parameters of newly constructed modules have requires_grad=True by default 44 | print("Loading model using class: {}, use_gpu: {}, freeze_layers: {}, freeze_initial_layers: {}, name_of_model: {}, pretrained: {}".format(len(class_names), args.use_gpu, args.freeze_layers, args.freeze_initial_layers, args.model_name, args.pretrained)) 45 | model_conv = all_pretrained_models(len(class_names), use_gpu=args.use_gpu, freeze_layers=args.freeze_layers, freeze_initial_layers= args.freeze_initial_layers, name=args.model_name, pretrained=args.pretrained) 46 | if args.use_parallel: 47 | print("[Using all the available GPUs]") 48 | model_conv = nn.DataParallel(model_conv, device_ids=[0, 1]) 49 | 50 | print("[Using CrossEntropyLoss...]") 51 | criterion = nn.CrossEntropyLoss() 52 | 53 | print("[Using small learning rate with momentum...]") 54 | optimizer_conv = optim.SGD(list(filter(lambda p: p.requires_grad, model_conv.parameters())), lr=0.1, momentum=0.9) 55 | 56 | print("[Creating Learning rate scheduler...]") 57 | exp_lr_scheduler = lr_scheduler.StepLR(optimizer_conv, step_size=30, gamma=0.1) 58 | 59 | print("[Training the model begun ....]") 60 | model_ft = train_model_mixup(model_conv, dataloaders, dataset_sizes, class_names, criterion, optimizer_conv, exp_lr_scheduler, args.use_gpu, 61 | args.epochs, args.mixup_lambda, args.alpha) 62 | 63 | print("[Save the best model]") 64 | model_save_loc = args.save_loc+args.model_name+"_"+str(args.freeze_layers)+"_freeze"+"_"+str(args.freeze_initial_layers)+"_freeze_initial_layer"+".pth" 65 | torch.save(model_ft.state_dict(), model_save_loc) 66 | -------------------------------------------------------------------------------- /tars_predict.py: -------------------------------------------------------------------------------- 1 | """ Given the pre-trained model and model_weights this will predict the output for the code 2 | 3 | python tars_predict.py -idl data/training_data -ll models/resnet18_True_freeze.pth -mo "resnet18" -nc 12 -is 224 4 | """ 5 | 6 | import torch 7 | import torchvision.transforms as transforms 8 | import torchvision.datasets as datasets 9 | from torch.autograd import Variable 10 | 11 | import pandas as pd 12 | from collections import OrderedDict 13 | 14 | import os 15 | import argparse 16 | 17 | from tars.tars_data_loaders import * 18 | from tars.tars_training import * 19 | from tars.tars_model import * 20 | 21 | parser = argparse.ArgumentParser(description='Inference for a trained PyTorch Model') 22 | parser.add_argument('-idl', '--input_data_loc', help='', default='data/training_data') 23 | parser.add_argument('-is', '--input_shape', default=224, type=int) 24 | parser.add_argument('-ll', '--load_loc', default="models/resnet18_True_freeze.pth" ) 25 | parser.add_argument('-mo', '--model_name', default="resnet18") 26 | parser.add_argument('-sl', '--save_loc', default="submission/") 27 | parser.add_argument("-g", '--use_gpu', default=True, action='store_false', help='Bool type gpu') 28 | parser.add_argument("-gi", '--used_dataparallel', default=True, action='store_false', help='Bool type gpu') 29 | parser.add_argument("-nc", '--num_classes', default=12, type=int) 30 | 31 | args = parser.parse_args() 32 | 33 | if not os.path.exists(args.save_loc): 34 | os.makedirs(args.save_loc) 35 | 36 | print("use_gpu: {}, name_of_model: {}".format(args.use_gpu, args.model_name)) 37 | model_conv = all_pretrained_models(args.num_classes, use_gpu=args.use_gpu, name=args.model_name) 38 | 39 | print("[Loading the pretrained model on this datasets]") 40 | state_dict = torch.load(args.load_loc) 41 | 42 | if args.used_dataparallel: 43 | new_state_dict = OrderedDict() 44 | for k, v in state_dict.items(): 45 | name = k[7:] # remove `module.` 46 | new_state_dict[name] = v 47 | print("[Loading Weights to the Model]") 48 | model_conv.load_state_dict(new_state_dict) 49 | 50 | if not args.used_dataparallel: 51 | model_conv = nn.DataParallel(model_conv, device_ids=[0, 1, 2]) 52 | model_conv.load_state_dict(state_dict) 53 | 54 | model_conv = model_conv.eval() 55 | 56 | 57 | 58 | 59 | print("[Validating on Train data]") 60 | train_predictions, class_to_idx = model_evaluation("train", model_conv, args.input_data_loc, args.input_shape, args.use_gpu, args.model_name) 61 | idx_to_class = {class_to_idx[i]:i for i in class_to_idx.keys()} 62 | train_predictions["predicted_class_name"] = train_predictions["predicted"].apply(lambda x: idx_to_class[x]) 63 | save_loc = args.save_loc+args.load_loc.rsplit("/")[-1].rsplit(".")[0]+"_train_"+".csv" 64 | train_predictions.to_csv(save_loc, index=False) 65 | 66 | print("[Validating on Validating data]") 67 | val_predictions, class_to_idx = model_evaluation("val", model_conv, args.input_data_loc, args.input_shape, args.use_gpu, args.model_name) 68 | idx_to_class = {class_to_idx[i]:i for i in class_to_idx.keys()} 69 | val_predictions["predicted_class_name"] = val_predictions["predicted"].apply(lambda x: idx_to_class[x]) 70 | save_loc = args.save_loc+args.load_loc.rsplit("/")[-1].rsplit(".")[0]+"_val_"+".csv" 71 | val_predictions.to_csv(save_loc, index=False) 72 | 73 | print("[Predictions on Test data]") 74 | predictions = model_evaluation_test("test", model_conv, "data/Test", args.input_shape, args.use_gpu, args.model_name) 75 | predictions["predicted_class_name"] = predictions["predicted"].apply(lambda x: idx_to_class[x]) 76 | save_loc = args.save_loc+args.load_loc.rsplit("/")[-1].rsplit(".")[0]+"_test_"+".csv" 77 | predictions.to_csv(save_loc, index=False) 78 | -------------------------------------------------------------------------------- /tars_train.py: -------------------------------------------------------------------------------- 1 | """ 2 | Some of the links which I found useful. 3 | https://discuss.pytorch.org/t/freeze-the-learnable-parameters-of-resnet-and-attach-it-to-a-new-network/949/9 4 | """ 5 | 6 | import torch 7 | import torch.nn as nn 8 | import torch.optim as optim 9 | from torch.optim import lr_scheduler 10 | import numpy as np 11 | import time 12 | import os 13 | import argparse 14 | 15 | from tars.tars_data_loaders import * 16 | from tars.tars_training import * 17 | from tars.tars_model import * 18 | 19 | parser = argparse.ArgumentParser(description='Training a pytorch model to classify different plants') 20 | parser.add_argument('-idl', '--input_data_loc', help='', default='data/training_data') 21 | parser.add_argument('-mo', '--model_name', default="resnet18") 22 | parser.add_argument('-f', '--freeze_layers', default=True, action='store_false', help='Bool type') 23 | parser.add_argument('-fi', '--freeze_initial_layers', default=True, action='store_false', help='Bool type') 24 | parser.add_argument('-ep', '--epochs', default=100, type=int) 25 | parser.add_argument('-b', '--batch_size', default=64, type=int) 26 | parser.add_argument('-is', '--input_shape', default=224, type=int) 27 | parser.add_argument('-sl', '--save_loc', default="models/" ) 28 | parser.add_argument("-g", '--use_gpu', default=True, action='store_false', help='Bool type gpu') 29 | parser.add_argument("-p", '--use_parallel', default=True, action='store_false', help='Bool type to use_parallel') 30 | parser.add_argument("-mx", '--mixup', default=True, action='store_true' ,help='Use mixup data augementation') 31 | parser.add_argument("-mxal", '--mixup_alpha', default=0.1, type = float, help='Alpha to be used in mixup agumentation') 32 | 33 | args = parser.parse_args() 34 | 35 | 36 | dataloaders, dataset_sizes, class_names = generate_data(args.input_data_loc, args.input_shape, args.model_name, batch_size=args.batch_size) 37 | print(class_names) 38 | 39 | print("[Load the model...]") 40 | # Parameters of newly constructed modules have requires_grad=True by default 41 | print("Loading model using class: {}, use_gpu: {}, freeze_layers: {}, freeze_initial_layers: {}, name_of_model: {}".format(len(class_names), args.use_gpu, args.freeze_layers, args.freeze_initial_layers, args.model_name)) 42 | model_conv = all_pretrained_models(len(class_names), use_gpu=args.use_gpu, freeze_layers=args.freeze_layers, freeze_initial_layers= args.freeze_initial_layers, name=args.model_name) 43 | if args.use_parallel: 44 | print("[Using all the available GPUs]") 45 | model_conv = nn.DataParallel(model_conv, device_ids=[0, 1]) 46 | 47 | print("[Using CrossEntropyLoss...]") 48 | criterion = nn.CrossEntropyLoss() 49 | 50 | print("[Using small learning rate with momentum...]") 51 | optimizer_conv = optim.SGD(list(filter(lambda p: p.requires_grad, model_conv.parameters())), lr=0.001, momentum=0.9) 52 | 53 | print("[Creating Learning rate scheduler...]") 54 | exp_lr_scheduler = lr_scheduler.StepLR(optimizer_conv, step_size=7, gamma=0.1) 55 | 56 | print("[Training the model begun ....]") 57 | print(args.mixup, args.mixup_alpha) 58 | model_ft = train_model(model_conv, dataloaders, dataset_sizes, criterion, optimizer_conv, exp_lr_scheduler, args.use_gpu, 59 | num_epochs=args.epochs, mixup = args.mixup, alpha = args.mixup_alpha) 60 | 61 | print("[Save the best model]") 62 | model_save_loc = args.save_loc+args.model_name+"_"+str(args.freeze_layers)+"_freeze"+"_"+str(args.freeze_initial_layers)+"_freeze_initial_layer"+".pth" 63 | torch.save(model_ft.state_dict(), model_save_loc) 64 | --------------------------------------------------------------------------------