okeowo1014 commited on
Commit
1b0b78e
·
1 Parent(s): a12441a

Create trainer2.py

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