-
Notifications
You must be signed in to change notification settings - Fork 0
/
vgg19_ct.py
90 lines (68 loc) · 2.96 KB
/
vgg19_ct.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# -*- coding: utf-8 -*-
"""VGG19_ct.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1kxgR7cmluctCIMesitVTHV5ps1ghcRtS
"""
from google.colab import drive
drive.mount('/content/drive')
# Installing Keras
# pip install --upgrade keras
# Part 1 - Building the CNN
# Importing the Keras libraries and packages
import tensorflow as tf
from tensorflow.keras.models import Model
import tensorflow as tf
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization, AveragePooling2D, GlobalAveragePooling2D
# Initialising the CNN
classifier_base = tf.keras.applications.VGG19(input_shape = (64,64,3),
include_top=False, weights = "imagenet"
)
# Compiling the CNN
classifier_base.trainable = False
classifier_base.summary()
x = classifier_base.output
x = GlobalAveragePooling2D()(x)
x = BatchNormalization()(x)
x = Dropout(0.5)(x)
predictions = Dense(1, activation = 'sigmoid')(x)
classifier = Model(classifier_base.input,predictions)
classifier.summary()
# Compiling the CNN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
tf.keras.utils.plot_model(
classifier, to_file='model.png', show_shapes=False, show_layer_names=True,
rankdir='TB', expand_nested=False, dpi=96
)
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('/content/drive/My Drive/covid-dataset/training_set',
target_size = (64, 64),
batch_size = 32,
class_mode = 'binary')
test_set = test_datagen.flow_from_directory('/content/drive/My Drive/covid-dataset/test_set',
target_size = (64, 64),
batch_size = 32,
class_mode = 'binary')
from keras.callbacks import EarlyStopping
#early = EarlyStopping(monitor='val_accuracy', min_delta=0, patience=20, verbose=1, mode='auto')
#,callbacks = early
classifier.fit(training_set,
epochs = 10,
validation_data = test_set)
classifier.save("vgg19.h5")
import matplotlib.pyplot as plt
plt.plot(classifier.history.history['accuracy'])
plt.plot(classifier.history.history['val_accuracy'])
plt.plot(classifier.history.history['loss'])
plt.plot(classifier.history.history['val_loss'])
plt.title("model accuracy")
plt.ylabel("Accuracy")
plt.xlabel("Epoch")
plt.legend(["Accuracy","Validation Accuracy","loss","Validation Loss"])
plt.show()