def get_googlenet(batchsize): model = L.GoogLeNet(pretrained_model=None) model = WrapperGooglenet(model) x = np.random.uniform(size=(batchsize, 3, 224, 224)).astype('f') x = chainer.as_variable(x) t1 = np.random.randint(size=(batchsize, ), low=0, high=1000) t1 = chainer.as_variable(t1.astype(np.int32)) t2 = np.random.randint(size=(batchsize, ), low=0, high=1000) t2 = chainer.as_variable(t2.astype(np.int32)) t3 = np.random.randint(size=(batchsize, ), low=0, high=1000) t3 = chainer.as_variable(t3.astype(np.int32)) return [x, t1, t2, t3], model
def set_model(cls, model_name, uses_device=0): """ Set model and device. uses_device = -1 : CPU uses_device >= 0 : GPU (default 0) """ # use gpu or cpu cls.uses_device = uses_device if uses_device >= 0: chainer.cuda.get_device_from_id(uses_device).use() chainer.cuda.check_cuda_available() import cupy as xp else: xp = np cls.xp = xp # set model cls.model_name = model_name if model_name == "VGG16": cls.model = L.VGG16Layers() cls.last_layer = 'fc8' cls.size = (224, 224) cls.mean = [103.939, 116.779, 123.68] elif model_name == "GoogLeNet": cls.model = L.GoogLeNet() cls.last_layer = 'loss3_fc' cls.size = (224, 224) cls.mean = [104.0, 117.0, 123.0] elif model_name == "ResNet152": cls.model = L.ResNet152Layers() cls.last_layer = 'fc6' cls.size = (224, 224) cls.mean = [103.063, 115.903, 123.152] else: raise Exception("Invalid model") if uses_device >= 0: cls.model.to_gpu() #for memory saving for param in cls.model.params(): param._requires_grad = False
def __init__( self, filename_obj, texture_size=4, init_bias=(0, 0, 0), image_size=224, layer_name='inception_4c', camera_distance=2.732, camera_distance_noise=0.1, elevation_min=-10, elevation_max=30, lr_vertices=0.01, lr_textures=1.0, lambda_length=0.0001, ): super(DeepDreamModel, self).__init__() self.image_size = image_size self.layer_name = layer_name self.camera_distance = camera_distance self.camera_distance_noise = camera_distance_noise self.elevation_min = elevation_min self.elevation_max = elevation_max self.lambda_length = lambda_length self.googlenet = cl.GoogLeNet() with self.init_scope(): # load .obj self.mesh = neural_renderer.Mesh(filename_obj, texture_size) self.mesh.set_lr(lr_vertices, lr_textures) # texture bias init_bias = self.xp.array(init_bias, 'float32') self.mesh.textures.data += init_bias[None, None, None, None, :] # setup renderer renderer = neural_renderer.Renderer() renderer.light_intensity_directional = 0.0 renderer.light_intensity_ambient = 1.0 renderer.image_size = image_size self.renderer = renderer
def __call__(self, layers=['loss3_fc']): out = self.prepare(L.model.vision.googlenet.prepare) googlenet = L.GoogLeNet() return googlenet(out, layers=layers)
def __init__(self, n_out=20): super().__init__() with self.init_scope(): self.base = L.GoogLeNet() self.fc = L.Linear(None, n_out)
def __init__(self): w = chainer.initializers.HeNormal() super(GoogLeNet, self).__init__(google=L.GoogLeNet(), )
def run(): # settings parser = argparse.ArgumentParser() parser.add_argument('-of', '--obj_filename', type=str) parser.add_argument('-od', '--output_directory', type=str) parser.add_argument('-l', '--light', type=bool, default=False) parser.add_argument('-bc', '--background_color', type=float, default=1.) parser.add_argument('-ib', '--init_bias', type=str, default='0') parser.add_argument('-ll', '--lambda_length', type=float, default=1e-4) parser.add_argument('-emax', '--elevation_max', type=float, default=30.) parser.add_argument('-emin', '--elevation_min', type=float, default=-10.) parser.add_argument('-amax', '--azimuth_max', type=float, default=180.) parser.add_argument('-amin', '--azimuth_min', type=float, default=-180.) parser.add_argument('-al', '--adam_lr', type=float, default=0.01) parser.add_argument('-ab1', '--adam_beta1', type=float, default=0.9) parser.add_argument('-lrv', '--lr_vertices', type=float, default=0.005) parser.add_argument('-is', '--image_size', type=int, default=224) parser.add_argument('-cd', '--camera_distance', type=float, default=2.5) parser.add_argument('-bs', '--batch_size', type=int, default=4) parser.add_argument('-ts', '--texture_size', type=int, default=4) parser.add_argument('-dn', '--distance_noise', type=float, default=0.1) parser.add_argument('-ni', '--num_iteration', type=int, default=1000) parser.add_argument('-ns', '--num_save', type=int, default=100) parser.add_argument('-wm', '--without_mask', type=int, default=1) parser.add_argument('-g', '--gpu', type=int, default=0) args = parser.parse_args() # init output directory if not os.path.exists(args.output_directory): os.makedirs(args.output_directory) # setup chainer chainer.cuda.get_device_from_id(args.gpu).use() cp.random.seed(0) np.random.seed(0) # load feature extractor googlenet = cl.GoogLeNet() googlenet.to_gpu() # setup scene & optimizer scene = Scene(args.batch_size, args.obj_filename, args.texture_size, args.lr_vertices, args.light, args.init_bias) scene.to_gpu() optimizer = lib.Adam(alpha=args.adam_lr, beta1=args.adam_beta1) optimizer.setup(scene) # save initial model filename = 'train_%08d.png' % 0 save_image(args.output_directory, filename, scene, args.image_size, args.camera_distance) # main loop num_iter_save = args.num_iteration / args.num_save for i in range(args.num_iteration): # setup camera azimuth_batch = cp.random.uniform(args.azimuth_min, args.azimuth_max, size=args.batch_size) azimuth_batch[azimuth_batch.size / 2:] += 180 elevation_batch = cp.random.uniform(args.elevation_min, args.elevation_max, size=args.batch_size) distance_batch = cp.random.uniform(-args.distance_noise, args.distance_noise).astype('float32') distance_batch += args.camera_distance scene.camera.set_eye(azimuth_batch, elevation_batch, distance_batch) # get loss background_color = cp.random.uniform(0., 1.).astype('float32') images = scene.rasterize(image_size=args.image_size, background_colors=background_color, fill_back=True, anti_aliasing=True) masks = scene.rasterize_silhouette(image_size=args.image_size, fill_back=True, anti_aliasing=True) loss = get_loss(scene, googlenet, images, masks, args.lambda_length, args.without_mask) # update optimizer.target.cleargrads() loss.backward() optimizer.update() # show progress if (i + 1) % num_iter_save == 0: num = (i + 1) / num_iter_save filename = 'train_%08d.png' % num save_image(args.output_directory, filename, scene, args.image_size, args.camera_distance) print 'iter: %08d / %08d,' % (i, args.num_iteration), 'loss: %.4f' % float(loss.data) # save turntable images for azimuth in range(0, 360, 2): filename = 'rotation_%08d.png' % azimuth save_image(args.output_directory, filename, scene, args.image_size, args.camera_distance, azimuth)