def test_multiple_models_custom_func(self): model1 = models.inception_v3(pretrained=True).eval() model2 = models.resnet18(pretrained=True).eval() bunch = ModelBunch(model_dict={'inception': model1, 'resnet': model2}) layers_to_use = [ bunch.model_dict['inception'].Mixed_6a, bunch.model_dict['resnet'].layer2[0].conv1 ] dreamy_boi = dreamer(model=bunch, quiet=False, device='cpu') def custom_func(layer_outputs): loss = layer_outputs[1][89].mean() + layer_outputs[0].mean()**2 return -loss image_param = dreamy_boi.render(layers=layers_to_use, custom_func=custom_func, iters=5) image_param.save(filename='test_multiple_models_custom_func.jpg') self.assertTrue(os.path.exists('test_multiple_models_custom_func.jpg')) self.assertTrue(isinstance(image_param, auto_image_param), 'should be an instance of auto_image_param') self.assertTrue(isinstance(image_param.__array__(), np.ndarray)) self.assertTrue(isinstance(image_param.to_hwc_tensor(), torch.Tensor), 'should be a torch.Tensor') self.assertTrue(isinstance(image_param.to_chw_tensor(), torch.Tensor), 'should be a torch.Tensor') os.remove('test_multiple_models_custom_func.jpg')
def test_custom_image_param_set_param(self): """ checks if custom_image_param.set_param correctly loads the image without discrepancies with an absolute tolerance of 1e-5 element-wise """ model = models.inception_v3(pretrained=True) dreamy_boi = dreamer(model=model, device='cpu', quiet=False) param = custom_image_param(image='images/sample_small.jpg', device='cpu') image_param = dreamy_boi.render(image_parameter=param, layers=[model.Mixed_6a], iters=5, lr=2e-4, grad_clip=0.1, weight_decay=1e-1) image_tensor = image_param.to_nchw_tensor() image_param.set_param(tensor=image_tensor) # print(torch.abs((image_tensor - image_param.to_nchw_tensor())).mean()) self.assertTrue( torch.allclose(image_tensor, image_param.to_nchw_tensor(), atol=1e-5))
def test_custom_image_param(self): model = models.inception_v3(pretrained=True) dreamy_boi = dreamer(model=model, device='cpu', quiet=False) param = custom_image_param(image='images/sample_small.jpg', device='cpu') image_param = dreamy_boi.render(image_parameter=param, layers=[model.Mixed_6a], iters=5, lr=2e-4, grad_clip=0.1, weight_decay=1e-1) image_param.save(filename='test_custom_image_param.jpg') self.assertTrue(os.path.exists('test_custom_image_param.jpg')) self.assertTrue(isinstance(image_param, custom_image_param), 'should be an instance of auto_image_param') self.assertTrue(isinstance(image_param.__array__(), np.ndarray)) self.assertTrue(isinstance(image_param.to_hwc_tensor(), torch.Tensor), 'should be a torch.Tensor') self.assertTrue(isinstance(image_param.to_chw_tensor(), torch.Tensor), 'should be a torch.Tensor') os.remove('test_custom_image_param.jpg')
def test_caricature(self): model = models.resnet18(pretrained=True) dreamy_boi = dreamer(model, device='cpu') param = custom_image_param(image='images/sample_small.jpg', device='cpu') image_tensor = param.to_nchw_tensor() param = dreamy_boi.caricature(input_tensor=image_tensor, layers=[model.layer3], power=1.0, iters=5) self.assertTrue(isinstance(param, auto_image_param), 'should be an auto_image_param')
def test_single_model(self): model = models.inception_v3(pretrained=True) dreamy_boi = dreamer(model=model, device='cpu', quiet=False) image_param = dreamy_boi.render(layers=[model.Mixed_6a], iters=5) image_param.save(filename='test_single_model.jpg') self.assertTrue(os.path.exists('test_single_model.jpg')) self.assertTrue(isinstance(image_param, auto_image_param), 'should be an instance of auto_image_param') self.assertTrue(isinstance(image_param.__array__(), np.ndarray)) self.assertTrue(isinstance(image_param.to_hwc_tensor(), torch.Tensor), 'should be a torch.Tensor') self.assertTrue(isinstance(image_param.to_chw_tensor(), torch.Tensor), 'should be a torch.Tensor') os.remove('test_single_model.jpg')
def test_custom_normalization(self): model = models.resnet18(pretrained=True) dreamy_boi = dreamer(model, device='cpu') t = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) dreamy_boi.set_custom_normalization(normalization_transform=t) param = dreamy_boi.render(layers=[model.layer3], iters=5) expected_transforms = transforms.Compose([ transforms.RandomAffine(15, translate=(0, 0)), random_resize(max_size_factor=1.2, min_size_factor=0.5), InverseTransform(old_mean=[0.485, 0.456, 0.406], old_std=[0.229, 0.224, 0.225], new_transforms=transforms.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])) ]) self.assertTrue(expected_transforms, dreamy_boi.transforms)
def run_core(models_dict): col1, col2 = st.beta_columns(2) model_name = col1.selectbox("Pick a model", options=list(models_dict.keys())) model = models_dict[model_name] dreamy_boi = dreamer(model, device='cuda') layers_dict = dict(model.named_modules()) names = col2.multiselect("Pick layer(s)", options=list(layers_dict.keys())) layers_to_use = [] for name in names: layers_to_use.append(layers_dict[name]) my_custom_func = None with st.beta_expander(label='Modify args'): columns = st.beta_columns(4) width = columns[0].number_input('width', value=256) height = columns[1].number_input('height', value=256) iters = columns[2].number_input('iters', value=120) lr = columns[3].number_input('lr', value=9e-3) columns2 = st.beta_columns(4) rotate_degrees = columns2[0].number_input('rotate (deg)', value=15) scale_max = columns2[1].number_input('scale max', value=1.2) scale_min = columns2[2].number_input('scale min', value=0.5) grad_clip = columns2[3].number_input('gradient clip', value=1.) columns3 = st.beta_columns(3) translate_x = columns3[0].number_input('translate (x)', value=0.2) translate_y = columns3[1].number_input('translate (y)', value=0.2) weight_decay = columns3[2].number_input('weight decay', value=1e-3) if st.checkbox('Channel objective'): c1, c2 = st.beta_columns(2) layer_index = c1.number_input(label="Enter layer index", value=0) channel_index = c2.number_input(label="Enter channel index", value=9) my_custom_func = make_custom_func(layer_number=layer_index, channel_number=channel_index, center=False) if st.checkbox('Write your own custom objective'): try: from .__custom_func__ import custom_func except ImportError: custom_func_file = open("rover/__custom_func__.py", "w") n = custom_func_file.write(Constants.default_str_custom_func) custom_func_file.close() text = st.text_area(label='Write your objective function here', value=Constants.default_str_custom_func, height=100) st.code(text) custom_func_file = open("rover/__custom_func__.py", "w") n = custom_func_file.write(text) custom_func_file.close() my_custom_func = custom_func if len(layers_to_use) != 0: if st.button(label='Run'): run_render(dreamer=dreamy_boi, layers=layers_to_use, custom_func=my_custom_func, width=width, height=height, iters=iters, lr=lr, rotate_degrees=rotate_degrees, scale_max=scale_max, scale_min=scale_min, translate_x=translate_x, translate_y=translate_y, weight_decay=weight_decay, grad_clip=grad_clip) else: # st.error("you did not pick any layer :(") pass
def setup_torch_dreams(): global model, dreamy_boi model = models.inception_v3(pretrained=True) dreamy_boi = dreamer(model, device='cuda', quiet=False) setup_layer_lookup() return dreamy_boi
def setup(): dreamy_boi = dreamer(model) return dreamy_boi