def main(): cudnn.benchmark = False test_video = Test_video(short_side=[224, 256]) model = slowfastnet.resnet50(class_num=Config.CLASS_NUM) assert Config.LOAD_MODEL_PATH is not None print("load model from:", Config.LOAD_MODEL_PATH) pretrained_dict = torch.load(Config.LOAD_MODEL_PATH, map_location='cpu') try: model_dict = model.module.state_dict() except AttributeError: model_dict = model.state_dict() pretrained_dict = { k: v for k, v in pretrained_dict.items() if k in model_dict } model_dict.update(pretrained_dict) model.load_state_dict(model_dict) model = model.cuda(params['gpu'][0]) validation(model, test_video)
def main(): cudnn.benchmark = False cur_time = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())) logdir = os.path.join(params['log'], cur_time) if not os.path.exists(logdir): os.makedirs(logdir) writer = SummaryWriter(log_dir=logdir) print("Loading dataset") train_dataloader = \ DataLoader( VideoDataset(params['dataset'], mode='train', clip_len=params['clip_len'], frame_sample_rate=params['frame_sample_rate']), batch_size=params['batch_size'], shuffle=True, num_workers=params['num_workers']) val_dataloader = \ DataLoader( VideoDataset(params['dataset'], mode='train', clip_len=params['clip_len'], frame_sample_rate=params['frame_sample_rate']), batch_size=params['batch_size'], shuffle=False, num_workers=params['num_workers']) print("load model") model = slowfastnet.resnet50(class_num=params['num_classes']) if params['pretrained'] is not None: pretrained_dict = torch.load(params['pretrained'], map_location='cpu') try: model_dict = model.module.state_dict() except AttributeError: model_dict = model.state_dict() pretrained_dict = { k: v for k, v in pretrained_dict.items() if k in model_dict } print("load pretrain model") model_dict.update(pretrained_dict) model.load_state_dict(model_dict) model = model.cuda(params['gpu'][0]) model = nn.DataParallel(model, device_ids=params['gpu']) # multi-Gpu criterion = nn.CrossEntropyLoss().cuda() optimizer = optim.SGD(model.parameters(), lr=params['learning_rate'], momentum=params['momentum'], weight_decay=params['weight_decay']) scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=params['step'], gamma=0.1) model_save_dir = os.path.join(params['save_path'], cur_time) if not os.path.exists(model_save_dir): os.makedirs(model_save_dir) for epoch in range(params['epoch_num']): train(model, train_dataloader, epoch, criterion, optimizer, writer) if (epoch + 1) % 5 == 0: validation(model, val_dataloader, epoch, criterion, optimizer, writer) scheduler.step() if epoch % 1 == 0: checkpoint = os.path.join( model_save_dir, "clip_len_" + str(params['clip_len']) + "frame_sample_rate_" + str(params['frame_sample_rate']) + "_checkpoint_" + str(epoch) + ".pth.tar") torch.save(model.module.state_dict(), checkpoint) writer.close()
def main(): # prepare images path cls_name = os.listdir(params["test_video_path"]) videos_name = [] labels = np.empty([0], np.int64) for i, cls in enumerate(cls_name): sub_path = os.path.join(params["test_video_path"], cls) sub_names = os.listdir(sub_path) videos_name += [os.path.join(sub_path, name) for name in sub_names] labels = np.concatenate( (labels, np.ones([len(sub_names)], np.int64) * i), 0) if len(videos_name) == 0: raise Exception("no image found in {}".format( params["test_video_path"])) # Start inference batch_size = 1 model = slowfastnet.resnet50(class_num=params['num_classes']) model.train(False) if params['pretrained'] is not None: pretrained_dict = torch.load(params['pretrained'], map_location='cpu') try: model_dict = model.module.state_dict() except AttributeError: model_dict = model.state_dict() pretrained_dict = { k: v for k, v in pretrained_dict.items() if k in model_dict } print("load pretrain model") model_dict.update(pretrained_dict) model.load_state_dict(model_dict) model = model.cuda(params['gpu'][0]) with torch.no_grad(): def fix_images(img_list, frame_num): img_list = img_list[frame_num:] return img_list top1 = AverageMeter() for i in range(0, len(videos_name)): capture = cv2.VideoCapture(videos_name[i]) frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT)) print('total frame:', frame_count) frame_width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) frame_height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) left = 0 top = 0 if frame_height < frame_width: resize_height = CROP_SIZE resize_width = int( float(resize_height) / frame_height * frame_width) left = (resize_width - CROP_SIZE) // 2 else: resize_width = CROP_SIZE resize_height = int( float(resize_width) / frame_width * frame_height) top = (resize_height - CROP_SIZE) // 2 frame_list = [] trace_torch_model = True while True: ret, frame = capture.read() if ret: # will resize frames if not already final size # if (frame_height != resize_height) or (frame_width != resize_width): # frame = cv2.resize(frame, (resize_width, resize_height)) frame = cv2.resize(frame, (CROP_SIZE, CROP_SIZE)) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) frame = frame.astype(np.float32) # frame = frame[top:top+CROP_SIZE,left:left+CROP_SIZE] frame = (frame / 128.0).astype(np.float32) - np.array( [[[1.0, 1.0, 1.0]]], np.float32) frame_list.append(frame) if len(frame_list) == params['clip_len']: tensor_frame = np.array(frame_list).transpose( (3, 0, 1, 2)) inputs = torch.from_numpy(tensor_frame).cuda() outputs = model(inputs.unsqueeze(0)) # if trace_torch_model: # trace_model = torch.jit.trace(model,inputs.unsqueeze(0)) # pred_out = trace_model(inputs.unsqueeze(0)) # pred_out1 = trace_model(torch.ones_like(inputs).unsqueeze(0).cuda()) # trace_torch_model = False # os.makedirs('./weights',exist_ok=True) # trace_model.save('./weights/fighting_detect_model.pt') prec1, = accuracy(outputs.data, labels[i]) if prec1.item() < 1: print('current presicion ', prec1.item(), 'avg presicon ', top1.avg, 'video name ', videos_name[i]) top1.update(prec1.item(), outputs.size(0)) frame_list = fix_images(frame_list, 1) else: break capture.release() print_string = 'Top-1 accuracy: {top1_acc:.2f}%'.format( top1_acc=top1.avg) print(print_string)
def main(): cudnn.benchmark = False cur_time = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())) logdir = os.path.join(params.log, cur_time) if not os.path.exists(logdir): os.makedirs(logdir) writer = SummaryWriter(log_dir=logdir) print("Loading dataset") train_dataloader = \ DataLoader( VideoDataset(params.dataset, mode='train', clip_len=params.clip_len, frame_sample_rate=params.frame_sample_rate), batch_size=params.batch_size, shuffle=True, num_workers=params.num_workers) val_dataloader = \ DataLoader( VideoDataset(params.dataset, mode='validation', clip_len=params.clip_len, frame_sample_rate=params.frame_sample_rate), batch_size=params.batch_size, shuffle=False, num_workers=params.num_workers) print("load model") model = slowfastnet.resnet50(class_num=params.num_classes) if params.pretrained is not None: pretrained_dict = torch.load(params.pretrained, map_location='cpu') try: model_dict = model.module.state_dict() except AttributeError: model_dict = model.state_dict() pretrained_dict = { k: v for k, v in pretrained_dict.items() if k in model_dict } print("Load pretrain model") model_dict.update(pretrained_dict) model.load_state_dict(model_dict) model = model.cuda(params.gpu[0]) model = nn.DataParallel(model, device_ids=params.gpu) # multi-Gpu criterion = nn.CrossEntropyLoss().cuda(params.gpu[0]) optimizer = optim.SGD(model.parameters(), lr=params.learning_rate, momentum=params.momentum, weight_decay=params.weight_decay) scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=params.step, gamma=0.1) model_save_dir = os.path.join(params.save_path, cur_time) if not os.path.exists(model_save_dir): os.makedirs(model_save_dir) best_loss = 1e6 best_valid = dict(top1_acc=0., top5_acc=0., epoch=0) no_loss_decrease_count = 0 for epoch in range(params.epoch_num): train_top1_acc, train_top5_acc, train_loss = train( model, train_dataloader, epoch, criterion, optimizer, writer) if (epoch + 1) % params.val_freq == 0: val_top1_acc, val_top5_acc, val_loss = validation( model, val_dataloader, epoch, criterion, writer) if val_top1_acc > best_valid['top1_acc']: best_valid['top1_acc'] = val_top1_acc best_valid['top5_acc'] = val_top5_acc best_valid['epoch'] = epoch if train_loss < best_loss: best_loss = train_loss no_loss_decrease_count = 0 else: no_loss_decrease_count += 1 if no_loss_decrease_count >= params.patience: print( f'Early stop on Epoch {epoch} with patience {params.patience}') write_exp_log(f'[{epoch}] Early stop') break scheduler.step() if epoch % 1 == 0: checkpoint = os.path.join( model_save_dir, "clip_len_" + str(params.clip_len) + "frame_sample_rate_" + str(params.frame_sample_rate) + "_checkpoint_" + str(epoch) + ".pth.tar") torch.save(model.module.state_dict(), checkpoint) print( f'Best Validated model was found on epoch {best_valid["epoch"]}: Top1 acc: {best_valid["top1_acc"]} Top5 acc: {best_valid["top5_acc"]}' ) write_exp_log( f'Best model found on epoch {best_valid["epoch"]}: Top1 acc: {best_valid["top1_acc"]} Top5 acc: {best_valid["top5_acc"]}' ) writer.close()
lr=params['learning_rate'], momentum=params['momentum'], weight_decay=params['weight_decay']) scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=params['step'], gamma=0.1) model_save_dir = os.path.join(params['save_path'], cur_time) if not os.path.exists(model_save_dir): os.makedirs(model_save_dir) for epoch in range(params['epoch_num']): train(model, train_dataloader, epoch, criterion, optimizer, writer) if epoch % 2 == 0: validation(model, val_dataloader, epoch, criterion, optimizer, writer) scheduler.step() if epoch % 1 == 0: checkpoint = os.path.join( model_save_dir, "clip_len_" + str(params['clip_len']) + "frame_sample_rate_" + str(params['frame_sample_rate']) + "_checkpoint_" + str(epoch) + ".pth.tar") torch.save(model.module.state_dict(), checkpoint) writer.close() if __name__ == '__main__': model = slowfastnet.resnet50(class_num=params['num_classes']) print(model) main()
def main(): ''' torch.backends.cudnn.benchmark = True cur_time = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())) logdir = os.path.join(params['log'], cur_time) if not os.path.exists(logdir): os.makedirs(logdir) ''' #writer = SummaryWriter(log_dir=logdir) #videos = videos_reales() with open('index.txt', 'w') as f: f.close() print("Loading dataset") data = VideoDataset(params['dataset'], mode='validation', clip_len=64, frame_sample_rate=1) #print('hola') val_dataloader = DataLoader(data, batch_size=1, shuffle=False, num_workers=8) print("load model") model = slowfastnet.resnet50(class_num=params['num_classes']) if params['pretrained'] is not None: pretrained_dict = torch.load(params['pretrained'], map_location='cpu') try: model_dict = model.module.state_dict() except AttributeError: model_dict = model.state_dict() pretrained_dict = { k: v for k, v in pretrained_dict.items() if k in model_dict } print("load pretrain model") model_dict.update(pretrained_dict) model.load_state_dict(model_dict) model = model.cuda(params['gpu'][0]) model = nn.DataParallel(model, device_ids=params['gpu']) # multi-Gpu model.eval() #contadores agregados para guardar video n = 0 m = 0 #esto se puede generalizar recorriendo una lista de las clases y haciendo un for despues de otro dic_predict_1 = dict() dic_predict_1['cellphone'] = 0 dic_predict_1['nothing'] = 0 dic_predict_2 = dict() dic_predict_2['cellphone'] = 0 dic_predict_2['nothing'] = 0 predicciones = dict() predicciones['cellphone'] = dic_predict_1 predicciones['nothing'] = dic_predict_2 end = time.time() with torch.no_grad(): for step, (inputs, labels) in enumerate(val_dataloader): #data_time.update(time.time() - end) #largo batch, canales, numero de imagenes, (112, 112) with open('index.txt', 'r') as f: list_index = f.readlines() f.close() labels_2 = labels inputs_cpu = np.array(inputs) inputs = inputs.cuda() #largo 16 por que hay 16 videos por batch labels = labels.cuda() #videos,clases 16,101, filas,columnas outputs = model(inputs) res, correct_k = accuracy(outputs.data, labels) video_path = data.fnames[int(list_index[n].rstrip('\n'))] capture = cv2.VideoCapture(video_path) frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT)) frame_width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) frame_height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) out_np = outputs.cpu().detach().numpy() index = np.argmax(out_np) ''' if (int(correct_k.cpu()/100) != (index == np.array(labels_2)[0])): print('ESTA MAL') print(int(correct_k.cpu() / 100), index, np.array(labels_2)[0]) ''' #ESTO DEJA AL DICCIONARIO COMO predicciones[lo que realemente es][lo que penso que era] #se puede mejorar leyendo el archivo class_labels.txt para hacerlo general if index == 0: texto = 'cellphone' if np.array(labels_2) == 0: predicciones['cellphone']['cellphone'] += 1 else: predicciones['nothing']['cellphone'] += 1 else: texto = 'nothing' if np.array(labels_2) == 0: predicciones['cellphone']['nothing'] += 1 else: predicciones['nothing']['nothing'] += 1 #print(list_index, outputs) #modificacion para guardar ciertos videos de la validacion y escribir lo que infiere la red vid_writer = cv2.VideoWriter('videos_out/video_' + str(n) + '.avi', cv2.VideoWriter_fourcc(*'MPEG'), 20, (frame_width, frame_height), True) while capture.isOpened(): ret, imag = capture.read() if imag is None: break cv2.putText(imag, texto, (frame_width - 200, frame_height - 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) vid_writer.write(imag.astype(np.uint8)) capture.release() n += 1 print(predicciones)
def main(): cudnn.benchmark = False cur_time = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())) logdir = os.path.join(params['log'], cur_time) if not os.path.exists(logdir): os.makedirs(logdir) writer = SummaryWriter(log_dir=logdir) if params['train_val_Shuffle']: with open(params['tagg_info_txt'], 'r', encoding='utf-8') as f: lines = f.readlines() random.shuffle(lines) train_nums = 0.9 * len(lines) out_file_train = open(params['filename_imglist_train'], 'w', encoding='utf-8') out_file_val = open(params['filename_imglist_val'], 'w', encoding='utf-8') try: for index, line in tqdm.tqdm(enumerate(lines), total=len(lines)): out_file = out_file_train if index < train_nums else out_file_val line = line.strip() if len(line.split('\t')) != 2: return path, label_strings = line.split('\t') vid = os.path.basename(path) out_file.write(vid + '\t' + label_strings + '\n') except Exception as e: print(e) else: if os.path.exists(params['filename_imglist_train']) and os.path.exists( params['filename_imglist_val']): print("loading existing train and val splits!!") else: return 0 print("Loading dataset") train_dataloader = \ DataLoader( VideoDataset(params['dataset'], params['filename_imglist_train'], params['label_id_file'], mode='train', clip_len=params['clip_len'], frame_sample_rate=params['frame_sample_rate']), batch_size=params['batch_size'], shuffle=True, num_workers=params['num_workers']) val_dataloader = \ DataLoader( VideoDataset(params['dataset'], params['filename_imglist_val'],params['label_id_file'], mode='validation', clip_len=params['clip_len'], frame_sample_rate=params['frame_sample_rate']), batch_size=params['batch_size'], shuffle=False, num_workers=params['num_workers']) print("load model") model = slowfastnet.resnet50(class_num=params['num_classes']) if params['pretrained'] is not None: pretrained_dict = torch.load(params['pretrained'], map_location='cpu') try: model_dict = model.module.state_dict() except AttributeError: model_dict = model.state_dict() pretrained_dict = { k: v for k, v in pretrained_dict.items() if k in model_dict } print("load pretrain model") model_dict.update(pretrained_dict) model.load_state_dict(model_dict) model = model.cuda(params['gpu'][0]) model = nn.DataParallel(model, device_ids=params['gpu']) # multi-Gpu criterion = loss.CrossEntropyLoss() optimizer = optim.SGD(model.parameters(), lr=params['learning_rate'], momentum=params['momentum'], weight_decay=params['weight_decay']) scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=params['step'], gamma=0.1) model_save_dir = os.path.join(params['save_path'], cur_time) if not os.path.exists(model_save_dir): os.makedirs(model_save_dir) gamma = params['gamma'] for epoch in range(params['epoch_num']): if params['train_rule'] == 'DRW': idx = epoch // 30 # idx = 1 betas = [0, 0.99] cls_num_list = [100] * params['num_classes'] effective_num = 1.0 - np.power(betas[idx], cls_num_list) per_cls_weights = (1.0 - betas[idx]) / np.array(effective_num) per_cls_weights = per_cls_weights / np.sum(per_cls_weights) * len( cls_num_list) per_cls_weights = torch.FloatTensor(per_cls_weights) else: per_cls_weights = torch.ones(1) print(f"per_cls_weights{per_cls_weights}") train(train_dataloader, model, criterion, optimizer, epoch, per_cls_weights, gamma, writer) if epoch % 2 == 0: gap = validate2(val_dataloader, model, criterion, epoch, tfwriter=writer) is_best = gap > best_gap best_gap = max(gap, best_gap) writer.add_scalar('acc/val_gap_best', best_gap, epoch) output_best = 'Best val gap: %.3f\n' % (best_gap) print(output_best) if is_best and gap > 0.75: checkpoint = os.path.join( model_save_dir, "clip_len_" + str(params['clip_len']) + "frame_sample_rate_" + str(params['frame_sample_rate']) + "_checkpoint_" + str(epoch) + "_gap" + str(gap) + ".pth.tar") torch.save(model.module.state_dict(), checkpoint) scheduler.step() writer.close