I am currently trying to implement a Bagnet on keras following this article :https://www.lyrn.ai/2019/02/14/bagnet-imagenet-with-a-simple-bof-model/
I am building one model to evaluate a patch and another to evaluate the averaging of all my patchs. And I try to ensemble them with an input: (number of patchs,x_patch,y_patch,channel_patch). But it isnt working well. My main issues come from evaluating all of the patchs and average the result.
Here is my code. If anyone has suggestion on how to deal with that.
def build_patch_model(lr, l2,patch_number):
##############
# BRANCH MODEL
##############
regul = regularizers.l2(l2)
optim = Adam(lr=lr)
kwargs = {'padding':'same', 'kernel_regularizer':regul}
inp = Input(shape=patch_size) # patch qxqx3
#x = MaxPooling2D((2, 2), strides=(2, 2))(inp) # 24x24x128
#x = BatchNormalization()(x)
x = Conv2D(32, (1,1), activation='relu', **kwargs)(inp)
for _ in range(6): x = subblock(x, 32, **kwargs)
x = MaxPooling2D((2, 2), strides=(2, 2))(x) # 24x24x128
x = BatchNormalization()(x)
x = Conv2D(64, (1,1), activation='relu', **kwargs)(x)
for _ in range(6): x = subblock(x, 64, **kwargs)
x = MaxPooling2D((2, 2), strides=(2, 2))(x) # 24x24x128
x = BatchNormalization()(x)
x = Conv2D(128, (1,1), activation='relu', **kwargs)(x)
for _ in range(6): x = subblock(x, 128, **kwargs)
#x = MaxPooling2D((2, 2), strides=(2, 2))(x) # 24x24x128
#x = BatchNormalization()(x)
x = Conv2D(256, (1,1), activation='relu', **kwargs)(x)
for _ in range(6): x = subblock(x, 256, **kwargs)
x = MaxPooling2D((2, 2), strides=(2, 2))(x) # 24x24x128
x = BatchNormalization()(x)
x = Conv2D(512, (1,1), activation='relu', **kwargs)(x)
for _ in range(6): x = subblock(x, 512, **kwargs)
#x = MaxPooling2D((2, 2), strides=(2, 2))(x) # 24x24x128
#x = BatchNormalization()(x)
x = Conv2D(1080, (1,1), activation='relu', **kwargs)(x)
for _ in range(6): x = subblock(x, 1080, **kwargs)
x = MaxPooling2D((2, 2), strides=(2, 2))(x) # 24x24x128
x = BatchNormalization()(x)
x = Conv2D(2048, (1,1), activation='relu', **kwargs)(x)
for _ in range(12): x = subblock(x, 2048, **kwargs)
x = GlobalMaxPooling2D()(x) # 2048
y = Dense(5004, use_bias=False, activation="softmax", name='patch_heatmap')(x)
branch_model = Model(inp, y)
branch_model.compile(optim, loss='categorical_crossentropy', metrics=['categorical_crossentropy', 'acc'])
return branch_model
build_averaging_model():
##############
# HEAD MODEL #
##############
#x = Average()(input_head)
inp = Input(shape=(5004,))
x = Dense(5004,use_bias=False, activation="softmax", name='patch_sum')(inp)
head_model = Model(inp, x, name='head')
#x = head_model([xa, xb])
return head_model
def build_model(head_model,branch_model):
inp = Input(shape=(patch_number,patch_size[0],patch_size[1],patch_size[2]))
input_head = []
for i in range(patch_number):
input_head.append(branch_model(inp[i,:,:,:]))
x = Average()(input_head)
x = head_model(x)
model = Model(inp,x)
return model
Related
I have a model which I have written both as a subclassed model and via Functional API.
class BatchNormReLU(tf.keras.Model):
def __init__(self, name='bn_relu', **kwargs):
super().__init__(**kwargs)
self.bn = BatchNormalization(name='bn')
self.relu = ReLU(name='relu')
def call(self, x):
out = self.bn(x)
out = self.relu(out)
return out
class UpsamplingBlock(tf.keras.Model):
def __init__(self, name='Upsampling Block', **kwargs):
super().__init__(**kwargs)
self.us_conv_a = Conv2D(256, 3, padding='same', use_bias=False, name='us_conv_a')
self.bnrelu = [BatchNormReLU(name='bn_relu')]
self.us_conv_b = Conv2D(128, 3, padding='same', use_bias=False, name='us_conv_b')
self.bnrelu.append(BatchNormReLU(name='bn_relu'))
self.us_conv_c = Conv2D(64, 3, padding='same', use_bias=False, name='us_conv_c')
self.bnrelu.append(BatchNormReLU(name='bn_relu'))
self.us_tconv_a = Conv2DTranspose(32, 2, strides=2, use_bias=False, name='us_tconv_a')
self.bnrelu.append(BatchNormReLU(name='bn_relu'))
self.finalmasks = Conv2D(1, 1, padding='same', activation='sigmoid', name='finalmasks')
def call(self, x):
i = 0
out = self.us_conv_a(x)
out = self.bnrelu[i](out)
i += 1
out = self.us_conv_b(out)
out = self.bnrelu[i](out)
i += 1
out = self.us_conv_c(out)
out = self.bnrelu[i](out)
i += 1
out = self.us_tconv_a(out)
out = self.finalmasks(out)
return out
This is the same architecture using Functional API.
def UpsamplingBlock(inputs):
x = Conv2D(256, 3, padding='same', use_bias=False, name='us_conv_a')(inputs)
x = BatchNormalization()(x)
x = ReLU()(x)
x = Conv2D(128, 3, padding='same', use_bias=False, name='us_conv_b')(x)
x = BatchNormalization()(x)
x = ReLU()(x)
x = Conv2D(64, 3, padding='same', use_bias=False, name='us_conv_c')(x)
x = BatchNormalization()(x)
x = ReLU()(x)
x = Conv2DTranspose(32, 2, strides=2, use_bias=False, name='us_tconv_a')(x)
x = BatchNormalization()(x)
x = ReLU()(x)
x = Conv2D(1, 1, padding='same', activation='sigmoid', name='finalmasks')(x)
return x
# return tf.keras.Model(inputs, x, name='UpsamplingBlock')
Obviously in the Functional API, each BatchNorm ReLU layer is seperate and trains individually.
I had doubt in the subclassed model. Will using BatchNorm and ReLU as a seperate class behave the same as Fuctional API. I am assuming that using the model subclassing like this will use the same instance of BN+ReLU in each call.
import numpy as np
import keras
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, Input, Add, BatchNormalization, Lambda, Concatenate, Reshape
from keras.models import Model
import tensorflow as tf
import os
import pandas as pd
import pydicom as dicom
import matplotlib.pyplot as plt
import math
import pydicom
import cv2
import os
def resize_dcm(dcmdosyası, boyut):
dcmgörsel = pydicom.read_file(dcmdosyası)
img = dcmgörsel.pixel_array
img = cv2.resize(img, boyut, interpolation = cv2.INTER_AREA)
dcmgörsel.PixelData = img.tobytes()
dcmgörsel.Rows, dcmgörsel.Columns = img.shape
return dcmgörsel
folder = 'C:/Users/USER/Desktop/data/'
ana1 = [f.path for f in os.scandir(folder) if f.is_dir()]
for ana2 in ana1:
görseller = [f.path for f in os.scandir(ana2) if f.is_file()]
for görsellercücük in görseller:
resize2 = resize_dcm(görsellercücük, (800, 800))
yeni = os.path.join(ana2, os.path.basename(görsellercücük))
pydicom.write_file(yeni, resize2)
inputs = Input(shape=(800,800,1))
input2 = Input(shape=(1))
input3 = Input(shape=(1))
x = Conv2D(64, (3, 3), activation='relu', padding='same')(inputs)
x = BatchNormalization()(x)
x = Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = BatchNormalization()(x)
x = Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
x = Dropout(0.5)(x)
x = Conv2D(128, (3, 3), activation='relu', strides=2, padding='same')(x)
x = BatchNormalization()(x)
x = Conv2D(128, (3, 3), activation='relu', strides=2, padding='same')(x)
x = BatchNormalization()(x)
res1 = Conv2D(128, (3, 3), activation='relu', padding='same')(x)
res2 = Conv2D(128, (3, 3), activation='relu', padding='same')(res1)
x = Add()([x, res2])
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
x = Dropout(0.5)(x)
x = Conv2D(256, (3, 3), activation='relu', strides=2, padding='same')(x)
x = BatchNormalization()(x)
x = Conv2D(256, (3, 3), activation='relu', strides=2, padding='same')(x)
x = BatchNormalization()(x)
res1 = Conv2D(256, (3, 3), activation='relu', padding='same')(x)
res2 = Conv2D(256, (3, 3), activation='relu', padding='same')(res1)
x = Add()([x, res2])
res1 = Conv2D(256, (3, 3), activation='relu', padding='same')(x)
res2 = Conv2D(256, (3, 3), activation='relu', padding='same')(res1)
x = Add()([x, res2])
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
x = Conv2D(512, (3, 3), activation='relu', strides=2, padding='same')(x)
x = BatchNormalization()(x)
x = Conv2D(512, (3, 3), activation='relu', strides=2, padding='same')(x)
x = BatchNormalization()(x)
res1 = Conv2D(512, (3, 3), activation='relu', padding='same')(x)
res2 = Conv2D(512, (3, 3), activation='relu', padding='same')(res1)
x = Add()([x, res2])
res1 = Conv2D(512, (3, 3), activation='relu', padding='same')(x)
res2 = Conv2D(512, (3, 3), activation='relu', padding='same')(res1)
x = Add()([x, res2])
x = Flatten()(x)
x = Concatenate()([x,input2,input3])
x = Dense(4096, activation='relu')(x)
x = Dense(4096, activation='relu')(x)
output = Dense(4, activation='softmax')(x) #birads score
output2 = Dense(4, activation='softmax')(x) #kompozisyon
output3 = Dense(2, activation='softmax')(x) #bolge 1
output4 = Dense(2, activation='softmax')(x) #bolge 2
output5 = Dense(2, activation='softmax')(x) #bolge 3
model = Model([inputs, input2,input3], [output,output2, output3])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
data = pd.read_excel('C:/Users/USER/Desktop/asdsadasdas.xlsx')
data.rename(columns={'HASTANO': 'ID', 'BIRADS KATEGORİSİ':'birads', 'MEME KOMPOZİSYONU': 'comp', 'KADRAN BİLGİSİ (SAĞ)': 'areaR', 'KADRAN BİLGİSİ (SOL)': 'areaL'}, inplace=True)
data.replace(['BI-RADS0', 'BI-RADS1-2', 'BI-RADS4-5'], [0,1,2], inplace=True)
data.drop(data.columns[5], inplace=True, axis=1)
data['comp'].value_counts()
data.fillna(0, inplace=True)
def transform_to_hu(medical_image, image):
intercept = medical_image.RescaleIntercept
slope = medical_image.RescaleSlope
hu_image = image * slope + intercept
return hu_image
def train():
mainDir = 'C:/Users/USER/Desktop/data/'
for d in data.iterrows():
patientDir = mainDir + str(d[1][0])
for f in os.listdir(patientDir):
yon = 1 if f[0] == 'R' else 0
view = 1 if f[1] == 'M' else 0
dcm = dicom.dcmread(patientDir + '/' + f)
image = transform_to_hu(dcm, dcm.pixel_array)
o1 = 0
o2 = 0
o3 = 0
areaStr = d[1]['area' + ('R' if yon == 1 else 'L')]
if areaStr!= 0:
areaStr = areaStr.replace('[', '').replace(']', '')
lst = areaStr.split(',')
newLst = list(map(lambda x : x.replace('"', ''), lst))
print(newLst)
for a in newLst:
if(a == 'MERKEZ'):
o2 = 1
continue
t = a.split(' ')
if(view == 1): #mlo
if t[0] == 'ÜST':
o1 = 1
else:
o3 = 1
else:
if t[1] == 'DIŞ':
o1 = 1
else:
o3 = 1
print(o1, o2, o3, sep=', ')
model.fit([image, yon, view], [d[1]['birads'], d[1]['comp'], o1, o2, o3])
break
train()
Error:
Traceback (most recent call last):
File "C:\Users\USER\Desktop\aaac.py", line 157, in <module>
train()
File "C:\Users\USER\Desktop\aaac.py", line 155, in train
model.fit([image, yon, view], [d[1]['birads'], d[1]['comp'], o1, o2, o3])
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\USER\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\engine\data_adapter.py", line 1081, in select_data_adapter
raise ValueError(
ValueError: Failed to find data adapter that can handle input: (<class 'list'> containing values of types {"<class 'numpy.ndarray'>", "<class 'int'>"}), (<class 'list'> containing values of types {"<class 'str'>", "<class 'int'>"})
Process finished with exit code 1
hello i wrote a deep learning code. At first, I shaped the code in accordance with the resnet architecture, and then I tried to obtain an output by combining the data on excel and visual. I have encountered such an error, I do not know why.
![Topluluk Tarafından Doğrulandı simgesi](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAQAAAAngNWGAAAA/0lEQVR4AYXNMSiEcRyA4cfmGHQbCZIipkuxnJgMStlMNmeyD2dwmc8+sZgxYJd9ErIZFHUyYYD7fkr6l4/rnvmtl7+KitrqV/fq2Y5eLY3Z9S48eRLe7BmVZ9qhTLhQ0algzZWQOVKSsCF8OjAnwbxDTWFDUhPK/jMr1H6HE/IqRky2DyvCefuwItwZzodVoYRiLqMkVCXrwpJ9twZ+sgfDYEFYl8wIWxZ9uFf7zkallxlJh4YrLGsKjZRx7VGHhLqwgFUN45DGdb8MeXGpgB4ABZdeDcpZEY51A+hyLKz4S1W4MQWm3AibWtgWmk6dyISa1pSdyWTOlLXVp0+eL9D/ZPfBTNanAAAAAElFTkSuQmCC)
I'm classifying images into two classes using binary crossentropy. So, when I'm trying to wrap my keras model with it, I get the error:
ValueError: logits and labels must have the same shape ((None, 2) vs (None, 1))
Please help. My code is:
import tensorflow as tf
import os
# os.path.join('Train', 'benign', 'malignant')
gpus = tf.config.experimental.list_physical_devices('GPU')
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
import cv2
import imghdr
from matplotlib import pyplot as plt
data_dir = ['Train', 'Test', 'Validation']
image_exts = ['jpg', 'jpeg', 'png', 'bmp']
# print(os.listdir(data_dir))
# print(os.listdir(os.path.join(data_dir, 'benign')))
'''
img = cv2.imread(os.path.join(data_dir, 'benign', 'image_7.jpg'))
print(img)
print(img.shape)
plt.imshow(img)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
'''
for i in data_dir:
for image_class in os.listdir(i):
for image in os.listdir(os.path.join(i, image_class)):
image_path = os.path.join(i, image_class, image)
try:
img = cv2.imread(image_path) # Opening up an image (opens as numpy array)
tip = imghdr.what(image_path)
if tip not in image_exts:
print('Image not in ext list {}'.format(image_path))
os.remove(image_path)
except Exception as e:
print('Issue with image {}'.format(image_path))
import numpy as np
data1 = tf.keras.utils.image_dataset_from_directory('Train', shuffle=True) # Building data pipeline
data2 = tf.keras.utils.image_dataset_from_directory('Test', shuffle=True) # Building data pipeline
data3 = tf.keras.utils.image_dataset_from_directory('Validation', shuffle=True) # Building data pipeline
data_iterator1 = data1.as_numpy_iterator() # Helps in accessing data pipeline
data_iterator2 = data2.as_numpy_iterator()
data_iterator3 = data3.as_numpy_iterator()
batch1 = data_iterator1.next() # Accessing data pipeline
batch2 = data_iterator2.next()
batch3 = data_iterator3.next()
# print(batch3[1])
fig, ax = plt.subplots(ncols=4, figsize=(20,20))
for idx, img in enumerate(batch1[0][:4]):
ax[idx].imshow(img.astype(int))
ax[idx].title.set_text(batch1[1][idx])
fig, ax = plt.subplots(ncols=4, figsize=(20,20))
for idx, img in enumerate(batch2[0][:4]):
ax[idx].imshow(img.astype(int))
ax[idx].title.set_text(batch2[1][idx])
fig, ax = plt.subplots(ncols=4, figsize=(20,20))
for idx, img in enumerate(batch3[0][:4]):
ax[idx].imshow(img.astype(int))
ax[idx].title.set_text(batch3[1][idx])
''' Preprocessing Data '''
# Scaling Data
data1 = data1.map(lambda x,y: (x/255, y))
data1.as_numpy_iterator().next()[0].max()
data2 = data2.map(lambda x,y: (x/255, y))
data2.as_numpy_iterator().next()[0].max()
data3 = data3.map(lambda x,y: (x/255, y))
data3.as_numpy_iterator().next()[0].max()
train_size = int(len(data1))
val_size = int(len(data3))
test_size = int(len(data2))
train = data1.take(train_size)
val = data3.take(val_size)
test = data2.take(test_size)
''' Deep Model '''
# The Network
from tensorflow.keras import Model
from tensorflow.keras.layers import Input, Conv2D, MaxPool2D, Dense, Flatten, Dropout, AveragePooling2D, Concatenate
def vgg(input_shape, n_classes):
input = Input(input_shape)
x = Conv2D(64, 3, padding='same', activation='relu')(input)
x = Conv2D(64, 3, padding='same', activation='relu')(x)
x = MaxPool2D(2, strides=2, padding='same')(x)
x = Conv2D(128, 3, padding='same', activation='relu')(x)
x = Conv2D(128, 3, padding='same', activation='relu')(x)
x = MaxPool2D(2, strides=2, padding='same')(x)
x = Conv2D(256, 3, padding='same', activation='relu')(x)
x = Conv2D(256, 3, padding='same', activation='relu')(x)
x = Conv2D(256, 3, padding='same', activation='relu')(x)
x = MaxPool2D(2, strides=2, padding='same')(x)
x = Conv2D(512, 3, padding='same', activation='relu')(x)
x = Conv2D(512, 3, padding='same', activation='relu')(x)
x = Conv2D(512, 3, padding='same', activation='relu')(x)
x = MaxPool2D(2, strides=2, padding='same')(x)
x = Conv2D(512, 3, padding='same', activation='relu')(x)
x = Conv2D(512, 3, padding='same', activation='relu')(x)
x = Conv2D(512, 3, padding='same', activation='relu')(x)
x = MaxPool2D(2, strides=2, padding='same')(x)
x = Flatten()(x)
x = Dense(4096, activation='relu')(x)
x = Dense(4096, activation='relu')(x)
output = Dense(n_classes, activation='softmax')(x)
model = Model(input, output)
return model
input_shape = (256, 256, 3)
n_classes = 2
model = vgg(input_shape, n_classes)
model.compile('adam', loss=tf.losses.BinaryCrossentropy(), metrics=['accuracy'])
model.summary()
# Train
logdir='logs'
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=logdir)
hist = model.fit(train, epochs=20, validation_data=val, callbacks=[tensorboard_callback])
print(hist.history)
I am dealing with audio data (.wav) files, I have created two CNN network to pass the data. so the two models at the end will use the function of Concatenate the output of these two models and give out the classification. but I am having problem for training it, it says the model expects to see 2 arrays but it got one array. any one who can help me with this problem I will appreciate, I have been struggling for weeks.
this is my config file
class Config:
def __init__(self, mode='conv', nfilt=26, nfeat=40, nfft=4096, rate=16000):
self.mode = mode
self.nfilt = nfilt
self.nfeat = nfeat
self.nfft = nfft
self.rate = rate
self.step = int(rate / 10)
self.model_path = os.path.join('models', mode + '.model')
self.p_path = os.path.join('pickles', mode + '.p')
I build one function for my mfcc
build a function for build
def build_rand_feat():
X = []
y = []
_min, _max = float('inf'), -float('inf')
for _ in tqdm(range(n_sample)):
rand_class = np.random.choice(class_dist.index, p=prob_dist)
file = np.random.choice(df[df.label == rand_class].index)
data, rate = sf.read('audios/' + file)
label = df.at[file, 'label']
rand_index = np.random.randint(0, data.shape[0] - config.step)
sample = data[rand_index:rand_index + config.step]
X_sample = mfcc(sample, rate, winlen=0.05, winstep=0.02, numcep=config.nfeat, nfilt=config.nfilt, nfft=config.nfft)
_min = min(np.amin(X_sample), _min)
_max = max(np.amax(X_sample), _max)
X.append(X_sample)
y.append(classes.index(label))
config.min = _min
X = np.array(X)
y = np.array(y)
X = (X - _min) / (_max - _min)
if config.mode == 'conv':
X = X.reshape(X.shape[0], X.shape[1], X.shape[2], 1)
print(X.shape)
elif config.mode == 'time':
X = X.reshape(X.shape[0], X.shape[1], X.shape[2])
y = to_categorical(y, num_classes=10)
config.data = (X, y)
return X, y
and this is my multi scale network
def get_conv_model():
main_model = Sequential()
main_model.add(Conv2D(128, (3, 3), activation='relu', strides=(1, 1), padding='same', input_shape=input_shape))
main_model.add(MaxPool2D((2, 2)))
main_model.add(Conv2D(128, (3, 3), activation='relu', strides=(1, 1), padding='same'))
main_model.add(MaxPool2D((2, 2), padding='same'))
main_model.add(Flatten())
second_model = Sequential()
second_model.add(Conv2D(256, (3, 3), activation='relu', strides=(1, 1), padding='same', input_shape=input_shape))
second_model.add(MaxPool2D((2, 2), padding='same'))
second_model.add(Conv2D(256, (3, 3), activation='relu', strides=(1, 1), padding='same'))
second_model.add(MaxPool2D((2, 2)))
second_model.add(Flatten())
# first model upper
main_model = Sequential()
main_model.add(Conv2D(64, (3, 3), activation='relu', strides=(1, 1), padding='same', input_shape=input_shape))
main_model.add(BatchNormalization())
main_model.add(Dropout(0.3))
main_model.add(Conv2D(128, (3, 3), activation='relu', strides=(1, 1), padding='same'))
main_model.add(BatchNormalization())
main_model.add(Dropout(0.3))
main_model.add(Conv2D(256, (3, 3), activation='relu', strides=(1, 1), padding='same'))
main_model.add(BatchNormalization())
main_model.add(Dropout(0.3))
main_model.add(Flatten())
# second model lower
lower_model1 = Sequential()
lower_model1.add(MaxPool2D(strides=(1, 1), padding='same', input_shape=input_shape))
lower_model1.add(Conv2D(128, (3, 3), activation='relu', strides=(1, 1), padding='same', input_shape=input_shape))
lower_model1.add(BatchNormalization())
lower_model1.add(Dropout(0.3))
lower_model1.add(Conv2D(256, (3, 3), activation='relu', strides=(1, 1), padding='same'))
lower_model1.add(BatchNormalization())
lower_model1.add(Dropout(0.3))
lower_model1.add(Conv2D(512, (3, 3), activation='relu', strides=(1, 1), padding='same'))
lower_model1.add(Flatten())
# merged models
merged_model = Concatenate()([main_model.output, lower_model1.output])
x = Dense(256, activation='relu')(merged_model)
x = Dropout(0.3)(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.3)(x)
x = Dense(128, activation='relu')(x)
x = Dropout(0.3)(x)
output = Dense(10, activation='softmax')(x)
final_model = Model(inputs=[main_model.input, lower_model1.input], outputs=[output])
final_model.summary()
final_model.compile(loss="categorical_crossentropy", optimizer=Adam(0.001), metrics=['acc'])
print(K.eval(final_model.optimizer.lr))
#class_weight = compute_class_weight('balanced', np.unique(y_flat), y_flat)
final_model.fit(X,y, epochs=10, batch_size=64, shuffle=True, validation_split=0.3)
return main_model, lower_model1, merged_model
I'm trying to do semantic segmentation on skin lesion. I used SegNet as my model. But after running this model, training loss was decreasing but validation loss was not decreasing. i.e. overfitting problem is occured. I tuned learning rate many times and reduced number of number dense layer but no solution came. I also used dropout but still overfitting is happening.
Here is the graph
Here is the code of my model:
def segnet(input_size=(512, 512, 1)):
# Encoding layer
img_input = Input(input_size)
x = Conv2D(64, (3, 3), padding='same', name='conv1',strides= (1,1))(img_input)
x = BatchNormalization(name='bn1')(x)
x = Activation('relu')(x)
x = Conv2D(64, (3, 3), padding='same', name='conv2')(x)
x = BatchNormalization(name='bn2')(x)
x = Activation('relu')(x)
x = MaxPooling2D()(x)
x = Dropout(0.7)(x)
x = Conv2D(128, (3, 3), padding='same', name='conv3')(x)
x = BatchNormalization(name='bn3')(x)
x = Activation('relu')(x)
x = Conv2D(128, (3, 3), padding='same', name='conv4')(x)
x = BatchNormalization(name='bn4')(x)
x = Activation('relu')(x)
x = MaxPooling2D()(x)
x = Dropout(0.7)(x)
x = Conv2D(256, (3, 3), padding='same', name='conv5')(x)
x = BatchNormalization(name='bn5')(x)
x = Activation('relu')(x)
x = Conv2D(256, (3, 3), padding='same', name='conv6')(x)
x = BatchNormalization(name='bn6')(x)
x = Activation('relu')(x)
x = Conv2D(256, (3, 3), padding='same', name='conv7')(x)
x = BatchNormalization(name='bn7')(x)
x = Activation('relu')(x)
x = MaxPooling2D()(x)
x = Dropout(0.7)(x)
x = Conv2D(512, (3, 3), padding='same', name='conv8')(x)
x = BatchNormalization(name='bn8')(x)
x = Activation('relu')(x)
x = Conv2D(512, (3, 3), padding='same', name='conv9')(x)
x = BatchNormalization(name='bn9')(x)
x = Activation('relu')(x)
x = Conv2D(512, (3, 3), padding='same', name='conv10')(x)
x = BatchNormalization(name='bn10')(x)
x = Activation('relu')(x)
x = MaxPooling2D()(x)
x = Dropout(0.7)(x)
x = Conv2D(512, (3, 3), padding='same', name='conv11')(x)
x = BatchNormalization(name='bn11')(x)
x = Activation('relu')(x)
x = Conv2D(512, (3, 3), padding='same', name='conv12')(x)
x = BatchNormalization(name='bn12')(x)
x = Activation('relu')(x)
x = Conv2D(512, (3, 3), padding='same', name='conv13')(x)
x = BatchNormalization(name='bn13')(x)
x = Activation('relu')(x)
x = MaxPooling2D()(x)
x = Dropout(0.7)(x)
x = Dense(256, activation = 'relu', name='fc1')(x)
x = Dense(256, activation = 'relu', name='fc2')(x)
# Decoding Layer
x = UpSampling2D()(x)
x = Conv2DTranspose(512, (3, 3), padding='same', name='deconv1')(x)
x = BatchNormalization(name='bn14')(x)
x = Activation('relu')(x)
x = Conv2DTranspose(512, (3, 3), padding='same', name='deconv2')(x)
x = BatchNormalization(name='bn15')(x)
x = Activation('relu')(x)
x = Conv2DTranspose(512, (3, 3), padding='same', name='deconv3')(x)
x = BatchNormalization(name='bn16')(x)
x = Activation('relu')(x)
x = Dropout(0.7)(x)
x = UpSampling2D()(x)
x = Conv2DTranspose(512, (3, 3), padding='same', name='deconv4')(x)
x = BatchNormalization(name='bn17')(x)
x = Activation('relu')(x)
x = Conv2DTranspose(512, (3, 3), padding='same', name='deconv5')(x)
x = BatchNormalization(name='bn18')(x)
x = Activation('relu')(x)
x = Conv2DTranspose(256, (3, 3), padding='same', name='deconv6')(x)
x = BatchNormalization(name='bn19')(x)
x = Activation('relu')(x)
x = Dropout(0.7)(x)
x = UpSampling2D()(x)
x = Conv2DTranspose(256, (3, 3), padding='same', name='deconv7')(x)
x = BatchNormalization(name='bn20')(x)
x = Activation('relu')(x)
x = Conv2DTranspose(256, (3, 3), padding='same', name='deconv8')(x)
x = BatchNormalization(name='bn21')(x)
x = Activation('relu')(x)
x = Conv2DTranspose(128, (3, 3), padding='same', name='deconv9')(x)
x = BatchNormalization(name='bn22')(x)
x = Activation('relu')(x)
x = Dropout(0.7)(x)
x = UpSampling2D()(x)
x = Conv2DTranspose(128, (3, 3), padding='same', name='deconv10')(x)
x = BatchNormalization(name='bn23')(x)
x = Activation('relu')(x)
x = Conv2DTranspose(64, (3, 3), padding='same', name='deconv11')(x)
x = BatchNormalization(name='bn24')(x)
x = Activation('relu')(x)
x = Dropout(0.7)(x)
x = UpSampling2D()(x)
x = Conv2DTranspose(64, (3, 3), padding='same', name='deconv12')(x)
x = BatchNormalization(name='bn25')(x)
x = Activation('relu')(x)
x = Conv2DTranspose(1, (3, 3), padding='same', name='deconv13')(x)
x = BatchNormalization(name='bn26')(x)
x = Activation('sigmoid')(x)
pred = Reshape((input_size[0], input_size[1]))(x)
return Model(inputs=img_input, outputs=pred)
I have used the same dataset for another modle UNet but there was no overfit for UNet. This is giving overfit only for SegNet model.
For more information :
model = segnet(input_size = (224, 224, INPUT_CHANNELS))
model.compile(optimizer= Adam(lr=1e-5), loss= [dice_coef_loss]
, metrics=[iou, dice_coe, precision, recall, accuracy])
model_checkpoint = ModelCheckpoint(str(j+1) + '_skin_leison.hdf5',
monitor='loss',
verbose=1,
save_best_only=True)
callbacks_list = [model_checkpoint]
history = model.fit(X_train_cv,
y_train_cv,
epochs= 70,
callbacks = callbacks_list,
batch_size= 8,
validation_data=(X_valid_cv, y_valid_cv))