diff --git a/README.md b/README.md index ea03ed9..f75b235 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,10 @@ The pytorch implementation is also very effieicent and the whole model takes onl #### Update History:
+-- 2023 May 13: + -- Add support for torch hub. -- 2023 Apr 14: - -- update benchmark results + -- update benchmark results. -- 2023 Apr 13: -- new weights for the removed paddings for 1x1 conv layers. -- some minor fixes @@ -147,6 +149,71 @@ For all benchmark results [look here](https://github.com/Coderx7/SimpleNet_Pytor #### Models and logs -- refer to each dataset directory in the repository for further information on how to access models. +### How to use +You can either download the [simplenet.py model](./imagenet/simplenet.py) and use it directly in your projects +or you can use torch hub. + +Using torch hub you can do something like this: +```python +import torch +# use the latest master +model = torch.hub.load("coderx7/simplenet_pytorch", "simplenetv1_5m_m1", pretrained=True) +# or any of these variants at the moment +# model = torch.hub.load("coderx7/simplenet_pytorch:v1.0.0", "simplenetv1_5m_m2", pretrained=True) +# model = torch.hub.load("coderx7/simplenet_pytorch:v1.0.0", "simplenetv1_9m_m1", pretrained=True) +# model = torch.hub.load("coderx7/simplenet_pytorch:v1.0.0", "simplenetv1_9m_m2", pretrained=True) +# model = torch.hub.load("coderx7/simplenet_pytorch:v1.0.0", "simplenetv1_small_m1_05", pretrained=True) +# model = torch.hub.load("coderx7/simplenet_pytorch:v1.0.0", "simplenetv1_small_m2_05", pretrained=True) +# model = torch.hub.load("coderx7/simplenet_pytorch:v1.0.0", "simplenetv1_small_m1_075", pretrained=True) +# model = torch.hub.load("coderx7/simplenet_pytorch:v1.0.0", "simplenetv1_small_m2_075", pretrained=True) +model.eval() + +# Download an example image from the pytorch website +import urllib +url, filename = ("https://github.com/pytorch/hub/raw/master/images/dog.jpg", "dog.jpg") +try: urllib.URLopener().retrieve(url, filename) +except: urllib.request.urlretrieve(url, filename) + +# sample execution (requires torchvision) +from PIL import Image +from torchvision import transforms +input_image = Image.open(filename) +preprocess = transforms.Compose([ + transforms.Resize(256), + transforms.CenterCrop(224), + transforms.ToTensor(), + transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), +]) +input_tensor = preprocess(input_image) +input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model + +# move the input and model to GPU for speed if available +if torch.cuda.is_available(): + input_batch = input_batch.to('cuda') + model.to('cuda') + +with torch.no_grad(): + output = model(input_batch) +# Tensor of shape 1000, with confidence scores over Imagenet's 1000 classes +print(output[0]) +# The output has unnormalized scores. To get probabilities, you can run a softmax on it. +probabilities = torch.nn.functional.softmax(output[0], dim=0) +print(probabilities) + +# Download ImageNet labels +!wget https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt + +# Read the categories +with open("imagenet_classes.txt", "r") as f: + categories = [s.strip() for s in f.readlines()] +# Show top categories per image +top5_prob, top5_catid = torch.topk(probabilities, 5) +for i in range(top5_prob.size(0)): + print(categories[top5_catid[i]], top5_prob[i].item()) +``` + +or you can run the demo online from huggingface simplenetspace: https://huggingface.co/spaces/coderx7/simplenet + ## Citation If you find SimpleNet useful in your research, please consider citing: diff --git a/cifar/README.md b/cifar/README.md index 563a9e8..0649e1a 100644 --- a/cifar/README.md +++ b/cifar/README.md @@ -50,9 +50,8 @@ Simply initiate the training like : `python3 main.py ./data/cifar.python --dataset cifar10 --arch simplenet_cifar_5m --save_path ./snapshots/simplenet --epochs 540 --batch_size 100 --workers 2` -Note 1: the initial learning rate, and optimization policy is hard coded just like caffe. -Note 2: for testing the cifar10/100 weights located in the repository, use the `simplenet_cifar_5m_extra_pool` model instead. see [issue #5](https://github.com/Coderx7/SimpleNet_Pytorch/issues/5) for more information. - +Note 1: The initial learning rate, and optimization policy is hard coded just like caffe. +Note 2: For testing the cifar10/100 weights located in the repository, use the `simplenet_cifar_5m_extra_pool` model instead. see [issue #5](https://github.com/Coderx7/SimpleNet_Pytorch/issues/5) and [Error loading checkpoint#7](https://github.com/Coderx7/SimpleNet_Pytorch/issues/7#issuecomment-1932491193) for more information. ## Citation If you find SimpleNet useful in your research, please consider citing: diff --git a/cifar/models/simplenet.py b/cifar/models/simplenet.py index 9d9acbf..b808219 100644 --- a/cifar/models/simplenet.py +++ b/cifar/models/simplenet.py @@ -297,6 +297,7 @@ def _make_layers(self, scale: float): # check to convert any possible integer value to its decimal counterpart. custom_dropout = None if custom_dropout is None else float(custom_dropout) kernel_size = 3 + padding = 1 if layer_type == ['k1']: kernel_size = 1 padding = 0 @@ -404,7 +405,7 @@ def simplenet(pretrained: bool = False, **kwargs: Any) -> SimpleNet: return _gen_simplenet(model_variant, num_classes, in_chans, scale, network_idx, mode, pretrained, drop_rates) -def remove_network_settings(kwargs: Dict[str, Any]) -> Dict[str, Any]: +def _remove_network_settings(kwargs: Dict[str, Any]) -> Dict[str, Any]: """Removes network related settings passed in kwargs for predefined network configruations below Returns: @@ -420,7 +421,7 @@ def simplenet_cifar_310k(pretrained: bool = False, **kwargs: Any) -> SimpleNet: that were used in the paper """ model_variant = "simplenet_cifar_310k" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, network_idx=2, mode=0, pretrained=pretrained, **model_args) @@ -429,7 +430,7 @@ def simplenet_cifar_460k(pretrained: bool = False, **kwargs: Any) -> SimpleNet: that were used in the paper """ model_variant = "simplenet_cifar_460k" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, network_idx=3, mode=0, pretrained=pretrained, **model_args) @@ -437,7 +438,7 @@ def simplenet_cifar_5m(pretrained: bool = False, **kwargs: Any) -> SimpleNet: """The original implementation of simplenet trained on cifar10/100 in caffe. """ model_variant = "simplenet_cifar_5m" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, network_idx=4, mode=0, pretrained=pretrained, **model_args) @@ -447,7 +448,7 @@ def simplenet_cifar_5m_extra_pool(pretrained: bool = False, **kwargs: Any) -> Si this is just here to be able to load the weights that were trained using this variation still available on the repository. """ model_variant = "simplenet_cifar_5m_extra_pool" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, network_idx=5, mode=0, pretrained=pretrained, **model_args) @@ -463,7 +464,7 @@ def simplenetv1_small_m1_05(pretrained: bool = False, **kwargs: Any) -> SimpleNe SimpleNet: a SimpleNet model instance is returned upon successful instantiation. """ model_variant = "simplenetv1_small_m1_05" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, scale=0.5, network_idx=0, mode=1, pretrained=pretrained, **model_args) @@ -478,7 +479,7 @@ def simplenetv1_small_m2_05(pretrained: bool = False, **kwargs: Any) -> SimpleNe SimpleNet: a SimpleNet model instance is returned upon successful instantiation. """ model_variant = "simplenetv1_small_m2_05" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, scale=0.5, network_idx=0, mode=2, pretrained=pretrained, **model_args) @@ -493,7 +494,7 @@ def simplenetv1_small_m1_075(pretrained: bool = False, **kwargs: Any) -> SimpleN SimpleNet: a SimpleNet model instance is returned upon successful instantiation. """ model_variant = "simplenetv1_small_m1_075" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, scale=0.75, network_idx=0, mode=1, pretrained=pretrained, **model_args) @@ -508,7 +509,7 @@ def simplenetv1_small_m2_075(pretrained: bool = False, **kwargs: Any) -> SimpleN SimpleNet: a SimpleNet model instance is returned upon successful instantiation. """ model_variant = "simplenetv1_small_m2_075" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, scale=0.75, network_idx=0, mode=2, pretrained=pretrained, **model_args) @@ -523,7 +524,7 @@ def simplenetv1_5m_m1(pretrained: bool = False, **kwargs: Any) -> SimpleNet: SimpleNet: a SimpleNet model instance is returned upon successful instantiation. """ model_variant = "simplenetv1_5m_m1" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, scale=1.0, network_idx=0, mode=1, pretrained=pretrained, **model_args) @@ -538,7 +539,7 @@ def simplenetv1_5m_m2(pretrained: bool = False, **kwargs: Any) -> SimpleNet: SimpleNet: a SimpleNet model instance is returned upon successful instantiation. """ model_variant = "simplenetv1_5m_m2" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, scale=1.0, network_idx=0, mode=2, pretrained=pretrained, **model_args) @@ -553,7 +554,7 @@ def simplenetv1_9m_m1(pretrained: bool = False, **kwargs: Any) -> SimpleNet: SimpleNet: a SimpleNet model instance is returned upon successful instantiation. """ model_variant = "simplenetv1_9m_m1" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, scale=1.0, network_idx=1, mode=1, pretrained=pretrained, **model_args) @@ -568,7 +569,7 @@ def simplenetv1_9m_m2(pretrained: bool = False, **kwargs: Any) -> SimpleNet: SimpleNet: a SimpleNet model instance is returned upon successful instantiation. """ model_variant = "simplenetv1_9m_m2" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, scale=1.0, network_idx=1, mode=2, pretrained=pretrained, **model_args) diff --git a/hubconf.py b/hubconf.py index 0c83215..ac5d31f 100644 --- a/hubconf.py +++ b/hubconf.py @@ -4,7 +4,6 @@ from torchvision.models import get_model_weights, get_weight from imagenet.simplenet import ( - simplenet, simplenetv1_5m_m1, simplenetv1_5m_m2, simplenetv1_9m_m1, diff --git a/imagenet/simplenet.py b/imagenet/simplenet.py index 01dcf0b..e104792 100644 --- a/imagenet/simplenet.py +++ b/imagenet/simplenet.py @@ -405,7 +405,7 @@ def simplenet(pretrained: bool = False, **kwargs: Any) -> SimpleNet: return _gen_simplenet(model_variant, num_classes, in_chans, scale, network_idx, mode, pretrained, drop_rates) -def remove_network_settings(kwargs: Dict[str, Any]) -> Dict[str, Any]: +def _remove_network_settings(kwargs: Dict[str, Any]) -> Dict[str, Any]: """Removes network related settings passed in kwargs for predefined network configruations below Returns: @@ -421,7 +421,7 @@ def simplenet_cifar_310k(pretrained: bool = False, **kwargs: Any) -> SimpleNet: that were used in the paper """ model_variant = "simplenet_cifar_310k" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, network_idx=2, mode=0, pretrained=pretrained, **model_args) @@ -430,7 +430,7 @@ def simplenet_cifar_460k(pretrained: bool = False, **kwargs: Any) -> SimpleNet: that were used in the paper """ model_variant = "simplenet_cifar_460k" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, network_idx=3, mode=0, pretrained=pretrained, **model_args) @@ -438,7 +438,7 @@ def simplenet_cifar_5m(pretrained: bool = False, **kwargs: Any) -> SimpleNet: """The original implementation of simplenet trained on cifar10/100 in caffe. """ model_variant = "simplenet_cifar_5m" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, network_idx=4, mode=0, pretrained=pretrained, **model_args) @@ -448,7 +448,7 @@ def simplenet_cifar_5m_extra_pool(pretrained: bool = False, **kwargs: Any) -> Si this is just here to be able to load the weights that were trained using this variation still available on the repository. """ model_variant = "simplenet_cifar_5m_extra_pool" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, network_idx=5, mode=0, pretrained=pretrained, **model_args) @@ -464,7 +464,7 @@ def simplenetv1_small_m1_05(pretrained: bool = False, **kwargs: Any) -> SimpleNe SimpleNet: a SimpleNet model instance is returned upon successful instantiation. """ model_variant = "simplenetv1_small_m1_05" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, scale=0.5, network_idx=0, mode=1, pretrained=pretrained, **model_args) @@ -479,7 +479,7 @@ def simplenetv1_small_m2_05(pretrained: bool = False, **kwargs: Any) -> SimpleNe SimpleNet: a SimpleNet model instance is returned upon successful instantiation. """ model_variant = "simplenetv1_small_m2_05" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, scale=0.5, network_idx=0, mode=2, pretrained=pretrained, **model_args) @@ -494,7 +494,7 @@ def simplenetv1_small_m1_075(pretrained: bool = False, **kwargs: Any) -> SimpleN SimpleNet: a SimpleNet model instance is returned upon successful instantiation. """ model_variant = "simplenetv1_small_m1_075" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, scale=0.75, network_idx=0, mode=1, pretrained=pretrained, **model_args) @@ -509,7 +509,7 @@ def simplenetv1_small_m2_075(pretrained: bool = False, **kwargs: Any) -> SimpleN SimpleNet: a SimpleNet model instance is returned upon successful instantiation. """ model_variant = "simplenetv1_small_m2_075" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, scale=0.75, network_idx=0, mode=2, pretrained=pretrained, **model_args) @@ -524,7 +524,7 @@ def simplenetv1_5m_m1(pretrained: bool = False, **kwargs: Any) -> SimpleNet: SimpleNet: a SimpleNet model instance is returned upon successful instantiation. """ model_variant = "simplenetv1_5m_m1" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, scale=1.0, network_idx=0, mode=1, pretrained=pretrained, **model_args) @@ -539,7 +539,7 @@ def simplenetv1_5m_m2(pretrained: bool = False, **kwargs: Any) -> SimpleNet: SimpleNet: a SimpleNet model instance is returned upon successful instantiation. """ model_variant = "simplenetv1_5m_m2" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, scale=1.0, network_idx=0, mode=2, pretrained=pretrained, **model_args) @@ -554,7 +554,7 @@ def simplenetv1_9m_m1(pretrained: bool = False, **kwargs: Any) -> SimpleNet: SimpleNet: a SimpleNet model instance is returned upon successful instantiation. """ model_variant = "simplenetv1_9m_m1" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, scale=1.0, network_idx=1, mode=1, pretrained=pretrained, **model_args) @@ -569,7 +569,7 @@ def simplenetv1_9m_m2(pretrained: bool = False, **kwargs: Any) -> SimpleNet: SimpleNet: a SimpleNet model instance is returned upon successful instantiation. """ model_variant = "simplenetv1_9m_m2" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, scale=1.0, network_idx=1, mode=2, pretrained=pretrained, **model_args) diff --git a/imagenet/training_scripts/imagenet_training/timm/models/simplenet.py b/imagenet/training_scripts/imagenet_training/timm/models/simplenet.py index 52ae526..e54a79e 100644 --- a/imagenet/training_scripts/imagenet_training/timm/models/simplenet.py +++ b/imagenet/training_scripts/imagenet_training/timm/models/simplenet.py @@ -437,7 +437,7 @@ def simplenet(pretrained: bool = False, **kwargs: Any) -> SimpleNet: return _gen_simplenet(model_variant, num_classes, in_chans, scale, network_idx, mode, pretrained, drop_rates) -def remove_network_settings(kwargs: Dict[str, Any]) -> Dict[str, Any]: +def _remove_network_settings(kwargs: Dict[str, Any]) -> Dict[str, Any]: """Removes network related settings passed in kwargs for predefined network configruations below Returns: @@ -497,7 +497,7 @@ def simplenetv1_small_m1_05(pretrained: bool = False, **kwargs: Any) -> SimpleNe SimpleNet: a SimpleNet model instance is returned upon successful instantiation. """ model_variant = "simplenetv1_small_m1_05" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, scale=0.5, network_idx=0, mode=1, pretrained=pretrained, **model_args) @@ -513,7 +513,7 @@ def simplenetv1_small_m2_05(pretrained: bool = False, **kwargs: Any) -> SimpleNe SimpleNet: a SimpleNet model instance is returned upon successful instantiation. """ model_variant = "simplenetv1_small_m2_05" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, scale=0.5, network_idx=0, mode=2, pretrained=pretrained, **model_args) @@ -529,7 +529,7 @@ def simplenetv1_small_m1_075(pretrained: bool = False, **kwargs: Any) -> SimpleN SimpleNet: a SimpleNet model instance is returned upon successful instantiation. """ model_variant = "simplenetv1_small_m1_075" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, scale=0.75, network_idx=0, mode=1, pretrained=pretrained, **model_args) @@ -545,7 +545,7 @@ def simplenetv1_small_m2_075(pretrained: bool = False, **kwargs: Any) -> SimpleN SimpleNet: a SimpleNet model instance is returned upon successful instantiation. """ model_variant = "simplenetv1_small_m2_075" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, scale=0.75, network_idx=0, mode=2, pretrained=pretrained, **model_args) @@ -561,7 +561,7 @@ def simplenetv1_5m_m1(pretrained: bool = False, **kwargs: Any) -> SimpleNet: SimpleNet: a SimpleNet model instance is returned upon successful instantiation. """ model_variant = "simplenetv1_5m_m1" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, scale=1.0, network_idx=0, mode=1, pretrained=pretrained, **model_args) @@ -577,7 +577,7 @@ def simplenetv1_5m_m2(pretrained: bool = False, **kwargs: Any) -> SimpleNet: SimpleNet: a SimpleNet model instance is returned upon successful instantiation. """ model_variant = "simplenetv1_5m_m2" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, scale=1.0, network_idx=0, mode=2, pretrained=pretrained, **model_args) @@ -593,7 +593,7 @@ def simplenetv1_9m_m1(pretrained: bool = False, **kwargs: Any) -> SimpleNet: SimpleNet: a SimpleNet model instance is returned upon successful instantiation. """ model_variant = "simplenetv1_9m_m1" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, scale=1.0, network_idx=1, mode=1, pretrained=pretrained, **model_args) @@ -609,7 +609,7 @@ def simplenetv1_9m_m2(pretrained: bool = False, **kwargs: Any) -> SimpleNet: SimpleNet: a SimpleNet model instance is returned upon successful instantiation. """ model_variant = "simplenetv1_9m_m2" - model_args = remove_network_settings(kwargs) + model_args = _remove_network_settings(kwargs) return _gen_simplenet(model_variant, scale=1.0, network_idx=1, mode=2, pretrained=pretrained, **model_args)pFad - Phonifier reborn Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: