code

code
mail@pastecode.io avatar
unknown
python
2 years ago
1.5 kB
4
Indexable
Never
#image_batch  = "malign.png"                               

classes = os.listdir("/content/drive/MyDrive/DDSM Dataset/")
matrix = {
    "Benign Masses1":0,         # bon
    "Malignant Masses1": 0,     # mauvais
    "Benign Masses2":0,         # mauvais
    "Malignant Masses2": 0      # bon
}
i, s = 0, 0

for classe in classes:
    i+=1
    path = f"drive/MyDrive/MIAS_Dataset/{classe}/"
    images = os.listdir(path)
    for image in images:
        img = tf.keras.utils.load_img(path+image, 
                                    target_size=(img_height, img_width))
        img_array = tf.keras.utils.img_to_array(img)
        img_array = tf.expand_dims(img_array, 0) # Create a batch

        predictions = model.predict(img_array)
        score = tf.nn.softmax(predictions[0])
        if class_names[np.argmax(score)] == classe:
            matrix[f"{classe}{i}"] += 1         # if the class is correct
        else:
            if i == 1 :
                matrix[f"Benign Masses{2}"] += 1     # if the class is not correct (i==1), then save in the other classe (i==2)
            else:
                matrix[f"Malignant Masses{1}"] += 1     # if the class is not correct (i==2), then save in the other classe (i==1)
        if s%100 == 0:
            print(f"{round(s*100/3816)}%")
        s +=1
        
        #print(
        #    "This image most likely belongs to {} with a {:.2f} percent confidence."
        #    .format(class_names[np.argmax(score)], 100 * np.max(score)))