Claudineuwa commited on
Commit
87bd492
·
verified ·
1 Parent(s): 59bbe96

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -1,35 +1,2 @@
1
- *.7z filter=lfs diff=lfs merge=lfs -text
2
- *.arrow filter=lfs diff=lfs merge=lfs -text
3
- *.bin filter=lfs diff=lfs merge=lfs -text
4
- *.bz2 filter=lfs diff=lfs merge=lfs -text
5
- *.ckpt filter=lfs diff=lfs merge=lfs -text
6
- *.ftz filter=lfs diff=lfs merge=lfs -text
7
- *.gz filter=lfs diff=lfs merge=lfs -text
8
- *.h5 filter=lfs diff=lfs merge=lfs -text
9
- *.joblib filter=lfs diff=lfs merge=lfs -text
10
- *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
- *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
- *.model filter=lfs diff=lfs merge=lfs -text
13
- *.msgpack filter=lfs diff=lfs merge=lfs -text
14
- *.npy filter=lfs diff=lfs merge=lfs -text
15
- *.npz filter=lfs diff=lfs merge=lfs -text
16
- *.onnx filter=lfs diff=lfs merge=lfs -text
17
- *.ot filter=lfs diff=lfs merge=lfs -text
18
- *.parquet filter=lfs diff=lfs merge=lfs -text
19
- *.pb filter=lfs diff=lfs merge=lfs -text
20
- *.pickle filter=lfs diff=lfs merge=lfs -text
21
- *.pkl filter=lfs diff=lfs merge=lfs -text
22
- *.pt filter=lfs diff=lfs merge=lfs -text
23
- *.pth filter=lfs diff=lfs merge=lfs -text
24
- *.rar filter=lfs diff=lfs merge=lfs -text
25
- *.safetensors filter=lfs diff=lfs merge=lfs -text
26
- saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
- *.tar.* filter=lfs diff=lfs merge=lfs -text
28
- *.tar filter=lfs diff=lfs merge=lfs -text
29
- *.tflite filter=lfs diff=lfs merge=lfs -text
30
- *.tgz filter=lfs diff=lfs merge=lfs -text
31
- *.wasm filter=lfs diff=lfs merge=lfs -text
32
- *.xz filter=lfs diff=lfs merge=lfs -text
33
- *.zip filter=lfs diff=lfs merge=lfs -text
34
- *.zst filter=lfs diff=lfs merge=lfs -text
35
- *tfevents* filter=lfs diff=lfs merge=lfs -text
 
1
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
2
+ *.pt filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
.gitignore ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ train.py
2
+ test.py
3
+ test.jpeg
4
+ vit_trainer_output
5
+ __pycache__
6
+ data
7
+ venv
README.md CHANGED
@@ -1,3 +1,65 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Waste Classifier AI
2
+
3
+ ## Dataset Structure
4
+
5
+ Place your dataset in a directory like:
6
+
7
+ ```
8
+ waste_dataset/
9
+ ├── biodegradable/
10
+ │ ├── img1.jpg
11
+ │ └── ...
12
+ └── non_biodegradable/
13
+ ├── img2.jpg
14
+ └── ...
15
+ ```
16
+
17
+ ## Training the Model
18
+
19
+ 1. Install dependencies:
20
+ ```
21
+ pip install -r requirements.txt
22
+ ```
23
+ 2. Run the training script:
24
+ ```
25
+ python train_waste_classifier.py
26
+ ```
27
+ This will save the model and processor to `models/waste_classifier_model/`.
28
+
29
+ ## Running the API
30
+
31
+ 1. Start the Flask API:
32
+ ```
33
+ python waste_classification_api.py
34
+ ```
35
+ 2. The API will be available at `http://localhost:5000/classify`.
36
+
37
+ ## API Usage
38
+
39
+ - **Endpoint:** `/classify` (POST)
40
+ - **Payload:** Multipart form with one or more images (field name: `images`)
41
+ - **Response:**
42
+ ```json
43
+ {
44
+ "results": [
45
+ {
46
+ "label": "biodegradable",
47
+ "confidence": 0.94,
48
+ "description": "Easily breaks down naturally. Good for composting.",
49
+ "recyclable": false,
50
+ "disposal": "Use compost or organic bin",
51
+ "example_items": ["banana peel", "food waste", "paper"]
52
+ },
53
+ ...
54
+ ]
55
+ }
56
+ ```
57
+
58
+ ## Frontend Integration
59
+
60
+ - The React Native frontend can POST images to `/classify` and display the results using the modal.
61
+ - No changes are needed to the modal if it expects the above JSON structure.
62
+
63
+ ## Notes
64
+ - If your dataset folders are named differently (e.g., `R` and `O`), update the LABEL2INFO mapping and class names in the training script.
65
+ - The model is based on `google/vit-base-patch16-224` and fine-tuned for binary classification.
download (5).jpeg ADDED
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ torch>=2.0.0
2
+ torchvision>=0.15.0
3
+ transformers>=4.30.0
4
+ datasets>=2.10.0
5
+ scikit-learn>=1.0.0
6
+ flask>=2.0.0
7
+ flask-cors>=3.0.10
8
+ pillow>=9.0.0
waste_classification_api.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ from flask_cors import CORS
3
+ from transformers import AutoImageProcessor, AutoModelForImageClassification
4
+ from PIL import Image
5
+ import torch
6
+ import io
7
+ import os
8
+ from pathlib import Path
9
+
10
+ app = Flask(__name__)
11
+ CORS(app)
12
+
13
+ MODEL_PATH = r"D:/Green_IQ/Green_IQ/AI/waste_classifier"
14
+
15
+ LABEL2INFO = {
16
+ 0: {
17
+ "label": "biodegradable",
18
+ "description": "Easily breaks down naturally. Good for composting.",
19
+ "recyclable": False,
20
+ "disposal": "Use compost or organic bin",
21
+ "example_items": ["banana peel", "food waste", "paper"],
22
+ "environmental_benefit": "Composting biodegradable waste returns nutrients to the soil, reduces landfill use, and lowers greenhouse gas emissions.",
23
+ "protection_tip": "Compost at home or use municipal organic waste bins. Avoid mixing with plastics or hazardous waste.",
24
+ "poor_disposal_effects": "If disposed of improperly, biodegradable waste can cause methane emissions in landfills and contribute to water pollution and eutrophication."
25
+ },
26
+ 1: {
27
+ "label": "non_biodegradable",
28
+ "description": "Does not break down easily. Should be disposed of carefully.",
29
+ "recyclable": False,
30
+ "disposal": "Use general waste bin or recycling if possible",
31
+ "example_items": ["plastic bag", "styrofoam", "metal can"],
32
+ "environmental_benefit": "Proper disposal and recycling of non-biodegradable waste reduces pollution, conserves resources, and protects wildlife.",
33
+ "protection_tip": "Reduce use, reuse items, and recycle whenever possible. Never burn or dump in nature.",
34
+ "poor_disposal_effects": "Improper disposal leads to soil and water pollution, harms wildlife, and causes long-term environmental damage. Plastics can persist for hundreds of years."
35
+ }
36
+ }
37
+
38
+ # Check if the model path exists
39
+ if not os.path.exists(MODEL_PATH):
40
+ raise FileNotFoundError(f"Model path does not exist: {MODEL_PATH}")
41
+
42
+ # Load model and processor with local_files_only=True
43
+ try:
44
+ model = AutoModelForImageClassification.from_pretrained(
45
+ MODEL_PATH,
46
+ local_files_only=True
47
+ )
48
+ image_processor = AutoImageProcessor.from_pretrained(
49
+ MODEL_PATH,
50
+ local_files_only=True
51
+ )
52
+ model.eval()
53
+ print("Model and processor loaded successfully!")
54
+ except Exception as e:
55
+ print(f"Error loading model: {e}")
56
+ raise
57
+
58
+ def predict_image(image_bytes, model, image_processor, device="cpu"):
59
+ image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
60
+ inputs = image_processor(images=image, return_tensors="pt")
61
+ inputs = {k: v.to(device) for k, v in inputs.items()}
62
+ with torch.no_grad():
63
+ outputs = model(**inputs)
64
+ probs = torch.softmax(outputs.logits, dim=1)
65
+ conf, pred = torch.max(probs, dim=1)
66
+ label_id = pred.item()
67
+ confidence = conf.item()
68
+ info = LABEL2INFO[label_id].copy()
69
+ info["confidence"] = round(confidence, 2)
70
+ info["eco_points_earned"] = 10 # Dummy value
71
+ return info
72
+
73
+ @app.route('/classify', methods=['POST'])
74
+ def classify():
75
+ results = []
76
+ files = request.files.getlist('images')
77
+ for file in files:
78
+ image_bytes = file.read()
79
+ result = predict_image(image_bytes, model, image_processor)
80
+ results.append(result)
81
+ return jsonify({"results": results})
82
+
83
+ if __name__ == '__main__':
84
+ app.run(debug=True, port=5000)
waste_classifier/config.json ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "ViTForImageClassification"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.0,
6
+ "encoder_stride": 16,
7
+ "hidden_act": "gelu",
8
+ "hidden_dropout_prob": 0.0,
9
+ "hidden_size": 768,
10
+ "id2label": {
11
+ "0": "biodegradable",
12
+ "1": "non_biodegradable"
13
+ },
14
+ "image_size": 224,
15
+ "initializer_range": 0.02,
16
+ "intermediate_size": 3072,
17
+ "label2id": {
18
+ "biodegradable": 0,
19
+ "non_biodegradable": 1
20
+ },
21
+ "layer_norm_eps": 1e-12,
22
+ "model_type": "vit",
23
+ "num_attention_heads": 12,
24
+ "num_channels": 3,
25
+ "num_hidden_layers": 12,
26
+ "patch_size": 16,
27
+ "pooler_act": "tanh",
28
+ "pooler_output_size": 768,
29
+ "problem_type": "single_label_classification",
30
+ "qkv_bias": true,
31
+ "torch_dtype": "float32",
32
+ "transformers_version": "4.53.2"
33
+ }
waste_classifier/model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:af8a93025faf3e4fd053b12c9825a286a22ec22f64a50a8f27728a73cd5c078b
3
+ size 343223968
waste_classifier/preprocessor_config.json ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "crop_size": null,
3
+ "data_format": "channels_first",
4
+ "default_to_square": true,
5
+ "device": null,
6
+ "disable_grouping": null,
7
+ "do_center_crop": null,
8
+ "do_convert_rgb": null,
9
+ "do_normalize": true,
10
+ "do_rescale": true,
11
+ "do_resize": true,
12
+ "image_mean": [
13
+ 0.5,
14
+ 0.5,
15
+ 0.5
16
+ ],
17
+ "image_processor_type": "ViTImageProcessorFast",
18
+ "image_std": [
19
+ 0.5,
20
+ 0.5,
21
+ 0.5
22
+ ],
23
+ "input_data_format": null,
24
+ "resample": 2,
25
+ "rescale_factor": 0.00392156862745098,
26
+ "return_tensors": null,
27
+ "size": {
28
+ "height": 224,
29
+ "width": 224
30
+ }
31
+ }