Multiclass Segmentation using u-net, Data format (how to label (mask it)

Adithia_Jo: <p>from <strong>future</strong> import print_function<br>
from keras.preprocessing.image import ImageDataGenerator<br>
import numpy as np<br>
import os<br>
import glob<br>
import <a href="http://skimage.io" rel="nofollow noopener">skimage.io</a> as io<br>
import skimage.transform as trans</p>
<p>def adjustData(img,mask,flag_multi_class,num_class):<br>
if(flag_multi_class):<br>
img = img / 255<br>
mask = mask[:,:,:,0] if(len(mask.shape) == 4) else mask[:,:,0]<br>
new_mask = np.zeros(mask.shape + (num_class,))<br>
for i in range(num_class):<br>
<span class="hashtag">#for</span> one pixel in the image, find the class in mask and convert it into one-hot vector<br>
<span class="hashtag">#index</span> = np.where(mask == i)<br>
<span class="hashtag">#index_mask</span> = (index[0],index[1],index[2],np.zeros(len(index[0]),dtype = np.int64) + i) if (len(mask.shape) == 4) else (index[0],index[1],np.zeros(len(index[0]),dtype = np.int64) + i)<br>
<span class="hashtag">#new_mask</span>[index_mask] = 1<br>
new_mask[mask == i,i] = 1<br>
new_mask = np.reshape(new_mask,(new_mask.shape[0],new_mask.shape[1]*new_mask.shape[2],new_mask.shape[3])) if flag_multi_class else np.reshape(new_mask,(new_mask.shape[0]*new_mask.shape[1],new_mask.shape[2]))<br>
mask = new_mask<br>
elif(np.max(img) &gt; 1):<br>
img = img / 255<br>
mask = mask /255<br>
mask[mask &gt; 0.5] = 1<br>
mask[mask &lt;= 0.5] = 0<br>
return (img,mask)</p>
<p>def trainGenerator(batch_size,train_path,image_folder,mask_folder,aug_dict,image_color_mode = “grayscale”,<br>
mask_color_mode = “grayscale”,image_save_prefix  = “image”,mask_save_prefix  = “mask”,<br>
flag_multi_class = False,num_class = 2,save_to_dir = None,target_size = (256,256),seed = 1):<br>
‘’’<br>
can generate image and mask at the same time<br>
use the same seed for image_datagen and mask_datagen to ensure the transformation for image and mask is the same<br>
if you want to visualize the results of generator, set save_to_dir = “your path”<br>
‘’’<br>
image_datagen = ImageDataGenerator(**aug_dict)<br>
mask_datagen = ImageDataGenerator(**aug_dict)<br>
image_generator = image_datagen.flow_from_directory(<br>
train_path,<br>
classes = [image_folder],<br>
class_mode = None,<br>
color_mode = image_color_mode,<br>
target_size = target_size,<br>
batch_size = batch_size,<br>
save_to_dir = save_to_dir,<br>
save_prefix  = image_save_prefix,<br>
seed = seed)<br>
mask_generator = mask_datagen.flow_from_directory(<br>
train_path,<br>
classes = [mask_folder],<br>
class_mode = None,<br>
color_mode = mask_color_mode,<br>
target_size = target_size,<br>
batch_size = batch_size,<br>
save_to_dir = save_to_dir,<br>
save_prefix  = mask_save_prefix,<br>
seed = seed)<br>
train_generator = zip(image_generator, mask_generator)<br>
for (img,mask) in train_generator:<br>
img,mask = adjustData(img,mask,flag_multi_class,num_class)<br>
yield (img,mask)</p>
<p>def valGenerator(batch_size,train_path,image_folder,mask_folder,image_color_mode = “grayscale”,<br>
mask_color_mode = “grayscale”,image_save_prefix  = “image”,mask_save_prefix  = “mask”,<br>
flag_multi_class = False,num_class = 2,save_to_dir = None,target_size = (256,256),seed = 1):<br>
‘’’<br>
can generate image and mask at the same time<br>
use the same seed for image_datagen and mask_datagen to ensure the transformation for image and mask is the same<br>
if you want to visualize the results of generator, set save_to_dir = “your path”<br>
‘’’<br>
image_datagen = ImageDataGenerator()<br>
mask_datagen = ImageDataGenerator()<br>
image_generator = image_datagen.flow_from_directory(<br>
train_path,<br>
classes = [image_folder],<br>
class_mode = None,<br>
color_mode = image_color_mode,<br>
target_size = target_size,<br>
batch_size = batch_size,<br>
save_to_dir = save_to_dir,<br>
save_prefix  = image_save_prefix,<br>
seed = seed)<br>
mask_generator = mask_datagen.flow_from_directory(<br>
train_path,<br>
classes = [mask_folder],<br>
class_mode = None,<br>
color_mode = mask_color_mode,<br>
target_size = target_size,<br>
batch_size = batch_size,<br>
save_to_dir = save_to_dir,<br>
save_prefix  = mask_save_prefix,<br>
seed = seed)<br>
train_generator = zip(image_generator, mask_generator)<br>
for (img,mask) in train_generator:<br>
img,mask = adjustData(img,mask,flag_multi_class,num_class)<br>
yield (img,mask)</p>
<p>def testGenerator(test_path,num_image = 18,target_size = (256,256),flag_multi_class = False,as_gray = True):<br>
for i in test_path:<br>
img = io.imread(i, as_gray = as_gray)<br>
print(i)</p>
<h1>img = img / 255</h1>
<pre><code>    img = trans.resize(img,target_size)
    img = np.reshape(img,img.shape+(1,)) if (not flag_multi_class) else img
    img = np.reshape(img,(1,)+img.shape)
    yield img
</code></pre>
<p>def testGenerator2(batch_size,test_path,target_size = (256,256), image_color_mode = “grayscale”, num_class = 2,flag_multi_class = False,as_gray = True):<br>
image_datagen = ImageDataGenerator()<br>
image_generator = image_datagen.flow_from_directory(<br>
test_path,<br>
class_mode = None,<br>
color_mode = image_color_mode,<br>
target_size = target_size,<br>
batch_size = batch_size<br>
)<br>
return image_generator</p>
<p>def geneTrainNpy(image_path,mask_path,flag_multi_class = False,num_class = 2,image_prefix = “image”,mask_prefix = “mask”,image_as_gray = True,mask_as_gray = True):<br>
image_name_arr = glob.glob(os.path.join(image_path,"%s*.png"%image_prefix))<br>
image_arr = []<br>
mask_arr = []<br>
for index,item in enumerate(image_name_arr):<br>
img = io.imread(item,as_gray = image_as_gray)<br>
img = np.reshape(img,img.shape + (1,)) if image_as_gray else img<br>
mask = io.imread(item.replace(image_path,mask_path).replace(image_prefix,mask_prefix),as_gray = mask_as_gray)<br>
mask = np.reshape(mask,mask.shape + (1,)) if mask_as_gray else mask<br>
img,mask = adjustData(img,mask,flag_multi_class,num_class)<br>
image_arr.append(img)<br>
mask_arr.append(mask)<br>
image_arr = np.array(image_arr)<br>
mask_arr = np.array(mask_arr)<br>
return image_arr,mask_arr</p>
<p>def labelVisualize(num_class,color_dict,img):<br>
img = img[:,:,0] if len(img.shape) == 3 else img<br>
img_out = np.zeros(img.shape + (3,))<br>
for i in range(num_class):<br>
img_out[img == i,:] = color_dict[i]<br>
return img_out</p>
<p>def labelVisualizeBinary(img):<br>
img[img &gt; 0.5] = 1<br>
img[img&lt;= 0.5] = 0<br>
return img</p>
<p>def saveResult(save_path,results_filename,npyfile,flag_multi_class = False,num_class = 2):<br>
print(results_filename[0])<br>
for i,item in enumerate(npyfile):<br>
img = labelVisualize(num_class,COLOR_DICT,item) if flag_multi_class else item[:,:,0]<br>
print(i)<br>
io.imsave(os.path.join(save_path,results_filename[i]),img)</p>
<h4>my problem</h4>
<p>this is my data code , i have problem how to change it to multiclass segmentation,<br>
i divided codes with 3 part , 1 for data(to transform (num classes), 1 for train(like epoch,path_to_train,etc) , and 1 for model (unet)</p>
<p>in my model i was change the loss function to sparse_categorical_crossentropy, and the last layer to softmax,i know my problem in the data codes (how to transform it multiclass (num classes), i has 3 output (black,white, red) that mean my num_classes = 3), but i dont know how to change it?  i just change the num_classes but nothing happend , maybe you can solve my problem thank you <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"> (codes up here)</p>
<h4></h4>

WaterKnight: <p>I suggest you to try FastAI 2 unet_leaner, it is mounted above pytorch. Semantic segmentation is very easy with that library</p>

Adithia_Jo: <aside class="quote no-group" data-username="WaterKnight" data-post="2" data-topic="77554">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/waterknight/48/54725_2.png" class="avatar"> WaterKnight:</div>
<blockquote>
<p>FastAI 2 unet_leaner, it is mounted above pytorch</p>
</blockquote>
</aside>
<p>if you have a link or codes can you give it to me? maybe i can try it… (m</p>

WaterKnight: <aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/muellerzr/Practical-Deep-Learning-for-Coders-2.0/blob/master/Computer%20Vision/04_Segmentation.ipynb" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/muellerzr/Practical-Deep-Learning-for-Coders-2.0/blob/master/Computer%20Vision/04_Segmentation.ipynb" target="_blank" rel="nofollow noopener">muellerzr/Practical-Deep-Learning-for-Coders-2.0/blob/master/Computer Vision/04_Segmentation.ipynb</a></h4>
<pre><code class="lang-ipynb">{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Image Segmentation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Run once per session\n",
    "!pip install fastai2 -q"
   ]
  },
  {
</code></pre>

  This file has been truncated. <a href="https://github.com/muellerzr/Practical-Deep-Learning-for-Coders-2.0/blob/master/Computer%20Vision/04_Segmentation.ipynb" target="_blank" rel="nofollow noopener">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


Adithia_Jo: <aside class="quote no-group quote-modified" data-username="WaterKnight" data-post="4" data-topic="77554">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/waterknight/48/54725_2.png" class="avatar"> WaterKnight:</div>
<blockquote>
<p>[ “<span class="hashtag-raw">#Run</span> once per session\n”, “!pip install fastai2 -q” ] }, {</p>
</blockquote>
</aside>
<p>thank you i will try it later , but i want to try use my codes use models etc, just has simple problem but dont know how to solve that (num_classes and how to give it to label)</p>

Adithia_Jo: <p>i see the fastai that"s good but that make me confused :), where save_path ,where the classes, how to label (ex: red,black,white) etc , thank you</p>

WaterKnight: <p>There is a segmentation data loader that does this for you, the mask is an image with int values each value represents a class. In that reposotory are linked some videos that explain how to use it!!!</p>
<p>You are welcome !</p>