Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Deep Learning and U-net Segmentation - unknown error

Mateusz Bielecki:

I have a problem with Deep Learning model. It's my first model for semantic segmentation but I don't know where I do mistake. The full code is below. I'm asking for help. I don't know what I'm doing wrong.

I have 24 images with 6000x6000 dimensions. In addition I have 24 labels with 6 class but in the code I want to do only one class classification. I think that the data is prepared correctly.


import numpy as np
import cv2

import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from os import listdir

input_shape = (6000,6000)
output_shape = (480,480)

folder_labels = 'labels'
folder_images = 'images'
list_ = listdir(folder_labels)

list_of_labels=[]
list_of_images=[]

for __, val in enumerate(list_):
if val[-3:]=='tif':
list_of_labels.append(val)
list_of_images.append(val[:-9]+'RGB.tif')

X = np.empty([len(list_of_images), output_shape[0], output_shape[1]],)
for i, image in enumerate(list_of_images):
im = cv2.imread(folder_images+'/'+image, 0)
im = cv2.resize(im, output_shape, interpolation=cv2.INTER_NEAREST)
im = (im-np.min(im))/(np.max(im)-np.min(im))
X[i] = im

Y = np.empty([len(list_of_labels), output_shape[0], output_shape[1]])
for i, image in enumerate(list_of_labels):
im = cv2.imread(folder_labels+'/'+image, 0)
im = cv2.resize(im, output_shape, interpolation=cv2.INTER_NEAREST)
im = (im==29)*np.ones(output_shape)/255
Y[i] = im

X = X[:,:,:,np.newaxis] #[24,480,480,1]
Y = Y[:,:,:,np.newaxis] #[24,480,480,1]

x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size = 0.2)

from keras.models import Model
from keras.layers import concatenate, Input, MaxPool2D, Dropout, Conv2D, UpSampling2D
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator

input_layer = Input(shape=x_train.shape[1:])

#480
c1 = Conv2D(filters=64, kernel_size=(3,3), activation='relu', padding='same')(input_layer)
c1 = Conv2D(filters=64, kernel_size=(3,3), activation='relu', padding='same')(c1)
l = MaxPool2D(strides=(2,2))(c1)
#240
c2 = Conv2D(filters=128, kernel_size=(3,3), activation='relu', padding='same')(l)
c2 = Conv2D(filters=128, kernel_size=(3,3), activation='relu', padding='same')(c2)
l = MaxPool2D(strides=(2,2))(c2)
#120
c3 = Conv2D(filters=256, kernel_size=(3,3), activation='relu', padding='same')(l)
c3 = Conv2D(filters=256, kernel_size=(3,3), activation='relu', padding='same')(c3)
l = MaxPool2D(strides=(2,2))(c3)
#60
c4 = Conv2D(filters=512, kernel_size=(3,3), activation='relu', padding='same')(l)
c4 = Conv2D(filters=512, kernel_size=(3,3), activation='relu', padding='same')(c4)
l = Dropout(0.5)(c4)
l = MaxPool2D(strides=(2,2))(l)
#30
c5 = Conv2D(filters=1024, kernel_size=(3,3), activation='relu', padding='same')(l)
c5 = Conv2D(filters=1024, kernel_size=(3,3), activation='relu', padding='same')(c5)
l = Dropout(0.5)(c5)
l = concatenate([UpSampling2D(size=(2,2))(l), c4], axis=-1)
#60
l = Conv2D(filters=512, kernel_size=(3,3), activation='relu', padding='same')(l)
l = Conv2D(filters=512, kernel_size=(3,3), activation='relu', padding='same')(l)
l = concatenate([UpSampling2D(size=(2,2))(l), c3], axis=-1)
#120
l = Conv2D(filters=256, kernel_size=(3,3), activation='relu', padding='same')(l)
l = Conv2D(filters=256, kernel_size=(3,3), activation='relu', padding='same')(l)
l = concatenate([UpSampling2D(size=(2,2))(l), c2], axis=-1)
#240
l = Conv2D(filters=128, kernel_size=(3,3), activation='relu', padding='same')(l)
l = Conv2D(filters=128, kernel_size=(3,3), activation='relu', padding='same')(l)
l = concatenate([UpSampling2D(size=(2,2))(l), c1], axis=-1)
#480
l = Conv2D(filters=64, kernel_size=(3,3), activation='relu', padding='same')(l)
l = Conv2D(filters=64, kernel_size=(3,3), activation='relu', padding='same')(l)
output_layer = Conv2D(filters=1, kernel_size=(1,1), activation='sigmoid')(l)

model = Model(input_layer, output_layer)

def my_generator(x_train, y_train, batch_size):
data_generator = ImageDataGenerator(
width_shift_range=0.1,
height_shift_range=0.1,
rotation_range=10,
zoom_range=0.1).flow(x_train, x_train, batch_size, seed=42)
mask_generator = ImageDataGenerator(
width_shift_range=0.1,
height_shift_range=0.1,
rotation_range=10,
zoom_range=0.1).flow(y_train, y_train, batch_size, seed=42)
while True:
x_batch, _ = data_generator.next()
y_batch, _ = mask_generator.next()
yield x_batch, y_batch

model.compile(optimizer=Adam(2e-4), loss='binary_crossentropy', metrics = ['accuracy'])

hist = model.fit_generator(my_generator(x_train, y_train, 5),
steps_per_epoch = 20,
validation_data = (x_test, y_test),
epochs=15, verbose=2)

After training:


y_hat = model.predict(x_test)
fig, ax = plt.subplots(1,3,figsize=(12,6))
ax[0].imshow(x_test[0,:,:,0], cmap='gray')
ax[1].imshow(y_test[0,:,:,0])
ax[2].imshow(y_hat[0,:,:,0])

Results:



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Deep Learning and U-net Segmentation - unknown error

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×