{"id":19138200,"url":"https://github.com/helias/car-model-recognition","last_synced_at":"2025-05-06T20:46:23.150Z","repository":{"id":145793230,"uuid":"174192915","full_name":"Helias/Car-Model-Recognition","owner":"Helias","description":"Car Model Recognition project","archived":false,"fork":false,"pushed_at":"2022-03-11T10:20:27.000Z","size":5039,"stargazers_count":78,"open_issues_count":0,"forks_count":20,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-19T14:58:04.572Z","etag":null,"topics":["car-model-recognition","machine-learning","recognition","resnet-152"],"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/Helias.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":"2019-03-06T17:50:11.000Z","updated_at":"2025-04-19T11:41:22.000Z","dependencies_parsed_at":null,"dependency_job_id":"1a54a8a4-67b5-4c00-beb7-02fa70fbe0d9","html_url":"https://github.com/Helias/Car-Model-Recognition","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/Helias%2FCar-Model-Recognition","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Helias%2FCar-Model-Recognition/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Helias%2FCar-Model-Recognition/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Helias%2FCar-Model-Recognition/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Helias","download_url":"https://codeload.github.com/Helias/Car-Model-Recognition/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252769132,"owners_count":21801373,"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":["car-model-recognition","machine-learning","recognition","resnet-152"],"created_at":"2024-11-09T06:42:02.645Z","updated_at":"2025-05-06T20:46:23.141Z","avatar_url":"https://github.com/Helias.png","language":"Python","funding_links":["https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=WE5LZM2D4WPBC\u0026source=url"],"categories":[],"sub_categories":[],"readme":"# Car Model Recognition\n\nThis is a university project for the course \"Computer Vision\".\nThis project consists of a classifier of a car model.\n\n## Requirements\n- Python3\n- numpy\n- pytorch\n- torchvision\n- scikit-learn\n- matplotlib\n- pillow\n- torch (pytorch)\n- torchvision\n\nYou can install the requirements using:\n```\npip3 install -r requirements.txt\n```\n\n**Troubleshooting**: if you get some errors about pytorch or torchvision install use `sudo` to install it.\n\n## Usage\n\nFirst, if you have no resnet152 model trained and you need from scratch to do it you need to:\n\n- download dataset\n- preprocess the dataset\n- train the model\n\nAfterward you can try a new sample.\n\n### Download dataset\n\nI suggest using [VMMRdb](http://vmmrdb.cecsresearch.org/) as a dataset, it's free and full of labeled images for car model recognition instead of detection (most datasets are for this).\n\nSo download the dataset, select some models and put the directory model in the dataset folder, any directory in \"dataset\" will be considered a new class.\n\nIf you need more data for your project you can also add the followings dataset:\n- [Stanford Cars Dataset from jkrause](https://ai.stanford.edu/~jkrause/cars/car_dataset.html) (low images quantity)\n- [Comprehensive Cars Database](http://mmlab.ie.cuhk.edu.hk/datasets/comp_cars/), here the module to get this dataset [MODULE](http://mmlab.ie.cuhk.edu.hk/datasets/comp_cars/agreement.pdf)\n\n### Handle CSV training, testing, validation and dataset structure\n\nThe dataset structure should be like this:\n```\ndataset / classes / file.jpg\n```\n\nFor example, we have 3 classes: **honda_civic, nissan and ford**:\n```\ndataset_dir / honda_civic / file1.jpg\ndataset_dir / honda_civic / file2.jpg\n....\ndataset_dir / nissan / file1.jpg\ndataset_dir / nissan / file2.jpg\n....\ndataset_dir / ford / file1.jpg\ndataset_dir / ford / file2.jpg\n...\nand so on.\n```\n\nThe **\"dataset_dir\"** is the **IMAGES_PATH** in config.py.\nThe python script will save the classes in a  dict() named **num_classes**, like this:\n```\nnum_classes = {\n  \"honda_civic\": 1,\n  \"nissan\": 2,\n  \"ford\": 3\n}\n```\n\nThis conversion happens automatically when you just add a directory inside the IMAGES_PATH, if you add tomorrow a new car, like, FIAT, the program will add automatically to the classes, just **pay attention to the order of the classes inside num_classes and the related training, testing and validation CSV files**.\n\nThe file **training, testing, and validation (CSV)** should contain only two columns:\n**FILE_NAME, NUM_CLASS**\n\nExample of CSV file:\n```\nfile1.jpg, 1\nfile2.jpg, 1\nfile1.jpg, 2\nfile2.jpg, 2\nfile1.jpg, 3\nfile2.jpg, 3\n```\n\nAnyway, this paragraph is only for your info, the CSV files are automatically generated by the preprocessing phase explained in the following paragraph.\n\n\n### Preprocess the dataset\nYou have to generate the CSV files and calculate the mean and standard deviation to apply a normalization, just use the -p parameter to process your dataset so type:\n\n```\n$ python3 main.py -p\n```\n\n### Train the model\n\n**Short introduction**\n\nBefore the training process, modify the `EPOCHS` parameter in `config.py`, usually with 3 classes 30-50 epochs should be enough, but you have to see the `results_graph.png` file (when you finish your training with the default epochs parameter) and check if the blue curve is stable.\n\nAn example of the graph could be the following:\n![graph results - Car Model Recognition](https://user-images.githubusercontent.com/519778/67412403-81fe5c00-f5bf-11e9-9bd1-e86251bb9a0c.png)\n\nAfter 45-50 epochs (number bottom of the graph), the blue curve is stable and does not have peaks down.\nMoreover, the testing curve (the orange one) is pretty \"stable\", even with some peaks, for the testing is normal that the peaks are frequently.\n\n**Train the model**\n\nTo train a new model resnet152 model you can run the main.py with the -t parameter, so type:\n\n```\n$ python3 main.py -t\n```\n\nThe results will be saved in the results/ directory with the F1 score, accuracy, confusion matrix, and the accuracy/loss graph difference between training and testing.\n\n## Try a new sample\n\nTry to predict a new sample you can just type:\n```\npython3 main.py -i path/file.jpg\n```\n\n---\n\nI used this project to predict 3 models:\n- Nissan Altima\n- Honda Civic\n- Ford Explorer\n\nI selected all 2000-2007 images from VMMRdb, so I downloaded the full dataset and choose the 2000-2007 images and put them into one directory per class (so I had 3 directories named \"Ford Explorer\", \"Nissan Altima\", \"Honda Civic\" in dataset folder).\n\n## Troubleshooting\n\n### - Size mismatch\n\nError:\n```python\nRuntimeError: Error(s) in loading state_dict for ResNet:\nsize mismatch for fc.weight: copying a param with shape torch.Size([1000, 2048]) from checkpoint, the shape in current model is torch.Size([3, 2048]).\nsize mismatch for fc.bias: copying a param with shape torch.Size([1000]) from checkpoint, the shape in current model is torch.Size([3]).\n```\n\n**Solution**: you probably need to re-train your neural network model because you are using the wrong model for your data and classes, so don't use some **pretrained** model but train a new neural network with your data/classes.\n\n### - CUDA out of memory\n\nError:\n```python\n######### ERROR #######\nCUDA out of memory. Tried to allocate 20.00 MiB (GPU 0; 1.96 GiB total capacity; 967.98 MiB already allocated; 25.94 MiB free; 48.02 MiB cached)\n\n\n######### batch #######\n[images.png, files_path.png, ....]\n\nTraceback (most recent call last):\n  File \"main.py\", line 227, in \u003cmodule\u003e\n    train_model_iter(\"resnet152\", resnet152_model)\n  File \"main.py\", line 215, in train_model_iter\n    model, loss_acc, y_testing, preds = train_model(model_name=model_name, model=model, weight_decay=weight_decay)\n  File \"main.py\", line 124, in train_model\n    epoch_loss /= samples\nZeroDivisionError: division by zero\n```\n\n**Solution**: you're using CUDA, probably, the memory of your GPU is too low for the batch size that you're giving in input, try to reduce the `BATCH_SIZE` from **config.py** or use your RAM instead of GPU memory if you have more, so put `USE_CUDA=false` in **config.py**.\n\n### - \"My model does not recognize exactly the class\"\nProbably you have to increase the **DATA PER CLASS** in your dataset, a good number of images per class could be 10k (10 000 items), but with only 3 classes you can even use 2k-5k items per class.\nAnother parameter that affects hugely the training is the **EPOCHS**, try to at least 50 epochs if you are not satisfied with the results.\n\n\n**You are not the only one to get these troubles, check the issue [#3](https://github.com/Helias/Car-Model-Recognition/issues/3) to get a full conversation of this solutions/troubleshooting.**\n\n\n## Credits\n- [Helias](https://github.com/Helias)\n- [Riccardo Pesce](https://github.com/RiccardoMPesce)\n### Contribute\nYou can help us [opening a new issue](https://github.com/Helias/Car-Model-Recognition/issues/new) to report a bug or a suggestion  \n\nor you can donate to support us\n\n[![Donate PayPal](https://camo.githubusercontent.com/ed44813b2a0ca01f80a00cca116f04208c127a80/68747470733a2f2f7777772e70617970616c2e636f6d2f656e5f47422f692f62746e2f62746e5f646f6e61746543435f4c472e676966)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=WE5LZM2D4WPBC\u0026source=url)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhelias%2Fcar-model-recognition","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhelias%2Fcar-model-recognition","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhelias%2Fcar-model-recognition/lists"}