-1

I am working on an image segmentation problem but I faced this issue. I am trying to create encode labels with multi diminutional array but it need to be flatten, encode and reshape

--------------------------------------------------------------------------- ValueError                                Traceback (most recent call last) 
/var/folders/5m/kbzbl3b92tz0jp0jsmk20kp00000gn/T/ipykernel_83886/1032773058.py in <module> 
3 from sklearn.preprocessing import LabelEncoder 
4 labelencoder = LabelEncoder() 
5 n, h, w = train_masks.shape 
6 train_masks_reshaped = train_masks.reshape(-1,1) 
7 train_masks_reshaped_encoded = labelencoder.fit_transform(train_masks_reshaped)

ValueError: not enough values to unpack (expected 3, got 1)

Here is the piece of code

from keras.utils import normalize
import glob
import numpy as np

SIZE_X = 128 
SIZE_Y = 128
n_classes=4 

#Capture training image info as a list
train_images = []

for directory_path in glob.glob("128_patches/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("128_patches/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... 
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)

np.unique(train_masks_encoded_original_shape)

#-----------------------------------------
train_images = np.expand_dims(train_images, axis=3)
train_images = normalize(train_images, axis=1)

train_masks_input = np.expand_dims(train_masks_encoded_original_shape, axis=3)

#Create a subset of data for quick testing
#Picking 10% for testing and remaining for training
from sklearn.model_selection import train_test_split
X1, X_test, y1, y_test = train_test_split(train_images, train_masks_input, test_size = 0.10, random_state = 0)

#Further split training data t a smaller subset for quick testing of models
X_train, X_do_not_use, y_train, y_do_not_use = train_test_split(X1, y1, test_size = 0.2, random_state = 0)

print("Class values in the dataset are ... ", np.unique(y_train))  # 0 is the background/few unlabeled 

from keras.utils import to_categorical
train_masks_cat = to_categorical(y_train, num_classes=n_classes)
y_train_cat = train_masks_cat.reshape((y_train.shape[0], y_train.shape[1], y_train.shape[2], n_classes))



test_masks_cat = to_categorical(y_test, num_classes=n_classes)
y_test_cat = test_masks_cat.reshape((y_test.shape[0], y_test.shape[1], y_test.shape[2], n_classes))



#-----------------------------------------
from sklearn.utils import class_weight
class_weights = class_weight.compute_class_weight('balanced',
                                                 np.unique(train_masks_reshaped_encoded),
                                                 train_masks_reshaped_encoded)
print("Class weights are...:", class_weights)

This is the piece of code trying to encode labels

#Encode labels... 
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)

np.unique(train_masks_encoded_original_shape)

Can you please help me fix this issue? Thank you!

Bet
  • 19
  • 2

1 Answers1

1
for directory_path in glob.glob("128_patches/masks/"):
        for mask_path in glob.glob(os.path.join(directory_path, "*.tif")):
            mask = cv2.imread(mask_path, 0)

if the mask_path is error, the mask is None, the train_masks will be a tuple contains one item.

Xiaomin Wu
  • 400
  • 1
  • 5