0fc111a5d3bf20727c4657fdaa03700d1a1a6e90 — David Hamner 1 year, 3 months ago
4 files changed, 383 insertions(+), 0 deletions(-)

A lgpl-3.0.txt
A setup.txt
A startup.sh
A think.py
A  => lgpl-3.0.txt +165 -0
@@ 1,165 @@
                       Version 3, 29 June 2007

 Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
 Everyone is permitted to copy and distribute verbatim copies
 of this license document, but changing it is not allowed.

  This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.

  0. Additional Definitions.

  As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.

  "The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.

  An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.

  A "Combined Work" is a work produced by combining or linking an
Application with the Library.  The particular version of the Library
with which the Combined Work was made is also called the "Linked

  The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.

  The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.

  1. Exception to Section 3 of the GNU GPL.

  You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.

  2. Conveying Modified Versions.

  If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified

   a) under this License, provided that you make a good faith effort to
   ensure that, in the event an Application does not supply the
   function or data, the facility still operates, and performs
   whatever part of its purpose remains meaningful, or

   b) under the GNU GPL, with none of the additional permissions of
   this License applicable to that copy.

  3. Object Code Incorporating Material from Library Header Files.

  The object code form of an Application may incorporate material from
a header file that is part of the Library.  You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:

   a) Give prominent notice with each copy of the object code that the
   Library is used in it and that the Library and its use are
   covered by this License.

   b) Accompany the object code with a copy of the GNU GPL and this license

  4. Combined Works.

  You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:

   a) Give prominent notice with each copy of the Combined Work that
   the Library is used in it and that the Library and its use are
   covered by this License.

   b) Accompany the Combined Work with a copy of the GNU GPL and this license

   c) For a Combined Work that displays copyright notices during
   execution, include the copyright notice for the Library among
   these notices, as well as a reference directing the user to the
   copies of the GNU GPL and this license document.

   d) Do one of the following:

       0) Convey the Minimal Corresponding Source under the terms of this
       License, and the Corresponding Application Code in a form
       suitable for, and under terms that permit, the user to
       recombine or relink the Application with a modified version of
       the Linked Version to produce a modified Combined Work, in the
       manner specified by section 6 of the GNU GPL for conveying
       Corresponding Source.

       1) Use a suitable shared library mechanism for linking with the
       Library.  A suitable mechanism is one that (a) uses at run time
       a copy of the Library already present on the user's computer
       system, and (b) will operate properly with a modified version
       of the Library that is interface-compatible with the Linked

   e) Provide Installation Information, but only if you would otherwise
   be required to provide such information under section 6 of the
   GNU GPL, and only to the extent that such information is
   necessary to install and execute a modified version of the
   Combined Work produced by recombining or relinking the
   Application with a modified version of the Linked Version. (If
   you use option 4d0, the Installation Information must accompany
   the Minimal Corresponding Source and Corresponding Application
   Code. If you use option 4d1, you must provide the Installation
   Information in the manner specified by section 6 of the GNU GPL
   for conveying Corresponding Source.)

  5. Combined Libraries.

  You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:

   a) Accompany the combined library with a copy of the same work based
   on the Library, uncombined with any other library facilities,
   conveyed under the terms of this License.

   b) Give prominent notice with the combined library that part of it
   is a work based on the Library, and explaining where to find the
   accompanying uncombined form of the same work.

  6. Revised Versions of the GNU Lesser General Public License.

  The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.

  Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.

  If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the

A  => setup.txt +19 -0
@@ 1,19 @@
Setup detectron2 and big sleep

sudo pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113
sudo python3 -m pip install 'git+https://github.com/facebookresearch/detectron2.git'

git clone https://github.com/facebookresearch/detectron2.git
cd detectron2/demo/
python3 demo.py --config-file ../configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml \
  --input input1.jpg input2.jpg

sudo pip3 install big-sleep

#sudo pip3 install cupy-cuda113
#sudo pip3 install waifu2x

A  => startup.sh +5 -0
@@ 1,5 @@
#Limit memory use
cd "$(dirname "$0")"
LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2 python3 ./think.py

A  => think.py +194 -0
@@ 1,194 @@
import time
import torch, detectron2
import shutil
import glob
import random
import cv2
TORCH_VERSION = ".".join(torch.__version__.split(".")[:2])
CUDA_VERSION = torch.__version__.split("+")[-1]
print("torch: ", TORCH_VERSION, "; cuda: ", CUDA_VERSION)
print("detectron2:", detectron2.__version__)
# Some basic setup:
# Setup detectron2 logger
import detectron2
from detectron2.utils.logger import setup_logger
# import some common libraries
import numpy as np
import os, json, cv2, random

from big_sleep import Imagine

# import some common detectron2 utilities
from detectron2 import model_zoo
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog, DatasetCatalog
cfg = get_cfg()
# add project-specific config (e.g., TensorMask) here if you're not running a model in detectron2's core library
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5  # set threshold for this model
# Find a model from detectron2's model zoo. You can use the https://dl.fbaipublicfiles... url as well
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
predictor = DefaultPredictor(cfg)

script_path = os.path.realpath(os.path.abspath(__file__))
script_dir = os.path.dirname(script_path)
img_name = f"{script_dir}/think.png"

descriptive_words = ["distorted tendrils and roots sprouting", "shaded", "colorful", "flat", "grouped", "dripping", "gradient", "Out of focus", "random", "reflection mirror room", "van gogh"]

#im = cv2.imread(img_name)

def what_is_this(file_name):
    global eyes_busy
    global predictor
    eyes_busy = True
    found_objs = []
    im = cv2.imread(file_name)

    with torch.no_grad():
        outputs = predictor(im)
    instances = outputs["instances"]
    detected_class_indexes = instances.pred_classes
    prediction_boxes = instances.pred_boxes
    metadata = MetadataCatalog.get(cfg.DATASETS.TRAIN[0])
    class_catalog = metadata.thing_classes

    #Thank you to: https://stackoverflow.com/a/68471952
    for idx, coordinates in enumerate(prediction_boxes):
        class_index = detected_class_indexes[idx]
        class_name = class_catalog[class_index]
        print(class_name, coordinates)

    #Clean up ram
    eyes_busy = False
    #cmd = f"lumi predict --checkpoint=fast".split(" ")
    #OUT = subprocess.Popen(cmd, stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
    #stdout,stderr = OUT.communicate()
    #stdout = stdout.decode().strip()
    #if "\n" in stdout:
    #    raw_text = stdout.split('\n')[-1]
    #    result = json.loads(raw_text)
    #    eyes_busy = False
    #    for obj in result['objects']:
    #        name = obj['label']
    #        if name not in found_objs:
    #            found_objs.append(name)
    if found_objs != []:
        new_name = "_".join(found_objs)
        old_name = file_name.split('/')[-1]
        full_new_name = os.path.dirname(file_name)
        full_new_name = f"{full_new_name}/{new_name}_{old_name}"
        #os.rename(file_name, full_new_name)

fav_thing = "Frogs diving"
last_saw = fav_thing
dream_obj = Imagine(
        text = last_saw,
        lr = 0.077,
        save_every = 1,
        save_progress = True,

def dream(of_this):
    file_name_as_text = "_".join(of_this.split())
    expected_name = f"{script_dir}/{file_name_as_text}.png"
    os.rename(expected_name, img_name)

def get_non_avg_color():
    myimg = cv2.imread(img_name)
    avg_color_per_row = np.average(myimg, axis=0)
    avg_color = np.average(avg_color_per_row, axis=0)
    red,green,blue = tuple(255 - c for c in avg_color)
    if red > green and red > blue:
    elif green > red and green > blue:

index = 0
terms_found = {}
while True:
    index = index + 1
    print(f"Dreaming {last_saw}")
    what_we_might_dream_next = what_is_this(img_name)
    for found in what_we_might_dream_next:
        if found not in terms_found.keys():
            terms_found[found] = 1
            terms_found[found] = terms_found[found] + 1
    #Take the most unseen terms for the subject
    if len(terms_found.keys()) > 3:
        marklist = sorted(terms_found.items(), key=lambda x:x[1])
        sortdict = dict(marklist)        
        what_we_might_dream_next = list(sortdict.keys())[:3]
    elif terms_found != {}:
        print("Using short list")
        what_we_might_dream_next = list(terms_found.keys())
    out_dir = f"{script_dir}/output/{index}/"
    for image in glob.glob(f"{script_dir}/*.png"):
        if image.endswith("think.png"):
        short_name = image.split("/")[-1]
        full_path = f"{out_dir}{short_name}"
        os.rename(image, full_path)
    if what_we_might_dream_next != []:
        input_text = " ".join(what_we_might_dream_next)
        next_img_name = f"{script_dir}/output/{index}_{last_saw}.png"
        if input_text != last_saw:
            shutil.copyfile(img_name, next_img_name)
            last_saw = input_text
            print(f"\n\nNew subject: {last_saw}")
            #add some color to image
            shutil.copyfile(img_name, next_img_name)
            #last_saw = f"hazy out of focus with {get_non_avg_color()} something"
            descriptive_bits = " ".join(random.choices(descriptive_words, k=random.randint(1,2)))
            #if " " in last_saw:
            #    last_saw = last_saw.split(" ")[0]
            last_saw = f"{descriptive_bits}"
            print(f"\n\nFix: {last_saw}")