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
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
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) # do some cleanup bc.cleanup()
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)
# 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")
#!/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)
# --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)
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) # draw the layer name on the image image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) cv2.putText(image, layer, (5, image.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.95, (0, 255, 0), 2) # construct the output path and write the image to file p = "{}/{}_{}.{}".format(args.output, filename, str(i + 1).zfill(4), ext) cv2.imwrite(p, image) except KeyError, e: # the current layer can not be used print("[ERROR] cannot use layer `{}`".format(layer)) # perform housekeeping
# 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)
# 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) # draw the layer name on the image image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) cv2.putText(image, layer, (5, image.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.95, (0, 255, 0), 2) # construct the output path and write the image to file p = "{}/{}_{}.{}".format( args.output, filename, layer, ext) # instead of the pic index, writes the layer used cv2.imwrite(p, image) except KeyError, e:
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) # construct the output path and write the image to file p = "{}/{}_{}_{}.{}".format( args.output, filename, args.layer, args.iter_n, ext) # instead of the pic index, writes the layer used cv2.imwrite(p, image)
"--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)) result.save(outputPath) # do some cleanup bc.cleanup()
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()
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, ) elif args.classtoshow: octaves = [ { 'layer':'loss3/classifier_zzzz', 'iter_n':190, 'start_sigma':2.5, 'end_sigma':0.78, 'start_step_size':11., 'end_step_size':11.
# --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)
# --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)
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) # draw the layer name on the image image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) cv2.putText(image, layer, (5, image.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.95, (0, 255, 0), 2) # construct the output path and write the image to file p = "{}/{}_{}.{}".format(args.output, filename, layer, ext) # instead of the pic index, writes the layer used cv2.imwrite(p, image) except KeyError, e: # the current layer can not be used print("[ERROR] cannot use layer `{}`".format(layer)) # perform housekeeping
#!/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)