Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import numpy as np | |
| from skimage.transform import resize | |
| from tensorflow.keras.models import Sequential, load_model | |
| from tensorflow.keras.layers import Conv2D, MaxPool2D, Dropout, Dense, Flatten, BatchNormalization | |
| class SkinCancer : | |
| def __init__ (self): | |
| self.model = self.load_model() | |
| def build_model (self) : | |
| model = Sequential() | |
| model.add(Conv2D(filters = 128, kernel_size = (4,4), input_shape = (32, 32, 3), activation = 'relu')) | |
| model.add(MaxPool2D(pool_size = (4,4))) | |
| model.add(Conv2D(filters = 64, kernel_size = (2,2), activation = 'relu')) | |
| model.add(MaxPool2D(pool_size = (2,2))) | |
| model.add(BatchNormalization()) | |
| #model.add(GlobalAveragePooling2D()) | |
| model.add(Flatten()) | |
| model.add(Dense(128, activation = 'relu')) | |
| model.add(Dropout(0.2)) | |
| model.add(Dense(2, activation = 'sigmoid')) # sigmoid is better for binary classification | |
| #model.summary() | |
| return model | |
| def load_model(self): | |
| model = self.build_model() | |
| model = load_model("Normal_skin_cancer_model.h5") | |
| return model | |
| def preprocess_image(self,img): | |
| img = resize(img, (32,32)) | |
| img = img.reshape(1,32,32,3) | |
| return img | |
| def predict(self,img): | |
| real_labels = ["benign", "malignant"] | |
| img = self.preprocess_image(img) | |
| res = np.argmax(self.model.predict(img)) | |
| return real_labels[res] | |
| def Test(img): | |
| model_new = SkinCancer() | |
| res = model_new.predict(img) | |
| return res | |
| #interface | |
| interface = gr.Interface(fn = Test, | |
| inputs = gr.inputs.Image(shape=(200,200)), | |
| outputs=["text"], | |
| title="Skin Cancer detection") | |
| interface.launch() |