Views
No views yet
1from huggingface_hub import hf_hub_download
2import tensorflow as tf
3import cv2
4import numpy as np
5import json
6import matplotlib.pyplot as plt
7
8# Load model
9repo_id = "maiurilorenzo/CBIS-DDSM-CNN"
10model_path = hf_hub_download(repo_id=repo_id, filename="CNN_model.h5")
11model = tf.keras.models.load_model(model_path)
12
13# Load preprocessing info
14preprocessing_path = hf_hub_download(repo_id=repo_id, filename="preprocessing.json")
15with open(preprocessing_path, "r") as f:
16 preprocessing_info = json.load(f)
17
18# Define preprocessing function
19def load_and_preprocess_image(image_path):
20 try:
21 img = cv2.imread(image_path, cv2.IMREAD_COLOR)
22 if img is None:
23 raise ValueError(f"Could not read image: {image_path}")
24
25 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
26 img = cv2.resize(img, tuple(preprocessing_info["target_size"]), interpolation=cv2.INTER_AREA)
27 img_array = img.astype(np.float32) / 255.0
28
29 return img_array
30 except Exception as e:
31 print(f"Error processing {image_path}: {str(e)}")
32 return None
33
34# Load and preprocess an example image
35image_path = "/kaggle/input/miniddsm2/MINI-DDSM-Complete-JPEG-8/Benign/0029/C_0029_1.LEFT_CC.jpg"
36img_array = load_and_preprocess_image(image_path)
37
38if img_array is not None:
39 img_batch = np.expand_dims(img_array, axis=0)
40 predictions = model.predict(img_batch)
41
42 cancer_probability = predictions[0][0] # Assuming "Cancer" is the first class
43 predicted_class = "Cancer" if cancer_probability >= 0.5 else "Normal"
44
45 plt.imshow(img_array)
46 plt.title(f'Predicted Class: {predicted_class}\nProbability of Cancer: {cancer_probability:.4f}')
47 plt.axis('off')
48 plt.show()
49else:
50 print("Image loading and preprocessing failed.")@misc{CBIS-DDSM-CNN,
author = {Lorenzo Maiuri},
title = {CBIS-DDSM-CNN},
year = {2025},
publisher = {Hugging Face Hub},
license = {MIT}
}