{"id":22799092,"url":"https://github.com/thomd/pytorch-image-classification-with-transfer-learning","last_synced_at":"2026-04-29T17:38:22.554Z","repository":{"id":66830089,"uuid":"468533615","full_name":"thomd/pytorch-image-classification-with-transfer-learning","owner":"thomd","description":"PyTorch Image Classification with Transfer Learning","archived":false,"fork":false,"pushed_at":"2022-04-19T12:38:34.000Z","size":290,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-05T21:42:44.531Z","etag":null,"topics":["computer-vision","deep-learning","machine-learning","pytorch"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thomd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-03-10T22:45:06.000Z","updated_at":"2022-09-19T11:24:55.000Z","dependencies_parsed_at":"2023-05-10T18:00:36.268Z","dependency_job_id":null,"html_url":"https://github.com/thomd/pytorch-image-classification-with-transfer-learning","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomd%2Fpytorch-image-classification-with-transfer-learning","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomd%2Fpytorch-image-classification-with-transfer-learning/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomd%2Fpytorch-image-classification-with-transfer-learning/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomd%2Fpytorch-image-classification-with-transfer-learning/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thomd","download_url":"https://codeload.github.com/thomd/pytorch-image-classification-with-transfer-learning/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246365651,"owners_count":20765549,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["computer-vision","deep-learning","machine-learning","pytorch"],"created_at":"2024-12-12T07:07:53.873Z","updated_at":"2026-04-29T17:38:17.522Z","avatar_url":"https://github.com/thomd.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PyTorch Image Classification with Transfer Learning\n\nIn this experiment we train an image classifier using [transfer learning](https://nbviewer.jupyter.org/github/thomd/pytorch-image-classification-with-transfer-learning/blob/main/transfer-learning.ipynb) of the pre-trained convolutional neural network **ResNet-50**.\n\n## Local Setup\n\n    conda env create -f environment.yml python=3.9\n    conda activate ictl\n\n### Image Data\n\nImages must be structured like this:\n\n    .\n    └── images\n        ├── label_1\n        │   ├── image_0.jpg\n        │   ├── image_1.jpg\n        │   └── image_2.jpg\n        └── label_2\n            ├── image_3.jpg\n            └── image_4.jpg\n\nEach class has it's own directory for the images. The images are then labeled with the class taken from the directory name.\n\n### Create Dataset\n\nFor this example, we use the [Flowers Dataset](https://www.kaggle.com/datasets/imsparsh/flowers-dataset/) to train a flower-classification model:\n\n    kaggle datasets list -s flowers\n    kaggle datasets download -d imsparsh/flowers-dataset\n    unzip flowers-dataset.zip -d flower-photos\n\nFor ease of demonstration, we only use the `train` part of the dataset and do a train-validate-test split ourself:\n\n    python build_dataset.py --help\n    python build_dataset.py --images-path flower-photos/train\n\n### Train and Validate Model by Transfer-Learning\n\n    python train.py --help\n    python train.py --show-labels\n\n    tensorboard --logdir=results\n    open http://localhost:6006\n\nTrain by Feature Extraction\n\n    python train.py --type feature-extraction --epochs 50 --batch 32 --lr 0.001 --export-onnx\n\nTrain by Fine Tuning\n\n    python train.py --type fine-tuning --epochs 50 --batch 32 --lr 0.0005 --export-onnx\n\n### Test Model\n\nTest a specific model from all experiments within the `results` folder:\n\n    python inference.py --model results/{best-experiment}/best_model.pth --batch 16\n\nPrint result and label of a class with\n\n    open output/batch_16.png\n    python train.py --show-labels\n\n### Prediction of an individual Image\n\n    python inference.py --model results/.../best_model.pth --image-path /path/to/image.jpg\n\n## Train in Google Colab\n\nCreate new [Colab Notebook](https://colab.research.google.com) and run these commands:\n\n    %cd /content\n    !nvidia-smi\n    !pip install -q kaggle torchmetrics albumentations==1.1.0 opencv-python-headless==4.2.0.34\n    from google.colab import files\n    uploaded = files.upload()\n    !mkdir ~/.kaggle\n    !mv kaggle.json ~/.kaggle/\n    !chmod 600 /root/.kaggle/kaggle.json\n\n    !git clone https://github.com/thomd/pytorch-image-classification-with-transfer-learning.git\n    %cd pytorch-image-classification-with-transfer-learning\n    !kaggle datasets download -d imsparsh/flowers-dataset\n    !unzip -qq flowers-dataset.zip -d flower-photos\n\n    !python build_dataset.py --images-path flower-photos/train\n\n    %load_ext tensorboard\n    %tensorboard --logdir=./results\n\n    !python train.py --show-labels\n    !python train.py --epochs 60 --batch 64 --lr 0.0001\n\n    !python inference.py --model results/.../best_model.pth --batch 16\n    from IPython.display import Image\n    display(Image('output/batch_16.png'))\n\n## Run Inference Endpoint with Fast API\n\nThis endpoint expects a trained **ONNX classification model** `best_model.onnx` in the root folder:\n\n    cp results/{best-experiment}/best_model.onnx .\n\nEither start [uvicorn](https://www.uvicorn.org/) web server with\n\n    uvicorn service:app\n    curl -F \"file=@image.jpg\" -H \"Content-Type: multipart/form-data\" http://127.0.0.1:8000/image\n\nor build and run as **Docker container** with\n\n    docker-compose up -d --build\n    docker logs image-classification\n    curl -F \"file=@image.jpg\" -H \"Content-Type: multipart/form-data\" http://127.0.0.1:8000/image\n    docker-compose down\n\n## Deploy Service as Azure Container Instance\n\nUse Azure Container Instances (ACI) to run serverless Docker containers in Azure.\n\nCreate Azure Container Registry (ACR):\n\n    az login\n    az group create -n \u003cgroup\u003e -l \u003clocation\u003e\n    az acr create -g \u003cgroup\u003e -n \u003creg\u003e --sku Basic --admin-enabled true\n    az acr list -g \u003cgroup\u003e -o table\n\nBuild \u0026 upload docker image to ACR\n\n    docker build -t image-classification-api .\n    docker tag image-classification-api \u003creg\u003e.azurecr.io/image-classification-api:v1\n    az acr login -n \u003creg\u003e\n    docker push \u003creg\u003e.azurecr.io/image-classification-api:v1\n\nCreate container (find username and password here: [Azure Portal](https://portal.azure.com) \u003e Container Registry \u003e Access Keys)\n\n    az container create -g \u003cgroup\u003e -n \u003ccontainer\u003e --image \u003creg\u003e.azurecr.io/image-classification-api:v1 --dns-name-label \u003clabel\u003e --ports 80\n    az container show -g \u003cgroup\u003e -n \u003ccontainer\u003e --query \"{FQDN:ipAddress.fqdn,ProvisioningState:provisioningState}\"\n    az container logs -g \u003cgroup\u003e -n \u003ccontainer\u003e --follow\n\nOpen SwaggerUI\n    \n    open http://\u003clabel\u003e.\u003clocation\u003e.azurecontainer.io\n\nDelete resource group\n    \n    az group delete -n \u003cgroup\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthomd%2Fpytorch-image-classification-with-transfer-learning","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthomd%2Fpytorch-image-classification-with-transfer-learning","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthomd%2Fpytorch-image-classification-with-transfer-learning/lists"}