Data Source
def get_array (path) :
X = []
y = []
for label, disease in enumerate(os.listdir(path)) :
# resize and append
for count, image in enumerate(os.listdir(os.path.join(path, disease))) :
X.append(img_to_array(
fromarray(
cv2.resize(
cv2.imread(
os.path.join(os.path.join(path, disease),image)),(128,128)))))
# only 5k imgs
y.append(label)
if count > 5e3 :
break
X = np.array(X)
y = np.array(y)
return X/255.0, to_categorical(y)
X_train, y_train = get_array(paths[0])
X_test , y_test = get_array(paths[1])
X_valid, y_valid = get_array(paths[2])
plt.figure(figsize = (20,20))
i = 0
while i < 16 :
x = np.random.randint(0, 20000)
plt.subplot(4, 4, i+1)
plt.imshow(X_train[x])
plt.axis('off')
plt.title(info[np.argmax(y_train[x])])
i += 1
plt.show()
def conv_layer (filterx) :
model = Sequential()
model.add(Conv2D(filterx, (4,4), padding = 'same', use_bias = False))
model.add(BatchNormalization())
model.add(Dropout(.2))
model.add(LeakyReLU())
model.add(MaxPooling2D((2, 2)))
return model
def dens_layer (hiddenx) :
model = Sequential()
model.add(Dense(hiddenx, kernel_regularizer = 'l2'))
model.add(BatchNormalization())
model.add(Dropout(.2))
model.add(LeakyReLU())
return model
Conv2D -
Convolution layers are the feature detectors for images. The filters move across the width and height of the image performing convolution operations and storing the values.
MaxPooling2D -
Pooling layer with mode = ‘max’ is used to reduce the image and retain the dominant features in the region of the filter applied.
BatchNormalization -
Normalize the hidden features to have zero mean and unit standard deviation. It helps in faster convergence and prevents internal covariate shift.
def cnn (filter1, filter2, filter3, hidden1) :
model = Sequential([
Input((128,128,3,)),
conv_layer(filter1),
conv_layer(filter2),
conv_layer(filter3),
Flatten(),
dens_layer(hidden1),
Dense(4, activation = 'softmax')
])
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
return model