File size: 4,430 Bytes
2417fa0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# Installation Guide for FoodViT

## Prerequisites

- Python 3.8 or higher
- pip package manager
- At least 4GB RAM (8GB recommended)
- GPU support optional but recommended for faster inference

## Installation Steps

### 1. Clone or Download the Project

Make sure you have all the project files in your directory:
- `app.py` - Main application
- `predict.py` - Command line tool
- `config.py` - Configuration
- `requirements.txt` - Dependencies
- `model/bestViT_PT.pth` - Trained model
- All utility and interface files

### 2. Create a Virtual Environment (Recommended)

```bash

# Create virtual environment

python -m venv foodvit_env



# Activate virtual environment

# On Windows:

foodvit_env\Scripts\activate

# On macOS/Linux:

source foodvit_env/bin/activate

```

### 3. Install Dependencies

```bash

# Install PyTorch first (choose appropriate version for your system)

# For CPU only:

pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu



# For CUDA (if you have NVIDIA GPU):

# pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118



# Install other dependencies

pip install -r requirements.txt

```

### 4. Troubleshooting Dependency Issues

If you encounter dependency conflicts, try this step-by-step approach:

```bash

# 1. Install core dependencies first

pip install torch torchvision

pip install transformers==4.28.0

pip install huggingface-hub==0.15.1

pip install accelerate==0.20.3



# 2. Install image processing libraries

pip install Pillow opencv-python albumentations



# 3. Install Gradio

pip install gradio==3.35.2



# 4. Install other utilities

pip install numpy scikit-learn datasets

```

### 5. Alternative: Use Conda

If you prefer conda:

```bash

# Create conda environment

conda create -n foodvit python=3.9

conda activate foodvit



# Install PyTorch

conda install pytorch torchvision -c pytorch



# Install other packages

pip install transformers==4.28.0 huggingface-hub==0.15.1

pip install gradio==3.35.2

pip install -r requirements.txt

```

## Testing the Installation

### 1. Run Basic Tests

```bash

python simple_test.py

```

This should show all tests passing.

### 2. Test the Web Interface

```bash

python app.py

```

Then open your browser to `http://localhost:7860`

### 3. Test Command Line Tool

```bash

# Test help

python predict.py --help



# Test with a sample image (if you have one)

python predict.py path/to/your/image.jpg

```

## Common Issues and Solutions

### Issue: "cannot import name 'split_torch_state_dict_into_shards'"



**Solution**: This is a version compatibility issue. Try:



```bash

pip uninstall huggingface-hub transformers accelerate

pip install huggingface-hub==0.15.1 transformers==4.28.0 accelerate==0.20.3

```



### Issue: CUDA/GPU not working



**Solution**: 

1. Check if you have NVIDIA GPU

2. Install appropriate CUDA version

3. Install PyTorch with CUDA support

4. Or set device to 'cpu' in `config.py`



### Issue: Model file not found



**Solution**: Ensure `model/bestViT_PT.pth` exists in the project directory.

### Issue: Memory errors

**Solution**: 
1. Close other applications
2. Use CPU instead of GPU
3. Reduce batch size in configuration

## System Requirements

### Minimum Requirements
- Python 3.8+
- 4GB RAM
- 500MB disk space

### Recommended Requirements
- Python 3.9+
- 8GB RAM
- NVIDIA GPU with CUDA support
- 1GB disk space

## Verification

After successful installation, you should be able to:

1. ✅ Run `python simple_test.py` without errors
2. ✅ Start the web interface with `python app.py`
3. ✅ Use command line tool with `python predict.py --help`
4. ✅ Upload images and get predictions in the web interface

## Getting Help

If you encounter issues:

1. Check the error messages carefully
2. Ensure all dependencies are installed correctly
3. Try the troubleshooting steps above
4. Check if your Python version is compatible
5. Verify the model file exists and is not corrupted

## Next Steps

Once installation is complete:

1. **Web Interface**: Run `python app.py` and visit `http://localhost:7860`
2. **Command Line**: Use `python predict.py` for batch processing
3. **Customization**: Edit `config.py` to modify settings
4. **Development**: Use the modular structure for extending functionality