Open dilip881 opened 2 years ago
In "Part 1 Data Preprocessing" inside the "DataGenerator2D" class go to the __load__()
function. Inside it, try switching id_name_actual, text, s_ = id_name.split('.')
to id_name_actual, text = id_name.split('.')
or id_name_actual, text, _ = id_name.split('.')
. In case that doesn't work, add print("Value:", id_name.split('.'))
above that line and show me the output.
Traceback (most recent call last): File "/Users/dilipreddy/Downloads/Lane-Segmentation-master/lane_segmentation.py", line 132, in <module> X, y = train_generator.__getitem__(0) File "/Users/dilipreddy/Downloads/Lane-Segmentation-master/lane_segmentation.py", line 107, in __getitem__ _img, _mask = self.__load__(id_name) File "/Users/dilipreddy/Downloads/Lane-Segmentation-master/lane_segmentation.py", line 80, in __load__ id_name_actual, text, _ = id_name.split('.') ValueError: not enough values to unpack (expected 3, got 2)
This is the Code :
`import matplotlib.pyplot as plt import numpy as np import cv2 import os import random import sys import tensorflow import keras from keras.layers import from keras.models import import os import os from tensorflow.keras.utils import Sequence import os from tensorflow.keras.utils import Sequence
Part 1 - Data Preprocessing
def get_mask(img_path, label_path): label_file = open(label_path, "r") if label_file.mode == 'r': contents = label_file.read() lines_text = contents.split('\n')
img = get_mask("data/CULane/driver_161_90frame/06030819_0755.MP4/00000.jpg", "data/CULane/driver_161_90frame/06030819_0755.MP4/00000.lines.txt") plt.imshow(img) print(img.shape)
class DataGenerator2D(Sequence): """Generates data for Keras Sequence based data generator. Suitable for building data generator for training and prediction. """
train_generator = DataGenerator2D(base_path='data', img_size=256, batchsize=64, shuffle=False) X, y ,s= train_generator.getitem(0) print(X.shape, y.shape)
fig = plt.figure(figsize=(17, 8)) columns = 4 rows = 3 for i in range(1, columns*rows + 1): img = X[i-1] fig.add_subplot(rows, columns, i) plt.imshow(img) plt.show()
fig = plt.figure(figsize=(17, 8)) columns = 4 rows = 3 for i in range(1, columns*rows + 1): img = y[i-1] fig.add_subplot(rows, columns, i) plt.imshow(img) plt.show()
Part 2 - Model
def unet(sz = (256, 256, 3)): x = Input(sz) inputs = x
down sampling
f = 8 layers = []
for i in range(0, 6): x = Conv2D(f, 3, activation='relu', padding='same') (x) x = BatchNormalization()(x) x = Conv2D(f, 3, activation='relu', padding='same') (x) x = BatchNormalization()(x) layers.append(x) x = MaxPooling2D() (x) x = BatchNormalization()(x) f = f*2 ff2 = 64
bottleneck
j = len(layers) - 1 x = Conv2D(f, 3, activation='relu', padding='same') (x) x = BatchNormalization()(x) x = Conv2D(f, 3, activation='relu', padding='same') (x) x = BatchNormalization()(x) x = Conv2DTranspose(ff2, 2, strides=(2, 2), padding='same') (x) x = BatchNormalization()(x) x = Concatenate(axis=3)([x, layers[j]]) j = j -1
upsampling
for i in range(0, 5): ff2 = ff2//2 f = f // 2 x = Conv2D(f, 3, activation='relu', padding='same') (x) x = BatchNormalization()(x) x = Conv2D(f, 3, activation='relu', padding='same') (x) x = BatchNormalization()(x) x = Conv2DTranspose(ff2, 2, strides=(2, 2), padding='same') (x) x = BatchNormalization()(x) x = Concatenate(axis=3)([x, layers[j]]) j = j -1
classification
x = Conv2D(f, 3, activation='relu', padding='same') (x) x = BatchNormalization()(x) x = Conv2D(f, 3, activation='relu', padding='same') (x) x = BatchNormalization()(x) outputs = Conv2D(1, 1, activation='sigmoid') (x)
model = Model(inputs=[inputs], outputs=[outputs])
return model
def iou(y_true, y_pred): def f(y_true, y_pred): intersection = (y_true * y_pred).sum() union = y_true.sum() + y_pred.sum() - intersection x = (intersection + 1e-15) / (union + 1e-15) x = x.astype(np.float32) return x
from tensorflow.keras import backend as K
def f1(y_true, y_pred): def recall(y_true, y_pred): """Recall metric.
def dice_coef(y_true, y_pred, smooth=1): """ Dice = (2|X & Y|)/ (|X|+ |Y|) = 2sum(|AB|)/(sum(A^2)+sum(B^2)) ref: https://arxiv.org/pdf/1606.04797v1.pdf """ intersection = K.sum(K.abs(y_true y_pred), axis=-1) return (2. * intersection + smooth) / (K.sum(K.square(y_true),-1) + K.sum(K.square(y_pred),-1) + smooth)
def dice_coef_loss(y_true, y_pred): return 1-dice_coef(y_true, y_pred)
COMPILING MODEL
model = unet()
opt = tf.keras.optimizers.Adam(0.001) metrics = ["acc", iou]
model.compile(loss=dice_coef_loss, optimizer=opt, metrics=metrics)
model.summary()
train_generator = DataGenerator2D('data/CU_Lane/content/data', img_size=256, batch_size=128, shuffle=True) val_generator = DataGenerator2D('data/CU_Lane/content/data', img_size=256, batch_size=128, shuffle=False)
Part 3 - Training
history = model.fit_generator(generator=train_generator, validation_data=val_generator, steps_per_epoch=200, validation_steps=5, epochs=10)
print(history.history.keys())
Part 4 - Visualization
summarize history for accuracy
plt.plot(history.history['acc']) plt.plot(history.history['val_acc']) plt.title('model accuracy') plt.ylabel('accuracy') plt.xlabel('epoch') plt.legend(['train', 'val'], loc='upper left') plt.show()
summarize history for loss
plt.plot(history.history['loss']) plt.plot(history.history['val_loss']) plt.title('model loss') plt.ylabel('loss') plt.xlabel('epoch') plt.legend(['train', 'val'], loc='upper left') plt.show()
val_generator = DataGenerator2D('content/data/', img_size=256,batch_size=128, shuffle=True) X, y = val_generator.getitem(10) print(X.shape, y.shape)
plt.imshow(X[2])
predict = model.predict(X) print(predict.shape) img = cv2.cvtColor(predict[2], cv2.COLOR_GRAY2BGR) plt.imshow(img)`