Arshul26 commited on
Commit
41223d1
·
verified ·
1 Parent(s): 370df5a

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. best_model.h5 +3 -0
  2. class_names.txt +45 -0
  3. prediction.py +26 -0
  4. train_resisc45.py +117 -0
best_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1758608733d41d3eb3874611d7638d2cdaca9068380b851b4b984e56787bb072
3
+ size 17620960
class_names.txt ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ airplane
2
+ airport
3
+ baseball_diamond
4
+ basketball_court
5
+ beach
6
+ bridge
7
+ chaparral
8
+ church
9
+ circular_farmland
10
+ cloud
11
+ commercial_area
12
+ dense_residential
13
+ desert
14
+ forest
15
+ freeway
16
+ golf_course
17
+ ground_track_field
18
+ harbor
19
+ industrial_area
20
+ intersection
21
+ island
22
+ lake
23
+ meadow
24
+ medium_residential
25
+ mobile_home_park
26
+ mountain
27
+ overpass
28
+ palace
29
+ parking_lot
30
+ railway
31
+ railway_station
32
+ rectangular_farmland
33
+ river
34
+ roundabout
35
+ runway
36
+ sea_ice
37
+ ship
38
+ snowberg
39
+ sparse_residential
40
+ stadium
41
+ storage_tank
42
+ tennis_court
43
+ terrace
44
+ thermal_power_station
45
+ wetland
prediction.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #Code for checking 1 sample image
2
+ import os
3
+ import numpy as np
4
+ from tensorflow.keras.preprocessing import image
5
+ from tensorflow.keras.models import load_model
6
+
7
+ # Set correct image size based on training
8
+ IMG_SIZE = (128, 128)
9
+
10
+ # Load your model
11
+ model = load_model("best_model.h5")
12
+
13
+ # Get class names from folders
14
+ DATA_DIR = r"C:\Users\arsul\Desktop\RESISC45\NWPU-RESISC45"
15
+ CLASS_NAMES = sorted(os.listdir(DATA_DIR))
16
+
17
+ # Load and preprocess the image
18
+ img_path = r"C:\Users\arsul\Desktop\RESISC45\ball-courts.jpg"
19
+ img = image.load_img(img_path, target_size=IMG_SIZE)
20
+ img_array = image.img_to_array(img)
21
+ img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
22
+
23
+ # Make prediction
24
+ preds = model.predict(img_array)
25
+ predicted_class = CLASS_NAMES[np.argmax(preds)]
26
+ print(f"Predicted class: {predicted_class}")
train_resisc45.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ from tensorflow.keras.applications import EfficientNetB0
3
+ from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout
4
+ from tensorflow.keras.models import Model
5
+ from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
6
+ import matplotlib.pyplot as plt
7
+ import os
8
+
9
+ # Paths
10
+ DATASET_DIR = r"C:\Path\To\NWPU-RESISC45" # Path to your Dataset
11
+ IMG_SIZE = (128, 128)
12
+ BATCH_SIZE = 16
13
+ NUM_CLASSES = 45
14
+ EPOCHS = 50
15
+ SEED = 42
16
+
17
+ # Sanity check: verify class folders
18
+ class_names = sorted(os.listdir(DATASET_DIR))
19
+ print(f"Detected {len(class_names)} classes:", class_names)
20
+
21
+ # Augmentation for training set
22
+ data_augmentation = tf.keras.Sequential([
23
+ tf.keras.layers.RandomFlip("horizontal_and_vertical"),
24
+ tf.keras.layers.RandomRotation(0.2),
25
+ tf.keras.layers.RandomZoom(0.1),
26
+ ])
27
+
28
+ # Load Datasets
29
+ train_ds = tf.keras.preprocessing.image_dataset_from_directory(
30
+ DATASET_DIR,
31
+ validation_split=0.2,
32
+ subset="training",
33
+ seed=SEED,
34
+ image_size=IMG_SIZE,
35
+ batch_size=BATCH_SIZE,
36
+ label_mode='categorical'
37
+ )
38
+
39
+ val_ds = tf.keras.preprocessing.image_dataset_from_directory(
40
+ DATASET_DIR,
41
+ validation_split=0.2,
42
+ subset="validation",
43
+ seed=SEED,
44
+ image_size=IMG_SIZE,
45
+ batch_size=BATCH_SIZE,
46
+ label_mode='categorical'
47
+ )
48
+
49
+ # Apply augmentation only to training
50
+ train_ds = train_ds.map(lambda x, y: (data_augmentation(x, training=True), y))
51
+
52
+ # Prefetch
53
+ AUTOTUNE = tf.data.AUTOTUNE
54
+ train_ds = train_ds.prefetch(buffer_size=AUTOTUNE)
55
+ val_ds = val_ds.prefetch(buffer_size=AUTOTUNE)
56
+
57
+ # Build Model
58
+ base_model = EfficientNetB0(weights='imagenet', include_top=False, input_shape=(*IMG_SIZE, 3))
59
+ base_model.trainable = False # Freeze base model initially
60
+
61
+ x = base_model.output
62
+ x = GlobalAveragePooling2D()(x)
63
+ x = Dropout(0.3)(x)
64
+ output = Dense(NUM_CLASSES, activation='softmax')(x)
65
+
66
+ model = Model(inputs=base_model.input, outputs=output)
67
+
68
+ model.compile(optimizer='adam',
69
+ loss='categorical_crossentropy',
70
+ metrics=['accuracy'])
71
+
72
+ model.summary()
73
+
74
+ # Callbacks
75
+ callbacks = [
76
+ ModelCheckpoint("best_model.h5", save_best_only=True, monitor="val_accuracy", mode="max"),
77
+ EarlyStopping(patience=10, restore_best_weights=True),
78
+ ReduceLROnPlateau(factor=0.2, patience=5, min_lr=1e-6)
79
+ ]
80
+
81
+ # Train
82
+ history = model.fit(
83
+ train_ds,
84
+ validation_data=val_ds,
85
+ epochs=EPOCHS,
86
+ callbacks=callbacks
87
+ )
88
+
89
+ # Optional: Fine-tune after warmup (unfreeze base model)
90
+ base_model.trainable = True
91
+ model.compile(optimizer=tf.keras.optimizers.Adam(1e-5), # Lower LR for fine-tuning
92
+ loss='categorical_crossentropy',
93
+ metrics=['accuracy'])
94
+
95
+ history_finetune = model.fit(
96
+ train_ds,
97
+ validation_data=val_ds,
98
+ epochs=10,
99
+ callbacks=callbacks
100
+ )
101
+
102
+ # Plotting
103
+ plt.plot(history.history["accuracy"] + history_finetune.history["accuracy"], label="train acc")
104
+ plt.plot(history.history["val_accuracy"] + history_finetune.history["val_accuracy"], label="val acc")
105
+ plt.xlabel("Epochs")
106
+ plt.ylabel("Accuracy")
107
+ plt.legend()
108
+ plt.title("Training vs Validation Accuracy")
109
+ plt.grid()
110
+ plt.savefig("training_accuracy.png")
111
+ plt.show()
112
+
113
+ # Save class names
114
+ with open("class_names.txt", "w") as f:
115
+ for name in class_names:
116
+ f.write(name + "\n")
117
+