def parse_args(): global DEBUG parser = ap.ArgumentParser() parser.add_argument("filepath", help="path to input audio file") parser.add_argument("trans_id", help="id in the database") parser.add_argument("-i", "--indir", help="directory containing input audio file, if filepath does not contain path") parser.add_argument("-d", "--debug", help="enble debugging output", type=bool) parser.add_argument("-s", "--sumpath", help="path to summary file. Can either be file or dir. If dir, summary file stored in dir/basefilename.txt") parser.add_argument("-f", "--segdir", help="directory to store temporary audio seg/frag-ment files") args = parser.parse_args() cfg_params = ut.get_cfg_params() filepath="" sumpath="" segdir="" #Get the input file path #FIX: Optimization, if os.path.isfile(args.filepath): filepath = args.filepath drct, fl = ut.split_path(args.filepath) if not drct: if args.indir and not os.path.isdir(args.indir): print "Invalid input directory specified" raise Exception if not fl: raise Exception print "Invalid input file specified" filepath = ut.dir_path( drct or args.indir or cfg_params["in_dir"] ) + fl if not os.path.exists(filepath): print "Input file not found. Filepath is {}".format(filepath) raise Exception #Get summary file path if args.sumpath: drct, fl = ut.split_path(args.sumpath) if drct and fl: sumpath = args.sumpath elif drct: sumpath = ut.dir_path(drct) + ut.base_filename(filepath) elif fl: sumpath = ut.dir_path(cfg_params["summary_dir"]) + fl else: print "Invalid summary path specified" raise Exception else: sumpath = ut.dir_path(cfg_params["summary_dir"]) + ut.base_filename(filepath) #Append extension if sumpath[-5:] != ".smry" : sumpath += ".smry" #Get segment directory path if args.segdir and not os.path.isdir(args.segdir): print "Invalid segment file directory specified" raise Exception segdir = ut.dir_path(args.segdir or cfg_params["seg_file_dir"]) #Set Debug flag if args.debug: DEBUG = args.debug return (filepath, sumpath, segdir)
def transcribe(filepath, sumpath, segdir): if DEBUG: print "filepath= {}, sumpath= {}, segdir= {}".format(filepath, sumpath, segdir) if DEBUG: print "File length = {}".format(ut.file_length(filepath)) #Writes all segment files to segdir #print time.time() - start_time, "seconds; starting prepro" seg_count = pp.prepro2(filepath, segdir) #print time.time() - start_time, "seconds; ending prepro" basefile = ut.base_filename(filepath) if DEBUG: print "basefile is {}".format(basefile) sf = open(sumpath, "w") #Consider using #while os.path.exists(opath): segfiles = (ut.dir_path(segdir) + basefile + "__" + str(i) + ".flac" for i in range(seg_count)) #print time.time() - start_time, "seconds; starting transcribe" trans_list = gt.google_transcribe(segfiles) #print time.time() - start_time, "seconds; ending transcribe" if DEBUG: print trans_list db_data = "" for t in trans_list: sf.write(t + "\n") db_data += str(t) + "\n" """ for seg in segfiles: if DEBUG: print "segment path is {}".format(seg) trans = gs.key_trans(seg) sf.write(trans + "\n") if DEBUG: print(trans) trans_list.append(trans) """ sf.close() ut.remove_seg_files(segdir, basefile + "__*.flac") #Trans_list is a list of lines that are transcribed return db_data
def transcribe(filepath, sumpath, segdir): if DEBUG: print "filepath= {}, sumpath= {}, segdir= {}".format(filepath, sumpath, segdir) if DEBUG: print "File length = {}".format(ut.file_length(filepath)) #Writes all segment files to segdir seg_count = pp.prepro2(filepath, segdir) basefile = ut.base_filename(filepath) if DEBUG: print "basefile is {}".format(basefile) trans_list = [] sf = open(sumpath, "w") #Consider using #while os.path.exists(opath): segfiles = (ut.dir_path(segdir) + basefile + "__" + str(i) + ".flac" for i in range(seg_count)) db_data = "" for seg in segfiles: if DEBUG: print "segment path is {}".format(seg) trans = gs.key_trans(seg) print "trans is: " + trans sf.write(trans + "\n") db_data += str(trans) + "\n" if DEBUG: print(trans) trans_list.append(trans) sf.close() ut.remove_seg_files(segdir, basefile + "__*.flac") return db_data
annotation_test = os.path.join(heatmap_root, "heatmap_annotation_test.txt") # load data dataset_train = HeatmapDataset(heatmap_root, annotation_train) dataset_test = HeatmapDataset(heatmap_root, annotation_test) train_loader = DataLoader(dataset_train, batch_size=config['batch_size'], num_workers=4, pin_memory=True) test_loader = DataLoader(dataset_test, batch_size=config['batch_size'], num_workers=4, pin_memory=True) # log path path = dir_path("sensor_heatmap_3dcnn_fusion_attention_SpatialSELayer3D", result_dir) # create model model = C3DFusionAttention(sample_duration=config['h_num_frames'], num_classes=config['num_classes']) model = model.to(device) # initialize critierion and optimizer # could add weighted loss e.g. pos_weight = torch.ones([64]) # criterion = nn.BCELoss() criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=config['lr']) lr_scheduler = torch.optim.lr_scheduler.StepLR( optimizer, step_size=config['lr_step_size'],
"submission_%s_%s.json" % (test_or_val, submission_text), 'w')) json.dump(ranks_json, open("result.json", 'w')) return best_mrr, best_ndcg if __name__ == '__main__': ''' Init Part ''' # Parser from visdial utils parser = get_parser() args = parser.parse_args() args.model_pathname = args.model_pathname print(args) mydir = dir_path(args) output_file = None # Finding vocabulary size visdial_meta = json.load(open('data/visdial_params.json', 'r')) if "word2ind" in visdial_meta.keys(): word2ind = visdial_meta["word2ind"] vocab_size = len(word2ind.values( )) + 1 # Zero is not in word2ind but appears in question/answers. else: sys.exit("Check the json file. It doesn't have 'word2ind' dictionary.") args.stop_id = len(word2ind.values()) + 1 args.empty_id = len(word2ind.values()) + 2
lr_decay_gamma=0.2, batch_size=16, num_classes=7, v_num_frames=30, h_num_frames=100, imag_size=224, # weight_alpha=0.7, # softmax_temperature=16.0, # loss_mode='cse' lambda_kd=1, loss_margin=0.1, ) # results dir result_dir = "FER/results" path = dir_path("C3D_Supervision_v3", result_dir) # save training config save_to_json(config, path['config']) # load data videos_root = 'C:\\Users\\Zber\\Desktop\\Subjects_Frames\\' v_train_ann = os.path.join(videos_root, 'annotations_att_train.txt') v_test_ann = os.path.join(videos_root, 'annotations_att_test.txt') heatmap_root = "C:/Users/Zber/Desktop/Subjects_Heatmap" h_train_ann = os.path.join(heatmap_root, "heatmap_annotation_train.txt") h_test_ann = os.path.join(heatmap_root, "heatmap_annotation_test.txt") preprocess = transforms.Compose([ ImglistToTensor(
# x = x.transpose((2, 0, 1)) # preprocessing data to min-max scale x = scale_range(x) # split data x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=25, stratify=y) train_loader = data_loader(x_train, y_train, batch_size=BATCH_SIZE) test_loader = data_loader(x_test, y_test, batch_size=BATCH_SIZE) # log path path = dir_path("sensor_lstm", result_dir) # model parameters input_size, hidden_size, num_classes = 36, 64, 6 model = mmwave_lstm(input_size, hidden_size, BATCH_SIZE, num_classes=num_classes) # create model model = model.to(device) # initialize critierion and optimizer # could add weighted loss e.g. pos_weight = torch.ones([64]) # criterion = nn.BCELoss() criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=LR)
# heatmap root dir heatmap_root = "C:/Users/Zber/Desktop/Subjects_Heatmap" # annotation dir annotation_train = os.path.join(heatmap_root, "heatmap_annotation_train.txt") annotation_test = os.path.join(heatmap_root, "heatmap_annotation_test.txt") # load data dataset_train = HeatmapDataset(heatmap_root, annotation_train) dataset_test = HeatmapDataset(heatmap_root, annotation_test) train_loader = DataLoader(dataset_train, batch_size=config['batch_size'], num_workers=4, pin_memory=True) test_loader = DataLoader(dataset_test, batch_size=config['batch_size'], num_workers=4, pin_memory=True) # log path path = dir_path("sensor_heatmap_3dcnn_fusion_baseline_crop(20-70)(3-8)_v2", result_dir) # create model model = C3DFusionBaseline_small(sample_duration=config['h_num_frames'], num_classes=config['num_classes']) model = model.to(device) # initialize critierion and optimizer # could add weighted loss e.g. pos_weight = torch.ones([64]) # criterion = nn.BCELoss() criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=config['lr']) lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=config['lr_step_size'], gamma=config['lr_decay_gamma']) metrics_dic = {
annotation_test = os.path.join(heatmap_root, "heatmap_annotation_test.txt") # load data dataset_train = HeatmapDataset(heatmap_root, annotation_train) dataset_test = HeatmapDataset(heatmap_root, annotation_test) train_loader = DataLoader(dataset_train, batch_size=config['batch_size'], num_workers=4, pin_memory=True) test_loader = DataLoader(dataset_test, batch_size=config['batch_size'], num_workers=4, pin_memory=True) # log path path = dir_path("sensor_heatmap_3dcnn_mmtm_v2", result_dir) # create model model = C3DMMTM_v2(sample_duration=config['h_num_frames'], num_classes=config['num_classes']) model = model.to(device) # initialize critierion and optimizer # could add weighted loss e.g. pos_weight = torch.ones([64]) # criterion = nn.BCELoss() criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=config['lr']) lr_scheduler = torch.optim.lr_scheduler.StepLR( optimizer, step_size=config['lr_step_size'],
def parse_args(fpath=None, trans_id=None, idr=None, dbg=None, spath=None, sgdr=None): global DEBUG parser = ap.ArgumentParser() if not fpath: parser.add_argument("filepath", help="path to input audio file") if not trans_id: parser.add_argument("trans_id", help="id in the database") if not idr: parser.add_argument("-i", "--indir", help="directory containing input audio file, if filepath does not contain path") if not dbg: parser.add_argument("-d", "--debug", help="enble debugging output", type=bool) if not spath: parser.add_argument("-s", "--sumpath", help="path to summary file. Can either be file or dir. If dir, summary file stored in dir/basefilename.txt") if not sgdr: parser.add_argument("-f", "--segdir", help="directory to store temporary audio seg/frag-ment files") args = parser.parse_args() cfg_params = ut.get_cfg_params() filepath="" sumpath="" segdir="" directory="" #Get the input file path if fpath and os.path.isfile(fpath): filepath = fpath else: drct, fl = ut.split_path(fpath if fpath else args.filepath) if not fl: print "Invalid input file: '{}'".format(drct+"/"+fl) raise Exception if not drct: directory = idr or args.indir or cfg_params["in_dir"] if not os.path.isdir(directory): print "Invalid input directory '{}'".format(directory) raise Exception filepath = ut.dir_path(directory) + fl else: filepath = ut.dir_path(drct) + fl if not os.path.exists(filepath): print "Input file not found. Filepath is {}".format(filepath) raise Exception #Get summary file path sumpath = spath or args.sumpath if sumpath: drct, fl = ut.split_path(sumpath) directory = drct if drct and fl: pass elif drct: sumpath = ut.dir_path(drct) + ut.base_filename(filepath) elif fl: directory=ut.dir_path(cfg_params["summary_dir"]) sumpath = directory + fl else: #TODO: Check if this is necessary print "Invalid summary path {}".format(sumpath) raise Exception else: directory = ut.dir_path(cfg_params["summary_dir"]) sumpath = directory + ut.base_filename(filepath) #Append extension if sumpath[-5:] != ".smry" : sumpath += ".smry" #If dir does not exist, create it if not os.path.exists(directory): if DEBUG: print "Creating dir {}".format(directory) os.makedirs(directory) #Get segment directory path segdir = sgdr or args.segdir or cfg_params["seg_file_dir"] if not os.path.exists(segdir): if DEBUG: print "Creating dir {}".format(segdir) os.makedirs(segdir) #Set Debug flag if args.debug: DEBUG = args.debug return (filepath, sumpath, segdir)
# load data dataset_train = HeatmapDataset(heatmap_root, annotation_train) dataset_test = HeatmapDataset(heatmap_root, annotation_test) train_loader = DataLoader(dataset_train, batch_size=config['batch_size'], num_workers=4, pin_memory=True) test_loader = DataLoader(dataset_test, batch_size=config['batch_size'], num_workers=4, pin_memory=True) # log path path = dir_path( "sensor_heatmap_3dcnn_fusion_baseline_full_size_full_length", result_dir) # create model model = C3DFusionBaselineFull(sample_duration=config['h_num_frames'], num_classes=config['num_classes']) model = model.to(device) # initialize critierion and optimizer # could add weighted loss e.g. pos_weight = torch.ones([64]) # criterion = nn.BCELoss() criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=config['lr']) lr_scheduler = torch.optim.lr_scheduler.StepLR( optimizer,
if __name__ == "__main__": config = dict(num_epochs=60, lr=0.0003, lr_step_size=30, lr_decay_gamma=0.2, batch_size=16, num_classes=7, v_num_frames=30, h_num_frames=100, imag_size=224) # results dir result_dir = "FER/results" path = dir_path("C3D_Video_V3", result_dir) # save training config save_to_json(config, path['config']) # data path and annotation files videos_root = 'C:\\Users\\Zber\\Desktop\\Subjects_Frames\\' annotation_train = os.path.join(videos_root, 'annotations_att_train.txt') annotation_test = os.path.join(videos_root, 'annotations_att_test.txt') # dataloader preprocess = transforms.Compose([ ImglistToTensor( ), # list of PIL images to (FRAMES x CHANNELS x HEIGHT x WIDTH) tensor transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
df_x = np.load('//data/emotion_3s_diff_x.npy') df_y = np.load('//data/emotion_3s_diff_y.npy') # normalization df_x = normalise_data(df_x) df_x = np.expand_dims(df_x, axis=2) # df_y_eye = np_to_eye(df_y, num_class=7) # split data x_train, x_test, y_train, y_test = train_test_split(df_x, df_y, test_size=0.2, random_state=25, stratify=df_y) train_loader = data_loader(x_train, y_train, batch_size=BATCH_SIZE) test_loader = data_loader(x_test, y_test, batch_size=BATCH_SIZE) # log path path = dir_path("emotion_3s_diff_segment", result_dir) # create model model = LeNet(**model_para) model = model.to(device) # initialize critierion and optimizer criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=LR) for epoch in range(N_EPOCHS): train_loss = train(model, data_loader=train_loader, criterion=criterion, optimizer=optimizer, epoch=epoch, to_log=path['log']) test_loss, acc = test(model, test_loader=test_loader, criterion=criterion, to_log=path['log'])
config = dict(num_epochs=60, lr=0.001, lr_step_size=20, lr_decay_gamma=0.2, batch_size=16, num_classes=7, v_num_frames=30, h_num_frames=100, imag_size=224, lambda_kd=1, delta=0.003 ) # results dir result_dir = "FER/results" path = dir_path("C3D_Supervision_Autoencoder", result_dir) # save training config save_to_json(config, path['config']) # load data videos_root = 'C:\\Users\\Zber\\Desktop\\Subjects_Frames\\' v_train_ann = os.path.join(videos_root, 'annotations_att_train.txt') v_test_ann = os.path.join(videos_root, 'annotations_att_test.txt') heatmap_root = "C:/Users/Zber/Desktop/Subjects_Heatmap" h_train_ann = os.path.join(heatmap_root, "heatmap_annotation_train.txt") h_test_ann = os.path.join(heatmap_root, "heatmap_annotation_test.txt") preprocess = transforms.Compose([ ImglistToTensor(), # list of PIL images to (FRAMES x CHANNELS x HEIGHT x WIDTH) tensor
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=25, stratify=y) train_loader = sensor_imag_data_loader(x_train, y_train, batch_size=BATCH_SIZE) test_loader = sensor_imag_data_loader(x_test, y_test, batch_size=np.shape(x_test)[0]) # log path path = dir_path( "vision_landmark_PHRNN_alignment_landmark_normalized_nobias", result_dir) # create model model = PHRNN(**model_config) model = model.to(device) # initialize critierion and optimizer # could add weighted loss e.g. pos_weight = torch.ones([64]) # criterion = nn.BCELoss() criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=LR) lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=50, gamma=0.2)
# heatmap root dir heatmap_root = "C:/Users/Zber/Desktop/Subjects_Heatmap" # annotation dir annotation_train = os.path.join(heatmap_root, "heatmap_annotation_train.txt") annotation_test = os.path.join(heatmap_root, "heatmap_annotation_test.txt") # load data dataset_train = HeatmapDataset(heatmap_root, annotation_train) dataset_test = HeatmapDataset(heatmap_root, annotation_test) train_loader = DataLoader(dataset_train, batch_size=config['batch_size'], num_workers=4, pin_memory=True) test_loader = DataLoader(dataset_test, batch_size=config['batch_size'], num_workers=4, pin_memory=True) # log path path = dir_path("sensor_heatmap_ConvLSTM", result_dir) # create model model = ConvLSTMFull(input_dim=1, hidden_dim=[2, 4, 8], kernel_size=(7, 3), num_layers=3, num_classes=config['num_classes'], batch_first=True, bias=True, return_all_layers=False) model = model.to(device) # initialize critierion and optimizer # could add weighted loss e.g. pos_weight = torch.ones([64]) # criterion = nn.BCELoss()
annotation_test = os.path.join(heatmap_root, "heatmap_annotation_test.txt") # load data dataset_train = HeatmapDataset(heatmap_root, annotation_train) dataset_test = HeatmapDataset(heatmap_root, annotation_test) train_loader = DataLoader(dataset_train, batch_size=config['batch_size'], num_workers=4, pin_memory=True) test_loader = DataLoader(dataset_test, batch_size=config['batch_size'], num_workers=4, pin_memory=True) # log path path = dir_path("sensor_heatmap_TSNet_v2", result_dir) # create model model = TimeSpaceNet(num_classes=config['num_classes']) model = model.to(device) # initialize critierion and optimizer # could add weighted loss e.g. pos_weight = torch.ones([64]) # criterion = nn.BCELoss() criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=config['lr']) lr_scheduler = torch.optim.lr_scheduler.StepLR( optimizer, step_size=config['lr_step_size'], gamma=config['lr_decay_gamma'])
lr_decay_gamma=0.2, batch_size=16, num_classes=7, v_num_frames=30, h_num_frames=100, imag_size=224, # weight_alpha=0.7, # softmax_temperature=16.0, # loss_mode='cse' lambda_kd=1, loss_margin=0.1, ) # results dir result_dir = "FER/results" path = dir_path("C3D_TF_autoencoder_v2", result_dir) # save training config save_to_json(config, path['config']) # load data videos_root = 'C:\\Users\\Zber\\Desktop\\Subjects_Frames\\' v_train_ann = os.path.join(videos_root, 'annotations_att_train.txt') v_test_ann = os.path.join(videos_root, 'annotations_att_test.txt') heatmap_root = "C:/Users/Zber/Desktop/Subjects_Heatmap" h_train_ann = os.path.join(heatmap_root, "heatmap_annotation_train.txt") h_test_ann = os.path.join(heatmap_root, "heatmap_annotation_test.txt") preprocess = transforms.Compose([ ImglistToTensor(), # list of PIL images to (FRAMES x CHANNELS x HEIGHT x WIDTH) tensor
phase_train = PhaseDataset(phase_root, annotation_train) phase_test = PhaseDataset(phase_root, annotation_test) dataset_train = ConcatDataset(heatmap_train, phase_train) dataset_test = ConcatDataset(heatmap_test, phase_test) train_loader = DataLoader(dataset_train, batch_size=config['batch_size'], num_workers=4, pin_memory=True) test_loader = DataLoader(dataset_test, batch_size=config['batch_size'], num_workers=4, pin_memory=True) # log path path = dir_path("sensor_heatmap_3dcnn_fusion_heatmap&phase", result_dir) # create model model = HeatmapPhaseNet(sample_duration=config['h_num_frames'], num_classes=config['num_classes']) model = model.to(device) # initialize critierion and optimizer # could add weighted loss e.g. pos_weight = torch.ones([64]) # criterion = nn.BCELoss() criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=config['lr']) lr_scheduler = torch.optim.lr_scheduler.StepLR( optimizer, step_size=config['lr_step_size'],
return (inverse_normalize(video_tensor) * 255.).type(torch.uint8).permute( 0, 2, 3, 1).numpy() if __name__ == "__main__": N_EPOCHS = 50 LR = 0.0003 BATCH_SIZE = 16 num_classes = 7 num_frames = 30 imag_size = 224 # results dir result_dir = "FER/results" path = dir_path("C3D_Video_att", result_dir) # load data videos_root = 'C:\\Users\\Zber\\Desktop\\Subjects_Frames\\' annotation_train = os.path.join(videos_root, 'annotations_att_train.txt') annotation_test = os.path.join(videos_root, 'annotations_att_test.txt') preprocess = transforms.Compose([ ImglistToTensor( ), # list of PIL images to (FRAMES x CHANNELS x HEIGHT x WIDTH) tensor transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ]) dataset_train = VideoFrameDataset(root_path=videos_root, annotationfile_path=annotation_train,
azi = normalise_data(azi) ele = normalise_data(ele) # split data azi_train, azi_test, ele_train, ele_test, label_train, label_test = train_test_split( azi, ele, label, test_size=0.2, random_state=25, stratify=label) train_loader = senor_heatmap_label_data_loader(azi_train, ele_train, label_train, batch_size=BATCH_SIZE) test_loader = senor_heatmap_label_data_loader( azi_test, ele_test, label_test, batch_size=np.shape(azi_test)[0]) # log path path = dir_path("sensor_heatmap_3dcnn", result_dir) # create model model = C3D(sample_duration=num_frames, num_classes=num_classes) model = model.to(device) # initialize critierion and optimizer # could add weighted loss e.g. pos_weight = torch.ones([64]) # criterion = nn.BCELoss() criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=LR) lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.2)
# heatmap root dir heatmap_root = "C:/Users/Zber/Desktop/Subjects_Heatmap" # annotation dir annotation_train = os.path.join(heatmap_root, "heatmap_annotation_train.txt") annotation_test = os.path.join(heatmap_root, "heatmap_annotation_test.txt") # load data dataset_train = HeatmapDataset(heatmap_root, annotation_train) dataset_test = HeatmapDataset(heatmap_root, annotation_test) train_loader = DataLoader(dataset_train, batch_size=config['batch_size'], num_workers=4, pin_memory=True) test_loader = DataLoader(dataset_test, batch_size=config['batch_size'], num_workers=4, pin_memory=True) # log path path = dir_path("sensor_heatmap_3dcnn_fusion_v2.1", result_dir) # create model model = C3DFusionV2(sample_duration=config['h_num_frames'], num_classes=config['num_classes']) model = model.to(device) # initialize critierion and optimizer # could add weighted loss e.g. pos_weight = torch.ones([64]) # criterion = nn.BCELoss() criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=config['lr']) lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=config['lr_step_size'], gamma=config['lr_decay_gamma']) metrics_dic = { 'loss': [],
# preprocessing data to min-max scale x = scale_range(x) # split data x_train, x_test, y_train, y_test, label_train, label_test = train_test_split( x, y, label, test_size=0.1, random_state=25, stratify=label) train_loader = sensor_imag_data_loader(x_train, y_train, batch_size=BATCH_SIZE) test_loader = sensor_imag_data_loader(x_test, y_test, batch_size=np.shape(x_test)[0]) # log path path = dir_path("sensor_imag_autoencoder", result_dir) # create model model = EMONet(n_class=np.shape(y)[1]) model = model.to(device) # initialize critierion and optimizer # could add weighted loss e.g. pos_weight = torch.ones([64]) # criterion = nn.BCELoss() # loss_fn to replace this criterion criterion = nn.MSELoss() optimizer = torch.optim.Adam(model.parameters(), lr=LR) metrics_dic = {'loss': [], 'precision': [], 'recall': []}
phase_train = PhaseDataset(phase_root, annotation_train) phase_test = PhaseDataset(phase_root, annotation_test) dataset_train = ConcatDataset(heatmap_train, phase_train) dataset_test = ConcatDataset(heatmap_test, phase_test) train_loader = DataLoader(dataset_train, batch_size=config['batch_size'], num_workers=4, pin_memory=True) test_loader = DataLoader(dataset_test, batch_size=config['batch_size'], num_workers=4, pin_memory=True) # log path path = dir_path("sensor_heatmap_conv2d_heatmap&phase", result_dir) # create model model = ImagePhaseNet(num_classes=config['num_classes']) model = model.to(device) # initialize critierion and optimizer # could add weighted loss e.g. pos_weight = torch.ones([64]) # criterion = nn.BCELoss() criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=config['lr']) lr_scheduler = torch.optim.lr_scheduler.StepLR( optimizer, step_size=config['lr_step_size'], gamma=config['lr_decay_gamma'])
x, y, label = format_dataset(sensor, imag, label) x = np.expand_dims(x, axis=2) # y = np.expand_dims(y, axis=2) # preprocessing data to min-max scale x = scale_range(x) y = scale_range(y) # split data x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=25, stratify=label) train_loader = sensor_imag_data_loader(x_train, y_train, batch_size=BATCH_SIZE) test_loader = sensor_imag_data_loader(x_test, y_test, batch_size=BATCH_SIZE) # log path path = dir_path("sensor_imag_basic", result_dir) # create model model = Autoencoder() model = model.to(device) model.load_state_dict(torch.load(model_path)) # initialize critierion and optimizer # could add weighted loss e.g. pos_weight = torch.ones([64]) criterion = nn.BCELoss() optimizer = torch.optim.Adam(model.parameters(), lr=LR) # test test_loss, acc = test(model, test_loader=test_loader, criterion=criterion, to_log=path['log'])
annotation_test = os.path.join(heatmap_root, "heatmap_annotation_test.txt") # load data dataset_train = HeatmapDataset(heatmap_root, annotation_train) dataset_test = HeatmapDataset(heatmap_root, annotation_test) train_loader = DataLoader(dataset_train, batch_size=config['batch_size'], num_workers=4, pin_memory=True) test_loader = DataLoader(dataset_test, batch_size=config['batch_size'], num_workers=4, pin_memory=True) # log path path = dir_path("sensor_heatmap_3dcnn_fusion_baseline_diff", result_dir) # create model model = C3DFusionBaseline(sample_duration=config['h_num_frames'], num_classes=config['num_classes']) model = model.to(device) criterion = nn.CrossEntropyLoss() # test dir = "C:/Users/Zber/Documents/Dev_program/OpenRadar/FER/results/sensor_heatmap_3dcnn_fusion_baseline_20211216-185624" evaluate(model, dir, test_loader) # initialize critierion and optimizer # could add weighted loss e.g. pos_weight = torch.ones([64]) # criterion = nn.BCELoss()
annotation_test = os.path.join(heatmap_root, "heatmap_annotation_test.txt") # load data dataset_train = HeatmapDataset(heatmap_root, annotation_train) dataset_test = HeatmapDataset(heatmap_root, annotation_test) train_loader = DataLoader(dataset_train, batch_size=config['batch_size'], num_workers=4, pin_memory=True) test_loader = DataLoader(dataset_test, batch_size=config['batch_size'], num_workers=4, pin_memory=True) # log path path = dir_path("sensor_heatmap_conv2d_timedistributed", result_dir) # create model model = ImageFull(num_classes=config['num_classes']) model = model.to(device) # initialize critierion and optimizer # could add weighted loss e.g. pos_weight = torch.ones([64]) # criterion = nn.BCELoss() criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=config['lr']) lr_scheduler = torch.optim.lr_scheduler.StepLR( optimizer, step_size=config['lr_step_size'], gamma=config['lr_decay_gamma'])