okeowo1014/intelimageclassifier
0
1from huggingface_hub import push_to_hub_keras2import numpy as np3import pandas as pd4import os5from sklearn.metrics import classification_report6import seaborn as sn7from sklearn.utils import shuffle8import matplotlib.pyplot as plt9import cv210import tensorflow as tf11from tqdm import tqdm12sac=os.getenv('accesstoken')13sn.set(font_scale=1.4)14 15class_names = ['buildings', 'forest', 'glacier', 'mountain', 'sea', 'street']16class_names_label = {class_name: i for i, class_name in enumerate(class_names)}17nb_classes = len(class_names)18print(class_names_label)19IMAGE_SIZE = (150, 150)20 21 22def 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 folder34 for file in os.listdir(os.path.join(path, folder)):35 # Get the path name of the image36 img_path = os.path.join(os.path.join(path, folder), file)37 # Open and resize the ing38 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 output42 images.append(image)43 labels.append(label)44 # Convert both the images and labels to a numpy array45 images = np.array(images, dtype='float32')46 labels = np.array(labels, dtype='int32')47 output.append((images, labels))48 return output49 50 51(train_images, train_labels), (test_images, test_labels) = load_data()52train_images, train_labels = shuffle(train_images, train_labels, random_state=25)53print("Train: ", train_images.shape, train_labels.shape)54print("Test: ", test_images.shape, test_labels.shape)55 56model = 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 70model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])71model.fit(train_images, train_labels, epochs=10, validation_split=0.1)72 73# Evaluate the model74model.evaluate(test_images, test_labels)75 76# save the model77model.save("model.keras")78#from transformers import push_to_hub_keras79 80# Save the model81#model.save("model.keras")82 83# Upload the model to your Hugging Face space repository84push_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=sac90)91 92 93 