python_for_microscopists
python_for_microscopists copied to clipboard
ValueError LabelEncoder
As can be seen below, the following code was used when training a Unet model through Spyder. I ended up taking a look at a Youtube tutorial by DigitalSreeni,in episode 208 where he looked at multi class semantic segmentation with the help of a Unet neural network. I recreated everything except the images (which are my own), but with the same dimensions, etc. Can anyone let me know what the issue is?
from simple_multi_unet_model import multi_unet_model #Uses softmax
from keras.utils import normalize import os import glob import cv2 import numpy as np from matplotlib import pyplot as plt
#Resizing images, if needed SIZE_X = 128 SIZE_Y = 128 n_classes=5 #Number of classes for segmentation
#Capture training image info as a list train_images = []
for directory_path in glob.glob("/Hydro/128bit/images/"):
for img_path in glob.glob(os.path.join(directory_path, "*.tif")):
img = cv2.imread(img_path, 0)
#img = cv2.resize(img, (SIZE_Y, SIZE_X))
train_images.append(img)
#Convert list to array for machine learning processing
train_images = np.array(train_images)
#Capture mask/label info as a list
train_masks = []
for directory_path in glob.glob("/Hydro/128bit/masks/"):
for mask_path in glob.glob(os.path.join(directory_path, "*.tif")):
mask = cv2.imread(mask_path, 0)
#mask = cv2.resize(mask, (SIZE_Y, SIZE_X), interpolation = cv2.INTER_NEAREST) #Otherwise ground truth changes due to interpolation
train_masks.append(mask)
#Convert list to array for machine learning processing
train_masks = np.array(train_masks)
############################################### #Encode labels... but multi dim array so need to flatten, encode and reshape from sklearn.preprocessing import LabelEncoder labelencoder = LabelEncoder() n, h, w = train_masks.shape train_masks_reshaped = train_masks.reshape(-1,1) train_masks_reshaped_encoded = labelencoder.fit_transform(train_masks_reshaped) train_masks_encoded_original_shape = train_masks_reshaped_encoded.reshape(n, h, w)
File "C:\Users\anish\208_multiclass_Unet_sandstone.py", line 63, in
source video: https://www.youtube.com/watch?v=XyX5HNuv-xE&list=PLZsOBAyNTZwbR08R959iCvYT3qzhxvGOE&index=13
Hi how you created the environment?