commit dd0ae28c745d477454813ccb468af28e659bae35 Author: ᴡᴏᴊᴄɪᴇᴄʜ ɢʀᴢʏʙᴏᴡꜱᴋɪ Date: Thu Jan 4 18:49:46 2024 +0100 initial commit diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..3ce3588 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/Cephalophus_niger.jpg b/Cephalophus_niger.jpg new file mode 100644 index 0000000..4b913de Binary files /dev/null and b/Cephalophus_niger.jpg differ diff --git a/FELV-cat.jpg b/FELV-cat.jpg new file mode 100644 index 0000000..6882a2f Binary files /dev/null and b/FELV-cat.jpg differ diff --git a/basics.ipynb b/basics.ipynb new file mode 100644 index 0000000..ce88847 --- /dev/null +++ b/basics.ipynb @@ -0,0 +1,39 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 7, + "id": "initial_id", + "metadata": { + "collapsed": true, + "ExecuteTime": { + "end_time": "2024-01-04T16:38:33.550511800Z", + "start_time": "2024-01-04T16:38:33.542353Z" + } + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/cat.png b/cat.png new file mode 100644 index 0000000..9abe07a Binary files /dev/null and b/cat.png differ diff --git a/main.py b/main.py new file mode 100644 index 0000000..95c15b3 --- /dev/null +++ b/main.py @@ -0,0 +1,59 @@ +from PIL import Image +import torch +import torch.nn.functional as F +from torchvision.models.resnet import resnet50, ResNet50_Weights +from torchvision.transforms import transforms + +# Load the pre-trained model +model = resnet50(weights=ResNet50_Weights.DEFAULT) + +model.eval() + +# Define the image transformations +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]), +]) + + +def is_cat(image_path): + # Open the image + img = Image.open(image_path) + + # Preprocess the image + img_t = preprocess(img) + batch_t = torch.unsqueeze(img_t, 0) + + # Make the prediction + out = model(batch_t) + + # Apply softmax to get probabilities + probabilities = F.softmax(out, dim=1) + + # Get the maximum predicted class and its probability + max_prob, max_class = torch.max(probabilities, dim=1) + max_prob = max_prob.item() + max_class = max_class.item() + + # Check if the maximum predicted class is within the range 281-285 + if 281 <= max_class <= 285: + return max_class, max_prob + else: + return max_class, None + + +image_path = 'pobrane.jpg' +max_class, max_prob = is_cat(image_path) +translator = { + 281: "tabby cat", + 282: "tiger cat", + 283: "persian cat", + 284: "siamese cat", + 285: "egyptian cat" +} +if max_prob is not None: + print(f"The image is recognized as '{translator[max_class]}' with a probability of {round(max_prob * 100, 2)}%") +else: + print(f"The image is not recognized as a class within the range 281-285 ({max_class})") diff --git a/pobrane.jpg b/pobrane.jpg new file mode 100644 index 0000000..5152a99 Binary files /dev/null and b/pobrane.jpg differ