python_for_microscopists icon indicating copy to clipboard operation
python_for_microscopists copied to clipboard

ValueError LabelEncoder

Open akn20222 opened this issue 2 years ago • 2 comments

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 n, h, w = train_masks.shape ValueError: not enough values to unpack (expected 3, got 1)

akn20222 avatar Aug 31 '22 15:08 akn20222

source video: https://www.youtube.com/watch?v=XyX5HNuv-xE&list=PLZsOBAyNTZwbR08R959iCvYT3qzhxvGOE&index=13

akn20222 avatar Aug 31 '22 15:08 akn20222

Hi how you created the environment?

anamce avatar Nov 27 '23 11:11 anamce