isnjb27 commited on
Commit
fc09124
·
verified ·
1 Parent(s): 13d8ab9

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +94 -0
main.py CHANGED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from tensorflow.keras.utils import image_dataset_from_directory
2
+ from tensorflow.keras import Sequential
3
+ from tensorflow.keras.layers import (
4
+ Dense,
5
+ Conv2D,
6
+ MaxPooling2D,
7
+ Flatten,
8
+ BatchNormalization,
9
+ Dropout,
10
+ )
11
+ from utils import plot_accuracy, plot_loss, process
12
+ import numpy as np
13
+ from sklearn.metrics import classification_report
14
+
15
+ train_ds = image_dataset_from_directory(
16
+ directory="sports-classification/train",
17
+ labels="inferred",
18
+ label_mode="int",
19
+ batch_size=64,
20
+ image_size=(256, 256),
21
+ )
22
+
23
+ validation_ds = image_dataset_from_directory(
24
+ directory="sports-classification/valid",
25
+ labels="inferred",
26
+ label_mode="int",
27
+ batch_size=64,
28
+ image_size=(256, 256),
29
+ )
30
+
31
+ test_ds = image_dataset_from_directory(
32
+ directory="sports-classification/test",
33
+ labels="inferred",
34
+ label_mode="int",
35
+ batch_size=64,
36
+ image_size=(256, 256),
37
+ )
38
+
39
+
40
+ train_ds = train_ds.map(process)
41
+ validation_ds = validation_ds.map(process)
42
+ test_ds = test_ds.map(process)
43
+
44
+ model = Sequential()
45
+ model.add(
46
+ Conv2D(
47
+ 128,
48
+ kernel_size=(3, 3),
49
+ padding="valid",
50
+ activation="leaky_relu",
51
+ input_shape=(256, 256, 3),
52
+ )
53
+ )
54
+ model.add(BatchNormalization())
55
+ model.add(MaxPooling2D(pool_size=(2, 2), strides=2, padding="valid"))
56
+ model.add(Conv2D(64, kernel_size=(3, 3), padding="valid", activation="leaky_relu"))
57
+ model.add(BatchNormalization())
58
+ model.add(MaxPooling2D(pool_size=(2, 2), strides=2, padding="valid"))
59
+ model.add(Conv2D(32, kernel_size=(3, 3), padding="valid", activation="leaky_relu"))
60
+ model.add(BatchNormalization())
61
+ model.add(MaxPooling2D(pool_size=(2, 2), strides=2, padding="valid"))
62
+ model.add(Flatten())
63
+ model.add(Dense(512, activation="leaky_relu"))
64
+ model.add(Dropout(0.1))
65
+ model.add(Dense(256, activation="leaky_relu"))
66
+ model.add(Dropout(0.1))
67
+ model.add(Dense(128, activation="leaky_relu"))
68
+ model.add(Dropout(0.1))
69
+ model.add(Dense(100, activation="softmax"))
70
+
71
+
72
+ model.compile(
73
+ optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"]
74
+ )
75
+
76
+ callback = tf.keras.callbacks.EarlyStopping(monitor="loss", patience=2)
77
+ history = model.fit(
78
+ train_ds,
79
+ epochs=50,
80
+ batch_size=32,
81
+ callbacks=[callback],
82
+ validation_data=validation_ds,
83
+ )
84
+
85
+ plot_loss(history)
86
+ plot_accuracy(history)
87
+
88
+ y_pred = np.array([])
89
+ y_true = np.array([])
90
+ for x, y in test_ds:
91
+ y_pred = np.concatenate([y_pred, model.predict_classes(x)])
92
+ y_true = np.concatenate([y_true, np.argmax(y.numpy(), axis=-1)])
93
+
94
+ print("Classification Report: \n", classification_report(y_pred, y_true))