from huggingface_hub import push_to_hub_keras import numpy as np import pandas as pd import os from sklearn.metrics import classification_report import seaborn as sn from sklearn.utils import shuffle import matplotlib.pyplot as plt import cv2 import tensorflow as tf from tqdm import tqdm sac=os.getenv('accesstoken') sn.set(font_scale=1.4) class_names = ['buildings', 'forest', 'glacier', 'mountain', 'sea', 'street'] class_names_label = {class_name: i for i, class_name in enumerate(class_names)} nb_classes = len(class_names) print(class_names_label) IMAGE_SIZE = (150, 150) def load_data(): DIRECTORY = "imgdataset" CATEGORY = ["seg_train", "seg_test"] output = [] for category in CATEGORY: path = os.path.join(DIRECTORY, category) images = [] labels = [] print("Loading {}".format(category)) for folder in os.listdir(path): label = class_names_label[folder] # Iterate through each image in our folder for file in os.listdir(os.path.join(path, folder)): # Get the path name of the image img_path = os.path.join(os.path.join(path, folder), file) # Open and resize the ing image = cv2.imread(img_path) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = cv2.resize(image, IMAGE_SIZE) # Append the image and its corresponding Label to the output images.append(image) labels.append(label) # Convert both the images and labels to a numpy array images = np.array(images, dtype='float32') labels = np.array(labels, dtype='int32') output.append((images, labels)) return output (train_images, train_labels), (test_images, test_labels) = load_data() train_images, train_labels = shuffle(train_images, train_labels, random_state=25) print("Train: ", train_images.shape, train_labels.shape) print("Test: ", test_images.shape, test_labels.shape) model = tf.keras.models.Sequential([ tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)), tf.keras.layers.MaxPooling2D(2, 2), tf.keras.layers.Conv2D(64, (3, 3), activation='relu'), tf.keras.layers.MaxPooling2D(2, 2), tf.keras.layers.Conv2D(128, (3, 3), activation='relu'), tf.keras.layers.MaxPooling2D(2, 2), tf.keras.layers.Conv2D(128, (3, 3), activation='relu'), tf.keras.layers.MaxPooling2D(2, 2), tf.keras.layers.Flatten(), tf.keras.layers.Dense(512, activation='relu'), tf.keras.layers.Dense(6, activation='softmax') ]) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(train_images, train_labels, epochs=10, validation_split=0.1) # Evaluate the model model.evaluate(test_images, test_labels) # save the model model.save("model.keras") #from transformers import push_to_hub_keras # Save the model #model.save("model.keras") # Upload the model to your Hugging Face space repository push_to_hub_keras( model, repo_id="okeowo1014/imgclassifiertrainingsample", commit_message="Optional commit message", tags=["image-classifier", "some_other_tag"], include_optimizer=True,token=sac )