-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcardio Classification.py
65 lines (48 loc) · 1.89 KB
/
cardio Classification.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)
train_generator = train_datagen.flow_from_directory(
'D:\\Cardio cnn\\data\\train',
target_size=(64, 64),
batch_size=32,
class_mode='binary',
shuffle=True
)
test_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)
test_generator = test_datagen.flow_from_directory(
'D:\\Cardio cnn\\data\\test',
target_size=(64, 64),
batch_size=32,
class_mode='binary'
)
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 3)),
MaxPooling2D(2, 2),
Conv2D(32, (3, 3), activation='relu'),
MaxPooling2D(2, 2),
Flatten(),
Dense(units=128, kernel_initializer='glorot_uniform', activation='relu'),
Dense(units=2, kernel_initializer='glorot_uniform', activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(
train_generator,
epochs=9,
validation_data=test_generator,
validation_steps=len(test_generator)
)
loss, accuracy = model.evaluate(test_generator)
from keras.models import load_model
from keras.preprocessing import image
import numpy as np
img = image.load_img("D:\\Cardio cnn\\data\\use to Prediction\\Normal.png", target_size=(64, 64)) #"D:\\Cardio cnn\\data\\use to Prediction\\Normal.png" "D:\\Cardio cnn\\data\\use to Prediction\\lb.png"
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
pred = model.predict(x)
y_pred = (pred > 0.5).astype(int)
index = ['Normal', 'left Bundle Branch block']
result = index[y_pred[0][0]]
print("Predicted Class:", result)