okeowo1014 commited on
Commit
c495e22
·
1 Parent(s): 92e4b52

add trainer

Browse files
Files changed (1) hide show
  1. trainer.py +76 -0
trainer.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pandas as pd
3
+ import os
4
+ from sklearn.metrics import classification_report
5
+ import seaborn as sn
6
+ from sklearn.utils import shuffle
7
+ import matplotlib.pyplot as plt
8
+ import cv2
9
+ import tensorflow as tf
10
+ from tqdm import tqdm
11
+
12
+ sn.set(font_scale=1.4)
13
+
14
+ class_names = ['buildings', 'forest', 'glacier', 'mountain', 'sea', 'street']
15
+ class_names_label = {class_name: i for i, class_name in enumerate(class_names)}
16
+ nb_classes = len(class_names)
17
+ print(class_names_label)
18
+ IMAGE_SIZE = (150, 150)
19
+
20
+
21
+ def load_data():
22
+ DIRECTORY = "C:/Users/Professional/PycharmProjects/intelimageclassifier/imgdataset"
23
+ CATEGORY = ["seg_train", "seg_test"]
24
+ output = []
25
+ for category in CATEGORY:
26
+ path = os.path.join(DIRECTORY, category)
27
+ images = []
28
+ labels = []
29
+ print("Loading {}".format(category))
30
+ for folder in os.listdir(path):
31
+ label = class_names_label[folder]
32
+ # Iterate through each image in our folder
33
+ for file in os.listdir(os.path.join(path, folder)):
34
+ # Get the path name of the image
35
+ img_path = os.path.join(os.path.join(path, folder), file)
36
+ # Open and resize the ing
37
+ image = cv2.imread(img_path)
38
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
39
+ image = cv2.resize(image, IMAGE_SIZE)
40
+ # Append the image and its corresponding Label to the output
41
+ images.append(image)
42
+ labels.append(label)
43
+ # Convert both the images and labels to a numpy array
44
+ images = np.array(images, dtype='float32')
45
+ labels = np.array(labels, dtype='int32')
46
+ output.append((images, labels))
47
+ return output
48
+
49
+
50
+ (train_images, train_labels), (test_images, test_labels) = load_data()
51
+ train_images, train_labels = shuffle(train_images, train_labels, random_state=25)
52
+ print("Train: ", train_images.shape, train_labels.shape)
53
+ print("Test: ", test_images.shape, test_labels.shape)
54
+
55
+ model = tf.keras.models.Sequential([
56
+ tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)),
57
+ tf.keras.layers.MaxPooling2D(2, 2),
58
+ tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
59
+ tf.keras.layers.MaxPooling2D(2, 2),
60
+ tf.keras.layers.Conv2D(128, (3, 3), activation='relu'),
61
+ tf.keras.layers.MaxPooling2D(2, 2),
62
+ tf.keras.layers.Conv2D(128, (3, 3), activation='relu'),
63
+ tf.keras.layers.MaxPooling2D(2, 2),
64
+ tf.keras.layers.Flatten(),
65
+ tf.keras.layers.Dense(512, activation='relu'),
66
+ tf.keras.layers.Dense(6, activation='softmax')
67
+ ])
68
+
69
+ model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
70
+ model.fit(train_images, train_labels, epochs=10, validation_split=0.1)
71
+
72
+ # Evaluate the model
73
+ model.evaluate(test_images, test_labels)
74
+
75
+ # save the model
76
+ model.save("model.h5")