def __init__(self, weights_path): """ Initialize C3D model """ c3d_model = C3D(weights_path) layer_name = 'fc6' self.model = Model(inputs=c3d_model.input, outputs=c3d_model.get_layer(layer_name).output)
def __init__(self, weights_path): """ Initialize C3D model """ c3d_model = C3D(weights_path) layer_name = 'fc6' self.model = Model(inputs=c3d_model.input, outputs=c3d_model.get_layer(layer_name).output) print("Number of model parameters: %d" % self.model.count_params())
import skvideo.io from c3d import C3D from sports1M_utils import preprocess_input, decode_predictions model = C3D(weights='sports1M') #VIDEO_PATH='cat.mp4' VIDEO_PATH = 'Ballpark.mp4' vid_path = VIDEO_PATH #vid_path = 'homerun.mp4' ''' return vid ndarray of dimension (T, M, N, C), T is the number of frames M is the height N is width C is depth. (channels or colors) ''' vid = skvideo.io.vread(vid_path) # Select 16 frames from video vid = vid[40:56] x = preprocess_input(vid) preds = model.predict(x) print('Predicted:', decode_predictions(preds)) #Predicted: [('baseball', 0.91488838)]
import torch from numpy import nper from medcam import medcam from c3d import C3D from custom_dataset import ucf_c3d from torch.utils.data import Dataset, DataLoader from vist_util import * model = C3D(num_classes=101,pretrained=True) loadfrom = 'c3d46perc.dict' model.load_state_dict(torch.load(loadfrom)) print(model) full_dataset = ucf_c3d('video_annotations_resnet','G:\\C3D Data', kernels=['original']) dataloader = DataLoader(full_dataset, batch_size=1, shuffle=True, num_workers=0, pin_memory = True) model = medcam.inject(model, output_dir="attention_maps", layer = 'conv5b', backend='gcam', return_attention = True, save_maps=True) model.eval() for batch in dataloader: video_matrix = batch['original'] action_list = batch['action'] score, attention = model(video_matrix) #print(score.shape) attention = torch.squeeze(attention).numpy() video_matrix = torch.squeeze(video_matrix).numpy().astype(np.int)#3,16,112,112 #print(video_matrix) video_matrix = np.transpose(video_matrix, (1,2,3,0)) #print(attention.shape) #print(video_matrix.shape) showbunch(attention) showbunch(video_matrix)
sess = create_session() # TODO test_X = pre.val_X test_y = pre.val_y #model.restore(args.model, sess) #livetest(sess, model, test_X, test_y, # pre.num_labels, args.prob_threshold, args.debug) elif args.task == "pretrain": """Pretrain C3D network.""" from c3d import C3D pre = get_preprocessor(config, args) pre.load_pretrain_data(test_subject) model = C3D(pre, args) train_X = pre.pretrain_X train_y = pre.pretrain_y sess = create_session() if (args.model): model.restore(args.model, sess) # save all variables for now # TODO save only necessary saver = tf.train.Saver() model_save_dir = os.path.join('models', 'C3D', pre.name) if args.debug: model_save_dir = os.path.join(model_save_dir, 'debug')
def main(): device = torch.device("cuda" if torch.cuda.is_available() else "cpu") args = parser.parse_args() set_logger(log_file=args.log_file, debug_mode=args.debug_mode) torch.manual_seed(args.random_seed) torch.cuda.manual_seed(args.random_seed) cudnn.benchmark = True mean = [124 / 255, 117 / 255, 104 / 255] std = [1 / (.0167 * 255)] * 3 normalize = transforms.Normalize(mean=mean, std=std) train_loader = VideoIterTrain( dataset_path=args.dataset_path, annotation_path=args.annotation_path, clip_length=args.clip_length, frame_interval=args.train_frame_interval, video_transform=transforms.Compose([ transforms.Resize((256, 256)), transforms.RandomCrop((224, 224)), transforms.ToTensor(), normalize, ]), name='train', return_item_subpath=False, ) train_iter = torch.utils.data.DataLoader( train_loader, batch_size=args.batch_size, shuffle=False, num_workers=32, # 4, # change this part accordingly pin_memory=True) val_loader = VideoIterTrain( dataset_path=args.dataset_path, annotation_path=args.annotation_path_test, clip_length=args.clip_length, frame_interval=args.val_frame_interval, video_transform=transforms.Compose([ transforms.Resize((256, 256)), transforms.RandomCrop((224, 224)), transforms.ToTensor(), normalize, ]), name='val', return_item_subpath=False, ) val_iter = torch.utils.data.DataLoader( val_loader, batch_size=args.batch_size, shuffle=False, num_workers=32, # 4, # change this part accordingly pin_memory=True) network = C3D(pretrained=args.pretrained_3d) network.to(device) if not path.exists(features_dir): mkdir(features_dir) features_writer = FeaturesWriter() for i_batch, (data, target, sampled_idx, dirs, vid_names) in tqdm(enumerate(train_iter)): data = data.to(device) with torch.no_grad(): input_var = torch.autograd.Variable(data) outputs = network(input_var) for i, (dir, vid_name, start_frame) in enumerate( zip(dirs, vid_names, sampled_idx.cpu().numpy())): dir = path.join(features_dir, dir) features_writer.write(feature=outputs[i], video_name=vid_name, start_frame=start_frame, dir=dir) features_writer.dump() features_writer = FeaturesWriter() for i_batch, (data, target, sampled_idx, dirs, vid_names) in tqdm(enumerate(val_iter)): data = data.to(device) with torch.no_grad(): input_var = torch.autograd.Variable(data) outputs = network(input_var) for i, (dir, vid_name, start_frame) in enumerate( zip(dirs, vid_names, sampled_idx.cpu().numpy())): dir = path.join(features_dir, dir) features_writer.write(feature=outputs[i], video_name=vid_name, start_frame=start_frame, dir=dir) features_writer.dump()
# # TODO # test_X = pre.val_X # test_y = pre.val_y # #model.restore(args.model, sess) # #livetest(sess, model, test_X, test_y, # # pre.num_labels, args.prob_threshold, args.debug) elif args.task == "pretrain": """Pretrain C3D network.""" from c3d import C3D # pre = get_preprocessor(config, args) # pre.load_pretrain_data(test_subject) model = C3D(args) # train_X = pre.pretrain_X # train_y = pre.pretrain_y train_lines = open('train.list', 'r') train_list = list(train_lines) np.random.shuffle(train_list) test_lines = open('test.list', 'r') test_list = list(test_lines) np.random.shuffle(test_list) sess = create_session() #if(args.c3d_model): model.restore(args.c3d_model, sess) # print('restore c3d model') # save all variables for now
def pretrained_c3d() -> torch.nn.Module: c3d = C3D(pretrained=True) c3d.eval() for param in c3d.parameters(): param.requires_grad = False return c3d
def load_model(): global model base_model = C3D(weights='sports1M') model = Model(inputs=base_model.input, outputs=base_model.get_layer('fc6').output)