Spaces:
Sleeping
Sleeping
Download train.py from kshitiz14/breedclassification: direct link, hf CLI and curl.
- Browser
- Download file 2.01 kB
-
https://huggingface.co/spaces/kshitiz14/breedclassification/resolve/main/train.py
- Command line
-
hf download hf://spaces/kshitiz14/breedclassification/train.py
-
curl -L -o train.py https://huggingface.co/spaces/kshitiz14/breedclassification/resolve/main/train.py
2.01 kB
| import tensorflow as tf | |
| from tensorflow.keras.applications import MobileNetV2 | |
| from tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout, BatchNormalization | |
| from tensorflow.keras.models import Model | |
| from tensorflow.keras.optimizers import Adam | |
| from tensorflow.keras.preprocessing.image import ImageDataGenerator | |
| from tensorflow.keras.applications import MobileNetV2 | |
| from tensorflow.keras import layers, models | |
| import os | |
| # ----------------------------- | |
| # Step 1: Data Preprocessing | |
| # ----------------------------- | |
| IMG_SIZE = (224, 224) | |
| BATCH_SIZE = 32 | |
| train_datagen = ImageDataGenerator( | |
| rescale=1./255, | |
| rotation_range=20, | |
| zoom_range=0.2, | |
| horizontal_flip=True, | |
| validation_split=0.2 | |
| ) | |
| val_datagen = ImageDataGenerator(rescale=1./255) | |
| train_generator = train_datagen.flow_from_directory( | |
| "d:/SIH/images", | |
| target_size=IMG_SIZE, | |
| batch_size=BATCH_SIZE, | |
| class_mode="categorical" | |
| ) | |
| val_generator = val_datagen.flow_from_directory( | |
| "d:/SIH/images", | |
| target_size=IMG_SIZE, | |
| batch_size=BATCH_SIZE, | |
| class_mode="categorical" | |
| ) | |
| # ----------------------------- | |
| # Step 2: Model Building (Transfer Learning) | |
| # ----------------------------- | |
| base_model = MobileNetV2(weights="imagenet", include_top=False, input_shape=(224,224,3)) | |
| base_model.trainable = False # freeze base model | |
| model = models.Sequential([ | |
| base_model, | |
| layers.GlobalAveragePooling2D(), | |
| layers.Dropout(0.3), | |
| layers.Dense(len(train_generator.class_indices), activation="softmax") | |
| ]) | |
| model.compile(optimizer="adam", | |
| loss="categorical_crossentropy", | |
| metrics=["accuracy"]) | |
| # ----------------------------- | |
| # Step 3: Training | |
| # ----------------------------- | |
| EPOCHS = 80 | |
| history = model.fit( | |
| train_generator, | |
| validation_data=val_generator, | |
| epochs=EPOCHS | |
| ) | |
| # ----------------------------- | |
| # Step 4: Save Model | |
| # ----------------------------- | |
| model.save("my_image_model.h5") | |
| print("✅ Training complete. Model saved as my_image_model.h5") | |