-
Notifications
You must be signed in to change notification settings - Fork 0
/
training_code.py
158 lines (124 loc) · 4.53 KB
/
training_code.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
from tqdm import tqdm
# Import Warnings
import warnings
warnings.filterwarnings('ignore')
import pandas as pd
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
# Import tensorflow as the backend for Keras
from keras import backend as K
K.set_image_dim_ordering('tf')
from keras.utils import np_utils
from keras.callbacks import ModelCheckpoint
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.optimizers import SGD,RMSprop,Adam
from keras.callbacks import TensorBoard
# Import required libraries for cnfusion matrix
from sklearn.metrics import classification_report,confusion_matrix
import itertools
"""=== Penyusunan dan Pengambilan Data ==="""
PATH = os.getcwd()
#Define data path
data_path = PATH + '/image vocal/main data'
data_dir_list = os.listdir(data_path)
data_dir_list
img_rows=128
img_cols=128
num_channel=1
num_epoch=20
# Define the number of classes
num_classes = 7
labels_name={'cyst':0, 'granuloma':1, 'nodule':2, 'none': 3, 'normal':4, 'papiloma':5, 'paralysis':6}
img_data_list= []
labels_list = []
for dataset in data_dir_list:
img_list=os.listdir(data_path+'/'+ dataset)
print ('Loaded the images of dataset-'+'{}\n'.format(dataset))
label = labels_name[dataset]
for img in tqdm(img_list):
input_img = cv2.imread(data_path + '/'+ dataset + '/'+ img)
input_img = cv2.cvtColor(input_img, cv2.COLOR_BGR2GRAY)
input_img_resize = cv2.resize(input_img,(img_rows,img_cols))
img_data_list.append(input_img_resize)
labels_list.append(label)
img_data = np.array(img_data_list)
img_data = img_data.astype('float32')
img_data /= 255
print (img_data.shape)
plt.imshow(img_data[1111,:])
plt.show()
labels = np.array(labels_list)
# print the count of number of samples for different classes
print(np.unique(labels,return_counts=True))
if num_channel==1:
if K.image_dim_ordering()=='th':
img_data= np.expand_dims(img_data, axis=1)
print (img_data.shape)
else:
img_data= np.expand_dims(img_data, axis=4)
print (img_data.shape)
else:
if K.image_dim_ordering()=='th':
img_data=np.rollaxis(img_data,3,1)
print (img_data.shape)
# one-hot encoding
Y = np_utils.to_categorical(labels, num_classes)
# Shuffle the dataset
x,y = shuffle(img_data,Y, random_state=2)
# Split the dataset
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=2)
print("X_train shape = {}".format(X_train.shape))
print("X_test shape = {}".format(X_test.shape))
print("y_train shape = {}".format(y_train.shape))
print("y_test shape = {}".format(y_test.shape))
print("img data shape = {}".format(img_data[0].shape))
"""==== PENGATURAN AUGMENTASI DATA ===="""
from keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.2,
shear_range=0.1,
horizontal_flip=True,
vertical_flip=True,
rotation_range=30,
fill_mode = "nearest")
datagen.fit(X_train)
batches = datagen.flow(X_train, y_train, batch_size=20)
print(batches)
X_batch, y_batch = next(batches)
fig, axs = plt.subplots(1, 15, figsize=(20, 5))
fig.tight_layout()
for i in range(15):
axs[i].imshow(X_batch[i].reshape(128,128), cmap="gray")
axs[i].axis('off')
#Visualize some images
image = X_test[22, :].reshape((128,128))
plt.imshow(image)
plt.show()
"""===== inisialisasi model CNN ====="""
#Initialising the input shape
input_shape=img_data[0].shape
# Design the CNN Sequential model
def leNet_model():
model = Sequential()
model.add(Conv2D(25, (3,3), input_shape =input_shape, padding='same', strides=1, activation='relu'))
model.add(MaxPooling2D(pool_size=(3,3)))
model.add(Conv2D(20,(3,3), padding='same', strides=1, activation='relu'))
model.add(MaxPooling2D(pool_size=(3,3)))
model.add(Conv2D(10,(3,3), padding='valid', strides=1, activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(37, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(Adam(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
return model
model = leNet_model()