def verify(args): dataset_dir = get_dataset_dir(args) log_dir = get_log_dir(args) model_class = get_model_class(args) model = model_class(False).to(device) checkpoint = torch.load(args.verify_model) # model.load_state_dict(checkpoint["state_dict"], strict=False) model.load_state_dict(checkpoint, strict=False) # print(model) model.eval() image_a, image_b = args.verify_images.split(",") image_a = transform_for_infer(model_class.IMAGE_SHAPE)( image_loader(image_a)) image_a = image_a.unsqueeze(0) image_a = image_a.to(device) # print(f"Image A shape : {image_a.shape}") # image_a = torch.randn(3, 96, 128) image_b = transform_for_infer(model_class.IMAGE_SHAPE)( image_loader(image_b)) # images = torch.stack([image_a, image_b]).to(device) # print(f"Image stack : {images.shape}") with torch.no_grad(): out = model(image_a)
def send_photo(bot, chat_id): style = image_loader('/content/gdrive/My Drive/pictures/style.jpg') content = image_loader('/content/gdrive/My Drive/pictures/content.jpg') input_img = content.clone() photo = run_style_transfer(cnn, cnn_normalization_mean, cnn_normalization_std, content, style, input_img, num_steps=30) bot.send_photo(chat_id=chat_id, photo=photo)
def predict(): try: # f = request.files['file'] image = Image.open('./img.jpg').convert("RGB") image = image_loader(image) encoder, decoder, vocab = initialize() features = encoder(image).unsqueeze(1) output = decoder.sample(features) sentence = clean_sentence(output, vocab) res = {} res['pred_1'] = sentence outputs = decoder.sample_beam_search(features) num_sents = min(len(outputs), 3) count = 2 for output in outputs[:num_sents]: sentence = clean_sentence(output, vocab) res['pred_{}'.format(count)] = sentence count += 1 # print(res) return app.response_class(response=json.dumps(res), status=200, mimetype='application/json') except Exception as error: err = str(error) print(err) return app.response_class(response=json.dumps(err), status=500, mimetype='application/json')
def run_model(content, style, folder): device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Use smaller image size if gpu isn't available if torch.cuda.is_available(): imsize = (512, 512) else: # Kept it small for quick debugging imsize = (128, 128) # Resize image and transform to torch tensor tfms = [transforms.Resize(imsize), transforms.ToTensor()] loader = transforms.Compose(tfms) style_img = image_loader(folder + '/' + content, loader, device) content_img = image_loader(folder + '/' + style, loader, device) unloader = transforms.ToPILImage() # import ssl # ssl._create_default_https_context = ssl._create_unverified_context cnn = models.vgg19(pretrained=True).features.to(device).eval() vgg_mean = torch.tensor([0.485, 0.456, 0.406]).to(device) vgg_std = torch.tensor([0.229, 0.224, 0.225]).to(device) input_img = content_img.clone() # For white noise, uncomment the line below # input_img = torch.randn(content_img.data.size(), device=device) output = run_style_transfer(cnn, vgg_mean, vgg_std, content_img, style_img, input_img, device, loader, unloader, folder) plt.figure(figsize=(8, 8)) output_name = imshow(output, loader, unloader, folder, title='Output Image', output=True) return output_name
def verify(args): dataset_dir = get_dataset_dir(args) log_dir = get_log_dir(args) model_class = get_model_class(args) model = model_class(False).to(device) checkpoint = torch.load(args.verify_model) model.load_state_dict(checkpoint['state_dict'], strict=False) model.eval() image_a, image_b = args.verify_images.split(',') image_a = transform_for_infer( model_class.IMAGE_SHAPE)(image_loader(image_a)) image_b = transform_for_infer( model_class.IMAGE_SHAPE)(image_loader(image_b)) images = torch.stack([image_a, image_b]).to(device) _, (embedings_a, embedings_b) = model(images) distance = torch.sum(torch.pow(embedings_a - embedings_b, 2)).item() print("distance: {}".format(distance))
def run_model(style, content, folder): device = torch.device("cuda" if torch.cuda.is_available else "cpu") if torch.cuda.is_available: imsize = (512, 512) else: imsize = (128, 128) trans = [transforms.Resize(imsize), transforms.ToTensor()] loader = transforms.Compose(trans) style_img = image_loader(folder + '/' + content, loader, device) content_img = image_loader(folder + '/' + style, loader, device) unloader = transforms.ToPILImage() cnn = models.vgg19(pretrained=True).features.to(device).eval() vgg_mean = torch.tensor([0.485, 0.456, 0.406]) vgg_std = torch.tensor([0.229, 0.224, 0.225]) input_img = content_img.clone() output = run_style_transfer(cnn, vgg_mean, vgg_std, content_img, style_img, input_img, device, loader, unloader, folder) plt.figure(figsize=(8, 8)) output_name = imshow(output, loader, unloader, folder, output=True, title='output image') return output_name
def predict(image_path, model, class_to_idx, topk, category_name): ''' Predict the class (or classes) of an image using a trained deep learning model. ''' idx_to_class = {v: k for k, v in class_to_idx.items()} np_image = image_loader(image_path) with torch.no_grad(): outputs = model(np_image) probs, class_idxs = outputs.topk(topk) np_idxs = class_idxs.numpy() idxs = [np_idxs.item(i) for i in range(np_idxs.shape[1])] classes = [str(idx_to_class[idx]) for idx in idxs] pro = np.exp(probs.detach().numpy()[0]) cat_to_name = None if category_name: with open(category_name, 'r') as f: cat_to_name = json.load(f) if topk == 1: if cat_to_name: print( 'This flower is predicted to be {} with probability of {:.4f}'. format(cat_to_name[classes[0]], pro[0])) else: print( 'This flower is predicted to be class {} with probability of {:.4f}' .format(classes[0], pro[0])) else: if cat_to_name: for c in classes: print('class', 'flower_name', 'probability') print(c, cat_to_name[c[0]], pro[0]) else: print( 'top {} likely classes this flower belongs too:'.format(topk)) print(classes) print('with corresponding probabilities') print(pro)
def __getitem__(self, index): image = image_loader(self.datasets[index][0]) if self.transform: image = self.transform(image) return (image, self.datasets[index][1], self.datasets[index][2])
print("Invalid sample") continue proj_ind_3d.append(proj_mapping[0]) proj_ind_2d.append(proj_mapping[1]) output, _ = model(None, proj_ind_3d, proj_ind_2d, None, None, None, dv) output = torch.cat(output) return output loader = transforms.Compose([ transforms.Resize((opt.img_sidelength, opt.img_sidelength)), transforms.ToTensor() ]) style_img = utils.image_loader(opt.style_image_path, loader, device) writer.add_image("Input images src style", style_img[0], 0) # Dummy Content Image with torch.no_grad(): output_image = get_images_from_poses(trgt_pose.unsqueeze(0), dv_orig).squeeze(0) writer.add_image("Initial-Rendered-Images", output_image + 0.5, 0) content_img = output_image + 0.5 content_img = content_img.unsqueeze(0) stm = StyleTransferModel2(content_img, style_img) # Train trgt_pose = trgt_pose.unsqueeze(0) for epoch in tqdm(range(opt.num_iterations)): # for batch_num, trgt_poses in enumerate(tqdm(dataloader)):
def main(img_idx=0, img_path="data"): # ---- Setup ---- args = process_args(img_idx, img_path) args.train_tag = f"_{args.optim}_s{args.style_weight}_c{args.content_weight}_lr{args.lr}" print("\n==== {} ====".format("Device Configuration")) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") dtype = torch.cuda.FloatTensor if torch.cuda.is_available( ) else torch.FloatTensor args.device = device args.dtype = dtype print("Device", device) print("dtype", dtype) # ---- Load data ---- print("\n==== {} ====".format("Loading images")) # 1 * 3 * W * H, 0-255, normalize to 0-1 style_img = (utils.image_loader(args.style_image).type(dtype).to(device)) content_img = (utils.image_loader( args.content_image).type(dtype).to(device)) interim_img = (utils.image_loader( args.interim_image).type(dtype).to(device)) # 1 * 1 * W * H, 0-1 mask_tight = (utils.image_loader(args.mask, "L").type(dtype).to(device)) mask_rough = (utils.image_loader(args.dilated_mask, "L").type(dtype).to(device)) # 1 * 1 * W * H, 0/1 mask_tight[mask_tight != 0] = 1 mask_rough[mask_rough != 0] = 1 # tmask_image = tmask_image.filter(ImageFilter.GaussianBlur()) print("Content image shape", content_img.shape) print("Style image shape", style_img.shape) print("Mask shape", mask_rough.shape) print("Tight Mask shape", mask_tight.shape) # ---- Import vgg model ---- print("\n==== {} ====".format("Importing vgg19 model")) cnn_normalization_mean = torch.tensor([0.485, 0.456, 0.406]).to(device) cnn_normalization_std = torch.tensor([0.229, 0.224, 0.225]).to(device) cnn = models.vgg19(pretrained=True).features.to(device).eval() cnn = copy.deepcopy(cnn) for param in cnn.parameters(): param.requires_grad = False assert cnn.training is False # ---- Build Style Transfer Model ---- print( "\n==== {} ====".format("Building Deep Painterly Harmonization model")) # input_img = torch.randn(content_img.data.size(), device=device) if args.is_pass == 1: style_layers = ["relu3_1", "relu4_1", "relu5_1"] content_layers = ["relu4_1"] model, tv_loss, content_losses, style_losses, histogram_losses = get_model_and_losses( cnn, cnn_normalization_mean, cnn_normalization_std, mask_rough, mask_tight, style_img=style_img, content_img=content_img, interim_img=interim_img, style_layers=style_layers, content_layers=content_layers, args=args) elif args.is_pass == 2: style_layers = ["relu1_1", "relu2_1", "relu3_1", "relu4_1"] content_layers = ["relu4_1"] histogram_layers = ["relu1_1", "relu4_1"] model, tv_loss, content_losses, style_losses, histogram_losses = get_model_and_losses( cnn, cnn_normalization_mean, cnn_normalization_std, mask_rough, mask_tight, style_img=style_img, content_img=content_img, interim_img=interim_img, style_layers=style_layers, content_layers=content_layers, histogram_layers=histogram_layers, args=args, ) else: print("Wrong input for the option --is-pass. Exit.") return # ---- Run model ---- print("\n==== {} ====".format("Start training.")) output_tensor, loss_history = run_painterly_transfer( model, content_img, style_img, mask_tight, mask_rough, style_losses, content_losses, histogram_losses, tv_loss, args=args, ) # ---- Plot lost history and save final result---- print("\n==== {} ====".format("Plot loss history figure.")) utils.history_plot(loss_history, args.train_tag, args.output) print(f"\n==== Save the final output image to {args.output} ====") output_masked = utils.mask_crop(output_tensor, style_img, mask_tight) utils.save_image(output_masked, args.output) return
assert os.path.exists(path_to_images), 'Image folder does not exist' assert os.path.exists(path_to_content), 'Content images folder does not exist' assert os.path.exists(path_to_style), 'Style images folder does not exist' assert os.path.exists(path_to_outputs), 'Output folder does not exist' if comments: print('Path to : ', path_to_images) print('Path to : ', path_to_content) print('Path to : ', path_to_style) print('Path to : ', path_to_outputs) # 1 - Load Images #content_path = os.path.join(path_to_content, args['content']) content_path = os.path.join(path_to_content, 'ace_pablo.jpg') content_image = image_loader(content_path, imsize, device) #style_path = os.path.join(path_to_style, args['style']) style_path = os.path.join(path_to_style, 'picasso.jpg') style_image = image_loader(style_path, imsize, device) input_image = content_image.clone() #input_image = torch.randn(content_image.data.size(), device=device) #plt.figure() #image_drawer(input_image, title='Input Image') # Sanity check assert content_image.size() == style_image.size(), \ 'Content and Image sizes must match' # Visualize the images
# Table for machine learning algo. table_selected = [features_name[1], features_name[2], features_name[4]] table_selected.extend( ['history_' + str(i) for i, _ in enumerate(med_history)]) table_selected.extend(['symptom_' + str(i) for i, _ in enumerate(med_sym)]) X_train, y_train = utils.union_features(df, train_dataset, feature_extractor, table_selected) X_test, y_test = utils.union_features(df, val_dataset, feature_extractor, table_selected) """ TODO: add more classifiers. From terminal an user can select the clf. """ clf = parameters['clf'] clf.fit(X_train, y_train) y_pred = clf.predict(X_test) print(classification_report(y_test, y_pred)) """ These lines are for the prediction of my sample """ for file_path in pathlib.Path(img_folder + "test/").glob("*.jpg"): image = utils.image_loader(file_path).to("cpu") img_name = str(file_path).split("\\")[2].replace(".jpg", "") outputs = feature_extractor(image) outputs = outputs.view(-1).tolist() sample = df.loc[utils.my_func(df['cough_filename'], img_name, True), table_selected].values.tolist() outputs.extend(sample[0]) y_pred = clf.predict([outputs]) print(str(file_path), y_pred)
import argparse from utils import image_loader from dl_model import reload_model, predict if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument(dest='input', help='image full path to predict') parser.add_argument(dest='checkpoint', help='checkpoint path to save model') parser.add_argument('--top_k', dest='top_k',type=int, default=1, help='top k most likely classes') parser.add_argument('--category_names', dest='category_names', help='category to class names JSON file ') parser.add_argument('--gpu', action='store_true', help='Use GPU for training') args = parser.parse_args() image = image_loader(args.input) model, class_to_idx = reload_model(args.checkpoint, args.gpu) predict(args.input, model, class_to_idx, args.top_k, args.category_names) # python predict.py flowers/train/1/image_06735.jpg model_checkpoint/checkpoint.pth
output_dir = "../output" content_dir = "../data/content_images" style_dir = "../data/style_images" means = torch.tensor([0.485, 0.456, 0.406]).to(device) stds = torch.tensor([0.229, 0.224, 0.225]).to(device) num_steps = 500 style_weight, content_weight = 1000000, 1 content_images = iterate_images(content_dir) style_images = iterate_images(style_dir) content_tensors = [ image_loader(content_img) for content_img in content_images ] style_tensors = [image_loader(style_img) for style_img in style_images] cnns = { "vgg11": models.vgg11(pretrained=True).features.to(device).eval(), "vgg19": models.vgg19(pretrained=True).features.to(device).eval(), "vgg11_bn": models.vgg11_bn(pretrained=True).features.to(device).eval(), "vgg19_bn": models.vgg19_bn(pretrained=True).features.to(device).eval(), "resnet18": models.resnet18(pretrained=True).to(device).eval(), "resnet34": models.resnet34(pretrained=True).to(device).eval() } histories = {key: {} for key in cnns}
help="size to resize image to") parser.add_argument("--model_path", default='trained_model/model_epoch_10.pth', help="path to saved model") args = parser.parse_args() return args if __name__ == "__main__": args = get_args() device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = DnCNN().to(device) model = torch.load(args.model_path, map_location=device) model = model['arch'] loader = transforms.Compose([ transforms.Resize((args.crop_size, args.crop_size)), transforms.ToTensor() ]) img_path = args.image_path # pass the image into the image_loader function image = image_loader(img_path, loader, device) # get prediction predict(model, image, args.save_image_path)
input_img = content_img.clone() output = run_style_transfer(cnn, content_img, style_img, input_img, style_weight=weight, num_steps=epochs) output = output.cpu() output = output.squeeze(0) output = toPIL(output) return output if __name__ == "__main__": parser = argparse.ArgumentParser(description="Stylize Image using VGG16, Gatys et al") parser.add_argument('-i', '-image', type=str, required=True, help="Image Content") parser.add_argument('-s', '-style', type=str, required=True, help="Image Style") parser.add_argument('--output', type=str, default="output.jpg", help="Location of output file") parser.add_argument('--w', type=int, default=100000, help="Style Weight in Loss Computation") parser.add_argument('--epochs', type=int, default=300, help="Style Weight in Loss Computation") args = parser.parse_args() print('Input file is ', args.i) print('Style file is ', args.s) style_img = image_loader(args.s, imsize, device) content_img = image_loader(args.i, imsize, device) output = stylize(content_img, style_img, weight=args.w, epochs=args.epochs) output.save(args.output)
if __name__ == '__main__': import time import pyscreenshot from pynput import keyboard, mouse from utils import check_ad_ready, check_ad_screen, image_loader, mse, check_main_page res = pyscreenshot.grab().size _, ad_not_ready_1, ad_ready_1, _, main = image_loader() Mouse = mouse.Controller() Keyboard = keyboard.Controller() time_limit = 10 for i in range(time_limit): print( 'Please switch to fullscreen Scrcpy with LINE play ad screen in: {} sec' .format(time_limit - i)) time.sleep(1) while True: time.sleep(5) if check_main_page(main, res): # click main page then click ad page Mouse.position = (res[0] * 0.5286, res[1] * 0.0388) Mouse.click(mouse.Button.left) time.sleep(5) Mouse.position = (res[0] * 0.6217, res[1] * 0.4592) Mouse.click(mouse.Button.left) else:
def main(in_video: str, style_img: str, output_path: str, stabilizer: int, num_steps: int, style_weight: int, content_weight: int, previous_weight: float, output_filename: str): print("Style Weight = {}".format(style_weight)) # Load video video_frames = video_loader(in_video) # Load style image style_image = image_loader(style_img) # Check they have same size assert style_image.size() == video_frames[0].size(), \ "Input video and style image must have the same proportions" flows = None if (stabilizer == 2): ## If we are using optical flow, generate flow between frames in_frames = read_frames(in_video) flows = generate_flows(in_frames) # Load pre-trained VGG model cnn = models.vgg19(pretrained=True).features.to(device).eval() # Generate input frames input_frames = generate_input_frames(video_frames) # Run Style Transfer if stabilizer == 0: print('Running style_transfer_nost') transformed_frames = run_style_transfer_no_st(cnn=cnn, normalization_mean=cnn_normalization_mean, normalization_std=cnn_normalization_std, video_frames=video_frames, style_img=style_image, input_frames=input_frames, num_steps=num_steps, style_weight=style_weight, content_weight=content_weight, output_path=output_path, output_filename=output_filename) elif stabilizer == 1 or stabilizer == 2: print('Running style_transfer_st') transformed_frames = run_style_transfer_st(stabilizer=stabilizer, cnn=cnn, normalization_mean=cnn_normalization_mean, normalization_std=cnn_normalization_std, video_frames=video_frames, style_img=style_image, input_frames=input_frames, num_steps=num_steps, style_weight=style_weight, content_weight=content_weight, previous_weight=previous_weight, output_path=output_path, output_filename=output_filename, flows=flows) print("Style video transfer successfully completed.")
def reset(self, name): ''' reset the StyleCNN network using the name, which is a string containing all the configuration information of StyleCNN. name is like 'pc-c1s1-a0b1w1-cl4_2sl1_12_13_14_15_1-oLBFGS' and can be divided to 5 parts separated by '-'. 1. first part, which is 'pc' or 'pn', records input image. 'pc' means input image is the same as content image. 'pn' means input image is random noise. 2. second part, like 'c1s1' or 'c1s1s2', records the content image and style images. 'c1' means content1.jpg, s1 means style1.jpg, etc. 3. third part, like 'a1b1000w1', records weight information. 'a' means content weight, in general, we choose it to be 0 or 1. 'b' means style weight, if <1, use 0_01 to represent 0.01. 'w' means in- styles weight. The number following 'w' is the type of in-styles weight we choose. 1 means average distribution. 2 means weight of style i = loss of style i / total style loss 4. fourth part, like 'cl4_2sl1_12_13_14_15_1', records content and style layers used to compute loss. 'cl' means content layer, '4_2' means conv_4_2. 'sl' means style layer, '1_12_13_14_15_1' means using ['conv_1_1', 'conv_2_1', 'conv_3_1', 'conv_4_1', 'conv_5_1']. 5. fifth part, like 'oLBFGS', records optimizer information. 'o' means optimizer. 'LBFGS' means optim.lBFGS optimizer. We only use optim.SGD, optim.Adam and optim.LBFGS. :param name: a string containing all the configuration information of StyleCNN :type name: str :return none ''' # CUDA Configurations dtype = torch.cuda.FloatTensor if self.use_cuda else torch.FloatTensor ## decode name and reset style_cnn keys = name.split('-') # reset content image and style images # self.content is a tensor, 1 * 3 * H * W # self.styles is a tensor, #styles * 3 * H * W images = keys[1].split('s') self.content = utils.image_loader('./contents/content' + images[0][1:] + '.jpg').type(dtype) self.styles = torch.Tensor(0) for i in range(1, len(images)): style = utils.image_loader('./styles/style' + images[i] + '.jpg') self.styles = torch.cat((self.styles, style), 0) self.styles = self.styles.type(dtype) # reset num of styles and in_style_weights self.num_styles = self.styles.size()[0] # reset pastiche # back propagate to update pixel values of pastiche # self.pastiche is Tensor here if keys[0] == 'pn': self.pastiche = nn.Parameter( torch.randn(self.content.size()).type(dtype)) else: self.pastiche = nn.Parameter(self.content.clone()) # reset content weight and style weight index_b = keys[2].find('b') index_w = keys[2].find('w') self.content_weight = float(keys[2][1:index_b]) self.style_weight = float(keys[2][index_b + 1:index_w].replace( '_', '.')) self.in_styles_weight = keys[2][index_w + 1:] # reset content layers and style layers self.content_layers = ['conv_' + keys[3][2:5]] num_style_layers = (len(keys[3]) - 7) // 3 self.style_layers = [] for i in range(num_style_layers): self.style_layers.append('conv_' + keys[3][7 + 3 * i:10 + 3 * i]) # reset optimizer if keys[4][1:] == 'SGD': self.optimizer = optim.SGD([self.pastiche], lr=0.001, momentum=0.9) elif keys[4][1:] == 'Adam': self.optimizer = optim.Adam([self.pastiche]) else: self.optimizer = optim.LBFGS([self.pastiche]) # reset losses ''' content_loss, style_loss, total_loss records the corresponding loss after each iteration. They are all tensors ''' self.total_loss = 0 self.content_loss = 0 self.style_losses = 0 return
content_img_file = args.content_img_file # setup logging model_name = 'nst-stroke' root = args.output_path vgg_weight_file = 'vgg_weights/vgg19_weights_normalized.h5' print_freq = 10 mon.initialize(model_name=model_name, root=root, print_freq=print_freq) mon.backup(('main.py', 'param_stroke.py', 'utils.py', 'losses.py', 'vgg.py')) # device device = torch.device(args.device) # desired size of the output image imsize = args.img_size content_img = utils.image_loader(content_img_file, imsize, device) style_img = utils.image_loader(style_img_file, 224, device) output_name = f'{os.path.basename(content_img_file).split(".")[0]}-{os.path.basename(style_img_file).split(".")[0]}' # desired depth layers to compute style/content losses : bs_content_layers = ['conv4_1', 'conv5_1'] bs_style_layers = ['conv1_1', 'conv2_1', 'conv3_1', 'conv4_1', 'conv5_1'] px_content_layers = ['conv1_1', 'conv2_1', 'conv3_1', 'conv4_1', 'conv5_1'] px_style_layers = ['conv1_1', 'conv2_1', 'conv3_1', 'conv4_1', 'conv5_1'] # brush strokes parameters canvas_color = args.canvas_color num_strokes = args.num_strokes samples_per_curve = args.samples_per_curve brushes_per_pixel = args.brushes_per_pixel _, _, H, W = content_img.shape
'The layers which you think have style information, name like conv_i or relu_i' ) parser.add_argument('--model', '-m', type=str, default='vgg19', help='The net structure') args = parser.parse_args() use_cuda = torch.cuda.is_available() and args.cuda dtype = torch.cuda.FloatTensor if use_cuda else torch.FloatTensor # desired size of the output image imsize = 512 if use_cuda else 128 # use small size if no gpu style_img = image_loader(args.style, imsize).type(dtype) content_img = image_loader_gray(args.content, imsize).type(dtype) if args.initialize_noise: input_img = Variable(torch.randn(content_img.data.size())).type(dtype) else: input_img = image_loader_gray(args.content, imsize).type(dtype) input_size = Image.open(args.content).size assert style_img.size() == content_img.size(), \ "we need to import style and content images of the same size" if args.model == 'vgg19': cnn = models.vgg19(pretrained=True).features elif args.model == 'vgg16': cnn = models.vgg16(pretrained=True).features
if __name__ == "__main__": args = get_args() if args.use_cuda: try: device = torch.device('cuda') except Exception as e: print("Check if you have correct nvidia driver installed!") print(e) device = None # throw error when loading model on device. Do not let run on cpu with gpu resources on cloud! else: device = torch.device('cpu') torch.manual_seed(args.seed) model = utils.get_model().to(device) train_file_path, test_file_path = utils.get_data_paths(args.data_dir) train_set = utils.ImageDataset(train_file_path, aug_images=True) train_loader = utils.image_loader(train_set, args.batch_size, True) test_set = utils.ImageDataset(test_file_path, aug_images=False) test_loader = utils.image_loader(test_set, args.test_batch_size, False) loss_func = torch.nn.CrossEntropyLoss(reduction='mean') optimizer = torch.optim.Adam(model.fc.parameters(), lr=args.lr) model, best_performance_metrics, log_df = train(args, model, train_loader, test_loader, loss_func, optimizer, device) utils.save_model(args.model_dir, model) utils.save_job_log(args.log_dir, log_df, best_performance_metrics)
parser = argparse.ArgumentParser() parser.add_argument("-src", "--src_img_path", required=True, type=str) parser.add_argument("-style", "--style_img_path", required=True, type=str) parser.add_argument("-n", "--num_iters", required=True, type=int) parser.add_argument("-l", "--log_dir", required=True, type=str) parser.add_argument("-sc", "--style_coeff", default=0.5, type=float) parser.add_argument("-cc", "--content_coeff", default=0.5, type=float) args = parser.parse_args() imsize = 512 device = data_util.get_device() loader = transforms.Compose( [transforms.Resize(imsize), transforms.ToTensor()]) content_img = utils.image_loader(args.src_img_path, loader, device) style_img = utils.image_loader(args.style_img_path, loader, device) input_img = content_img.clone() model = StyleTransferModel2(content_img, style_img) for p in model.model.parameters(): p.requires_grad = False optimizer = optim.LBFGS([input_img.requires_grad_()]) for iter_num in tqdm(range(args.num_iters)): def closure(): input_img.data.clamp_(0, 1) optimizer.zero_grad() ss, cs = model.get_loss(input_img) ss = args.style_coeff * ss
parser.add_argument('--show_every', default=200, type=int, help='displaying the target image, intermittently') parser.add_argument('--steps', default=2000, type=int, help='iterations to update image') opt = parser.parse_args() if opt.use_cuda: # checking if cuda is available device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # load in content and style image content = image_loader(opt.content_path, shape=(512, 512)).to(device) style = image_loader(opt.style_path, shape=(512, 512)).to(device) # display images if opt.show_img: fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 10)) ax1.imshow(tensor_to_image(content)) ax2.imshow(tensor_to_image(style)) # weights for each style layer # weighting earlier layers more will result in *larger* style artifacts # notice we are excluding `conv4_2` our content representation style_weights = { 'conv1_1': 1., 'conv2_1': 0.75, 'conv3_1': 0.2,
def main(): # params device = torch.device('cpu') num_steps = 300 style_weight = 10 content_weight = 1 cnn = models.vgg19(pretrained=True).features.to(device).eval() # Style Image / Content Image # style_img = image_loader('./picasso.jpg') # content_img = image_loader('./dancing.jpg') style_img = image_loader('./style.jpg') content_img = image_loader('./content.jpg') # Input Image input_img = content_img.clone() plt.figure() imshow(input_img, title='Input Image') assert style_img.size() == content_img.size(), \ 'We need to import style and content images of the same size' # run the tansfer print('Building the style transfer model...') model, style_losses, content_losses = \ get_style_model_and_losses(cnn, style_img, content_img) optimizer = get_input_optimizer(input_img) print('Optimizing...') for step in range(1, num_steps, 1): def closure(): # Clamp all elements in input into the range [0, 1] input_img.data.clamp = (0, 1) optimizer.zero_grad() model(input_img) style_score = 0 content_score = 0 for style_loss in style_losses: style_score += style_loss.loss for content_loss in content_losses: content_score += content_loss.loss style_score *= style_weight content_score *= content_weight loss = style_score + content_score loss.backward() if step % 5 == 0: print("step {}:".format(step)) print('Style Loss : {:4f} Content Loss: {:4f}'.format( style_score.item(), content_score.item())) print() return loss.item() optimizer.step(closure) output_img = input_img.data.clamp(0, 1) plt.figure() imshow(output_img, title='Output Image') plt.ioff() plt.show()