def eval(logdir1, logdir2): # Load graph model = Net2() # dataflow df = Net2DataFlow(hp.test2.data_path, hp.test2.batch_size) ckpt1 = tf.train.latest_checkpoint(logdir1) ckpt2 = tf.train.latest_checkpoint(logdir2) session_inits = [] if ckpt2: session_inits.append(SaverRestore(ckpt2)) if ckpt1: session_inits.append(SaverRestore(ckpt1, ignore=['global_step'])) pred_conf = PredictConfig(model=model, input_names=get_eval_input_names(), output_names=get_eval_output_names(), session_init=ChainInit(session_inits)) predictor = OfflinePredictor(pred_conf) # x_mfccs, y_spec, _ = next(df().get_data()) summ_loss, = predictor(next(df().get_data())) writer = tf.summary.FileWriter(logdir2) writer.add_summary(summ_loss) writer.close()
def do_convert(args, logdir1, logdir2): # Load graph model = Net2() df = Net2DataFlow(hp.convert.data_path, hp.convert.batch_size) ckpt1 = tf.train.latest_checkpoint(logdir1) ckpt2 = '{}/{}'.format(logdir2, args.ckpt) if args.ckpt else tf.train.latest_checkpoint(logdir2) session_inits = [] if ckpt2: session_inits.append(SaverRestore(ckpt2)) if ckpt1: session_inits.append(SaverRestore(ckpt1, ignore=['global_step'])) pred_conf = PredictConfig( model=model, input_names=get_eval_input_names(), output_names=get_eval_output_names(), session_init=ChainInit(session_inits)) predictor = OfflinePredictor(pred_conf) audio, y_audio, ppgs = convert(predictor, df) # Write the result tf.summary.audio('A', y_audio, hp.default.sr, max_outputs=hp.convert.batch_size) tf.summary.audio('B', audio, hp.default.sr, max_outputs=hp.convert.batch_size) # Visualize PPGs heatmap = np.expand_dims(ppgs, 3) # channel=1 tf.summary.image('PPG', heatmap, max_outputs=ppgs.shape[0]) writer = tf.summary.FileWriter(logdir2) with tf.Session() as sess: summ = sess.run(tf.summary.merge_all()) writer.add_summary(summ) writer.close()
def do_convert(logdir1, logdir2, input_path, output_path): # Load graph model = Net2() model.actual_duration = librosa.core.get_duration(filename=input_path, sr=hp.default.sr) # TODO isolate out logdirs, uhh and how to pre-dl from s3? assert len(input_path) > 0, "must be non-empty input path" df = Net2DataFlow(data_path=input_path, batch_size=1) ckpt1 = tf.train.latest_checkpoint(logdir1) ckpt2 = tf.train.latest_checkpoint(logdir2) session_inits = [] session_inits.append(SaverRestore(ckpt2)) session_inits.append(SaverRestore(ckpt1, ignore=['global_step'])) pred_conf = PredictConfig(model=model, input_names=get_eval_input_names(), output_names=get_eval_output_names(), session_init=ChainInit(session_inits)) predictor = OfflinePredictor(pred_conf) audio, y_audio, ppgs = convert(predictor, df) write_wav(audio[0], hp.default.sr, output_path)
def __init__(self): self.logdir1 = ('home/sergey/Documents/vc/train1/timit/checkpoint') self.logdir2 = ('home/sergey/Documents/vc/train2/arctic/checkpoint') # Load graph self.model = Net2() ckpt1 = tf.train.latest_checkpoint(self.logdir1) ckpt2 = tf.train.latest_checkpoint(self.logdir2) print(ckpt1)
def train(args, logdir1, logdir2): # model model = Net2() preprocessing(data_path, logdir2) # dataflow df = Net2DataFlow(data_path, hp.train2.batch_size) # set logger for event and model saver logger.set_logger_dir(logdir2) # session_conf = tf.ConfigProto( # gpu_options=tf.GPUOptions( # allow_growth=True, # per_process_gpu_memory_fraction=0.6, # ), # ) dataset_size = len(glob.glob(data_path + '/wav/*.wav')) print("\t\data_path : ", data_path) print("\t\tDataset Size : ", dataset_size) print("\t\tBatch Size : ", hp.train2.batch_size) print("\t\tSteps per epoch : ", (dataset_size // hp.train2.batch_size)) from time import sleep sleep(10) session_inits = [] ckpt2 = '{}/{}'.format( logdir2, args.ckpt) if args.ckpt else tf.train.latest_checkpoint(logdir2) if ckpt2: session_inits.append(SaverRestore(ckpt2)) ckpt1 = tf.train.latest_checkpoint(logdir1) if ckpt1: session_inits.append(SaverRestore(ckpt1, ignore=['global_step'])) train_conf = AutoResumeTrainConfig( model=model, data=QueueInput(df(n_prefetch=1000, n_thread=8)), callbacks=[ # TODO save on prefix net2 ModelSaver(checkpoint_dir=logdir2), # ConvertCallback(logdir2, hp.train2.test_per_epoch), ], max_epoch=hp.train2.num_epochs, steps_per_epoch=dataset_size // hp.train2.batch_size, session_init=ChainInit(session_inits)) if args.gpu: os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu train_conf.nr_tower = len(args.gpu.split(',')) gpu_list = args.gpu.split(',') gpu_list = list(map(int, gpu_list)) #trainer = SimpleTrainer() trainer = SyncMultiGPUTrainerReplicated(gpu_list) #trainer = AsyncMultiGPUTrainer(gpu_list, False) launch_train_with_config(train_conf, trainer=trainer)
def train(logdir_train1, logdir_train2): # Load model net1 net1_model = Net1(hp.default.phns_len) checkpoint_path1 = '{}/checkpoint.tar'.format(logdir_train1) checkpoint1 = torch.load(checkpoint_path1) if checkpoint1: net1_model.load_state_dict(checkpoint1['model_state_dict']) # Load model net2 net2_model = Net2() checkpoint_path2 = '{}/checkpoint.tar'.format(logdir_train2) checkpoint2 = None epoch = 0 loss = 100 lr = hp.train2.lr optimizer = torch.optim.Adam(net2_model.parameters(), lr=lr) if os.path.exists(checkpoint_path2): checkpoint2 = torch.load(checkpoint_path2) if checkpoint2: train_list, eval_list = load_train_eval_lists(logdir_train2) logger.info("Reuse existing train_list, eval_list from {}".format(logdir_train2)) net2_model.load_state_dict(checkpoint2['model_state_dict']) optimizer.load_state_dict(checkpoint2['optimizer_state_dict']) lr = optimizer.param_groups[0]['lr'] epoch = checkpoint2['epoch'] loss = checkpoint2['loss'] logger.debug("Checkpoint loaded") else: data_dir = hp.train2.data_path train_list, eval_list, _ = generate_data_list(logdir_train2, data_dir, 0.8, 0.1, 0.1) logger.info("Generate new train_list, eval_list, test_list.") net2_model.train() # Set to train mode # Create train/valid loader training_set = Net2DataDir(os.path.join(data_dir,'train')) training_loader = DataLoader(training_set, batch_size=hp.train2.batch_size, shuffle=True, drop_last=True, num_workers=hp.train2.num_workers) logger.debug("Training loader created. Size: {} samples".format(training_set.size)) validation_set = Net2DataDir(os.path.join(data_dir, 'eval')) # If batch_size is inconsistent at the last batch, audio_utils.net2_out_to_pdf fails ''' TODO: not sure if validation_loader requires separate batch size as 'eval2.batch_size' maybe implement later ''' validation_loader = DataLoader(validation_set, batch_size=hp.train2.batch_size, shuffle=True, drop_last=True, num_workers=hp.eval2.num_workers) logger.debug("Validation loader created. Size: {} samples".format(validation_set.size)) # Create criterion criterion = MyMSELoss() logger.debug("Loss type: Sum of MSE loss on mel spectrogram and linear spectrogram") # Run model net2_model, epoch, best_loss = net2_train(checkpoint_path2, net1_model, net2_model, training_loader, validation_loader, criterion, epoch, device=hp.train2.device, lr=lr, loss=loss)
def main(): hyp_batch_size = 20 net = Net2() model_dir = '../saved_models/' model_name = 'keypoints_model_2.pt' data_transform = transforms.Compose([ Rescale(256), RandomCrop(224), Normalize(), ToTensor() ]) # retreive the saved model net_state_dict = torch.load(model_dir+model_name) net.load_state_dict(net_state_dict) # load the test data test_dataset = FacialKeypointsDataset(csv_file='../files/test_frames_keypoints.csv', root_dir='../files/test/', transform=data_transform) # load test data in batches batch_size = hyp_batch_size test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=True, num_workers=0) test_images, test_outputs, gt_pts = net_sample_output(test_loader, net) print(test_images.data.size()) print(test_outputs.data.size()) print(gt_pts.size()) # Get the weights in the first conv layer, "conv1" # if necessary, change this to reflect the name of your first conv layer weights1 = net.conv1.weight.data w = weights1.numpy() filter_index = 0 print(w[filter_index][0]) print(w[filter_index][0].shape) # display the filter weights plt.imshow(w[filter_index][0], cmap='gray') #plt.show() ##TODO: load in and display any image from the transformed test dataset i = 1 show_image(test_images, w, i)
def do_convert(args, logdir1, logdir2): # Load graph model = Net2() data = get_mfccs_and_spectrogram(args.file) ckpt1 = '{}/{}'.format( logdir1, args.net1) if args.net1 else tf.train.latest_checkpoint(logdir1) ckpt2 = '{}/{}'.format( logdir2, args.net2) if args.net2 else tf.train.latest_checkpoint(logdir2) session_inits = [] if ckpt2: session_inits.append(SaverRestore(ckpt2)) if ckpt1: session_inits.append(SaverRestore(ckpt1, ignore=['global_step'])) pred_conf = PredictConfig(model=model, input_names=get_eval_input_names(), output_names=get_eval_output_names(), session_init=ChainInit(session_inits)) predictor = OfflinePredictor(pred_conf) audio, y_audio, ppgs = convert(predictor, data) target_file = args.file.split('/')[-1] portion = os.path.splitext(target_file) # converted_file = target_file.split('.')[0] + '_converted.wav' converted_file = portion[0] + '.wav' write_wav(audio[0], hp.Default.sr, args.savepath + converted_file) # Write the result tf.summary.audio('A', y_audio, hp.Default.sr, max_outputs=hp.Convert.batch_size) tf.summary.audio('B', audio, hp.Default.sr, max_outputs=hp.Convert.batch_size) # Visualize PPGs heatmap = np.expand_dims(ppgs, 3) # channel=1 tf.summary.image('PPG', heatmap, max_outputs=ppgs.shape[0]) writer = tf.summary.FileWriter(args.savepath) with tf.Session() as sess: summ = sess.run(tf.summary.merge_all()) writer.add_summary(summ) writer.close()
def __init__(self, args): self.args = args self.num_channels = NUM_CHANNELS if args.netType == 1: self.net = Net(self.num_channels, args) elif args.netType == 2: self.net = Net2(self.num_channels, args) if args.cuda: self.net = self.net.cuda() self.load_dataset_from_folder() self.writer = SummaryWriter() self.unique_tok = str(time.time()) self.init_weights()
def train(args, logdir1, logdir2): # model model = Net2() # dataflow df = Net2DataFlow(hp.train2.data_path, hp.train2.batch_size) # set logger for event and model saver logger.set_logger_dir(logdir2) session_conf = tf.ConfigProto( # log_device_placement=True, allow_soft_placement=True, gpu_options=tf.GPUOptions( # allow_growth=True, per_process_gpu_memory_fraction=0.6, ), ) session_inits = [] ckpt2 = '{}/{}'.format(logdir2, args.ckpt) if args.ckpt else tf.train.latest_checkpoint(logdir2) if ckpt2: session_inits.append(SaverRestore(ckpt2)) ckpt1 = tf.train.latest_checkpoint(logdir1) if ckpt1: session_inits.append(SaverRestore(ckpt1, ignore=['global_step'])) train_conf = TrainConfig( model=model, data=QueueInput(df(n_prefetch=1000, n_thread=4)), callbacks=[ # TODO save on prefix net2 ModelSaver(checkpoint_dir=logdir2), # ConvertCallback(logdir2, hp.train2.test_per_epoch), ], max_epoch=hp.train2.num_epochs, steps_per_epoch=hp.train2.steps_per_epoch, session_init=ChainInit(session_inits), session_config=session_conf ) if args.gpu: os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu train_conf.nr_tower = len(args.gpu.split(',')) #trainer = SyncMultiGPUTrainerParameterServer(hp.train2.num_gpu) trainer = SimpleTrainer() launch_train_with_config(train_conf, trainer=trainer)
def do_convert(args, logdir1, logdir2, input_dir): # Load graph model = Net2() # input_dir = hp.convert.data_base_dir_original + hp.convert.data_path df = Net2DataFlow(input_dir, hp.convert.batch_size) ckpt1 = tf.train.latest_checkpoint(logdir1) ckpt2 = '{}/{}'.format( logdir2, args.ckpt) if args.ckpt else tf.train.latest_checkpoint(logdir2) session_inits = [] if ckpt2: session_inits.append(SaverRestore(ckpt2)) if ckpt1: session_inits.append(SaverRestore(ckpt1, ignore=['global_step'])) pred_conf = PredictConfig(model=model, input_names=get_eval_input_names(), output_names=get_eval_output_names(), session_init=ChainInit(session_inits)) predictor = OfflinePredictor(pred_conf) # loop over all the audio files for wav_file in df.wav_files: # check if file is present audio out_path = wav_file.replace(hp.convert.data_base_dir_original, hp.convert.data_base_dir_convert) # change file extension from wv1/wv2 to wav out_path = out_path[:-2] + 'av' if os.path.isfile(out_path): # file is already present, move on to the next one. print("skipping " + wav_file) continue print("converting " + wav_file) # convert audio audio_len, feats = df.get_features(wav_file) audio_full = [] for feat in feats: input_arr = ([feat[0]], [feat[1]], [feat[2]]) audio, ppgs = convert(predictor, input_arr) audio_full.append( (audio[0] * hp.convert.amplitude_multiplier).astype(np.int16)) scipy.io.wavfile.write(out_path, hp.default.sr, np.concatenate(audio_full)[:audio_len])
def convert(logdir_eval1, logdir_eval2): # Load model net1 net1_model = Net1() checkpoint_path1 = '{}/checkpoint.tar'.format(logdir_eval1) checkpoint1 = torch.load(checkpoint_path1) if checkpoint1: net1_model.load_state_dict(checkpoint1['model_state_dict']) # Load model net2 net2_model = Net2() checkpoint_path2 = '{}/checkpoint.tar'.format(logdir_eval2) checkpoint2 = torch.load(checkpoint_path2) if checkpoint2: net2_model.load_state_dict(checkpoint2['model_state_dict']) # Create conversion source loader conversion_source_set = Net2Data(hp.convert.data_path) conversion_source_loader = DataLoader(conversion_source_set, batch_size=hp.convert.batch_size, shuffle=False, drop_last=False) # Run model spectrogram_batch = convert(net1_model, net2_model, conversion_source_loader)
def train(args, logdir2): # model model = Net2() # dataflow df = Net2DataFlow(hp.train2.mel_path, hp.train2.ppgs_path, hp.train2.batch_size) session_inits = [] ckpt2 = '{}/{}'.format( logdir2, args.ckpt) if args.ckpt else tf.train.latest_checkpoint(logdir2) if ckpt2: session_inits.append(SaverRestore(ckpt2)) ''' ckpt1 = tf.train.latest_checkpoint(logdir1) if ckpt1: session_inits.append(SaverRestore(ckpt1, ignore=['global_step'])) ''' train_conf = TrainConfig( model=model, data=QueueInput(df(n_prefetch=1000, n_thread=4)), callbacks=[ # TODO save on prefix net2 ModelSaver(checkpoint_dir=logdir2), # ConvertCallback(logdir2, hp.train2.test_per_epoch), ], max_epoch=hp.train2.num_epochs, steps_per_epoch=hp.train2.steps_per_epoch, session_init=ChainInit(session_inits)) if args.gpu: os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu train_conf.nr_tower = len(args.gpu.split(',')) trainer = SyncMultiGPUTrainerReplicated(hp.train2.num_gpu) print("strated trainer") launch_train_with_config(train_conf, trainer=trainer)
ap.add_argument("-m", "--model", default="Custom", type=str, help="Model Name") ap.add_argument("-l", "--loss", default="MSE", type=str, help="Loss function") ap.add_argument("-s", "--split", default=True, type=bool, help="Split method") ap.add_argument("-x", "--train30", default=False, type=bool, help="Subset for 30 annotations") ap.add_argument("-y", "--train8", default=True, type=bool, help="Subset for 8 annotations") args = vars(ap.parse_args()) if args['train8']: n = 8 elif args['train30']: n = 30 if args['model'] == "NaimishNet": net = NaimishNet(n) elif args['model'] == "VggFace": net = VggFace(n) elif args['model'] == "Custom": net = Net2(n) else: net = LeNet5(n) model_name = args['model'] print(net) device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') # order matters! i.e. rescaling should come before a smaller crop train_transform = transforms.Compose([Rescale(110),RandomCrop(96),Albu(),Normalize(args["dataset"]),ToTensor()]) test_transform = transforms.Compose([Normalize(args["dataset"]),ToTensor()]) # testing that you've defined a transform assert(train_transform is not None and test_transform is not None), 'Define a data_transform' # create the transformed dataset
def train(logdir_train1, logdir_train2, logdir_train3): # Load model Net1 for evaluation net1_model = Net1(hp.default.phns_len) checkpoint_path1 = '{}/checkpoint.tar'.format(logdir_train1) checkpoint1 = torch.load(checkpoint_path1) if checkpoint1: net1_model.load_state_dict(checkpoint1['model_state_dict']) # Load model Net2 for evaluation net2_model = Net2() checkpoint_path2 = '{}/checkpoint.tar'.format(logdir_train2) checkpoint2 = torch.load(checkpoint_path2) if checkpoint2: net2_model.load_state_dict(checkpoint2['model_state_dict']) # Load model Net3 for training net3_model = Net3() optimizer = torch.optim.Adam(net3_model.parameters(), lr=hp.train3.lr) checkpoint_path3 = '{}/checkpoint.tar'.format(logdir_train3) checkpoint3 = None if os.path.exists(checkpoint_path3): checkpoint3 = torch.load(checkpoint_path3) epoch = 0 loss = 100.0 lr = hp.train3.lr data_dir = hp.train3.data_path if checkpoint3: logger.info("Reuse existing train_list, eval_list from {}".format( logdir_train3)) net3_model.load_state_dict(checkpoint3['model_state_dict']) optimizer.load_state_dict(checkpoint3['optimizer_state_dict']) lr = optimizer.param_groups[0]['lr'] epoch = checkpoint3['epoch'] loss = checkpoint3['loss'] else: logger.info("Generate new train_list, eval_list, test_list.") net3_model.train() # Set to train mode # Create train/valid loader if hp.train3.multi_speaker: training_set = Net3DataDir(os.path.join(data_dir, 'train', '*'), hp.train3.multi_speaker, k=300) validation_set = Net3DataDir(os.path.join(data_dir, 'eval'), hp.train3.multi_speaker, k=40) else: training_set = Net3DataDir(os.path.join(data_dir, 'train')) validation_set = Net3DataDir(os.path.join(data_dir, 'eval')) training_loader = DataLoader(training_set, batch_size=hp.train3.batch_size, shuffle=True, drop_last=True, num_workers=hp.train3.num_workers) logger.debug("Training loader created. Size: {} samples".format( training_set.size)) validation_loader = DataLoader(validation_set, batch_size=hp.train3.batch_size, shuffle=True, drop_last=True, num_workers=hp.eval3.num_workers) logger.debug("Validation loader created. Size: {}".format( validation_set.size)) # Create criterion criterion = MyMSELoss() logger.debug("Loss type: MSE loss on linear and mel-spectrogram") # Run model net3_model, _, _ = net3_train(checkpoint_path3, net1_model, net2_model, net3_model, training_loader, validation_loader, criterion, starting_epoch=epoch, device=hp.train3.device, lr=lr, loss=loss)
# split baseline data _, _, base_test_set = random_split(baseline_data, [num_training, num_val, num_test], generator=torch.Generator().manual_seed(42)) # split is data _, _, is_test_set = random_split(is_data, [num_training, num_val, num_test], generator=torch.Generator().manual_seed(42)) # create dataloader objects base_test_loader = DataLoader(base_test_set, batch_size=32, shuffle=False) is_test_loader = DataLoader(is_test_set, batch_size=32, shuffle=False) # create models m1 = Net1(name='GCN') m2 = Net2(name='kGNN') m3 = Net3(name='kGNN_TopK') m4 = Net4(name='GAT') baseline_models = [m1, m2, m3, m4] m5 = Net1(name='IS_GCN') m6 = Net2(name='IS_kGNN') m7 = Net3(name='IS_kGNN_TopK') m8 = Net4(name='IS_GAT') is_models = [m5, m6, m7, m8] def eval(log): accuracy, f1_macro, precision, recall = 0, 0, 0, 0 prob_log, label_log = [], []
from torch.utils.data import Dataset, DataLoader from torchvision import transforms, utils, models from data_load import FacialKeypointsDataset from data_load import Normalize, ToTensor from trainer import train_net from validater import val_net idlookup_file = './kaggle/IdLookupTable.csv' def plot_face_pts(img, pts): plt.imshow(img[:, :, 0], cmap='gray') for i in range(1, 31, 2): plt.plot(pts[i - 1], pts[i], 'b.') #load models net_8 = Net2(8) net_8.load_state_dict( torch.load('./saved_models/Kaggle_Custom_1.68410162627697.pt')) net_30 = Net2(30) net_30.load_state_dict( torch.load('./saved_models/Kaggle_Custom_1.6080801971256733.pt')) device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') data_transform = transforms.Compose([Normalize("Kaggle"), ToTensor()]) # testing that you've defined a transform assert (data_transform is not None), 'Define a data_transform' X, y = load_KagggleDataset(test=True) y = np.zeros((X.shape[0], 30))
import torch from models import Net, Net2, Net3, Net4 net = Net(3, 10) t = torch.tensor([1,2,3]) out = net(t.detach()) print(out) net2 = Net2(3, 10) t = torch.tensor([1,2,3]) out = net2(t.detach()) print(out) net3 = Net3(3, 10) t = torch.tensor([1,2,3]) out = net3(t.detach()) print(out) net4 = Net4(3, 10) t = torch.tensor([1,2,3]) out = net4(t.detach()) print(out)
def main(): #------------------------------------------------------------------------------------------------------------------ # Hyperparameters hyp_epochs = 5 hyp_batch_size = 20 #hyp_optim = "SGD" hyp_optim = "Adam" #hyp_net = Net1() hyp_net = Net2() print("Hyperparameters") print("--------------") print("Epochs = ", hyp_epochs) print("Batch Size = ", hyp_batch_size) print("Optimizer = ", hyp_optim) print("--------------") ## TODO: Define the Net in models.py net = hyp_net print(net) ## TODO: define the data_transform using transforms.Compose([all tx's, . , .]) # order matters! i.e. rescaling should come before a smaller crop data_transform = transforms.Compose( [Rescale(256), RandomCrop(224), Normalize(), ToTensor()]) # testing that you've defined a transform assert (data_transform is not None), 'Define a data_transform' # create the transformed dataset transformed_dataset = FacialKeypointsDataset( csv_file='../files/training_frames_keypoints.csv', root_dir='../files/training/', transform=data_transform) print('Number of images: ', len(transformed_dataset)) # iterate through the transformed dataset and print some stats about the first few samples for i in range(4): sample = transformed_dataset[i] print(i, sample['image'].size(), sample['keypoints'].size()) # load training data in batches batch_size = hyp_batch_size train_loader = DataLoader(transformed_dataset, batch_size=batch_size, shuffle=True, num_workers=0) # load in the test data, using the dataset class # AND apply the data_transform you defined above # create the test dataset test_dataset = FacialKeypointsDataset( csv_file='../files/test_frames_keypoints.csv', root_dir='../files/test/', transform=data_transform) # load test data in batches batch_size = hyp_batch_size test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=True, num_workers=0) # test the model on a batch of test images # call the above function # returns: test images, test predicted keypoints, test ground truth keypoints test_images, test_outputs, gt_pts = net_sample_output(test_loader, net) # print out the dimensions of the data to see if they make sense print(test_images.data.size()) print(test_outputs.data.size()) print(gt_pts.size()) # visualize the output # by default this shows a batch of 10 images # call it _visualise = False if _visualise == True: visualize_output(test_images, test_outputs, gt_pts) ## TODO: Define the loss and optimization import torch.optim as optim criterion = nn.MSELoss() hyp_optimizer = None if hyp_optim == "Adam": hyp_optimizer = optim.Adam(net.parameters(), lr=0.001) if hyp_optim == "SGD": hyp_optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) optimizer = hyp_optimizer # train your network n_epochs = hyp_epochs # start small, and increase when you've decided on your model structure and hyperparams # this is a Workspaces-specific context manager to keep the connection # alive while training your model, not part of pytorch train_net(n_epochs, train_loader, net, criterion, optimizer) # get a sample of test data again test_images, test_outputs, gt_pts = net_sample_output(test_loader, net) print(test_images.data.size()) print(test_outputs.data.size()) print(gt_pts.size()) ## TODO: change the name to something uniqe for each new model model_dir = '../saved_models/' model_name = 'keypoints_model_2.pt' # after training, save your model parameters in the dir 'saved_models' torch.save(net.state_dict(), model_dir + model_name) # -------------------------------------------------------------------- # To run the following code after retreiving an existing model, # you can do so in the resume.py file # -------------------------------------------------------------------- # Get the weights in the first conv layer, "conv1" # if necessary, change this to reflect the name of your first conv layer weights1 = net.conv1.weight.data w = weights1.numpy() filter_index = 0 print(w[filter_index][0]) print(w[filter_index][0].shape) # display the filter weights plt.imshow(w[filter_index][0], cmap='gray') ##TODO: load in and display any image from the transformed test dataset i = 1 show_image(test_images, w, i)
def ckpt2mel(predictor, ppgs_dir, mel_dir, save_dir): print("get into ckpt") for fi in os.listdir(ppgs_dir): print("fi",fi) #ppgs_name = os.path.join(ppgs_dir, fi) mel, ppgs = queue_input(fi, ppgs_dir, mel_dir) pred_mel = predictor(mel, ppgs) #print("pred_mel",pred_mel.size()) pred_mel = np.array(pred_mel) print("pred_mel",pred_mel.shape) length = pred_mel.shape[2] width = pred_mel.shape[3] pred_mel = pred_mel.reshape((length, width)) save_name = fi.split('.npy')[0] if hp.default.n_mels == 20: npy_dir = os.path.join(save_dir,'lpc20') if not os.path.exists(npy_dir): os.makedirs(npy_dir) npy_path = os.path.join(npy_dir, '%s_20.npy' %save_name) np.save(npy_path, pred_mel) print('saved',npy_dir)if hp.default.n_mels == 32: npy_dir = os.path.join(save_dir,'lpc32') if not os.path.exists(npy_dir): os.makedirs(npy_dir) npy_path = os.path.join(npy_dir, '%s_32.npy' %save_name) np.save(npy_path, pred_mel) print('saved',npy_dir)def do_convert(args, logdir2): # Load graph model = Net2() index = 0 ppgs_dir = hp.convert.ppgs_path mel_dir = hp.convert.mel_path #for fi in os.listdir(ppgs_dir): #print("fi",fi) #ppgs_path = os.path.join(ppgs_dir, fi) #df = Net2DataFlow(hp.convert.mel_path, ppgs_path, hp.convert.batch_size) #print("finish df") ckpt2 = '{}/{}'.format(logdir2, args.ckpt) if args.ckpt else tf.train.latest_checkpoint(logdir2) print("ckpt2",ckpt2) session_inits = [] if ckpt2: session_inits.append(SaverRestore(ckpt2)) pred_conf = PredictConfig( model=model, input_names=get_eval_input_names(), output_names=get_eval_output_names(), session_init=ChainInit(session_inits)) predictor = OfflinePredictor(pred_conf) print("after predictor") #import pdb;pdb.set_trace() ckpt2mel(predictor, ppgs_dir, mel_dir, hp.convert.save_path) print("success") def get_arguments(): parser = argparse.ArgumentParser() parser.add_argument('case2', type=str, help='experiment case name of train2') parser.add_argument('-ckpt', help='checkpoint to load model.') arguments = parser.parse_args() return arguments if __name__ == '__main__': args = get_arguments() hp.set_hparam_yaml(args.case2) logdir_train2 = '{}/{}/train2'.format(hp.logdir_path, args.case2) print('case2: {},logdir2: {}'.format(args.case2, logdir_train2)) s = datetime.datetime.now() do_convert(args, logdir2=logdir_train2) e = datetime.datetime.now() diff = e - s print("Done. elapsed time:{}s".format(diff.seconds))