def iterate(self): THIS_DIR = os.getcwd() os.chdir(os.path.join(ANNEX_DIR, self.base_path)) try: iter_num = len(self.getAssetsByTagName(ASSET_TAGS['DLXDD_DD'])) bc = BatCountry(os.path.join(getConfig('caffe_root'), "models", "bvlc_googlenet")) img = bc.dream(np.float32(self.get_image(file_name="dream_%d.jpg" % iter_num))) bc.cleanup() os.chdir(THIS_DIR) iter_num += 1 dream = Image.fromarray(np.uint8(img)) asset_path = self.addAsset(None, "dream_%d.jpg" % iter_num, \ tags=[ASSET_TAGS['DLXDD_DD']], description="deep dream iteration") if asset_path is not None: dream.save(os.path.join(ANNEX_DIR, asset_path)) return True except Exception as e: print "ERROR ON ITERATION:" print e, type(e) return False
def POST(self): data = web.input(auth_code='', image={}) if data.auth_code == '': raise web.Forbidden() if data.auth_code != config.auth_code: raise web.Forbidden() if 'image' not in data: raise web.BadRequest() input_image = Image.open(data['image'].file) bc = BatCountry(os.environ['CAFFE_HOME'] + '/models/bvlc_googlenet') result_data = bc.dream(np.float32(input_image), end='inception_3b/5x5_reduce') bc.cleanup() result_image = Image.fromarray(np.uint8(result_data)) result = io.BytesIO() result_image.save(result, 'PNG') result.seek(0) web.header('Content-type', 'image/png') return result.read()
def iterate(self): THIS_DIR = os.getcwd() os.chdir(os.path.join(ANNEX_DIR, self.base_path)) try: iter_num = len(self.getAssetsByTagName(ASSET_TAGS['DLXDD_DD'])) bc = BatCountry( os.path.join(getConfig('caffe_root'), "models", "bvlc_googlenet")) img = bc.dream( np.float32(self.get_image(file_name="dream_%d.jpg" % iter_num))) bc.cleanup() os.chdir(THIS_DIR) iter_num += 1 dream = Image.fromarray(np.uint8(img)) asset_path = self.addAsset(None, "dream_%d.jpg" % iter_num, \ tags=[ASSET_TAGS['DLXDD_DD']], description="deep dream iteration") if asset_path is not None: dream.save(os.path.join(ANNEX_DIR, asset_path)) return True except Exception as e: print "ERROR ON ITERATION:" print e, type(e) return False
def dream_that_image(before, after, layer, seed, filehash, iteration): # dreaming... mydebugmsg("Dreaming dream #" + str(iteration)) mydebugmsg("before = [" + before + "]") mydebugmsg("after = [" + after + "]") bc = BatCountry(DREAMMODEL) features = bc.prepare_guide(Image.open(seed), end=layer) image = bc.dream(np.float32(Image.open(before)), end=layer, iter_n=20, objective_fn=BatCountry.guided_objective, objective_features=features, verbose=VERBOSITY) bc.cleanup() # # write the output image to file # result = Image.fromarray(np.uint8(image)) result.save(after) # # Save both the input image and output image to S3 using the MD5 hash of the original file content as the key name # keyname = filehash + ".jpg" key = beforebucket.new_key(keyname) key.set_contents_from_filename(before) key.set_acl('public-read') mydebugmsg("new key name = [" + keyname + "]") create_thumbnail(before, keyname, before_thumbnails_bucket) # # keyname should look like hashvalue.1.jpg # keyname = filehash + "." + str(iteration) + ".jpg" key = afterbucket.new_key(keyname) key.set_contents_from_filename(after) key.set_acl('public-read') mydebugmsg("new key name = [" + keyname + "]") create_thumbnail(after, keyname, after_thumbnails_bucket) photo_after_url = "https://{}.{}/{}".format(after_bucket_name, s3.server_name(), keyname) tweet_the_nightmare(photo_after_url) mydebugmsg("url for tweepy = " + photo_after_url) mydebugmsg("------------------------------------------") return
# USAGE # python demo.py --base-model $CAFFE_ROOT/models/bvlc_googlenet \ # --image initial_images/fear_and_loathing/fal_01.jpg \ # --output examples/simple_fal.jpg # import the necessary packages import matplotlib matplotlib.use('Agg') from batcountry import BatCountry from PIL import Image import numpy as np import argparse # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-b", "--base-model", required=True, help="base model path") ap.add_argument("-l", "--layer", type=str, default="conv2/3x3", help="layer of CNN to use") ap.add_argument("-i", "--image", required=True, help="path to base image") ap.add_argument("-o", "--output", required=True, help="path to output image") args = ap.parse_args() # we can't stop here... bc = BatCountry(args.base_model) image = bc.dream(np.float32(Image.open(args.image)), end=args.layer) bc.cleanup() # write the output image to file result = Image.fromarray(np.uint8(image)) result.save(args.output)
import os from batcountry import BatCountry import numpy as np from PIL import Image bc = BatCountry(os.path.expanduser("~/install/caffe/models/bvlc_googlenet")) image = bc.dream(np.float32(Image.open("cat.1.jpg"))) bc.cleanup() result = Image.fromarray(np.uint8(image)) result.save("output.jpg")
# --guide-image initial_images/seed_images/starry_night.jpg \ # --output examples/output/seeded/clouds_and_starry_night.jpg # import the necessary packages from batcountry import BatCountry from PIL import Image import numpy as np import argparse # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-b", "--base-model", required=True, help="base model path") ap.add_argument("-l", "--layer", type=str, default="inception_4c/output", help="layer of CNN to use") ap.add_argument("-i", "--image", required=True, help="path to base image") ap.add_argument("-g", "--guide-image", required=True, help="path to guide image") ap.add_argument("-o", "--output", required=True, help="path to output image") args = ap.parse_args() # we can't stop here... bc = BatCountry(args.base_model) features = bc.prepare_guide(Image.open(args.guide_image), end=args.layer) image = bc.dream(np.float32(Image.open(args.image)), end=args.layer, iter_n=20, objective_fn=BatCountry.guided_objective, objective_features=features,) bc.cleanup() # write the output image to file result = Image.fromarray(np.uint8(image)) result.save(args.output)
# --image initial_images/fear_and_loathing/fal_01.jpg \ # --vis examples/output/visualizations # import the necessary packages from batcountry import BatCountry from PIL import Image import numpy as np import argparse # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-b", "--base-model", required=True, help="base model path") ap.add_argument("-l", "--layer", type=str, default="conv2/3x3", help="layer of CNN to use") ap.add_argument("-i", "--image", required=True, help="path to base image") ap.add_argument("-v", "--vis", required=True, help="path to output directory for visualizations") args = ap.parse_args() # we can't stop here... bc = BatCountry(args.base_model) (image, visualizations) = bc.dream(np.float32(Image.open(args.image)), end=args.layer, visualize=True) bc.cleanup() # loop over the visualizations for (k, vis) in visualizations: # write the visualization to file outputPath = "{}/{}.jpg".format(args.vis, k) result = Image.fromarray(np.uint8(vis)) result.save(outputPath)
from batcountry import BatCountry import numpy as np from PIL import * import pdb bc = BatCountry("/home/dylan/caffe/models/bvlc_googlenet") features = bc.prepare_guide(Image.open('./guide.jpg'), end='inception_5b/5x5_reduce') image = bc.dream(np.float32(Image.open('./image.jpg')), end='inception_5b/5x5_reduce', iter_n=20, objective_fn=BatCountry.guided_objective, objective_features=features,) pdb.set_trace() bc.cleanup()
ap.add_argument("-s", "--octave-scale", required=False, type=float, help="Scale of the octaves (default = 1.4)") ap.add_argument("-i", "--image", required=True, help="path to image file") ap.add_argument("-o", "--output", required=True, help="path to output directory") args = ap.parse_args() # filter warnings, initialize bat country, and grab the layer names of # the CNN warnings.filterwarnings("ignore") bc = BatCountry(args.base_model, args.proto, args.caffe_model) layers = bc.layers() # extract the filename and extension of the input image filename = args.image[args.image.rfind("/") + 1:] (filename, ext) = filename.split(".") # loop over the layers -- VISUALIZING ALL LAYERS of the model for (i, layer) in enumerate(layers): # perform visualizing using the current layer print("[INFO] processing layer `{}` {}/{}".format(layer, i + 1, len(layers))) try: # pass the image through the network image = bc.dream(np.float32(Image.open(args.image)),
"--layer", required=False, type=str, default="conv3", help="layer of CNN to use") ap.add_argument("-i", "--image", required=True, help="path to image file") ap.add_argument("-o", "--output", required=True, help="path to output directory") args = ap.parse_args() # filter warnings, initialize bat country, and grab the layer names of # the CNN warnings.filterwarnings("ignore") bc = BatCountry(args.base_model, args.proto, args.caffe_model) print("[INFO] processing layer `{}`".format(args.layer)) image = bc.dream(np.float32(Image.open(args.image)), iter_n=args.iter_n, octave_n=args.octave_n, end=args.layer) # extract the filename and extension of the input image filename = args.image[args.image.rfind("/") + 1:] (filename, ext) = filename.split(".") image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) cv2.putText(image, args.layer, (5, image.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.95, (0, 255, 0), 2)
#!/usr/bin/python import sys from batcountry import BatCountry from PIL import Image import numpy as np # dream.py <path_to_guide_image> <path_to_source_image> <path_to_save_image> # ./dream ./guide.jpg ./in.jpg ./out.jpg guide = sys.argv[1] imgin = sys.argv[2] imgout = sys.argv[3] bc = BatCountry("/opt/caffe/models/bvlc_googlenet") features = bc.prepare_guide(Image.open(guide)) image = bc.dream(np.float32(Image.open(imgin)), iter_n=20, objective_fn=BatCountry.guided_objective, objective_features=features,) bc.cleanup() result = Image.fromarray(np.uint8(image)) result.save(imgout)
# construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-b", "--base-model", required=True, help="base model path") ap.add_argument("-i", "--images", required=True, help="base path to input directory of images") ap.add_argument("-o", "--output", required=True, help="base path to output directory") args = ap.parse_args() # buy the ticket, take the ride bc = BatCountry(args.base_model) layers = ("conv2/3x3", "inception_3b/5x5_reduce", "inception_4c/output") # loop over the input directory of images for imagePath in paths.list_images(args.images): # loop over the layers for layer in layers: # we can't stop here... print("[INFO] processing `{}`".format(imagePath)) image = bc.dream(np.float32(Image.open(imagePath)), end=layer) # write the output image to file filename = imagePath[imagePath.rfind("/") + 1:] outputPath = "{}/{}_{}".format(args.output, layer.replace("/", "_"), filename) result = Image.fromarray(np.uint8(image))
#!/usr/bin/python import sys from batcountry import BatCountry from PIL import Image import numpy as np # dream.py <path_to_guide_image> <path_to_source_image> <path_to_save_image> # ./dream ./guide.jpg ./in.jpg ./out.jpg guide = sys.argv[1] imgin = sys.argv[2] imgout = sys.argv[3] bc = BatCountry("/opt/caffe/models/bvlc_googlenet") features = bc.prepare_guide(Image.open(guide)) image = bc.dream( np.float32(Image.open(imgin)), iter_n=20, objective_fn=BatCountry.guided_objective, objective_features=features, ) bc.cleanup() result = Image.fromarray(np.uint8(image)) result.save(imgout)
ap.add_argument("--guide", help="Image to guide deep dream") ap.add_argument("--mixlayer", help="Layer to mix") ap.add_argument("--classtoshow", help="Specific image to show") args = ap.parse_args() if args.output == None: args.output = "/data/output/"+ str(int(time.time())) + ".jpg" # Rename model file so inference script can pick it up search_dir = args.base_model files = filter(os.path.isfile, glob.glob(search_dir + "*.caffemodel")) files.sort(key=lambda x: os.path.getmtime(x)) model_file = files[-1] shutil.move(model_file, args.base_model+'/bvlc_googlenet.caffemodel') # we can't stop here... if args.classtoshow: bc = BatCountry(args.base_model, deploy_path='/data/model_cache/deploy_class.prototxt') else: bc = BatCountry(args.base_model) for layer in args.layer: if args.guide: features = bc.prepare_guide(Image.open(args.guide), end=layer) image = bc.dream(np.float32(Image.open(args.image)), end=layer, iter_n=args.iteration_count, objective_fn=BatCountry.guided_objective, objective_features=features,) elif args.mixlayer: mixed_features = bc.prepare_guide(Image.open(args.image), end=args.mixlayer) image = bc.dream(np.float32(Image.open(args.image)), end=layer, iter_n=args.iteration_count, objective_fn=BatCountry.guided_objective, objective_features=mixed_features, )
import numpy as np import argparse # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-b", "--base-model", required=True, help="base model path") ap.add_argument("-l", "--layer", type=str, default="conv2/3x3", help="layer of CNN to use") ap.add_argument("-i", "--image", required=True, help="path to base image") ap.add_argument("-v", "--vis", required=True, help="path to output directory for visualizations") args = ap.parse_args() # we can't stop here... bc = BatCountry(args.base_model) (image, visualizations) = bc.dream(np.float32(Image.open(args.image)), end=args.layer, visualize=True) # loop over the visualizations for (k, vis) in visualizations: # write the visualization to file outputPath = "{}/{}.jpg".format(args.vis, k) result = Image.fromarray(np.uint8(vis)) result.save(outputPath)
import argparse # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-b", "--base-model", required=True, help="base model path") ap.add_argument("-i", "--images", required=True, help="base path to input directory of images") ap.add_argument("-o", "--output", required=True, help="base path to output directory") ap.add_argument("-l", "--layers", nargs='+', default=["conv2/3x3", "inception_3b/5x5_reduce", "inception_4c/output"], help="layer or layers to use") args = ap.parse_args() # buy the ticket, take the ride bc = BatCountry(args.base_model) # loop over the input directory of images for imagePath in paths.list_images(args.images): # loop over the layers for layer in args.layers: # we can't stop here... print("[INFO] processing `{}`".format(imagePath)) image = bc.dream(np.float32(Image.open(imagePath)), end=layer) # write the output image to file filename = imagePath[imagePath.rfind("/") + 1:] outputPath = "{}/{}_{}".format(args.output, layer.replace("/", "_"), filename) result = Image.fromarray(np.uint8(image)) result.save(outputPath)
# USAGE # python demo.py --base-model $CAFFE_ROOT/models/bvlc_googlenet \ # --image initial_images/fear_and_loathing/fal_01.jpg \ # --output examples/simple_fal.jpg # import the necessary packages from batcountry import BatCountry from PIL import Image import numpy as np import argparse # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-b", "--base-model", required=True, help="base model path") ap.add_argument("-l", "--layer", type=str, default="conv2/3x3", help="layer of CNN to use") ap.add_argument("-i", "--image", required=True, help="path to base image") ap.add_argument("-o", "--output", required=True, help="path to output image") args = ap.parse_args() # we can't stop here... bc = BatCountry(args.base_model) image = bc.dream(np.float32(Image.open(args.image)), end=args.layer) bc.cleanup() # write the output image to file result = Image.fromarray(np.uint8(image)) result.save(args.output)
# --image initial_images/clouds.jpg \ # --guide-image initial_images/seed_images/starry_night.jpg \ # --output examples/output/seeded/clouds_and_starry_night.jpg # import the necessary packages from batcountry import BatCountry from PIL import Image import numpy as np import argparse # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-b", "--base-model", required=True, help="base model path") ap.add_argument("-l", "--layer", type=str, default="inception_4c/output", help="layer of CNN to use") ap.add_argument("-i", "--image", required=True, help="path to base image") ap.add_argument("-g", "--guide-image", required=True, help="path to guide image") ap.add_argument("-o", "--output", required=True, help="path to output image") args = ap.parse_args() # we can't stop here... bc = BatCountry(args.base_model) features = bc.prepare_guide(Image.open(args.guide_image), end=args.layer) image = bc.dream(np.float32(Image.open(args.image)), end=args.layer, iter_n=20, objective_fn=BatCountry.guided_objective, objective_features=features,) # write the output image to file result = Image.fromarray(np.uint8(image)) result.save(args.output)
import numpy as np import argparse import warnings import cv2 # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-b", "--base-model", required=True, help="base model path") ap.add_argument("-i", "--image", help="path to image file") ap.add_argument("-o", "--output", help="path to output directory") args = ap.parse_args() # filter warnings, initialize bat country, and grab the layer names of # the CNN warnings.filterwarnings("ignore") bc = BatCountry(args.base_model) layers = bc.layers() # extract the filename and extension of the input image filename = args.image[args.image.rfind("/") + 1:] (filename, ext) = filename.split(".") # loop over the layers for (i, layer) in enumerate(layers): # perform visualizing using the current layer print("[INFO] processing layer `{}` {}/{}".format(layer, i + 1, len(layers))) try: # pass the image through the network image = bc.dream(np.float32(Image.open(args.image)), end=layer, verbose=False)
# construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-b", "--base-model", required=True, help = "base model path") ap.add_argument("-p", "--proto", required=False, help = "prototxt model path (default = 'deploy.prototxt')") # added for the specific CNN prototxt ap.add_argument("-m", "--caffe-model", required=False, help= "caffe model path (default = 'imagenet.caffemodel')") # added for custom caffe-model ap.add_argument("-it","--iter-n", required=False, type=int, help = "Number of iterations of the layer (default = 10)") ap.add_argument("-oc","--octave-n", required=False, type=int, help = "Number of octaves (default = 4)") ap.add_argument("-s","--octave-scale", required=False, type=float, help = "Scale of the octaves (default = 1.4)") ap.add_argument("-i", "--image", required = True, help = "path to image file") ap.add_argument("-o", "--output", required = True, help = "path to output directory") args = ap.parse_args() # filter warnings, initialize bat country, and grab the layer names of # the CNN warnings.filterwarnings("ignore") bc = BatCountry(args.base_model,args.proto,args.caffe_model) layers = bc.layers() # extract the filename and extension of the input image filename = args.image[args.image.rfind("/") + 1:] (filename, ext) = filename.split(".") # loop over the layers -- VISUALIZING ALL LAYERS of the model for (i, layer) in enumerate(layers): # perform visualizing using the current layer print("[INFO] processing layer `{}` {}/{}".format(layer, i + 1, len(layers))) try: # pass the image through the network image = bc.dream(np.float32(Image.open(args.image)),iter_n=int(args.iter_n), octave_n=int(args.octave_n),end=layer,verbose=False)
layer_list = all_layers else: if re.search( r'/all$',i,re.M|re.I): layer_root = re.sub("\/","\/",re.sub("/all$","/*",i)) for j in all_layers: if re.search(layer_root,j): layer_list.append(j) else: layer_list.append(i) #quick fix for the car deploy.txt - for some reason it needs to be _ instead of / if base_model == "cars": def car_re(l): return re.sub("\/","_",l) layer_list = map(car_re,layer_list) bc = BatCountry(base_model) if args.guide_image is None: if len(layer_list) > 1: for layer in layer_list: layer_args = get_args_list(args.octave_layer,dream_o,layer) base_name = output_base+'_'+re.sub("\/","_",layer) zdream = zoom_dream(base_img,zoom_num,layer_args,dream_i,base_name,None) else: layer_args = get_args_list(args.octave_layer,dream_o,layer_list[0]) zdream = zoom_dream(base_img,zoom_num,layer_args,dream_i,output_base,None) else: for guide_image in guide_list: if len(guide_list) > 1: output_stub = output_base + "_" + re.sub("[^a-zA-Z_0-9]","_",guide_image) else: output_stub = output_base