Example #1
0
def show_all_dream_image_layers(file_name = "test.png",show=True):
   """
   Args:   
      file_name: image to deep dream on
      show: to show the image
   
   Saves and shows the dream image for all 10 layers
   """
   for num in range(1,11):
      layer_tensor = model.layer_tensors[num]

      img_result = load_image(filename='{}'.format(file_name))

      img_result = recursive_optimize(layer_tensor=layer_tensor, image=img_result,
                     # how clear is the dream vs original image        
                     num_iterations=100, step_size=1.0, rescale_factor=0.5,
                     # How many "passes" over the data. More passes, the more granular the gradients will be.
                     num_repeats=8, blend=0.2)

      img_result = np.clip(img_result, 0.0, 255.0)
      img_result = img_result.astype(np.uint8)
      result = PIL.Image.fromarray(img_result, mode='RGB')
      result.save('{}_{}.png'.format(file_name[:-4], num))
      if show:
         result.show()
Example #2
0
def DreamImage(fileDirectory, layers, iterations, stepSize, rescaleFactor,
               repeats, blend, pictureName, outputDirectory):
    """
    Amplyfies the patterns the inception model sees in one image.
    """

    # Load the input image.
    filePath = fileDirectory + pictureName
    baseImage = load_image(filename='{}'.format(filePath))

    # Loop through all passed layers, amplify the inception model patterns.
    for layer in layers:
        try:
            img_result = recursive_optimize(
                layer_tensor=model.layer_tensors[layer],
                image=baseImage,
                num_iterations=iterations,
                step_size=stepSize,
                rescale_factor=rescaleFactor,
                num_repeats=repeats,
                blend=blend)

            # Save the output image.
            img_result = np.clip(img_result, 0.0, 255.0)
            img_result = img_result.astype(np.uint8)
            result = PIL.Image.fromarray(img_result, mode='RGB')
            result.save(
                "{}Layer{}_Iter{}_Step{}_Rescale{}_Repeats{}_Blend{}.jpg".
                format(outputDirectory, layer, iterations, stepSize,
                       rescaleFactor, repeats, blend))
        except Exception as ex:
            print(ex)
Example #3
0
def processing(filename):
    from deepdreamer import model, load_image, recursive_optimize
    import numpy as np
    import PIL.Image

    layer_tensor = model.layer_tensors[1]
    img_result = load_image(filename='{}'.format(filename))

    img_result = recursive_optimize(
        layer_tensor=layer_tensor,
        image=img_result,
        # how clear is the dream vs original image
        num_iterations=5,
        step_size=1.0,
        rescale_factor=0.5,
        # How many "passes" over the data. More passes, the more granular the gradients will be.
        num_repeats=5,
        blend=1)

    img_result = np.clip(img_result, 0.0, 255.0)
    img_result = img_result.astype(np.uint8)
    result = PIL.Image.fromarray(img_result, mode='RGB')
    filename = filename.split(".")
    filename = str(filename[0]) + "_out.jpg"
    result.save(f'{filename}')
Example #4
0
def process(img):
    img = recursive_optimize(
        layer_tensor=layer_tensor,
        image=img,
        # how clear is the dream vs original image
        num_iterations=20,
        step_size=1.0,
        rescale_factor=0.5,
        # How many "passes" over the data. More passes, the more granular the gradients will be.
        num_repeats=6,
        blend=0.2)
    img -= (np.max(img) - np.min(img)) / 2
    img = np.clip(img, 0.0, 255.0)
    img = img.astype(np.uint8)
    return img
Example #5
0
    def singleDream(self, output, layer=3):
        # Chose Layer to enhance
        layer_tensor = model.layer_tensors[layer]
        file_name = self.__image
        img_result = load_image(filename='{}'.format(file_name))

        # Call recursive optimization to generate dream
        img_result = recursive_optimize(
            layer_tensor=layer_tensor,
            image=img_result,
            # how clear is the dream vs original image
            num_iterations=20,
            step_size=1.0,
            rescale_factor=0.5,
            # How many "passes" over the data. More passes, the more granular the gradients will be.
            num_repeats=8,
            blend=0.2)
        # Make sure output results are valid pixel values
        img_result = np.clip(img_result, 0.0, 255.0)
        img_result = img_result.astype(np.uint8)
        # Generate and save image out of numpy array
        result = PIL.Image.fromarray(img_result, mode='RGB')
        result.save("./dream/" + self.__name + "/" + output)
Example #6
0
from deepdreamer import model, load_image, recursive_optimize
import numpy as np
import PIL.Image

# determines layer to apply to image
layerTensor = model.layer_tensors[1]

# instantiates img filename and img object
filename = "casey-horner-1268624-unsplash.jpg"
img = load_image(filename='{}'.format(filename))

img_result = recursive_optimize(
    layer_tensor=layerTensor,
    image=img,
    # determines image clarity
    num_iterations=20,
    step_size=1.0,
    rescale_factor=0.5,
    # How many passes over the data. More passes means more granular gradients
    num_repeats=8,
    blend=0.2)

img_result = np.clip(img_result, 0.0, 255.0)
img_result = img_result.astype(np.uint8)
result = PIL.Image.fromarray(img_result, mode='RGB')
result.save('dream_image_out.jpg')
result.show()
Example #7
0
        img_result = img_result[0 + x_trim:y_size - y_trim,
                                0 + y_trim:x_size - x_trim]
        img_result = cv2.resize(img_result, (x_size, y_size))

        img_result[:, :, 0] += 2
        img_result[:, :, 1] += 2
        img_result[:, :, 2] += 2

        img_result = np.clip(img_result, 0.0, 255.0)
        img_result = img_result.astype(np.uint8)

        img_result = recursive_optimize(
            layer_tensor=layer_tensor,
            image=img_result,
            # how clear is the dream vs original image
            num_iterations=15,
            step_size=1.0,
            rescale_factor=0.7,
            # How many "passes" over the data. More passes, the more granular the gradients will be.
            num_repeats=1,
            blend=0.2)

        img_result = np.clip(img_result, 0.0, 255.0)
        img_result = img_result.astype(np.uint8)
        result = PIL.Image.fromarray(img_result, mode='RGB')
        result.save('images/img{}.jpg'.format(i + 1))

        counts += 1
        if counts > max_count:
            break
        img_result = img_result[0 + x_trim:y_size - y_trim,
                                0 + y_trim:x_size - x_trim]
        img_result = cv2.resize(img_result, (x_size, y_size))

        # for modifying the general colors and brightness of results.

        # +2 is slowly dimmer
        # +3 is slowly brighter
        img_result[:, :, 0] += 2  #reds
        img_result[:, :, 1] += 2  #greens
        img_result[:, :, 2] += 2  #blues

        img_result = np.clip(img_result, 0.0, 255.0)
        img_result = img_result.astype(np.uint8)

        img_result = recursive_optimize(layer_tensor=layer_tensor,
                                        image=img_result,
                                        num_iterations=20,
                                        step_size=1.0,
                                        rescale_factor=0.7,
                                        num_repeats=1,
                                        blend=0.2)

        img_result = np.clip(img_result, 0.0, 255.0)
        img_result = img_result.astype(np.uint8)
        result = PIL.Image.fromarray(img_result, mode='RGB')
        result.save('dream/{}/img_{}.jpg'.format(dream_name, i + 1))

        created_count += 1
        if created_count > max_count:
            break
Example #9
0
		#results tend to get dimmer or brighter over time , so you want to 
		#mannually adjust this over time
		
		#+2 is slowly dimmer 
		#+3 is slowly brighter
		brightness_d = sqrt(0.241*img_result[:,:,0]+0.691*img_result[:,:,1]+0.068*img_result[:,:,2])
		if(brightness_d<Avg_brightness):
			img_result[:,:,0] += r.randint(3,4) #red
			img_result[:,:,1] += r.randint(3,4) #green
			img_result[:,:,2] += r.randint(3,4) #blue
		img_result = np.clip(img_result, 0.0 , 255.0)
		img_result = img_result.astype(np.uint8)
		
		img_result = recursive_optimize(layer_tensor=model.layer_tensors[r.randint(0,11)],
										image = img_result,
										num_iterations = 2,
										step_size = 1.0,
										rescale_factor = 0.7,
										num_repeats = 8,
										blend = 0.2)
										
		img_result = np.clip(img_result,0.0,255.0)
		img_result = img_result.astype(np.uint8)
		result = PIL.Image.fromarray(img_result,mode = 'RGB')
		result.save('dream/{}/img_{}.jpg'.format(dream_name,i+1))
		
		created_count+=1
		if created_count > max_count:
			break
											
Example #10
0
def DreamFrames(dreamDirectory, layers, iterations, stepSize, rescaleFactor, repeats, blend
                , fps, dreamName, dreamLength, changeLayerAfterSec, trimPercent):
    """
    Creates and saves the frames (images) for a dream video.
    """

    # Randomly chose initial values for RGB correction.
    redCorrection = int(random.choice("23"))
    greenCorrection = int(random.choice("23"))
    blueCorrection = int(random.choice("23"))

    for i in range(0, dreamLength * fps):
        # Check if the image has already been processed.
        if not os.path.isfile("{}{}/img_{}.jpg".format(dreamDirectory, dreamName, i + 1)):
            # Load the input/previous image.
            img_result = load_image(filename="{}{}/img_{}.jpg".format(dreamDirectory, dreamName, i))

            # Get the size of the image.
            xSize = len(img_result[0])
            ySize = len(img_result)

            # Trim the image to create a "zoom-in" effect.
            xTrim = math.ceil(xSize * trimPercent / 100)
            yTrim = math.ceil(ySize * trimPercent / 100)
            img_result = img_result[yTrim:ySize - yTrim - 9, xTrim:xSize - xTrim - 9]
            img_result = imresize(img_result, (ySize, xSize), "nearest")

            # Adjust the brightness of the image by ensuring that the red green and blue portions of the 
            # image are above/below the defined thresholds.
            upperBrightnessLevel = xSize*ySize*255*0.8
            lowerBrightnessLevel = xSize*ySize*255*0.2
            redSum = numpy.sum(img_result[:, :, 0])
            if redSum < lowerBrightnessLevel:
                redCorrection = int(random.choice("34"))
            elif redSum > upperBrightnessLevel:
                redCorrection = int(random.choice("12"))
            img_result[:, :, 0] += redCorrection
            greenSum = numpy.sum(img_result[:, :, 0])
            if greenSum < lowerBrightnessLevel:
                greenCorrection = int(random.choice("34"))
            elif greenSum > upperBrightnessLevel:
                greenCorrection = int(random.choice("12"))
            img_result[:, :, 1] += greenCorrection
            blueSum = numpy.sum(img_result[:, :, 0])
            if blueSum < lowerBrightnessLevel:
                blueCorrection = int(random.choice("34"))
            elif blueSum > upperBrightnessLevel:
                blueCorrection = int(random.choice("12"))
            img_result[:, :, 2] += blueCorrection
            img_result = np.clip(img_result, 0.0, 255.0)
            img_result = img_result.astype(np.uint8)

            # Select the inception model layer for next dream iteration.
            framesPerLayer = fps * changeLayerAfterSec
            # Option 1: Automatically loop through layers 10..2
            #layer = int(11 - (math.ceil((i - math.floor(i/(9*framesPerLayer))*(9*framesPerLayer)) / framesPerLayer)))
            # Option 2: Cycle through layers list
            layer = layers[int(math.ceil(i - (math.floor(i/(framesPerLayer*len(layers)))) * (framesPerLayer*len(layers)))/framesPerLayer)]

            # Select the number of iterations for the selected layer.
            iterations = int(min(21.0, layer * math.pi) + 2)

            # Amplify the inception model patterns in the image.
            img_result = recursive_optimize(layer_tensor=model.layer_tensors[layer],
                                            image=img_result,
                                            num_iterations=iterations,
                                            step_size=stepSize,
                                            rescale_factor=rescaleFactor,
                                            num_repeats=1,
                                            blend=blend)

            # Save the output image.
            img_result = np.clip(img_result, 0.0, 255.0)
            img_result = img_result.astype(np.uint8)
            result = PIL.Image.fromarray(img_result, mode="RGB")
            print("{}{}/img_{}.jpg".format(dreamDirectory, dreamName, i + 1))
            result.save("{}{}/img_{}.jpg".format(dreamDirectory, dreamName, i + 1))
        else:
            print("Already processed image {}".format(i))
Example #11
0
                                0 + y_trim:x_size - x_trim]
        img_result = cv2.resize(img_result, (x_size, y_size))

        # Use these to modify the general colors and brightness of results.
        # results tend to get dimmer or brighter over time, so you want to
        # manually adjust this over time.

        # +2 is slowly dimmer
        # +3 is slowly brighter

        img_result[:, :, 0] += random.choice([2, 2.5, 3])  # reds
        img_result[:, :, 1] += random.choice([2, 2.5, 3])  # greens
        img_result[:, :, 2] += random.choice([2, 2.5, 3])  # blues

        img_result = np.clip(img_result, 0.0, 255.0)
        img_result = img_result.astype(np.uint8)

        img_result = recursive_optimize(
            layer_tensor=layer_tensor,
            image=img_result,
            num_iterations=num_iterations,
            step_size=step_size,
            rescale_factor=rescale_factor,  #downscaling
            num_repeats=
            num_repeats,  #it's better to keep it one because every frame will change gradually
            blend=blend)  #how much filter effects on picture

        img_result = np.clip(img_result, 0.0, 255.0)
        img_result = img_result.astype(np.uint8)
        result = PIL.Image.fromarray(img_result, mode='RGB')
        result.save('dreams/{}/img_{}.jpg'.format(dream_name, i + 1))
from deepdreamer import model, load_image, recursive_optimize
import numpy as np
import PIL.Image
import random

layer_tensor = model.layer_tensors[10]
file_name = "8.jpeg"
img = load_image(filename='{}'.format(file_name))

img_result = recursive_optimize(layer_tensor=layer_tensor,
                                image=img,
                                num_iterations=25,
                                step_size=2,
                                rescale_factor=0.5,
                                num_repeats=8,
                                blend=0.2)

img_result = np.clip(img_result, 0.0, 255.0)
img_result = img_result.astype(np.uint8)
result = PIL.Image.fromarray(img_result, mode='RGB')
result.save(file_name + '_out.jpg')
result.show()
Example #13
0
def create_dream_frames(file_name = "test.png", target_dream_layer = 3, total_frames=5, zoom_in=False):
    """
    Args:
        file_name: orginal image file
        target_dream_layer: targeted dream layer style
        total_frames: total number of frames of dream images to generate
    Returns:
        dream_layer_dir_path: directory path storing the frames
        file_name: name of original image file
    Creates the total number of frames, inside dream_frames/img_name
    """
    curr_path = os.getcwd()
    dream_frames_path = os.path.join(curr_path, 'dream_frames')
    # zoom-in effect directory
    if zoom_in:
        dream_frames_path = dream_frames_path + '_zoom_in'

    # check and create '/dream_frames' directory
    if not os.path.exists(dream_frames_path):
        os.mkdir(dream_frames_path)

    # /file_name dir
    dream_dir_path = os.path.join(dream_frames_path, '{}'.format(format(file_name[:-4])))
    if not os.path.exists(dream_dir_path):
        os.mkdir(dream_dir_path)
    
    # /layer_num
    dream_layer_dir_path = os.path.join(dream_dir_path, 'layer_{}'.format(target_dream_layer))
    if not os.path.exists(dream_layer_dir_path):
        os.mkdir(dream_layer_dir_path)

    dream_layer_tensor = model.layer_tensors[target_dream_layer]

    # saves original image as starter image
    img_path = os.path.join(dream_layer_dir_path, 'img_0.png')
    result = PIL.Image.open(file_name)
    (x_size, y_size) = result.size
    result.save(img_path)

    # generates the frames
    for i in range(total_frames-1):
        img_result = load_image(filename='{}{}.png'.format(img_path[:-5], i))
        if zoom_in:
            x_trim = 10
            y_trim = 10
            img_result = img_result[0+x_trim:y_size-y_trim, 0+y_trim:x_size-x_trim]
            img_result = cv2.resize(img_result, (x_size, y_size))
            img_result[:, :, 0] += 3  # reds
            img_result[:, :, 1] += 3  # greens
            img_result[:, :, 2] += 3  # blues
            img_result = np.clip(img_result, 0.0, 255.0)
            img_result = img_result.astype(np.uint8)
        img_result = recursive_optimize(layer_tensor=dream_layer_tensor,
                                        image=img_result,
                                        num_iterations=15,
                                        step_size=1.0,
                                        rescale_factor=0.7,
                                        num_repeats=1,
                                        blend=0.2)

        img_result = np.clip(img_result, 0.0, 255.0)
        img_result = img_result.astype(np.uint8)
        result = PIL.Image.fromarray(img_result, mode='RGB')
        result.save('{}{}.png'.format(img_path[:-5], i+1))
        print('{}/{} DONE'.format(i+1,total_frames))
    return dream_layer_dir_path, file_name
Example #14
0
    def startDreaming(self):
        # Define dream name
        dream_name = self.__name
        # Create output folder for dream frames if needed
        if not os.path.exists("./dream/" + dream_name):
            os.makedirs("./dream/" + dream_name)
        # Copy initial image as the first Frame
        shutil.copyfile(self.__image, "./dream/" + dream_name + "/img_0.jpg")
        # Open first frame and get its size
        im = PIL.Image.open(self.__image)
        x_size, y_size = im.size
        # Dream for at most max_count frames
        created_count = 0
        max_count = 30 * 60 * 15  # Max of 15 min at 30 FPS
        for i in range(0, 9999999999999999):
            # Search for already existing frames
            if os.path.isfile('dream/{}/img_{}.jpg'.format(dream_name, i + 1)):
                print('{} already exists, continuing along...'.format(i + 1))

            else:
                # Define layer boundaries
                starting_layer = 1
                max_layer = 11
                change_after = 30
                # Calculate current layer - auto increase layers over time / over i
                layer = min(
                    int((1 / change_after) * i) + starting_layer, max_layer)
                print("Enhancing layer {}".format(layer))
                layer_tensor = model.layer_tensors[layer]
                # Loading image
                img_result = load_image(
                    filename='dream/{}/img_{}.jpg'.format(dream_name, i))

                # Define zoom speed in pixels
                x_trim = 2
                y_trim = 1
                # Zoom image
                img_result = img_result[0 + x_trim:y_size - y_trim,
                                        0 + y_trim:x_size - x_trim]
                img_result = cv2.resize(img_result, (x_size, y_size))

                # Use these to modify the general colors and brightness of results.
                # results tend to get dimmer or brighter over time, so you want to
                # manually adjust this over time.

                # +2 is slowly dimmer
                # +3 is slowly brighter
                img_result[:, :, 0] += 2  # reds
                img_result[:, :, 1] += 2  # greens
                img_result[:, :, 2] += 2  # blues

                # Again make sure to keep pixel values in range
                img_result = np.clip(img_result, 0.0, 255.0)
                img_result = img_result.astype(np.uint8)

                # Recursively optimize / dream image with 2 repeats per frame
                img_result = recursive_optimize(layer_tensor=layer_tensor,
                                                image=img_result,
                                                num_iterations=2,
                                                step_size=1.0,
                                                rescale_factor=0.5,
                                                num_repeats=1,
                                                blend=0.2)

                # Clip again
                img_result = np.clip(img_result, 0.0, 255.0)
                img_result = img_result.astype(np.uint8)

                # Performing histogram equalizaiton
                img_yuv = cv2.cvtColor(img_result, cv2.COLOR_BGR2YUV)

                # equalize the histogram of the Y channel
                img_yuv[:, :, 0] = cv2.equalizeHist(img_yuv[:, :, 0])

                # convert the YUV image back to RGB format
                equalized_image = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR)
                img_result = cv2.addWeighted(img_result, 0.98, equalized_image,
                                             0.02, 0)
                # Sharpen n times
                for s in range(0):
                    blurred = cv2.GaussianBlur(img_result, (3, 3), 0)
                    img_result = cv2.addWeighted(img_result, 1.5, blurred,
                                                 -0.5, 0)

                # Generate image from numpy array and save it into dream folder
                result = PIL.Image.fromarray(img_result, mode='RGB')
                result.save('dream/{}/img_{}.jpg'.format(dream_name, i + 1))

                created_count += 1
                if created_count > max_count:
                    break