Spaces:
Runtime error
Runtime error
| import tensorflow as tf | |
| from datasets import load_dataset | |
| from tensorflow.keras.preprocessing import image | |
| import numpy as np | |
| from sklearn.model_selection import train_test_split | |
| # Load the HAM10000 dataset using Hugging Face datasets library | |
| dataset = load_dataset("Nagabu/HAM10000") | |
| # Preprocess and prepare the dataset | |
| def preprocess_image(img_path, target_size=(224, 224)): | |
| img = image.load_img(img_path, target_size=target_size) | |
| img_array = image.img_to_array(img) | |
| img_array = np.expand_dims(img_array, axis=0) / 255.0 # Normalize pixel values | |
| return img_array | |
| def load_model(): | |
| model = tf.keras.models.load_model('skin_cancer_model.h5') | |
| return model | |
| def prepare_data(): | |
| # Assuming you have a directory of images and their corresponding labels | |
| # Create the dataset | |
| images = [] | |
| labels = [] | |
| # Extract image paths and labels from the dataset | |
| for example in dataset['train']: | |
| images.append(example['image']) # Path to the image | |
| labels.append(example['dx']) # The label (skin cancer class) | |
| # Convert lists to numpy arrays | |
| images = np.array(images) | |
| labels = np.array(labels) | |
| # Split into train and test sets | |
| x_train, x_test, y_train, y_test = train_test_split(images, labels, test_size=0.2, random_state=42) | |
| return x_train, x_test, y_train, y_test | |
| # Simple CNN Model | |
| def build_model(): | |
| model = tf.keras.models.Sequential([ | |
| tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 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.Flatten(), | |
| tf.keras.layers.Dense(128, activation='relu'), | |
| tf.keras.layers.Dense(7, activation='softmax') # 7 classes for skin cancer types | |
| ]) | |
| model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) | |
| return model | |