# Learn about Fully Convolutional Networks for semantic segmentation

In this blog post, I will learn a semantic segmentation problem and review fully convolutional networks. In an image for the semantic segmentation, each pixcel is usually labeled with the class of its enclosing object or region. For example, a pixcel might belongs to a road, car, building or a person. The semantic segmentation problem requires to make a classification at every pixel.

I will use Fully Convolutional Networks (FCN) to classify every pixcel.

To understand the semantic segmentation problem, let's look at an example data prepared by divamgupta. Note: I will use this example data rather than famous segmentation data e.g., pascal VOC2012 because it requires pre-processing.

## Reference¶

In [1]:
dir_data = "dataset1/"
dir_seg = dir_data + "/annotations_prepped_train/"
dir_img = dir_data + "/images_prepped_train/"


## Visualize a single segmentation image¶

In this data, there are 12 segmentation classes and the image is from a driving car.

In [2]:
import cv2, os
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
## seaborn has white grid by default so I will get rid of this.
sns.set_style("whitegrid", {'axes.grid' : False})

ldseg = np.array(os.listdir(dir_seg))
## pick the first image file
fnm = ldseg[0]
print(fnm)

## read in the original image and segmentation labels
seg = cv2.imread(dir_seg + fnm ) # (360, 480, 3)
img_is = cv2.imread(dir_img + fnm )
print("seg.shape={}, img_is.shape={}".format(seg.shape,img_is.shape))

## Check the number of labels
mi, ma = np.min(seg), np.max(seg)
n_classes = ma - mi + 1
print("minimum seg = {}, maximum seg = {}, Total number of segmentation classes = {}".format(mi,ma, n_classes))

fig = plt.figure(figsize=(5,5))
ax.imshow(img_is)
ax.set_title("original image")
plt.show()

fig = plt.figure(figsize=(15,10))
for k in range(mi,ma+1):
ax.imshow((seg == k)*1.0)
ax.set_title("label = {}".format(k))

plt.show()

0016E5_01620.png
seg.shape=(360, 480, 3), img_is.shape=(360, 480, 3)
minimum seg = 0, maximum seg = 11, Total number of segmentation classes = 12


## Data preprocessing: Resize image¶

To simplify the problem, I will reshape all the images to the same size: (224,224). Why (224,224)? This is the iamge shape used in VGG and FCN model in this blog uses a network that takes advantage of VGG structure. The FCN model becomes easier to explain when the image shape is (224,224). However, FCN does not requires the image shape to be (224,224).

Let's visualize how the resizing make the images look like. The images look fine.

In [3]:
import random
def give_color_to_seg_img(seg,n_classes):
'''
seg : (input_width,input_height,3)
'''

if len(seg.shape)==3:
seg = seg[:,:,0]
seg_img = np.zeros( (seg.shape[0],seg.shape[1],3) ).astype('float')
colors = sns.color_palette("hls", n_classes)

for c in range(n_classes):
segc = (seg == c)
seg_img[:,:,0] += (segc*( colors[c][0] ))
seg_img[:,:,1] += (segc*( colors[c][1] ))
seg_img[:,:,2] += (segc*( colors[c][2] ))

return(seg_img)

input_height , input_width = 224 , 224
output_height , output_width = 224 , 224

ldseg = np.array(os.listdir(dir_seg))
for fnm in ldseg[np.random.choice(len(ldseg),3,replace=False)]:
fnm = fnm.split(".")[0]
seg = cv2.imread(dir_seg + fnm + ".png") # (360, 480, 3)
img_is = cv2.imread(dir_img + fnm + ".png")
seg_img = give_color_to_seg_img(seg,n_classes)

fig = plt.figure(figsize=(20,40))
ax.imshow(seg_img)