1
0
Fork 0
resnet-dogcat/README.md

69 lines
2.8 KiB
Markdown
Raw Permalink Normal View History

2024-05-15 16:34:04 +08:00
# Cat-Dog Classification Model
## Introduction
This repository contains a Cat-Dog classification model based on the ResNet-50 architecture. The model is trained to distinguish between images of cats and dogs.
## ResNet Model
ResNet-50 is a deep convolutional neural network with 50 layers. It is designed to overcome the vanishing gradient problem, which is common in very deep networks, by using skip connections or residuals. This allows the network to be significantly deeper while still being easy to optimize.
## Training
The model is trained on a dataset of cat and dog images. The training process involves the following steps:
1. **Data Preprocessing**: Images are resized, cropped, and normalized.
2. **Model Initialization**: A pre-trained ResNet-50 model is loaded and the final fully connected layer is adjusted to output two classes (cat and dog).
3. **Training Loop**: The model is trained using a standard training loop with stochastic gradient descent (SGD) and a learning rate scheduler.
4. **Model Evaluation**: The best model is selected based on validation accuracy and saved for inference.
### Training Code
Here is a simplified version of the training code:
2024-05-15 16:40:10 +08:00
[train.py](./train.py)
2024-05-15 16:34:04 +08:00
## Inference
To perform inference, you can use the following code. The inference is based on the transformer model with model ID `ailb/resnet-dogcat`.
### Inference Code
**You need set env HF_ENDPOINT=http://10.0.101.71**
```python
from transformers import AutoImageProcessor, ResNetForImageClassification
from PIL import Image
import requests
import torch
# Load model
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)
processor = AutoImageProcessor.from_pretrained("ailab/resnet-dogcat")
model = ResNetForImageClassification.from_pretrained("ailab/resnet-dogcat")
inputs = processor(image, return_tensors="pt")
with torch.no_grad():
logits = model(**inputs).logits
# model predicts one of the 1000 ImageNet classes
predicted_label = logits.argmax(-1).item()
print(model.config.id2label[predicted_label])
```
2024-05-27 14:20:01 +08:00
```python
2024-05-27 14:21:12 +08:00
from transformers import pipeline
2024-05-27 14:20:01 +08:00
import gradio as gr
pipe = pipeline("image-classification", model="ailab/resnet-dogcat")
gr.Interface.from_pipeline(pipe).launch(server_name="0.0.0.0")
```
2024-05-27 14:47:40 +08:00
*[{'label': 'cat', 'score': 0.5016139149665833}, {'label': 'dog', 'score': 0.49838611483573914}]*
2024-05-27 14:20:01 +08:00
![1716790696165.png](https://img2.imgtp.com/2024/05/27/hIlNpCRj.png)
2024-05-15 16:34:04 +08:00
## Conclusion
This repository provides a comprehensive solution for training and performing inference on a Cat-Dog classification task using a ResNet-50 model. The training script demonstrates how to preprocess data, train the model, and save the trained model. The inference script shows how to use the trained model to classify new images.