def test_load_partial_checkpoint(self): with contextlib.redirect_stdout(StringIO()): trainer, epoch_itr = get_trainer_and_epoch_itr(2, 150, 200, 50) with patch('train.reload_train', return_value=epoch_itr): train.load_checkpoint(self.args_mock, trainer, epoch_itr, 512, None) self.assertEqual(epoch_itr.epoch, 2) self.assertEqual(epoch_itr.iterations_in_epoch, 50) itr = epoch_itr.next_epoch_itr(shuffle=False) self.assertEqual(epoch_itr.epoch, 2) self.assertEqual(epoch_itr.iterations_in_epoch, 50) self.assertEqual(next(itr)['net_input']['src_tokens'][0].item(), 50) self.assertEqual(epoch_itr.iterations_in_epoch, 51) for _ in range(150 - 52): next(itr) self.assertEqual(epoch_itr.iterations_in_epoch, 149) self.assertTrue(itr.has_next()) next(itr) self.assertFalse(itr.has_next()) itr = epoch_itr.next_epoch_itr(shuffle=False) self.assertTrue(itr.has_next()) self.assertEqual(epoch_itr.epoch, 3) self.assertEqual(epoch_itr.iterations_in_epoch, 0)
def test_load_full_checkpoint(self): with contextlib.redirect_stdout(StringIO()): trainer, epoch_itr = get_trainer_and_epoch_itr(2, 150, 300, 150) train.load_checkpoint(self.args_mock, trainer, epoch_itr) itr = epoch_itr.next_epoch_itr(shuffle=False) self.assertEqual(epoch_itr.epoch, 3) self.assertEqual(epoch_itr.iterations_in_epoch, 0) self.assertEqual(next(itr)['net_input']['src_tokens'][0].item(), 0)
def test_load_no_checkpoint(self): with contextlib.redirect_stdout(StringIO()): trainer, epoch_itr = get_trainer_and_epoch_itr(0, 150, 0, 0) self.patches['os.path.isfile'].return_value = False train.load_checkpoint(self.args_mock, trainer, epoch_itr) itr = epoch_itr.next_epoch_itr(shuffle=False) self.assertEqual(epoch_itr.epoch, 1) self.assertEqual(epoch_itr.iterations_in_epoch, 0) self.assertEqual(next(itr)['net_input']['src_tokens'][0].item(), 0)
def test_load_no_checkpoint(self): with contextlib.redirect_stdout(StringIO()): trainer, epoch_itr = get_trainer_and_epoch_itr(0, 150, 0, 0) self.patches['os.path.isfile'].return_value = False train.load_checkpoint(self.args_mock, trainer, epoch_itr, 512, None) itr = epoch_itr.next_epoch_itr(shuffle=False) self.assertEqual(epoch_itr.epoch, 1) self.assertEqual(epoch_itr.iterations_in_epoch, 0) self.assertEqual(next(itr)['net_input']['src_tokens'][0].item(), 0)
def sampling_conditional(sketch_data_dir, photo_data_dir, sampling_base_dir, model_base_dir): [train_set, valid_set, test_set, hps_model, eval_hps_model, sample_hps_model] = \ load_env_compatible(sketch_data_dir, photo_data_dir, model_base_dir) model_dir = os.path.join(model_base_dir, sample_hps_model.data_type) # construct the sketch-rnn model here: reset_graph() model = sketch_p2s_model.Model(hps_model) eval_model = sketch_p2s_model.Model(eval_hps_model, reuse=True) sampling_model = sketch_p2s_model.Model(sample_hps_model, reuse=True) tfconfig = tf.ConfigProto() tfconfig.gpu_options.allow_growth = True sess = tf.InteractiveSession(config=tfconfig) sess.run(tf.global_variables_initializer()) # loads the weights from checkpoint into our model load_checkpoint(sess, model_dir) for idx in range(10): rand_idx = random.randint(0, test_set.num_batches - 1) # orig_x, unused_point_x, unused_point_l, img_x, img_paths = test_set.get_batch orig_x, unused_point_x, unused_point_l, img_x, img_paths = test_set.get_batch(rand_idx) img_path = img_paths[0] img_name = img_path[img_path.rfind('/') + 1:-4] sub_sampling_dir = os.path.join(sampling_base_dir, sample_hps_model.data_type, img_name) os.makedirs(sub_sampling_dir, exist_ok=True) print('rand_idx', rand_idx, 'stroke.shape', orig_x[0].shape, img_paths) ori_img = img_x[0].astype(np.uint8) ori_img_png = Image.fromarray(ori_img, 'RGB') ori_img_png.save(os.path.join(sub_sampling_dir, 'photo_gt.png'), 'PNG') draw_strokes(orig_x[0], os.path.join(sub_sampling_dir, 'sketch_gt.svg')) # encode the image common_pix_h = sess.run(sampling_model.pix_h, feed_dict={sampling_model.input_photo: img_x}) # decoding for sampling strokes_out = sample(sess, sampling_model, common_pix_h, eval_model.hps.max_seq_len, temperature=0.1) # in stroke-3 format draw_strokes(strokes_out, os.path.join(sub_sampling_dir, 'sketch_pred.svg')) # Create generated grid at various temperatures from 0.1 to 1.0 stroke_list = [] for i in range(10): for j in range(1): print(i, j) stroke_list.append([sample(sess, sampling_model, common_pix_h, eval_model.hps.max_seq_len, temperature=0.1), [j, i]]) stroke_grid = make_grid_svg(stroke_list) draw_strokes(stroke_grid, os.path.join(sub_sampling_dir, 'sketch_pred_multi.svg'))
def test_load_partial_checkpoint(self): with contextlib.redirect_stdout(StringIO()): trainer, epoch_itr = get_trainer_and_epoch_itr(2, 150, 200, 50) train.load_checkpoint(MagicMock(), trainer, epoch_itr) self.assertEqual(epoch_itr.epoch, 2) self.assertEqual(epoch_itr.iterations_in_epoch, 50) itr = epoch_itr.next_epoch_itr(shuffle=False) self.assertEqual(epoch_itr.epoch, 2) self.assertEqual(epoch_itr.iterations_in_epoch, 50) self.assertEqual( next(itr)['net_input']['src_tokens'][0].item(), 50) self.assertEqual(epoch_itr.iterations_in_epoch, 51)
def main(): in_arg = get_input_args() data_dir = in_arg.data_dir train_dir = data_dir + '/train' test_dir = data_dir + '/test' valid_dir = data_dir + '/valid' #path = ('./flowers/test/34/image_06961.jpg') path = ('./flowers/test/10/image_07090.jpg') arch = in_arg.arch device_model = in_arg.gpu lr = in_arg.learning_rate save_checkpoint = in_arg.save_dir category_name = in_arg.cat_name epoch_s = in_arg.epochs dropout = in_arg.dropout #path = ('./flowers/test/34/image_06961.jpg') #Function that checks command line arguments using in_arg data_transforms, image_datasets, dataloader = model_transformation( data_dir) mapping(category_name) model, criterion, optimizer = classify_network(arch, dropout, lr) train(device_model, model, dataloader, optimizer, criterion) network_test(device_model, model, dataloader, optimizer, criterion) checkpoint(model, image_datasets, optimizer, lr, epoch_s) save_model = load_checkpoint(save_checkpoint) probs, classes = predict(test_image, save_model, topk=5) print(probs) print(classes)
def predict(args): # TODO: Implement the code to predict the class from an image file device = torch.device("cuda" if ((torch.cuda.is_available() )and (args.device == "cuda")) else "cpu") image_path=args.input img = process_image(image_path) img = torch.from_numpy(img).type(torch.FloatTensor) img=img.to("cuda") img_torch=img.unsqueeze(0) model=train.load_checkpoint(args.checkpoint) model.to("cuda") model.eval() probs=torch.exp(model.forward(img_torch)) top_probs,top_labels=probs.topk(5,dim=1) idx_to_class = {val: key for key, val in model.class_to_idx.items()} top_labels = top_labels.detach().type(torch.FloatTensor).numpy().tolist()[0] top_probs=top_probs.detach().type(torch.FloatTensor).numpy().tolist()[0] top_class = [str(idx_to_class[index]) for index in top_labels] if args.category_names : with open(args.category_names, 'r') as f: cat_to_name = json.load(f) top_flowers=[cat_to_name[label] for label in top_class] print(top_flowers) print(top_probs) else: print(top_class) print(top_probs)
def check_flower(image_path): start_time = time() input_args = get_input_args() cat_to_name = cat_to_names() topk = input_args.top_k """gets model and class_to_idx from checkpoint:""" model, model.class_to_idx = load_checkpoint() """gets predicted name, max probability, topk probabilities and topk classes for test image:""" pred_name, pred_prob, top_probabilities, top_classes = predict(model, image_path) """Printing results:""" print(" Pred_Name: {:20} Probability: {:0.3f}\n Top {} probabilities: {}\n Top {} classes: {}".format(pred_name, pred_prob, topk, top_probabilities, topk, top_classes)) for i in range(1,topk): print(" #{}: {:20} probability: {:0.5f}".format(i+1, [cat_to_name[cl] for cl in top_classes][i], top_probabilities.tolist()[0][i])) """Computes overall runtime and prints it in hh:mm:ss format:""" end_time = time() tot_time = end_time - start_time print("\n** Total Elapsed Runtime:", str(int((tot_time/3600)))+":"+str(int((tot_time%3600)/60))+":" +str(round((tot_time%3600)%60)) ) return pred_name, pred_prob, top_classes[0]
def main(): input_arguments = input_argparse() im = Image.open(input_arguments.input_image_path) device = train.device_in_use(gpu_ind=input_arguments.gpu) label_to_name_json = cat_to_name_conv() model = train.load_checkpoint( checkpoint_loc=input_arguments.checkpoint_name + '.pth') probability, prediction = predict(image_path=im, model=model, topk=input_arguments.top_k, device=device) probability = probability.to('cpu') prediction = prediction.to('cpu') idx_to_class = {value: key for key, value in model.class_to_idx.items()} prediction.numpy()[0] = [idx_to_class[x] for x in prediction.numpy()[0]] top_classes = [label_to_name_json[str(x)] for x in prediction.numpy()[0]] top_probabilities = probability.numpy()[0] print('predicted flower name :' + str(top_classes[0])) print('PROBABILITY' + ' ' + 'PREDICTION') for probability, prediction in zip(top_probabilities, top_classes): print(str(probability) + ' : ' + str(prediction)) os.environ['QT_QPA_PLATFORM'] = 'offscreen' show_result_image(image=im, probability=top_probabilities, top_classes=top_classes, data_dir=input_arguments.input_image_path)
def main(): # Parse arguments args = parse_args() # Get categories names with open(args.annotations, 'r') as anno: js = json.loads(anno.read()) coco_names = js['categories'] # Prepare map of COCO labels to COCO names name_map = {} for name in coco_names: name_map[name['id']] = name['name'] # Prepare map of SSD to COCO labels deleted = [12, 26, 29, 30, 45, 66, 68, 69, 71, 83] inv_map = {} cnt = 0 for i in range(1, 81): while i + cnt in deleted: cnt += 1 inv_map[i] = i + cnt # Prepare colors for categories category_id_to_color = dict([ (cat_id, [random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1)]) for cat_id in range(1, 91) ]) # Set math plot lib size plt.rcParams["figure.figsize"] = (12, 8) # Build and load SSD model ssd300 = SSD300(81, backbone="resnet34", model_path=None, dilation=None) load_checkpoint(ssd300, args.model) ssd300.eval() # Prepare encoder dboxes = dboxes300_coco() encoder = Encoder(dboxes) # Print images for image in args.images: print_image(image, ssd300, encoder, inv_map, name_map, category_id_to_color, args.threshold)
def load_model(model_name): from train import build_model from train import restore_parts, load_checkpoint checkpoint_path = model_name model = build_model() model = load_checkpoint(checkpoint_path, model, None, True) return model
def main(): model = train.load_checkpoint(path) with open(cat_names, 'r') as json_file: cat_to_name = json.load(json_file) probabilities = train.predict(path_image, model, top_k, device) labels = [ cat_to_name[str(index + 1)] for index in np.array(probabilities[1][0]) ] probability = np.array(probabilities[0][0]) i = 0 while i < top_k: print("{} it`s probability {}".format(labels[i], probability[i])) i += 1 print("predect is done!")
def test(): model = Model().cuda() test_data = load_images(['test'])[0] load_checkpoint(408408, model) with torch.no_grad(): for key, batch in test_data.items(): for i in range(0, len(batch[0]), N): x = batch[0][i:i + N].cuda() y = batch[1][i:i + N][0].cuda() y_pred = model(x) for j in range(0, N): img = x[j].reshape(x[j].shape[1:]).cpu().numpy() real = get_prediction(y[j]) gen = get_prediction(y_pred[j].argmax(dim=0)) print('real:', real) print('gen:', gen) plt.imshow(img) plt.show() loss /= n return loss pass
def infer(t2m_checkpoint, ssrn_checkpoint, text_file): t2m, _ = create_model('T2M') load_checkpoint(t2m_checkpoint, t2m, None, None, cuda_present) t2m = t2m.to(device) t2m.eval() #ssrn, _ = create_model('SSRN') #load_checkpoint(ssrn_checkpoint, ssrn, None, None, cuda_present) #ssrn = ssrn.to(device) #ssrn.eval() sample_dir = hp['sample_dir'] os.makedirs(sample_dir, exist_ok=True) text_batch, max_text_batch_len = prepare_text_batch(text_file) num_texts = text_batch.shape[0] text_batch = torch.LongTensor(text_batch).to(device) max_mel_batch_len = max_text_batch_len + 50 coarse_mels = torch.FloatTensor(np.zeros((len(text_batch), hp['n_mels'], max_mel_batch_len))).to(device) timesteps = max_mel_batch_len # initial step for t = 0, attentions = None new_coarse, prev_atten, K, V = t2m(text_batch, coarse_mels) coarse_mels[:, :, 1].data.copy_(new_coarse[:, :, 0].data) for t in tqdm(range(1, timesteps-1)): new_coarse, prev_atten = t2m.infer(coarse_mels, K, V, t-1, prev_atten) coarse_mels[:, :, t+1].data.copy_(new_coarse[:, :, t].data) np.save('test_mels3.npy', coarse_mels) #_, mags = ssrn(coarse_mels) '''for i in range(num_texts):
def main(): # Get input arguments args = get_command_line_args() use_gpu = torch.cuda.is_available() and args.gpu print("Input file: {}".format(args.input)) print("Checkpoint file: {}".format(args.checkpoint)) if args.top_k: print("Returning {} most likely classes".format(args.top_k)) if args.category_names: print("Category names file: {}".format(args.cat_names)) if use_gpu: print("Using GPU.") else: print("Using CPU.") # Load the checkpoint model = train.load_checkpoint(args.checkpoint) print("Checkpoint loaded.") # Move tensors to GPU if use_gpu: model.cuda() # Load categories file if args.cat_names: with open(args.cat_names, 'r') as f: categories = json.load(f) print("Category names loaded") # Predict print("Processing image") probabilities, classes, names = predict(args.input, model, args.top_k) # Show the results # Print results print("Top {} Classes for '{}':".format(len(classes), args.input)) if args.cat_names: print("{:<30} {}".format("Flower", "Probability")) print("------------------------------------------") else: print("{:<10} {}".format("Class", "Probability")) print("----------------------") for i in range(0, len(classes)): if args.cat_names: print("{:<30} {:.2f}".format(categories[names[i]], probabilities[i])) else: print("{:<10} {:.2f}".format(names[i], probabilities[i]))
def setup(): os.chdir(ROOT) if not exists(NAME): url = "https://github.com/r9y9/" + NAME os.system("git clone %s" % url) os.chdir(NAME) os.system("pip install -q -e \'.[bin]\'") # THIS MIGHT NOT WORK: TEST #os.system("./dependency_scipt.sh") # Install python dependcies #os.system("./dependency_scipt.sh") # Install bash dependcies os.system("python -m nltk.downloader cmudict") # English text processing # Get the model os.system("git checkout %s --quiet" % BRANCH) if not exists(PRESET): url = "https://www.dropbox.com/s/0ck82unm0bo0rxd/" + PRESET os.system("curl -O -L %s" % url) if not exists(CHECKPOINT): url = "https://www.dropbox.com/s/5ucl9remrwy5oeg/" + CHECKPOINT os.system("curl -O -L %s" % url) # Hyper parameters import hparams import json # Load parameters from preset with open(PRESET) as f: hparams.hparams.parse_json(f.read()) # Inject frontend text processor import synthesis import train from deepvoice3_pytorch import frontend synthesis._frontend = getattr(frontend, "en") train._frontend = getattr(frontend, "en") # alises fs = hparams.hparams.sample_rate hop_length = hparams.hparams.hop_size print(os.path.dirname(os.path.realpath(__file__))) # Load model from train import build_model from train import restore_parts, load_checkpoint print("Building model") model = build_model() model = load_checkpoint(CHECKPOINT, model, None, True) return model
def main(): input_arguments = input_argparse() im = input_arguments.input_image_path device = train.device_in_use(gpu_ind=input_arguments.gpu) cat_to_name = cat_to_name_func(input_arguments.category_names) model = train.load_checkpoint(checkpoint_loc=input_arguments.checkpoint_name + '.pth') top_probabilities, top_classes = predict(image_path=im, model=model, topk=input_arguments.top_k, device=device) top_labels = [] for i in top_classes: top_labels += [cat_to_name[i]] print('Predicted flower name: ' + str(cat_to_name[top_classes[0]]) + "\n") print('PROBABILITY' + ' ' + 'PREDICTION') for probability, prediction in zip(top_probabilities, top_classes): print(str(probability) + ' : ' + str(cat_to_name[prediction])) os.environ['QT_QPA_PLATFORM'] = 'offscreen' show_result_image(image=im, probability=top_probabilities, top_classes=top_classes, top_labels=top_labels, data_dir=input_arguments.input_image_path)
def infer(mel, f0, model_path, data_mean_std_path="downsample_lj_mean_std.pkl"): device = torch.device("cuda" if use_cuda else "cpu") mel, f0 = mel.to(device), f0.to(device) with open(data_mean_std_path, "rb") as f: data_mean_std = pickle.load(f) model = Model(**network_config["nsf_config"]).to(device) model, _, _ = load_checkpoint(model_path, model, None, reset_optimizer=True) model.eval() with torch.no_grad(): output, _, _ = model(mel, f0) output = output[0].squeeze().cpu().data.numpy() mel_output = librosa.feature.melspectrogram(output, **data_config["mel_config"]) mel_output = np.log(np.abs(mel_output).clip(1e-5, 10)).astype(np.float32) mel_output = prepare_spec_image(mel_output) return output, mel_output
def main(): global args, options, model, cnn, transform, trainset args = parser.parse_args() options = {'logs': {'dir_logs': args.dir_logs}} if args.path_opt is not None: with open(args.path_opt, 'r') as handle: options_yaml = yaml.load(handle) options = utils.update_values(options, options_yaml) print('## args') pprint(vars(args)) print('## options') pprint(options) trainset = datasets.factory_VQA(options['vqa']['trainsplit'], options['vqa']) #options['coco']) normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) transform = transforms.Compose([ transforms.Scale(options['coco']['size']), transforms.CenterCrop(options['coco']['size']), transforms.ToTensor(), normalize, ]) opt_factory_cnn = {'arch': options['coco']['arch']} cnn = convnets.factory(opt_factory_cnn, cuda=args.cuda, data_parallel=False) model = models.factory(options['model'], trainset.vocab_words(), trainset.vocab_answers(), cuda=args.cuda, data_parallel=False) model.eval() start_epoch, best_acc1, _ = load_checkpoint( model, None, os.path.join(options['logs']['dir_logs'], args.resume)) my_local_ip = '192.168.0.32' my_local_port = 3456 run_simple(my_local_ip, my_local_port, application)
def main(): """! @brief Main function for predicting the image class(es) using a trained model. """ args = parse_arguments() test_on_gpu = (args.gpu and check_cuda()) cat_to_name = get_label_mapping(input_json=args.cat_to_name) # Load checkpoint model, ckpt_dict = load_checkpoint(args.checkpoint, train_on_gpu=test_on_gpu) idx_to_class = { idx: cat_to_name[c] for c, idx in ckpt_dict['class_to_idx'].items() } # Pre-process image image = process_image(args.image_path) image = torch.unsqueeze(image, 0) if test_on_gpu: image = image.cuda() # Get actual label label = os.path.basename(os.path.dirname(args.image_path)) label = cat_to_name[label] # Predictions - top K classes prob_k, ind_k = predict(image, model, topk=args.top_k) classes_k = map_classes(ind_k, idx_to_class) print("True label: '{}'".format(label)) print("") print_results(classes_k, prob_k) # Plot image and predictions if args.plot: fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(5, 10)) imshow(torch.squeeze(image.cpu()), ax=ax1, title=label) plot_predictions(prob_k, classes_k, ax=ax2, topk=args.top_k) plt.show()
def infer_test(model_path, dataset, save_dir): device = torch.device("cuda" if use_cuda else "cpu") model = Model(**network_config["nsf_config"]).to(device) model, _, _, _ = load_checkpoint(model_path, model, None, reset_optimizer=True) model.remove_weight_norm() model.eval() for idx in tqdm(range(len(dataset.test_wav))): wav, mel, f0 = dataset.get_all_length_data(idx, is_test=True) mel = mel.to(device) f0 = f0.to(device) with torch.no_grad(): output = model(mel, f0) output = output[0].squeeze().cpu().data.numpy() os.makedirs(save_dir, exist_ok=True) check_point_name = os.path.splitext(os.path.basename(model_path))[0] save_name = check_point_name + "_" + dataset.test_wav[idx] save_path = os.path.join(save_dir, save_name) librosa.output.write_wav( save_path, output[: wav.size(0)], sr=data_config["sampling_rate"] )
def main(): start_time = time() input_args = get_input_args() cat_to_name = cat_to_names() image_path = input_args.image_path topk = input_args.top_k #trains a network and shows running_loss, test accuracy, valid loss and valid accuracy: train() #saves checkpoint: save_checkpoint() #gets model and class_to_idx from checkpoint: model, model.class_to_idx = load_checkpoint() #gets predicted name, max probability, topk probabilities and topk classes for test image: pred_name, pred_prob, top_probabilities, top_classes = predict(model) #Printing results: test_name = cat_to_name[image_path.split("/")[-2]] print( "Pred_Name: {}, Probability: {}\n Test_Name: {}\n Top {} probabilities: {}\n Top {} classes: {}" .format(pred_name, pred_prob, test_name, topk, top_probabilities, topk, top_classes)) #Computes overall runtime in seconds & prints it in hh:mm:ss format: end_time = time() tot_time = end_time - start_time print( "\n** Total Elapsed Runtime:", str(int((tot_time / 3600))) + ":" + str(int( (tot_time % 3600) / 60)) + ":" + str(round( (tot_time % 3600) % 60))) #Shows predicted image, flower name and plot top probabilities to top classes: show_result(top_probabilities, top_classes)
args = parser.parse_args() args.cuda = args.cuda and torch.cuda.is_available() x, y1, y2 = [], [], [] for dir_path in glob(os.path.join(args.models_dir, '*')): weak_perc = float(os.path.basename(dir_path).split('_')[-1]) loader = torch.utils.data.DataLoader(datasets.MultiMNIST( './data', train=False, download=True, transform=transforms.ToTensor(), target_transform=charlist_tensor), batch_size=128, shuffle=True) vae = load_checkpoint(os.path.join(dir_path, 'model_best.pth.tar'), use_cuda=args.cuda) vae.eval() weak_char_acc, weak_len_acc = test_multimnist(vae, loader, use_cuda=args.cuda, verbose=False) x.append(weak_perc) y1.append(weak_char_acc) y2.append(weak_len_acc) print('Got accuracies for %s.' % dir_path) x, y1, y2 = np.array(x), np.array(y1), np.array(y2) ix = np.argsort(x) x, y1, y2 = x[ix], y1[ix], y2[ix] plt.figure()
print('\nTest set: Accuracy: {}/{} ({:.0f}%)\n'.format( correct, len(loader.dataset), 100. * correct / len(loader.dataset))) return correct / float(len(loader.dataset)) if __name__ == "__main__": import argparse parser = argparse.ArgumentParser() parser.add_argument('model_path', type=str, help='path to trained model file') parser.add_argument('--cuda', action='store_true', default=False, help='enables CUDA training') args = parser.parse_args() args.cuda = args.cuda and torch.cuda.is_available() # loader for MNIST loader = torch.utils.data.DataLoader(datasets.MNIST( './data', train=False, download=True, transform=transforms.ToTensor()), batch_size=128, shuffle=True) vae = load_checkpoint(args.model_path, use_cuda=args.cuda) vae.eval() test_mnist(vae, loader, use_cuda=args.cuda)
device = "cuda" if torch.cuda.is_available() else "cpu" size = len(dataset.dataset) model.eval() correct = 0 with torch.no_grad(): for X, y in dataset: X, y = X.to(device), y.to(device) pred = model(X) correct += (pred.argmax(1) == y).type(torch.float).sum().item() correct /= size print(f"Test Error: Accuracy: {(100*correct):>0.1f}%") return correct if __name__ == "__main__": from model_factory import create_cnn_model, is_resnet from data_loader import get_cifar from train import load_checkpoint model_fname = "plain2_Bpodw_best.pth.tar" name = model_fname.split('_')[0] model = create_cnn_model(name, 'cifar100', use_cuda=True) model = load_checkpoint(model, model_fname) train, test = get_cifar() evaluate(model, test)
if args.model == 'DAN': model = Model(emb_dim=64, ntokens=ntokens, hidden_dim=32, output_dim=16).to(device) vocab = load_vocab(os.path.join(args.data_dir, 'vocab.txt')) tokenizer = None elif args.model in ['GPT', 'GPT-2']: model = PretrainedModel(model_name=args.model).to(device) vocab = None tokenizer = get_tokenizer(args.model) else: raise NotImplementedError(f'{args.model} --- no such model') model = load_checkpoint( model, os.path.join(args.model_dir, f'checkpoint_{args.epoch}')) pred = Predictor(model, tokenizer) dataset = CsvDataset(csv_path=os.path.join(args.data_dir, f'data.csv'), vocab=vocab, max_len=50, tokenizer=tokenizer) dataloader = DataLoader(dataset, batch_size=1, shuffle=False, drop_last=True) if args.train == True: pred.fit(dataloader) with open('pred.pickle', 'wb') as f: pickle.dump(pred, f)
os.makedirs(result_dir, exist_ok=True) #device = torch.device("cuda" if use_cuda else "cpu") device = torch.device("cpu") train_seq2seq = True train_postnet = True model = build_model(train_seq2seq, train_postnet, device, hparams) optimizer = torch.optim.Adam(model.parameters(), lr=hparams.initial_learning_rate, betas=(hparams.adam_beta1, hparams.adam_beta2), eps=hparams.adam_eps, weight_decay=hparams.weight_decay, amsgrad=False) if checkpoint_path is not None: model, optimizer, _, _ = load_checkpoint(checkpoint_path, model, optimizer) model.eval() LING_DIR = join(train_dir, 'Linguistic_frame') MEL_DIR = join(train_dir, 'Acoustic_frame/mel') LINEAR_DIR = join(train_dir, 'Acoustic_frame/linear') ling_name = ling + '.npy' ling = np.load(join(LING_DIR, ling_name)) ling = norm_minmax(ling, np.load(join(train_dir, 'stat_linguistic_frame.npy'))) ling = torch.from_numpy(ling).unsqueeze(0).to(device) speaker_list = ['ema', 'emb', 'emc', 'emd', 'eme'] emotions = [0, 1, 2, 3] for ref_spk in speaker_list: for emo in emotions:
def generate_cloned_samples(cloning_texts_location=None, no_speakers=108, fast=True, p=0): # Clone name = "deepvoice3_pytorch" # if not exists(name): # print("Clone the repo!!") # else: # print("Exists!") # Change working directory to the project dir os.chdir(join(expanduser("."), name)) import hparams import json import synthesis import train from deepvoice3_pytorch import frontend from train import build_model from train import restore_parts, load_checkpoint from synthesis import tts as _tts # get_ipython().system(u' python3 -m nltk.downloader cmudict') checkpoint_path = "20171222_deepvoice3_vctk108_checkpoint_step000300000.pth" if not exists(checkpoint_path): print("Dowload the Pre-Trained Network!!") # !curl -O -L "https://www.dropbox.com/s/uzmtzgcedyu531k/20171222_deepvoice3_vctk108_checkpoint_step000300000.pth" # Copy preset file (json) from master # The preset file describes hyper parameters # get_ipython().system(u' git checkout master --quiet') preset = "./presets/deepvoice3_vctk.json" # get_ipython().system(u' cp -v $preset .') # preset = "./deepvoice3_vctk.json" # And then git checkout to the working commit # This is due to the model was trained a few months ago and it's not compatible # with the current master. # ! git checkout 0421749 --quiet # ! pip install -q -e . # print(hparams.hparams.get_model_structure()) # Newly added params. Need to inject dummy values for dummy, v in [("fmin", 0), ("fmax", 0), ("rescaling", False), ("rescaling_max", 0.999), ("allow_clipping_in_normalization", False)]: #if hparams.hparams.get(dummy) is None: hparams.hparams.add_hparam(dummy, v) # Load parameters from preset with open(preset) as f: hparams.hparams.parse_json(f.read()) # Tell we are using multi-speaker DeepVoice3 hparams.hparams.builder = "deepvoice3_multispeaker" # Inject frontend text processor synthesis._frontend = getattr(frontend, "en") train._frontend = getattr(frontend, "en") # alises fs = hparams.hparams.sample_rate hop_length = hparams.hparams.hop_size model = build_model() model = load_checkpoint(checkpoint_path, model, None, True) # text = "here i am" # speaker_id = 0 # fast = True # p = 0 # waveform, alignment, spectrogram, mel = _tts(model, text, p, speaker_id, fast) # print(waveform.shape) # print(alignment.shape) # print(spectrogram.shape) # print(mel.shape) # print(type(mel)) cloning_texts = ["this is the first", "this is the first"] if (cloning_texts_location == None): cloning_texts_location = "./Cloning_Audio/cloning_text.txt" # cloning_texts = open("./Cloning_Audio/cloning_text.txt").splitlines() # no_cloning_texts = len(cloning_texts) all_speakers = [] for speaker_id in range(no_speakers): speaker_cloning_mel = [] print("The Speaker being cloned speaker-{}".format(speaker_id)) for text in cloning_texts: waveform, alignment, spectrogram, mel = _tts( model, text, p, speaker_id, fast) speaker_cloning_mel.append(mel) #print(np.array(speaker_cloning_mel).shape) all_speakers.append(speaker_cloning_mel) with open("./Cloning_Audio/speakers_cloned_voices_mel.p", "wb") as fp: #Pickling pickle.dump(all_speakers, fp) print("") print(np.array(all_speakers).shape) # print(all_speakers.shape) # all speakers[speaker_id][cloned_audio_number] # print(all_speakers[0][1].shape) return all_speakers
std = [ 0.229, 0.224, 0.225 ]), ]) data_loader = get_loader(root=root, cocofile=cocofile, vocab=vocab, transform=transform, batch_size=16) # Model img_features = ImageFeatures(embedding_dim) generator = CaptionGen(embedding_dim=embedding_dim, hidden_dim=64, vocab_size=vocab_size, batch_size=1) generator.hidden = generator.init_hidden() loss = nn.NLLLoss() model_parameters = list(generator.parameters()) + [param for param in img_features.parameters() if param.requires_grad] optimizer = torch.optim.Adam(model_parameters, lr=1e-3) load_checkpoint('../Coco data/model_best.pth.tar') idx = 0 print anns[idx]['caption'] coco = CocoData(root=root, cocofile=cocofile, vocab=vocab, transform=transform) image, caption = coco[idx] image = Variable(image) caption = Variable(caption) img_features.eval() features = img_features(image) pred_caption = generator(features, caption) _, words_idx = torch.max(pred_caption, 2, keepdim=True)
def main(args): if args.max_tokens is None: args.max_tokens = 6000 print(args) if not torch.cuda.is_available(): raise NotImplementedError('Training on CPU is not supported') torch.cuda.set_device(args.device_id) torch.manual_seed(args.seed) # Setup task, e.g., translation, language modeling, etc. task = tasks.setup_task(args) # Load dataset splits load_dataset_splits(task, ['train', "valid"]) # Build model and criterion model = task.build_model(args) criterion = task.build_criterion(args) print('| model {}, criterion {},'.format(args.arch, criterion.__class__.__name__)) print('| num. model params: {}'.format( sum(p.numel() for p in model.parameters()))) # Make a dummy batch to (i) warm the caching allocator and (ii) as a # placeholder DistributedDataParallel when there's an uneven number of # batches per worker. max_positions = utils.resolve_max_positions( task.max_positions(), model.max_positions(), ) dummy_batch = task.dataset('train').get_dummy_batch( args.max_tokens, max_positions) # Build trainer trainer = Trainer(args, task, model, criterion, dummy_batch) print('| training on {} GPUs'.format(args.distributed_world_size)) print('| max tokens per GPU = {} and max sentences per GPU = {}'.format( args.max_tokens, args.max_sentences, )) print('| Optimizer {}'.format(trainer.optimizer.__class__.__name__)) # Initialize dataloader epoch_itr = task.get_batch_iterator( dataset=task.dataset(args.train_subset), max_tokens=args.max_tokens, max_sentences=args.max_sentences, max_positions=max_positions, ignore_invalid_inputs=True, required_batch_size_multiple=8, seed=args.seed, num_shards=args.distributed_world_size, shard_id=args.distributed_rank, ) # Load the latest checkpoint if one is available if not load_checkpoint(args, trainer, epoch_itr): trainer.dummy_train_step([dummy_batch]) # Train until the learning rate gets too small prune_meter = StopwatchMeter() prune_meter.start() # Estimate head importance scores head_importance, head_stats = estimate_head_importance( args, trainer, task, epoch_itr) prune_meter.stop() print('| done estimating head importance in {:.1f} seconds'.format( prune_meter.sum)) torch.save(head_stats, f"{os.path.dirname(args.restore_file)}/heads_stats.bin") # Print print("Head importances") print("Encoder self attention") for layer in range(head_importance["encoder_self"].size(0)): print("\t".join(f"{x:.5f}" for x in head_importance["encoder_self"][layer])) print("Encoder decoder attention") for layer in range(head_importance["encoder_decoder"].size(0)): print("\t".join(f"{x:.5f}" for x in head_importance["encoder_decoder"][layer])) print("Decoder self attention") for layer in range(head_importance["decoder_self"].size(0)): print("\t".join(f"{x:.5f}" for x in head_importance["decoder_self"][layer])) # Print sorted pruning profile encoder_self_profile = get_profile(head_importance["encoder_self"], prefix="E") encoder_decoder_profile = get_profile(head_importance["encoder_decoder"], prefix="A") decoder_self_profile = get_profile(head_importance["decoder_self"], prefix="D") # Join all all_profiles = {} if not (args.decoder_self_only or args.encoder_decoder_only): all_profiles.update(encoder_self_profile) if not (args.encoder_self_only or args.decoder_self_only): all_profiles.update(encoder_decoder_profile) if not (args.encoder_self_only or args.encoder_decoder_only): all_profiles.update(decoder_self_profile) sorted_profiles = sorted(all_profiles.items(), key=lambda x: x[1], reverse=args.one_minus) print("Heads sorted by importance:") print(" ".join(p for p, _ in sorted_profiles)) print("Sorted head importance scores:") print(" ".join(f"{v.data:.5f}" for _, v in sorted_profiles)) if args.only_importance: return tot_n_heads = len(sorted_profiles) # Eval pruning if args.one_head: kept_layers = set() to_prune_profile = [] for p, _ in reversed(sorted_profiles): layer_name = ":".join(p.split(":")[:-1]) if layer_name not in kept_layers: kept_layers.add(layer_name) continue else: to_prune_profile.insert(0, p) to_prune = parse_head_pruning_descriptors(to_prune_profile, reverse_descriptors=False) print(f"Evaluating following profile: \t{' '.join(to_prune_profile)}") # Apply pruning mask_heads(model, to_prune, args.transformer_mask_rescale) bleu = eval_bleu_score( model, task, task.dataset(args.valid_subset), beam=args.beam, replace_unk=args.replace_unk, lenpen=args.lenpen, buffer_size=100, use_cuda=torch.cuda.is_available() and not args.cpu, remove_bpe=args.remove_bpe, max_sentences=args.max_sentences, max_tokens=args.max_tokens, stop_early=not args.no_early_stop, normalize_scores=not args.unnormalized, min_len=args.min_len, ) print(f"BLEU score: \t{bleu.score:.2f}") sys.stdout.flush() return for i in range(0, 10): n_to_prune = int(ceil(tot_n_heads * i / 10)) to_prune_profile = [p for p, _ in sorted_profiles[:n_to_prune]] to_prune = parse_head_pruning_descriptors(to_prune_profile, reverse_descriptors=False) print(f"Evaluating following profile: \t{' '.join(to_prune_profile)}") # Apply pruning mask_heads(model, to_prune, args.transformer_mask_rescale) bleu = eval_bleu_score( model, task, task.dataset(args.valid_subset), beam=args.beam, replace_unk=args.replace_unk, lenpen=args.lenpen, buffer_size=100, use_cuda=torch.cuda.is_available() and not args.cpu, remove_bpe=args.remove_bpe, max_sentences=args.max_sentences, max_tokens=args.max_tokens, stop_early=not args.no_early_stop, normalize_scores=not args.unnormalized, min_len=args.min_len, ) print(f"BLEU score: \t{bleu.score:.2f}") sys.stdout.flush()