Ошибка в методе random_shadow в Utils.py Index Строка ошибки № 101 - PullRequest
0 голосов
/ 16 апреля 2019

Я пытался поработать над моделью и вот этот код прямо здесь. также я попытался типизировать функцию «маска». В приведенном ниже файле содержатся утилиты, необходимые для обучения моей модели.

PS: это код для самостоятельного вождения автомобиля.

Это была ошибка, которую я получаю после тренировки моей модели

->python model.py

Using TensorFlow backend.
------------------------------
Parameters
------------------------------
data_dir             := data
test_size            := 0.2
keep_prob            := 0.5
nb_epoch             := 10
samples_per_epoch    := 20000
batch_size           := 40
save_best_only       := True
learning_rate        := 0.0001
------------------------------
model.py:66: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(24, (5, 5), activation="elu", strides=(2, 2))`
  model.add(Conv2D(24, 5, 5, activation='elu', subsample=(2, 2)))
WARNING:tensorflow:From C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
model.py:67: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(36, (5, 5), activation="elu", strides=(2, 2))`
  model.add(Conv2D(36, 5, 5, activation='elu', subsample=(2, 2)))
model.py:68: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(48, (5, 5), activation="elu", strides=(2, 2))`
  model.add(Conv2D(48, 5, 5, activation='elu', subsample=(2, 2)))
model.py:69: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(64, (3, 3), activation="elu")`
  model.add(Conv2D(64, 3, 3, activation='elu'))
model.py:70: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(64, (3, 3), activation="elu")`
  model.add(Conv2D(64, 3, 3, activation='elu'))
WARNING:tensorflow:From C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
lambda_1 (Lambda)            (None, 66, 200, 3)        0
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 31, 98, 24)        1824
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 14, 47, 36)        21636
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 5, 22, 48)         43248
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 3, 20, 64)         27712
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 1, 18, 64)         36928
_________________________________________________________________
dropout_1 (Dropout)          (None, 1, 18, 64)         0
_________________________________________________________________
flatten_1 (Flatten)          (None, 1152)              0
_________________________________________________________________
dense_1 (Dense)              (None, 100)               115300
_________________________________________________________________
dense_2 (Dense)              (None, 50)                5050
_________________________________________________________________
dense_3 (Dense)              (None, 10)                510
_________________________________________________________________
dense_4 (Dense)              (None, 1)                 11
=================================================================
Total params: 252,219
Trainable params: 252,219
Non-trainable params: 0
_________________________________________________________________
model.py:120: UserWarning: The semantics of the Keras 2 argument `steps_per_epoch` is not the same as the Keras 1 argument `samples_per_epoch`. `steps_per_epoch` is the number of batches to draw from the generator at each epoch. Basically steps_per_epoch = samples_per_epoch/batch_size. Similarly `nb_val_samples`->`validation_steps` and `val_samples`->`steps` arguments have changed. Update your method calls accordingly.
  verbose=1)
model.py:120: UserWarning: Update your `fit_generator` call to the Keras 2 API: `fit_generator(<generator..., 20000, 10, validation_data=<generator..., callbacks=[<keras.ca..., verbose=1, validation_steps=489, max_queue_size=1)`
  verbose=1)
WARNING:tensorflow:From C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
Epoch 1/10
Traceback (most recent call last):
  File "model.py", line 163, in <module>
    main()
  File "model.py", line 159, in main
    train_model(model, args, *data)
  File "model.py", line 120, in train_model
    verbose=1)
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 1418, in fit_generator
    initial_epoch=initial_epoch)
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training_generator.py", line 181, in fit_generator
    generator_output = next(output_generator)
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\utils\data_utils.py", line 709, in get
    six.reraise(*sys.exc_info())
  File "C:\ProgramData\Anaconda3\lib\site-packages\six.py", line 693, in reraise
    raise value
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\utils\data_utils.py", line 685, in get
    inputs = self.queue.get(block=True).get()
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\pool.py", line 683, in get
    raise self._value
  File "C:\ProgramData\Anaconda3\lib\multiprocessing\pool.py", line 121, in worker
    result = (True, func(*args, **kwds))
  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\utils\data_utils.py", line 626, in next_sample
    return six.next(_SHARED_SEQUENCES[uid])
  File "C:\Users\BHAROSE\Desktop\Project\How_to_simulate_a_self_driving_car-master\How_to_simulate_a_self_driving_car-master\utils.py", line 149, in batch_generator
    image, steering_angle = augument(data_dir, center, left, right, steering_angle)
  File "C:\Users\BHAROSE\Desktop\Project\How_to_simulate_a_self_driving_car-master\How_to_simulate_a_self_driving_car-master\utils.py", line 131, in augument
    image = random_shadow(image)
  File "C:\Users\BHAROSE\Desktop\Project\How_to_simulate_a_self_driving_car-master\How_to_simulate_a_self_driving_car-master\utils.py", line 100, in random_shadow
    mask[np.where(float ((ym - y1) * (x2 - x1) - (y2 - y1) * (xm - x1))) > 0] = 1
TypeError: only size-1 arrays can be converted to Python scalars

Ниже приведен файл utils.py ## [https://github.com/PapaBravo87/Self-Driving-Car-Simulator

] 1

    import cv2, os
    import numpy as np
    import matplotlib.image as mpimg


    IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_CHANNELS = 66, 200, 3
    INPUT_SHAPE = (IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_CHANNELS)


    def load_image(data_dir, image_file):
        """
        Load RGB images from a file
        """
        return mpimg.imread(os.path.join(data_dir, image_file.strip()))


    def crop(image):
        """
        Crop the image (removing the sky at the top and the car front at the bottom)
        """
        return image[60:-25, :, :] # remove the sky and the car front


    def resize(image):
        """
        Resize the image to the input shape used by the network model
        """
        return cv2.resize(image, (IMAGE_WIDTH, IMAGE_HEIGHT), cv2.INTER_AREA)


    def rgb2yuv(image):
        """
        Convert the image from RGB to YUV (This is what the NVIDIA model does)
        """
        return cv2.cvtColor(image, cv2.COLOR_RGB2YUV)


    def preprocess(image):
        """
        Combine all preprocess functions into one
        """
        image = crop(image)
        image = resize(image)
        image = rgb2yuv(image)
        return image


    def choose_image(data_dir, center, left, right, steering_angle):
        """
        Randomly choose an image from the center, left or right, and adjust
        the steering angle.
        """
        choice = np.random.choice(3)
        if choice == 0:
            return load_image(data_dir, left), steering_angle + 0.2
        elif choice == 1:
            return load_image(data_dir, right), steering_angle - 0.2
        return load_image(data_dir, center), steering_angle


    def random_flip(image, steering_angle):
        """
        Randomly flipt the image left <-> right, and adjust the steering angle.
        """
        if np.random.rand() < 0.5:
            image = cv2.flip(image, 1)
            steering_angle = -steering_angle
        return image, steering_angle


    def random_translate(image, steering_angle, range_x, range_y):
        """
        Randomly shift the image virtially and horizontally (translation).
        """
        trans_x = range_x * (np.random.rand() - 0.5)
        trans_y = range_y * (np.random.rand() - 0.5)
        steering_angle += trans_x * 0.002
        trans_m = np.float32([[1, 0, trans_x], [0, 1, trans_y]])
        height, width = image.shape[:2]
        image = cv2.warpAffine(image, trans_m, (width, height))
        return image, steering_angle


    def random_shadow(image):
        """
        Generates and adds random shadow
        """
        # (x1, y1) and (x2, y2) forms a line
        # xm, ym gives all the locations of the image
        x1, y1 = IMAGE_WIDTH * np.random.rand(), 0
        x2, y2 = IMAGE_WIDTH * np.random.rand(), IMAGE_HEIGHT
        xm, ym = np.mgrid[0:IMAGE_HEIGHT, 0:IMAGE_WIDTH]

        # mathematically speaking, we want to set 1 below the line and zero otherwise
        # Our coordinate is up side down.  So, the above the line: 
        # (ym-y1)/(xm-x1) > (y2-y1)/(x2-x1)
        # as x2 == x1 causes zero-division problem, we'll write it in the below form:
        # (ym-y1)*(x2-x1) - (y2-y1)*(xm-x1) > 0
        mask = np.zeros_like(image[:, :, 1])
        mask[np.where(float ((ym - y1) * (x2 - x1) - (y2 - y1) * (xm - x1))) > 0] = 1

        # choose which side should have shadow and adjust saturation
        cond = mask == np.random.randint(2)
        s_ratio = np.random.uniform(low=0.2, high=0.5)

        # adjust Saturation in HLS(Hue, Light, Saturation)
        hls = cv2.cvtColor(image, cv2.COLOR_RGB2HLS)
        hls[:, :, 1][cond] = hls[:, :, 1][cond] * s_ratio
        return cv2.cvtColor(hls, cv2.COLOR_HLS2RGB)


    def random_brightness(image):
        """
        Randomly adjust brightness of the image.
        """
        # HSV (Hue, Saturation, Value) is also called HSB ('B' for Brightness).
        hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
        ratio = 1.0 + 0.4 * (np.random.rand() - 0.5)
        hsv[:,:,2] =  hsv[:,:,2] * ratio
        return cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB)


    def augument(data_dir, center, left, right, steering_angle, range_x=100, range_y=10):
        """
        Generate an augumented image and adjust steering angle.
        (The steering angle is associated with the center image)
        """
        image, steering_angle = choose_image(data_dir, center, left, right, steering_angle)
        image, steering_angle = random_flip(image, steering_angle)
        image, steering_angle = random_translate(image, steering_angle, range_x, range_y)
        image = random_shadow(image)
        image = random_brightness(image)
        return image, steering_angle


    def batch_generator(data_dir, image_paths, steering_angles, batch_size, is_training):
        """
        Generate training image give image paths and associated steering angles
        """
        images = np.empty([batch_size, IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_CHANNELS])
        steers = np.empty(batch_size)
        while True:
            i = 0
            for index in np.random.permutation(image_paths.shape[0]):
                center, left, right = image_paths[index]
                steering_angle = steering_angles[index]
                # argumentation
                if is_training and np.random.rand() < 0.6:
                    image, steering_angle = augument(data_dir, center, left, right, steering_angle)
                else:
                    image = load_image(data_dir, center) 
                # add the image and steering angle to the batch
                images[i] = preprocess(image)
                steers[i] = steering_angle
                i += 1
                if i == batch_size:
                    break
            yield images, steers

`

----------
...