lstm例子generate_movies函数分析

demo地址:https://github.com/keras-team/keras/blob/2.3.0/examples/conv_lstm.py

import numpy as np


def generate_movies(n_samples=1200, n_frames=15):
    row = 80
    col = 80
    noisy__movies = np.zeros((n_samples, n_frames, row, col, 1), dtype=np.float)
    shifted__movies = np.zeros((n_samples, n_frames, row, col, 1), dtype=np.float)

    for i in range(n_samples):
        # Add 3 to 7 moving squares
        n = np.random.randint(3, 8)

        for j in range(n):
            # Initial position
            xstart = np.random.randint(20, 60)
            ystart = np.random.randint(20, 60)
            # Direction of motion
            directionx = np.random.randint(0, 3) - 1
            directiony = np.random.randint(0, 3) - 1

            # Size of the square
            w = np.random.randint(2, 4)

            for t in range(n_frames):
                x_shift = xstart + directionx * t
                y_shift = ystart + directiony * t
                noisy__movies[i, t, x_shift - w: x_shift + w, y_shift - w: y_shift + w, 0] += 1

                # Make it more robust by adding noise.
                # The idea is that if during inference,
                # the value of the pixel is not exactly one,
                # we need to train the network to be robust and still
                # consider it as a pixel belonging to a square.
                # if np.random.randint(0, 2):
                #     noise_f = (-1) ** np.random.randint(0, 2)
                #     noisy_movies[i, t,  x_shift - w - 1: x_shift + w + 1, y_shift - w - 1: y_shift + w + 1, 0] += noise_f * 0.1

                # Shift the ground truth by 1
                x_shift = xstart + directionx * (t + 1)
                y_shift = ystart + directiony * (t + 1)
                shifted__movies[i, t, x_shift - w: x_shift + w, y_shift - w: y_shift + w, 0] += 1

    # Cut to a 40x40 window
    noisy_movies = noisy__movies[::, ::, 20:60, 20:60, ::]
    shifted_movies = shifted__movies[::, ::, 20:60, 20:60, ::]
    noisy_movies[noisy_movies >= 1] = 1
    shifted_movies[shifted_movies >= 1] = 1
    return noisy_movies, shifted_movies


if __name__ == '__main__':
    noisy_movies, shifted_movies = generate_movies(n_samples=120)
    any_group_index = 0
    # 对于每一组(15帧)图片, 不加噪声的情况下, noisy_movies的1:15帧的图片和shifted_movies的0:14帧图片一模一样
    frames0 = noisy_movies[any_group_index][1:15]
    frames1 = shifted_movies[any_group_index][0:14]
    print((frames0 == frames1).all())
原文地址:https://www.cnblogs.com/iuyy/p/14066810.html